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)
1050 target->working_area = target->working_area_virt;
1054 target->working_area = target->working_area_phys;
1058 /* only allocate multiples of 4 byte */
1061 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
1062 size = (size + 3) & (~3);
1065 /* see if there's already a matching working area */
1068 if ((c->free) && (c->size == size))
1076 /* if not, allocate a new one */
1079 working_area_t **p = &target->working_areas;
1080 uint32_t first_free = target->working_area;
1081 uint32_t free_size = target->working_area_size;
1083 LOG_DEBUG("allocating new working area");
1085 c = target->working_areas;
1088 first_free += c->size;
1089 free_size -= c->size;
1094 if (free_size < size)
1096 LOG_WARNING("not enough working area available(requested %u, free %u)",
1097 (unsigned)(size), (unsigned)(free_size));
1098 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1101 new_wa = malloc(sizeof(working_area_t));
1102 new_wa->next = NULL;
1103 new_wa->size = size;
1104 new_wa->address = first_free;
1106 if (target->backup_working_area)
1109 new_wa->backup = malloc(new_wa->size);
1110 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1112 free(new_wa->backup);
1119 new_wa->backup = NULL;
1122 /* put new entry in list */
1126 /* mark as used, and return the new (reused) area */
1131 new_wa->user = area;
1136 int target_free_working_area_restore(struct target_s *target, working_area_t *area, int restore)
1141 if (restore && target->backup_working_area)
1144 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1150 /* mark user pointer invalid */
1157 int target_free_working_area(struct target_s *target, working_area_t *area)
1159 return target_free_working_area_restore(target, area, 1);
1162 /* free resources and restore memory, if restoring memory fails,
1163 * free up resources anyway
1165 void target_free_all_working_areas_restore(struct target_s *target, int restore)
1167 working_area_t *c = target->working_areas;
1171 working_area_t *next = c->next;
1172 target_free_working_area_restore(target, c, restore);
1182 target->working_areas = NULL;
1185 void target_free_all_working_areas(struct target_s *target)
1187 target_free_all_working_areas_restore(target, 1);
1190 int target_register_commands(struct command_context_s *cmd_ctx)
1193 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)");
1198 register_jim(cmd_ctx, "target", jim_target, "configure target");
1203 int target_arch_state(struct target_s *target)
1208 LOG_USER("No target has been configured");
1212 LOG_USER("target state: %s", target_state_name( target ));
1214 if (target->state != TARGET_HALTED)
1217 retval = target->type->arch_state(target);
1221 /* Single aligned words are guaranteed to use 16 or 32 bit access
1222 * mode respectively, otherwise data is handled as quickly as
1225 int target_write_buffer(struct target_s *target, uint32_t address, uint32_t size, uint8_t *buffer)
1228 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1229 (int)size, (unsigned)address);
1231 if (!target_was_examined(target))
1233 LOG_ERROR("Target not examined yet");
1241 if ((address + size - 1) < address)
1243 /* GDB can request this when e.g. PC is 0xfffffffc*/
1244 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1250 if (((address % 2) == 0) && (size == 2))
1252 return target_write_memory(target, address, 2, 1, buffer);
1255 /* handle unaligned head bytes */
1258 uint32_t unaligned = 4 - (address % 4);
1260 if (unaligned > size)
1263 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1266 buffer += unaligned;
1267 address += unaligned;
1271 /* handle aligned words */
1274 int aligned = size - (size % 4);
1276 /* use bulk writes above a certain limit. This may have to be changed */
1279 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1284 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1293 /* handle tail writes of less than 4 bytes */
1296 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1303 /* Single aligned words are guaranteed to use 16 or 32 bit access
1304 * mode respectively, otherwise data is handled as quickly as
1307 int target_read_buffer(struct target_s *target, uint32_t address, uint32_t size, uint8_t *buffer)
1310 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1311 (int)size, (unsigned)address);
1313 if (!target_was_examined(target))
1315 LOG_ERROR("Target not examined yet");
1323 if ((address + size - 1) < address)
1325 /* GDB can request this when e.g. PC is 0xfffffffc*/
1326 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1332 if (((address % 2) == 0) && (size == 2))
1334 return target_read_memory(target, address, 2, 1, buffer);
1337 /* handle unaligned head bytes */
1340 uint32_t unaligned = 4 - (address % 4);
1342 if (unaligned > size)
1345 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1348 buffer += unaligned;
1349 address += unaligned;
1353 /* handle aligned words */
1356 int aligned = size - (size % 4);
1358 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1366 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1369 int aligned = size - (size%2);
1370 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1371 if (retval != ERROR_OK)
1378 /* handle tail writes of less than 4 bytes */
1381 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1388 int target_checksum_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t* crc)
1393 uint32_t checksum = 0;
1394 if (!target_was_examined(target))
1396 LOG_ERROR("Target not examined yet");
1400 if ((retval = target->type->checksum_memory(target, address,
1401 size, &checksum)) != ERROR_OK)
1403 buffer = malloc(size);
1406 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1407 return ERROR_INVALID_ARGUMENTS;
1409 retval = target_read_buffer(target, address, size, buffer);
1410 if (retval != ERROR_OK)
1416 /* convert to target endianess */
1417 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1419 uint32_t target_data;
1420 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1421 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1424 retval = image_calculate_checksum(buffer, size, &checksum);
1433 int target_blank_check_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t* blank)
1436 if (!target_was_examined(target))
1438 LOG_ERROR("Target not examined yet");
1442 if (target->type->blank_check_memory == 0)
1443 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1445 retval = target->type->blank_check_memory(target, address, size, blank);
1450 int target_read_u32(struct target_s *target, uint32_t address, uint32_t *value)
1452 uint8_t value_buf[4];
1453 if (!target_was_examined(target))
1455 LOG_ERROR("Target not examined yet");
1459 int retval = target_read_memory(target, address, 4, 1, value_buf);
1461 if (retval == ERROR_OK)
1463 *value = target_buffer_get_u32(target, value_buf);
1464 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1471 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1478 int target_read_u16(struct target_s *target, uint32_t address, uint16_t *value)
1480 uint8_t value_buf[2];
1481 if (!target_was_examined(target))
1483 LOG_ERROR("Target not examined yet");
1487 int retval = target_read_memory(target, address, 2, 1, value_buf);
1489 if (retval == ERROR_OK)
1491 *value = target_buffer_get_u16(target, value_buf);
1492 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1499 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1506 int target_read_u8(struct target_s *target, uint32_t address, uint8_t *value)
1508 int retval = target_read_memory(target, address, 1, 1, value);
1509 if (!target_was_examined(target))
1511 LOG_ERROR("Target not examined yet");
1515 if (retval == ERROR_OK)
1517 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1524 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1531 int target_write_u32(struct target_s *target, uint32_t address, uint32_t value)
1534 uint8_t value_buf[4];
1535 if (!target_was_examined(target))
1537 LOG_ERROR("Target not examined yet");
1541 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1545 target_buffer_set_u32(target, value_buf, value);
1546 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1548 LOG_DEBUG("failed: %i", retval);
1554 int target_write_u16(struct target_s *target, uint32_t address, uint16_t value)
1557 uint8_t value_buf[2];
1558 if (!target_was_examined(target))
1560 LOG_ERROR("Target not examined yet");
1564 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1568 target_buffer_set_u16(target, value_buf, value);
1569 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1571 LOG_DEBUG("failed: %i", retval);
1577 int target_write_u8(struct target_s *target, uint32_t address, uint8_t value)
1580 if (!target_was_examined(target))
1582 LOG_ERROR("Target not examined yet");
1586 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1589 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1591 LOG_DEBUG("failed: %i", retval);
1597 int target_register_user_commands(struct command_context_s *cmd_ctx)
1599 int retval = ERROR_OK;
1602 /* script procedures */
1603 register_command(cmd_ctx, NULL, "profile", handle_profile_command, COMMAND_EXEC, "profiling samples the CPU PC");
1604 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>");
1605 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>");
1607 register_jim(cmd_ctx, "mrc", jim_mcrmrc, "read coprocessor <cpnum> <op1> <op2> <CRn> <CRm>");
1608 register_jim(cmd_ctx, "mcr", jim_mcrmrc, "write coprocessor <cpnum> <op1> <op2> <CRn> <CRm> <value>");
1610 register_command(cmd_ctx, NULL, "fast_load_image", handle_fast_load_image_command, COMMAND_ANY,
1611 "same args as load_image, image stored in memory - mainly for profiling purposes");
1613 register_command(cmd_ctx, NULL, "fast_load", handle_fast_load_command, COMMAND_ANY,
1614 "loads active fast load image to current target - mainly for profiling purposes");
1617 register_command(cmd_ctx, NULL, "virt2phys", handle_virt2phys_command, COMMAND_ANY, "translate a virtual address into a physical address");
1618 register_command(cmd_ctx, NULL, "reg", handle_reg_command, COMMAND_EXEC, "display or set a register");
1619 register_command(cmd_ctx, NULL, "poll", handle_poll_command, COMMAND_EXEC, "poll target state");
1620 register_command(cmd_ctx, NULL, "wait_halt", handle_wait_halt_command, COMMAND_EXEC, "wait for target halt [time (s)]");
1621 register_command(cmd_ctx, NULL, "halt", handle_halt_command, COMMAND_EXEC, "halt target");
1622 register_command(cmd_ctx, NULL, "resume", handle_resume_command, COMMAND_EXEC, "resume target [addr]");
1623 register_command(cmd_ctx, NULL, "step", handle_step_command, COMMAND_EXEC, "step one instruction from current PC or [addr]");
1624 register_command(cmd_ctx, NULL, "reset", handle_reset_command, COMMAND_EXEC, "reset target [run | halt | init] - default is run");
1625 register_command(cmd_ctx, NULL, "soft_reset_halt", handle_soft_reset_halt_command, COMMAND_EXEC, "halt the target and do a soft reset");
1627 register_command(cmd_ctx, NULL, "mdw", handle_md_command, COMMAND_EXEC, "display memory words [phys] <addr> [count]");
1628 register_command(cmd_ctx, NULL, "mdh", handle_md_command, COMMAND_EXEC, "display memory half-words [phys] <addr> [count]");
1629 register_command(cmd_ctx, NULL, "mdb", handle_md_command, COMMAND_EXEC, "display memory bytes [phys] <addr> [count]");
1631 register_command(cmd_ctx, NULL, "mww", handle_mw_command, COMMAND_EXEC, "write memory word [phys] <addr> <value> [count]");
1632 register_command(cmd_ctx, NULL, "mwh", handle_mw_command, COMMAND_EXEC, "write memory half-word [phys] <addr> <value> [count]");
1633 register_command(cmd_ctx, NULL, "mwb", handle_mw_command, COMMAND_EXEC, "write memory byte [phys] <addr> <value> [count]");
1635 register_command(cmd_ctx, NULL, "bp",
1636 handle_bp_command, COMMAND_EXEC,
1637 "list or set breakpoint [<address> <length> [hw]]");
1638 register_command(cmd_ctx, NULL, "rbp",
1639 handle_rbp_command, COMMAND_EXEC,
1640 "remove breakpoint <address>");
1641 register_command(cmd_ctx, NULL, "wp",
1642 handle_wp_command, COMMAND_EXEC,
1643 "list or set watchpoint "
1644 "[<address> <length> <r/w/a> [value] [mask]]");
1645 register_command(cmd_ctx, NULL, "rwp",
1646 handle_rwp_command, COMMAND_EXEC,
1647 "remove watchpoint <address>");
1649 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]");
1650 register_command(cmd_ctx, NULL, "dump_image", handle_dump_image_command, COMMAND_EXEC, "dump_image <file> <address> <size>");
1651 register_command(cmd_ctx, NULL, "verify_image", handle_verify_image_command, COMMAND_EXEC, "verify_image <file> [offset] [type]");
1652 register_command(cmd_ctx, NULL, "test_image", handle_test_image_command, COMMAND_EXEC, "test_image <file> [offset] [type]");
1654 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
1656 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
1662 static int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1664 target_t *target = all_targets;
1668 target = get_target(args[0]);
1669 if (target == NULL) {
1670 command_print(cmd_ctx,"Target: %s is unknown, try one of:\n", args[0]);
1673 if (!target->tap->enabled) {
1674 command_print(cmd_ctx,"Target: TAP %s is disabled, "
1675 "can't be the current target\n",
1676 target->tap->dotted_name);
1680 cmd_ctx->current_target = target->target_number;
1685 target = all_targets;
1686 command_print(cmd_ctx, " TargetName Type Endian TapName State ");
1687 command_print(cmd_ctx, "-- ------------------ ---------- ------ ------------------ ------------");
1693 if (target->tap->enabled)
1694 state = target_state_name( target );
1696 state = "tap-disabled";
1698 if (cmd_ctx->current_target == target->target_number)
1701 /* keep columns lined up to match the headers above */
1702 command_print(cmd_ctx, "%2d%c %-18s %-10s %-6s %-18s %s",
1703 target->target_number,
1706 target_get_name(target),
1707 Jim_Nvp_value2name_simple(nvp_target_endian,
1708 target->endianness)->name,
1709 target->tap->dotted_name,
1711 target = target->next;
1717 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1719 static int powerDropout;
1720 static int srstAsserted;
1722 static int runPowerRestore;
1723 static int runPowerDropout;
1724 static int runSrstAsserted;
1725 static int runSrstDeasserted;
1727 static int sense_handler(void)
1729 static int prevSrstAsserted = 0;
1730 static int prevPowerdropout = 0;
1733 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1737 powerRestored = prevPowerdropout && !powerDropout;
1740 runPowerRestore = 1;
1743 long long current = timeval_ms();
1744 static long long lastPower = 0;
1745 int waitMore = lastPower + 2000 > current;
1746 if (powerDropout && !waitMore)
1748 runPowerDropout = 1;
1749 lastPower = current;
1752 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1756 srstDeasserted = prevSrstAsserted && !srstAsserted;
1758 static long long lastSrst = 0;
1759 waitMore = lastSrst + 2000 > current;
1760 if (srstDeasserted && !waitMore)
1762 runSrstDeasserted = 1;
1766 if (!prevSrstAsserted && srstAsserted)
1768 runSrstAsserted = 1;
1771 prevSrstAsserted = srstAsserted;
1772 prevPowerdropout = powerDropout;
1774 if (srstDeasserted || powerRestored)
1776 /* Other than logging the event we can't do anything here.
1777 * Issuing a reset is a particularly bad idea as we might
1778 * be inside a reset already.
1785 static void target_call_event_callbacks_all(enum target_event e) {
1787 target = all_targets;
1789 target_call_event_callbacks(target, e);
1790 target = target->next;
1794 /* process target state changes */
1795 int handle_target(void *priv)
1797 int retval = ERROR_OK;
1799 /* we do not want to recurse here... */
1800 static int recursive = 0;
1805 /* danger! running these procedures can trigger srst assertions and power dropouts.
1806 * We need to avoid an infinite loop/recursion here and we do that by
1807 * clearing the flags after running these events.
1809 int did_something = 0;
1810 if (runSrstAsserted)
1812 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1813 Jim_Eval(interp, "srst_asserted");
1816 if (runSrstDeasserted)
1818 Jim_Eval(interp, "srst_deasserted");
1821 if (runPowerDropout)
1823 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1824 Jim_Eval(interp, "power_dropout");
1827 if (runPowerRestore)
1829 Jim_Eval(interp, "power_restore");
1835 /* clear detect flags */
1839 /* clear action flags */
1841 runSrstAsserted = 0;
1842 runSrstDeasserted = 0;
1843 runPowerRestore = 0;
1844 runPowerDropout = 0;
1849 /* Poll targets for state changes unless that's globally disabled.
1850 * Skip targets that are currently disabled.
1852 for (target_t *target = all_targets;
1853 is_jtag_poll_safe() && target;
1854 target = target->next)
1856 if (!target->tap->enabled)
1859 /* only poll target if we've got power and srst isn't asserted */
1860 if (!powerDropout && !srstAsserted)
1862 /* polling may fail silently until the target has been examined */
1863 if ((retval = target_poll(target)) != ERROR_OK)
1865 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1874 static int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1883 target = get_current_target(cmd_ctx);
1885 /* list all available registers for the current target */
1888 reg_cache_t *cache = target->reg_cache;
1895 command_print(cmd_ctx, "===== %s", cache->name);
1897 for (i = 0, reg = cache->reg_list;
1898 i < cache->num_regs;
1899 i++, reg++, count++)
1901 /* only print cached values if they are valid */
1903 value = buf_to_str(reg->value,
1905 command_print(cmd_ctx,
1906 "(%i) %s (/%" PRIu32 "): 0x%s%s",
1914 command_print(cmd_ctx, "(%i) %s (/%" PRIu32 ")",
1919 cache = cache->next;
1925 /* access a single register by its ordinal number */
1926 if ((args[0][0] >= '0') && (args[0][0] <= '9'))
1929 int retval = parse_uint(args[0], &num);
1930 if (ERROR_OK != retval)
1931 return ERROR_COMMAND_SYNTAX_ERROR;
1933 reg_cache_t *cache = target->reg_cache;
1938 for (i = 0; i < cache->num_regs; i++)
1940 if (count++ == (int)num)
1942 reg = &cache->reg_list[i];
1948 cache = cache->next;
1953 command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1956 } else /* access a single register by its name */
1958 reg = register_get_by_name(target->reg_cache, args[0], 1);
1962 command_print(cmd_ctx, "register %s not found in current target", args[0]);
1967 /* display a register */
1968 if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
1970 if ((argc == 2) && (strcmp(args[1], "force") == 0))
1973 if (reg->valid == 0)
1975 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1976 arch_type->get(reg);
1978 value = buf_to_str(reg->value, reg->size, 16);
1979 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1984 /* set register value */
1987 uint8_t *buf = malloc(CEIL(reg->size, 8));
1988 str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
1990 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1991 arch_type->set(reg, buf);
1993 value = buf_to_str(reg->value, reg->size, 16);
1994 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2002 command_print(cmd_ctx, "usage: reg <#|name> [value]");
2007 static int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2009 int retval = ERROR_OK;
2010 target_t *target = get_current_target(cmd_ctx);
2014 command_print(cmd_ctx, "background polling: %s",
2015 jtag_poll_get_enabled() ? "on" : "off");
2016 command_print(cmd_ctx, "TAP: %s (%s)",
2017 target->tap->dotted_name,
2018 target->tap->enabled ? "enabled" : "disabled");
2019 if (!target->tap->enabled)
2021 if ((retval = target_poll(target)) != ERROR_OK)
2023 if ((retval = target_arch_state(target)) != ERROR_OK)
2029 if (strcmp(args[0], "on") == 0)
2031 jtag_poll_set_enabled(true);
2033 else if (strcmp(args[0], "off") == 0)
2035 jtag_poll_set_enabled(false);
2039 command_print(cmd_ctx, "arg is \"on\" or \"off\"");
2043 return ERROR_COMMAND_SYNTAX_ERROR;
2049 static int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2052 return ERROR_COMMAND_SYNTAX_ERROR;
2057 int retval = parse_uint(args[0], &ms);
2058 if (ERROR_OK != retval)
2060 command_print(cmd_ctx, "usage: %s [seconds]", cmd);
2061 return ERROR_COMMAND_SYNTAX_ERROR;
2063 // convert seconds (given) to milliseconds (needed)
2067 target_t *target = get_current_target(cmd_ctx);
2068 return target_wait_state(target, TARGET_HALTED, ms);
2071 /* wait for target state to change. The trick here is to have a low
2072 * latency for short waits and not to suck up all the CPU time
2075 * After 500ms, keep_alive() is invoked
2077 int target_wait_state(target_t *target, enum target_state state, int ms)
2080 long long then = 0, cur;
2085 if ((retval = target_poll(target)) != ERROR_OK)
2087 if (target->state == state)
2095 then = timeval_ms();
2096 LOG_DEBUG("waiting for target %s...",
2097 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2105 if ((cur-then) > ms)
2107 LOG_ERROR("timed out while waiting for target %s",
2108 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2116 static int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2120 target_t *target = get_current_target(cmd_ctx);
2121 int retval = target_halt(target);
2122 if (ERROR_OK != retval)
2128 retval = parse_uint(args[0], &wait);
2129 if (ERROR_OK != retval)
2130 return ERROR_COMMAND_SYNTAX_ERROR;
2135 return handle_wait_halt_command(cmd_ctx, cmd, args, argc);
2138 static int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2140 target_t *target = get_current_target(cmd_ctx);
2142 LOG_USER("requesting target halt and executing a soft reset");
2144 target->type->soft_reset_halt(target);
2149 static int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2152 return ERROR_COMMAND_SYNTAX_ERROR;
2154 enum target_reset_mode reset_mode = RESET_RUN;
2158 n = Jim_Nvp_name2value_simple(nvp_reset_modes, args[0]);
2159 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2160 return ERROR_COMMAND_SYNTAX_ERROR;
2162 reset_mode = n->value;
2165 /* reset *all* targets */
2166 return target_process_reset(cmd_ctx, reset_mode);
2170 static int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2174 return ERROR_COMMAND_SYNTAX_ERROR;
2176 target_t *target = get_current_target(cmd_ctx);
2177 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2179 /* with no args, resume from current pc, addr = 0,
2180 * with one arguments, addr = args[0],
2181 * handle breakpoints, not debugging */
2185 int retval = parse_u32(args[0], &addr);
2186 if (ERROR_OK != retval)
2191 return target_resume(target, current, addr, 1, 0);
2194 static int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2197 return ERROR_COMMAND_SYNTAX_ERROR;
2201 /* with no args, step from current pc, addr = 0,
2202 * with one argument addr = args[0],
2203 * handle breakpoints, debugging */
2208 int retval = parse_u32(args[0], &addr);
2209 if (ERROR_OK != retval)
2214 target_t *target = get_current_target(cmd_ctx);
2216 return target->type->step(target, current_pc, addr, 1);
2219 static void handle_md_output(struct command_context_s *cmd_ctx,
2220 struct target_s *target, uint32_t address, unsigned size,
2221 unsigned count, const uint8_t *buffer)
2223 const unsigned line_bytecnt = 32;
2224 unsigned line_modulo = line_bytecnt / size;
2226 char output[line_bytecnt * 4 + 1];
2227 unsigned output_len = 0;
2229 const char *value_fmt;
2231 case 4: value_fmt = "%8.8x "; break;
2232 case 2: value_fmt = "%4.2x "; break;
2233 case 1: value_fmt = "%2.2x "; break;
2235 LOG_ERROR("invalid memory read size: %u", size);
2239 for (unsigned i = 0; i < count; i++)
2241 if (i % line_modulo == 0)
2243 output_len += snprintf(output + output_len,
2244 sizeof(output) - output_len,
2246 (unsigned)(address + (i*size)));
2250 const uint8_t *value_ptr = buffer + i * size;
2252 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2253 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2254 case 1: value = *value_ptr;
2256 output_len += snprintf(output + output_len,
2257 sizeof(output) - output_len,
2260 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2262 command_print(cmd_ctx, "%s", output);
2268 static int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2271 return ERROR_COMMAND_SYNTAX_ERROR;
2275 case 'w': size = 4; break;
2276 case 'h': size = 2; break;
2277 case 'b': size = 1; break;
2278 default: return ERROR_COMMAND_SYNTAX_ERROR;
2281 bool physical=strcmp(args[0], "phys")==0;
2282 int (*fn)(struct target_s *target,
2283 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2288 fn=target_read_phys_memory;
2291 fn=target_read_memory;
2293 if ((argc < 1) || (argc > 2))
2295 return ERROR_COMMAND_SYNTAX_ERROR;
2298 int retval = parse_u32(args[0], &address);
2299 if (ERROR_OK != retval)
2305 retval = parse_uint(args[1], &count);
2306 if (ERROR_OK != retval)
2310 uint8_t *buffer = calloc(count, size);
2312 target_t *target = get_current_target(cmd_ctx);
2313 retval = fn(target, address, size, count, buffer);
2314 if (ERROR_OK == retval)
2315 handle_md_output(cmd_ctx, target, address, size, count, buffer);
2322 static int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2326 return ERROR_COMMAND_SYNTAX_ERROR;
2328 bool physical=strcmp(args[0], "phys")==0;
2329 int (*fn)(struct target_s *target,
2330 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2335 fn=target_write_phys_memory;
2338 fn=target_write_memory;
2340 if ((argc < 2) || (argc > 3))
2341 return ERROR_COMMAND_SYNTAX_ERROR;
2344 int retval = parse_u32(args[0], &address);
2345 if (ERROR_OK != retval)
2349 retval = parse_u32(args[1], &value);
2350 if (ERROR_OK != retval)
2356 retval = parse_uint(args[2], &count);
2357 if (ERROR_OK != retval)
2361 target_t *target = get_current_target(cmd_ctx);
2363 uint8_t value_buf[4];
2368 target_buffer_set_u32(target, value_buf, value);
2372 target_buffer_set_u16(target, value_buf, value);
2376 value_buf[0] = value;
2379 return ERROR_COMMAND_SYNTAX_ERROR;
2381 for (unsigned i = 0; i < count; i++)
2384 address + i * wordsize, wordsize, 1, value_buf);
2385 if (ERROR_OK != retval)
2394 static int parse_load_image_command_args(char **args, int argc,
2395 image_t *image, uint32_t *min_address, uint32_t *max_address)
2397 if (argc < 1 || argc > 5)
2398 return ERROR_COMMAND_SYNTAX_ERROR;
2400 /* a base address isn't always necessary,
2401 * default to 0x0 (i.e. don't relocate) */
2405 int retval = parse_u32(args[1], &addr);
2406 if (ERROR_OK != retval)
2407 return ERROR_COMMAND_SYNTAX_ERROR;
2408 image->base_address = addr;
2409 image->base_address_set = 1;
2412 image->base_address_set = 0;
2414 image->start_address_set = 0;
2418 int retval = parse_u32(args[3], min_address);
2419 if (ERROR_OK != retval)
2420 return ERROR_COMMAND_SYNTAX_ERROR;
2424 int retval = parse_u32(args[4], max_address);
2425 if (ERROR_OK != retval)
2426 return ERROR_COMMAND_SYNTAX_ERROR;
2427 // use size (given) to find max (required)
2428 *max_address += *min_address;
2431 if (*min_address > *max_address)
2432 return ERROR_COMMAND_SYNTAX_ERROR;
2437 static int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2441 uint32_t image_size;
2442 uint32_t min_address = 0;
2443 uint32_t max_address = 0xffffffff;
2449 duration_t duration;
2450 char *duration_text;
2452 int retval = parse_load_image_command_args(args, argc,
2453 &image, &min_address, &max_address);
2454 if (ERROR_OK != retval)
2457 target_t *target = get_current_target(cmd_ctx);
2458 duration_start_measure(&duration);
2460 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
2467 for (i = 0; i < image.num_sections; i++)
2469 buffer = malloc(image.sections[i].size);
2472 command_print(cmd_ctx,
2473 "error allocating buffer for section (%d bytes)",
2474 (int)(image.sections[i].size));
2478 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2484 uint32_t offset = 0;
2485 uint32_t length = buf_cnt;
2487 /* DANGER!!! beware of unsigned comparision here!!! */
2489 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2490 (image.sections[i].base_address < max_address))
2492 if (image.sections[i].base_address < min_address)
2494 /* clip addresses below */
2495 offset += min_address-image.sections[i].base_address;
2499 if (image.sections[i].base_address + buf_cnt > max_address)
2501 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2504 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2509 image_size += length;
2510 command_print(cmd_ctx, "%u bytes written at address 0x%8.8" PRIx32 "",
2511 (unsigned int)length,
2512 image.sections[i].base_address + offset);
2518 if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2520 image_close(&image);
2524 if (retval == ERROR_OK)
2526 command_print(cmd_ctx, "downloaded %u byte in %s",
2527 (unsigned int)image_size,
2530 free(duration_text);
2532 image_close(&image);
2538 static int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2542 uint8_t buffer[560];
2545 duration_t duration;
2546 char *duration_text;
2548 target_t *target = get_current_target(cmd_ctx);
2552 command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
2557 int retval = parse_u32(args[1], &address);
2558 if (ERROR_OK != retval)
2562 retval = parse_u32(args[2], &size);
2563 if (ERROR_OK != retval)
2566 if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2571 duration_start_measure(&duration);
2575 uint32_t size_written;
2576 uint32_t this_run_size = (size > 560) ? 560 : size;
2578 retval = target_read_buffer(target, address, this_run_size, buffer);
2579 if (retval != ERROR_OK)
2584 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2585 if (retval != ERROR_OK)
2590 size -= this_run_size;
2591 address += this_run_size;
2594 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2597 if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2600 if (retval == ERROR_OK)
2602 command_print(cmd_ctx, "dumped %lld byte in %s",
2603 fileio.size, duration_text);
2604 free(duration_text);
2610 static int handle_verify_image_command_internal(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, int verify)
2614 uint32_t image_size;
2616 int retval, retvaltemp;
2617 uint32_t checksum = 0;
2618 uint32_t mem_checksum = 0;
2622 duration_t duration;
2623 char *duration_text;
2625 target_t *target = get_current_target(cmd_ctx);
2629 return ERROR_COMMAND_SYNTAX_ERROR;
2634 LOG_ERROR("no target selected");
2638 duration_start_measure(&duration);
2643 retval = parse_u32(args[1], &addr);
2644 if (ERROR_OK != retval)
2645 return ERROR_COMMAND_SYNTAX_ERROR;
2646 image.base_address = addr;
2647 image.base_address_set = 1;
2651 image.base_address_set = 0;
2652 image.base_address = 0x0;
2655 image.start_address_set = 0;
2657 if ((retval = image_open(&image, args[0], (argc == 3) ? args[2] : NULL)) != ERROR_OK)
2664 for (i = 0; i < image.num_sections; i++)
2666 buffer = malloc(image.sections[i].size);
2669 command_print(cmd_ctx,
2670 "error allocating buffer for section (%d bytes)",
2671 (int)(image.sections[i].size));
2674 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2682 /* calculate checksum of image */
2683 image_calculate_checksum(buffer, buf_cnt, &checksum);
2685 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2686 if (retval != ERROR_OK)
2692 if (checksum != mem_checksum)
2694 /* failed crc checksum, fall back to a binary compare */
2697 command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
2699 data = (uint8_t*)malloc(buf_cnt);
2701 /* Can we use 32bit word accesses? */
2703 int count = buf_cnt;
2704 if ((count % 4) == 0)
2709 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2710 if (retval == ERROR_OK)
2713 for (t = 0; t < buf_cnt; t++)
2715 if (data[t] != buffer[t])
2717 command_print(cmd_ctx,
2718 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2719 (unsigned)(t + image.sections[i].base_address),
2724 retval = ERROR_FAIL;
2738 command_print(cmd_ctx, "address 0x%08" PRIx32 " length 0x%08" PRIx32 "",
2739 image.sections[i].base_address,
2744 image_size += buf_cnt;
2748 if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2750 image_close(&image);
2754 if (retval == ERROR_OK)
2756 command_print(cmd_ctx, "verified %u bytes in %s",
2757 (unsigned int)image_size,
2760 free(duration_text);
2762 image_close(&image);
2767 static int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2769 return handle_verify_image_command_internal(cmd_ctx, cmd, args, argc, 1);
2772 static int handle_test_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2774 return handle_verify_image_command_internal(cmd_ctx, cmd, args, argc, 0);
2777 static int handle_bp_command_list(struct command_context_s *cmd_ctx)
2779 target_t *target = get_current_target(cmd_ctx);
2780 breakpoint_t *breakpoint = target->breakpoints;
2783 if (breakpoint->type == BKPT_SOFT)
2785 char* buf = buf_to_str(breakpoint->orig_instr,
2786 breakpoint->length, 16);
2787 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2788 breakpoint->address,
2790 breakpoint->set, buf);
2795 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2796 breakpoint->address,
2797 breakpoint->length, breakpoint->set);
2800 breakpoint = breakpoint->next;
2805 static int handle_bp_command_set(struct command_context_s *cmd_ctx,
2806 uint32_t addr, uint32_t length, int hw)
2808 target_t *target = get_current_target(cmd_ctx);
2809 int retval = breakpoint_add(target, addr, length, hw);
2810 if (ERROR_OK == retval)
2811 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2813 LOG_ERROR("Failure setting breakpoint");
2817 static int handle_bp_command(struct command_context_s *cmd_ctx,
2818 char *cmd, char **args, int argc)
2821 return handle_bp_command_list(cmd_ctx);
2823 if (argc < 2 || argc > 3)
2825 command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
2826 return ERROR_COMMAND_SYNTAX_ERROR;
2830 int retval = parse_u32(args[0], &addr);
2831 if (ERROR_OK != retval)
2835 retval = parse_u32(args[1], &length);
2836 if (ERROR_OK != retval)
2842 if (strcmp(args[2], "hw") == 0)
2845 return ERROR_COMMAND_SYNTAX_ERROR;
2848 return handle_bp_command_set(cmd_ctx, addr, length, hw);
2851 static int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2854 return ERROR_COMMAND_SYNTAX_ERROR;
2857 int retval = parse_u32(args[0], &addr);
2858 if (ERROR_OK != retval)
2861 target_t *target = get_current_target(cmd_ctx);
2862 breakpoint_remove(target, addr);
2867 static int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2869 target_t *target = get_current_target(cmd_ctx);
2873 watchpoint_t *watchpoint = target->watchpoints;
2877 command_print(cmd_ctx,
2878 "address: 0x%8.8" PRIx32 ", len: 0x%8.8x, r/w/a: %i, value: 0x%8.8" PRIx32 ", mask: 0x%8.8" PRIx32 "",
2879 watchpoint->address,
2881 (int)(watchpoint->rw),
2884 watchpoint = watchpoint->next;
2889 enum watchpoint_rw type = WPT_ACCESS;
2891 uint32_t length = 0;
2892 uint32_t data_value = 0x0;
2893 uint32_t data_mask = 0xffffffff;
2899 retval = parse_u32(args[4], &data_mask);
2900 if (ERROR_OK != retval)
2904 retval = parse_u32(args[3], &data_value);
2905 if (ERROR_OK != retval)
2921 LOG_ERROR("invalid watchpoint mode ('%c')", args[2][0]);
2922 return ERROR_COMMAND_SYNTAX_ERROR;
2926 retval = parse_u32(args[1], &length);
2927 if (ERROR_OK != retval)
2929 retval = parse_u32(args[0], &addr);
2930 if (ERROR_OK != retval)
2935 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2936 return ERROR_COMMAND_SYNTAX_ERROR;
2939 retval = watchpoint_add(target, addr, length, type,
2940 data_value, data_mask);
2941 if (ERROR_OK != retval)
2942 LOG_ERROR("Failure setting watchpoints");
2947 static int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2950 return ERROR_COMMAND_SYNTAX_ERROR;
2953 int retval = parse_u32(args[0], &addr);
2954 if (ERROR_OK != retval)
2957 target_t *target = get_current_target(cmd_ctx);
2958 watchpoint_remove(target, addr);
2965 * Translate a virtual address to a physical address.
2967 * The low-level target implementation must have logged a detailed error
2968 * which is forwarded to telnet/GDB session.
2970 static int handle_virt2phys_command(command_context_t *cmd_ctx,
2971 char *cmd, char **args, int argc)
2974 return ERROR_COMMAND_SYNTAX_ERROR;
2977 int retval = parse_u32(args[0], &va);
2978 if (ERROR_OK != retval)
2982 target_t *target = get_current_target(cmd_ctx);
2983 retval = target->type->virt2phys(target, va, &pa);
2984 if (retval == ERROR_OK)
2985 command_print(cmd_ctx, "Physical address 0x%08" PRIx32 "", pa);
2990 static void writeData(FILE *f, const void *data, size_t len)
2992 size_t written = fwrite(data, 1, len, f);
2994 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
2997 static void writeLong(FILE *f, int l)
3000 for (i = 0; i < 4; i++)
3002 char c = (l >> (i*8))&0xff;
3003 writeData(f, &c, 1);
3008 static void writeString(FILE *f, char *s)
3010 writeData(f, s, strlen(s));
3013 /* Dump a gmon.out histogram file. */
3014 static void writeGmon(uint32_t *samples, uint32_t sampleNum, char *filename)
3017 FILE *f = fopen(filename, "w");
3020 writeString(f, "gmon");
3021 writeLong(f, 0x00000001); /* Version */
3022 writeLong(f, 0); /* padding */
3023 writeLong(f, 0); /* padding */
3024 writeLong(f, 0); /* padding */
3026 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
3027 writeData(f, &zero, 1);
3029 /* figure out bucket size */
3030 uint32_t min = samples[0];
3031 uint32_t max = samples[0];
3032 for (i = 0; i < sampleNum; i++)
3034 if (min > samples[i])
3038 if (max < samples[i])
3044 int addressSpace = (max-min + 1);
3046 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
3047 uint32_t length = addressSpace;
3048 if (length > maxBuckets)
3050 length = maxBuckets;
3052 int *buckets = malloc(sizeof(int)*length);
3053 if (buckets == NULL)
3058 memset(buckets, 0, sizeof(int)*length);
3059 for (i = 0; i < sampleNum;i++)
3061 uint32_t address = samples[i];
3062 long long a = address-min;
3063 long long b = length-1;
3064 long long c = addressSpace-1;
3065 int index = (a*b)/c; /* danger!!!! int32 overflows */
3069 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3070 writeLong(f, min); /* low_pc */
3071 writeLong(f, max); /* high_pc */
3072 writeLong(f, length); /* # of samples */
3073 writeLong(f, 64000000); /* 64MHz */
3074 writeString(f, "seconds");
3075 for (i = 0; i < (15-strlen("seconds")); i++)
3076 writeData(f, &zero, 1);
3077 writeString(f, "s");
3079 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3081 char *data = malloc(2*length);
3084 for (i = 0; i < length;i++)
3093 data[i*2 + 1]=(val >> 8)&0xff;
3096 writeData(f, data, length * 2);
3106 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
3107 static int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
3109 target_t *target = get_current_target(cmd_ctx);
3110 struct timeval timeout, now;
3112 gettimeofday(&timeout, NULL);
3115 return ERROR_COMMAND_SYNTAX_ERROR;
3118 int retval = parse_uint(args[0], &offset);
3119 if (ERROR_OK != retval)
3122 timeval_add_time(&timeout, offset, 0);
3124 command_print(cmd_ctx, "Starting profiling. Halting and resuming the target as often as we can...");
3126 static const int maxSample = 10000;
3127 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3128 if (samples == NULL)
3132 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3133 reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1);
3137 target_poll(target);
3138 if (target->state == TARGET_HALTED)
3140 uint32_t t=*((uint32_t *)reg->value);
3141 samples[numSamples++]=t;
3142 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3143 target_poll(target);
3144 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3145 } else if (target->state == TARGET_RUNNING)
3147 /* We want to quickly sample the PC. */
3148 if ((retval = target_halt(target)) != ERROR_OK)
3155 command_print(cmd_ctx, "Target not halted or running");
3159 if (retval != ERROR_OK)
3164 gettimeofday(&now, NULL);
3165 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3167 command_print(cmd_ctx, "Profiling completed. %d samples.", numSamples);
3168 if ((retval = target_poll(target)) != ERROR_OK)
3173 if (target->state == TARGET_HALTED)
3175 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3177 if ((retval = target_poll(target)) != ERROR_OK)
3182 writeGmon(samples, numSamples, args[1]);
3183 command_print(cmd_ctx, "Wrote %s", args[1]);
3192 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3195 Jim_Obj *nameObjPtr, *valObjPtr;
3198 namebuf = alloc_printf("%s(%d)", varname, idx);
3202 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3203 valObjPtr = Jim_NewIntObj(interp, val);
3204 if (!nameObjPtr || !valObjPtr)
3210 Jim_IncrRefCount(nameObjPtr);
3211 Jim_IncrRefCount(valObjPtr);
3212 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3213 Jim_DecrRefCount(interp, nameObjPtr);
3214 Jim_DecrRefCount(interp, valObjPtr);
3216 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3220 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3222 command_context_t *context;
3225 context = Jim_GetAssocData(interp, "context");
3226 if (context == NULL)
3228 LOG_ERROR("mem2array: no command context");
3231 target = get_current_target(context);
3234 LOG_ERROR("mem2array: no current target");
3238 return target_mem2array(interp, target, argc-1, argv + 1);
3241 static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
3249 const char *varname;
3250 uint8_t buffer[4096];
3254 /* argv[1] = name of array to receive the data
3255 * argv[2] = desired width
3256 * argv[3] = memory address
3257 * argv[4] = count of times to read
3260 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3263 varname = Jim_GetString(argv[0], &len);
3264 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3266 e = Jim_GetLong(interp, argv[1], &l);
3272 e = Jim_GetLong(interp, argv[2], &l);
3277 e = Jim_GetLong(interp, argv[3], &l);
3293 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3294 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3298 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3299 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3302 if ((addr + (len * width)) < addr) {
3303 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3304 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3307 /* absurd transfer size? */
3309 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3310 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3315 ((width == 2) && ((addr & 1) == 0)) ||
3316 ((width == 4) && ((addr & 3) == 0))) {
3320 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3321 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3324 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3335 /* Slurp... in buffer size chunks */
3337 count = len; /* in objects.. */
3338 if (count > (sizeof(buffer)/width)) {
3339 count = (sizeof(buffer)/width);
3342 retval = target_read_memory(target, addr, width, count, buffer);
3343 if (retval != ERROR_OK) {
3345 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3349 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3350 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3354 v = 0; /* shut up gcc */
3355 for (i = 0 ;i < count ;i++, n++) {
3358 v = target_buffer_get_u32(target, &buffer[i*width]);
3361 v = target_buffer_get_u16(target, &buffer[i*width]);
3364 v = buffer[i] & 0x0ff;
3367 new_int_array_element(interp, varname, n, v);
3373 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3378 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3381 Jim_Obj *nameObjPtr, *valObjPtr;
3385 namebuf = alloc_printf("%s(%d)", varname, idx);
3389 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3396 Jim_IncrRefCount(nameObjPtr);
3397 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3398 Jim_DecrRefCount(interp, nameObjPtr);
3400 if (valObjPtr == NULL)
3403 result = Jim_GetLong(interp, valObjPtr, &l);
3404 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3409 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3411 command_context_t *context;
3414 context = Jim_GetAssocData(interp, "context");
3415 if (context == NULL) {
3416 LOG_ERROR("array2mem: no command context");
3419 target = get_current_target(context);
3420 if (target == NULL) {
3421 LOG_ERROR("array2mem: no current target");
3425 return target_array2mem(interp,target, argc-1, argv + 1);
3427 static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
3435 const char *varname;
3436 uint8_t buffer[4096];
3440 /* argv[1] = name of array to get the data
3441 * argv[2] = desired width
3442 * argv[3] = memory address
3443 * argv[4] = count to write
3446 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3449 varname = Jim_GetString(argv[0], &len);
3450 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3452 e = Jim_GetLong(interp, argv[1], &l);
3458 e = Jim_GetLong(interp, argv[2], &l);
3463 e = Jim_GetLong(interp, argv[3], &l);
3479 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3480 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3484 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3485 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3488 if ((addr + (len * width)) < addr) {
3489 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3490 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3493 /* absurd transfer size? */
3495 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3496 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3501 ((width == 2) && ((addr & 1) == 0)) ||
3502 ((width == 4) && ((addr & 3) == 0))) {
3506 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3507 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3510 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3521 /* Slurp... in buffer size chunks */
3523 count = len; /* in objects.. */
3524 if (count > (sizeof(buffer)/width)) {
3525 count = (sizeof(buffer)/width);
3528 v = 0; /* shut up gcc */
3529 for (i = 0 ;i < count ;i++, n++) {
3530 get_int_array_element(interp, varname, n, &v);
3533 target_buffer_set_u32(target, &buffer[i*width], v);
3536 target_buffer_set_u16(target, &buffer[i*width], v);
3539 buffer[i] = v & 0x0ff;
3545 retval = target_write_memory(target, addr, width, count, buffer);
3546 if (retval != ERROR_OK) {
3548 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3552 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3553 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3559 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3564 void target_all_handle_event(enum target_event e)
3568 LOG_DEBUG("**all*targets: event: %d, %s",
3570 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3572 target = all_targets;
3574 target_handle_event(target, e);
3575 target = target->next;
3580 /* FIX? should we propagate errors here rather than printing them
3583 void target_handle_event(target_t *target, enum target_event e)
3585 target_event_action_t *teap;
3587 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3588 if (teap->event == e) {
3589 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3590 target->target_number,
3592 target_get_name(target),
3594 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3595 Jim_GetString(teap->body, NULL));
3596 if (Jim_EvalObj(interp, teap->body) != JIM_OK)
3598 Jim_PrintErrorMessage(interp);
3604 enum target_cfg_param {
3607 TCFG_WORK_AREA_VIRT,
3608 TCFG_WORK_AREA_PHYS,
3609 TCFG_WORK_AREA_SIZE,
3610 TCFG_WORK_AREA_BACKUP,
3613 TCFG_CHAIN_POSITION,
3616 static Jim_Nvp nvp_config_opts[] = {
3617 { .name = "-type", .value = TCFG_TYPE },
3618 { .name = "-event", .value = TCFG_EVENT },
3619 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3620 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3621 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3622 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3623 { .name = "-endian" , .value = TCFG_ENDIAN },
3624 { .name = "-variant", .value = TCFG_VARIANT },
3625 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3627 { .name = NULL, .value = -1 }
3630 static int target_configure(Jim_GetOptInfo *goi, target_t *target)
3638 /* parse config or cget options ... */
3639 while (goi->argc > 0) {
3640 Jim_SetEmptyResult(goi->interp);
3641 /* Jim_GetOpt_Debug(goi); */
3643 if (target->type->target_jim_configure) {
3644 /* target defines a configure function */
3645 /* target gets first dibs on parameters */
3646 e = (*(target->type->target_jim_configure))(target, goi);
3655 /* otherwise we 'continue' below */
3657 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3659 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3665 if (goi->isconfigure) {
3666 Jim_SetResult_sprintf(goi->interp, "not setable: %s", n->name);
3670 if (goi->argc != 0) {
3671 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "NO PARAMS");
3675 Jim_SetResultString(goi->interp, target_get_name(target), -1);
3679 if (goi->argc == 0) {
3680 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3684 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3686 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3690 if (goi->isconfigure) {
3691 if (goi->argc != 1) {
3692 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3696 if (goi->argc != 0) {
3697 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3703 target_event_action_t *teap;
3705 teap = target->event_action;
3706 /* replace existing? */
3708 if (teap->event == (enum target_event)n->value) {
3714 if (goi->isconfigure) {
3715 bool replace = true;
3718 teap = calloc(1, sizeof(*teap));
3721 teap->event = n->value;
3722 Jim_GetOpt_Obj(goi, &o);
3724 Jim_DecrRefCount(interp, teap->body);
3726 teap->body = Jim_DuplicateObj(goi->interp, o);
3729 * Tcl/TK - "tk events" have a nice feature.
3730 * See the "BIND" command.
3731 * We should support that here.
3732 * You can specify %X and %Y in the event code.
3733 * The idea is: %T - target name.
3734 * The idea is: %N - target number
3735 * The idea is: %E - event name.
3737 Jim_IncrRefCount(teap->body);
3741 /* add to head of event list */
3742 teap->next = target->event_action;
3743 target->event_action = teap;
3745 Jim_SetEmptyResult(goi->interp);
3749 Jim_SetEmptyResult(goi->interp);
3751 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3758 case TCFG_WORK_AREA_VIRT:
3759 if (goi->isconfigure) {
3760 target_free_all_working_areas(target);
3761 e = Jim_GetOpt_Wide(goi, &w);
3765 target->working_area_virt = w;
3767 if (goi->argc != 0) {
3771 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3775 case TCFG_WORK_AREA_PHYS:
3776 if (goi->isconfigure) {
3777 target_free_all_working_areas(target);
3778 e = Jim_GetOpt_Wide(goi, &w);
3782 target->working_area_phys = w;
3784 if (goi->argc != 0) {
3788 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3792 case TCFG_WORK_AREA_SIZE:
3793 if (goi->isconfigure) {
3794 target_free_all_working_areas(target);
3795 e = Jim_GetOpt_Wide(goi, &w);
3799 target->working_area_size = w;
3801 if (goi->argc != 0) {
3805 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3809 case TCFG_WORK_AREA_BACKUP:
3810 if (goi->isconfigure) {
3811 target_free_all_working_areas(target);
3812 e = Jim_GetOpt_Wide(goi, &w);
3816 /* make this exactly 1 or 0 */
3817 target->backup_working_area = (!!w);
3819 if (goi->argc != 0) {
3823 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3824 /* loop for more e*/
3828 if (goi->isconfigure) {
3829 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3831 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3834 target->endianness = n->value;
3836 if (goi->argc != 0) {
3840 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3841 if (n->name == NULL) {
3842 target->endianness = TARGET_LITTLE_ENDIAN;
3843 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3845 Jim_SetResultString(goi->interp, n->name, -1);
3850 if (goi->isconfigure) {
3851 if (goi->argc < 1) {
3852 Jim_SetResult_sprintf(goi->interp,
3857 if (target->variant) {
3858 free((void *)(target->variant));
3860 e = Jim_GetOpt_String(goi, &cp, NULL);
3861 target->variant = strdup(cp);
3863 if (goi->argc != 0) {
3867 Jim_SetResultString(goi->interp, target->variant,-1);
3870 case TCFG_CHAIN_POSITION:
3871 if (goi->isconfigure) {
3874 target_free_all_working_areas(target);
3875 e = Jim_GetOpt_Obj(goi, &o);
3879 tap = jtag_tap_by_jim_obj(goi->interp, o);
3883 /* make this exactly 1 or 0 */
3886 if (goi->argc != 0) {
3890 Jim_SetResultString(interp, target->tap->dotted_name, -1);
3891 /* loop for more e*/
3894 } /* while (goi->argc) */
3897 /* done - we return */
3901 /** this is the 'tcl' handler for the target specific command */
3902 static int tcl_target_func(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3907 uint8_t target_buf[32];
3910 struct command_context_s *cmd_ctx;
3917 TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
3918 TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
3919 TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
3920 TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
3928 TS_CMD_INVOKE_EVENT,
3931 static const Jim_Nvp target_options[] = {
3932 { .name = "configure", .value = TS_CMD_CONFIGURE },
3933 { .name = "cget", .value = TS_CMD_CGET },
3934 { .name = "mww", .value = TS_CMD_MWW },
3935 { .name = "mwh", .value = TS_CMD_MWH },
3936 { .name = "mwb", .value = TS_CMD_MWB },
3937 { .name = "mdw", .value = TS_CMD_MDW },
3938 { .name = "mdh", .value = TS_CMD_MDH },
3939 { .name = "mdb", .value = TS_CMD_MDB },
3940 { .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
3941 { .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
3942 { .name = "eventlist", .value = TS_CMD_EVENTLIST },
3943 { .name = "curstate", .value = TS_CMD_CURSTATE },
3945 { .name = "arp_examine", .value = TS_CMD_EXAMINE },
3946 { .name = "arp_poll", .value = TS_CMD_POLL },
3947 { .name = "arp_reset", .value = TS_CMD_RESET },
3948 { .name = "arp_halt", .value = TS_CMD_HALT },
3949 { .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
3950 { .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
3952 { .name = NULL, .value = -1 },
3955 /* go past the "command" */
3956 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
3958 target = Jim_CmdPrivData(goi.interp);
3959 cmd_ctx = Jim_GetAssocData(goi.interp, "context");
3961 /* commands here are in an NVP table */
3962 e = Jim_GetOpt_Nvp(&goi, target_options, &n);
3964 Jim_GetOpt_NvpUnknown(&goi, target_options, 0);
3967 /* Assume blank result */
3968 Jim_SetEmptyResult(goi.interp);
3971 case TS_CMD_CONFIGURE:
3973 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
3976 goi.isconfigure = 1;
3977 return target_configure(&goi, target);
3979 // some things take params
3981 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "missing: ?-option?");
3984 goi.isconfigure = 0;
3985 return target_configure(&goi, target);
3993 * argv[3] = optional count.
3996 if ((goi.argc == 2) || (goi.argc == 3)) {
4000 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR DATA [COUNT]", n->name);
4004 e = Jim_GetOpt_Wide(&goi, &a);
4009 e = Jim_GetOpt_Wide(&goi, &b);
4013 if (goi.argc == 3) {
4014 e = Jim_GetOpt_Wide(&goi, &c);
4024 target_buffer_set_u32(target, target_buf, b);
4028 target_buffer_set_u16(target, target_buf, b);
4032 target_buffer_set_u8(target, target_buf, b);
4036 for (x = 0 ; x < c ; x++) {
4037 e = target_write_memory(target, a, b, 1, target_buf);
4038 if (e != ERROR_OK) {
4039 Jim_SetResult_sprintf(interp, "Error writing @ 0x%08x: %d\n", (int)(a), e);
4052 /* argv[0] = command
4054 * argv[2] = optional count
4056 if ((goi.argc == 2) || (goi.argc == 3)) {
4057 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR [COUNT]", n->name);
4060 e = Jim_GetOpt_Wide(&goi, &a);
4065 e = Jim_GetOpt_Wide(&goi, &c);
4072 b = 1; /* shut up gcc */
4085 /* convert to "bytes" */
4087 /* count is now in 'BYTES' */
4093 e = target_read_memory(target, a, b, y / b, target_buf);
4094 if (e != ERROR_OK) {
4095 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
4099 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
4102 for (x = 0 ; (x < 16) && (x < y) ; x += 4) {
4103 z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
4104 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
4106 for (; (x < 16) ; x += 4) {
4107 Jim_fprintf(interp, interp->cookie_stdout, " ");
4111 for (x = 0 ; (x < 16) && (x < y) ; x += 2) {
4112 z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
4113 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
4115 for (; (x < 16) ; x += 2) {
4116 Jim_fprintf(interp, interp->cookie_stdout, " ");
4121 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4122 z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
4123 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
4125 for (; (x < 16) ; x += 1) {
4126 Jim_fprintf(interp, interp->cookie_stdout, " ");
4130 /* ascii-ify the bytes */
4131 for (x = 0 ; x < y ; x++) {
4132 if ((target_buf[x] >= 0x20) &&
4133 (target_buf[x] <= 0x7e)) {
4137 target_buf[x] = '.';
4142 target_buf[x] = ' ';
4147 /* print - with a newline */
4148 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
4154 case TS_CMD_MEM2ARRAY:
4155 return target_mem2array(goi.interp, target, goi.argc, goi.argv);
4157 case TS_CMD_ARRAY2MEM:
4158 return target_array2mem(goi.interp, target, goi.argc, goi.argv);
4160 case TS_CMD_EXAMINE:
4162 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4165 if (!target->tap->enabled)
4166 goto err_tap_disabled;
4167 e = target->type->examine(target);
4168 if (e != ERROR_OK) {
4169 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
4175 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4178 if (!target->tap->enabled)
4179 goto err_tap_disabled;
4180 if (!(target_was_examined(target))) {
4181 e = ERROR_TARGET_NOT_EXAMINED;
4183 e = target->type->poll(target);
4185 if (e != ERROR_OK) {
4186 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4193 if (goi.argc != 2) {
4194 Jim_WrongNumArgs(interp, 2, argv,
4195 "([tT]|[fF]|assert|deassert) BOOL");
4198 e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4200 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4203 /* the halt or not param */
4204 e = Jim_GetOpt_Wide(&goi, &a);
4208 if (!target->tap->enabled)
4209 goto err_tap_disabled;
4210 if (!target->type->assert_reset
4211 || !target->type->deassert_reset) {
4212 Jim_SetResult_sprintf(interp,
4213 "No target-specific reset for %s",
4217 /* determine if we should halt or not. */
4218 target->reset_halt = !!a;
4219 /* When this happens - all workareas are invalid. */
4220 target_free_all_working_areas_restore(target, 0);
4223 if (n->value == NVP_ASSERT) {
4224 e = target->type->assert_reset(target);
4226 e = target->type->deassert_reset(target);
4228 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4231 Jim_WrongNumArgs(goi.interp, 0, argv, "halt [no parameters]");
4234 if (!target->tap->enabled)
4235 goto err_tap_disabled;
4236 e = target->type->halt(target);
4237 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4238 case TS_CMD_WAITSTATE:
4239 /* params: <name> statename timeoutmsecs */
4240 if (goi.argc != 2) {
4241 Jim_SetResult_sprintf(goi.interp, "%s STATENAME TIMEOUTMSECS", n->name);
4244 e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4246 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4249 e = Jim_GetOpt_Wide(&goi, &a);
4253 if (!target->tap->enabled)
4254 goto err_tap_disabled;
4255 e = target_wait_state(target, n->value, a);
4256 if (e != ERROR_OK) {
4257 Jim_SetResult_sprintf(goi.interp,
4258 "target: %s wait %s fails (%d) %s",
4261 e, target_strerror_safe(e));
4266 case TS_CMD_EVENTLIST:
4267 /* List for human, Events defined for this target.
4268 * scripts/programs should use 'name cget -event NAME'
4271 target_event_action_t *teap;
4272 teap = target->event_action;
4273 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4274 target->target_number,
4276 command_print(cmd_ctx, "%-25s | Body", "Event");
4277 command_print(cmd_ctx, "------------------------- | ----------------------------------------");
4279 command_print(cmd_ctx,
4281 Jim_Nvp_value2name_simple(nvp_target_event, teap->event)->name,
4282 Jim_GetString(teap->body, NULL));
4285 command_print(cmd_ctx, "***END***");
4288 case TS_CMD_CURSTATE:
4289 if (goi.argc != 0) {
4290 Jim_WrongNumArgs(goi.interp, 0, argv, "[no parameters]");
4293 Jim_SetResultString(goi.interp,
4294 target_state_name( target ),
4297 case TS_CMD_INVOKE_EVENT:
4298 if (goi.argc != 1) {
4299 Jim_SetResult_sprintf(goi.interp, "%s ?EVENTNAME?",n->name);
4302 e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4304 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4307 target_handle_event(target, n->value);
4313 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4317 static int target_create(Jim_GetOptInfo *goi)
4326 struct command_context_s *cmd_ctx;
4328 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4329 if (goi->argc < 3) {
4330 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4335 Jim_GetOpt_Obj(goi, &new_cmd);
4336 /* does this command exist? */
4337 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4339 cp = Jim_GetString(new_cmd, NULL);
4340 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4345 e = Jim_GetOpt_String(goi, &cp2, NULL);
4347 /* now does target type exist */
4348 for (x = 0 ; target_types[x] ; x++) {
4349 if (0 == strcmp(cp, target_types[x]->name)) {
4354 if (target_types[x] == NULL) {
4355 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4356 for (x = 0 ; target_types[x] ; x++) {
4357 if (target_types[x + 1]) {
4358 Jim_AppendStrings(goi->interp,
4359 Jim_GetResult(goi->interp),
4360 target_types[x]->name,
4363 Jim_AppendStrings(goi->interp,
4364 Jim_GetResult(goi->interp),
4366 target_types[x]->name,NULL);
4373 target = calloc(1,sizeof(target_t));
4374 /* set target number */
4375 target->target_number = new_target_number();
4377 /* allocate memory for each unique target type */
4378 target->type = (target_type_t*)calloc(1,sizeof(target_type_t));
4380 memcpy(target->type, target_types[x], sizeof(target_type_t));
4382 /* will be set by "-endian" */
4383 target->endianness = TARGET_ENDIAN_UNKNOWN;
4385 target->working_area = 0x0;
4386 target->working_area_size = 0x0;
4387 target->working_areas = NULL;
4388 target->backup_working_area = 0;
4390 target->state = TARGET_UNKNOWN;
4391 target->debug_reason = DBG_REASON_UNDEFINED;
4392 target->reg_cache = NULL;
4393 target->breakpoints = NULL;
4394 target->watchpoints = NULL;
4395 target->next = NULL;
4396 target->arch_info = NULL;
4398 target->display = 1;
4400 target->halt_issued = false;
4402 /* initialize trace information */
4403 target->trace_info = malloc(sizeof(trace_t));
4404 target->trace_info->num_trace_points = 0;
4405 target->trace_info->trace_points_size = 0;
4406 target->trace_info->trace_points = NULL;
4407 target->trace_info->trace_history_size = 0;
4408 target->trace_info->trace_history = NULL;
4409 target->trace_info->trace_history_pos = 0;
4410 target->trace_info->trace_history_overflowed = 0;
4412 target->dbgmsg = NULL;
4413 target->dbg_msg_enabled = 0;
4415 target->endianness = TARGET_ENDIAN_UNKNOWN;
4417 /* Do the rest as "configure" options */
4418 goi->isconfigure = 1;
4419 e = target_configure(goi, target);
4421 if (target->tap == NULL)
4423 Jim_SetResultString(interp, "-chain-position required when creating target", -1);
4433 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4434 /* default endian to little if not specified */
4435 target->endianness = TARGET_LITTLE_ENDIAN;
4438 /* incase variant is not set */
4439 if (!target->variant)
4440 target->variant = strdup("");
4442 /* create the target specific commands */
4443 if (target->type->register_commands) {
4444 (*(target->type->register_commands))(cmd_ctx);
4446 if (target->type->target_create) {
4447 (*(target->type->target_create))(target, goi->interp);
4450 /* append to end of list */
4453 tpp = &(all_targets);
4455 tpp = &((*tpp)->next);
4460 cp = Jim_GetString(new_cmd, NULL);
4461 target->cmd_name = strdup(cp);
4463 /* now - create the new target name command */
4464 e = Jim_CreateCommand(goi->interp,
4467 tcl_target_func, /* C function */
4468 target, /* private data */
4469 NULL); /* no del proc */
4474 static int jim_target(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4478 struct command_context_s *cmd_ctx;
4482 /* TG = target generic */
4490 const char *target_cmds[] = {
4491 "create", "types", "names", "current", "number",
4493 NULL /* terminate */
4496 LOG_DEBUG("Target command params:");
4497 LOG_DEBUG("%s", Jim_Debug_ArgvString(interp, argc, argv));
4499 cmd_ctx = Jim_GetAssocData(interp, "context");
4501 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
4503 if (goi.argc == 0) {
4504 Jim_WrongNumArgs(interp, 1, argv, "missing: command ...");
4508 /* Jim_GetOpt_Debug(&goi); */
4509 r = Jim_GetOpt_Enum(&goi, target_cmds, &x);
4516 Jim_Panic(goi.interp,"Why am I here?");
4518 case TG_CMD_CURRENT:
4519 if (goi.argc != 0) {
4520 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4523 Jim_SetResultString(goi.interp, get_current_target(cmd_ctx)->cmd_name, -1);
4526 if (goi.argc != 0) {
4527 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4530 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4531 for (x = 0 ; target_types[x] ; x++) {
4532 Jim_ListAppendElement(goi.interp,
4533 Jim_GetResult(goi.interp),
4534 Jim_NewStringObj(goi.interp, target_types[x]->name, -1));
4538 if (goi.argc != 0) {
4539 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4542 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4543 target = all_targets;
4545 Jim_ListAppendElement(goi.interp,
4546 Jim_GetResult(goi.interp),
4547 Jim_NewStringObj(goi.interp, target->cmd_name, -1));
4548 target = target->next;
4553 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "?name ... config options ...");
4556 return target_create(&goi);
4559 /* It's OK to remove this mechanism sometime after August 2010 or so */
4560 LOG_WARNING("don't use numbers as target identifiers; use names");
4561 if (goi.argc != 1) {
4562 Jim_SetResult_sprintf(goi.interp, "expected: target number ?NUMBER?");
4565 e = Jim_GetOpt_Wide(&goi, &w);
4569 for (x = 0, target = all_targets; target; target = target->next, x++) {
4570 if (target->target_number == w)
4573 if (target == NULL) {
4574 Jim_SetResult_sprintf(goi.interp,
4575 "Target: number %d does not exist", (int)(w));
4578 Jim_SetResultString(goi.interp, target->cmd_name, -1);
4581 if (goi.argc != 0) {
4582 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "<no parameters>");
4585 for (x = 0, target = all_targets; target; target = target->next, x++)
4587 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, x));
4603 static int fastload_num;
4604 static struct FastLoad *fastload;
4606 static void free_fastload(void)
4608 if (fastload != NULL)
4611 for (i = 0; i < fastload_num; i++)
4613 if (fastload[i].data)
4614 free(fastload[i].data);
4624 static int handle_fast_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4628 uint32_t image_size;
4629 uint32_t min_address = 0;
4630 uint32_t max_address = 0xffffffff;
4635 duration_t duration;
4636 char *duration_text;
4638 int retval = parse_load_image_command_args(args, argc,
4639 &image, &min_address, &max_address);
4640 if (ERROR_OK != retval)
4643 duration_start_measure(&duration);
4645 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
4652 fastload_num = image.num_sections;
4653 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4654 if (fastload == NULL)
4656 image_close(&image);
4659 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4660 for (i = 0; i < image.num_sections; i++)
4662 buffer = malloc(image.sections[i].size);
4665 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)",
4666 (int)(image.sections[i].size));
4670 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4676 uint32_t offset = 0;
4677 uint32_t length = buf_cnt;
4680 /* DANGER!!! beware of unsigned comparision here!!! */
4682 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4683 (image.sections[i].base_address < max_address))
4685 if (image.sections[i].base_address < min_address)
4687 /* clip addresses below */
4688 offset += min_address-image.sections[i].base_address;
4692 if (image.sections[i].base_address + buf_cnt > max_address)
4694 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4697 fastload[i].address = image.sections[i].base_address + offset;
4698 fastload[i].data = malloc(length);
4699 if (fastload[i].data == NULL)
4704 memcpy(fastload[i].data, buffer + offset, length);
4705 fastload[i].length = length;
4707 image_size += length;
4708 command_print(cmd_ctx, "%u bytes written at address 0x%8.8x",
4709 (unsigned int)length,
4710 ((unsigned int)(image.sections[i].base_address + offset)));
4716 duration_stop_measure(&duration, &duration_text);
4717 if (retval == ERROR_OK)
4719 command_print(cmd_ctx, "Loaded %u bytes in %s", (unsigned int)image_size, duration_text);
4720 command_print(cmd_ctx, "NB!!! image has not been loaded to target, issue a subsequent 'fast_load' to do so.");
4722 free(duration_text);
4724 image_close(&image);
4726 if (retval != ERROR_OK)
4734 static int handle_fast_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4737 return ERROR_COMMAND_SYNTAX_ERROR;
4738 if (fastload == NULL)
4740 LOG_ERROR("No image in memory");
4744 int ms = timeval_ms();
4746 int retval = ERROR_OK;
4747 for (i = 0; i < fastload_num;i++)
4749 target_t *target = get_current_target(cmd_ctx);
4750 command_print(cmd_ctx, "Write to 0x%08x, length 0x%08x",
4751 (unsigned int)(fastload[i].address),
4752 (unsigned int)(fastload[i].length));
4753 if (retval == ERROR_OK)
4755 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4757 size += fastload[i].length;
4759 int after = timeval_ms();
4760 command_print(cmd_ctx, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
4764 static int jim_mcrmrc(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4766 command_context_t *context;
4770 context = Jim_GetAssocData(interp, "context");
4771 if (context == NULL) {
4772 LOG_ERROR("array2mem: no command context");
4775 target = get_current_target(context);
4776 if (target == NULL) {
4777 LOG_ERROR("array2mem: no current target");
4781 if ((argc < 6) || (argc > 7))
4795 e = Jim_GetLong(interp, argv[1], &l);
4801 e = Jim_GetLong(interp, argv[2], &l);
4807 e = Jim_GetLong(interp, argv[3], &l);
4813 e = Jim_GetLong(interp, argv[4], &l);
4819 e = Jim_GetLong(interp, argv[5], &l);
4827 LOG_DEBUG("%d %d %d %d %d %d", cpnum, op1, op2, CRn, CRm, value);
4831 e = Jim_GetLong(interp, argv[6], &l);
4837 retval = target_mcr(target, cpnum, op1, op2, CRn, CRm, value);
4838 if (retval != ERROR_OK)
4842 retval = target_mrc(target, cpnum, op1, op2, CRn, CRm, &value);
4843 if (retval != ERROR_OK)
4846 Jim_SetResult(interp, Jim_NewIntObj(interp, value));