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)
499 LOG_ERROR("Not implemented.");
503 static int default_has_mmu(struct target_s *target, bool *has_mmu)
509 static int default_examine(struct target_s *target)
511 target_set_examined(target);
515 int target_examine_one(struct target_s *target)
517 return target->type->examine(target);
520 static int jtag_enable_callback(enum jtag_event event, void *priv)
522 target_t *target = priv;
524 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
527 jtag_unregister_event_callback(jtag_enable_callback, target);
528 return target_examine_one(target);
532 /* Targets that correctly implement init + examine, i.e.
533 * no communication with target during init:
537 int target_examine(void)
539 int retval = ERROR_OK;
542 for (target = all_targets; target; target = target->next)
544 /* defer examination, but don't skip it */
545 if (!target->tap->enabled) {
546 jtag_register_event_callback(jtag_enable_callback,
550 if ((retval = target_examine_one(target)) != ERROR_OK)
555 const char *target_get_name(struct target_s *target)
557 return target->type->name;
560 static int target_write_memory_imp(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
562 if (!target_was_examined(target))
564 LOG_ERROR("Target not examined yet");
567 return target->type->write_memory_imp(target, address, size, count, buffer);
570 static int target_read_memory_imp(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
572 if (!target_was_examined(target))
574 LOG_ERROR("Target not examined yet");
577 return target->type->read_memory_imp(target, address, size, count, buffer);
580 static int target_soft_reset_halt_imp(struct target_s *target)
582 if (!target_was_examined(target))
584 LOG_ERROR("Target not examined yet");
587 if (!target->type->soft_reset_halt_imp) {
588 LOG_ERROR("Target %s does not support soft_reset_halt",
592 return target->type->soft_reset_halt_imp(target);
595 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)
597 if (!target_was_examined(target))
599 LOG_ERROR("Target not examined yet");
602 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);
605 int target_read_memory(struct target_s *target,
606 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
608 return target->type->read_memory(target, address, size, count, buffer);
611 int target_read_phys_memory(struct target_s *target,
612 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
614 return target->type->read_phys_memory(target, address, size, count, buffer);
617 int target_write_memory(struct target_s *target,
618 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
620 return target->type->write_memory(target, address, size, count, buffer);
623 int target_write_phys_memory(struct target_s *target,
624 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
626 return target->type->write_phys_memory(target, address, size, count, buffer);
629 int target_bulk_write_memory(struct target_s *target,
630 uint32_t address, uint32_t count, uint8_t *buffer)
632 return target->type->bulk_write_memory(target, address, count, buffer);
635 int target_add_breakpoint(struct target_s *target,
636 struct breakpoint_s *breakpoint)
638 return target->type->add_breakpoint(target, breakpoint);
640 int target_remove_breakpoint(struct target_s *target,
641 struct breakpoint_s *breakpoint)
643 return target->type->remove_breakpoint(target, breakpoint);
646 int target_add_watchpoint(struct target_s *target,
647 struct watchpoint_s *watchpoint)
649 return target->type->add_watchpoint(target, watchpoint);
651 int target_remove_watchpoint(struct target_s *target,
652 struct watchpoint_s *watchpoint)
654 return target->type->remove_watchpoint(target, watchpoint);
657 int target_get_gdb_reg_list(struct target_s *target,
658 struct reg_s **reg_list[], int *reg_list_size)
660 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
662 int target_step(struct target_s *target,
663 int current, uint32_t address, int handle_breakpoints)
665 return target->type->step(target, current, address, handle_breakpoints);
669 int target_run_algorithm(struct target_s *target,
670 int num_mem_params, mem_param_t *mem_params,
671 int num_reg_params, reg_param_t *reg_param,
672 uint32_t entry_point, uint32_t exit_point,
673 int timeout_ms, void *arch_info)
675 return target->type->run_algorithm(target,
676 num_mem_params, mem_params, num_reg_params, reg_param,
677 entry_point, exit_point, timeout_ms, arch_info);
680 /// @returns @c true if the target has been examined.
681 bool target_was_examined(struct target_s *target)
683 return target->type->examined;
685 /// Sets the @c examined flag for the given target.
686 void target_set_examined(struct target_s *target)
688 target->type->examined = true;
690 // Reset the @c examined flag for the given target.
691 void target_reset_examined(struct target_s *target)
693 target->type->examined = false;
698 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)
700 LOG_ERROR("Not implemented");
704 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)
706 LOG_ERROR("Not implemented");
710 static int arm_cp_check(struct target_s *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm)
713 if (!target_was_examined(target))
715 LOG_ERROR("Target not examined yet");
719 if ((cpnum <0) || (cpnum > 15))
721 LOG_ERROR("Illegal co-processor %d", cpnum);
727 LOG_ERROR("Illegal op1");
733 LOG_ERROR("Illegal op2");
739 LOG_ERROR("Illegal CRn");
745 LOG_ERROR("Illegal CRm");
752 int target_mrc(struct target_s *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t *value)
756 retval = arm_cp_check(target, cpnum, op1, op2, CRn, CRm);
757 if (retval != ERROR_OK)
760 return target->type->mrc(target, cpnum, op1, op2, CRn, CRm, value);
763 int target_mcr(struct target_s *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t value)
767 retval = arm_cp_check(target, cpnum, op1, op2, CRn, CRm);
768 if (retval != ERROR_OK)
771 return target->type->mcr(target, cpnum, op1, op2, CRn, CRm, value);
774 static int default_read_phys_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
778 retval = target->type->has_mmu(target, &mmu);
779 if (retval != ERROR_OK)
783 LOG_ERROR("Not implemented");
786 return target_read_memory(target, address, size, count, buffer);
789 static int default_write_phys_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
793 retval = target->type->has_mmu(target, &mmu);
794 if (retval != ERROR_OK)
798 LOG_ERROR("Not implemented");
801 return target_write_memory(target, address, size, count, buffer);
805 int target_init(struct command_context_s *cmd_ctx)
807 target_t *target = all_targets;
812 target_reset_examined(target);
813 if (target->type->examine == NULL)
815 target->type->examine = default_examine;
818 if ((retval = target->type->init_target(cmd_ctx, target)) != ERROR_OK)
820 LOG_ERROR("target '%s' init failed", target_get_name(target));
824 /* Set up default functions if none are provided by target */
825 if (target->type->virt2phys == NULL)
827 target->type->virt2phys = default_virt2phys;
830 if (target->type->read_phys_memory == NULL)
832 target->type->read_phys_memory = default_read_phys_memory;
835 if (target->type->write_phys_memory == NULL)
837 target->type->write_phys_memory = default_write_phys_memory;
840 if (target->type->mcr == NULL)
842 target->type->mcr = default_mcr;
845 /* FIX! multiple targets will generally register global commands
846 * multiple times. Only register this one if *one* of the
847 * targets need the command. Hmm... make it a command on the
848 * Jim Tcl target object?
850 register_jim(cmd_ctx, "mcr", jim_mcrmrc, "write coprocessor <cpnum> <op1> <op2> <CRn> <CRm> <value>");
853 if (target->type->mrc == NULL)
855 target->type->mrc = default_mrc;
858 register_jim(cmd_ctx, "mrc", jim_mcrmrc, "read coprocessor <cpnum> <op1> <op2> <CRn> <CRm>");
862 /* a non-invasive way(in terms of patches) to add some code that
863 * runs before the type->write/read_memory implementation
865 target->type->write_memory_imp = target->type->write_memory;
866 target->type->write_memory = target_write_memory_imp;
867 target->type->read_memory_imp = target->type->read_memory;
868 target->type->read_memory = target_read_memory_imp;
869 target->type->soft_reset_halt_imp = target->type->soft_reset_halt;
870 target->type->soft_reset_halt = target_soft_reset_halt_imp;
871 target->type->run_algorithm_imp = target->type->run_algorithm;
872 target->type->run_algorithm = target_run_algorithm_imp;
874 if (target->type->mmu == NULL)
876 target->type->mmu = default_mmu;
878 if (target->type->has_mmu == NULL)
880 target->type->has_mmu = default_has_mmu;
882 target = target->next;
887 if ((retval = target_register_user_commands(cmd_ctx)) != ERROR_OK)
889 if ((retval = target_register_timer_callback(handle_target, 100, 1, NULL)) != ERROR_OK)
896 int target_register_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
898 target_event_callback_t **callbacks_p = &target_event_callbacks;
900 if (callback == NULL)
902 return ERROR_INVALID_ARGUMENTS;
907 while ((*callbacks_p)->next)
908 callbacks_p = &((*callbacks_p)->next);
909 callbacks_p = &((*callbacks_p)->next);
912 (*callbacks_p) = malloc(sizeof(target_event_callback_t));
913 (*callbacks_p)->callback = callback;
914 (*callbacks_p)->priv = priv;
915 (*callbacks_p)->next = NULL;
920 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
922 target_timer_callback_t **callbacks_p = &target_timer_callbacks;
925 if (callback == NULL)
927 return ERROR_INVALID_ARGUMENTS;
932 while ((*callbacks_p)->next)
933 callbacks_p = &((*callbacks_p)->next);
934 callbacks_p = &((*callbacks_p)->next);
937 (*callbacks_p) = malloc(sizeof(target_timer_callback_t));
938 (*callbacks_p)->callback = callback;
939 (*callbacks_p)->periodic = periodic;
940 (*callbacks_p)->time_ms = time_ms;
942 gettimeofday(&now, NULL);
943 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
944 time_ms -= (time_ms % 1000);
945 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
946 if ((*callbacks_p)->when.tv_usec > 1000000)
948 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
949 (*callbacks_p)->when.tv_sec += 1;
952 (*callbacks_p)->priv = priv;
953 (*callbacks_p)->next = NULL;
958 int target_unregister_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
960 target_event_callback_t **p = &target_event_callbacks;
961 target_event_callback_t *c = target_event_callbacks;
963 if (callback == NULL)
965 return ERROR_INVALID_ARGUMENTS;
970 target_event_callback_t *next = c->next;
971 if ((c->callback == callback) && (c->priv == priv))
985 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
987 target_timer_callback_t **p = &target_timer_callbacks;
988 target_timer_callback_t *c = target_timer_callbacks;
990 if (callback == NULL)
992 return ERROR_INVALID_ARGUMENTS;
997 target_timer_callback_t *next = c->next;
998 if ((c->callback == callback) && (c->priv == priv))
1012 int target_call_event_callbacks(target_t *target, enum target_event event)
1014 target_event_callback_t *callback = target_event_callbacks;
1015 target_event_callback_t *next_callback;
1017 if (event == TARGET_EVENT_HALTED)
1019 /* execute early halted first */
1020 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1023 LOG_DEBUG("target event %i (%s)",
1025 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
1027 target_handle_event(target, event);
1031 next_callback = callback->next;
1032 callback->callback(target, event, callback->priv);
1033 callback = next_callback;
1039 static int target_timer_callback_periodic_restart(
1040 target_timer_callback_t *cb, struct timeval *now)
1042 int time_ms = cb->time_ms;
1043 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
1044 time_ms -= (time_ms % 1000);
1045 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
1046 if (cb->when.tv_usec > 1000000)
1048 cb->when.tv_usec = cb->when.tv_usec - 1000000;
1049 cb->when.tv_sec += 1;
1054 static int target_call_timer_callback(target_timer_callback_t *cb,
1055 struct timeval *now)
1057 cb->callback(cb->priv);
1060 return target_timer_callback_periodic_restart(cb, now);
1062 return target_unregister_timer_callback(cb->callback, cb->priv);
1065 static int target_call_timer_callbacks_check_time(int checktime)
1070 gettimeofday(&now, NULL);
1072 target_timer_callback_t *callback = target_timer_callbacks;
1075 // cleaning up may unregister and free this callback
1076 target_timer_callback_t *next_callback = callback->next;
1078 bool call_it = callback->callback &&
1079 ((!checktime && callback->periodic) ||
1080 now.tv_sec > callback->when.tv_sec ||
1081 (now.tv_sec == callback->when.tv_sec &&
1082 now.tv_usec >= callback->when.tv_usec));
1086 int retval = target_call_timer_callback(callback, &now);
1087 if (retval != ERROR_OK)
1091 callback = next_callback;
1097 int target_call_timer_callbacks(void)
1099 return target_call_timer_callbacks_check_time(1);
1102 /* invoke periodic callbacks immediately */
1103 int target_call_timer_callbacks_now(void)
1105 return target_call_timer_callbacks_check_time(0);
1108 int target_alloc_working_area(struct target_s *target, uint32_t size, working_area_t **area)
1110 working_area_t *c = target->working_areas;
1111 working_area_t *new_wa = NULL;
1113 /* Reevaluate working area address based on MMU state*/
1114 if (target->working_areas == NULL)
1118 retval = target->type->mmu(target, &enabled);
1119 if (retval != ERROR_OK)
1126 if (target->working_area_phys_spec)
1128 LOG_DEBUG("MMU disabled, using physical address for working memory 0x%08x", (unsigned)target->working_area_phys);
1129 target->working_area = target->working_area_phys;
1132 LOG_ERROR("No working memory available. Specify -work-area-phys to target.");
1133 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1137 if (target->working_area_virt_spec)
1139 LOG_DEBUG("MMU enabled, using virtual address for working memory 0x%08x", (unsigned)target->working_area_virt);
1140 target->working_area = target->working_area_virt;
1143 LOG_ERROR("No working memory available. Specify -work-area-virt to target.");
1144 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1149 /* only allocate multiples of 4 byte */
1152 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
1153 size = (size + 3) & (~3);
1156 /* see if there's already a matching working area */
1159 if ((c->free) && (c->size == size))
1167 /* if not, allocate a new one */
1170 working_area_t **p = &target->working_areas;
1171 uint32_t first_free = target->working_area;
1172 uint32_t free_size = target->working_area_size;
1174 c = target->working_areas;
1177 first_free += c->size;
1178 free_size -= c->size;
1183 if (free_size < size)
1185 LOG_WARNING("not enough working area available(requested %u, free %u)",
1186 (unsigned)(size), (unsigned)(free_size));
1187 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1190 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free);
1192 new_wa = malloc(sizeof(working_area_t));
1193 new_wa->next = NULL;
1194 new_wa->size = size;
1195 new_wa->address = first_free;
1197 if (target->backup_working_area)
1200 new_wa->backup = malloc(new_wa->size);
1201 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1203 free(new_wa->backup);
1210 new_wa->backup = NULL;
1213 /* put new entry in list */
1217 /* mark as used, and return the new (reused) area */
1222 new_wa->user = area;
1227 int target_free_working_area_restore(struct target_s *target, working_area_t *area, int restore)
1232 if (restore && target->backup_working_area)
1235 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1241 /* mark user pointer invalid */
1248 int target_free_working_area(struct target_s *target, working_area_t *area)
1250 return target_free_working_area_restore(target, area, 1);
1253 /* free resources and restore memory, if restoring memory fails,
1254 * free up resources anyway
1256 void target_free_all_working_areas_restore(struct target_s *target, int restore)
1258 working_area_t *c = target->working_areas;
1262 working_area_t *next = c->next;
1263 target_free_working_area_restore(target, c, restore);
1273 target->working_areas = NULL;
1276 void target_free_all_working_areas(struct target_s *target)
1278 target_free_all_working_areas_restore(target, 1);
1281 int target_register_commands(struct command_context_s *cmd_ctx)
1284 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)");
1289 register_jim(cmd_ctx, "target", jim_target, "configure target");
1294 int target_arch_state(struct target_s *target)
1299 LOG_USER("No target has been configured");
1303 LOG_USER("target state: %s", target_state_name( target ));
1305 if (target->state != TARGET_HALTED)
1308 retval = target->type->arch_state(target);
1312 /* Single aligned words are guaranteed to use 16 or 32 bit access
1313 * mode respectively, otherwise data is handled as quickly as
1316 int target_write_buffer(struct target_s *target, uint32_t address, uint32_t size, uint8_t *buffer)
1319 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1320 (int)size, (unsigned)address);
1322 if (!target_was_examined(target))
1324 LOG_ERROR("Target not examined yet");
1332 if ((address + size - 1) < address)
1334 /* GDB can request this when e.g. PC is 0xfffffffc*/
1335 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1341 if (((address % 2) == 0) && (size == 2))
1343 return target_write_memory(target, address, 2, 1, buffer);
1346 /* handle unaligned head bytes */
1349 uint32_t unaligned = 4 - (address % 4);
1351 if (unaligned > size)
1354 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1357 buffer += unaligned;
1358 address += unaligned;
1362 /* handle aligned words */
1365 int aligned = size - (size % 4);
1367 /* use bulk writes above a certain limit. This may have to be changed */
1370 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1375 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1384 /* handle tail writes of less than 4 bytes */
1387 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1394 /* Single aligned words are guaranteed to use 16 or 32 bit access
1395 * mode respectively, otherwise data is handled as quickly as
1398 int target_read_buffer(struct target_s *target, uint32_t address, uint32_t size, uint8_t *buffer)
1401 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1402 (int)size, (unsigned)address);
1404 if (!target_was_examined(target))
1406 LOG_ERROR("Target not examined yet");
1414 if ((address + size - 1) < address)
1416 /* GDB can request this when e.g. PC is 0xfffffffc*/
1417 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1423 if (((address % 2) == 0) && (size == 2))
1425 return target_read_memory(target, address, 2, 1, buffer);
1428 /* handle unaligned head bytes */
1431 uint32_t unaligned = 4 - (address % 4);
1433 if (unaligned > size)
1436 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1439 buffer += unaligned;
1440 address += unaligned;
1444 /* handle aligned words */
1447 int aligned = size - (size % 4);
1449 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1457 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1460 int aligned = size - (size%2);
1461 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1462 if (retval != ERROR_OK)
1469 /* handle tail writes of less than 4 bytes */
1472 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1479 int target_checksum_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t* crc)
1484 uint32_t checksum = 0;
1485 if (!target_was_examined(target))
1487 LOG_ERROR("Target not examined yet");
1491 if ((retval = target->type->checksum_memory(target, address,
1492 size, &checksum)) != ERROR_OK)
1494 buffer = malloc(size);
1497 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1498 return ERROR_INVALID_ARGUMENTS;
1500 retval = target_read_buffer(target, address, size, buffer);
1501 if (retval != ERROR_OK)
1507 /* convert to target endianess */
1508 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1510 uint32_t target_data;
1511 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1512 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1515 retval = image_calculate_checksum(buffer, size, &checksum);
1524 int target_blank_check_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t* blank)
1527 if (!target_was_examined(target))
1529 LOG_ERROR("Target not examined yet");
1533 if (target->type->blank_check_memory == 0)
1534 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1536 retval = target->type->blank_check_memory(target, address, size, blank);
1541 int target_read_u32(struct target_s *target, uint32_t address, uint32_t *value)
1543 uint8_t value_buf[4];
1544 if (!target_was_examined(target))
1546 LOG_ERROR("Target not examined yet");
1550 int retval = target_read_memory(target, address, 4, 1, value_buf);
1552 if (retval == ERROR_OK)
1554 *value = target_buffer_get_u32(target, value_buf);
1555 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1562 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1569 int target_read_u16(struct target_s *target, uint32_t address, uint16_t *value)
1571 uint8_t value_buf[2];
1572 if (!target_was_examined(target))
1574 LOG_ERROR("Target not examined yet");
1578 int retval = target_read_memory(target, address, 2, 1, value_buf);
1580 if (retval == ERROR_OK)
1582 *value = target_buffer_get_u16(target, value_buf);
1583 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1590 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1597 int target_read_u8(struct target_s *target, uint32_t address, uint8_t *value)
1599 int retval = target_read_memory(target, address, 1, 1, value);
1600 if (!target_was_examined(target))
1602 LOG_ERROR("Target not examined yet");
1606 if (retval == ERROR_OK)
1608 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1615 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1622 int target_write_u32(struct target_s *target, uint32_t address, uint32_t value)
1625 uint8_t value_buf[4];
1626 if (!target_was_examined(target))
1628 LOG_ERROR("Target not examined yet");
1632 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1636 target_buffer_set_u32(target, value_buf, value);
1637 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1639 LOG_DEBUG("failed: %i", retval);
1645 int target_write_u16(struct target_s *target, uint32_t address, uint16_t value)
1648 uint8_t value_buf[2];
1649 if (!target_was_examined(target))
1651 LOG_ERROR("Target not examined yet");
1655 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1659 target_buffer_set_u16(target, value_buf, value);
1660 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1662 LOG_DEBUG("failed: %i", retval);
1668 int target_write_u8(struct target_s *target, uint32_t address, uint8_t value)
1671 if (!target_was_examined(target))
1673 LOG_ERROR("Target not examined yet");
1677 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1680 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1682 LOG_DEBUG("failed: %i", retval);
1688 int target_register_user_commands(struct command_context_s *cmd_ctx)
1690 int retval = ERROR_OK;
1693 /* script procedures */
1694 register_command(cmd_ctx, NULL, "profile", handle_profile_command, COMMAND_EXEC, "profiling samples the CPU PC");
1695 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>");
1696 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>");
1698 register_command(cmd_ctx, NULL, "fast_load_image", handle_fast_load_image_command, COMMAND_ANY,
1699 "same args as load_image, image stored in memory - mainly for profiling purposes");
1701 register_command(cmd_ctx, NULL, "fast_load", handle_fast_load_command, COMMAND_ANY,
1702 "loads active fast load image to current target - mainly for profiling purposes");
1705 register_command(cmd_ctx, NULL, "virt2phys", handle_virt2phys_command, COMMAND_ANY, "translate a virtual address into a physical address");
1706 register_command(cmd_ctx, NULL, "reg", handle_reg_command, COMMAND_EXEC, "display or set a register");
1707 register_command(cmd_ctx, NULL, "poll", handle_poll_command, COMMAND_EXEC, "poll target state");
1708 register_command(cmd_ctx, NULL, "wait_halt", handle_wait_halt_command, COMMAND_EXEC, "wait for target halt [time (s)]");
1709 register_command(cmd_ctx, NULL, "halt", handle_halt_command, COMMAND_EXEC, "halt target");
1710 register_command(cmd_ctx, NULL, "resume", handle_resume_command, COMMAND_EXEC, "resume target [addr]");
1711 register_command(cmd_ctx, NULL, "step", handle_step_command, COMMAND_EXEC, "step one instruction from current PC or [addr]");
1712 register_command(cmd_ctx, NULL, "reset", handle_reset_command, COMMAND_EXEC, "reset target [run | halt | init] - default is run");
1713 register_command(cmd_ctx, NULL, "soft_reset_halt", handle_soft_reset_halt_command, COMMAND_EXEC, "halt the target and do a soft reset");
1715 register_command(cmd_ctx, NULL, "mdw", handle_md_command, COMMAND_EXEC, "display memory words [phys] <addr> [count]");
1716 register_command(cmd_ctx, NULL, "mdh", handle_md_command, COMMAND_EXEC, "display memory half-words [phys] <addr> [count]");
1717 register_command(cmd_ctx, NULL, "mdb", handle_md_command, COMMAND_EXEC, "display memory bytes [phys] <addr> [count]");
1719 register_command(cmd_ctx, NULL, "mww", handle_mw_command, COMMAND_EXEC, "write memory word [phys] <addr> <value> [count]");
1720 register_command(cmd_ctx, NULL, "mwh", handle_mw_command, COMMAND_EXEC, "write memory half-word [phys] <addr> <value> [count]");
1721 register_command(cmd_ctx, NULL, "mwb", handle_mw_command, COMMAND_EXEC, "write memory byte [phys] <addr> <value> [count]");
1723 register_command(cmd_ctx, NULL, "bp",
1724 handle_bp_command, COMMAND_EXEC,
1725 "list or set breakpoint [<address> <length> [hw]]");
1726 register_command(cmd_ctx, NULL, "rbp",
1727 handle_rbp_command, COMMAND_EXEC,
1728 "remove breakpoint <address>");
1729 register_command(cmd_ctx, NULL, "wp",
1730 handle_wp_command, COMMAND_EXEC,
1731 "list or set watchpoint "
1732 "[<address> <length> <r/w/a> [value] [mask]]");
1733 register_command(cmd_ctx, NULL, "rwp",
1734 handle_rwp_command, COMMAND_EXEC,
1735 "remove watchpoint <address>");
1737 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]");
1738 register_command(cmd_ctx, NULL, "dump_image", handle_dump_image_command, COMMAND_EXEC, "dump_image <file> <address> <size>");
1739 register_command(cmd_ctx, NULL, "verify_image", handle_verify_image_command, COMMAND_EXEC, "verify_image <file> [offset] [type]");
1740 register_command(cmd_ctx, NULL, "test_image", handle_test_image_command, COMMAND_EXEC, "test_image <file> [offset] [type]");
1742 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
1744 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
1750 static int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1752 target_t *target = all_targets;
1756 target = get_target(args[0]);
1757 if (target == NULL) {
1758 command_print(cmd_ctx,"Target: %s is unknown, try one of:\n", args[0]);
1761 if (!target->tap->enabled) {
1762 command_print(cmd_ctx,"Target: TAP %s is disabled, "
1763 "can't be the current target\n",
1764 target->tap->dotted_name);
1768 cmd_ctx->current_target = target->target_number;
1773 target = all_targets;
1774 command_print(cmd_ctx, " TargetName Type Endian TapName State ");
1775 command_print(cmd_ctx, "-- ------------------ ---------- ------ ------------------ ------------");
1781 if (target->tap->enabled)
1782 state = target_state_name( target );
1784 state = "tap-disabled";
1786 if (cmd_ctx->current_target == target->target_number)
1789 /* keep columns lined up to match the headers above */
1790 command_print(cmd_ctx, "%2d%c %-18s %-10s %-6s %-18s %s",
1791 target->target_number,
1794 target_get_name(target),
1795 Jim_Nvp_value2name_simple(nvp_target_endian,
1796 target->endianness)->name,
1797 target->tap->dotted_name,
1799 target = target->next;
1805 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1807 static int powerDropout;
1808 static int srstAsserted;
1810 static int runPowerRestore;
1811 static int runPowerDropout;
1812 static int runSrstAsserted;
1813 static int runSrstDeasserted;
1815 static int sense_handler(void)
1817 static int prevSrstAsserted = 0;
1818 static int prevPowerdropout = 0;
1821 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1825 powerRestored = prevPowerdropout && !powerDropout;
1828 runPowerRestore = 1;
1831 long long current = timeval_ms();
1832 static long long lastPower = 0;
1833 int waitMore = lastPower + 2000 > current;
1834 if (powerDropout && !waitMore)
1836 runPowerDropout = 1;
1837 lastPower = current;
1840 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1844 srstDeasserted = prevSrstAsserted && !srstAsserted;
1846 static long long lastSrst = 0;
1847 waitMore = lastSrst + 2000 > current;
1848 if (srstDeasserted && !waitMore)
1850 runSrstDeasserted = 1;
1854 if (!prevSrstAsserted && srstAsserted)
1856 runSrstAsserted = 1;
1859 prevSrstAsserted = srstAsserted;
1860 prevPowerdropout = powerDropout;
1862 if (srstDeasserted || powerRestored)
1864 /* Other than logging the event we can't do anything here.
1865 * Issuing a reset is a particularly bad idea as we might
1866 * be inside a reset already.
1873 static void target_call_event_callbacks_all(enum target_event e) {
1875 target = all_targets;
1877 target_call_event_callbacks(target, e);
1878 target = target->next;
1882 /* process target state changes */
1883 int handle_target(void *priv)
1885 int retval = ERROR_OK;
1887 /* we do not want to recurse here... */
1888 static int recursive = 0;
1893 /* danger! running these procedures can trigger srst assertions and power dropouts.
1894 * We need to avoid an infinite loop/recursion here and we do that by
1895 * clearing the flags after running these events.
1897 int did_something = 0;
1898 if (runSrstAsserted)
1900 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1901 Jim_Eval(interp, "srst_asserted");
1904 if (runSrstDeasserted)
1906 Jim_Eval(interp, "srst_deasserted");
1909 if (runPowerDropout)
1911 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1912 Jim_Eval(interp, "power_dropout");
1915 if (runPowerRestore)
1917 Jim_Eval(interp, "power_restore");
1923 /* clear detect flags */
1927 /* clear action flags */
1929 runSrstAsserted = 0;
1930 runSrstDeasserted = 0;
1931 runPowerRestore = 0;
1932 runPowerDropout = 0;
1937 /* Poll targets for state changes unless that's globally disabled.
1938 * Skip targets that are currently disabled.
1940 for (target_t *target = all_targets;
1941 is_jtag_poll_safe() && target;
1942 target = target->next)
1944 if (!target->tap->enabled)
1947 /* only poll target if we've got power and srst isn't asserted */
1948 if (!powerDropout && !srstAsserted)
1950 /* polling may fail silently until the target has been examined */
1951 if ((retval = target_poll(target)) != ERROR_OK)
1953 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1962 static int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1971 target = get_current_target(cmd_ctx);
1973 /* list all available registers for the current target */
1976 reg_cache_t *cache = target->reg_cache;
1983 command_print(cmd_ctx, "===== %s", cache->name);
1985 for (i = 0, reg = cache->reg_list;
1986 i < cache->num_regs;
1987 i++, reg++, count++)
1989 /* only print cached values if they are valid */
1991 value = buf_to_str(reg->value,
1993 command_print(cmd_ctx,
1994 "(%i) %s (/%" PRIu32 "): 0x%s%s",
2002 command_print(cmd_ctx, "(%i) %s (/%" PRIu32 ")",
2007 cache = cache->next;
2013 /* access a single register by its ordinal number */
2014 if ((args[0][0] >= '0') && (args[0][0] <= '9'))
2017 COMMAND_PARSE_NUMBER(uint, args[0], num);
2019 reg_cache_t *cache = target->reg_cache;
2024 for (i = 0; i < cache->num_regs; i++)
2026 if (count++ == (int)num)
2028 reg = &cache->reg_list[i];
2034 cache = cache->next;
2039 command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
2042 } else /* access a single register by its name */
2044 reg = register_get_by_name(target->reg_cache, args[0], 1);
2048 command_print(cmd_ctx, "register %s not found in current target", args[0]);
2053 /* display a register */
2054 if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
2056 if ((argc == 2) && (strcmp(args[1], "force") == 0))
2059 if (reg->valid == 0)
2061 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
2062 arch_type->get(reg);
2064 value = buf_to_str(reg->value, reg->size, 16);
2065 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2070 /* set register value */
2073 uint8_t *buf = malloc(CEIL(reg->size, 8));
2074 str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
2076 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
2077 arch_type->set(reg, buf);
2079 value = buf_to_str(reg->value, reg->size, 16);
2080 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2088 command_print(cmd_ctx, "usage: reg <#|name> [value]");
2093 static int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2095 int retval = ERROR_OK;
2096 target_t *target = get_current_target(cmd_ctx);
2100 command_print(cmd_ctx, "background polling: %s",
2101 jtag_poll_get_enabled() ? "on" : "off");
2102 command_print(cmd_ctx, "TAP: %s (%s)",
2103 target->tap->dotted_name,
2104 target->tap->enabled ? "enabled" : "disabled");
2105 if (!target->tap->enabled)
2107 if ((retval = target_poll(target)) != ERROR_OK)
2109 if ((retval = target_arch_state(target)) != ERROR_OK)
2115 if (strcmp(args[0], "on") == 0)
2117 jtag_poll_set_enabled(true);
2119 else if (strcmp(args[0], "off") == 0)
2121 jtag_poll_set_enabled(false);
2125 command_print(cmd_ctx, "arg is \"on\" or \"off\"");
2129 return ERROR_COMMAND_SYNTAX_ERROR;
2135 static int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2138 return ERROR_COMMAND_SYNTAX_ERROR;
2143 int retval = parse_uint(args[0], &ms);
2144 if (ERROR_OK != retval)
2146 command_print(cmd_ctx, "usage: %s [seconds]", cmd);
2147 return ERROR_COMMAND_SYNTAX_ERROR;
2149 // convert seconds (given) to milliseconds (needed)
2153 target_t *target = get_current_target(cmd_ctx);
2154 return target_wait_state(target, TARGET_HALTED, ms);
2157 /* wait for target state to change. The trick here is to have a low
2158 * latency for short waits and not to suck up all the CPU time
2161 * After 500ms, keep_alive() is invoked
2163 int target_wait_state(target_t *target, enum target_state state, int ms)
2166 long long then = 0, cur;
2171 if ((retval = target_poll(target)) != ERROR_OK)
2173 if (target->state == state)
2181 then = timeval_ms();
2182 LOG_DEBUG("waiting for target %s...",
2183 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2191 if ((cur-then) > ms)
2193 LOG_ERROR("timed out while waiting for target %s",
2194 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2202 static int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2206 target_t *target = get_current_target(cmd_ctx);
2207 int retval = target_halt(target);
2208 if (ERROR_OK != retval)
2214 retval = parse_uint(args[0], &wait);
2215 if (ERROR_OK != retval)
2216 return ERROR_COMMAND_SYNTAX_ERROR;
2221 return handle_wait_halt_command(cmd_ctx, cmd, args, argc);
2224 static int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2226 target_t *target = get_current_target(cmd_ctx);
2228 LOG_USER("requesting target halt and executing a soft reset");
2230 target->type->soft_reset_halt(target);
2235 static int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2238 return ERROR_COMMAND_SYNTAX_ERROR;
2240 enum target_reset_mode reset_mode = RESET_RUN;
2244 n = Jim_Nvp_name2value_simple(nvp_reset_modes, args[0]);
2245 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2246 return ERROR_COMMAND_SYNTAX_ERROR;
2248 reset_mode = n->value;
2251 /* reset *all* targets */
2252 return target_process_reset(cmd_ctx, reset_mode);
2256 static int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2260 return ERROR_COMMAND_SYNTAX_ERROR;
2262 target_t *target = get_current_target(cmd_ctx);
2263 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2265 /* with no args, resume from current pc, addr = 0,
2266 * with one arguments, addr = args[0],
2267 * handle breakpoints, not debugging */
2271 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2275 return target_resume(target, current, addr, 1, 0);
2278 static int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2281 return ERROR_COMMAND_SYNTAX_ERROR;
2285 /* with no args, step from current pc, addr = 0,
2286 * with one argument addr = args[0],
2287 * handle breakpoints, debugging */
2292 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2296 target_t *target = get_current_target(cmd_ctx);
2298 return target->type->step(target, current_pc, addr, 1);
2301 static void handle_md_output(struct command_context_s *cmd_ctx,
2302 struct target_s *target, uint32_t address, unsigned size,
2303 unsigned count, const uint8_t *buffer)
2305 const unsigned line_bytecnt = 32;
2306 unsigned line_modulo = line_bytecnt / size;
2308 char output[line_bytecnt * 4 + 1];
2309 unsigned output_len = 0;
2311 const char *value_fmt;
2313 case 4: value_fmt = "%8.8x "; break;
2314 case 2: value_fmt = "%4.2x "; break;
2315 case 1: value_fmt = "%2.2x "; break;
2317 LOG_ERROR("invalid memory read size: %u", size);
2321 for (unsigned i = 0; i < count; i++)
2323 if (i % line_modulo == 0)
2325 output_len += snprintf(output + output_len,
2326 sizeof(output) - output_len,
2328 (unsigned)(address + (i*size)));
2332 const uint8_t *value_ptr = buffer + i * size;
2334 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2335 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2336 case 1: value = *value_ptr;
2338 output_len += snprintf(output + output_len,
2339 sizeof(output) - output_len,
2342 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2344 command_print(cmd_ctx, "%s", output);
2350 static int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2353 return ERROR_COMMAND_SYNTAX_ERROR;
2357 case 'w': size = 4; break;
2358 case 'h': size = 2; break;
2359 case 'b': size = 1; break;
2360 default: return ERROR_COMMAND_SYNTAX_ERROR;
2363 bool physical=strcmp(args[0], "phys")==0;
2364 int (*fn)(struct target_s *target,
2365 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2370 fn=target_read_phys_memory;
2373 fn=target_read_memory;
2375 if ((argc < 1) || (argc > 2))
2377 return ERROR_COMMAND_SYNTAX_ERROR;
2381 COMMAND_PARSE_NUMBER(u32, args[0], address);
2385 COMMAND_PARSE_NUMBER(uint, args[1], count);
2387 uint8_t *buffer = calloc(count, size);
2389 target_t *target = get_current_target(cmd_ctx);
2390 int retval = fn(target, address, size, count, buffer);
2391 if (ERROR_OK == retval)
2392 handle_md_output(cmd_ctx, target, address, size, count, buffer);
2399 static int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2403 return ERROR_COMMAND_SYNTAX_ERROR;
2405 bool physical=strcmp(args[0], "phys")==0;
2406 int (*fn)(struct target_s *target,
2407 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2412 fn=target_write_phys_memory;
2415 fn=target_write_memory;
2417 if ((argc < 2) || (argc > 3))
2418 return ERROR_COMMAND_SYNTAX_ERROR;
2421 COMMAND_PARSE_NUMBER(u32, args[0], address);
2424 COMMAND_PARSE_NUMBER(u32, args[1], value);
2428 COMMAND_PARSE_NUMBER(uint, args[2], count);
2430 target_t *target = get_current_target(cmd_ctx);
2432 uint8_t value_buf[4];
2437 target_buffer_set_u32(target, value_buf, value);
2441 target_buffer_set_u16(target, value_buf, value);
2445 value_buf[0] = value;
2448 return ERROR_COMMAND_SYNTAX_ERROR;
2450 for (unsigned i = 0; i < count; i++)
2452 int retval = fn(target,
2453 address + i * wordsize, wordsize, 1, value_buf);
2454 if (ERROR_OK != retval)
2463 static int parse_load_image_command_args(struct command_context_s *cmd_ctx,
2464 char **args, int argc, image_t *image,
2465 uint32_t *min_address, uint32_t *max_address)
2467 if (argc < 1 || argc > 5)
2468 return ERROR_COMMAND_SYNTAX_ERROR;
2470 /* a base address isn't always necessary,
2471 * default to 0x0 (i.e. don't relocate) */
2475 COMMAND_PARSE_NUMBER(u32, args[1], addr);
2476 image->base_address = addr;
2477 image->base_address_set = 1;
2480 image->base_address_set = 0;
2482 image->start_address_set = 0;
2486 COMMAND_PARSE_NUMBER(u32, args[3], *min_address);
2490 COMMAND_PARSE_NUMBER(u32, args[4], *max_address);
2491 // use size (given) to find max (required)
2492 *max_address += *min_address;
2495 if (*min_address > *max_address)
2496 return ERROR_COMMAND_SYNTAX_ERROR;
2501 static int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2505 uint32_t image_size;
2506 uint32_t min_address = 0;
2507 uint32_t max_address = 0xffffffff;
2513 duration_t duration;
2514 char *duration_text;
2516 int retval = parse_load_image_command_args(cmd_ctx, args, argc,
2517 &image, &min_address, &max_address);
2518 if (ERROR_OK != retval)
2521 target_t *target = get_current_target(cmd_ctx);
2522 duration_start_measure(&duration);
2524 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
2531 for (i = 0; i < image.num_sections; i++)
2533 buffer = malloc(image.sections[i].size);
2536 command_print(cmd_ctx,
2537 "error allocating buffer for section (%d bytes)",
2538 (int)(image.sections[i].size));
2542 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2548 uint32_t offset = 0;
2549 uint32_t length = buf_cnt;
2551 /* DANGER!!! beware of unsigned comparision here!!! */
2553 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2554 (image.sections[i].base_address < max_address))
2556 if (image.sections[i].base_address < min_address)
2558 /* clip addresses below */
2559 offset += min_address-image.sections[i].base_address;
2563 if (image.sections[i].base_address + buf_cnt > max_address)
2565 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2568 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2573 image_size += length;
2574 command_print(cmd_ctx, "%u bytes written at address 0x%8.8" PRIx32 "",
2575 (unsigned int)length,
2576 image.sections[i].base_address + offset);
2582 if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2584 image_close(&image);
2588 if (retval == ERROR_OK)
2590 command_print(cmd_ctx, "downloaded %u byte in %s",
2591 (unsigned int)image_size,
2594 free(duration_text);
2596 image_close(&image);
2602 static int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2606 uint8_t buffer[560];
2609 duration_t duration;
2610 char *duration_text;
2612 target_t *target = get_current_target(cmd_ctx);
2616 command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
2621 COMMAND_PARSE_NUMBER(u32, args[1], address);
2623 COMMAND_PARSE_NUMBER(u32, args[2], size);
2625 if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2630 duration_start_measure(&duration);
2632 int retval = ERROR_OK;
2635 uint32_t size_written;
2636 uint32_t this_run_size = (size > 560) ? 560 : size;
2637 retval = target_read_buffer(target, address, this_run_size, buffer);
2638 if (retval != ERROR_OK)
2643 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2644 if (retval != ERROR_OK)
2649 size -= this_run_size;
2650 address += this_run_size;
2653 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2656 if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2659 if (retval == ERROR_OK)
2661 command_print(cmd_ctx, "dumped %lld byte in %s",
2662 fileio.size, duration_text);
2663 free(duration_text);
2669 static int handle_verify_image_command_internal(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, int verify)
2673 uint32_t image_size;
2675 int retval, retvaltemp;
2676 uint32_t checksum = 0;
2677 uint32_t mem_checksum = 0;
2681 duration_t duration;
2682 char *duration_text;
2684 target_t *target = get_current_target(cmd_ctx);
2688 return ERROR_COMMAND_SYNTAX_ERROR;
2693 LOG_ERROR("no target selected");
2697 duration_start_measure(&duration);
2702 COMMAND_PARSE_NUMBER(u32, args[1], addr);
2703 image.base_address = addr;
2704 image.base_address_set = 1;
2708 image.base_address_set = 0;
2709 image.base_address = 0x0;
2712 image.start_address_set = 0;
2714 if ((retval = image_open(&image, args[0], (argc == 3) ? args[2] : NULL)) != ERROR_OK)
2721 for (i = 0; i < image.num_sections; i++)
2723 buffer = malloc(image.sections[i].size);
2726 command_print(cmd_ctx,
2727 "error allocating buffer for section (%d bytes)",
2728 (int)(image.sections[i].size));
2731 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2739 /* calculate checksum of image */
2740 image_calculate_checksum(buffer, buf_cnt, &checksum);
2742 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2743 if (retval != ERROR_OK)
2749 if (checksum != mem_checksum)
2751 /* failed crc checksum, fall back to a binary compare */
2754 command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
2756 data = (uint8_t*)malloc(buf_cnt);
2758 /* Can we use 32bit word accesses? */
2760 int count = buf_cnt;
2761 if ((count % 4) == 0)
2766 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2767 if (retval == ERROR_OK)
2770 for (t = 0; t < buf_cnt; t++)
2772 if (data[t] != buffer[t])
2774 command_print(cmd_ctx,
2775 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2776 (unsigned)(t + image.sections[i].base_address),
2781 retval = ERROR_FAIL;
2795 command_print(cmd_ctx, "address 0x%08" PRIx32 " length 0x%08" PRIx32 "",
2796 image.sections[i].base_address,
2801 image_size += buf_cnt;
2805 if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2807 image_close(&image);
2811 if (retval == ERROR_OK)
2813 command_print(cmd_ctx, "verified %u bytes in %s",
2814 (unsigned int)image_size,
2817 free(duration_text);
2819 image_close(&image);
2824 static int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2826 return handle_verify_image_command_internal(cmd_ctx, cmd, args, argc, 1);
2829 static int handle_test_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2831 return handle_verify_image_command_internal(cmd_ctx, cmd, args, argc, 0);
2834 static int handle_bp_command_list(struct command_context_s *cmd_ctx)
2836 target_t *target = get_current_target(cmd_ctx);
2837 breakpoint_t *breakpoint = target->breakpoints;
2840 if (breakpoint->type == BKPT_SOFT)
2842 char* buf = buf_to_str(breakpoint->orig_instr,
2843 breakpoint->length, 16);
2844 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2845 breakpoint->address,
2847 breakpoint->set, buf);
2852 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2853 breakpoint->address,
2854 breakpoint->length, breakpoint->set);
2857 breakpoint = breakpoint->next;
2862 static int handle_bp_command_set(struct command_context_s *cmd_ctx,
2863 uint32_t addr, uint32_t length, int hw)
2865 target_t *target = get_current_target(cmd_ctx);
2866 int retval = breakpoint_add(target, addr, length, hw);
2867 if (ERROR_OK == retval)
2868 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2870 LOG_ERROR("Failure setting breakpoint");
2874 static int handle_bp_command(struct command_context_s *cmd_ctx,
2875 char *cmd, char **args, int argc)
2878 return handle_bp_command_list(cmd_ctx);
2880 if (argc < 2 || argc > 3)
2882 command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
2883 return ERROR_COMMAND_SYNTAX_ERROR;
2887 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2889 COMMAND_PARSE_NUMBER(u32, args[1], length);
2894 if (strcmp(args[2], "hw") == 0)
2897 return ERROR_COMMAND_SYNTAX_ERROR;
2900 return handle_bp_command_set(cmd_ctx, addr, length, hw);
2903 static int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2906 return ERROR_COMMAND_SYNTAX_ERROR;
2909 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2911 target_t *target = get_current_target(cmd_ctx);
2912 breakpoint_remove(target, addr);
2917 static int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2919 target_t *target = get_current_target(cmd_ctx);
2923 watchpoint_t *watchpoint = target->watchpoints;
2927 command_print(cmd_ctx,
2928 "address: 0x%8.8" PRIx32 ", len: 0x%8.8x, r/w/a: %i, value: 0x%8.8" PRIx32 ", mask: 0x%8.8" PRIx32 "",
2929 watchpoint->address,
2931 (int)(watchpoint->rw),
2934 watchpoint = watchpoint->next;
2939 enum watchpoint_rw type = WPT_ACCESS;
2941 uint32_t length = 0;
2942 uint32_t data_value = 0x0;
2943 uint32_t data_mask = 0xffffffff;
2948 COMMAND_PARSE_NUMBER(u32, args[4], data_mask);
2951 COMMAND_PARSE_NUMBER(u32, args[3], data_value);
2966 LOG_ERROR("invalid watchpoint mode ('%c')", args[2][0]);
2967 return ERROR_COMMAND_SYNTAX_ERROR;
2971 COMMAND_PARSE_NUMBER(u32, args[1], length);
2972 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2976 command_print(cmd_ctx, "usage: wp [address length "
2977 "[(r|w|a) [value [mask]]]]");
2978 return ERROR_COMMAND_SYNTAX_ERROR;
2981 int retval = watchpoint_add(target, addr, length, type,
2982 data_value, data_mask);
2983 if (ERROR_OK != retval)
2984 LOG_ERROR("Failure setting watchpoints");
2989 static int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2992 return ERROR_COMMAND_SYNTAX_ERROR;
2995 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2997 target_t *target = get_current_target(cmd_ctx);
2998 watchpoint_remove(target, addr);
3005 * Translate a virtual address to a physical address.
3007 * The low-level target implementation must have logged a detailed error
3008 * which is forwarded to telnet/GDB session.
3010 static int handle_virt2phys_command(command_context_t *cmd_ctx,
3011 char *cmd, char **args, int argc)
3014 return ERROR_COMMAND_SYNTAX_ERROR;
3017 COMMAND_PARSE_NUMBER(u32, args[0], va);
3020 target_t *target = get_current_target(cmd_ctx);
3021 int retval = target->type->virt2phys(target, va, &pa);
3022 if (retval == ERROR_OK)
3023 command_print(cmd_ctx, "Physical address 0x%08" PRIx32 "", pa);
3028 static void writeData(FILE *f, const void *data, size_t len)
3030 size_t written = fwrite(data, 1, len, f);
3032 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
3035 static void writeLong(FILE *f, int l)
3038 for (i = 0; i < 4; i++)
3040 char c = (l >> (i*8))&0xff;
3041 writeData(f, &c, 1);
3046 static void writeString(FILE *f, char *s)
3048 writeData(f, s, strlen(s));
3051 /* Dump a gmon.out histogram file. */
3052 static void writeGmon(uint32_t *samples, uint32_t sampleNum, char *filename)
3055 FILE *f = fopen(filename, "w");
3058 writeString(f, "gmon");
3059 writeLong(f, 0x00000001); /* Version */
3060 writeLong(f, 0); /* padding */
3061 writeLong(f, 0); /* padding */
3062 writeLong(f, 0); /* padding */
3064 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
3065 writeData(f, &zero, 1);
3067 /* figure out bucket size */
3068 uint32_t min = samples[0];
3069 uint32_t max = samples[0];
3070 for (i = 0; i < sampleNum; i++)
3072 if (min > samples[i])
3076 if (max < samples[i])
3082 int addressSpace = (max-min + 1);
3084 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
3085 uint32_t length = addressSpace;
3086 if (length > maxBuckets)
3088 length = maxBuckets;
3090 int *buckets = malloc(sizeof(int)*length);
3091 if (buckets == NULL)
3096 memset(buckets, 0, sizeof(int)*length);
3097 for (i = 0; i < sampleNum;i++)
3099 uint32_t address = samples[i];
3100 long long a = address-min;
3101 long long b = length-1;
3102 long long c = addressSpace-1;
3103 int index = (a*b)/c; /* danger!!!! int32 overflows */
3107 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3108 writeLong(f, min); /* low_pc */
3109 writeLong(f, max); /* high_pc */
3110 writeLong(f, length); /* # of samples */
3111 writeLong(f, 64000000); /* 64MHz */
3112 writeString(f, "seconds");
3113 for (i = 0; i < (15-strlen("seconds")); i++)
3114 writeData(f, &zero, 1);
3115 writeString(f, "s");
3117 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3119 char *data = malloc(2*length);
3122 for (i = 0; i < length;i++)
3131 data[i*2 + 1]=(val >> 8)&0xff;
3134 writeData(f, data, length * 2);
3144 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
3145 static int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
3147 target_t *target = get_current_target(cmd_ctx);
3148 struct timeval timeout, now;
3150 gettimeofday(&timeout, NULL);
3153 return ERROR_COMMAND_SYNTAX_ERROR;
3156 COMMAND_PARSE_NUMBER(uint, args[0], offset);
3158 timeval_add_time(&timeout, offset, 0);
3160 command_print(cmd_ctx, "Starting profiling. Halting and resuming the target as often as we can...");
3162 static const int maxSample = 10000;
3163 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3164 if (samples == NULL)
3168 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3169 reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1);
3174 target_poll(target);
3175 if (target->state == TARGET_HALTED)
3177 uint32_t t=*((uint32_t *)reg->value);
3178 samples[numSamples++]=t;
3179 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3180 target_poll(target);
3181 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3182 } else if (target->state == TARGET_RUNNING)
3184 /* We want to quickly sample the PC. */
3185 if ((retval = target_halt(target)) != ERROR_OK)
3192 command_print(cmd_ctx, "Target not halted or running");
3196 if (retval != ERROR_OK)
3201 gettimeofday(&now, NULL);
3202 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3204 command_print(cmd_ctx, "Profiling completed. %d samples.", numSamples);
3205 if ((retval = target_poll(target)) != ERROR_OK)
3210 if (target->state == TARGET_HALTED)
3212 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3214 if ((retval = target_poll(target)) != ERROR_OK)
3219 writeGmon(samples, numSamples, args[1]);
3220 command_print(cmd_ctx, "Wrote %s", args[1]);
3229 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3232 Jim_Obj *nameObjPtr, *valObjPtr;
3235 namebuf = alloc_printf("%s(%d)", varname, idx);
3239 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3240 valObjPtr = Jim_NewIntObj(interp, val);
3241 if (!nameObjPtr || !valObjPtr)
3247 Jim_IncrRefCount(nameObjPtr);
3248 Jim_IncrRefCount(valObjPtr);
3249 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3250 Jim_DecrRefCount(interp, nameObjPtr);
3251 Jim_DecrRefCount(interp, valObjPtr);
3253 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3257 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3259 command_context_t *context;
3262 context = Jim_GetAssocData(interp, "context");
3263 if (context == NULL)
3265 LOG_ERROR("mem2array: no command context");
3268 target = get_current_target(context);
3271 LOG_ERROR("mem2array: no current target");
3275 return target_mem2array(interp, target, argc-1, argv + 1);
3278 static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
3286 const char *varname;
3287 uint8_t buffer[4096];
3291 /* argv[1] = name of array to receive the data
3292 * argv[2] = desired width
3293 * argv[3] = memory address
3294 * argv[4] = count of times to read
3297 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3300 varname = Jim_GetString(argv[0], &len);
3301 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3303 e = Jim_GetLong(interp, argv[1], &l);
3309 e = Jim_GetLong(interp, argv[2], &l);
3314 e = Jim_GetLong(interp, argv[3], &l);
3330 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3331 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3335 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3336 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3339 if ((addr + (len * width)) < addr) {
3340 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3341 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3344 /* absurd transfer size? */
3346 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3347 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3352 ((width == 2) && ((addr & 1) == 0)) ||
3353 ((width == 4) && ((addr & 3) == 0))) {
3357 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3358 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3361 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3372 /* Slurp... in buffer size chunks */
3374 count = len; /* in objects.. */
3375 if (count > (sizeof(buffer)/width)) {
3376 count = (sizeof(buffer)/width);
3379 retval = target_read_memory(target, addr, width, count, buffer);
3380 if (retval != ERROR_OK) {
3382 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3386 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3387 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3391 v = 0; /* shut up gcc */
3392 for (i = 0 ;i < count ;i++, n++) {
3395 v = target_buffer_get_u32(target, &buffer[i*width]);
3398 v = target_buffer_get_u16(target, &buffer[i*width]);
3401 v = buffer[i] & 0x0ff;
3404 new_int_array_element(interp, varname, n, v);
3410 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3415 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3418 Jim_Obj *nameObjPtr, *valObjPtr;
3422 namebuf = alloc_printf("%s(%d)", varname, idx);
3426 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3433 Jim_IncrRefCount(nameObjPtr);
3434 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3435 Jim_DecrRefCount(interp, nameObjPtr);
3437 if (valObjPtr == NULL)
3440 result = Jim_GetLong(interp, valObjPtr, &l);
3441 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3446 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3448 command_context_t *context;
3451 context = Jim_GetAssocData(interp, "context");
3452 if (context == NULL) {
3453 LOG_ERROR("array2mem: no command context");
3456 target = get_current_target(context);
3457 if (target == NULL) {
3458 LOG_ERROR("array2mem: no current target");
3462 return target_array2mem(interp,target, argc-1, argv + 1);
3464 static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
3472 const char *varname;
3473 uint8_t buffer[4096];
3477 /* argv[1] = name of array to get the data
3478 * argv[2] = desired width
3479 * argv[3] = memory address
3480 * argv[4] = count to write
3483 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3486 varname = Jim_GetString(argv[0], &len);
3487 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3489 e = Jim_GetLong(interp, argv[1], &l);
3495 e = Jim_GetLong(interp, argv[2], &l);
3500 e = Jim_GetLong(interp, argv[3], &l);
3516 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3517 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3521 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3522 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3525 if ((addr + (len * width)) < addr) {
3526 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3527 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3530 /* absurd transfer size? */
3532 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3533 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3538 ((width == 2) && ((addr & 1) == 0)) ||
3539 ((width == 4) && ((addr & 3) == 0))) {
3543 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3544 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3547 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3558 /* Slurp... in buffer size chunks */
3560 count = len; /* in objects.. */
3561 if (count > (sizeof(buffer)/width)) {
3562 count = (sizeof(buffer)/width);
3565 v = 0; /* shut up gcc */
3566 for (i = 0 ;i < count ;i++, n++) {
3567 get_int_array_element(interp, varname, n, &v);
3570 target_buffer_set_u32(target, &buffer[i*width], v);
3573 target_buffer_set_u16(target, &buffer[i*width], v);
3576 buffer[i] = v & 0x0ff;
3582 retval = target_write_memory(target, addr, width, count, buffer);
3583 if (retval != ERROR_OK) {
3585 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3589 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3590 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3596 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3601 void target_all_handle_event(enum target_event e)
3605 LOG_DEBUG("**all*targets: event: %d, %s",
3607 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3609 target = all_targets;
3611 target_handle_event(target, e);
3612 target = target->next;
3617 /* FIX? should we propagate errors here rather than printing them
3620 void target_handle_event(target_t *target, enum target_event e)
3622 target_event_action_t *teap;
3624 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3625 if (teap->event == e) {
3626 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3627 target->target_number,
3629 target_get_name(target),
3631 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3632 Jim_GetString(teap->body, NULL));
3633 if (Jim_EvalObj(interp, teap->body) != JIM_OK)
3635 Jim_PrintErrorMessage(interp);
3641 enum target_cfg_param {
3644 TCFG_WORK_AREA_VIRT,
3645 TCFG_WORK_AREA_PHYS,
3646 TCFG_WORK_AREA_SIZE,
3647 TCFG_WORK_AREA_BACKUP,
3650 TCFG_CHAIN_POSITION,
3653 static Jim_Nvp nvp_config_opts[] = {
3654 { .name = "-type", .value = TCFG_TYPE },
3655 { .name = "-event", .value = TCFG_EVENT },
3656 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3657 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3658 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3659 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3660 { .name = "-endian" , .value = TCFG_ENDIAN },
3661 { .name = "-variant", .value = TCFG_VARIANT },
3662 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3664 { .name = NULL, .value = -1 }
3667 static int target_configure(Jim_GetOptInfo *goi, target_t *target)
3675 /* parse config or cget options ... */
3676 while (goi->argc > 0) {
3677 Jim_SetEmptyResult(goi->interp);
3678 /* Jim_GetOpt_Debug(goi); */
3680 if (target->type->target_jim_configure) {
3681 /* target defines a configure function */
3682 /* target gets first dibs on parameters */
3683 e = (*(target->type->target_jim_configure))(target, goi);
3692 /* otherwise we 'continue' below */
3694 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3696 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3702 if (goi->isconfigure) {
3703 Jim_SetResult_sprintf(goi->interp, "not setable: %s", n->name);
3707 if (goi->argc != 0) {
3708 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "NO PARAMS");
3712 Jim_SetResultString(goi->interp, target_get_name(target), -1);
3716 if (goi->argc == 0) {
3717 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3721 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3723 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3727 if (goi->isconfigure) {
3728 if (goi->argc != 1) {
3729 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3733 if (goi->argc != 0) {
3734 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3740 target_event_action_t *teap;
3742 teap = target->event_action;
3743 /* replace existing? */
3745 if (teap->event == (enum target_event)n->value) {
3751 if (goi->isconfigure) {
3752 bool replace = true;
3755 teap = calloc(1, sizeof(*teap));
3758 teap->event = n->value;
3759 Jim_GetOpt_Obj(goi, &o);
3761 Jim_DecrRefCount(interp, teap->body);
3763 teap->body = Jim_DuplicateObj(goi->interp, o);
3766 * Tcl/TK - "tk events" have a nice feature.
3767 * See the "BIND" command.
3768 * We should support that here.
3769 * You can specify %X and %Y in the event code.
3770 * The idea is: %T - target name.
3771 * The idea is: %N - target number
3772 * The idea is: %E - event name.
3774 Jim_IncrRefCount(teap->body);
3778 /* add to head of event list */
3779 teap->next = target->event_action;
3780 target->event_action = teap;
3782 Jim_SetEmptyResult(goi->interp);
3786 Jim_SetEmptyResult(goi->interp);
3788 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3795 case TCFG_WORK_AREA_VIRT:
3796 if (goi->isconfigure) {
3797 target_free_all_working_areas(target);
3798 e = Jim_GetOpt_Wide(goi, &w);
3802 target->working_area_virt = w;
3803 target->working_area_virt_spec = true;
3805 if (goi->argc != 0) {
3809 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3813 case TCFG_WORK_AREA_PHYS:
3814 if (goi->isconfigure) {
3815 target_free_all_working_areas(target);
3816 e = Jim_GetOpt_Wide(goi, &w);
3820 target->working_area_phys = w;
3821 target->working_area_phys_spec = true;
3823 if (goi->argc != 0) {
3827 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3831 case TCFG_WORK_AREA_SIZE:
3832 if (goi->isconfigure) {
3833 target_free_all_working_areas(target);
3834 e = Jim_GetOpt_Wide(goi, &w);
3838 target->working_area_size = w;
3840 if (goi->argc != 0) {
3844 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3848 case TCFG_WORK_AREA_BACKUP:
3849 if (goi->isconfigure) {
3850 target_free_all_working_areas(target);
3851 e = Jim_GetOpt_Wide(goi, &w);
3855 /* make this exactly 1 or 0 */
3856 target->backup_working_area = (!!w);
3858 if (goi->argc != 0) {
3862 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3863 /* loop for more e*/
3867 if (goi->isconfigure) {
3868 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3870 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3873 target->endianness = n->value;
3875 if (goi->argc != 0) {
3879 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3880 if (n->name == NULL) {
3881 target->endianness = TARGET_LITTLE_ENDIAN;
3882 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3884 Jim_SetResultString(goi->interp, n->name, -1);
3889 if (goi->isconfigure) {
3890 if (goi->argc < 1) {
3891 Jim_SetResult_sprintf(goi->interp,
3896 if (target->variant) {
3897 free((void *)(target->variant));
3899 e = Jim_GetOpt_String(goi, &cp, NULL);
3900 target->variant = strdup(cp);
3902 if (goi->argc != 0) {
3906 Jim_SetResultString(goi->interp, target->variant,-1);
3909 case TCFG_CHAIN_POSITION:
3910 if (goi->isconfigure) {
3913 target_free_all_working_areas(target);
3914 e = Jim_GetOpt_Obj(goi, &o);
3918 tap = jtag_tap_by_jim_obj(goi->interp, o);
3922 /* make this exactly 1 or 0 */
3925 if (goi->argc != 0) {
3929 Jim_SetResultString(interp, target->tap->dotted_name, -1);
3930 /* loop for more e*/
3933 } /* while (goi->argc) */
3936 /* done - we return */
3940 /** this is the 'tcl' handler for the target specific command */
3941 static int tcl_target_func(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3946 uint8_t target_buf[32];
3949 struct command_context_s *cmd_ctx;
3956 TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
3957 TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
3958 TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
3959 TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
3967 TS_CMD_INVOKE_EVENT,
3970 static const Jim_Nvp target_options[] = {
3971 { .name = "configure", .value = TS_CMD_CONFIGURE },
3972 { .name = "cget", .value = TS_CMD_CGET },
3973 { .name = "mww", .value = TS_CMD_MWW },
3974 { .name = "mwh", .value = TS_CMD_MWH },
3975 { .name = "mwb", .value = TS_CMD_MWB },
3976 { .name = "mdw", .value = TS_CMD_MDW },
3977 { .name = "mdh", .value = TS_CMD_MDH },
3978 { .name = "mdb", .value = TS_CMD_MDB },
3979 { .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
3980 { .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
3981 { .name = "eventlist", .value = TS_CMD_EVENTLIST },
3982 { .name = "curstate", .value = TS_CMD_CURSTATE },
3984 { .name = "arp_examine", .value = TS_CMD_EXAMINE },
3985 { .name = "arp_poll", .value = TS_CMD_POLL },
3986 { .name = "arp_reset", .value = TS_CMD_RESET },
3987 { .name = "arp_halt", .value = TS_CMD_HALT },
3988 { .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
3989 { .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
3991 { .name = NULL, .value = -1 },
3994 /* go past the "command" */
3995 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
3997 target = Jim_CmdPrivData(goi.interp);
3998 cmd_ctx = Jim_GetAssocData(goi.interp, "context");
4000 /* commands here are in an NVP table */
4001 e = Jim_GetOpt_Nvp(&goi, target_options, &n);
4003 Jim_GetOpt_NvpUnknown(&goi, target_options, 0);
4006 /* Assume blank result */
4007 Jim_SetEmptyResult(goi.interp);
4010 case TS_CMD_CONFIGURE:
4012 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
4015 goi.isconfigure = 1;
4016 return target_configure(&goi, target);
4018 // some things take params
4020 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "missing: ?-option?");
4023 goi.isconfigure = 0;
4024 return target_configure(&goi, target);
4032 * argv[3] = optional count.
4035 if ((goi.argc == 2) || (goi.argc == 3)) {
4039 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR DATA [COUNT]", n->name);
4043 e = Jim_GetOpt_Wide(&goi, &a);
4048 e = Jim_GetOpt_Wide(&goi, &b);
4052 if (goi.argc == 3) {
4053 e = Jim_GetOpt_Wide(&goi, &c);
4063 target_buffer_set_u32(target, target_buf, b);
4067 target_buffer_set_u16(target, target_buf, b);
4071 target_buffer_set_u8(target, target_buf, b);
4075 for (x = 0 ; x < c ; x++) {
4076 e = target_write_memory(target, a, b, 1, target_buf);
4077 if (e != ERROR_OK) {
4078 Jim_SetResult_sprintf(interp, "Error writing @ 0x%08x: %d\n", (int)(a), e);
4091 /* argv[0] = command
4093 * argv[2] = optional count
4095 if ((goi.argc == 2) || (goi.argc == 3)) {
4096 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR [COUNT]", n->name);
4099 e = Jim_GetOpt_Wide(&goi, &a);
4104 e = Jim_GetOpt_Wide(&goi, &c);
4111 b = 1; /* shut up gcc */
4124 /* convert to "bytes" */
4126 /* count is now in 'BYTES' */
4132 e = target_read_memory(target, a, b, y / b, target_buf);
4133 if (e != ERROR_OK) {
4134 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
4138 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
4141 for (x = 0 ; (x < 16) && (x < y) ; x += 4) {
4142 z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
4143 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
4145 for (; (x < 16) ; x += 4) {
4146 Jim_fprintf(interp, interp->cookie_stdout, " ");
4150 for (x = 0 ; (x < 16) && (x < y) ; x += 2) {
4151 z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
4152 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
4154 for (; (x < 16) ; x += 2) {
4155 Jim_fprintf(interp, interp->cookie_stdout, " ");
4160 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4161 z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
4162 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
4164 for (; (x < 16) ; x += 1) {
4165 Jim_fprintf(interp, interp->cookie_stdout, " ");
4169 /* ascii-ify the bytes */
4170 for (x = 0 ; x < y ; x++) {
4171 if ((target_buf[x] >= 0x20) &&
4172 (target_buf[x] <= 0x7e)) {
4176 target_buf[x] = '.';
4181 target_buf[x] = ' ';
4186 /* print - with a newline */
4187 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
4193 case TS_CMD_MEM2ARRAY:
4194 return target_mem2array(goi.interp, target, goi.argc, goi.argv);
4196 case TS_CMD_ARRAY2MEM:
4197 return target_array2mem(goi.interp, target, goi.argc, goi.argv);
4199 case TS_CMD_EXAMINE:
4201 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4204 if (!target->tap->enabled)
4205 goto err_tap_disabled;
4206 e = target->type->examine(target);
4207 if (e != ERROR_OK) {
4208 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
4214 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4217 if (!target->tap->enabled)
4218 goto err_tap_disabled;
4219 if (!(target_was_examined(target))) {
4220 e = ERROR_TARGET_NOT_EXAMINED;
4222 e = target->type->poll(target);
4224 if (e != ERROR_OK) {
4225 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4232 if (goi.argc != 2) {
4233 Jim_WrongNumArgs(interp, 2, argv,
4234 "([tT]|[fF]|assert|deassert) BOOL");
4237 e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4239 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4242 /* the halt or not param */
4243 e = Jim_GetOpt_Wide(&goi, &a);
4247 if (!target->tap->enabled)
4248 goto err_tap_disabled;
4249 if (!target->type->assert_reset
4250 || !target->type->deassert_reset) {
4251 Jim_SetResult_sprintf(interp,
4252 "No target-specific reset for %s",
4256 /* determine if we should halt or not. */
4257 target->reset_halt = !!a;
4258 /* When this happens - all workareas are invalid. */
4259 target_free_all_working_areas_restore(target, 0);
4262 if (n->value == NVP_ASSERT) {
4263 e = target->type->assert_reset(target);
4265 e = target->type->deassert_reset(target);
4267 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4270 Jim_WrongNumArgs(goi.interp, 0, argv, "halt [no parameters]");
4273 if (!target->tap->enabled)
4274 goto err_tap_disabled;
4275 e = target->type->halt(target);
4276 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4277 case TS_CMD_WAITSTATE:
4278 /* params: <name> statename timeoutmsecs */
4279 if (goi.argc != 2) {
4280 Jim_SetResult_sprintf(goi.interp, "%s STATENAME TIMEOUTMSECS", n->name);
4283 e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4285 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4288 e = Jim_GetOpt_Wide(&goi, &a);
4292 if (!target->tap->enabled)
4293 goto err_tap_disabled;
4294 e = target_wait_state(target, n->value, a);
4295 if (e != ERROR_OK) {
4296 Jim_SetResult_sprintf(goi.interp,
4297 "target: %s wait %s fails (%d) %s",
4300 e, target_strerror_safe(e));
4305 case TS_CMD_EVENTLIST:
4306 /* List for human, Events defined for this target.
4307 * scripts/programs should use 'name cget -event NAME'
4310 target_event_action_t *teap;
4311 teap = target->event_action;
4312 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4313 target->target_number,
4315 command_print(cmd_ctx, "%-25s | Body", "Event");
4316 command_print(cmd_ctx, "------------------------- | ----------------------------------------");
4318 command_print(cmd_ctx,
4320 Jim_Nvp_value2name_simple(nvp_target_event, teap->event)->name,
4321 Jim_GetString(teap->body, NULL));
4324 command_print(cmd_ctx, "***END***");
4327 case TS_CMD_CURSTATE:
4328 if (goi.argc != 0) {
4329 Jim_WrongNumArgs(goi.interp, 0, argv, "[no parameters]");
4332 Jim_SetResultString(goi.interp,
4333 target_state_name( target ),
4336 case TS_CMD_INVOKE_EVENT:
4337 if (goi.argc != 1) {
4338 Jim_SetResult_sprintf(goi.interp, "%s ?EVENTNAME?",n->name);
4341 e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4343 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4346 target_handle_event(target, n->value);
4352 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4356 static int target_create(Jim_GetOptInfo *goi)
4365 struct command_context_s *cmd_ctx;
4367 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4368 if (goi->argc < 3) {
4369 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4374 Jim_GetOpt_Obj(goi, &new_cmd);
4375 /* does this command exist? */
4376 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4378 cp = Jim_GetString(new_cmd, NULL);
4379 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4384 e = Jim_GetOpt_String(goi, &cp2, NULL);
4386 /* now does target type exist */
4387 for (x = 0 ; target_types[x] ; x++) {
4388 if (0 == strcmp(cp, target_types[x]->name)) {
4393 if (target_types[x] == NULL) {
4394 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4395 for (x = 0 ; target_types[x] ; x++) {
4396 if (target_types[x + 1]) {
4397 Jim_AppendStrings(goi->interp,
4398 Jim_GetResult(goi->interp),
4399 target_types[x]->name,
4402 Jim_AppendStrings(goi->interp,
4403 Jim_GetResult(goi->interp),
4405 target_types[x]->name,NULL);
4412 target = calloc(1,sizeof(target_t));
4413 /* set target number */
4414 target->target_number = new_target_number();
4416 /* allocate memory for each unique target type */
4417 target->type = (target_type_t*)calloc(1,sizeof(target_type_t));
4419 memcpy(target->type, target_types[x], sizeof(target_type_t));
4421 /* will be set by "-endian" */
4422 target->endianness = TARGET_ENDIAN_UNKNOWN;
4424 target->working_area = 0x0;
4425 target->working_area_size = 0x0;
4426 target->working_areas = NULL;
4427 target->backup_working_area = 0;
4429 target->state = TARGET_UNKNOWN;
4430 target->debug_reason = DBG_REASON_UNDEFINED;
4431 target->reg_cache = NULL;
4432 target->breakpoints = NULL;
4433 target->watchpoints = NULL;
4434 target->next = NULL;
4435 target->arch_info = NULL;
4437 target->display = 1;
4439 target->halt_issued = false;
4441 /* initialize trace information */
4442 target->trace_info = malloc(sizeof(trace_t));
4443 target->trace_info->num_trace_points = 0;
4444 target->trace_info->trace_points_size = 0;
4445 target->trace_info->trace_points = NULL;
4446 target->trace_info->trace_history_size = 0;
4447 target->trace_info->trace_history = NULL;
4448 target->trace_info->trace_history_pos = 0;
4449 target->trace_info->trace_history_overflowed = 0;
4451 target->dbgmsg = NULL;
4452 target->dbg_msg_enabled = 0;
4454 target->endianness = TARGET_ENDIAN_UNKNOWN;
4456 /* Do the rest as "configure" options */
4457 goi->isconfigure = 1;
4458 e = target_configure(goi, target);
4460 if (target->tap == NULL)
4462 Jim_SetResultString(interp, "-chain-position required when creating target", -1);
4472 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4473 /* default endian to little if not specified */
4474 target->endianness = TARGET_LITTLE_ENDIAN;
4477 /* incase variant is not set */
4478 if (!target->variant)
4479 target->variant = strdup("");
4481 /* create the target specific commands */
4482 if (target->type->register_commands) {
4483 (*(target->type->register_commands))(cmd_ctx);
4485 if (target->type->target_create) {
4486 (*(target->type->target_create))(target, goi->interp);
4489 /* append to end of list */
4492 tpp = &(all_targets);
4494 tpp = &((*tpp)->next);
4499 cp = Jim_GetString(new_cmd, NULL);
4500 target->cmd_name = strdup(cp);
4502 /* now - create the new target name command */
4503 e = Jim_CreateCommand(goi->interp,
4506 tcl_target_func, /* C function */
4507 target, /* private data */
4508 NULL); /* no del proc */
4513 static int jim_target(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4517 struct command_context_s *cmd_ctx;
4521 /* TG = target generic */
4529 const char *target_cmds[] = {
4530 "create", "types", "names", "current", "number",
4532 NULL /* terminate */
4535 LOG_DEBUG("Target command params:");
4536 LOG_DEBUG("%s", Jim_Debug_ArgvString(interp, argc, argv));
4538 cmd_ctx = Jim_GetAssocData(interp, "context");
4540 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
4542 if (goi.argc == 0) {
4543 Jim_WrongNumArgs(interp, 1, argv, "missing: command ...");
4547 /* Jim_GetOpt_Debug(&goi); */
4548 r = Jim_GetOpt_Enum(&goi, target_cmds, &x);
4555 Jim_Panic(goi.interp,"Why am I here?");
4557 case TG_CMD_CURRENT:
4558 if (goi.argc != 0) {
4559 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4562 Jim_SetResultString(goi.interp, get_current_target(cmd_ctx)->cmd_name, -1);
4565 if (goi.argc != 0) {
4566 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4569 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4570 for (x = 0 ; target_types[x] ; x++) {
4571 Jim_ListAppendElement(goi.interp,
4572 Jim_GetResult(goi.interp),
4573 Jim_NewStringObj(goi.interp, target_types[x]->name, -1));
4577 if (goi.argc != 0) {
4578 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4581 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4582 target = all_targets;
4584 Jim_ListAppendElement(goi.interp,
4585 Jim_GetResult(goi.interp),
4586 Jim_NewStringObj(goi.interp, target->cmd_name, -1));
4587 target = target->next;
4592 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "?name ... config options ...");
4595 return target_create(&goi);
4598 /* It's OK to remove this mechanism sometime after August 2010 or so */
4599 LOG_WARNING("don't use numbers as target identifiers; use names");
4600 if (goi.argc != 1) {
4601 Jim_SetResult_sprintf(goi.interp, "expected: target number ?NUMBER?");
4604 e = Jim_GetOpt_Wide(&goi, &w);
4608 for (x = 0, target = all_targets; target; target = target->next, x++) {
4609 if (target->target_number == w)
4612 if (target == NULL) {
4613 Jim_SetResult_sprintf(goi.interp,
4614 "Target: number %d does not exist", (int)(w));
4617 Jim_SetResultString(goi.interp, target->cmd_name, -1);
4620 if (goi.argc != 0) {
4621 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "<no parameters>");
4624 for (x = 0, target = all_targets; target; target = target->next, x++)
4626 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, x));
4642 static int fastload_num;
4643 static struct FastLoad *fastload;
4645 static void free_fastload(void)
4647 if (fastload != NULL)
4650 for (i = 0; i < fastload_num; i++)
4652 if (fastload[i].data)
4653 free(fastload[i].data);
4663 static int handle_fast_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4667 uint32_t image_size;
4668 uint32_t min_address = 0;
4669 uint32_t max_address = 0xffffffff;
4674 duration_t duration;
4675 char *duration_text;
4677 int retval = parse_load_image_command_args(cmd_ctx, args, argc,
4678 &image, &min_address, &max_address);
4679 if (ERROR_OK != retval)
4682 duration_start_measure(&duration);
4684 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
4691 fastload_num = image.num_sections;
4692 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4693 if (fastload == NULL)
4695 image_close(&image);
4698 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4699 for (i = 0; i < image.num_sections; i++)
4701 buffer = malloc(image.sections[i].size);
4704 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)",
4705 (int)(image.sections[i].size));
4709 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4715 uint32_t offset = 0;
4716 uint32_t length = buf_cnt;
4719 /* DANGER!!! beware of unsigned comparision here!!! */
4721 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4722 (image.sections[i].base_address < max_address))
4724 if (image.sections[i].base_address < min_address)
4726 /* clip addresses below */
4727 offset += min_address-image.sections[i].base_address;
4731 if (image.sections[i].base_address + buf_cnt > max_address)
4733 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4736 fastload[i].address = image.sections[i].base_address + offset;
4737 fastload[i].data = malloc(length);
4738 if (fastload[i].data == NULL)
4743 memcpy(fastload[i].data, buffer + offset, length);
4744 fastload[i].length = length;
4746 image_size += length;
4747 command_print(cmd_ctx, "%u bytes written at address 0x%8.8x",
4748 (unsigned int)length,
4749 ((unsigned int)(image.sections[i].base_address + offset)));
4755 duration_stop_measure(&duration, &duration_text);
4756 if (retval == ERROR_OK)
4758 command_print(cmd_ctx, "Loaded %u bytes in %s", (unsigned int)image_size, duration_text);
4759 command_print(cmd_ctx, "NB!!! image has not been loaded to target, issue a subsequent 'fast_load' to do so.");
4761 free(duration_text);
4763 image_close(&image);
4765 if (retval != ERROR_OK)
4773 static int handle_fast_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4776 return ERROR_COMMAND_SYNTAX_ERROR;
4777 if (fastload == NULL)
4779 LOG_ERROR("No image in memory");
4783 int ms = timeval_ms();
4785 int retval = ERROR_OK;
4786 for (i = 0; i < fastload_num;i++)
4788 target_t *target = get_current_target(cmd_ctx);
4789 command_print(cmd_ctx, "Write to 0x%08x, length 0x%08x",
4790 (unsigned int)(fastload[i].address),
4791 (unsigned int)(fastload[i].length));
4792 if (retval == ERROR_OK)
4794 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4796 size += fastload[i].length;
4798 int after = timeval_ms();
4799 command_print(cmd_ctx, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
4803 static int jim_mcrmrc(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4805 command_context_t *context;
4809 context = Jim_GetAssocData(interp, "context");
4810 if (context == NULL) {
4811 LOG_ERROR("array2mem: no command context");
4814 target = get_current_target(context);
4815 if (target == NULL) {
4816 LOG_ERROR("array2mem: no current target");
4820 if ((argc < 6) || (argc > 7))
4834 e = Jim_GetLong(interp, argv[1], &l);
4840 e = Jim_GetLong(interp, argv[2], &l);
4846 e = Jim_GetLong(interp, argv[3], &l);
4852 e = Jim_GetLong(interp, argv[4], &l);
4858 e = Jim_GetLong(interp, argv[5], &l);
4868 e = Jim_GetLong(interp, argv[6], &l);
4874 retval = target_mcr(target, cpnum, op1, op2, CRn, CRm, value);
4875 if (retval != ERROR_OK)
4879 retval = target_mrc(target, cpnum, op1, op2, CRn, CRm, &value);
4880 if (retval != ERROR_OK)
4883 Jim_SetResult(interp, Jim_NewIntObj(interp, value));