Revert "target: add target->type->has_mmu fn"
[openocd/genbsdl.git] / src / target / target.c
blob6e5d3fbd98edbee76454619be7941fa14d6ab720
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007-2009 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
10 * *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
13 * *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
16 * *
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. *
21 * *
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. *
26 * *
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 ***************************************************************************/
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
36 #include "target.h"
37 #include "target_type.h"
38 #include "target_request.h"
39 #include "time_support.h"
40 #include "register.h"
41 #include "trace.h"
42 #include "image.h"
43 #include "jtag.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);
79 /* targets */
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[] =
98 &arm7tdmi_target,
99 &arm9tdmi_target,
100 &arm920t_target,
101 &arm720t_target,
102 &arm966e_target,
103 &arm926ejs_target,
104 &fa526_target,
105 &feroceon_target,
106 &dragonite_target,
107 &xscale_target,
108 &cortexm3_target,
109 &cortexa8_target,
110 &arm11_target,
111 &mips_m4k_target,
112 &avr_target,
113 NULL,
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)
147 const Jim_Nvp *n;
149 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
150 if (n->name == NULL) {
151 return "unknown";
152 } else {
153 return n->name;
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 },
243 const char *
244 target_state_name( target_t *t )
246 const char *cp;
247 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
248 if( !cp ){
249 LOG_ERROR("Invalid target state: %d", (int)(t->state));
250 cp = "(*BUG*unknown*BUG*)";
252 return cp;
255 /* determine the number of the new target */
256 static int new_target_number(void)
258 target_t *t;
259 int x;
261 /* number is 0 based */
262 x = -1;
263 t = all_targets;
264 while (t) {
265 if (x < t->target_number) {
266 x = t->target_number;
268 t = t->next;
270 return x + 1;
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);
278 else
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);
287 else
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);
302 else
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);
311 else
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)
318 *buffer = value;
321 /* return a pointer to a configured target; id is name or number */
322 target_t *get_target(const char *id)
324 target_t *target;
326 /* try as tcltarget name */
327 for (target = all_targets; target; target = target->next) {
328 if (target->cmd_name == NULL)
329 continue;
330 if (strcmp(id, target->cmd_name) == 0)
331 return target;
334 /* It's OK to remove this fallback sometime after August 2010 or so */
336 /* no match, try as number */
337 unsigned num;
338 if (parse_uint(id, &num) != ERROR_OK)
339 return NULL;
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);
345 return target;
349 return NULL;
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;
357 while (target) {
358 if (target->target_number == num) {
359 return target;
361 target = target->next;
364 return NULL;
367 target_t* get_current_target(command_context_t *cmd_ctx)
369 target_t *target = get_target_by_num(cmd_ctx->current_target);
371 if (target == NULL)
373 LOG_ERROR("BUG: current_target out of bounds");
374 exit(-1);
377 return target;
380 int target_poll(struct target_s *target)
382 int retval;
384 /* We can't poll until after examine */
385 if (!target_was_examined(target))
387 /* Fail silently lest we pollute the log */
388 return ERROR_FAIL;
391 retval = target->type->poll(target);
392 if (retval != ERROR_OK)
393 return retval;
395 if (target->halt_issued)
397 if (target->state == TARGET_HALTED)
399 target->halt_issued = false;
400 } else
402 long long t = timeval_ms() - target->halt_issued_time;
403 if (t>1000)
405 target->halt_issued = false;
406 LOG_INFO("Halt timed out, wake up GDB.");
407 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
412 return ERROR_OK;
415 int target_halt(struct target_s *target)
417 int retval;
418 /* We can't poll until after examine */
419 if (!target_was_examined(target))
421 LOG_ERROR("Target not examined yet");
422 return ERROR_FAIL;
425 retval = target->type->halt(target);
426 if (retval != ERROR_OK)
427 return retval;
429 target->halt_issued = true;
430 target->halt_issued_time = timeval_ms();
432 return ERROR_OK;
435 int target_resume(struct target_s *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
437 int retval;
439 /* We can't poll until after examine */
440 if (!target_was_examined(target))
442 LOG_ERROR("Target not examined yet");
443 return ERROR_FAIL;
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?)
448 * the application.
450 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
451 return retval;
453 return retval;
456 int target_process_reset(struct command_context_s *cmd_ctx, enum target_reset_mode reset_mode)
458 char buf[100];
459 int retval;
460 Jim_Nvp *n;
461 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
462 if (n->name == NULL) {
463 LOG_ERROR("invalid reset mode");
464 return ERROR_FAIL;
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);
482 return ERROR_FAIL;
485 /* We want any events to be processed before the prompt */
486 retval = target_call_timer_callbacks_now();
488 return retval;
491 static int default_virt2phys(struct target_s *target, uint32_t virtual, uint32_t *physical)
493 *physical = virtual;
494 return ERROR_OK;
497 static int default_mmu(struct target_s *target, int *enabled)
499 *enabled = 0;
500 return ERROR_OK;
503 static int default_examine(struct target_s *target)
505 target_set_examined(target);
506 return ERROR_OK;
509 int target_examine_one(struct target_s *target)
511 return target->type->examine(target);
514 static int jtag_enable_callback(enum jtag_event event, void *priv)
516 target_t *target = priv;
518 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
519 return ERROR_OK;
521 jtag_unregister_event_callback(jtag_enable_callback, target);
522 return target_examine_one(target);
526 /* Targets that correctly implement init + examine, i.e.
527 * no communication with target during init:
529 * XScale
531 int target_examine(void)
533 int retval = ERROR_OK;
534 target_t *target;
536 for (target = all_targets; target; target = target->next)
538 /* defer examination, but don't skip it */
539 if (!target->tap->enabled) {
540 jtag_register_event_callback(jtag_enable_callback,
541 target);
542 continue;
544 if ((retval = target_examine_one(target)) != ERROR_OK)
545 return retval;
547 return retval;
549 const char *target_get_name(struct target_s *target)
551 return target->type->name;
554 static int target_write_memory_imp(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
556 if (!target_was_examined(target))
558 LOG_ERROR("Target not examined yet");
559 return ERROR_FAIL;
561 return target->type->write_memory_imp(target, address, size, count, buffer);
564 static int target_read_memory_imp(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
566 if (!target_was_examined(target))
568 LOG_ERROR("Target not examined yet");
569 return ERROR_FAIL;
571 return target->type->read_memory_imp(target, address, size, count, buffer);
574 static int target_soft_reset_halt_imp(struct target_s *target)
576 if (!target_was_examined(target))
578 LOG_ERROR("Target not examined yet");
579 return ERROR_FAIL;
581 if (!target->type->soft_reset_halt_imp) {
582 LOG_ERROR("Target %s does not support soft_reset_halt",
583 target->cmd_name);
584 return ERROR_FAIL;
586 return target->type->soft_reset_halt_imp(target);
589 static int target_run_algorithm_imp(struct target_s *target, int num_mem_params, mem_param_t *mem_params, int num_reg_params, reg_param_t *reg_param, uint32_t entry_point, uint32_t exit_point, int timeout_ms, void *arch_info)
591 if (!target_was_examined(target))
593 LOG_ERROR("Target not examined yet");
594 return ERROR_FAIL;
596 return target->type->run_algorithm_imp(target, num_mem_params, mem_params, num_reg_params, reg_param, entry_point, exit_point, timeout_ms, arch_info);
599 int target_read_memory(struct target_s *target,
600 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
602 return target->type->read_memory(target, address, size, count, buffer);
605 int target_read_phys_memory(struct target_s *target,
606 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
608 return target->type->read_phys_memory(target, address, size, count, buffer);
611 int target_write_memory(struct target_s *target,
612 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
614 return target->type->write_memory(target, address, size, count, buffer);
617 int target_write_phys_memory(struct target_s *target,
618 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
620 return target->type->write_phys_memory(target, address, size, count, buffer);
623 int target_bulk_write_memory(struct target_s *target,
624 uint32_t address, uint32_t count, uint8_t *buffer)
626 return target->type->bulk_write_memory(target, address, count, buffer);
629 int target_add_breakpoint(struct target_s *target,
630 struct breakpoint_s *breakpoint)
632 return target->type->add_breakpoint(target, breakpoint);
634 int target_remove_breakpoint(struct target_s *target,
635 struct breakpoint_s *breakpoint)
637 return target->type->remove_breakpoint(target, breakpoint);
640 int target_add_watchpoint(struct target_s *target,
641 struct watchpoint_s *watchpoint)
643 return target->type->add_watchpoint(target, watchpoint);
645 int target_remove_watchpoint(struct target_s *target,
646 struct watchpoint_s *watchpoint)
648 return target->type->remove_watchpoint(target, watchpoint);
651 int target_get_gdb_reg_list(struct target_s *target,
652 struct reg_s **reg_list[], int *reg_list_size)
654 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
656 int target_step(struct target_s *target,
657 int current, uint32_t address, int handle_breakpoints)
659 return target->type->step(target, current, address, handle_breakpoints);
663 int target_run_algorithm(struct target_s *target,
664 int num_mem_params, mem_param_t *mem_params,
665 int num_reg_params, reg_param_t *reg_param,
666 uint32_t entry_point, uint32_t exit_point,
667 int timeout_ms, void *arch_info)
669 return target->type->run_algorithm(target,
670 num_mem_params, mem_params, num_reg_params, reg_param,
671 entry_point, exit_point, timeout_ms, arch_info);
674 /// @returns @c true if the target has been examined.
675 bool target_was_examined(struct target_s *target)
677 return target->type->examined;
679 /// Sets the @c examined flag for the given target.
680 void target_set_examined(struct target_s *target)
682 target->type->examined = true;
684 // Reset the @c examined flag for the given target.
685 void target_reset_examined(struct target_s *target)
687 target->type->examined = false;
692 static int default_mrc(struct target_s *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t *value)
694 LOG_ERROR("Not implemented");
695 return ERROR_FAIL;
698 static int default_mcr(struct target_s *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t value)
700 LOG_ERROR("Not implemented");
701 return ERROR_FAIL;
704 static int arm_cp_check(struct target_s *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm)
706 /* basic check */
707 if (!target_was_examined(target))
709 LOG_ERROR("Target not examined yet");
710 return ERROR_FAIL;
713 if ((cpnum <0) || (cpnum > 15))
715 LOG_ERROR("Illegal co-processor %d", cpnum);
716 return ERROR_FAIL;
719 if (op1 > 7)
721 LOG_ERROR("Illegal op1");
722 return ERROR_FAIL;
725 if (op2 > 7)
727 LOG_ERROR("Illegal op2");
728 return ERROR_FAIL;
731 if (CRn > 15)
733 LOG_ERROR("Illegal CRn");
734 return ERROR_FAIL;
737 if (CRm > 15)
739 LOG_ERROR("Illegal CRm");
740 return ERROR_FAIL;
743 return ERROR_OK;
746 int target_mrc(struct target_s *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t *value)
748 int retval;
750 retval = arm_cp_check(target, cpnum, op1, op2, CRn, CRm);
751 if (retval != ERROR_OK)
752 return retval;
754 return target->type->mrc(target, cpnum, op1, op2, CRn, CRm, value);
757 int target_mcr(struct target_s *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t value)
759 int retval;
761 retval = arm_cp_check(target, cpnum, op1, op2, CRn, CRm);
762 if (retval != ERROR_OK)
763 return retval;
765 return target->type->mcr(target, cpnum, op1, op2, CRn, CRm, value);
768 static int default_read_phys_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
770 LOG_ERROR("Not implemented");
771 return ERROR_FAIL;
774 static int default_write_phys_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
776 LOG_ERROR("Not implemented");
777 return ERROR_FAIL;
781 int target_init(struct command_context_s *cmd_ctx)
783 target_t *target = all_targets;
784 int retval;
786 while (target)
788 target_reset_examined(target);
789 if (target->type->examine == NULL)
791 target->type->examine = default_examine;
794 if ((retval = target->type->init_target(cmd_ctx, target)) != ERROR_OK)
796 LOG_ERROR("target '%s' init failed", target_get_name(target));
797 return retval;
800 /* Set up default functions if none are provided by target */
801 if (target->type->virt2phys == NULL)
803 target->type->virt2phys = default_virt2phys;
806 if (target->type->read_phys_memory == NULL)
808 target->type->read_phys_memory = default_read_phys_memory;
811 if (target->type->write_phys_memory == NULL)
813 target->type->write_phys_memory = default_write_phys_memory;
816 if (target->type->mcr == NULL)
818 target->type->mcr = default_mcr;
819 } else
821 /* FIX! multiple targets will generally register global commands
822 * multiple times. Only register this one if *one* of the
823 * targets need the command. Hmm... make it a command on the
824 * Jim Tcl target object?
826 register_jim(cmd_ctx, "mcr", jim_mcrmrc, "write coprocessor <cpnum> <op1> <op2> <CRn> <CRm> <value>");
829 if (target->type->mrc == NULL)
831 target->type->mrc = default_mrc;
832 } else
834 register_jim(cmd_ctx, "mrc", jim_mcrmrc, "read coprocessor <cpnum> <op1> <op2> <CRn> <CRm>");
838 /* a non-invasive way(in terms of patches) to add some code that
839 * runs before the type->write/read_memory implementation
841 target->type->write_memory_imp = target->type->write_memory;
842 target->type->write_memory = target_write_memory_imp;
843 target->type->read_memory_imp = target->type->read_memory;
844 target->type->read_memory = target_read_memory_imp;
845 target->type->soft_reset_halt_imp = target->type->soft_reset_halt;
846 target->type->soft_reset_halt = target_soft_reset_halt_imp;
847 target->type->run_algorithm_imp = target->type->run_algorithm;
848 target->type->run_algorithm = target_run_algorithm_imp;
850 if (target->type->mmu == NULL)
852 target->type->mmu = default_mmu;
854 target = target->next;
857 if (all_targets)
859 if ((retval = target_register_user_commands(cmd_ctx)) != ERROR_OK)
860 return retval;
861 if ((retval = target_register_timer_callback(handle_target, 100, 1, NULL)) != ERROR_OK)
862 return retval;
865 return ERROR_OK;
868 int target_register_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
870 target_event_callback_t **callbacks_p = &target_event_callbacks;
872 if (callback == NULL)
874 return ERROR_INVALID_ARGUMENTS;
877 if (*callbacks_p)
879 while ((*callbacks_p)->next)
880 callbacks_p = &((*callbacks_p)->next);
881 callbacks_p = &((*callbacks_p)->next);
884 (*callbacks_p) = malloc(sizeof(target_event_callback_t));
885 (*callbacks_p)->callback = callback;
886 (*callbacks_p)->priv = priv;
887 (*callbacks_p)->next = NULL;
889 return ERROR_OK;
892 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
894 target_timer_callback_t **callbacks_p = &target_timer_callbacks;
895 struct timeval now;
897 if (callback == NULL)
899 return ERROR_INVALID_ARGUMENTS;
902 if (*callbacks_p)
904 while ((*callbacks_p)->next)
905 callbacks_p = &((*callbacks_p)->next);
906 callbacks_p = &((*callbacks_p)->next);
909 (*callbacks_p) = malloc(sizeof(target_timer_callback_t));
910 (*callbacks_p)->callback = callback;
911 (*callbacks_p)->periodic = periodic;
912 (*callbacks_p)->time_ms = time_ms;
914 gettimeofday(&now, NULL);
915 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
916 time_ms -= (time_ms % 1000);
917 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
918 if ((*callbacks_p)->when.tv_usec > 1000000)
920 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
921 (*callbacks_p)->when.tv_sec += 1;
924 (*callbacks_p)->priv = priv;
925 (*callbacks_p)->next = NULL;
927 return ERROR_OK;
930 int target_unregister_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
932 target_event_callback_t **p = &target_event_callbacks;
933 target_event_callback_t *c = target_event_callbacks;
935 if (callback == NULL)
937 return ERROR_INVALID_ARGUMENTS;
940 while (c)
942 target_event_callback_t *next = c->next;
943 if ((c->callback == callback) && (c->priv == priv))
945 *p = next;
946 free(c);
947 return ERROR_OK;
949 else
950 p = &(c->next);
951 c = next;
954 return ERROR_OK;
957 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
959 target_timer_callback_t **p = &target_timer_callbacks;
960 target_timer_callback_t *c = target_timer_callbacks;
962 if (callback == NULL)
964 return ERROR_INVALID_ARGUMENTS;
967 while (c)
969 target_timer_callback_t *next = c->next;
970 if ((c->callback == callback) && (c->priv == priv))
972 *p = next;
973 free(c);
974 return ERROR_OK;
976 else
977 p = &(c->next);
978 c = next;
981 return ERROR_OK;
984 int target_call_event_callbacks(target_t *target, enum target_event event)
986 target_event_callback_t *callback = target_event_callbacks;
987 target_event_callback_t *next_callback;
989 if (event == TARGET_EVENT_HALTED)
991 /* execute early halted first */
992 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
995 LOG_DEBUG("target event %i (%s)",
996 event,
997 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
999 target_handle_event(target, event);
1001 while (callback)
1003 next_callback = callback->next;
1004 callback->callback(target, event, callback->priv);
1005 callback = next_callback;
1008 return ERROR_OK;
1011 static int target_timer_callback_periodic_restart(
1012 target_timer_callback_t *cb, struct timeval *now)
1014 int time_ms = cb->time_ms;
1015 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
1016 time_ms -= (time_ms % 1000);
1017 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
1018 if (cb->when.tv_usec > 1000000)
1020 cb->when.tv_usec = cb->when.tv_usec - 1000000;
1021 cb->when.tv_sec += 1;
1023 return ERROR_OK;
1026 static int target_call_timer_callback(target_timer_callback_t *cb,
1027 struct timeval *now)
1029 cb->callback(cb->priv);
1031 if (cb->periodic)
1032 return target_timer_callback_periodic_restart(cb, now);
1034 return target_unregister_timer_callback(cb->callback, cb->priv);
1037 static int target_call_timer_callbacks_check_time(int checktime)
1039 keep_alive();
1041 struct timeval now;
1042 gettimeofday(&now, NULL);
1044 target_timer_callback_t *callback = target_timer_callbacks;
1045 while (callback)
1047 // cleaning up may unregister and free this callback
1048 target_timer_callback_t *next_callback = callback->next;
1050 bool call_it = callback->callback &&
1051 ((!checktime && callback->periodic) ||
1052 now.tv_sec > callback->when.tv_sec ||
1053 (now.tv_sec == callback->when.tv_sec &&
1054 now.tv_usec >= callback->when.tv_usec));
1056 if (call_it)
1058 int retval = target_call_timer_callback(callback, &now);
1059 if (retval != ERROR_OK)
1060 return retval;
1063 callback = next_callback;
1066 return ERROR_OK;
1069 int target_call_timer_callbacks(void)
1071 return target_call_timer_callbacks_check_time(1);
1074 /* invoke periodic callbacks immediately */
1075 int target_call_timer_callbacks_now(void)
1077 return target_call_timer_callbacks_check_time(0);
1080 int target_alloc_working_area(struct target_s *target, uint32_t size, working_area_t **area)
1082 working_area_t *c = target->working_areas;
1083 working_area_t *new_wa = NULL;
1085 /* Reevaluate working area address based on MMU state*/
1086 if (target->working_areas == NULL)
1088 int retval;
1089 int enabled;
1091 retval = target->type->mmu(target, &enabled);
1092 if (retval != ERROR_OK)
1094 return retval;
1097 if (!enabled) {
1098 if (target->working_area_phys_spec) {
1099 LOG_DEBUG("MMU disabled, using physical "
1100 "address for working memory 0x%08x",
1101 (unsigned)target->working_area_phys);
1102 target->working_area = target->working_area_phys;
1103 } else {
1104 LOG_ERROR("No working memory available. "
1105 "Specify -work-area-phys to target.");
1106 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1108 } else {
1109 if (target->working_area_virt_spec) {
1110 LOG_DEBUG("MMU enabled, using virtual "
1111 "address for working memory 0x%08x",
1112 (unsigned)target->working_area_virt);
1113 target->working_area = target->working_area_virt;
1114 } else {
1115 LOG_ERROR("No working memory available. "
1116 "Specify -work-area-virt to target.");
1117 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1122 /* only allocate multiples of 4 byte */
1123 if (size % 4)
1125 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
1126 size = (size + 3) & (~3);
1129 /* see if there's already a matching working area */
1130 while (c)
1132 if ((c->free) && (c->size == size))
1134 new_wa = c;
1135 break;
1137 c = c->next;
1140 /* if not, allocate a new one */
1141 if (!new_wa)
1143 working_area_t **p = &target->working_areas;
1144 uint32_t first_free = target->working_area;
1145 uint32_t free_size = target->working_area_size;
1147 c = target->working_areas;
1148 while (c)
1150 first_free += c->size;
1151 free_size -= c->size;
1152 p = &c->next;
1153 c = c->next;
1156 if (free_size < size)
1158 LOG_WARNING("not enough working area available(requested %u, free %u)",
1159 (unsigned)(size), (unsigned)(free_size));
1160 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1163 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free);
1165 new_wa = malloc(sizeof(working_area_t));
1166 new_wa->next = NULL;
1167 new_wa->size = size;
1168 new_wa->address = first_free;
1170 if (target->backup_working_area)
1172 int retval;
1173 new_wa->backup = malloc(new_wa->size);
1174 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1176 free(new_wa->backup);
1177 free(new_wa);
1178 return retval;
1181 else
1183 new_wa->backup = NULL;
1186 /* put new entry in list */
1187 *p = new_wa;
1190 /* mark as used, and return the new (reused) area */
1191 new_wa->free = 0;
1192 *area = new_wa;
1194 /* user pointer */
1195 new_wa->user = area;
1197 return ERROR_OK;
1200 int target_free_working_area_restore(struct target_s *target, working_area_t *area, int restore)
1202 if (area->free)
1203 return ERROR_OK;
1205 if (restore && target->backup_working_area)
1207 int retval;
1208 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1209 return retval;
1212 area->free = 1;
1214 /* mark user pointer invalid */
1215 *area->user = NULL;
1216 area->user = NULL;
1218 return ERROR_OK;
1221 int target_free_working_area(struct target_s *target, working_area_t *area)
1223 return target_free_working_area_restore(target, area, 1);
1226 /* free resources and restore memory, if restoring memory fails,
1227 * free up resources anyway
1229 void target_free_all_working_areas_restore(struct target_s *target, int restore)
1231 working_area_t *c = target->working_areas;
1233 while (c)
1235 working_area_t *next = c->next;
1236 target_free_working_area_restore(target, c, restore);
1238 if (c->backup)
1239 free(c->backup);
1241 free(c);
1243 c = next;
1246 target->working_areas = NULL;
1249 void target_free_all_working_areas(struct target_s *target)
1251 target_free_all_working_areas_restore(target, 1);
1254 int target_register_commands(struct command_context_s *cmd_ctx)
1257 register_command(cmd_ctx, NULL, "targets",
1258 handle_targets_command, COMMAND_EXEC,
1259 "change current command line target (one parameter) "
1260 "or list targets (no parameters)");
1262 register_jim(cmd_ctx, "target", jim_target, "configure target");
1264 return ERROR_OK;
1267 int target_arch_state(struct target_s *target)
1269 int retval;
1270 if (target == NULL)
1272 LOG_USER("No target has been configured");
1273 return ERROR_OK;
1276 LOG_USER("target state: %s", target_state_name( target ));
1278 if (target->state != TARGET_HALTED)
1279 return ERROR_OK;
1281 retval = target->type->arch_state(target);
1282 return retval;
1285 /* Single aligned words are guaranteed to use 16 or 32 bit access
1286 * mode respectively, otherwise data is handled as quickly as
1287 * possible
1289 int target_write_buffer(struct target_s *target, uint32_t address, uint32_t size, uint8_t *buffer)
1291 int retval;
1292 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1293 (int)size, (unsigned)address);
1295 if (!target_was_examined(target))
1297 LOG_ERROR("Target not examined yet");
1298 return ERROR_FAIL;
1301 if (size == 0) {
1302 return ERROR_OK;
1305 if ((address + size - 1) < address)
1307 /* GDB can request this when e.g. PC is 0xfffffffc*/
1308 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1309 (unsigned)address,
1310 (unsigned)size);
1311 return ERROR_FAIL;
1314 if (((address % 2) == 0) && (size == 2))
1316 return target_write_memory(target, address, 2, 1, buffer);
1319 /* handle unaligned head bytes */
1320 if (address % 4)
1322 uint32_t unaligned = 4 - (address % 4);
1324 if (unaligned > size)
1325 unaligned = size;
1327 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1328 return retval;
1330 buffer += unaligned;
1331 address += unaligned;
1332 size -= unaligned;
1335 /* handle aligned words */
1336 if (size >= 4)
1338 int aligned = size - (size % 4);
1340 /* use bulk writes above a certain limit. This may have to be changed */
1341 if (aligned > 128)
1343 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1344 return retval;
1346 else
1348 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1349 return retval;
1352 buffer += aligned;
1353 address += aligned;
1354 size -= aligned;
1357 /* handle tail writes of less than 4 bytes */
1358 if (size > 0)
1360 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1361 return retval;
1364 return ERROR_OK;
1367 /* Single aligned words are guaranteed to use 16 or 32 bit access
1368 * mode respectively, otherwise data is handled as quickly as
1369 * possible
1371 int target_read_buffer(struct target_s *target, uint32_t address, uint32_t size, uint8_t *buffer)
1373 int retval;
1374 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1375 (int)size, (unsigned)address);
1377 if (!target_was_examined(target))
1379 LOG_ERROR("Target not examined yet");
1380 return ERROR_FAIL;
1383 if (size == 0) {
1384 return ERROR_OK;
1387 if ((address + size - 1) < address)
1389 /* GDB can request this when e.g. PC is 0xfffffffc*/
1390 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1391 address,
1392 size);
1393 return ERROR_FAIL;
1396 if (((address % 2) == 0) && (size == 2))
1398 return target_read_memory(target, address, 2, 1, buffer);
1401 /* handle unaligned head bytes */
1402 if (address % 4)
1404 uint32_t unaligned = 4 - (address % 4);
1406 if (unaligned > size)
1407 unaligned = size;
1409 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1410 return retval;
1412 buffer += unaligned;
1413 address += unaligned;
1414 size -= unaligned;
1417 /* handle aligned words */
1418 if (size >= 4)
1420 int aligned = size - (size % 4);
1422 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1423 return retval;
1425 buffer += aligned;
1426 address += aligned;
1427 size -= aligned;
1430 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1431 if(size >=2)
1433 int aligned = size - (size%2);
1434 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1435 if (retval != ERROR_OK)
1436 return retval;
1438 buffer += aligned;
1439 address += aligned;
1440 size -= aligned;
1442 /* handle tail writes of less than 4 bytes */
1443 if (size > 0)
1445 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1446 return retval;
1449 return ERROR_OK;
1452 int target_checksum_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t* crc)
1454 uint8_t *buffer;
1455 int retval;
1456 uint32_t i;
1457 uint32_t checksum = 0;
1458 if (!target_was_examined(target))
1460 LOG_ERROR("Target not examined yet");
1461 return ERROR_FAIL;
1464 if ((retval = target->type->checksum_memory(target, address,
1465 size, &checksum)) != ERROR_OK)
1467 buffer = malloc(size);
1468 if (buffer == NULL)
1470 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1471 return ERROR_INVALID_ARGUMENTS;
1473 retval = target_read_buffer(target, address, size, buffer);
1474 if (retval != ERROR_OK)
1476 free(buffer);
1477 return retval;
1480 /* convert to target endianess */
1481 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1483 uint32_t target_data;
1484 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1485 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1488 retval = image_calculate_checksum(buffer, size, &checksum);
1489 free(buffer);
1492 *crc = checksum;
1494 return retval;
1497 int target_blank_check_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t* blank)
1499 int retval;
1500 if (!target_was_examined(target))
1502 LOG_ERROR("Target not examined yet");
1503 return ERROR_FAIL;
1506 if (target->type->blank_check_memory == 0)
1507 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1509 retval = target->type->blank_check_memory(target, address, size, blank);
1511 return retval;
1514 int target_read_u32(struct target_s *target, uint32_t address, uint32_t *value)
1516 uint8_t value_buf[4];
1517 if (!target_was_examined(target))
1519 LOG_ERROR("Target not examined yet");
1520 return ERROR_FAIL;
1523 int retval = target_read_memory(target, address, 4, 1, value_buf);
1525 if (retval == ERROR_OK)
1527 *value = target_buffer_get_u32(target, value_buf);
1528 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1529 address,
1530 *value);
1532 else
1534 *value = 0x0;
1535 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1536 address);
1539 return retval;
1542 int target_read_u16(struct target_s *target, uint32_t address, uint16_t *value)
1544 uint8_t value_buf[2];
1545 if (!target_was_examined(target))
1547 LOG_ERROR("Target not examined yet");
1548 return ERROR_FAIL;
1551 int retval = target_read_memory(target, address, 2, 1, value_buf);
1553 if (retval == ERROR_OK)
1555 *value = target_buffer_get_u16(target, value_buf);
1556 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1557 address,
1558 *value);
1560 else
1562 *value = 0x0;
1563 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1564 address);
1567 return retval;
1570 int target_read_u8(struct target_s *target, uint32_t address, uint8_t *value)
1572 int retval = target_read_memory(target, address, 1, 1, value);
1573 if (!target_was_examined(target))
1575 LOG_ERROR("Target not examined yet");
1576 return ERROR_FAIL;
1579 if (retval == ERROR_OK)
1581 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1582 address,
1583 *value);
1585 else
1587 *value = 0x0;
1588 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1589 address);
1592 return retval;
1595 int target_write_u32(struct target_s *target, uint32_t address, uint32_t value)
1597 int retval;
1598 uint8_t value_buf[4];
1599 if (!target_was_examined(target))
1601 LOG_ERROR("Target not examined yet");
1602 return ERROR_FAIL;
1605 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1606 address,
1607 value);
1609 target_buffer_set_u32(target, value_buf, value);
1610 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1612 LOG_DEBUG("failed: %i", retval);
1615 return retval;
1618 int target_write_u16(struct target_s *target, uint32_t address, uint16_t value)
1620 int retval;
1621 uint8_t value_buf[2];
1622 if (!target_was_examined(target))
1624 LOG_ERROR("Target not examined yet");
1625 return ERROR_FAIL;
1628 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1629 address,
1630 value);
1632 target_buffer_set_u16(target, value_buf, value);
1633 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1635 LOG_DEBUG("failed: %i", retval);
1638 return retval;
1641 int target_write_u8(struct target_s *target, uint32_t address, uint8_t value)
1643 int retval;
1644 if (!target_was_examined(target))
1646 LOG_ERROR("Target not examined yet");
1647 return ERROR_FAIL;
1650 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1651 address, value);
1653 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1655 LOG_DEBUG("failed: %i", retval);
1658 return retval;
1661 int target_register_user_commands(struct command_context_s *cmd_ctx)
1663 int retval = ERROR_OK;
1666 /* script procedures */
1667 register_command(cmd_ctx, NULL, "profile", handle_profile_command, COMMAND_EXEC, "profiling samples the CPU PC");
1668 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>");
1669 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>");
1671 register_command(cmd_ctx, NULL, "fast_load_image", handle_fast_load_image_command, COMMAND_ANY,
1672 "same args as load_image, image stored in memory - mainly for profiling purposes");
1674 register_command(cmd_ctx, NULL, "fast_load", handle_fast_load_command, COMMAND_ANY,
1675 "loads active fast load image to current target - mainly for profiling purposes");
1678 register_command(cmd_ctx, NULL, "virt2phys", handle_virt2phys_command, COMMAND_ANY, "translate a virtual address into a physical address");
1679 register_command(cmd_ctx, NULL, "reg", handle_reg_command, COMMAND_EXEC, "display or set a register");
1680 register_command(cmd_ctx, NULL, "poll", handle_poll_command, COMMAND_EXEC, "poll target state");
1681 register_command(cmd_ctx, NULL, "wait_halt", handle_wait_halt_command, COMMAND_EXEC, "wait for target halt [time (s)]");
1682 register_command(cmd_ctx, NULL, "halt", handle_halt_command, COMMAND_EXEC, "halt target");
1683 register_command(cmd_ctx, NULL, "resume", handle_resume_command, COMMAND_EXEC, "resume target [addr]");
1684 register_command(cmd_ctx, NULL, "step", handle_step_command, COMMAND_EXEC, "step one instruction from current PC or [addr]");
1685 register_command(cmd_ctx, NULL, "reset", handle_reset_command, COMMAND_EXEC, "reset target [run | halt | init] - default is run");
1686 register_command(cmd_ctx, NULL, "soft_reset_halt", handle_soft_reset_halt_command, COMMAND_EXEC, "halt the target and do a soft reset");
1688 register_command(cmd_ctx, NULL, "mdw", handle_md_command, COMMAND_EXEC, "display memory words [phys] <addr> [count]");
1689 register_command(cmd_ctx, NULL, "mdh", handle_md_command, COMMAND_EXEC, "display memory half-words [phys] <addr> [count]");
1690 register_command(cmd_ctx, NULL, "mdb", handle_md_command, COMMAND_EXEC, "display memory bytes [phys] <addr> [count]");
1692 register_command(cmd_ctx, NULL, "mww", handle_mw_command, COMMAND_EXEC, "write memory word [phys] <addr> <value> [count]");
1693 register_command(cmd_ctx, NULL, "mwh", handle_mw_command, COMMAND_EXEC, "write memory half-word [phys] <addr> <value> [count]");
1694 register_command(cmd_ctx, NULL, "mwb", handle_mw_command, COMMAND_EXEC, "write memory byte [phys] <addr> <value> [count]");
1696 register_command(cmd_ctx, NULL, "bp",
1697 handle_bp_command, COMMAND_EXEC,
1698 "list or set breakpoint [<address> <length> [hw]]");
1699 register_command(cmd_ctx, NULL, "rbp",
1700 handle_rbp_command, COMMAND_EXEC,
1701 "remove breakpoint <address>");
1702 register_command(cmd_ctx, NULL, "wp",
1703 handle_wp_command, COMMAND_EXEC,
1704 "list or set watchpoint "
1705 "[<address> <length> <r/w/a> [value] [mask]]");
1706 register_command(cmd_ctx, NULL, "rwp",
1707 handle_rwp_command, COMMAND_EXEC,
1708 "remove watchpoint <address>");
1710 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]");
1711 register_command(cmd_ctx, NULL, "dump_image", handle_dump_image_command, COMMAND_EXEC, "dump_image <file> <address> <size>");
1712 register_command(cmd_ctx, NULL, "verify_image", handle_verify_image_command, COMMAND_EXEC, "verify_image <file> [offset] [type]");
1713 register_command(cmd_ctx, NULL, "test_image", handle_test_image_command, COMMAND_EXEC, "test_image <file> [offset] [type]");
1715 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
1716 return retval;
1717 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
1718 return retval;
1720 return retval;
1723 static int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1725 target_t *target = all_targets;
1727 if (argc == 1)
1729 target = get_target(args[0]);
1730 if (target == NULL) {
1731 command_print(cmd_ctx,"Target: %s is unknown, try one of:\n", args[0]);
1732 goto DumpTargets;
1734 if (!target->tap->enabled) {
1735 command_print(cmd_ctx,"Target: TAP %s is disabled, "
1736 "can't be the current target\n",
1737 target->tap->dotted_name);
1738 return ERROR_FAIL;
1741 cmd_ctx->current_target = target->target_number;
1742 return ERROR_OK;
1744 DumpTargets:
1746 target = all_targets;
1747 command_print(cmd_ctx, " TargetName Type Endian TapName State ");
1748 command_print(cmd_ctx, "-- ------------------ ---------- ------ ------------------ ------------");
1749 while (target)
1751 const char *state;
1752 char marker = ' ';
1754 if (target->tap->enabled)
1755 state = target_state_name( target );
1756 else
1757 state = "tap-disabled";
1759 if (cmd_ctx->current_target == target->target_number)
1760 marker = '*';
1762 /* keep columns lined up to match the headers above */
1763 command_print(cmd_ctx, "%2d%c %-18s %-10s %-6s %-18s %s",
1764 target->target_number,
1765 marker,
1766 target->cmd_name,
1767 target_get_name(target),
1768 Jim_Nvp_value2name_simple(nvp_target_endian,
1769 target->endianness)->name,
1770 target->tap->dotted_name,
1771 state);
1772 target = target->next;
1775 return ERROR_OK;
1778 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1780 static int powerDropout;
1781 static int srstAsserted;
1783 static int runPowerRestore;
1784 static int runPowerDropout;
1785 static int runSrstAsserted;
1786 static int runSrstDeasserted;
1788 static int sense_handler(void)
1790 static int prevSrstAsserted = 0;
1791 static int prevPowerdropout = 0;
1793 int retval;
1794 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1795 return retval;
1797 int powerRestored;
1798 powerRestored = prevPowerdropout && !powerDropout;
1799 if (powerRestored)
1801 runPowerRestore = 1;
1804 long long current = timeval_ms();
1805 static long long lastPower = 0;
1806 int waitMore = lastPower + 2000 > current;
1807 if (powerDropout && !waitMore)
1809 runPowerDropout = 1;
1810 lastPower = current;
1813 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1814 return retval;
1816 int srstDeasserted;
1817 srstDeasserted = prevSrstAsserted && !srstAsserted;
1819 static long long lastSrst = 0;
1820 waitMore = lastSrst + 2000 > current;
1821 if (srstDeasserted && !waitMore)
1823 runSrstDeasserted = 1;
1824 lastSrst = current;
1827 if (!prevSrstAsserted && srstAsserted)
1829 runSrstAsserted = 1;
1832 prevSrstAsserted = srstAsserted;
1833 prevPowerdropout = powerDropout;
1835 if (srstDeasserted || powerRestored)
1837 /* Other than logging the event we can't do anything here.
1838 * Issuing a reset is a particularly bad idea as we might
1839 * be inside a reset already.
1843 return ERROR_OK;
1846 static void target_call_event_callbacks_all(enum target_event e) {
1847 target_t *target;
1848 target = all_targets;
1849 while (target) {
1850 target_call_event_callbacks(target, e);
1851 target = target->next;
1855 /* process target state changes */
1856 int handle_target(void *priv)
1858 int retval = ERROR_OK;
1860 /* we do not want to recurse here... */
1861 static int recursive = 0;
1862 if (! recursive)
1864 recursive = 1;
1865 sense_handler();
1866 /* danger! running these procedures can trigger srst assertions and power dropouts.
1867 * We need to avoid an infinite loop/recursion here and we do that by
1868 * clearing the flags after running these events.
1870 int did_something = 0;
1871 if (runSrstAsserted)
1873 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1874 Jim_Eval(interp, "srst_asserted");
1875 did_something = 1;
1877 if (runSrstDeasserted)
1879 Jim_Eval(interp, "srst_deasserted");
1880 did_something = 1;
1882 if (runPowerDropout)
1884 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1885 Jim_Eval(interp, "power_dropout");
1886 did_something = 1;
1888 if (runPowerRestore)
1890 Jim_Eval(interp, "power_restore");
1891 did_something = 1;
1894 if (did_something)
1896 /* clear detect flags */
1897 sense_handler();
1900 /* clear action flags */
1902 runSrstAsserted = 0;
1903 runSrstDeasserted = 0;
1904 runPowerRestore = 0;
1905 runPowerDropout = 0;
1907 recursive = 0;
1910 /* Poll targets for state changes unless that's globally disabled.
1911 * Skip targets that are currently disabled.
1913 for (target_t *target = all_targets;
1914 is_jtag_poll_safe() && target;
1915 target = target->next)
1917 if (!target->tap->enabled)
1918 continue;
1920 /* only poll target if we've got power and srst isn't asserted */
1921 if (!powerDropout && !srstAsserted)
1923 /* polling may fail silently until the target has been examined */
1924 if ((retval = target_poll(target)) != ERROR_OK)
1926 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1927 return retval;
1932 return retval;
1935 static int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1937 target_t *target;
1938 reg_t *reg = NULL;
1939 int count = 0;
1940 char *value;
1942 LOG_DEBUG("-");
1944 target = get_current_target(cmd_ctx);
1946 /* list all available registers for the current target */
1947 if (argc == 0)
1949 reg_cache_t *cache = target->reg_cache;
1951 count = 0;
1952 while (cache)
1954 int i;
1956 command_print(cmd_ctx, "===== %s", cache->name);
1958 for (i = 0, reg = cache->reg_list;
1959 i < cache->num_regs;
1960 i++, reg++, count++)
1962 /* only print cached values if they are valid */
1963 if (reg->valid) {
1964 value = buf_to_str(reg->value,
1965 reg->size, 16);
1966 command_print(cmd_ctx,
1967 "(%i) %s (/%" PRIu32 "): 0x%s%s",
1968 count, reg->name,
1969 reg->size, value,
1970 reg->dirty
1971 ? " (dirty)"
1972 : "");
1973 free(value);
1974 } else {
1975 command_print(cmd_ctx, "(%i) %s (/%" PRIu32 ")",
1976 count, reg->name,
1977 reg->size) ;
1980 cache = cache->next;
1983 return ERROR_OK;
1986 /* access a single register by its ordinal number */
1987 if ((args[0][0] >= '0') && (args[0][0] <= '9'))
1989 unsigned num;
1990 COMMAND_PARSE_NUMBER(uint, args[0], num);
1992 reg_cache_t *cache = target->reg_cache;
1993 count = 0;
1994 while (cache)
1996 int i;
1997 for (i = 0; i < cache->num_regs; i++)
1999 if (count++ == (int)num)
2001 reg = &cache->reg_list[i];
2002 break;
2005 if (reg)
2006 break;
2007 cache = cache->next;
2010 if (!reg)
2012 command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
2013 return ERROR_OK;
2015 } else /* access a single register by its name */
2017 reg = register_get_by_name(target->reg_cache, args[0], 1);
2019 if (!reg)
2021 command_print(cmd_ctx, "register %s not found in current target", args[0]);
2022 return ERROR_OK;
2026 /* display a register */
2027 if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
2029 if ((argc == 2) && (strcmp(args[1], "force") == 0))
2030 reg->valid = 0;
2032 if (reg->valid == 0)
2034 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
2035 arch_type->get(reg);
2037 value = buf_to_str(reg->value, reg->size, 16);
2038 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2039 free(value);
2040 return ERROR_OK;
2043 /* set register value */
2044 if (argc == 2)
2046 uint8_t *buf = malloc(CEIL(reg->size, 8));
2047 str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
2049 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
2050 arch_type->set(reg, buf);
2052 value = buf_to_str(reg->value, reg->size, 16);
2053 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2054 free(value);
2056 free(buf);
2058 return ERROR_OK;
2061 command_print(cmd_ctx, "usage: reg <#|name> [value]");
2063 return ERROR_OK;
2066 static int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2068 int retval = ERROR_OK;
2069 target_t *target = get_current_target(cmd_ctx);
2071 if (argc == 0)
2073 command_print(cmd_ctx, "background polling: %s",
2074 jtag_poll_get_enabled() ? "on" : "off");
2075 command_print(cmd_ctx, "TAP: %s (%s)",
2076 target->tap->dotted_name,
2077 target->tap->enabled ? "enabled" : "disabled");
2078 if (!target->tap->enabled)
2079 return ERROR_OK;
2080 if ((retval = target_poll(target)) != ERROR_OK)
2081 return retval;
2082 if ((retval = target_arch_state(target)) != ERROR_OK)
2083 return retval;
2086 else if (argc == 1)
2088 if (strcmp(args[0], "on") == 0)
2090 jtag_poll_set_enabled(true);
2092 else if (strcmp(args[0], "off") == 0)
2094 jtag_poll_set_enabled(false);
2096 else
2098 command_print(cmd_ctx, "arg is \"on\" or \"off\"");
2100 } else
2102 return ERROR_COMMAND_SYNTAX_ERROR;
2105 return retval;
2108 static int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2110 if (argc > 1)
2111 return ERROR_COMMAND_SYNTAX_ERROR;
2113 unsigned ms = 5000;
2114 if (1 == argc)
2116 int retval = parse_uint(args[0], &ms);
2117 if (ERROR_OK != retval)
2119 command_print(cmd_ctx, "usage: %s [seconds]", cmd);
2120 return ERROR_COMMAND_SYNTAX_ERROR;
2122 // convert seconds (given) to milliseconds (needed)
2123 ms *= 1000;
2126 target_t *target = get_current_target(cmd_ctx);
2127 return target_wait_state(target, TARGET_HALTED, ms);
2130 /* wait for target state to change. The trick here is to have a low
2131 * latency for short waits and not to suck up all the CPU time
2132 * on longer waits.
2134 * After 500ms, keep_alive() is invoked
2136 int target_wait_state(target_t *target, enum target_state state, int ms)
2138 int retval;
2139 long long then = 0, cur;
2140 int once = 1;
2142 for (;;)
2144 if ((retval = target_poll(target)) != ERROR_OK)
2145 return retval;
2146 if (target->state == state)
2148 break;
2150 cur = timeval_ms();
2151 if (once)
2153 once = 0;
2154 then = timeval_ms();
2155 LOG_DEBUG("waiting for target %s...",
2156 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2159 if (cur-then > 500)
2161 keep_alive();
2164 if ((cur-then) > ms)
2166 LOG_ERROR("timed out while waiting for target %s",
2167 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2168 return ERROR_FAIL;
2172 return ERROR_OK;
2175 static int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2177 LOG_DEBUG("-");
2179 target_t *target = get_current_target(cmd_ctx);
2180 int retval = target_halt(target);
2181 if (ERROR_OK != retval)
2182 return retval;
2184 if (argc == 1)
2186 unsigned wait;
2187 retval = parse_uint(args[0], &wait);
2188 if (ERROR_OK != retval)
2189 return ERROR_COMMAND_SYNTAX_ERROR;
2190 if (!wait)
2191 return ERROR_OK;
2194 return handle_wait_halt_command(cmd_ctx, cmd, args, argc);
2197 static int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2199 target_t *target = get_current_target(cmd_ctx);
2201 LOG_USER("requesting target halt and executing a soft reset");
2203 target->type->soft_reset_halt(target);
2205 return ERROR_OK;
2208 static int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2210 if (argc > 1)
2211 return ERROR_COMMAND_SYNTAX_ERROR;
2213 enum target_reset_mode reset_mode = RESET_RUN;
2214 if (argc == 1)
2216 const Jim_Nvp *n;
2217 n = Jim_Nvp_name2value_simple(nvp_reset_modes, args[0]);
2218 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2219 return ERROR_COMMAND_SYNTAX_ERROR;
2221 reset_mode = n->value;
2224 /* reset *all* targets */
2225 return target_process_reset(cmd_ctx, reset_mode);
2229 static int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2231 int current = 1;
2232 if (argc > 1)
2233 return ERROR_COMMAND_SYNTAX_ERROR;
2235 target_t *target = get_current_target(cmd_ctx);
2236 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2238 /* with no args, resume from current pc, addr = 0,
2239 * with one arguments, addr = args[0],
2240 * handle breakpoints, not debugging */
2241 uint32_t addr = 0;
2242 if (argc == 1)
2244 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2245 current = 0;
2248 return target_resume(target, current, addr, 1, 0);
2251 static int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2253 if (argc > 1)
2254 return ERROR_COMMAND_SYNTAX_ERROR;
2256 LOG_DEBUG("-");
2258 /* with no args, step from current pc, addr = 0,
2259 * with one argument addr = args[0],
2260 * handle breakpoints, debugging */
2261 uint32_t addr = 0;
2262 int current_pc = 1;
2263 if (argc == 1)
2265 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2266 current_pc = 0;
2269 target_t *target = get_current_target(cmd_ctx);
2271 return target->type->step(target, current_pc, addr, 1);
2274 static void handle_md_output(struct command_context_s *cmd_ctx,
2275 struct target_s *target, uint32_t address, unsigned size,
2276 unsigned count, const uint8_t *buffer)
2278 const unsigned line_bytecnt = 32;
2279 unsigned line_modulo = line_bytecnt / size;
2281 char output[line_bytecnt * 4 + 1];
2282 unsigned output_len = 0;
2284 const char *value_fmt;
2285 switch (size) {
2286 case 4: value_fmt = "%8.8x "; break;
2287 case 2: value_fmt = "%4.2x "; break;
2288 case 1: value_fmt = "%2.2x "; break;
2289 default:
2290 LOG_ERROR("invalid memory read size: %u", size);
2291 exit(-1);
2294 for (unsigned i = 0; i < count; i++)
2296 if (i % line_modulo == 0)
2298 output_len += snprintf(output + output_len,
2299 sizeof(output) - output_len,
2300 "0x%8.8x: ",
2301 (unsigned)(address + (i*size)));
2304 uint32_t value = 0;
2305 const uint8_t *value_ptr = buffer + i * size;
2306 switch (size) {
2307 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2308 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2309 case 1: value = *value_ptr;
2311 output_len += snprintf(output + output_len,
2312 sizeof(output) - output_len,
2313 value_fmt, value);
2315 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2317 command_print(cmd_ctx, "%s", output);
2318 output_len = 0;
2323 static int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2325 if (argc < 1)
2326 return ERROR_COMMAND_SYNTAX_ERROR;
2328 unsigned size = 0;
2329 switch (cmd[2]) {
2330 case 'w': size = 4; break;
2331 case 'h': size = 2; break;
2332 case 'b': size = 1; break;
2333 default: return ERROR_COMMAND_SYNTAX_ERROR;
2336 bool physical=strcmp(args[0], "phys")==0;
2337 int (*fn)(struct target_s *target,
2338 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2339 if (physical)
2341 argc--;
2342 args++;
2343 fn=target_read_phys_memory;
2344 } else
2346 fn=target_read_memory;
2348 if ((argc < 1) || (argc > 2))
2350 return ERROR_COMMAND_SYNTAX_ERROR;
2353 uint32_t address;
2354 COMMAND_PARSE_NUMBER(u32, args[0], address);
2356 unsigned count = 1;
2357 if (argc == 2)
2358 COMMAND_PARSE_NUMBER(uint, args[1], count);
2360 uint8_t *buffer = calloc(count, size);
2362 target_t *target = get_current_target(cmd_ctx);
2363 int retval = fn(target, address, size, count, buffer);
2364 if (ERROR_OK == retval)
2365 handle_md_output(cmd_ctx, target, address, size, count, buffer);
2367 free(buffer);
2369 return retval;
2372 static int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2374 if (argc < 2)
2376 return ERROR_COMMAND_SYNTAX_ERROR;
2378 bool physical=strcmp(args[0], "phys")==0;
2379 int (*fn)(struct target_s *target,
2380 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2381 if (physical)
2383 argc--;
2384 args++;
2385 fn=target_write_phys_memory;
2386 } else
2388 fn=target_write_memory;
2390 if ((argc < 2) || (argc > 3))
2391 return ERROR_COMMAND_SYNTAX_ERROR;
2393 uint32_t address;
2394 COMMAND_PARSE_NUMBER(u32, args[0], address);
2396 uint32_t value;
2397 COMMAND_PARSE_NUMBER(u32, args[1], value);
2399 unsigned count = 1;
2400 if (argc == 3)
2401 COMMAND_PARSE_NUMBER(uint, args[2], count);
2403 target_t *target = get_current_target(cmd_ctx);
2404 unsigned wordsize;
2405 uint8_t value_buf[4];
2406 switch (cmd[2])
2408 case 'w':
2409 wordsize = 4;
2410 target_buffer_set_u32(target, value_buf, value);
2411 break;
2412 case 'h':
2413 wordsize = 2;
2414 target_buffer_set_u16(target, value_buf, value);
2415 break;
2416 case 'b':
2417 wordsize = 1;
2418 value_buf[0] = value;
2419 break;
2420 default:
2421 return ERROR_COMMAND_SYNTAX_ERROR;
2423 for (unsigned i = 0; i < count; i++)
2425 int retval = fn(target,
2426 address + i * wordsize, wordsize, 1, value_buf);
2427 if (ERROR_OK != retval)
2428 return retval;
2429 keep_alive();
2432 return ERROR_OK;
2436 static int parse_load_image_command_args(struct command_context_s *cmd_ctx,
2437 char **args, int argc, image_t *image,
2438 uint32_t *min_address, uint32_t *max_address)
2440 if (argc < 1 || argc > 5)
2441 return ERROR_COMMAND_SYNTAX_ERROR;
2443 /* a base address isn't always necessary,
2444 * default to 0x0 (i.e. don't relocate) */
2445 if (argc >= 2)
2447 uint32_t addr;
2448 COMMAND_PARSE_NUMBER(u32, args[1], addr);
2449 image->base_address = addr;
2450 image->base_address_set = 1;
2452 else
2453 image->base_address_set = 0;
2455 image->start_address_set = 0;
2457 if (argc >= 4)
2459 COMMAND_PARSE_NUMBER(u32, args[3], *min_address);
2461 if (argc == 5)
2463 COMMAND_PARSE_NUMBER(u32, args[4], *max_address);
2464 // use size (given) to find max (required)
2465 *max_address += *min_address;
2468 if (*min_address > *max_address)
2469 return ERROR_COMMAND_SYNTAX_ERROR;
2471 return ERROR_OK;
2474 static int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2476 uint8_t *buffer;
2477 uint32_t buf_cnt;
2478 uint32_t image_size;
2479 uint32_t min_address = 0;
2480 uint32_t max_address = 0xffffffff;
2481 int i;
2482 image_t image;
2484 int retval = parse_load_image_command_args(cmd_ctx, args, argc,
2485 &image, &min_address, &max_address);
2486 if (ERROR_OK != retval)
2487 return retval;
2489 target_t *target = get_current_target(cmd_ctx);
2491 struct duration bench;
2492 duration_start(&bench);
2494 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
2496 return ERROR_OK;
2499 image_size = 0x0;
2500 retval = ERROR_OK;
2501 for (i = 0; i < image.num_sections; i++)
2503 buffer = malloc(image.sections[i].size);
2504 if (buffer == NULL)
2506 command_print(cmd_ctx,
2507 "error allocating buffer for section (%d bytes)",
2508 (int)(image.sections[i].size));
2509 break;
2512 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2514 free(buffer);
2515 break;
2518 uint32_t offset = 0;
2519 uint32_t length = buf_cnt;
2521 /* DANGER!!! beware of unsigned comparision here!!! */
2523 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2524 (image.sections[i].base_address < max_address))
2526 if (image.sections[i].base_address < min_address)
2528 /* clip addresses below */
2529 offset += min_address-image.sections[i].base_address;
2530 length -= offset;
2533 if (image.sections[i].base_address + buf_cnt > max_address)
2535 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2538 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2540 free(buffer);
2541 break;
2543 image_size += length;
2544 command_print(cmd_ctx, "%u bytes written at address 0x%8.8" PRIx32 "",
2545 (unsigned int)length,
2546 image.sections[i].base_address + offset);
2549 free(buffer);
2552 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2554 command_print(cmd_ctx, "downloaded %" PRIu32 " bytes "
2555 "in %fs (%0.3f kb/s)", image_size,
2556 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2559 image_close(&image);
2561 return retval;
2565 static int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2567 fileio_t fileio;
2569 uint8_t buffer[560];
2570 int retvaltemp;
2573 target_t *target = get_current_target(cmd_ctx);
2575 if (argc != 3)
2577 command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
2578 return ERROR_OK;
2581 uint32_t address;
2582 COMMAND_PARSE_NUMBER(u32, args[1], address);
2583 uint32_t size;
2584 COMMAND_PARSE_NUMBER(u32, args[2], size);
2586 if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2588 return ERROR_OK;
2591 struct duration bench;
2592 duration_start(&bench);
2594 int retval = ERROR_OK;
2595 while (size > 0)
2597 uint32_t size_written;
2598 uint32_t this_run_size = (size > 560) ? 560 : size;
2599 retval = target_read_buffer(target, address, this_run_size, buffer);
2600 if (retval != ERROR_OK)
2602 break;
2605 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2606 if (retval != ERROR_OK)
2608 break;
2611 size -= this_run_size;
2612 address += this_run_size;
2615 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2616 return retvaltemp;
2618 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2620 command_print(cmd_ctx,
2621 "dumped %lld bytes in %fs (%0.3f kb/s)", fileio.size,
2622 duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
2625 return retval;
2628 static int handle_verify_image_command_internal(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, int verify)
2630 uint8_t *buffer;
2631 uint32_t buf_cnt;
2632 uint32_t image_size;
2633 int i;
2634 int retval;
2635 uint32_t checksum = 0;
2636 uint32_t mem_checksum = 0;
2638 image_t image;
2640 target_t *target = get_current_target(cmd_ctx);
2642 if (argc < 1)
2644 return ERROR_COMMAND_SYNTAX_ERROR;
2647 if (!target)
2649 LOG_ERROR("no target selected");
2650 return ERROR_FAIL;
2653 struct duration bench;
2654 duration_start(&bench);
2656 if (argc >= 2)
2658 uint32_t addr;
2659 COMMAND_PARSE_NUMBER(u32, args[1], addr);
2660 image.base_address = addr;
2661 image.base_address_set = 1;
2663 else
2665 image.base_address_set = 0;
2666 image.base_address = 0x0;
2669 image.start_address_set = 0;
2671 if ((retval = image_open(&image, args[0], (argc == 3) ? args[2] : NULL)) != ERROR_OK)
2673 return retval;
2676 image_size = 0x0;
2677 retval = ERROR_OK;
2678 for (i = 0; i < image.num_sections; i++)
2680 buffer = malloc(image.sections[i].size);
2681 if (buffer == NULL)
2683 command_print(cmd_ctx,
2684 "error allocating buffer for section (%d bytes)",
2685 (int)(image.sections[i].size));
2686 break;
2688 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2690 free(buffer);
2691 break;
2694 if (verify)
2696 /* calculate checksum of image */
2697 image_calculate_checksum(buffer, buf_cnt, &checksum);
2699 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2700 if (retval != ERROR_OK)
2702 free(buffer);
2703 break;
2706 if (checksum != mem_checksum)
2708 /* failed crc checksum, fall back to a binary compare */
2709 uint8_t *data;
2711 command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
2713 data = (uint8_t*)malloc(buf_cnt);
2715 /* Can we use 32bit word accesses? */
2716 int size = 1;
2717 int count = buf_cnt;
2718 if ((count % 4) == 0)
2720 size *= 4;
2721 count /= 4;
2723 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2724 if (retval == ERROR_OK)
2726 uint32_t t;
2727 for (t = 0; t < buf_cnt; t++)
2729 if (data[t] != buffer[t])
2731 command_print(cmd_ctx,
2732 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2733 (unsigned)(t + image.sections[i].base_address),
2734 data[t],
2735 buffer[t]);
2736 free(data);
2737 free(buffer);
2738 retval = ERROR_FAIL;
2739 goto done;
2741 if ((t%16384) == 0)
2743 keep_alive();
2748 free(data);
2750 } else
2752 command_print(cmd_ctx, "address 0x%08" PRIx32 " length 0x%08" PRIx32 "",
2753 image.sections[i].base_address,
2754 buf_cnt);
2757 free(buffer);
2758 image_size += buf_cnt;
2760 done:
2761 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2763 command_print(cmd_ctx, "verified %" PRIu32 " bytes "
2764 "in %fs (%0.3f kb/s)", image_size,
2765 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2768 image_close(&image);
2770 return retval;
2773 static int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2775 return handle_verify_image_command_internal(cmd_ctx, cmd, args, argc, 1);
2778 static int handle_test_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2780 return handle_verify_image_command_internal(cmd_ctx, cmd, args, argc, 0);
2783 static int handle_bp_command_list(struct command_context_s *cmd_ctx)
2785 target_t *target = get_current_target(cmd_ctx);
2786 breakpoint_t *breakpoint = target->breakpoints;
2787 while (breakpoint)
2789 if (breakpoint->type == BKPT_SOFT)
2791 char* buf = buf_to_str(breakpoint->orig_instr,
2792 breakpoint->length, 16);
2793 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2794 breakpoint->address,
2795 breakpoint->length,
2796 breakpoint->set, buf);
2797 free(buf);
2799 else
2801 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2802 breakpoint->address,
2803 breakpoint->length, breakpoint->set);
2806 breakpoint = breakpoint->next;
2808 return ERROR_OK;
2811 static int handle_bp_command_set(struct command_context_s *cmd_ctx,
2812 uint32_t addr, uint32_t length, int hw)
2814 target_t *target = get_current_target(cmd_ctx);
2815 int retval = breakpoint_add(target, addr, length, hw);
2816 if (ERROR_OK == retval)
2817 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2818 else
2819 LOG_ERROR("Failure setting breakpoint");
2820 return retval;
2823 static int handle_bp_command(struct command_context_s *cmd_ctx,
2824 char *cmd, char **args, int argc)
2826 if (argc == 0)
2827 return handle_bp_command_list(cmd_ctx);
2829 if (argc < 2 || argc > 3)
2831 command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
2832 return ERROR_COMMAND_SYNTAX_ERROR;
2835 uint32_t addr;
2836 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2837 uint32_t length;
2838 COMMAND_PARSE_NUMBER(u32, args[1], length);
2840 int hw = BKPT_SOFT;
2841 if (argc == 3)
2843 if (strcmp(args[2], "hw") == 0)
2844 hw = BKPT_HARD;
2845 else
2846 return ERROR_COMMAND_SYNTAX_ERROR;
2849 return handle_bp_command_set(cmd_ctx, addr, length, hw);
2852 static int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2854 if (argc != 1)
2855 return ERROR_COMMAND_SYNTAX_ERROR;
2857 uint32_t addr;
2858 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2860 target_t *target = get_current_target(cmd_ctx);
2861 breakpoint_remove(target, addr);
2863 return ERROR_OK;
2866 static int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2868 target_t *target = get_current_target(cmd_ctx);
2870 if (argc == 0)
2872 watchpoint_t *watchpoint = target->watchpoints;
2874 while (watchpoint)
2876 command_print(cmd_ctx,
2877 "address: 0x%8.8" PRIx32 ", len: 0x%8.8x, r/w/a: %i, value: 0x%8.8" PRIx32 ", mask: 0x%8.8" PRIx32 "",
2878 watchpoint->address,
2879 watchpoint->length,
2880 (int)(watchpoint->rw),
2881 watchpoint->value,
2882 watchpoint->mask);
2883 watchpoint = watchpoint->next;
2885 return ERROR_OK;
2888 enum watchpoint_rw type = WPT_ACCESS;
2889 uint32_t addr = 0;
2890 uint32_t length = 0;
2891 uint32_t data_value = 0x0;
2892 uint32_t data_mask = 0xffffffff;
2894 switch (argc)
2896 case 5:
2897 COMMAND_PARSE_NUMBER(u32, args[4], data_mask);
2898 // fall through
2899 case 4:
2900 COMMAND_PARSE_NUMBER(u32, args[3], data_value);
2901 // fall through
2902 case 3:
2903 switch (args[2][0])
2905 case 'r':
2906 type = WPT_READ;
2907 break;
2908 case 'w':
2909 type = WPT_WRITE;
2910 break;
2911 case 'a':
2912 type = WPT_ACCESS;
2913 break;
2914 default:
2915 LOG_ERROR("invalid watchpoint mode ('%c')", args[2][0]);
2916 return ERROR_COMMAND_SYNTAX_ERROR;
2918 // fall through
2919 case 2:
2920 COMMAND_PARSE_NUMBER(u32, args[1], length);
2921 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2922 break;
2924 default:
2925 command_print(cmd_ctx, "usage: wp [address length "
2926 "[(r|w|a) [value [mask]]]]");
2927 return ERROR_COMMAND_SYNTAX_ERROR;
2930 int retval = watchpoint_add(target, addr, length, type,
2931 data_value, data_mask);
2932 if (ERROR_OK != retval)
2933 LOG_ERROR("Failure setting watchpoints");
2935 return retval;
2938 static int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2940 if (argc != 1)
2941 return ERROR_COMMAND_SYNTAX_ERROR;
2943 uint32_t addr;
2944 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2946 target_t *target = get_current_target(cmd_ctx);
2947 watchpoint_remove(target, addr);
2949 return ERROR_OK;
2954 * Translate a virtual address to a physical address.
2956 * The low-level target implementation must have logged a detailed error
2957 * which is forwarded to telnet/GDB session.
2959 static int handle_virt2phys_command(command_context_t *cmd_ctx,
2960 char *cmd, char **args, int argc)
2962 if (argc != 1)
2963 return ERROR_COMMAND_SYNTAX_ERROR;
2965 uint32_t va;
2966 COMMAND_PARSE_NUMBER(u32, args[0], va);
2967 uint32_t pa;
2969 target_t *target = get_current_target(cmd_ctx);
2970 int retval = target->type->virt2phys(target, va, &pa);
2971 if (retval == ERROR_OK)
2972 command_print(cmd_ctx, "Physical address 0x%08" PRIx32 "", pa);
2974 return retval;
2977 static void writeData(FILE *f, const void *data, size_t len)
2979 size_t written = fwrite(data, 1, len, f);
2980 if (written != len)
2981 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
2984 static void writeLong(FILE *f, int l)
2986 int i;
2987 for (i = 0; i < 4; i++)
2989 char c = (l >> (i*8))&0xff;
2990 writeData(f, &c, 1);
2995 static void writeString(FILE *f, char *s)
2997 writeData(f, s, strlen(s));
3000 /* Dump a gmon.out histogram file. */
3001 static void writeGmon(uint32_t *samples, uint32_t sampleNum, char *filename)
3003 uint32_t i;
3004 FILE *f = fopen(filename, "w");
3005 if (f == NULL)
3006 return;
3007 writeString(f, "gmon");
3008 writeLong(f, 0x00000001); /* Version */
3009 writeLong(f, 0); /* padding */
3010 writeLong(f, 0); /* padding */
3011 writeLong(f, 0); /* padding */
3013 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
3014 writeData(f, &zero, 1);
3016 /* figure out bucket size */
3017 uint32_t min = samples[0];
3018 uint32_t max = samples[0];
3019 for (i = 0; i < sampleNum; i++)
3021 if (min > samples[i])
3023 min = samples[i];
3025 if (max < samples[i])
3027 max = samples[i];
3031 int addressSpace = (max-min + 1);
3033 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
3034 uint32_t length = addressSpace;
3035 if (length > maxBuckets)
3037 length = maxBuckets;
3039 int *buckets = malloc(sizeof(int)*length);
3040 if (buckets == NULL)
3042 fclose(f);
3043 return;
3045 memset(buckets, 0, sizeof(int)*length);
3046 for (i = 0; i < sampleNum;i++)
3048 uint32_t address = samples[i];
3049 long long a = address-min;
3050 long long b = length-1;
3051 long long c = addressSpace-1;
3052 int index = (a*b)/c; /* danger!!!! int32 overflows */
3053 buckets[index]++;
3056 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3057 writeLong(f, min); /* low_pc */
3058 writeLong(f, max); /* high_pc */
3059 writeLong(f, length); /* # of samples */
3060 writeLong(f, 64000000); /* 64MHz */
3061 writeString(f, "seconds");
3062 for (i = 0; i < (15-strlen("seconds")); i++)
3063 writeData(f, &zero, 1);
3064 writeString(f, "s");
3066 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3068 char *data = malloc(2*length);
3069 if (data != NULL)
3071 for (i = 0; i < length;i++)
3073 int val;
3074 val = buckets[i];
3075 if (val > 65535)
3077 val = 65535;
3079 data[i*2]=val&0xff;
3080 data[i*2 + 1]=(val >> 8)&0xff;
3082 free(buckets);
3083 writeData(f, data, length * 2);
3084 free(data);
3085 } else
3087 free(buckets);
3090 fclose(f);
3093 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
3094 static int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
3096 target_t *target = get_current_target(cmd_ctx);
3097 struct timeval timeout, now;
3099 gettimeofday(&timeout, NULL);
3100 if (argc != 2)
3102 return ERROR_COMMAND_SYNTAX_ERROR;
3104 unsigned offset;
3105 COMMAND_PARSE_NUMBER(uint, args[0], offset);
3107 timeval_add_time(&timeout, offset, 0);
3109 command_print(cmd_ctx, "Starting profiling. Halting and resuming the target as often as we can...");
3111 static const int maxSample = 10000;
3112 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3113 if (samples == NULL)
3114 return ERROR_OK;
3116 int numSamples = 0;
3117 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3118 reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1);
3120 for (;;)
3122 int retval;
3123 target_poll(target);
3124 if (target->state == TARGET_HALTED)
3126 uint32_t t=*((uint32_t *)reg->value);
3127 samples[numSamples++]=t;
3128 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3129 target_poll(target);
3130 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3131 } else if (target->state == TARGET_RUNNING)
3133 /* We want to quickly sample the PC. */
3134 if ((retval = target_halt(target)) != ERROR_OK)
3136 free(samples);
3137 return retval;
3139 } else
3141 command_print(cmd_ctx, "Target not halted or running");
3142 retval = ERROR_OK;
3143 break;
3145 if (retval != ERROR_OK)
3147 break;
3150 gettimeofday(&now, NULL);
3151 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3153 command_print(cmd_ctx, "Profiling completed. %d samples.", numSamples);
3154 if ((retval = target_poll(target)) != ERROR_OK)
3156 free(samples);
3157 return retval;
3159 if (target->state == TARGET_HALTED)
3161 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3163 if ((retval = target_poll(target)) != ERROR_OK)
3165 free(samples);
3166 return retval;
3168 writeGmon(samples, numSamples, args[1]);
3169 command_print(cmd_ctx, "Wrote %s", args[1]);
3170 break;
3173 free(samples);
3175 return ERROR_OK;
3178 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3180 char *namebuf;
3181 Jim_Obj *nameObjPtr, *valObjPtr;
3182 int result;
3184 namebuf = alloc_printf("%s(%d)", varname, idx);
3185 if (!namebuf)
3186 return JIM_ERR;
3188 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3189 valObjPtr = Jim_NewIntObj(interp, val);
3190 if (!nameObjPtr || !valObjPtr)
3192 free(namebuf);
3193 return JIM_ERR;
3196 Jim_IncrRefCount(nameObjPtr);
3197 Jim_IncrRefCount(valObjPtr);
3198 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3199 Jim_DecrRefCount(interp, nameObjPtr);
3200 Jim_DecrRefCount(interp, valObjPtr);
3201 free(namebuf);
3202 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3203 return result;
3206 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3208 command_context_t *context;
3209 target_t *target;
3211 context = Jim_GetAssocData(interp, "context");
3212 if (context == NULL)
3214 LOG_ERROR("mem2array: no command context");
3215 return JIM_ERR;
3217 target = get_current_target(context);
3218 if (target == NULL)
3220 LOG_ERROR("mem2array: no current target");
3221 return JIM_ERR;
3224 return target_mem2array(interp, target, argc-1, argv + 1);
3227 static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
3229 long l;
3230 uint32_t width;
3231 int len;
3232 uint32_t addr;
3233 uint32_t count;
3234 uint32_t v;
3235 const char *varname;
3236 uint8_t buffer[4096];
3237 int n, e, retval;
3238 uint32_t i;
3240 /* argv[1] = name of array to receive the data
3241 * argv[2] = desired width
3242 * argv[3] = memory address
3243 * argv[4] = count of times to read
3245 if (argc != 4) {
3246 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3247 return JIM_ERR;
3249 varname = Jim_GetString(argv[0], &len);
3250 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3252 e = Jim_GetLong(interp, argv[1], &l);
3253 width = l;
3254 if (e != JIM_OK) {
3255 return e;
3258 e = Jim_GetLong(interp, argv[2], &l);
3259 addr = l;
3260 if (e != JIM_OK) {
3261 return e;
3263 e = Jim_GetLong(interp, argv[3], &l);
3264 len = l;
3265 if (e != JIM_OK) {
3266 return e;
3268 switch (width) {
3269 case 8:
3270 width = 1;
3271 break;
3272 case 16:
3273 width = 2;
3274 break;
3275 case 32:
3276 width = 4;
3277 break;
3278 default:
3279 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3280 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3281 return JIM_ERR;
3283 if (len == 0) {
3284 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3285 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3286 return JIM_ERR;
3288 if ((addr + (len * width)) < addr) {
3289 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3290 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3291 return JIM_ERR;
3293 /* absurd transfer size? */
3294 if (len > 65536) {
3295 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3296 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3297 return JIM_ERR;
3300 if ((width == 1) ||
3301 ((width == 2) && ((addr & 1) == 0)) ||
3302 ((width == 4) && ((addr & 3) == 0))) {
3303 /* all is well */
3304 } else {
3305 char buf[100];
3306 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3307 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3308 addr,
3309 width);
3310 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3311 return JIM_ERR;
3314 /* Transfer loop */
3316 /* index counter */
3317 n = 0;
3318 /* assume ok */
3319 e = JIM_OK;
3320 while (len) {
3321 /* Slurp... in buffer size chunks */
3323 count = len; /* in objects.. */
3324 if (count > (sizeof(buffer)/width)) {
3325 count = (sizeof(buffer)/width);
3328 retval = target_read_memory(target, addr, width, count, buffer);
3329 if (retval != ERROR_OK) {
3330 /* BOO !*/
3331 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3332 (unsigned int)addr,
3333 (int)width,
3334 (int)count);
3335 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3336 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3337 e = JIM_ERR;
3338 len = 0;
3339 } else {
3340 v = 0; /* shut up gcc */
3341 for (i = 0 ;i < count ;i++, n++) {
3342 switch (width) {
3343 case 4:
3344 v = target_buffer_get_u32(target, &buffer[i*width]);
3345 break;
3346 case 2:
3347 v = target_buffer_get_u16(target, &buffer[i*width]);
3348 break;
3349 case 1:
3350 v = buffer[i] & 0x0ff;
3351 break;
3353 new_int_array_element(interp, varname, n, v);
3355 len -= count;
3359 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3361 return JIM_OK;
3364 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3366 char *namebuf;
3367 Jim_Obj *nameObjPtr, *valObjPtr;
3368 int result;
3369 long l;
3371 namebuf = alloc_printf("%s(%d)", varname, idx);
3372 if (!namebuf)
3373 return JIM_ERR;
3375 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3376 if (!nameObjPtr)
3378 free(namebuf);
3379 return JIM_ERR;
3382 Jim_IncrRefCount(nameObjPtr);
3383 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3384 Jim_DecrRefCount(interp, nameObjPtr);
3385 free(namebuf);
3386 if (valObjPtr == NULL)
3387 return JIM_ERR;
3389 result = Jim_GetLong(interp, valObjPtr, &l);
3390 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3391 *val = l;
3392 return result;
3395 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3397 command_context_t *context;
3398 target_t *target;
3400 context = Jim_GetAssocData(interp, "context");
3401 if (context == NULL) {
3402 LOG_ERROR("array2mem: no command context");
3403 return JIM_ERR;
3405 target = get_current_target(context);
3406 if (target == NULL) {
3407 LOG_ERROR("array2mem: no current target");
3408 return JIM_ERR;
3411 return target_array2mem(interp,target, argc-1, argv + 1);
3413 static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
3415 long l;
3416 uint32_t width;
3417 int len;
3418 uint32_t addr;
3419 uint32_t count;
3420 uint32_t v;
3421 const char *varname;
3422 uint8_t buffer[4096];
3423 int n, e, retval;
3424 uint32_t i;
3426 /* argv[1] = name of array to get the data
3427 * argv[2] = desired width
3428 * argv[3] = memory address
3429 * argv[4] = count to write
3431 if (argc != 4) {
3432 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3433 return JIM_ERR;
3435 varname = Jim_GetString(argv[0], &len);
3436 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3438 e = Jim_GetLong(interp, argv[1], &l);
3439 width = l;
3440 if (e != JIM_OK) {
3441 return e;
3444 e = Jim_GetLong(interp, argv[2], &l);
3445 addr = l;
3446 if (e != JIM_OK) {
3447 return e;
3449 e = Jim_GetLong(interp, argv[3], &l);
3450 len = l;
3451 if (e != JIM_OK) {
3452 return e;
3454 switch (width) {
3455 case 8:
3456 width = 1;
3457 break;
3458 case 16:
3459 width = 2;
3460 break;
3461 case 32:
3462 width = 4;
3463 break;
3464 default:
3465 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3466 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3467 return JIM_ERR;
3469 if (len == 0) {
3470 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3471 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3472 return JIM_ERR;
3474 if ((addr + (len * width)) < addr) {
3475 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3476 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3477 return JIM_ERR;
3479 /* absurd transfer size? */
3480 if (len > 65536) {
3481 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3482 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3483 return JIM_ERR;
3486 if ((width == 1) ||
3487 ((width == 2) && ((addr & 1) == 0)) ||
3488 ((width == 4) && ((addr & 3) == 0))) {
3489 /* all is well */
3490 } else {
3491 char buf[100];
3492 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3493 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3494 (unsigned int)addr,
3495 (int)width);
3496 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3497 return JIM_ERR;
3500 /* Transfer loop */
3502 /* index counter */
3503 n = 0;
3504 /* assume ok */
3505 e = JIM_OK;
3506 while (len) {
3507 /* Slurp... in buffer size chunks */
3509 count = len; /* in objects.. */
3510 if (count > (sizeof(buffer)/width)) {
3511 count = (sizeof(buffer)/width);
3514 v = 0; /* shut up gcc */
3515 for (i = 0 ;i < count ;i++, n++) {
3516 get_int_array_element(interp, varname, n, &v);
3517 switch (width) {
3518 case 4:
3519 target_buffer_set_u32(target, &buffer[i*width], v);
3520 break;
3521 case 2:
3522 target_buffer_set_u16(target, &buffer[i*width], v);
3523 break;
3524 case 1:
3525 buffer[i] = v & 0x0ff;
3526 break;
3529 len -= count;
3531 retval = target_write_memory(target, addr, width, count, buffer);
3532 if (retval != ERROR_OK) {
3533 /* BOO !*/
3534 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3535 (unsigned int)addr,
3536 (int)width,
3537 (int)count);
3538 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3539 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3540 e = JIM_ERR;
3541 len = 0;
3545 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3547 return JIM_OK;
3550 void target_all_handle_event(enum target_event e)
3552 target_t *target;
3554 LOG_DEBUG("**all*targets: event: %d, %s",
3555 (int)e,
3556 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3558 target = all_targets;
3559 while (target) {
3560 target_handle_event(target, e);
3561 target = target->next;
3566 /* FIX? should we propagate errors here rather than printing them
3567 * and continuing?
3569 void target_handle_event(target_t *target, enum target_event e)
3571 target_event_action_t *teap;
3573 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3574 if (teap->event == e) {
3575 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3576 target->target_number,
3577 target->cmd_name,
3578 target_get_name(target),
3580 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3581 Jim_GetString(teap->body, NULL));
3582 if (Jim_EvalObj(interp, teap->body) != JIM_OK)
3584 Jim_PrintErrorMessage(interp);
3590 enum target_cfg_param {
3591 TCFG_TYPE,
3592 TCFG_EVENT,
3593 TCFG_WORK_AREA_VIRT,
3594 TCFG_WORK_AREA_PHYS,
3595 TCFG_WORK_AREA_SIZE,
3596 TCFG_WORK_AREA_BACKUP,
3597 TCFG_ENDIAN,
3598 TCFG_VARIANT,
3599 TCFG_CHAIN_POSITION,
3602 static Jim_Nvp nvp_config_opts[] = {
3603 { .name = "-type", .value = TCFG_TYPE },
3604 { .name = "-event", .value = TCFG_EVENT },
3605 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3606 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3607 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3608 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3609 { .name = "-endian" , .value = TCFG_ENDIAN },
3610 { .name = "-variant", .value = TCFG_VARIANT },
3611 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3613 { .name = NULL, .value = -1 }
3616 static int target_configure(Jim_GetOptInfo *goi, target_t *target)
3618 Jim_Nvp *n;
3619 Jim_Obj *o;
3620 jim_wide w;
3621 char *cp;
3622 int e;
3624 /* parse config or cget options ... */
3625 while (goi->argc > 0) {
3626 Jim_SetEmptyResult(goi->interp);
3627 /* Jim_GetOpt_Debug(goi); */
3629 if (target->type->target_jim_configure) {
3630 /* target defines a configure function */
3631 /* target gets first dibs on parameters */
3632 e = (*(target->type->target_jim_configure))(target, goi);
3633 if (e == JIM_OK) {
3634 /* more? */
3635 continue;
3637 if (e == JIM_ERR) {
3638 /* An error */
3639 return e;
3641 /* otherwise we 'continue' below */
3643 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3644 if (e != JIM_OK) {
3645 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3646 return e;
3648 switch (n->value) {
3649 case TCFG_TYPE:
3650 /* not setable */
3651 if (goi->isconfigure) {
3652 Jim_SetResult_sprintf(goi->interp, "not setable: %s", n->name);
3653 return JIM_ERR;
3654 } else {
3655 no_params:
3656 if (goi->argc != 0) {
3657 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "NO PARAMS");
3658 return JIM_ERR;
3661 Jim_SetResultString(goi->interp, target_get_name(target), -1);
3662 /* loop for more */
3663 break;
3664 case TCFG_EVENT:
3665 if (goi->argc == 0) {
3666 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3667 return JIM_ERR;
3670 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3671 if (e != JIM_OK) {
3672 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3673 return e;
3676 if (goi->isconfigure) {
3677 if (goi->argc != 1) {
3678 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3679 return JIM_ERR;
3681 } else {
3682 if (goi->argc != 0) {
3683 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3684 return JIM_ERR;
3689 target_event_action_t *teap;
3691 teap = target->event_action;
3692 /* replace existing? */
3693 while (teap) {
3694 if (teap->event == (enum target_event)n->value) {
3695 break;
3697 teap = teap->next;
3700 if (goi->isconfigure) {
3701 bool replace = true;
3702 if (teap == NULL) {
3703 /* create new */
3704 teap = calloc(1, sizeof(*teap));
3705 replace = false;
3707 teap->event = n->value;
3708 Jim_GetOpt_Obj(goi, &o);
3709 if (teap->body) {
3710 Jim_DecrRefCount(interp, teap->body);
3712 teap->body = Jim_DuplicateObj(goi->interp, o);
3714 * FIXME:
3715 * Tcl/TK - "tk events" have a nice feature.
3716 * See the "BIND" command.
3717 * We should support that here.
3718 * You can specify %X and %Y in the event code.
3719 * The idea is: %T - target name.
3720 * The idea is: %N - target number
3721 * The idea is: %E - event name.
3723 Jim_IncrRefCount(teap->body);
3725 if (!replace)
3727 /* add to head of event list */
3728 teap->next = target->event_action;
3729 target->event_action = teap;
3731 Jim_SetEmptyResult(goi->interp);
3732 } else {
3733 /* get */
3734 if (teap == NULL) {
3735 Jim_SetEmptyResult(goi->interp);
3736 } else {
3737 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3741 /* loop for more */
3742 break;
3744 case TCFG_WORK_AREA_VIRT:
3745 if (goi->isconfigure) {
3746 target_free_all_working_areas(target);
3747 e = Jim_GetOpt_Wide(goi, &w);
3748 if (e != JIM_OK) {
3749 return e;
3751 target->working_area_virt = w;
3752 target->working_area_virt_spec = true;
3753 } else {
3754 if (goi->argc != 0) {
3755 goto no_params;
3758 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3759 /* loop for more */
3760 break;
3762 case TCFG_WORK_AREA_PHYS:
3763 if (goi->isconfigure) {
3764 target_free_all_working_areas(target);
3765 e = Jim_GetOpt_Wide(goi, &w);
3766 if (e != JIM_OK) {
3767 return e;
3769 target->working_area_phys = w;
3770 target->working_area_phys_spec = true;
3771 } else {
3772 if (goi->argc != 0) {
3773 goto no_params;
3776 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3777 /* loop for more */
3778 break;
3780 case TCFG_WORK_AREA_SIZE:
3781 if (goi->isconfigure) {
3782 target_free_all_working_areas(target);
3783 e = Jim_GetOpt_Wide(goi, &w);
3784 if (e != JIM_OK) {
3785 return e;
3787 target->working_area_size = w;
3788 } else {
3789 if (goi->argc != 0) {
3790 goto no_params;
3793 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3794 /* loop for more */
3795 break;
3797 case TCFG_WORK_AREA_BACKUP:
3798 if (goi->isconfigure) {
3799 target_free_all_working_areas(target);
3800 e = Jim_GetOpt_Wide(goi, &w);
3801 if (e != JIM_OK) {
3802 return e;
3804 /* make this exactly 1 or 0 */
3805 target->backup_working_area = (!!w);
3806 } else {
3807 if (goi->argc != 0) {
3808 goto no_params;
3811 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3812 /* loop for more e*/
3813 break;
3815 case TCFG_ENDIAN:
3816 if (goi->isconfigure) {
3817 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3818 if (e != JIM_OK) {
3819 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3820 return e;
3822 target->endianness = n->value;
3823 } else {
3824 if (goi->argc != 0) {
3825 goto no_params;
3828 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3829 if (n->name == NULL) {
3830 target->endianness = TARGET_LITTLE_ENDIAN;
3831 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3833 Jim_SetResultString(goi->interp, n->name, -1);
3834 /* loop for more */
3835 break;
3837 case TCFG_VARIANT:
3838 if (goi->isconfigure) {
3839 if (goi->argc < 1) {
3840 Jim_SetResult_sprintf(goi->interp,
3841 "%s ?STRING?",
3842 n->name);
3843 return JIM_ERR;
3845 if (target->variant) {
3846 free((void *)(target->variant));
3848 e = Jim_GetOpt_String(goi, &cp, NULL);
3849 target->variant = strdup(cp);
3850 } else {
3851 if (goi->argc != 0) {
3852 goto no_params;
3855 Jim_SetResultString(goi->interp, target->variant,-1);
3856 /* loop for more */
3857 break;
3858 case TCFG_CHAIN_POSITION:
3859 if (goi->isconfigure) {
3860 Jim_Obj *o;
3861 jtag_tap_t *tap;
3862 target_free_all_working_areas(target);
3863 e = Jim_GetOpt_Obj(goi, &o);
3864 if (e != JIM_OK) {
3865 return e;
3867 tap = jtag_tap_by_jim_obj(goi->interp, o);
3868 if (tap == NULL) {
3869 return JIM_ERR;
3871 /* make this exactly 1 or 0 */
3872 target->tap = tap;
3873 } else {
3874 if (goi->argc != 0) {
3875 goto no_params;
3878 Jim_SetResultString(interp, target->tap->dotted_name, -1);
3879 /* loop for more e*/
3880 break;
3882 } /* while (goi->argc) */
3885 /* done - we return */
3886 return JIM_OK;
3889 /** this is the 'tcl' handler for the target specific command */
3890 static int tcl_target_func(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3892 Jim_GetOptInfo goi;
3893 jim_wide a,b,c;
3894 int x,y,z;
3895 uint8_t target_buf[32];
3896 Jim_Nvp *n;
3897 target_t *target;
3898 struct command_context_s *cmd_ctx;
3899 int e;
3901 enum {
3902 TS_CMD_CONFIGURE,
3903 TS_CMD_CGET,
3905 TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
3906 TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
3907 TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
3908 TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
3909 TS_CMD_EXAMINE,
3910 TS_CMD_POLL,
3911 TS_CMD_RESET,
3912 TS_CMD_HALT,
3913 TS_CMD_WAITSTATE,
3914 TS_CMD_EVENTLIST,
3915 TS_CMD_CURSTATE,
3916 TS_CMD_INVOKE_EVENT,
3919 static const Jim_Nvp target_options[] = {
3920 { .name = "configure", .value = TS_CMD_CONFIGURE },
3921 { .name = "cget", .value = TS_CMD_CGET },
3922 { .name = "mww", .value = TS_CMD_MWW },
3923 { .name = "mwh", .value = TS_CMD_MWH },
3924 { .name = "mwb", .value = TS_CMD_MWB },
3925 { .name = "mdw", .value = TS_CMD_MDW },
3926 { .name = "mdh", .value = TS_CMD_MDH },
3927 { .name = "mdb", .value = TS_CMD_MDB },
3928 { .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
3929 { .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
3930 { .name = "eventlist", .value = TS_CMD_EVENTLIST },
3931 { .name = "curstate", .value = TS_CMD_CURSTATE },
3933 { .name = "arp_examine", .value = TS_CMD_EXAMINE },
3934 { .name = "arp_poll", .value = TS_CMD_POLL },
3935 { .name = "arp_reset", .value = TS_CMD_RESET },
3936 { .name = "arp_halt", .value = TS_CMD_HALT },
3937 { .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
3938 { .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
3940 { .name = NULL, .value = -1 },
3943 /* go past the "command" */
3944 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
3946 target = Jim_CmdPrivData(goi.interp);
3947 cmd_ctx = Jim_GetAssocData(goi.interp, "context");
3949 /* commands here are in an NVP table */
3950 e = Jim_GetOpt_Nvp(&goi, target_options, &n);
3951 if (e != JIM_OK) {
3952 Jim_GetOpt_NvpUnknown(&goi, target_options, 0);
3953 return e;
3955 /* Assume blank result */
3956 Jim_SetEmptyResult(goi.interp);
3958 switch (n->value) {
3959 case TS_CMD_CONFIGURE:
3960 if (goi.argc < 2) {
3961 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
3962 return JIM_ERR;
3964 goi.isconfigure = 1;
3965 return target_configure(&goi, target);
3966 case TS_CMD_CGET:
3967 // some things take params
3968 if (goi.argc < 1) {
3969 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "missing: ?-option?");
3970 return JIM_ERR;
3972 goi.isconfigure = 0;
3973 return target_configure(&goi, target);
3974 break;
3975 case TS_CMD_MWW:
3976 case TS_CMD_MWH:
3977 case TS_CMD_MWB:
3978 /* argv[0] = cmd
3979 * argv[1] = address
3980 * argv[2] = data
3981 * argv[3] = optional count.
3984 if ((goi.argc == 2) || (goi.argc == 3)) {
3985 /* all is well */
3986 } else {
3987 mwx_error:
3988 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR DATA [COUNT]", n->name);
3989 return JIM_ERR;
3992 e = Jim_GetOpt_Wide(&goi, &a);
3993 if (e != JIM_OK) {
3994 goto mwx_error;
3997 e = Jim_GetOpt_Wide(&goi, &b);
3998 if (e != JIM_OK) {
3999 goto mwx_error;
4001 if (goi.argc == 3) {
4002 e = Jim_GetOpt_Wide(&goi, &c);
4003 if (e != JIM_OK) {
4004 goto mwx_error;
4006 } else {
4007 c = 1;
4010 switch (n->value) {
4011 case TS_CMD_MWW:
4012 target_buffer_set_u32(target, target_buf, b);
4013 b = 4;
4014 break;
4015 case TS_CMD_MWH:
4016 target_buffer_set_u16(target, target_buf, b);
4017 b = 2;
4018 break;
4019 case TS_CMD_MWB:
4020 target_buffer_set_u8(target, target_buf, b);
4021 b = 1;
4022 break;
4024 for (x = 0 ; x < c ; x++) {
4025 e = target_write_memory(target, a, b, 1, target_buf);
4026 if (e != ERROR_OK) {
4027 Jim_SetResult_sprintf(interp, "Error writing @ 0x%08x: %d\n", (int)(a), e);
4028 return JIM_ERR;
4030 /* b = width */
4031 a = a + b;
4033 return JIM_OK;
4034 break;
4036 /* display */
4037 case TS_CMD_MDW:
4038 case TS_CMD_MDH:
4039 case TS_CMD_MDB:
4040 /* argv[0] = command
4041 * argv[1] = address
4042 * argv[2] = optional count
4044 if ((goi.argc == 2) || (goi.argc == 3)) {
4045 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR [COUNT]", n->name);
4046 return JIM_ERR;
4048 e = Jim_GetOpt_Wide(&goi, &a);
4049 if (e != JIM_OK) {
4050 return JIM_ERR;
4052 if (goi.argc) {
4053 e = Jim_GetOpt_Wide(&goi, &c);
4054 if (e != JIM_OK) {
4055 return JIM_ERR;
4057 } else {
4058 c = 1;
4060 b = 1; /* shut up gcc */
4061 switch (n->value) {
4062 case TS_CMD_MDW:
4063 b = 4;
4064 break;
4065 case TS_CMD_MDH:
4066 b = 2;
4067 break;
4068 case TS_CMD_MDB:
4069 b = 1;
4070 break;
4073 /* convert to "bytes" */
4074 c = c * b;
4075 /* count is now in 'BYTES' */
4076 while (c > 0) {
4077 y = c;
4078 if (y > 16) {
4079 y = 16;
4081 e = target_read_memory(target, a, b, y / b, target_buf);
4082 if (e != ERROR_OK) {
4083 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
4084 return JIM_ERR;
4087 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
4088 switch (b) {
4089 case 4:
4090 for (x = 0 ; (x < 16) && (x < y) ; x += 4) {
4091 z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
4092 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
4094 for (; (x < 16) ; x += 4) {
4095 Jim_fprintf(interp, interp->cookie_stdout, " ");
4097 break;
4098 case 2:
4099 for (x = 0 ; (x < 16) && (x < y) ; x += 2) {
4100 z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
4101 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
4103 for (; (x < 16) ; x += 2) {
4104 Jim_fprintf(interp, interp->cookie_stdout, " ");
4106 break;
4107 case 1:
4108 default:
4109 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4110 z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
4111 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
4113 for (; (x < 16) ; x += 1) {
4114 Jim_fprintf(interp, interp->cookie_stdout, " ");
4116 break;
4118 /* ascii-ify the bytes */
4119 for (x = 0 ; x < y ; x++) {
4120 if ((target_buf[x] >= 0x20) &&
4121 (target_buf[x] <= 0x7e)) {
4122 /* good */
4123 } else {
4124 /* smack it */
4125 target_buf[x] = '.';
4128 /* space pad */
4129 while (x < 16) {
4130 target_buf[x] = ' ';
4131 x++;
4133 /* terminate */
4134 target_buf[16] = 0;
4135 /* print - with a newline */
4136 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
4137 /* NEXT... */
4138 c -= 16;
4139 a += 16;
4141 return JIM_OK;
4142 case TS_CMD_MEM2ARRAY:
4143 return target_mem2array(goi.interp, target, goi.argc, goi.argv);
4144 break;
4145 case TS_CMD_ARRAY2MEM:
4146 return target_array2mem(goi.interp, target, goi.argc, goi.argv);
4147 break;
4148 case TS_CMD_EXAMINE:
4149 if (goi.argc) {
4150 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4151 return JIM_ERR;
4153 if (!target->tap->enabled)
4154 goto err_tap_disabled;
4155 e = target->type->examine(target);
4156 if (e != ERROR_OK) {
4157 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
4158 return JIM_ERR;
4160 return JIM_OK;
4161 case TS_CMD_POLL:
4162 if (goi.argc) {
4163 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4164 return JIM_ERR;
4166 if (!target->tap->enabled)
4167 goto err_tap_disabled;
4168 if (!(target_was_examined(target))) {
4169 e = ERROR_TARGET_NOT_EXAMINED;
4170 } else {
4171 e = target->type->poll(target);
4173 if (e != ERROR_OK) {
4174 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4175 return JIM_ERR;
4176 } else {
4177 return JIM_OK;
4179 break;
4180 case TS_CMD_RESET:
4181 if (goi.argc != 2) {
4182 Jim_WrongNumArgs(interp, 2, argv,
4183 "([tT]|[fF]|assert|deassert) BOOL");
4184 return JIM_ERR;
4186 e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4187 if (e != JIM_OK) {
4188 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4189 return e;
4191 /* the halt or not param */
4192 e = Jim_GetOpt_Wide(&goi, &a);
4193 if (e != JIM_OK) {
4194 return e;
4196 if (!target->tap->enabled)
4197 goto err_tap_disabled;
4198 if (!target->type->assert_reset
4199 || !target->type->deassert_reset) {
4200 Jim_SetResult_sprintf(interp,
4201 "No target-specific reset for %s",
4202 target->cmd_name);
4203 return JIM_ERR;
4205 /* determine if we should halt or not. */
4206 target->reset_halt = !!a;
4207 /* When this happens - all workareas are invalid. */
4208 target_free_all_working_areas_restore(target, 0);
4210 /* do the assert */
4211 if (n->value == NVP_ASSERT) {
4212 e = target->type->assert_reset(target);
4213 } else {
4214 e = target->type->deassert_reset(target);
4216 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4217 case TS_CMD_HALT:
4218 if (goi.argc) {
4219 Jim_WrongNumArgs(goi.interp, 0, argv, "halt [no parameters]");
4220 return JIM_ERR;
4222 if (!target->tap->enabled)
4223 goto err_tap_disabled;
4224 e = target->type->halt(target);
4225 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4226 case TS_CMD_WAITSTATE:
4227 /* params: <name> statename timeoutmsecs */
4228 if (goi.argc != 2) {
4229 Jim_SetResult_sprintf(goi.interp, "%s STATENAME TIMEOUTMSECS", n->name);
4230 return JIM_ERR;
4232 e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4233 if (e != JIM_OK) {
4234 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4235 return e;
4237 e = Jim_GetOpt_Wide(&goi, &a);
4238 if (e != JIM_OK) {
4239 return e;
4241 if (!target->tap->enabled)
4242 goto err_tap_disabled;
4243 e = target_wait_state(target, n->value, a);
4244 if (e != ERROR_OK) {
4245 Jim_SetResult_sprintf(goi.interp,
4246 "target: %s wait %s fails (%d) %s",
4247 target->cmd_name,
4248 n->name,
4249 e, target_strerror_safe(e));
4250 return JIM_ERR;
4251 } else {
4252 return JIM_OK;
4254 case TS_CMD_EVENTLIST:
4255 /* List for human, Events defined for this target.
4256 * scripts/programs should use 'name cget -event NAME'
4259 target_event_action_t *teap;
4260 teap = target->event_action;
4261 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4262 target->target_number,
4263 target->cmd_name);
4264 command_print(cmd_ctx, "%-25s | Body", "Event");
4265 command_print(cmd_ctx, "------------------------- | ----------------------------------------");
4266 while (teap) {
4267 command_print(cmd_ctx,
4268 "%-25s | %s",
4269 Jim_Nvp_value2name_simple(nvp_target_event, teap->event)->name,
4270 Jim_GetString(teap->body, NULL));
4271 teap = teap->next;
4273 command_print(cmd_ctx, "***END***");
4274 return JIM_OK;
4276 case TS_CMD_CURSTATE:
4277 if (goi.argc != 0) {
4278 Jim_WrongNumArgs(goi.interp, 0, argv, "[no parameters]");
4279 return JIM_ERR;
4281 Jim_SetResultString(goi.interp,
4282 target_state_name( target ),
4283 -1);
4284 return JIM_OK;
4285 case TS_CMD_INVOKE_EVENT:
4286 if (goi.argc != 1) {
4287 Jim_SetResult_sprintf(goi.interp, "%s ?EVENTNAME?",n->name);
4288 return JIM_ERR;
4290 e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4291 if (e != JIM_OK) {
4292 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4293 return e;
4295 target_handle_event(target, n->value);
4296 return JIM_OK;
4298 return JIM_ERR;
4300 err_tap_disabled:
4301 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4302 return JIM_ERR;
4305 static int target_create(Jim_GetOptInfo *goi)
4307 Jim_Obj *new_cmd;
4308 Jim_Cmd *cmd;
4309 const char *cp;
4310 char *cp2;
4311 int e;
4312 int x;
4313 target_t *target;
4314 struct command_context_s *cmd_ctx;
4316 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4317 if (goi->argc < 3) {
4318 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4319 return JIM_ERR;
4322 /* COMMAND */
4323 Jim_GetOpt_Obj(goi, &new_cmd);
4324 /* does this command exist? */
4325 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4326 if (cmd) {
4327 cp = Jim_GetString(new_cmd, NULL);
4328 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4329 return JIM_ERR;
4332 /* TYPE */
4333 e = Jim_GetOpt_String(goi, &cp2, NULL);
4334 cp = cp2;
4335 /* now does target type exist */
4336 for (x = 0 ; target_types[x] ; x++) {
4337 if (0 == strcmp(cp, target_types[x]->name)) {
4338 /* found */
4339 break;
4342 if (target_types[x] == NULL) {
4343 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4344 for (x = 0 ; target_types[x] ; x++) {
4345 if (target_types[x + 1]) {
4346 Jim_AppendStrings(goi->interp,
4347 Jim_GetResult(goi->interp),
4348 target_types[x]->name,
4349 ", ", NULL);
4350 } else {
4351 Jim_AppendStrings(goi->interp,
4352 Jim_GetResult(goi->interp),
4353 " or ",
4354 target_types[x]->name,NULL);
4357 return JIM_ERR;
4360 /* Create it */
4361 target = calloc(1,sizeof(target_t));
4362 /* set target number */
4363 target->target_number = new_target_number();
4365 /* allocate memory for each unique target type */
4366 target->type = (target_type_t*)calloc(1,sizeof(target_type_t));
4368 memcpy(target->type, target_types[x], sizeof(target_type_t));
4370 /* will be set by "-endian" */
4371 target->endianness = TARGET_ENDIAN_UNKNOWN;
4373 target->working_area = 0x0;
4374 target->working_area_size = 0x0;
4375 target->working_areas = NULL;
4376 target->backup_working_area = 0;
4378 target->state = TARGET_UNKNOWN;
4379 target->debug_reason = DBG_REASON_UNDEFINED;
4380 target->reg_cache = NULL;
4381 target->breakpoints = NULL;
4382 target->watchpoints = NULL;
4383 target->next = NULL;
4384 target->arch_info = NULL;
4386 target->display = 1;
4388 target->halt_issued = false;
4390 /* initialize trace information */
4391 target->trace_info = malloc(sizeof(trace_t));
4392 target->trace_info->num_trace_points = 0;
4393 target->trace_info->trace_points_size = 0;
4394 target->trace_info->trace_points = NULL;
4395 target->trace_info->trace_history_size = 0;
4396 target->trace_info->trace_history = NULL;
4397 target->trace_info->trace_history_pos = 0;
4398 target->trace_info->trace_history_overflowed = 0;
4400 target->dbgmsg = NULL;
4401 target->dbg_msg_enabled = 0;
4403 target->endianness = TARGET_ENDIAN_UNKNOWN;
4405 /* Do the rest as "configure" options */
4406 goi->isconfigure = 1;
4407 e = target_configure(goi, target);
4409 if (target->tap == NULL)
4411 Jim_SetResultString(interp, "-chain-position required when creating target", -1);
4412 e = JIM_ERR;
4415 if (e != JIM_OK) {
4416 free(target->type);
4417 free(target);
4418 return e;
4421 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4422 /* default endian to little if not specified */
4423 target->endianness = TARGET_LITTLE_ENDIAN;
4426 /* incase variant is not set */
4427 if (!target->variant)
4428 target->variant = strdup("");
4430 /* create the target specific commands */
4431 if (target->type->register_commands) {
4432 (*(target->type->register_commands))(cmd_ctx);
4434 if (target->type->target_create) {
4435 (*(target->type->target_create))(target, goi->interp);
4438 /* append to end of list */
4440 target_t **tpp;
4441 tpp = &(all_targets);
4442 while (*tpp) {
4443 tpp = &((*tpp)->next);
4445 *tpp = target;
4448 cp = Jim_GetString(new_cmd, NULL);
4449 target->cmd_name = strdup(cp);
4451 /* now - create the new target name command */
4452 e = Jim_CreateCommand(goi->interp,
4453 /* name */
4455 tcl_target_func, /* C function */
4456 target, /* private data */
4457 NULL); /* no del proc */
4459 return e;
4462 static int jim_target(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4464 int x,r,e;
4465 jim_wide w;
4466 struct command_context_s *cmd_ctx;
4467 target_t *target;
4468 Jim_GetOptInfo goi;
4469 enum tcmd {
4470 /* TG = target generic */
4471 TG_CMD_CREATE,
4472 TG_CMD_TYPES,
4473 TG_CMD_NAMES,
4474 TG_CMD_CURRENT,
4475 TG_CMD_NUMBER,
4476 TG_CMD_COUNT,
4478 const char *target_cmds[] = {
4479 "create", "types", "names", "current", "number",
4480 "count",
4481 NULL /* terminate */
4484 LOG_DEBUG("Target command params:");
4485 LOG_DEBUG("%s", Jim_Debug_ArgvString(interp, argc, argv));
4487 cmd_ctx = Jim_GetAssocData(interp, "context");
4489 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
4491 if (goi.argc == 0) {
4492 Jim_WrongNumArgs(interp, 1, argv, "missing: command ...");
4493 return JIM_ERR;
4496 /* Jim_GetOpt_Debug(&goi); */
4497 r = Jim_GetOpt_Enum(&goi, target_cmds, &x);
4498 if (r != JIM_OK) {
4499 return r;
4502 switch (x) {
4503 default:
4504 Jim_Panic(goi.interp,"Why am I here?");
4505 return JIM_ERR;
4506 case TG_CMD_CURRENT:
4507 if (goi.argc != 0) {
4508 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4509 return JIM_ERR;
4511 Jim_SetResultString(goi.interp, get_current_target(cmd_ctx)->cmd_name, -1);
4512 return JIM_OK;
4513 case TG_CMD_TYPES:
4514 if (goi.argc != 0) {
4515 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4516 return JIM_ERR;
4518 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4519 for (x = 0 ; target_types[x] ; x++) {
4520 Jim_ListAppendElement(goi.interp,
4521 Jim_GetResult(goi.interp),
4522 Jim_NewStringObj(goi.interp, target_types[x]->name, -1));
4524 return JIM_OK;
4525 case TG_CMD_NAMES:
4526 if (goi.argc != 0) {
4527 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4528 return JIM_ERR;
4530 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4531 target = all_targets;
4532 while (target) {
4533 Jim_ListAppendElement(goi.interp,
4534 Jim_GetResult(goi.interp),
4535 Jim_NewStringObj(goi.interp, target->cmd_name, -1));
4536 target = target->next;
4538 return JIM_OK;
4539 case TG_CMD_CREATE:
4540 if (goi.argc < 3) {
4541 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "?name ... config options ...");
4542 return JIM_ERR;
4544 return target_create(&goi);
4545 break;
4546 case TG_CMD_NUMBER:
4547 /* It's OK to remove this mechanism sometime after August 2010 or so */
4548 LOG_WARNING("don't use numbers as target identifiers; use names");
4549 if (goi.argc != 1) {
4550 Jim_SetResult_sprintf(goi.interp, "expected: target number ?NUMBER?");
4551 return JIM_ERR;
4553 e = Jim_GetOpt_Wide(&goi, &w);
4554 if (e != JIM_OK) {
4555 return JIM_ERR;
4557 for (x = 0, target = all_targets; target; target = target->next, x++) {
4558 if (target->target_number == w)
4559 break;
4561 if (target == NULL) {
4562 Jim_SetResult_sprintf(goi.interp,
4563 "Target: number %d does not exist", (int)(w));
4564 return JIM_ERR;
4566 Jim_SetResultString(goi.interp, target->cmd_name, -1);
4567 return JIM_OK;
4568 case TG_CMD_COUNT:
4569 if (goi.argc != 0) {
4570 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "<no parameters>");
4571 return JIM_ERR;
4573 for (x = 0, target = all_targets; target; target = target->next, x++)
4574 continue;
4575 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, x));
4576 return JIM_OK;
4579 return JIM_ERR;
4583 struct FastLoad
4585 uint32_t address;
4586 uint8_t *data;
4587 int length;
4591 static int fastload_num;
4592 static struct FastLoad *fastload;
4594 static void free_fastload(void)
4596 if (fastload != NULL)
4598 int i;
4599 for (i = 0; i < fastload_num; i++)
4601 if (fastload[i].data)
4602 free(fastload[i].data);
4604 free(fastload);
4605 fastload = NULL;
4612 static int handle_fast_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4614 uint8_t *buffer;
4615 uint32_t buf_cnt;
4616 uint32_t image_size;
4617 uint32_t min_address = 0;
4618 uint32_t max_address = 0xffffffff;
4619 int i;
4621 image_t image;
4623 int retval = parse_load_image_command_args(cmd_ctx, args, argc,
4624 &image, &min_address, &max_address);
4625 if (ERROR_OK != retval)
4626 return retval;
4628 struct duration bench;
4629 duration_start(&bench);
4631 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
4633 return ERROR_OK;
4636 image_size = 0x0;
4637 retval = ERROR_OK;
4638 fastload_num = image.num_sections;
4639 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4640 if (fastload == NULL)
4642 image_close(&image);
4643 return ERROR_FAIL;
4645 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4646 for (i = 0; i < image.num_sections; i++)
4648 buffer = malloc(image.sections[i].size);
4649 if (buffer == NULL)
4651 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)",
4652 (int)(image.sections[i].size));
4653 break;
4656 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4658 free(buffer);
4659 break;
4662 uint32_t offset = 0;
4663 uint32_t length = buf_cnt;
4666 /* DANGER!!! beware of unsigned comparision here!!! */
4668 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4669 (image.sections[i].base_address < max_address))
4671 if (image.sections[i].base_address < min_address)
4673 /* clip addresses below */
4674 offset += min_address-image.sections[i].base_address;
4675 length -= offset;
4678 if (image.sections[i].base_address + buf_cnt > max_address)
4680 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4683 fastload[i].address = image.sections[i].base_address + offset;
4684 fastload[i].data = malloc(length);
4685 if (fastload[i].data == NULL)
4687 free(buffer);
4688 break;
4690 memcpy(fastload[i].data, buffer + offset, length);
4691 fastload[i].length = length;
4693 image_size += length;
4694 command_print(cmd_ctx, "%u bytes written at address 0x%8.8x",
4695 (unsigned int)length,
4696 ((unsigned int)(image.sections[i].base_address + offset)));
4699 free(buffer);
4702 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
4704 command_print(cmd_ctx, "Loaded %" PRIu32 " bytes "
4705 "in %fs (%0.3f kb/s)", image_size,
4706 duration_elapsed(&bench), duration_kbps(&bench, image_size));
4708 command_print(cmd_ctx,
4709 "WARNING: image has not been loaded to target!"
4710 "You can issue a 'fast_load' to finish loading.");
4713 image_close(&image);
4715 if (retval != ERROR_OK)
4717 free_fastload();
4720 return retval;
4723 static int handle_fast_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4725 if (argc > 0)
4726 return ERROR_COMMAND_SYNTAX_ERROR;
4727 if (fastload == NULL)
4729 LOG_ERROR("No image in memory");
4730 return ERROR_FAIL;
4732 int i;
4733 int ms = timeval_ms();
4734 int size = 0;
4735 int retval = ERROR_OK;
4736 for (i = 0; i < fastload_num;i++)
4738 target_t *target = get_current_target(cmd_ctx);
4739 command_print(cmd_ctx, "Write to 0x%08x, length 0x%08x",
4740 (unsigned int)(fastload[i].address),
4741 (unsigned int)(fastload[i].length));
4742 if (retval == ERROR_OK)
4744 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4746 size += fastload[i].length;
4748 int after = timeval_ms();
4749 command_print(cmd_ctx, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
4750 return retval;
4753 static int jim_mcrmrc(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4755 command_context_t *context;
4756 target_t *target;
4757 int retval;
4759 context = Jim_GetAssocData(interp, "context");
4760 if (context == NULL) {
4761 LOG_ERROR("array2mem: no command context");
4762 return JIM_ERR;
4764 target = get_current_target(context);
4765 if (target == NULL) {
4766 LOG_ERROR("array2mem: no current target");
4767 return JIM_ERR;
4770 if ((argc < 6) || (argc > 7))
4772 return JIM_ERR;
4775 int cpnum;
4776 uint32_t op1;
4777 uint32_t op2;
4778 uint32_t CRn;
4779 uint32_t CRm;
4780 uint32_t value;
4782 int e;
4783 long l;
4784 e = Jim_GetLong(interp, argv[1], &l);
4785 if (e != JIM_OK) {
4786 return e;
4788 cpnum = l;
4790 e = Jim_GetLong(interp, argv[2], &l);
4791 if (e != JIM_OK) {
4792 return e;
4794 op1 = l;
4796 e = Jim_GetLong(interp, argv[3], &l);
4797 if (e != JIM_OK) {
4798 return e;
4800 CRn = l;
4802 e = Jim_GetLong(interp, argv[4], &l);
4803 if (e != JIM_OK) {
4804 return e;
4806 CRm = l;
4808 e = Jim_GetLong(interp, argv[5], &l);
4809 if (e != JIM_OK) {
4810 return e;
4812 op2 = l;
4814 value = 0;
4816 if (argc == 7)
4818 e = Jim_GetLong(interp, argv[6], &l);
4819 if (e != JIM_OK) {
4820 return e;
4822 value = l;
4824 retval = target_mcr(target, cpnum, op1, op2, CRn, CRm, value);
4825 if (retval != ERROR_OK)
4826 return JIM_ERR;
4827 } else
4829 retval = target_mrc(target, cpnum, op1, op2, CRn, CRm, &value);
4830 if (retval != ERROR_OK)
4831 return JIM_ERR;
4833 Jim_SetResult(interp, Jim_NewIntObj(interp, value));
4836 return JIM_OK;