1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
17 * Copyright (C) 2011 by Broadcom Corporation *
18 * Evan Hunter - ehunter@broadcom.com *
20 * Copyright (C) ST-Ericsson SA 2011 *
21 * michel.jaouen@stericsson.com : smp minimum support *
23 * Copyright (C) 2011 Andreas Fritiofson *
24 * andreas.fritiofson@gmail.com *
26 * This program is free software; you can redistribute it and/or modify *
27 * it under the terms of the GNU General Public License as published by *
28 * the Free Software Foundation; either version 2 of the License, or *
29 * (at your option) any later version. *
31 * This program is distributed in the hope that it will be useful, *
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
34 * GNU General Public License for more details. *
36 * You should have received a copy of the GNU General Public License *
37 * along with this program; if not, write to the *
38 * Free Software Foundation, Inc., *
39 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
40 ***************************************************************************/
46 #include <helper/time_support.h>
47 #include <jtag/jtag.h>
48 #include <flash/nor/core.h>
51 #include "target_type.h"
52 #include "target_request.h"
53 #include "breakpoints.h"
57 #include "rtos/rtos.h"
59 static int target_read_buffer_default(struct target
*target
, uint32_t address
,
60 uint32_t size
, uint8_t *buffer
);
61 static int target_write_buffer_default(struct target
*target
, uint32_t address
,
62 uint32_t size
, const uint8_t *buffer
);
63 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
64 int argc
, Jim_Obj
* const *argv
);
65 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
66 int argc
, Jim_Obj
* const *argv
);
67 static int target_register_user_commands(struct command_context
*cmd_ctx
);
70 extern struct target_type arm7tdmi_target
;
71 extern struct target_type arm720t_target
;
72 extern struct target_type arm9tdmi_target
;
73 extern struct target_type arm920t_target
;
74 extern struct target_type arm966e_target
;
75 extern struct target_type arm946e_target
;
76 extern struct target_type arm926ejs_target
;
77 extern struct target_type fa526_target
;
78 extern struct target_type feroceon_target
;
79 extern struct target_type dragonite_target
;
80 extern struct target_type xscale_target
;
81 extern struct target_type cortexm3_target
;
82 extern struct target_type cortexa8_target
;
83 extern struct target_type arm11_target
;
84 extern struct target_type mips_m4k_target
;
85 extern struct target_type avr_target
;
86 extern struct target_type dsp563xx_target
;
87 extern struct target_type dsp5680xx_target
;
88 extern struct target_type testee_target
;
89 extern struct target_type avr32_ap7k_target
;
90 extern struct target_type hla_target
;
92 static struct target_type
*target_types
[] = {
117 struct target
*all_targets
;
118 static struct target_event_callback
*target_event_callbacks
;
119 static struct target_timer_callback
*target_timer_callbacks
;
120 static const int polling_interval
= 100;
122 static const Jim_Nvp nvp_assert
[] = {
123 { .name
= "assert", NVP_ASSERT
},
124 { .name
= "deassert", NVP_DEASSERT
},
125 { .name
= "T", NVP_ASSERT
},
126 { .name
= "F", NVP_DEASSERT
},
127 { .name
= "t", NVP_ASSERT
},
128 { .name
= "f", NVP_DEASSERT
},
129 { .name
= NULL
, .value
= -1 }
132 static const Jim_Nvp nvp_error_target
[] = {
133 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
134 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
135 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
136 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
137 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
138 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
139 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
140 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
141 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
142 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
143 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
144 { .value
= -1, .name
= NULL
}
147 static const char *target_strerror_safe(int err
)
151 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
158 static const Jim_Nvp nvp_target_event
[] = {
160 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
161 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
162 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
163 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
164 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
166 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
167 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
169 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
170 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
171 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
172 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
173 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
174 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
175 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
176 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
177 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
178 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
179 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
180 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
182 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
183 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
185 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
186 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
188 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
189 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
191 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
192 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
194 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
195 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
197 { .name
= NULL
, .value
= -1 }
200 static const Jim_Nvp nvp_target_state
[] = {
201 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
202 { .name
= "running", .value
= TARGET_RUNNING
},
203 { .name
= "halted", .value
= TARGET_HALTED
},
204 { .name
= "reset", .value
= TARGET_RESET
},
205 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
206 { .name
= NULL
, .value
= -1 },
209 static const Jim_Nvp nvp_target_debug_reason
[] = {
210 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
211 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
212 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
213 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
214 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
215 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
216 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
217 { .name
= NULL
, .value
= -1 },
220 static const Jim_Nvp nvp_target_endian
[] = {
221 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
222 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
223 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
224 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
225 { .name
= NULL
, .value
= -1 },
228 static const Jim_Nvp nvp_reset_modes
[] = {
229 { .name
= "unknown", .value
= RESET_UNKNOWN
},
230 { .name
= "run" , .value
= RESET_RUN
},
231 { .name
= "halt" , .value
= RESET_HALT
},
232 { .name
= "init" , .value
= RESET_INIT
},
233 { .name
= NULL
, .value
= -1 },
236 const char *debug_reason_name(struct target
*t
)
240 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
241 t
->debug_reason
)->name
;
243 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
244 cp
= "(*BUG*unknown*BUG*)";
249 const char *target_state_name(struct target
*t
)
252 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
254 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
255 cp
= "(*BUG*unknown*BUG*)";
260 /* determine the number of the new target */
261 static int new_target_number(void)
266 /* number is 0 based */
270 if (x
< t
->target_number
)
271 x
= t
->target_number
;
277 /* read a uint32_t from a buffer in target memory endianness */
278 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
280 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
281 return le_to_h_u32(buffer
);
283 return be_to_h_u32(buffer
);
286 /* read a uint24_t from a buffer in target memory endianness */
287 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
289 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
290 return le_to_h_u24(buffer
);
292 return be_to_h_u24(buffer
);
295 /* read a uint16_t from a buffer in target memory endianness */
296 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
298 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
299 return le_to_h_u16(buffer
);
301 return be_to_h_u16(buffer
);
304 /* read a uint8_t from a buffer in target memory endianness */
305 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
307 return *buffer
& 0x0ff;
310 /* write a uint32_t to a buffer in target memory endianness */
311 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
313 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
314 h_u32_to_le(buffer
, value
);
316 h_u32_to_be(buffer
, value
);
319 /* write a uint24_t to a buffer in target memory endianness */
320 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
322 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
323 h_u24_to_le(buffer
, value
);
325 h_u24_to_be(buffer
, value
);
328 /* write a uint16_t to a buffer in target memory endianness */
329 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
331 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
332 h_u16_to_le(buffer
, value
);
334 h_u16_to_be(buffer
, value
);
337 /* write a uint8_t to a buffer in target memory endianness */
338 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
343 /* write a uint32_t array to a buffer in target memory endianness */
344 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
347 for (i
= 0; i
< count
; i
++)
348 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
351 /* write a uint16_t array to a buffer in target memory endianness */
352 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
355 for (i
= 0; i
< count
; i
++)
356 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
359 /* write a uint32_t array to a buffer in target memory endianness */
360 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, uint32_t *srcbuf
)
363 for (i
= 0; i
< count
; i
++)
364 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
367 /* write a uint16_t array to a buffer in target memory endianness */
368 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, uint16_t *srcbuf
)
371 for (i
= 0; i
< count
; i
++)
372 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
375 /* return a pointer to a configured target; id is name or number */
376 struct target
*get_target(const char *id
)
378 struct target
*target
;
380 /* try as tcltarget name */
381 for (target
= all_targets
; target
; target
= target
->next
) {
382 if (target
->cmd_name
== NULL
)
384 if (strcmp(id
, target
->cmd_name
) == 0)
388 /* It's OK to remove this fallback sometime after August 2010 or so */
390 /* no match, try as number */
392 if (parse_uint(id
, &num
) != ERROR_OK
)
395 for (target
= all_targets
; target
; target
= target
->next
) {
396 if (target
->target_number
== (int)num
) {
397 LOG_WARNING("use '%s' as target identifier, not '%u'",
398 target
->cmd_name
, num
);
406 /* returns a pointer to the n-th configured target */
407 static struct target
*get_target_by_num(int num
)
409 struct target
*target
= all_targets
;
412 if (target
->target_number
== num
)
414 target
= target
->next
;
420 struct target
*get_current_target(struct command_context
*cmd_ctx
)
422 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
424 if (target
== NULL
) {
425 LOG_ERROR("BUG: current_target out of bounds");
432 int target_poll(struct target
*target
)
436 /* We can't poll until after examine */
437 if (!target_was_examined(target
)) {
438 /* Fail silently lest we pollute the log */
442 retval
= target
->type
->poll(target
);
443 if (retval
!= ERROR_OK
)
446 if (target
->halt_issued
) {
447 if (target
->state
== TARGET_HALTED
)
448 target
->halt_issued
= false;
450 long long t
= timeval_ms() - target
->halt_issued_time
;
452 target
->halt_issued
= false;
453 LOG_INFO("Halt timed out, wake up GDB.");
454 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
462 int target_halt(struct target
*target
)
465 /* We can't poll until after examine */
466 if (!target_was_examined(target
)) {
467 LOG_ERROR("Target not examined yet");
471 retval
= target
->type
->halt(target
);
472 if (retval
!= ERROR_OK
)
475 target
->halt_issued
= true;
476 target
->halt_issued_time
= timeval_ms();
482 * Make the target (re)start executing using its saved execution
483 * context (possibly with some modifications).
485 * @param target Which target should start executing.
486 * @param current True to use the target's saved program counter instead
487 * of the address parameter
488 * @param address Optionally used as the program counter.
489 * @param handle_breakpoints True iff breakpoints at the resumption PC
490 * should be skipped. (For example, maybe execution was stopped by
491 * such a breakpoint, in which case it would be counterprodutive to
493 * @param debug_execution False if all working areas allocated by OpenOCD
494 * should be released and/or restored to their original contents.
495 * (This would for example be true to run some downloaded "helper"
496 * algorithm code, which resides in one such working buffer and uses
497 * another for data storage.)
499 * @todo Resolve the ambiguity about what the "debug_execution" flag
500 * signifies. For example, Target implementations don't agree on how
501 * it relates to invalidation of the register cache, or to whether
502 * breakpoints and watchpoints should be enabled. (It would seem wrong
503 * to enable breakpoints when running downloaded "helper" algorithms
504 * (debug_execution true), since the breakpoints would be set to match
505 * target firmware being debugged, not the helper algorithm.... and
506 * enabling them could cause such helpers to malfunction (for example,
507 * by overwriting data with a breakpoint instruction. On the other
508 * hand the infrastructure for running such helpers might use this
509 * procedure but rely on hardware breakpoint to detect termination.)
511 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
515 /* We can't poll until after examine */
516 if (!target_was_examined(target
)) {
517 LOG_ERROR("Target not examined yet");
521 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
523 /* note that resume *must* be asynchronous. The CPU can halt before
524 * we poll. The CPU can even halt at the current PC as a result of
525 * a software breakpoint being inserted by (a bug?) the application.
527 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
528 if (retval
!= ERROR_OK
)
531 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
536 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
541 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
542 if (n
->name
== NULL
) {
543 LOG_ERROR("invalid reset mode");
547 /* disable polling during reset to make reset event scripts
548 * more predictable, i.e. dr/irscan & pathmove in events will
549 * not have JTAG operations injected into the middle of a sequence.
551 bool save_poll
= jtag_poll_get_enabled();
553 jtag_poll_set_enabled(false);
555 sprintf(buf
, "ocd_process_reset %s", n
->name
);
556 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
558 jtag_poll_set_enabled(save_poll
);
560 if (retval
!= JIM_OK
) {
561 Jim_MakeErrorMessage(cmd_ctx
->interp
);
562 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
566 /* We want any events to be processed before the prompt */
567 retval
= target_call_timer_callbacks_now();
569 struct target
*target
;
570 for (target
= all_targets
; target
; target
= target
->next
)
571 target
->type
->check_reset(target
);
576 static int identity_virt2phys(struct target
*target
,
577 uint32_t virtual, uint32_t *physical
)
583 static int no_mmu(struct target
*target
, int *enabled
)
589 static int default_examine(struct target
*target
)
591 target_set_examined(target
);
595 /* no check by default */
596 static int default_check_reset(struct target
*target
)
601 int target_examine_one(struct target
*target
)
603 return target
->type
->examine(target
);
606 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
608 struct target
*target
= priv
;
610 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
613 jtag_unregister_event_callback(jtag_enable_callback
, target
);
615 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
617 int retval
= target_examine_one(target
);
618 if (retval
!= ERROR_OK
)
621 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
626 /* Targets that correctly implement init + examine, i.e.
627 * no communication with target during init:
631 int target_examine(void)
633 int retval
= ERROR_OK
;
634 struct target
*target
;
636 for (target
= all_targets
; target
; target
= target
->next
) {
637 /* defer examination, but don't skip it */
638 if (!target
->tap
->enabled
) {
639 jtag_register_event_callback(jtag_enable_callback
,
644 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
646 retval
= target_examine_one(target
);
647 if (retval
!= ERROR_OK
)
650 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
655 const char *target_type_name(struct target
*target
)
657 return target
->type
->name
;
660 static int target_write_memory_imp(struct target
*target
, uint32_t address
,
661 uint32_t size
, uint32_t count
, const uint8_t *buffer
)
663 if (!target_was_examined(target
)) {
664 LOG_ERROR("Target not examined yet");
667 return target
->type
->write_memory_imp(target
, address
, size
, count
, buffer
);
670 static int target_read_memory_imp(struct target
*target
, uint32_t address
,
671 uint32_t size
, uint32_t count
, uint8_t *buffer
)
673 if (!target_was_examined(target
)) {
674 LOG_ERROR("Target not examined yet");
677 return target
->type
->read_memory_imp(target
, address
, size
, count
, buffer
);
680 static int target_soft_reset_halt_imp(struct target
*target
)
682 if (!target_was_examined(target
)) {
683 LOG_ERROR("Target not examined yet");
686 if (!target
->type
->soft_reset_halt_imp
) {
687 LOG_ERROR("Target %s does not support soft_reset_halt",
688 target_name(target
));
691 return target
->type
->soft_reset_halt_imp(target
);
695 * Downloads a target-specific native code algorithm to the target,
696 * and executes it. * Note that some targets may need to set up, enable,
697 * and tear down a breakpoint (hard or * soft) to detect algorithm
698 * termination, while others may support lower overhead schemes where
699 * soft breakpoints embedded in the algorithm automatically terminate the
702 * @param target used to run the algorithm
703 * @param arch_info target-specific description of the algorithm.
705 int target_run_algorithm(struct target
*target
,
706 int num_mem_params
, struct mem_param
*mem_params
,
707 int num_reg_params
, struct reg_param
*reg_param
,
708 uint32_t entry_point
, uint32_t exit_point
,
709 int timeout_ms
, void *arch_info
)
711 int retval
= ERROR_FAIL
;
713 if (!target_was_examined(target
)) {
714 LOG_ERROR("Target not examined yet");
717 if (!target
->type
->run_algorithm
) {
718 LOG_ERROR("Target type '%s' does not support %s",
719 target_type_name(target
), __func__
);
723 target
->running_alg
= true;
724 retval
= target
->type
->run_algorithm(target
,
725 num_mem_params
, mem_params
,
726 num_reg_params
, reg_param
,
727 entry_point
, exit_point
, timeout_ms
, arch_info
);
728 target
->running_alg
= false;
735 * Downloads a target-specific native code algorithm to the target,
736 * executes and leaves it running.
738 * @param target used to run the algorithm
739 * @param arch_info target-specific description of the algorithm.
741 int target_start_algorithm(struct target
*target
,
742 int num_mem_params
, struct mem_param
*mem_params
,
743 int num_reg_params
, struct reg_param
*reg_params
,
744 uint32_t entry_point
, uint32_t exit_point
,
747 int retval
= ERROR_FAIL
;
749 if (!target_was_examined(target
)) {
750 LOG_ERROR("Target not examined yet");
753 if (!target
->type
->start_algorithm
) {
754 LOG_ERROR("Target type '%s' does not support %s",
755 target_type_name(target
), __func__
);
758 if (target
->running_alg
) {
759 LOG_ERROR("Target is already running an algorithm");
763 target
->running_alg
= true;
764 retval
= target
->type
->start_algorithm(target
,
765 num_mem_params
, mem_params
,
766 num_reg_params
, reg_params
,
767 entry_point
, exit_point
, arch_info
);
774 * Waits for an algorithm started with target_start_algorithm() to complete.
776 * @param target used to run the algorithm
777 * @param arch_info target-specific description of the algorithm.
779 int target_wait_algorithm(struct target
*target
,
780 int num_mem_params
, struct mem_param
*mem_params
,
781 int num_reg_params
, struct reg_param
*reg_params
,
782 uint32_t exit_point
, int timeout_ms
,
785 int retval
= ERROR_FAIL
;
787 if (!target
->type
->wait_algorithm
) {
788 LOG_ERROR("Target type '%s' does not support %s",
789 target_type_name(target
), __func__
);
792 if (!target
->running_alg
) {
793 LOG_ERROR("Target is not running an algorithm");
797 retval
= target
->type
->wait_algorithm(target
,
798 num_mem_params
, mem_params
,
799 num_reg_params
, reg_params
,
800 exit_point
, timeout_ms
, arch_info
);
801 if (retval
!= ERROR_TARGET_TIMEOUT
)
802 target
->running_alg
= false;
809 * Executes a target-specific native code algorithm in the target.
810 * It differs from target_run_algorithm in that the algorithm is asynchronous.
811 * Because of this it requires an compliant algorithm:
812 * see contrib/loaders/flash/stm32f1x.S for example.
814 * @param target used to run the algorithm
817 int target_run_flash_async_algorithm(struct target
*target
,
818 uint8_t *buffer
, uint32_t count
, int block_size
,
819 int num_mem_params
, struct mem_param
*mem_params
,
820 int num_reg_params
, struct reg_param
*reg_params
,
821 uint32_t buffer_start
, uint32_t buffer_size
,
822 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
827 /* Set up working area. First word is write pointer, second word is read pointer,
828 * rest is fifo data area. */
829 uint32_t wp_addr
= buffer_start
;
830 uint32_t rp_addr
= buffer_start
+ 4;
831 uint32_t fifo_start_addr
= buffer_start
+ 8;
832 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
834 uint32_t wp
= fifo_start_addr
;
835 uint32_t rp
= fifo_start_addr
;
837 /* validate block_size is 2^n */
838 assert(!block_size
|| !(block_size
& (block_size
- 1)));
840 retval
= target_write_u32(target
, wp_addr
, wp
);
841 if (retval
!= ERROR_OK
)
843 retval
= target_write_u32(target
, rp_addr
, rp
);
844 if (retval
!= ERROR_OK
)
847 /* Start up algorithm on target and let it idle while writing the first chunk */
848 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
849 num_reg_params
, reg_params
,
854 if (retval
!= ERROR_OK
) {
855 LOG_ERROR("error starting target flash write algorithm");
861 retval
= target_read_u32(target
, rp_addr
, &rp
);
862 if (retval
!= ERROR_OK
) {
863 LOG_ERROR("failed to get read pointer");
867 LOG_DEBUG("count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
, count
, wp
, rp
);
870 LOG_ERROR("flash write algorithm aborted by target");
871 retval
= ERROR_FLASH_OPERATION_FAILED
;
875 if ((rp
& (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
876 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
880 /* Count the number of bytes available in the fifo without
881 * crossing the wrap around. Make sure to not fill it completely,
882 * because that would make wp == rp and that's the empty condition. */
883 uint32_t thisrun_bytes
;
885 thisrun_bytes
= rp
- wp
- block_size
;
886 else if (rp
> fifo_start_addr
)
887 thisrun_bytes
= fifo_end_addr
- wp
;
889 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
891 if (thisrun_bytes
== 0) {
892 /* Throttle polling a bit if transfer is (much) faster than flash
893 * programming. The exact delay shouldn't matter as long as it's
894 * less than buffer size / flash speed. This is very unlikely to
895 * run when using high latency connections such as USB. */
898 /* to stop an infinite loop on some targets check and increment a timeout
899 * this issue was observed on a stellaris using the new ICDI interface */
900 if (timeout
++ >= 500) {
901 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
902 return ERROR_FLASH_OPERATION_FAILED
;
907 /* reset our timeout */
910 /* Limit to the amount of data we actually want to write */
911 if (thisrun_bytes
> count
* block_size
)
912 thisrun_bytes
= count
* block_size
;
914 /* Write data to fifo */
915 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
916 if (retval
!= ERROR_OK
)
919 /* Update counters and wrap write pointer */
920 buffer
+= thisrun_bytes
;
921 count
-= thisrun_bytes
/ block_size
;
923 if (wp
>= fifo_end_addr
)
924 wp
= fifo_start_addr
;
926 /* Store updated write pointer to target */
927 retval
= target_write_u32(target
, wp_addr
, wp
);
928 if (retval
!= ERROR_OK
)
932 if (retval
!= ERROR_OK
) {
933 /* abort flash write algorithm on target */
934 target_write_u32(target
, wp_addr
, 0);
937 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
938 num_reg_params
, reg_params
,
943 if (retval2
!= ERROR_OK
) {
944 LOG_ERROR("error waiting for target flash write algorithm");
951 int target_read_memory(struct target
*target
,
952 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
954 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
957 static int target_read_phys_memory(struct target
*target
,
958 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
960 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
963 int target_write_memory(struct target
*target
,
964 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
966 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
969 static int target_write_phys_memory(struct target
*target
,
970 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
972 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
975 int target_bulk_write_memory(struct target
*target
,
976 uint32_t address
, uint32_t count
, const uint8_t *buffer
)
978 return target
->type
->bulk_write_memory(target
, address
, count
, buffer
);
981 int target_add_breakpoint(struct target
*target
,
982 struct breakpoint
*breakpoint
)
984 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
985 LOG_WARNING("target %s is not halted", target
->cmd_name
);
986 return ERROR_TARGET_NOT_HALTED
;
988 return target
->type
->add_breakpoint(target
, breakpoint
);
991 int target_add_context_breakpoint(struct target
*target
,
992 struct breakpoint
*breakpoint
)
994 if (target
->state
!= TARGET_HALTED
) {
995 LOG_WARNING("target %s is not halted", target
->cmd_name
);
996 return ERROR_TARGET_NOT_HALTED
;
998 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1001 int target_add_hybrid_breakpoint(struct target
*target
,
1002 struct breakpoint
*breakpoint
)
1004 if (target
->state
!= TARGET_HALTED
) {
1005 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1006 return ERROR_TARGET_NOT_HALTED
;
1008 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1011 int target_remove_breakpoint(struct target
*target
,
1012 struct breakpoint
*breakpoint
)
1014 return target
->type
->remove_breakpoint(target
, breakpoint
);
1017 int target_add_watchpoint(struct target
*target
,
1018 struct watchpoint
*watchpoint
)
1020 if (target
->state
!= TARGET_HALTED
) {
1021 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1022 return ERROR_TARGET_NOT_HALTED
;
1024 return target
->type
->add_watchpoint(target
, watchpoint
);
1026 int target_remove_watchpoint(struct target
*target
,
1027 struct watchpoint
*watchpoint
)
1029 return target
->type
->remove_watchpoint(target
, watchpoint
);
1032 int target_get_gdb_reg_list(struct target
*target
,
1033 struct reg
**reg_list
[], int *reg_list_size
)
1035 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
);
1037 int target_step(struct target
*target
,
1038 int current
, uint32_t address
, int handle_breakpoints
)
1040 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1044 * Reset the @c examined flag for the given target.
1045 * Pure paranoia -- targets are zeroed on allocation.
1047 static void target_reset_examined(struct target
*target
)
1049 target
->examined
= false;
1052 static int err_read_phys_memory(struct target
*target
, uint32_t address
,
1053 uint32_t size
, uint32_t count
, uint8_t *buffer
)
1055 LOG_ERROR("Not implemented: %s", __func__
);
1059 static int err_write_phys_memory(struct target
*target
, uint32_t address
,
1060 uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1062 LOG_ERROR("Not implemented: %s", __func__
);
1066 static int handle_target(void *priv
);
1068 static int target_init_one(struct command_context
*cmd_ctx
,
1069 struct target
*target
)
1071 target_reset_examined(target
);
1073 struct target_type
*type
= target
->type
;
1074 if (type
->examine
== NULL
)
1075 type
->examine
= default_examine
;
1077 if (type
->check_reset
== NULL
)
1078 type
->check_reset
= default_check_reset
;
1080 assert(type
->init_target
!= NULL
);
1082 int retval
= type
->init_target(cmd_ctx
, target
);
1083 if (ERROR_OK
!= retval
) {
1084 LOG_ERROR("target '%s' init failed", target_name(target
));
1089 * @todo get rid of those *memory_imp() methods, now that all
1090 * callers are using target_*_memory() accessors ... and make
1091 * sure the "physical" paths handle the same issues.
1093 /* a non-invasive way(in terms of patches) to add some code that
1094 * runs before the type->write/read_memory implementation
1096 type
->write_memory_imp
= target
->type
->write_memory
;
1097 type
->write_memory
= target_write_memory_imp
;
1099 type
->read_memory_imp
= target
->type
->read_memory
;
1100 type
->read_memory
= target_read_memory_imp
;
1102 type
->soft_reset_halt_imp
= target
->type
->soft_reset_halt
;
1103 type
->soft_reset_halt
= target_soft_reset_halt_imp
;
1105 /* Sanity-check MMU support ... stub in what we must, to help
1106 * implement it in stages, but warn if we need to do so.
1109 if (type
->write_phys_memory
== NULL
) {
1110 LOG_ERROR("type '%s' is missing write_phys_memory",
1112 type
->write_phys_memory
= err_write_phys_memory
;
1114 if (type
->read_phys_memory
== NULL
) {
1115 LOG_ERROR("type '%s' is missing read_phys_memory",
1117 type
->read_phys_memory
= err_read_phys_memory
;
1119 if (type
->virt2phys
== NULL
) {
1120 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1121 type
->virt2phys
= identity_virt2phys
;
1124 /* Make sure no-MMU targets all behave the same: make no
1125 * distinction between physical and virtual addresses, and
1126 * ensure that virt2phys() is always an identity mapping.
1128 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1129 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1132 type
->write_phys_memory
= type
->write_memory
;
1133 type
->read_phys_memory
= type
->read_memory
;
1134 type
->virt2phys
= identity_virt2phys
;
1137 if (target
->type
->read_buffer
== NULL
)
1138 target
->type
->read_buffer
= target_read_buffer_default
;
1140 if (target
->type
->write_buffer
== NULL
)
1141 target
->type
->write_buffer
= target_write_buffer_default
;
1146 static int target_init(struct command_context
*cmd_ctx
)
1148 struct target
*target
;
1151 for (target
= all_targets
; target
; target
= target
->next
) {
1152 retval
= target_init_one(cmd_ctx
, target
);
1153 if (ERROR_OK
!= retval
)
1160 retval
= target_register_user_commands(cmd_ctx
);
1161 if (ERROR_OK
!= retval
)
1164 retval
= target_register_timer_callback(&handle_target
,
1165 polling_interval
, 1, cmd_ctx
->interp
);
1166 if (ERROR_OK
!= retval
)
1172 COMMAND_HANDLER(handle_target_init_command
)
1177 return ERROR_COMMAND_SYNTAX_ERROR
;
1179 static bool target_initialized
;
1180 if (target_initialized
) {
1181 LOG_INFO("'target init' has already been called");
1184 target_initialized
= true;
1186 retval
= command_run_line(CMD_CTX
, "init_targets");
1187 if (ERROR_OK
!= retval
)
1190 retval
= command_run_line(CMD_CTX
, "init_board");
1191 if (ERROR_OK
!= retval
)
1194 LOG_DEBUG("Initializing targets...");
1195 return target_init(CMD_CTX
);
1198 int target_register_event_callback(int (*callback
)(struct target
*target
,
1199 enum target_event event
, void *priv
), void *priv
)
1201 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1203 if (callback
== NULL
)
1204 return ERROR_COMMAND_SYNTAX_ERROR
;
1207 while ((*callbacks_p
)->next
)
1208 callbacks_p
= &((*callbacks_p
)->next
);
1209 callbacks_p
= &((*callbacks_p
)->next
);
1212 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1213 (*callbacks_p
)->callback
= callback
;
1214 (*callbacks_p
)->priv
= priv
;
1215 (*callbacks_p
)->next
= NULL
;
1220 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1222 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1225 if (callback
== NULL
)
1226 return ERROR_COMMAND_SYNTAX_ERROR
;
1229 while ((*callbacks_p
)->next
)
1230 callbacks_p
= &((*callbacks_p
)->next
);
1231 callbacks_p
= &((*callbacks_p
)->next
);
1234 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1235 (*callbacks_p
)->callback
= callback
;
1236 (*callbacks_p
)->periodic
= periodic
;
1237 (*callbacks_p
)->time_ms
= time_ms
;
1239 gettimeofday(&now
, NULL
);
1240 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
1241 time_ms
-= (time_ms
% 1000);
1242 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
1243 if ((*callbacks_p
)->when
.tv_usec
> 1000000) {
1244 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
1245 (*callbacks_p
)->when
.tv_sec
+= 1;
1248 (*callbacks_p
)->priv
= priv
;
1249 (*callbacks_p
)->next
= NULL
;
1254 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1255 enum target_event event
, void *priv
), void *priv
)
1257 struct target_event_callback
**p
= &target_event_callbacks
;
1258 struct target_event_callback
*c
= target_event_callbacks
;
1260 if (callback
== NULL
)
1261 return ERROR_COMMAND_SYNTAX_ERROR
;
1264 struct target_event_callback
*next
= c
->next
;
1265 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1277 static int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1279 struct target_timer_callback
**p
= &target_timer_callbacks
;
1280 struct target_timer_callback
*c
= target_timer_callbacks
;
1282 if (callback
== NULL
)
1283 return ERROR_COMMAND_SYNTAX_ERROR
;
1286 struct target_timer_callback
*next
= c
->next
;
1287 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1299 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1301 struct target_event_callback
*callback
= target_event_callbacks
;
1302 struct target_event_callback
*next_callback
;
1304 if (event
== TARGET_EVENT_HALTED
) {
1305 /* execute early halted first */
1306 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1309 LOG_DEBUG("target event %i (%s)", event
,
1310 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1312 target_handle_event(target
, event
);
1315 next_callback
= callback
->next
;
1316 callback
->callback(target
, event
, callback
->priv
);
1317 callback
= next_callback
;
1323 static int target_timer_callback_periodic_restart(
1324 struct target_timer_callback
*cb
, struct timeval
*now
)
1326 int time_ms
= cb
->time_ms
;
1327 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1328 time_ms
-= (time_ms
% 1000);
1329 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1330 if (cb
->when
.tv_usec
> 1000000) {
1331 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1332 cb
->when
.tv_sec
+= 1;
1337 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1338 struct timeval
*now
)
1340 cb
->callback(cb
->priv
);
1343 return target_timer_callback_periodic_restart(cb
, now
);
1345 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1348 static int target_call_timer_callbacks_check_time(int checktime
)
1353 gettimeofday(&now
, NULL
);
1355 struct target_timer_callback
*callback
= target_timer_callbacks
;
1357 /* cleaning up may unregister and free this callback */
1358 struct target_timer_callback
*next_callback
= callback
->next
;
1360 bool call_it
= callback
->callback
&&
1361 ((!checktime
&& callback
->periodic
) ||
1362 now
.tv_sec
> callback
->when
.tv_sec
||
1363 (now
.tv_sec
== callback
->when
.tv_sec
&&
1364 now
.tv_usec
>= callback
->when
.tv_usec
));
1367 int retval
= target_call_timer_callback(callback
, &now
);
1368 if (retval
!= ERROR_OK
)
1372 callback
= next_callback
;
1378 int target_call_timer_callbacks(void)
1380 return target_call_timer_callbacks_check_time(1);
1383 /* invoke periodic callbacks immediately */
1384 int target_call_timer_callbacks_now(void)
1386 return target_call_timer_callbacks_check_time(0);
1389 /* Prints the working area layout for debug purposes */
1390 static void print_wa_layout(struct target
*target
)
1392 struct working_area
*c
= target
->working_areas
;
1395 LOG_DEBUG("%c%c 0x%08"PRIx32
"-0x%08"PRIx32
" (%"PRIu32
" bytes)",
1396 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1397 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1402 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1403 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1405 assert(area
->free
); /* Shouldn't split an allocated area */
1406 assert(size
<= area
->size
); /* Caller should guarantee this */
1408 /* Split only if not already the right size */
1409 if (size
< area
->size
) {
1410 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1415 new_wa
->next
= area
->next
;
1416 new_wa
->size
= area
->size
- size
;
1417 new_wa
->address
= area
->address
+ size
;
1418 new_wa
->backup
= NULL
;
1419 new_wa
->user
= NULL
;
1420 new_wa
->free
= true;
1422 area
->next
= new_wa
;
1425 /* If backup memory was allocated to this area, it has the wrong size
1426 * now so free it and it will be reallocated if/when needed */
1429 area
->backup
= NULL
;
1434 /* Merge all adjacent free areas into one */
1435 static void target_merge_working_areas(struct target
*target
)
1437 struct working_area
*c
= target
->working_areas
;
1439 while (c
&& c
->next
) {
1440 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1442 /* Find two adjacent free areas */
1443 if (c
->free
&& c
->next
->free
) {
1444 /* Merge the last into the first */
1445 c
->size
+= c
->next
->size
;
1447 /* Remove the last */
1448 struct working_area
*to_be_freed
= c
->next
;
1449 c
->next
= c
->next
->next
;
1450 if (to_be_freed
->backup
)
1451 free(to_be_freed
->backup
);
1454 /* If backup memory was allocated to the remaining area, it's has
1455 * the wrong size now */
1466 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1468 /* Reevaluate working area address based on MMU state*/
1469 if (target
->working_areas
== NULL
) {
1473 retval
= target
->type
->mmu(target
, &enabled
);
1474 if (retval
!= ERROR_OK
)
1478 if (target
->working_area_phys_spec
) {
1479 LOG_DEBUG("MMU disabled, using physical "
1480 "address for working memory 0x%08"PRIx32
,
1481 target
->working_area_phys
);
1482 target
->working_area
= target
->working_area_phys
;
1484 LOG_ERROR("No working memory available. "
1485 "Specify -work-area-phys to target.");
1486 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1489 if (target
->working_area_virt_spec
) {
1490 LOG_DEBUG("MMU enabled, using virtual "
1491 "address for working memory 0x%08"PRIx32
,
1492 target
->working_area_virt
);
1493 target
->working_area
= target
->working_area_virt
;
1495 LOG_ERROR("No working memory available. "
1496 "Specify -work-area-virt to target.");
1497 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1501 /* Set up initial working area on first call */
1502 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1504 new_wa
->next
= NULL
;
1505 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1506 new_wa
->address
= target
->working_area
;
1507 new_wa
->backup
= NULL
;
1508 new_wa
->user
= NULL
;
1509 new_wa
->free
= true;
1512 target
->working_areas
= new_wa
;
1515 /* only allocate multiples of 4 byte */
1517 size
= (size
+ 3) & (~3UL);
1519 struct working_area
*c
= target
->working_areas
;
1521 /* Find the first large enough working area */
1523 if (c
->free
&& c
->size
>= size
)
1529 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1531 /* Split the working area into the requested size */
1532 target_split_working_area(c
, size
);
1534 LOG_DEBUG("allocated new working area of %"PRIu32
" bytes at address 0x%08"PRIx32
, size
, c
->address
);
1536 if (target
->backup_working_area
) {
1537 if (c
->backup
== NULL
) {
1538 c
->backup
= malloc(c
->size
);
1539 if (c
->backup
== NULL
)
1543 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1544 if (retval
!= ERROR_OK
)
1548 /* mark as used, and return the new (reused) area */
1555 print_wa_layout(target
);
1560 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1564 retval
= target_alloc_working_area_try(target
, size
, area
);
1565 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1566 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1571 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1573 int retval
= ERROR_OK
;
1575 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1576 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1577 if (retval
!= ERROR_OK
)
1578 LOG_ERROR("failed to restore %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1579 area
->size
, area
->address
);
1585 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1586 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1588 int retval
= ERROR_OK
;
1594 retval
= target_restore_working_area(target
, area
);
1595 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1596 if (retval
!= ERROR_OK
)
1602 LOG_DEBUG("freed %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1603 area
->size
, area
->address
);
1605 /* mark user pointer invalid */
1606 /* TODO: Is this really safe? It points to some previous caller's memory.
1607 * How could we know that the area pointer is still in that place and not
1608 * some other vital data? What's the purpose of this, anyway? */
1612 target_merge_working_areas(target
);
1614 print_wa_layout(target
);
1619 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1621 return target_free_working_area_restore(target
, area
, 1);
1624 /* free resources and restore memory, if restoring memory fails,
1625 * free up resources anyway
1627 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1629 struct working_area
*c
= target
->working_areas
;
1631 LOG_DEBUG("freeing all working areas");
1633 /* Loop through all areas, restoring the allocated ones and marking them as free */
1637 target_restore_working_area(target
, c
);
1639 *c
->user
= NULL
; /* Same as above */
1645 /* Run a merge pass to combine all areas into one */
1646 target_merge_working_areas(target
);
1648 print_wa_layout(target
);
1651 void target_free_all_working_areas(struct target
*target
)
1653 target_free_all_working_areas_restore(target
, 1);
1656 /* Find the largest number of bytes that can be allocated */
1657 uint32_t target_get_working_area_avail(struct target
*target
)
1659 struct working_area
*c
= target
->working_areas
;
1660 uint32_t max_size
= 0;
1663 return target
->working_area_size
;
1666 if (c
->free
&& max_size
< c
->size
)
1675 int target_arch_state(struct target
*target
)
1678 if (target
== NULL
) {
1679 LOG_USER("No target has been configured");
1683 LOG_USER("target state: %s", target_state_name(target
));
1685 if (target
->state
!= TARGET_HALTED
)
1688 retval
= target
->type
->arch_state(target
);
1692 /* Single aligned words are guaranteed to use 16 or 32 bit access
1693 * mode respectively, otherwise data is handled as quickly as
1696 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
1698 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1699 (int)size
, (unsigned)address
);
1701 if (!target_was_examined(target
)) {
1702 LOG_ERROR("Target not examined yet");
1709 if ((address
+ size
- 1) < address
) {
1710 /* GDB can request this when e.g. PC is 0xfffffffc*/
1711 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1717 return target
->type
->write_buffer(target
, address
, size
, buffer
);
1720 static int target_write_buffer_default(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
1722 int retval
= ERROR_OK
;
1724 if (((address
% 2) == 0) && (size
== 2))
1725 return target_write_memory(target
, address
, 2, 1, buffer
);
1727 /* handle unaligned head bytes */
1729 uint32_t unaligned
= 4 - (address
% 4);
1731 if (unaligned
> size
)
1734 retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
);
1735 if (retval
!= ERROR_OK
)
1738 buffer
+= unaligned
;
1739 address
+= unaligned
;
1743 /* handle aligned words */
1745 int aligned
= size
- (size
% 4);
1747 /* use bulk writes above a certain limit. This may have to be changed */
1748 if (aligned
> 128) {
1749 retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
);
1750 if (retval
!= ERROR_OK
)
1753 retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
);
1754 if (retval
!= ERROR_OK
)
1763 /* handle tail writes of less than 4 bytes */
1765 retval
= target_write_memory(target
, address
, 1, size
, buffer
);
1766 if (retval
!= ERROR_OK
)
1773 /* Single aligned words are guaranteed to use 16 or 32 bit access
1774 * mode respectively, otherwise data is handled as quickly as
1777 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1779 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1780 (int)size
, (unsigned)address
);
1782 if (!target_was_examined(target
)) {
1783 LOG_ERROR("Target not examined yet");
1790 if ((address
+ size
- 1) < address
) {
1791 /* GDB can request this when e.g. PC is 0xfffffffc*/
1792 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1798 return target
->type
->read_buffer(target
, address
, size
, buffer
);
1801 static int target_read_buffer_default(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1803 int retval
= ERROR_OK
;
1805 if (((address
% 2) == 0) && (size
== 2))
1806 return target_read_memory(target
, address
, 2, 1, buffer
);
1808 /* handle unaligned head bytes */
1810 uint32_t unaligned
= 4 - (address
% 4);
1812 if (unaligned
> size
)
1815 retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
);
1816 if (retval
!= ERROR_OK
)
1819 buffer
+= unaligned
;
1820 address
+= unaligned
;
1824 /* handle aligned words */
1826 int aligned
= size
- (size
% 4);
1828 retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
);
1829 if (retval
!= ERROR_OK
)
1837 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1839 int aligned
= size
- (size
% 2);
1840 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1841 if (retval
!= ERROR_OK
)
1848 /* handle tail writes of less than 4 bytes */
1850 retval
= target_read_memory(target
, address
, 1, size
, buffer
);
1851 if (retval
!= ERROR_OK
)
1858 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1863 uint32_t checksum
= 0;
1864 if (!target_was_examined(target
)) {
1865 LOG_ERROR("Target not examined yet");
1869 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
1870 if (retval
!= ERROR_OK
) {
1871 buffer
= malloc(size
);
1872 if (buffer
== NULL
) {
1873 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1874 return ERROR_COMMAND_SYNTAX_ERROR
;
1876 retval
= target_read_buffer(target
, address
, size
, buffer
);
1877 if (retval
!= ERROR_OK
) {
1882 /* convert to target endianness */
1883 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
1884 uint32_t target_data
;
1885 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1886 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1889 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1898 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1901 if (!target_was_examined(target
)) {
1902 LOG_ERROR("Target not examined yet");
1906 if (target
->type
->blank_check_memory
== 0)
1907 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1909 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1914 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1916 uint8_t value_buf
[4];
1917 if (!target_was_examined(target
)) {
1918 LOG_ERROR("Target not examined yet");
1922 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1924 if (retval
== ERROR_OK
) {
1925 *value
= target_buffer_get_u32(target
, value_buf
);
1926 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1931 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1938 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
1940 uint8_t value_buf
[2];
1941 if (!target_was_examined(target
)) {
1942 LOG_ERROR("Target not examined yet");
1946 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1948 if (retval
== ERROR_OK
) {
1949 *value
= target_buffer_get_u16(target
, value_buf
);
1950 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1955 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1962 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
1964 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1965 if (!target_was_examined(target
)) {
1966 LOG_ERROR("Target not examined yet");
1970 if (retval
== ERROR_OK
) {
1971 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1976 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1983 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
1986 uint8_t value_buf
[4];
1987 if (!target_was_examined(target
)) {
1988 LOG_ERROR("Target not examined yet");
1992 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1996 target_buffer_set_u32(target
, value_buf
, value
);
1997 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
1998 if (retval
!= ERROR_OK
)
1999 LOG_DEBUG("failed: %i", retval
);
2004 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
2007 uint8_t value_buf
[2];
2008 if (!target_was_examined(target
)) {
2009 LOG_ERROR("Target not examined yet");
2013 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
2017 target_buffer_set_u16(target
, value_buf
, value
);
2018 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2019 if (retval
!= ERROR_OK
)
2020 LOG_DEBUG("failed: %i", retval
);
2025 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
2028 if (!target_was_examined(target
)) {
2029 LOG_ERROR("Target not examined yet");
2033 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
2036 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2037 if (retval
!= ERROR_OK
)
2038 LOG_DEBUG("failed: %i", retval
);
2043 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2045 struct target
*target
= get_target(name
);
2046 if (target
== NULL
) {
2047 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2050 if (!target
->tap
->enabled
) {
2051 LOG_USER("Target: TAP %s is disabled, "
2052 "can't be the current target\n",
2053 target
->tap
->dotted_name
);
2057 cmd_ctx
->current_target
= target
->target_number
;
2062 COMMAND_HANDLER(handle_targets_command
)
2064 int retval
= ERROR_OK
;
2065 if (CMD_ARGC
== 1) {
2066 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2067 if (retval
== ERROR_OK
) {
2073 struct target
*target
= all_targets
;
2074 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2075 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2080 if (target
->tap
->enabled
)
2081 state
= target_state_name(target
);
2083 state
= "tap-disabled";
2085 if (CMD_CTX
->current_target
== target
->target_number
)
2088 /* keep columns lined up to match the headers above */
2089 command_print(CMD_CTX
,
2090 "%2d%c %-18s %-10s %-6s %-18s %s",
2091 target
->target_number
,
2093 target_name(target
),
2094 target_type_name(target
),
2095 Jim_Nvp_value2name_simple(nvp_target_endian
,
2096 target
->endianness
)->name
,
2097 target
->tap
->dotted_name
,
2099 target
= target
->next
;
2105 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2107 static int powerDropout
;
2108 static int srstAsserted
;
2110 static int runPowerRestore
;
2111 static int runPowerDropout
;
2112 static int runSrstAsserted
;
2113 static int runSrstDeasserted
;
2115 static int sense_handler(void)
2117 static int prevSrstAsserted
;
2118 static int prevPowerdropout
;
2120 int retval
= jtag_power_dropout(&powerDropout
);
2121 if (retval
!= ERROR_OK
)
2125 powerRestored
= prevPowerdropout
&& !powerDropout
;
2127 runPowerRestore
= 1;
2129 long long current
= timeval_ms();
2130 static long long lastPower
;
2131 int waitMore
= lastPower
+ 2000 > current
;
2132 if (powerDropout
&& !waitMore
) {
2133 runPowerDropout
= 1;
2134 lastPower
= current
;
2137 retval
= jtag_srst_asserted(&srstAsserted
);
2138 if (retval
!= ERROR_OK
)
2142 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2144 static long long lastSrst
;
2145 waitMore
= lastSrst
+ 2000 > current
;
2146 if (srstDeasserted
&& !waitMore
) {
2147 runSrstDeasserted
= 1;
2151 if (!prevSrstAsserted
&& srstAsserted
)
2152 runSrstAsserted
= 1;
2154 prevSrstAsserted
= srstAsserted
;
2155 prevPowerdropout
= powerDropout
;
2157 if (srstDeasserted
|| powerRestored
) {
2158 /* Other than logging the event we can't do anything here.
2159 * Issuing a reset is a particularly bad idea as we might
2160 * be inside a reset already.
2167 static int backoff_times
;
2168 static int backoff_count
;
2170 /* process target state changes */
2171 static int handle_target(void *priv
)
2173 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2174 int retval
= ERROR_OK
;
2176 if (!is_jtag_poll_safe()) {
2177 /* polling is disabled currently */
2181 /* we do not want to recurse here... */
2182 static int recursive
;
2186 /* danger! running these procedures can trigger srst assertions and power dropouts.
2187 * We need to avoid an infinite loop/recursion here and we do that by
2188 * clearing the flags after running these events.
2190 int did_something
= 0;
2191 if (runSrstAsserted
) {
2192 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2193 Jim_Eval(interp
, "srst_asserted");
2196 if (runSrstDeasserted
) {
2197 Jim_Eval(interp
, "srst_deasserted");
2200 if (runPowerDropout
) {
2201 LOG_INFO("Power dropout detected, running power_dropout proc.");
2202 Jim_Eval(interp
, "power_dropout");
2205 if (runPowerRestore
) {
2206 Jim_Eval(interp
, "power_restore");
2210 if (did_something
) {
2211 /* clear detect flags */
2215 /* clear action flags */
2217 runSrstAsserted
= 0;
2218 runSrstDeasserted
= 0;
2219 runPowerRestore
= 0;
2220 runPowerDropout
= 0;
2225 if (backoff_times
> backoff_count
) {
2226 /* do not poll this time as we failed previously */
2232 /* Poll targets for state changes unless that's globally disabled.
2233 * Skip targets that are currently disabled.
2235 for (struct target
*target
= all_targets
;
2236 is_jtag_poll_safe() && target
;
2237 target
= target
->next
) {
2238 if (!target
->tap
->enabled
)
2241 /* only poll target if we've got power and srst isn't asserted */
2242 if (!powerDropout
&& !srstAsserted
) {
2243 /* polling may fail silently until the target has been examined */
2244 retval
= target_poll(target
);
2245 if (retval
!= ERROR_OK
) {
2246 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2247 if (backoff_times
* polling_interval
< 5000) {
2251 LOG_USER("Polling target %s failed, GDB will be halted. Polling again in %dms",
2252 target_name(target
),
2253 backoff_times
* polling_interval
);
2255 /* Tell GDB to halt the debugger. This allows the user to
2256 * run monitor commands to handle the situation.
2258 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2261 /* Since we succeeded, we reset backoff count */
2262 if (backoff_times
> 0)
2263 LOG_USER("Polling target %s succeeded again", target_name(target
));
2271 COMMAND_HANDLER(handle_reg_command
)
2273 struct target
*target
;
2274 struct reg
*reg
= NULL
;
2280 target
= get_current_target(CMD_CTX
);
2282 /* list all available registers for the current target */
2283 if (CMD_ARGC
== 0) {
2284 struct reg_cache
*cache
= target
->reg_cache
;
2290 command_print(CMD_CTX
, "===== %s", cache
->name
);
2292 for (i
= 0, reg
= cache
->reg_list
;
2293 i
< cache
->num_regs
;
2294 i
++, reg
++, count
++) {
2295 /* only print cached values if they are valid */
2297 value
= buf_to_str(reg
->value
,
2299 command_print(CMD_CTX
,
2300 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2308 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2313 cache
= cache
->next
;
2319 /* access a single register by its ordinal number */
2320 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2322 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2324 struct reg_cache
*cache
= target
->reg_cache
;
2328 for (i
= 0; i
< cache
->num_regs
; i
++) {
2329 if (count
++ == num
) {
2330 reg
= &cache
->reg_list
[i
];
2336 cache
= cache
->next
;
2340 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2341 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2345 /* access a single register by its name */
2346 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2349 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2354 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2356 /* display a register */
2357 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2358 && (CMD_ARGV
[1][0] <= '9')))) {
2359 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2362 if (reg
->valid
== 0)
2363 reg
->type
->get(reg
);
2364 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2365 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2370 /* set register value */
2371 if (CMD_ARGC
== 2) {
2372 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2375 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2377 reg
->type
->set(reg
, buf
);
2379 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2380 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2388 return ERROR_COMMAND_SYNTAX_ERROR
;
2391 COMMAND_HANDLER(handle_poll_command
)
2393 int retval
= ERROR_OK
;
2394 struct target
*target
= get_current_target(CMD_CTX
);
2396 if (CMD_ARGC
== 0) {
2397 command_print(CMD_CTX
, "background polling: %s",
2398 jtag_poll_get_enabled() ? "on" : "off");
2399 command_print(CMD_CTX
, "TAP: %s (%s)",
2400 target
->tap
->dotted_name
,
2401 target
->tap
->enabled
? "enabled" : "disabled");
2402 if (!target
->tap
->enabled
)
2404 retval
= target_poll(target
);
2405 if (retval
!= ERROR_OK
)
2407 retval
= target_arch_state(target
);
2408 if (retval
!= ERROR_OK
)
2410 } else if (CMD_ARGC
== 1) {
2412 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2413 jtag_poll_set_enabled(enable
);
2415 return ERROR_COMMAND_SYNTAX_ERROR
;
2420 COMMAND_HANDLER(handle_wait_halt_command
)
2423 return ERROR_COMMAND_SYNTAX_ERROR
;
2426 if (1 == CMD_ARGC
) {
2427 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2428 if (ERROR_OK
!= retval
)
2429 return ERROR_COMMAND_SYNTAX_ERROR
;
2430 /* convert seconds (given) to milliseconds (needed) */
2434 struct target
*target
= get_current_target(CMD_CTX
);
2435 return target_wait_state(target
, TARGET_HALTED
, ms
);
2438 /* wait for target state to change. The trick here is to have a low
2439 * latency for short waits and not to suck up all the CPU time
2442 * After 500ms, keep_alive() is invoked
2444 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2447 long long then
= 0, cur
;
2451 retval
= target_poll(target
);
2452 if (retval
!= ERROR_OK
)
2454 if (target
->state
== state
)
2459 then
= timeval_ms();
2460 LOG_DEBUG("waiting for target %s...",
2461 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2467 if ((cur
-then
) > ms
) {
2468 LOG_ERROR("timed out while waiting for target %s",
2469 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2477 COMMAND_HANDLER(handle_halt_command
)
2481 struct target
*target
= get_current_target(CMD_CTX
);
2482 int retval
= target_halt(target
);
2483 if (ERROR_OK
!= retval
)
2486 if (CMD_ARGC
== 1) {
2487 unsigned wait_local
;
2488 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2489 if (ERROR_OK
!= retval
)
2490 return ERROR_COMMAND_SYNTAX_ERROR
;
2495 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2498 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2500 struct target
*target
= get_current_target(CMD_CTX
);
2502 LOG_USER("requesting target halt and executing a soft reset");
2504 target
->type
->soft_reset_halt(target
);
2509 COMMAND_HANDLER(handle_reset_command
)
2512 return ERROR_COMMAND_SYNTAX_ERROR
;
2514 enum target_reset_mode reset_mode
= RESET_RUN
;
2515 if (CMD_ARGC
== 1) {
2517 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2518 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2519 return ERROR_COMMAND_SYNTAX_ERROR
;
2520 reset_mode
= n
->value
;
2523 /* reset *all* targets */
2524 return target_process_reset(CMD_CTX
, reset_mode
);
2528 COMMAND_HANDLER(handle_resume_command
)
2532 return ERROR_COMMAND_SYNTAX_ERROR
;
2534 struct target
*target
= get_current_target(CMD_CTX
);
2536 /* with no CMD_ARGV, resume from current pc, addr = 0,
2537 * with one arguments, addr = CMD_ARGV[0],
2538 * handle breakpoints, not debugging */
2540 if (CMD_ARGC
== 1) {
2541 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2545 return target_resume(target
, current
, addr
, 1, 0);
2548 COMMAND_HANDLER(handle_step_command
)
2551 return ERROR_COMMAND_SYNTAX_ERROR
;
2555 /* with no CMD_ARGV, step from current pc, addr = 0,
2556 * with one argument addr = CMD_ARGV[0],
2557 * handle breakpoints, debugging */
2560 if (CMD_ARGC
== 1) {
2561 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2565 struct target
*target
= get_current_target(CMD_CTX
);
2567 return target
->type
->step(target
, current_pc
, addr
, 1);
2570 static void handle_md_output(struct command_context
*cmd_ctx
,
2571 struct target
*target
, uint32_t address
, unsigned size
,
2572 unsigned count
, const uint8_t *buffer
)
2574 const unsigned line_bytecnt
= 32;
2575 unsigned line_modulo
= line_bytecnt
/ size
;
2577 char output
[line_bytecnt
* 4 + 1];
2578 unsigned output_len
= 0;
2580 const char *value_fmt
;
2583 value_fmt
= "%8.8x ";
2586 value_fmt
= "%4.4x ";
2589 value_fmt
= "%2.2x ";
2592 /* "can't happen", caller checked */
2593 LOG_ERROR("invalid memory read size: %u", size
);
2597 for (unsigned i
= 0; i
< count
; i
++) {
2598 if (i
% line_modulo
== 0) {
2599 output_len
+= snprintf(output
+ output_len
,
2600 sizeof(output
) - output_len
,
2602 (unsigned)(address
+ (i
*size
)));
2606 const uint8_t *value_ptr
= buffer
+ i
* size
;
2609 value
= target_buffer_get_u32(target
, value_ptr
);
2612 value
= target_buffer_get_u16(target
, value_ptr
);
2617 output_len
+= snprintf(output
+ output_len
,
2618 sizeof(output
) - output_len
,
2621 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
2622 command_print(cmd_ctx
, "%s", output
);
2628 COMMAND_HANDLER(handle_md_command
)
2631 return ERROR_COMMAND_SYNTAX_ERROR
;
2634 switch (CMD_NAME
[2]) {
2645 return ERROR_COMMAND_SYNTAX_ERROR
;
2648 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2649 int (*fn
)(struct target
*target
,
2650 uint32_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
2654 fn
= target_read_phys_memory
;
2656 fn
= target_read_memory
;
2657 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2658 return ERROR_COMMAND_SYNTAX_ERROR
;
2661 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2665 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2667 uint8_t *buffer
= calloc(count
, size
);
2669 struct target
*target
= get_current_target(CMD_CTX
);
2670 int retval
= fn(target
, address
, size
, count
, buffer
);
2671 if (ERROR_OK
== retval
)
2672 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2679 typedef int (*target_write_fn
)(struct target
*target
,
2680 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
2682 static int target_write_memory_fast(struct target
*target
,
2683 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
2685 return target_write_buffer(target
, address
, size
* count
, buffer
);
2688 static int target_fill_mem(struct target
*target
,
2697 /* We have to write in reasonably large chunks to be able
2698 * to fill large memory areas with any sane speed */
2699 const unsigned chunk_size
= 16384;
2700 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
2701 if (target_buf
== NULL
) {
2702 LOG_ERROR("Out of memory");
2706 for (unsigned i
= 0; i
< chunk_size
; i
++) {
2707 switch (data_size
) {
2709 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
2712 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
2715 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
2722 int retval
= ERROR_OK
;
2724 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
2727 if (current
> chunk_size
)
2728 current
= chunk_size
;
2729 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
2730 if (retval
!= ERROR_OK
)
2732 /* avoid GDB timeouts */
2741 COMMAND_HANDLER(handle_mw_command
)
2744 return ERROR_COMMAND_SYNTAX_ERROR
;
2745 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2750 fn
= target_write_phys_memory
;
2752 fn
= target_write_memory_fast
;
2753 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2754 return ERROR_COMMAND_SYNTAX_ERROR
;
2757 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2760 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2764 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2766 struct target
*target
= get_current_target(CMD_CTX
);
2768 switch (CMD_NAME
[2]) {
2779 return ERROR_COMMAND_SYNTAX_ERROR
;
2782 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
2785 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2786 uint32_t *min_address
, uint32_t *max_address
)
2788 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2789 return ERROR_COMMAND_SYNTAX_ERROR
;
2791 /* a base address isn't always necessary,
2792 * default to 0x0 (i.e. don't relocate) */
2793 if (CMD_ARGC
>= 2) {
2795 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2796 image
->base_address
= addr
;
2797 image
->base_address_set
= 1;
2799 image
->base_address_set
= 0;
2801 image
->start_address_set
= 0;
2804 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2805 if (CMD_ARGC
== 5) {
2806 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2807 /* use size (given) to find max (required) */
2808 *max_address
+= *min_address
;
2811 if (*min_address
> *max_address
)
2812 return ERROR_COMMAND_SYNTAX_ERROR
;
2817 COMMAND_HANDLER(handle_load_image_command
)
2821 uint32_t image_size
;
2822 uint32_t min_address
= 0;
2823 uint32_t max_address
= 0xffffffff;
2827 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2828 &image
, &min_address
, &max_address
);
2829 if (ERROR_OK
!= retval
)
2832 struct target
*target
= get_current_target(CMD_CTX
);
2834 struct duration bench
;
2835 duration_start(&bench
);
2837 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2842 for (i
= 0; i
< image
.num_sections
; i
++) {
2843 buffer
= malloc(image
.sections
[i
].size
);
2844 if (buffer
== NULL
) {
2845 command_print(CMD_CTX
,
2846 "error allocating buffer for section (%d bytes)",
2847 (int)(image
.sections
[i
].size
));
2851 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
2852 if (retval
!= ERROR_OK
) {
2857 uint32_t offset
= 0;
2858 uint32_t length
= buf_cnt
;
2860 /* DANGER!!! beware of unsigned comparision here!!! */
2862 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
2863 (image
.sections
[i
].base_address
< max_address
)) {
2865 if (image
.sections
[i
].base_address
< min_address
) {
2866 /* clip addresses below */
2867 offset
+= min_address
-image
.sections
[i
].base_address
;
2871 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2872 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2874 retval
= target_write_buffer(target
,
2875 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
2876 if (retval
!= ERROR_OK
) {
2880 image_size
+= length
;
2881 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2882 (unsigned int)length
,
2883 image
.sections
[i
].base_address
+ offset
);
2889 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
2890 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2891 "in %fs (%0.3f KiB/s)", image_size
,
2892 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2895 image_close(&image
);
2901 COMMAND_HANDLER(handle_dump_image_command
)
2903 struct fileio fileio
;
2905 int retval
, retvaltemp
;
2906 uint32_t address
, size
;
2907 struct duration bench
;
2908 struct target
*target
= get_current_target(CMD_CTX
);
2911 return ERROR_COMMAND_SYNTAX_ERROR
;
2913 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2914 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2916 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
2917 buffer
= malloc(buf_size
);
2921 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
2922 if (retval
!= ERROR_OK
) {
2927 duration_start(&bench
);
2930 size_t size_written
;
2931 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
2932 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2933 if (retval
!= ERROR_OK
)
2936 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2937 if (retval
!= ERROR_OK
)
2940 size
-= this_run_size
;
2941 address
+= this_run_size
;
2946 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
2948 retval
= fileio_size(&fileio
, &filesize
);
2949 if (retval
!= ERROR_OK
)
2951 command_print(CMD_CTX
,
2952 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize
,
2953 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
2956 retvaltemp
= fileio_close(&fileio
);
2957 if (retvaltemp
!= ERROR_OK
)
2963 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
2967 uint32_t image_size
;
2970 uint32_t checksum
= 0;
2971 uint32_t mem_checksum
= 0;
2975 struct target
*target
= get_current_target(CMD_CTX
);
2978 return ERROR_COMMAND_SYNTAX_ERROR
;
2981 LOG_ERROR("no target selected");
2985 struct duration bench
;
2986 duration_start(&bench
);
2988 if (CMD_ARGC
>= 2) {
2990 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2991 image
.base_address
= addr
;
2992 image
.base_address_set
= 1;
2994 image
.base_address_set
= 0;
2995 image
.base_address
= 0x0;
2998 image
.start_address_set
= 0;
3000 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3001 if (retval
!= ERROR_OK
)
3007 for (i
= 0; i
< image
.num_sections
; i
++) {
3008 buffer
= malloc(image
.sections
[i
].size
);
3009 if (buffer
== NULL
) {
3010 command_print(CMD_CTX
,
3011 "error allocating buffer for section (%d bytes)",
3012 (int)(image
.sections
[i
].size
));
3015 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3016 if (retval
!= ERROR_OK
) {
3022 /* calculate checksum of image */
3023 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3024 if (retval
!= ERROR_OK
) {
3029 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3030 if (retval
!= ERROR_OK
) {
3035 if (checksum
!= mem_checksum
) {
3036 /* failed crc checksum, fall back to a binary compare */
3040 LOG_ERROR("checksum mismatch - attempting binary compare");
3042 data
= (uint8_t *)malloc(buf_cnt
);
3044 /* Can we use 32bit word accesses? */
3046 int count
= buf_cnt
;
3047 if ((count
% 4) == 0) {
3051 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3052 if (retval
== ERROR_OK
) {
3054 for (t
= 0; t
< buf_cnt
; t
++) {
3055 if (data
[t
] != buffer
[t
]) {
3056 command_print(CMD_CTX
,
3057 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3059 (unsigned)(t
+ image
.sections
[i
].base_address
),
3062 if (diffs
++ >= 127) {
3063 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3075 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
3076 image
.sections
[i
].base_address
,
3081 image_size
+= buf_cnt
;
3084 command_print(CMD_CTX
, "No more differences found.");
3087 retval
= ERROR_FAIL
;
3088 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3089 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3090 "in %fs (%0.3f KiB/s)", image_size
,
3091 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3094 image_close(&image
);
3099 COMMAND_HANDLER(handle_verify_image_command
)
3101 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
3104 COMMAND_HANDLER(handle_test_image_command
)
3106 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
3109 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3111 struct target
*target
= get_current_target(cmd_ctx
);
3112 struct breakpoint
*breakpoint
= target
->breakpoints
;
3113 while (breakpoint
) {
3114 if (breakpoint
->type
== BKPT_SOFT
) {
3115 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3116 breakpoint
->length
, 16);
3117 command_print(cmd_ctx
, "IVA breakpoint: 0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
3118 breakpoint
->address
,
3120 breakpoint
->set
, buf
);
3123 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3124 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3126 breakpoint
->length
, breakpoint
->set
);
3127 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3128 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3129 breakpoint
->address
,
3130 breakpoint
->length
, breakpoint
->set
);
3131 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3134 command_print(cmd_ctx
, "Breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3135 breakpoint
->address
,
3136 breakpoint
->length
, breakpoint
->set
);
3139 breakpoint
= breakpoint
->next
;
3144 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
3145 uint32_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3147 struct target
*target
= get_current_target(cmd_ctx
);
3150 int retval
= breakpoint_add(target
, addr
, length
, hw
);
3151 if (ERROR_OK
== retval
)
3152 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
3154 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3157 } else if (addr
== 0) {
3158 int retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3159 if (ERROR_OK
== retval
)
3160 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3162 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3166 int retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3167 if (ERROR_OK
== retval
)
3168 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3170 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3177 COMMAND_HANDLER(handle_bp_command
)
3186 return handle_bp_command_list(CMD_CTX
);
3190 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3191 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3192 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3195 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3197 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3199 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3202 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3203 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
3205 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3206 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3208 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3213 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3214 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3215 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3216 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3219 return ERROR_COMMAND_SYNTAX_ERROR
;
3223 COMMAND_HANDLER(handle_rbp_command
)
3226 return ERROR_COMMAND_SYNTAX_ERROR
;
3229 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3231 struct target
*target
= get_current_target(CMD_CTX
);
3232 breakpoint_remove(target
, addr
);
3237 COMMAND_HANDLER(handle_wp_command
)
3239 struct target
*target
= get_current_target(CMD_CTX
);
3241 if (CMD_ARGC
== 0) {
3242 struct watchpoint
*watchpoint
= target
->watchpoints
;
3244 while (watchpoint
) {
3245 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
3246 ", len: 0x%8.8" PRIx32
3247 ", r/w/a: %i, value: 0x%8.8" PRIx32
3248 ", mask: 0x%8.8" PRIx32
,
3249 watchpoint
->address
,
3251 (int)watchpoint
->rw
,
3254 watchpoint
= watchpoint
->next
;
3259 enum watchpoint_rw type
= WPT_ACCESS
;
3261 uint32_t length
= 0;
3262 uint32_t data_value
= 0x0;
3263 uint32_t data_mask
= 0xffffffff;
3267 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3270 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3273 switch (CMD_ARGV
[2][0]) {
3284 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3285 return ERROR_COMMAND_SYNTAX_ERROR
;
3289 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3290 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3294 return ERROR_COMMAND_SYNTAX_ERROR
;
3297 int retval
= watchpoint_add(target
, addr
, length
, type
,
3298 data_value
, data_mask
);
3299 if (ERROR_OK
!= retval
)
3300 LOG_ERROR("Failure setting watchpoints");
3305 COMMAND_HANDLER(handle_rwp_command
)
3308 return ERROR_COMMAND_SYNTAX_ERROR
;
3311 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3313 struct target
*target
= get_current_target(CMD_CTX
);
3314 watchpoint_remove(target
, addr
);
3320 * Translate a virtual address to a physical address.
3322 * The low-level target implementation must have logged a detailed error
3323 * which is forwarded to telnet/GDB session.
3325 COMMAND_HANDLER(handle_virt2phys_command
)
3328 return ERROR_COMMAND_SYNTAX_ERROR
;
3331 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
3334 struct target
*target
= get_current_target(CMD_CTX
);
3335 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3336 if (retval
== ERROR_OK
)
3337 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
3342 static void writeData(FILE *f
, const void *data
, size_t len
)
3344 size_t written
= fwrite(data
, 1, len
, f
);
3346 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3349 static void writeLong(FILE *f
, int l
)
3352 for (i
= 0; i
< 4; i
++) {
3353 char c
= (l
>> (i
*8))&0xff;
3354 writeData(f
, &c
, 1);
3359 static void writeString(FILE *f
, char *s
)
3361 writeData(f
, s
, strlen(s
));
3364 /* Dump a gmon.out histogram file. */
3365 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
)
3368 FILE *f
= fopen(filename
, "w");
3371 writeString(f
, "gmon");
3372 writeLong(f
, 0x00000001); /* Version */
3373 writeLong(f
, 0); /* padding */
3374 writeLong(f
, 0); /* padding */
3375 writeLong(f
, 0); /* padding */
3377 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3378 writeData(f
, &zero
, 1);
3380 /* figure out bucket size */
3381 uint32_t min
= samples
[0];
3382 uint32_t max
= samples
[0];
3383 for (i
= 0; i
< sampleNum
; i
++) {
3384 if (min
> samples
[i
])
3386 if (max
< samples
[i
])
3390 int addressSpace
= (max
- min
+ 1);
3391 assert(addressSpace
>= 2);
3393 static const uint32_t maxBuckets
= 16 * 1024; /* maximum buckets. */
3394 uint32_t length
= addressSpace
;
3395 if (length
> maxBuckets
)
3396 length
= maxBuckets
;
3397 int *buckets
= malloc(sizeof(int)*length
);
3398 if (buckets
== NULL
) {
3402 memset(buckets
, 0, sizeof(int) * length
);
3403 for (i
= 0; i
< sampleNum
; i
++) {
3404 uint32_t address
= samples
[i
];
3405 long long a
= address
- min
;
3406 long long b
= length
- 1;
3407 long long c
= addressSpace
- 1;
3408 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
3412 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3413 writeLong(f
, min
); /* low_pc */
3414 writeLong(f
, max
); /* high_pc */
3415 writeLong(f
, length
); /* # of samples */
3416 writeLong(f
, 100); /* KLUDGE! We lie, ca. 100Hz best case. */
3417 writeString(f
, "seconds");
3418 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3419 writeData(f
, &zero
, 1);
3420 writeString(f
, "s");
3422 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3424 char *data
= malloc(2 * length
);
3426 for (i
= 0; i
< length
; i
++) {
3431 data
[i
* 2] = val
&0xff;
3432 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
3435 writeData(f
, data
, length
* 2);
3443 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3444 * which will be used as a random sampling of PC */
3445 COMMAND_HANDLER(handle_profile_command
)
3447 struct target
*target
= get_current_target(CMD_CTX
);
3448 struct timeval timeout
, now
;
3450 gettimeofday(&timeout
, NULL
);
3452 return ERROR_COMMAND_SYNTAX_ERROR
;
3454 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], offset
);
3456 timeval_add_time(&timeout
, offset
, 0);
3459 * @todo: Some cores let us sample the PC without the
3460 * annoying halt/resume step; for example, ARMv7 PCSR.
3461 * Provide a way to use that more efficient mechanism.
3464 command_print(CMD_CTX
, "Starting profiling. Halting and resuming the target as often as we can...");
3466 static const int maxSample
= 10000;
3467 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
3468 if (samples
== NULL
)
3472 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3473 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
3475 int retval
= ERROR_OK
;
3477 target_poll(target
);
3478 if (target
->state
== TARGET_HALTED
) {
3479 uint32_t t
= *((uint32_t *)reg
->value
);
3480 samples
[numSamples
++] = t
;
3481 /* current pc, addr = 0, do not handle breakpoints, not debugging */
3482 retval
= target_resume(target
, 1, 0, 0, 0);
3483 target_poll(target
);
3484 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3485 } else if (target
->state
== TARGET_RUNNING
) {
3486 /* We want to quickly sample the PC. */
3487 retval
= target_halt(target
);
3488 if (retval
!= ERROR_OK
) {
3493 command_print(CMD_CTX
, "Target not halted or running");
3497 if (retval
!= ERROR_OK
)
3500 gettimeofday(&now
, NULL
);
3501 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
)
3502 && (now
.tv_usec
>= timeout
.tv_usec
))) {
3503 command_print(CMD_CTX
, "Profiling completed. %d samples.", numSamples
);
3504 retval
= target_poll(target
);
3505 if (retval
!= ERROR_OK
) {
3509 if (target
->state
== TARGET_HALTED
) {
3510 /* current pc, addr = 0, do not handle
3511 * breakpoints, not debugging */
3512 target_resume(target
, 1, 0, 0, 0);
3514 retval
= target_poll(target
);
3515 if (retval
!= ERROR_OK
) {
3519 writeGmon(samples
, numSamples
, CMD_ARGV
[1]);
3520 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3529 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
3532 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3535 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3539 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3540 valObjPtr
= Jim_NewIntObj(interp
, val
);
3541 if (!nameObjPtr
|| !valObjPtr
) {
3546 Jim_IncrRefCount(nameObjPtr
);
3547 Jim_IncrRefCount(valObjPtr
);
3548 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3549 Jim_DecrRefCount(interp
, nameObjPtr
);
3550 Jim_DecrRefCount(interp
, valObjPtr
);
3552 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3556 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3558 struct command_context
*context
;
3559 struct target
*target
;
3561 context
= current_command_context(interp
);
3562 assert(context
!= NULL
);
3564 target
= get_current_target(context
);
3565 if (target
== NULL
) {
3566 LOG_ERROR("mem2array: no current target");
3570 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
3573 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3581 const char *varname
;
3585 /* argv[1] = name of array to receive the data
3586 * argv[2] = desired width
3587 * argv[3] = memory address
3588 * argv[4] = count of times to read
3591 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3594 varname
= Jim_GetString(argv
[0], &len
);
3595 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3597 e
= Jim_GetLong(interp
, argv
[1], &l
);
3602 e
= Jim_GetLong(interp
, argv
[2], &l
);
3606 e
= Jim_GetLong(interp
, argv
[3], &l
);
3621 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3622 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3626 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3627 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3630 if ((addr
+ (len
* width
)) < addr
) {
3631 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3632 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3635 /* absurd transfer size? */
3637 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3638 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3643 ((width
== 2) && ((addr
& 1) == 0)) ||
3644 ((width
== 4) && ((addr
& 3) == 0))) {
3648 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3649 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3652 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3661 size_t buffersize
= 4096;
3662 uint8_t *buffer
= malloc(buffersize
);
3669 /* Slurp... in buffer size chunks */
3671 count
= len
; /* in objects.. */
3672 if (count
> (buffersize
/ width
))
3673 count
= (buffersize
/ width
);
3675 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3676 if (retval
!= ERROR_OK
) {
3678 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3682 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3683 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3687 v
= 0; /* shut up gcc */
3688 for (i
= 0; i
< count
; i
++, n
++) {
3691 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3694 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3697 v
= buffer
[i
] & 0x0ff;
3700 new_int_array_element(interp
, varname
, n
, v
);
3708 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3713 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
3716 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3720 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3724 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3730 Jim_IncrRefCount(nameObjPtr
);
3731 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3732 Jim_DecrRefCount(interp
, nameObjPtr
);
3734 if (valObjPtr
== NULL
)
3737 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3738 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3743 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3745 struct command_context
*context
;
3746 struct target
*target
;
3748 context
= current_command_context(interp
);
3749 assert(context
!= NULL
);
3751 target
= get_current_target(context
);
3752 if (target
== NULL
) {
3753 LOG_ERROR("array2mem: no current target");
3757 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
3760 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
3761 int argc
, Jim_Obj
*const *argv
)
3769 const char *varname
;
3773 /* argv[1] = name of array to get the data
3774 * argv[2] = desired width
3775 * argv[3] = memory address
3776 * argv[4] = count to write
3779 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3782 varname
= Jim_GetString(argv
[0], &len
);
3783 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3785 e
= Jim_GetLong(interp
, argv
[1], &l
);
3790 e
= Jim_GetLong(interp
, argv
[2], &l
);
3794 e
= Jim_GetLong(interp
, argv
[3], &l
);
3809 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3810 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3811 "Invalid width param, must be 8/16/32", NULL
);
3815 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3816 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3817 "array2mem: zero width read?", NULL
);
3820 if ((addr
+ (len
* width
)) < addr
) {
3821 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3822 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3823 "array2mem: addr + len - wraps to zero?", NULL
);
3826 /* absurd transfer size? */
3828 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3829 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3830 "array2mem: absurd > 64K item request", NULL
);
3835 ((width
== 2) && ((addr
& 1) == 0)) ||
3836 ((width
== 4) && ((addr
& 3) == 0))) {
3840 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3841 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3844 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3855 size_t buffersize
= 4096;
3856 uint8_t *buffer
= malloc(buffersize
);
3861 /* Slurp... in buffer size chunks */
3863 count
= len
; /* in objects.. */
3864 if (count
> (buffersize
/ width
))
3865 count
= (buffersize
/ width
);
3867 v
= 0; /* shut up gcc */
3868 for (i
= 0; i
< count
; i
++, n
++) {
3869 get_int_array_element(interp
, varname
, n
, &v
);
3872 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
3875 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
3878 buffer
[i
] = v
& 0x0ff;
3884 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3885 if (retval
!= ERROR_OK
) {
3887 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3891 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3892 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3900 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3905 /* FIX? should we propagate errors here rather than printing them
3908 void target_handle_event(struct target
*target
, enum target_event e
)
3910 struct target_event_action
*teap
;
3912 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3913 if (teap
->event
== e
) {
3914 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3915 target
->target_number
,
3916 target_name(target
),
3917 target_type_name(target
),
3919 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3920 Jim_GetString(teap
->body
, NULL
));
3921 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
) {
3922 Jim_MakeErrorMessage(teap
->interp
);
3923 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
3930 * Returns true only if the target has a handler for the specified event.
3932 bool target_has_event_action(struct target
*target
, enum target_event event
)
3934 struct target_event_action
*teap
;
3936 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3937 if (teap
->event
== event
)
3943 enum target_cfg_param
{
3946 TCFG_WORK_AREA_VIRT
,
3947 TCFG_WORK_AREA_PHYS
,
3948 TCFG_WORK_AREA_SIZE
,
3949 TCFG_WORK_AREA_BACKUP
,
3953 TCFG_CHAIN_POSITION
,
3958 static Jim_Nvp nvp_config_opts
[] = {
3959 { .name
= "-type", .value
= TCFG_TYPE
},
3960 { .name
= "-event", .value
= TCFG_EVENT
},
3961 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3962 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3963 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3964 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3965 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3966 { .name
= "-variant", .value
= TCFG_VARIANT
},
3967 { .name
= "-coreid", .value
= TCFG_COREID
},
3968 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3969 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
3970 { .name
= "-rtos", .value
= TCFG_RTOS
},
3971 { .name
= NULL
, .value
= -1 }
3974 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
3982 /* parse config or cget options ... */
3983 while (goi
->argc
> 0) {
3984 Jim_SetEmptyResult(goi
->interp
);
3985 /* Jim_GetOpt_Debug(goi); */
3987 if (target
->type
->target_jim_configure
) {
3988 /* target defines a configure function */
3989 /* target gets first dibs on parameters */
3990 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
3999 /* otherwise we 'continue' below */
4001 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
4003 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
4009 if (goi
->isconfigure
) {
4010 Jim_SetResultFormatted(goi
->interp
,
4011 "not settable: %s", n
->name
);
4015 if (goi
->argc
!= 0) {
4016 Jim_WrongNumArgs(goi
->interp
,
4017 goi
->argc
, goi
->argv
,
4022 Jim_SetResultString(goi
->interp
,
4023 target_type_name(target
), -1);
4027 if (goi
->argc
== 0) {
4028 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
4032 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
4034 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
4038 if (goi
->isconfigure
) {
4039 if (goi
->argc
!= 1) {
4040 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
4044 if (goi
->argc
!= 0) {
4045 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
4051 struct target_event_action
*teap
;
4053 teap
= target
->event_action
;
4054 /* replace existing? */
4056 if (teap
->event
== (enum target_event
)n
->value
)
4061 if (goi
->isconfigure
) {
4062 bool replace
= true;
4065 teap
= calloc(1, sizeof(*teap
));
4068 teap
->event
= n
->value
;
4069 teap
->interp
= goi
->interp
;
4070 Jim_GetOpt_Obj(goi
, &o
);
4072 Jim_DecrRefCount(teap
->interp
, teap
->body
);
4073 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
4076 * Tcl/TK - "tk events" have a nice feature.
4077 * See the "BIND" command.
4078 * We should support that here.
4079 * You can specify %X and %Y in the event code.
4080 * The idea is: %T - target name.
4081 * The idea is: %N - target number
4082 * The idea is: %E - event name.
4084 Jim_IncrRefCount(teap
->body
);
4087 /* add to head of event list */
4088 teap
->next
= target
->event_action
;
4089 target
->event_action
= teap
;
4091 Jim_SetEmptyResult(goi
->interp
);
4095 Jim_SetEmptyResult(goi
->interp
);
4097 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
4103 case TCFG_WORK_AREA_VIRT
:
4104 if (goi
->isconfigure
) {
4105 target_free_all_working_areas(target
);
4106 e
= Jim_GetOpt_Wide(goi
, &w
);
4109 target
->working_area_virt
= w
;
4110 target
->working_area_virt_spec
= true;
4115 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
4119 case TCFG_WORK_AREA_PHYS
:
4120 if (goi
->isconfigure
) {
4121 target_free_all_working_areas(target
);
4122 e
= Jim_GetOpt_Wide(goi
, &w
);
4125 target
->working_area_phys
= w
;
4126 target
->working_area_phys_spec
= true;
4131 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
4135 case TCFG_WORK_AREA_SIZE
:
4136 if (goi
->isconfigure
) {
4137 target_free_all_working_areas(target
);
4138 e
= Jim_GetOpt_Wide(goi
, &w
);
4141 target
->working_area_size
= w
;
4146 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4150 case TCFG_WORK_AREA_BACKUP
:
4151 if (goi
->isconfigure
) {
4152 target_free_all_working_areas(target
);
4153 e
= Jim_GetOpt_Wide(goi
, &w
);
4156 /* make this exactly 1 or 0 */
4157 target
->backup_working_area
= (!!w
);
4162 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
4163 /* loop for more e*/
4168 if (goi
->isconfigure
) {
4169 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
4171 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
4174 target
->endianness
= n
->value
;
4179 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4180 if (n
->name
== NULL
) {
4181 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4182 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4184 Jim_SetResultString(goi
->interp
, n
->name
, -1);
4189 if (goi
->isconfigure
) {
4190 if (goi
->argc
< 1) {
4191 Jim_SetResultFormatted(goi
->interp
,
4196 if (target
->variant
)
4197 free((void *)(target
->variant
));
4198 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
4201 target
->variant
= strdup(cp
);
4206 Jim_SetResultString(goi
->interp
, target
->variant
, -1);
4211 if (goi
->isconfigure
) {
4212 e
= Jim_GetOpt_Wide(goi
, &w
);
4215 target
->coreid
= (int32_t)w
;
4220 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4224 case TCFG_CHAIN_POSITION
:
4225 if (goi
->isconfigure
) {
4227 struct jtag_tap
*tap
;
4228 target_free_all_working_areas(target
);
4229 e
= Jim_GetOpt_Obj(goi
, &o_t
);
4232 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
4235 /* make this exactly 1 or 0 */
4241 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
4242 /* loop for more e*/
4245 if (goi
->isconfigure
) {
4246 e
= Jim_GetOpt_Wide(goi
, &w
);
4249 target
->dbgbase
= (uint32_t)w
;
4250 target
->dbgbase_set
= true;
4255 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
4262 int result
= rtos_create(goi
, target
);
4263 if (result
!= JIM_OK
)
4269 } /* while (goi->argc) */
4272 /* done - we return */
4276 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4280 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4281 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4282 int need_args
= 1 + goi
.isconfigure
;
4283 if (goi
.argc
< need_args
) {
4284 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4286 ? "missing: -option VALUE ..."
4287 : "missing: -option ...");
4290 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4291 return target_configure(&goi
, target
);
4294 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4296 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4299 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4301 if (goi
.argc
< 2 || goi
.argc
> 4) {
4302 Jim_SetResultFormatted(goi
.interp
,
4303 "usage: %s [phys] <address> <data> [<count>]", cmd_name
);
4308 fn
= target_write_memory_fast
;
4311 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4313 struct Jim_Obj
*obj
;
4314 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4318 fn
= target_write_phys_memory
;
4322 e
= Jim_GetOpt_Wide(&goi
, &a
);
4327 e
= Jim_GetOpt_Wide(&goi
, &b
);
4332 if (goi
.argc
== 1) {
4333 e
= Jim_GetOpt_Wide(&goi
, &c
);
4338 /* all args must be consumed */
4342 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4344 if (strcasecmp(cmd_name
, "mww") == 0)
4346 else if (strcasecmp(cmd_name
, "mwh") == 0)
4348 else if (strcasecmp(cmd_name
, "mwb") == 0)
4351 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4355 return (target_fill_mem(target
, a
, fn
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4359 * @brief Reads an array of words/halfwords/bytes from target memory starting at specified address.
4361 * Usage: mdw [phys] <address> [<count>] - for 32 bit reads
4362 * mdh [phys] <address> [<count>] - for 16 bit reads
4363 * mdb [phys] <address> [<count>] - for 8 bit reads
4365 * Count defaults to 1.
4367 * Calls target_read_memory or target_read_phys_memory depending on
4368 * the presence of the "phys" argument
4369 * Reads the target memory in blocks of max. 32 bytes, and returns an array of ints formatted
4370 * to int representation in base16.
4371 * Also outputs read data in a human readable form using command_print
4373 * @param phys if present target_read_phys_memory will be used instead of target_read_memory
4374 * @param address address where to start the read. May be specified in decimal or hex using the standard "0x" prefix
4375 * @param count optional count parameter to read an array of values. If not specified, defaults to 1.
4376 * @returns: JIM_ERR on error or JIM_OK on success and sets the result string to an array of ascii formatted numbers
4377 * on success, with [<count>] number of elements.
4379 * In case of little endian target:
4380 * Example1: "mdw 0x00000000" returns "10123456"
4381 * Exmaple2: "mdh 0x00000000 1" returns "3456"
4382 * Example3: "mdb 0x00000000" returns "56"
4383 * Example4: "mdh 0x00000000 2" returns "3456 1012"
4384 * Example5: "mdb 0x00000000 3" returns "56 34 12"
4386 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4388 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4391 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4393 if ((goi
.argc
< 1) || (goi
.argc
> 3)) {
4394 Jim_SetResultFormatted(goi
.interp
,
4395 "usage: %s [phys] <address> [<count>]", cmd_name
);
4399 int (*fn
)(struct target
*target
,
4400 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
4401 fn
= target_read_memory
;
4404 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4406 struct Jim_Obj
*obj
;
4407 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4411 fn
= target_read_phys_memory
;
4414 /* Read address parameter */
4416 e
= Jim_GetOpt_Wide(&goi
, &addr
);
4420 /* If next parameter exists, read it out as the count parameter, if not, set it to 1 (default) */
4422 if (goi
.argc
== 1) {
4423 e
= Jim_GetOpt_Wide(&goi
, &count
);
4429 /* all args must be consumed */
4433 jim_wide dwidth
= 1; /* shut up gcc */
4434 if (strcasecmp(cmd_name
, "mdw") == 0)
4436 else if (strcasecmp(cmd_name
, "mdh") == 0)
4438 else if (strcasecmp(cmd_name
, "mdb") == 0)
4441 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4445 /* convert count to "bytes" */
4446 int bytes
= count
* dwidth
;
4448 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4449 uint8_t target_buf
[32];
4452 y
= (bytes
< 16) ? bytes
: 16; /* y = min(bytes, 16); */
4454 /* Try to read out next block */
4455 e
= fn(target
, addr
, dwidth
, y
/ dwidth
, target_buf
);
4457 if (e
!= ERROR_OK
) {
4458 Jim_SetResultFormatted(interp
, "error reading target @ 0x%08lx", (long)addr
);
4462 command_print_sameline(NULL
, "0x%08x ", (int)(addr
));
4465 for (x
= 0; x
< 16 && x
< y
; x
+= 4) {
4466 z
= target_buffer_get_u32(target
, &(target_buf
[x
]));
4467 command_print_sameline(NULL
, "%08x ", (int)(z
));
4469 for (; (x
< 16) ; x
+= 4)
4470 command_print_sameline(NULL
, " ");
4473 for (x
= 0; x
< 16 && x
< y
; x
+= 2) {
4474 z
= target_buffer_get_u16(target
, &(target_buf
[x
]));
4475 command_print_sameline(NULL
, "%04x ", (int)(z
));
4477 for (; (x
< 16) ; x
+= 2)
4478 command_print_sameline(NULL
, " ");
4482 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4483 z
= target_buffer_get_u8(target
, &(target_buf
[x
]));
4484 command_print_sameline(NULL
, "%02x ", (int)(z
));
4486 for (; (x
< 16) ; x
+= 1)
4487 command_print_sameline(NULL
, " ");
4490 /* ascii-ify the bytes */
4491 for (x
= 0 ; x
< y
; x
++) {
4492 if ((target_buf
[x
] >= 0x20) &&
4493 (target_buf
[x
] <= 0x7e)) {
4497 target_buf
[x
] = '.';
4502 target_buf
[x
] = ' ';
4507 /* print - with a newline */
4508 command_print_sameline(NULL
, "%s\n", target_buf
);
4516 static int jim_target_mem2array(Jim_Interp
*interp
,
4517 int argc
, Jim_Obj
*const *argv
)
4519 struct target
*target
= Jim_CmdPrivData(interp
);
4520 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4523 static int jim_target_array2mem(Jim_Interp
*interp
,
4524 int argc
, Jim_Obj
*const *argv
)
4526 struct target
*target
= Jim_CmdPrivData(interp
);
4527 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4530 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4532 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
4536 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4539 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4542 struct target
*target
= Jim_CmdPrivData(interp
);
4543 if (!target
->tap
->enabled
)
4544 return jim_target_tap_disabled(interp
);
4546 int e
= target
->type
->examine(target
);
4552 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4555 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4558 struct target
*target
= Jim_CmdPrivData(interp
);
4560 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
4566 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4569 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4572 struct target
*target
= Jim_CmdPrivData(interp
);
4573 if (!target
->tap
->enabled
)
4574 return jim_target_tap_disabled(interp
);
4577 if (!(target_was_examined(target
)))
4578 e
= ERROR_TARGET_NOT_EXAMINED
;
4580 e
= target
->type
->poll(target
);
4586 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4589 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4591 if (goi
.argc
!= 2) {
4592 Jim_WrongNumArgs(interp
, 0, argv
,
4593 "([tT]|[fF]|assert|deassert) BOOL");
4598 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4600 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4603 /* the halt or not param */
4605 e
= Jim_GetOpt_Wide(&goi
, &a
);
4609 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4610 if (!target
->tap
->enabled
)
4611 return jim_target_tap_disabled(interp
);
4612 if (!(target_was_examined(target
))) {
4613 LOG_ERROR("Target not examined yet");
4614 return ERROR_TARGET_NOT_EXAMINED
;
4616 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
4617 Jim_SetResultFormatted(interp
,
4618 "No target-specific reset for %s",
4619 target_name(target
));
4622 /* determine if we should halt or not. */
4623 target
->reset_halt
= !!a
;
4624 /* When this happens - all workareas are invalid. */
4625 target_free_all_working_areas_restore(target
, 0);
4628 if (n
->value
== NVP_ASSERT
)
4629 e
= target
->type
->assert_reset(target
);
4631 e
= target
->type
->deassert_reset(target
);
4632 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4635 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4638 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4641 struct target
*target
= Jim_CmdPrivData(interp
);
4642 if (!target
->tap
->enabled
)
4643 return jim_target_tap_disabled(interp
);
4644 int e
= target
->type
->halt(target
);
4645 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4648 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4651 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4653 /* params: <name> statename timeoutmsecs */
4654 if (goi
.argc
!= 2) {
4655 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4656 Jim_SetResultFormatted(goi
.interp
,
4657 "%s <state_name> <timeout_in_msec>", cmd_name
);
4662 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4664 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
4668 e
= Jim_GetOpt_Wide(&goi
, &a
);
4671 struct target
*target
= Jim_CmdPrivData(interp
);
4672 if (!target
->tap
->enabled
)
4673 return jim_target_tap_disabled(interp
);
4675 e
= target_wait_state(target
, n
->value
, a
);
4676 if (e
!= ERROR_OK
) {
4677 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
4678 Jim_SetResultFormatted(goi
.interp
,
4679 "target: %s wait %s fails (%#s) %s",
4680 target_name(target
), n
->name
,
4681 eObj
, target_strerror_safe(e
));
4682 Jim_FreeNewObj(interp
, eObj
);
4687 /* List for human, Events defined for this target.
4688 * scripts/programs should use 'name cget -event NAME'
4690 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4692 struct command_context
*cmd_ctx
= current_command_context(interp
);
4693 assert(cmd_ctx
!= NULL
);
4695 struct target
*target
= Jim_CmdPrivData(interp
);
4696 struct target_event_action
*teap
= target
->event_action
;
4697 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4698 target
->target_number
,
4699 target_name(target
));
4700 command_print(cmd_ctx
, "%-25s | Body", "Event");
4701 command_print(cmd_ctx
, "------------------------- | "
4702 "----------------------------------------");
4704 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
4705 command_print(cmd_ctx
, "%-25s | %s",
4706 opt
->name
, Jim_GetString(teap
->body
, NULL
));
4709 command_print(cmd_ctx
, "***END***");
4712 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4715 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4718 struct target
*target
= Jim_CmdPrivData(interp
);
4719 Jim_SetResultString(interp
, target_state_name(target
), -1);
4722 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4725 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4726 if (goi
.argc
!= 1) {
4727 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4728 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
4732 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4734 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4737 struct target
*target
= Jim_CmdPrivData(interp
);
4738 target_handle_event(target
, n
->value
);
4742 static const struct command_registration target_instance_command_handlers
[] = {
4744 .name
= "configure",
4745 .mode
= COMMAND_CONFIG
,
4746 .jim_handler
= jim_target_configure
,
4747 .help
= "configure a new target for use",
4748 .usage
= "[target_attribute ...]",
4752 .mode
= COMMAND_ANY
,
4753 .jim_handler
= jim_target_configure
,
4754 .help
= "returns the specified target attribute",
4755 .usage
= "target_attribute",
4759 .mode
= COMMAND_EXEC
,
4760 .jim_handler
= jim_target_mw
,
4761 .help
= "Write 32-bit word(s) to target memory",
4762 .usage
= "address data [count]",
4766 .mode
= COMMAND_EXEC
,
4767 .jim_handler
= jim_target_mw
,
4768 .help
= "Write 16-bit half-word(s) to target memory",
4769 .usage
= "address data [count]",
4773 .mode
= COMMAND_EXEC
,
4774 .jim_handler
= jim_target_mw
,
4775 .help
= "Write byte(s) to target memory",
4776 .usage
= "address data [count]",
4780 .mode
= COMMAND_EXEC
,
4781 .jim_handler
= jim_target_md
,
4782 .help
= "Display target memory as 32-bit words",
4783 .usage
= "address [count]",
4787 .mode
= COMMAND_EXEC
,
4788 .jim_handler
= jim_target_md
,
4789 .help
= "Display target memory as 16-bit half-words",
4790 .usage
= "address [count]",
4794 .mode
= COMMAND_EXEC
,
4795 .jim_handler
= jim_target_md
,
4796 .help
= "Display target memory as 8-bit bytes",
4797 .usage
= "address [count]",
4800 .name
= "array2mem",
4801 .mode
= COMMAND_EXEC
,
4802 .jim_handler
= jim_target_array2mem
,
4803 .help
= "Writes Tcl array of 8/16/32 bit numbers "
4805 .usage
= "arrayname bitwidth address count",
4808 .name
= "mem2array",
4809 .mode
= COMMAND_EXEC
,
4810 .jim_handler
= jim_target_mem2array
,
4811 .help
= "Loads Tcl array of 8/16/32 bit numbers "
4812 "from target memory",
4813 .usage
= "arrayname bitwidth address count",
4816 .name
= "eventlist",
4817 .mode
= COMMAND_EXEC
,
4818 .jim_handler
= jim_target_event_list
,
4819 .help
= "displays a table of events defined for this target",
4823 .mode
= COMMAND_EXEC
,
4824 .jim_handler
= jim_target_current_state
,
4825 .help
= "displays the current state of this target",
4828 .name
= "arp_examine",
4829 .mode
= COMMAND_EXEC
,
4830 .jim_handler
= jim_target_examine
,
4831 .help
= "used internally for reset processing",
4834 .name
= "arp_halt_gdb",
4835 .mode
= COMMAND_EXEC
,
4836 .jim_handler
= jim_target_halt_gdb
,
4837 .help
= "used internally for reset processing to halt GDB",
4841 .mode
= COMMAND_EXEC
,
4842 .jim_handler
= jim_target_poll
,
4843 .help
= "used internally for reset processing",
4846 .name
= "arp_reset",
4847 .mode
= COMMAND_EXEC
,
4848 .jim_handler
= jim_target_reset
,
4849 .help
= "used internally for reset processing",
4853 .mode
= COMMAND_EXEC
,
4854 .jim_handler
= jim_target_halt
,
4855 .help
= "used internally for reset processing",
4858 .name
= "arp_waitstate",
4859 .mode
= COMMAND_EXEC
,
4860 .jim_handler
= jim_target_wait_state
,
4861 .help
= "used internally for reset processing",
4864 .name
= "invoke-event",
4865 .mode
= COMMAND_EXEC
,
4866 .jim_handler
= jim_target_invoke_event
,
4867 .help
= "invoke handler for specified event",
4868 .usage
= "event_name",
4870 COMMAND_REGISTRATION_DONE
4873 static int target_create(Jim_GetOptInfo
*goi
)
4881 struct target
*target
;
4882 struct command_context
*cmd_ctx
;
4884 cmd_ctx
= current_command_context(goi
->interp
);
4885 assert(cmd_ctx
!= NULL
);
4887 if (goi
->argc
< 3) {
4888 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4893 Jim_GetOpt_Obj(goi
, &new_cmd
);
4894 /* does this command exist? */
4895 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4897 cp
= Jim_GetString(new_cmd
, NULL
);
4898 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
4903 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4907 /* now does target type exist */
4908 for (x
= 0 ; target_types
[x
] ; x
++) {
4909 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4914 /* check for deprecated name */
4915 if (target_types
[x
]->deprecated_name
) {
4916 if (0 == strcmp(cp
, target_types
[x
]->deprecated_name
)) {
4918 LOG_WARNING("target name is deprecated use: \'%s\'", target_types
[x
]->name
);
4923 if (target_types
[x
] == NULL
) {
4924 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4925 for (x
= 0 ; target_types
[x
] ; x
++) {
4926 if (target_types
[x
+ 1]) {
4927 Jim_AppendStrings(goi
->interp
,
4928 Jim_GetResult(goi
->interp
),
4929 target_types
[x
]->name
,
4932 Jim_AppendStrings(goi
->interp
,
4933 Jim_GetResult(goi
->interp
),
4935 target_types
[x
]->name
, NULL
);
4942 target
= calloc(1, sizeof(struct target
));
4943 /* set target number */
4944 target
->target_number
= new_target_number();
4946 /* allocate memory for each unique target type */
4947 target
->type
= (struct target_type
*)calloc(1, sizeof(struct target_type
));
4949 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
4951 /* will be set by "-endian" */
4952 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4954 /* default to first core, override with -coreid */
4957 target
->working_area
= 0x0;
4958 target
->working_area_size
= 0x0;
4959 target
->working_areas
= NULL
;
4960 target
->backup_working_area
= 0;
4962 target
->state
= TARGET_UNKNOWN
;
4963 target
->debug_reason
= DBG_REASON_UNDEFINED
;
4964 target
->reg_cache
= NULL
;
4965 target
->breakpoints
= NULL
;
4966 target
->watchpoints
= NULL
;
4967 target
->next
= NULL
;
4968 target
->arch_info
= NULL
;
4970 target
->display
= 1;
4972 target
->halt_issued
= false;
4974 /* initialize trace information */
4975 target
->trace_info
= malloc(sizeof(struct trace
));
4976 target
->trace_info
->num_trace_points
= 0;
4977 target
->trace_info
->trace_points_size
= 0;
4978 target
->trace_info
->trace_points
= NULL
;
4979 target
->trace_info
->trace_history_size
= 0;
4980 target
->trace_info
->trace_history
= NULL
;
4981 target
->trace_info
->trace_history_pos
= 0;
4982 target
->trace_info
->trace_history_overflowed
= 0;
4984 target
->dbgmsg
= NULL
;
4985 target
->dbg_msg_enabled
= 0;
4987 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4989 target
->rtos
= NULL
;
4990 target
->rtos_auto_detect
= false;
4992 /* Do the rest as "configure" options */
4993 goi
->isconfigure
= 1;
4994 e
= target_configure(goi
, target
);
4996 if (target
->tap
== NULL
) {
4997 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
5007 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
5008 /* default endian to little if not specified */
5009 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5012 /* incase variant is not set */
5013 if (!target
->variant
)
5014 target
->variant
= strdup("");
5016 cp
= Jim_GetString(new_cmd
, NULL
);
5017 target
->cmd_name
= strdup(cp
);
5019 /* create the target specific commands */
5020 if (target
->type
->commands
) {
5021 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
5023 LOG_ERROR("unable to register '%s' commands", cp
);
5025 if (target
->type
->target_create
)
5026 (*(target
->type
->target_create
))(target
, goi
->interp
);
5028 /* append to end of list */
5030 struct target
**tpp
;
5031 tpp
= &(all_targets
);
5033 tpp
= &((*tpp
)->next
);
5037 /* now - create the new target name command */
5038 const const struct command_registration target_subcommands
[] = {
5040 .chain
= target_instance_command_handlers
,
5043 .chain
= target
->type
->commands
,
5045 COMMAND_REGISTRATION_DONE
5047 const const struct command_registration target_commands
[] = {
5050 .mode
= COMMAND_ANY
,
5051 .help
= "target command group",
5053 .chain
= target_subcommands
,
5055 COMMAND_REGISTRATION_DONE
5057 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
5061 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
5063 command_set_handler_data(c
, target
);
5065 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
5068 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5071 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5074 struct command_context
*cmd_ctx
= current_command_context(interp
);
5075 assert(cmd_ctx
!= NULL
);
5077 Jim_SetResultString(interp
, get_current_target(cmd_ctx
)->cmd_name
, -1);
5081 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5084 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5087 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5088 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
5089 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5090 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
5095 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5098 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5101 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5102 struct target
*target
= all_targets
;
5104 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5105 Jim_NewStringObj(interp
, target_name(target
), -1));
5106 target
= target
->next
;
5111 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5114 const char *targetname
;
5116 struct target
*target
= (struct target
*) NULL
;
5117 struct target_list
*head
, *curr
, *new;
5118 curr
= (struct target_list
*) NULL
;
5119 head
= (struct target_list
*) NULL
;
5122 LOG_DEBUG("%d", argc
);
5123 /* argv[1] = target to associate in smp
5124 * argv[2] = target to assoicate in smp
5128 for (i
= 1; i
< argc
; i
++) {
5130 targetname
= Jim_GetString(argv
[i
], &len
);
5131 target
= get_target(targetname
);
5132 LOG_DEBUG("%s ", targetname
);
5134 new = malloc(sizeof(struct target_list
));
5135 new->target
= target
;
5136 new->next
= (struct target_list
*)NULL
;
5137 if (head
== (struct target_list
*)NULL
) {
5146 /* now parse the list of cpu and put the target in smp mode*/
5149 while (curr
!= (struct target_list
*)NULL
) {
5150 target
= curr
->target
;
5152 target
->head
= head
;
5156 if (target
&& target
->rtos
)
5157 retval
= rtos_smp_init(head
->target
);
5163 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5166 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5168 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5169 "<name> <target_type> [<target_options> ...]");
5172 return target_create(&goi
);
5175 static int jim_target_number(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5178 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5180 /* It's OK to remove this mechanism sometime after August 2010 or so */
5181 LOG_WARNING("don't use numbers as target identifiers; use names");
5182 if (goi
.argc
!= 1) {
5183 Jim_SetResultFormatted(goi
.interp
, "usage: target number <number>");
5187 int e
= Jim_GetOpt_Wide(&goi
, &w
);
5191 struct target
*target
;
5192 for (target
= all_targets
; NULL
!= target
; target
= target
->next
) {
5193 if (target
->target_number
!= w
)
5196 Jim_SetResultString(goi
.interp
, target_name(target
), -1);
5200 Jim_Obj
*wObj
= Jim_NewIntObj(goi
.interp
, w
);
5201 Jim_SetResultFormatted(goi
.interp
,
5202 "Target: number %#s does not exist", wObj
);
5203 Jim_FreeNewObj(interp
, wObj
);
5208 static int jim_target_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5211 Jim_WrongNumArgs(interp
, 1, argv
, "<no parameters>");
5215 struct target
*target
= all_targets
;
5216 while (NULL
!= target
) {
5217 target
= target
->next
;
5220 Jim_SetResult(interp
, Jim_NewIntObj(interp
, count
));
5224 static const struct command_registration target_subcommand_handlers
[] = {
5227 .mode
= COMMAND_CONFIG
,
5228 .handler
= handle_target_init_command
,
5229 .help
= "initialize targets",
5233 /* REVISIT this should be COMMAND_CONFIG ... */
5234 .mode
= COMMAND_ANY
,
5235 .jim_handler
= jim_target_create
,
5236 .usage
= "name type '-chain-position' name [options ...]",
5237 .help
= "Creates and selects a new target",
5241 .mode
= COMMAND_ANY
,
5242 .jim_handler
= jim_target_current
,
5243 .help
= "Returns the currently selected target",
5247 .mode
= COMMAND_ANY
,
5248 .jim_handler
= jim_target_types
,
5249 .help
= "Returns the available target types as "
5250 "a list of strings",
5254 .mode
= COMMAND_ANY
,
5255 .jim_handler
= jim_target_names
,
5256 .help
= "Returns the names of all targets as a list of strings",
5260 .mode
= COMMAND_ANY
,
5261 .jim_handler
= jim_target_number
,
5263 .help
= "Returns the name of the numbered target "
5268 .mode
= COMMAND_ANY
,
5269 .jim_handler
= jim_target_count
,
5270 .help
= "Returns the number of targets as an integer "
5275 .mode
= COMMAND_ANY
,
5276 .jim_handler
= jim_target_smp
,
5277 .usage
= "targetname1 targetname2 ...",
5278 .help
= "gather several target in a smp list"
5281 COMMAND_REGISTRATION_DONE
5291 static int fastload_num
;
5292 static struct FastLoad
*fastload
;
5294 static void free_fastload(void)
5296 if (fastload
!= NULL
) {
5298 for (i
= 0; i
< fastload_num
; i
++) {
5299 if (fastload
[i
].data
)
5300 free(fastload
[i
].data
);
5307 COMMAND_HANDLER(handle_fast_load_image_command
)
5311 uint32_t image_size
;
5312 uint32_t min_address
= 0;
5313 uint32_t max_address
= 0xffffffff;
5318 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5319 &image
, &min_address
, &max_address
);
5320 if (ERROR_OK
!= retval
)
5323 struct duration bench
;
5324 duration_start(&bench
);
5326 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5327 if (retval
!= ERROR_OK
)
5332 fastload_num
= image
.num_sections
;
5333 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5334 if (fastload
== NULL
) {
5335 command_print(CMD_CTX
, "out of memory");
5336 image_close(&image
);
5339 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5340 for (i
= 0; i
< image
.num_sections
; i
++) {
5341 buffer
= malloc(image
.sections
[i
].size
);
5342 if (buffer
== NULL
) {
5343 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5344 (int)(image
.sections
[i
].size
));
5345 retval
= ERROR_FAIL
;
5349 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5350 if (retval
!= ERROR_OK
) {
5355 uint32_t offset
= 0;
5356 uint32_t length
= buf_cnt
;
5358 /* DANGER!!! beware of unsigned comparision here!!! */
5360 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5361 (image
.sections
[i
].base_address
< max_address
)) {
5362 if (image
.sections
[i
].base_address
< min_address
) {
5363 /* clip addresses below */
5364 offset
+= min_address
-image
.sections
[i
].base_address
;
5368 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5369 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5371 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5372 fastload
[i
].data
= malloc(length
);
5373 if (fastload
[i
].data
== NULL
) {
5375 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5377 retval
= ERROR_FAIL
;
5380 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5381 fastload
[i
].length
= length
;
5383 image_size
+= length
;
5384 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
5385 (unsigned int)length
,
5386 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5392 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
5393 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
5394 "in %fs (%0.3f KiB/s)", image_size
,
5395 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5397 command_print(CMD_CTX
,
5398 "WARNING: image has not been loaded to target!"
5399 "You can issue a 'fast_load' to finish loading.");
5402 image_close(&image
);
5404 if (retval
!= ERROR_OK
)
5410 COMMAND_HANDLER(handle_fast_load_command
)
5413 return ERROR_COMMAND_SYNTAX_ERROR
;
5414 if (fastload
== NULL
) {
5415 LOG_ERROR("No image in memory");
5419 int ms
= timeval_ms();
5421 int retval
= ERROR_OK
;
5422 for (i
= 0; i
< fastload_num
; i
++) {
5423 struct target
*target
= get_current_target(CMD_CTX
);
5424 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
5425 (unsigned int)(fastload
[i
].address
),
5426 (unsigned int)(fastload
[i
].length
));
5427 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5428 if (retval
!= ERROR_OK
)
5430 size
+= fastload
[i
].length
;
5432 if (retval
== ERROR_OK
) {
5433 int after
= timeval_ms();
5434 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5439 static const struct command_registration target_command_handlers
[] = {
5442 .handler
= handle_targets_command
,
5443 .mode
= COMMAND_ANY
,
5444 .help
= "change current default target (one parameter) "
5445 "or prints table of all targets (no parameters)",
5446 .usage
= "[target]",
5450 .mode
= COMMAND_CONFIG
,
5451 .help
= "configure target",
5453 .chain
= target_subcommand_handlers
,
5455 COMMAND_REGISTRATION_DONE
5458 int target_register_commands(struct command_context
*cmd_ctx
)
5460 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5463 static bool target_reset_nag
= true;
5465 bool get_target_reset_nag(void)
5467 return target_reset_nag
;
5470 COMMAND_HANDLER(handle_target_reset_nag
)
5472 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5473 &target_reset_nag
, "Nag after each reset about options to improve "
5477 COMMAND_HANDLER(handle_ps_command
)
5479 struct target
*target
= get_current_target(CMD_CTX
);
5481 if (target
->state
!= TARGET_HALTED
) {
5482 LOG_INFO("target not halted !!");
5486 if ((target
->rtos
) && (target
->rtos
->type
)
5487 && (target
->rtos
->type
->ps_command
)) {
5488 display
= target
->rtos
->type
->ps_command(target
);
5489 command_print(CMD_CTX
, "%s", display
);
5494 return ERROR_TARGET_FAILURE
;
5498 static const struct command_registration target_exec_command_handlers
[] = {
5500 .name
= "fast_load_image",
5501 .handler
= handle_fast_load_image_command
,
5502 .mode
= COMMAND_ANY
,
5503 .help
= "Load image into server memory for later use by "
5504 "fast_load; primarily for profiling",
5505 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5506 "[min_address [max_length]]",
5509 .name
= "fast_load",
5510 .handler
= handle_fast_load_command
,
5511 .mode
= COMMAND_EXEC
,
5512 .help
= "loads active fast load image to current target "
5513 "- mainly for profiling purposes",
5518 .handler
= handle_profile_command
,
5519 .mode
= COMMAND_EXEC
,
5520 .usage
= "seconds filename",
5521 .help
= "profiling samples the CPU PC",
5523 /** @todo don't register virt2phys() unless target supports it */
5525 .name
= "virt2phys",
5526 .handler
= handle_virt2phys_command
,
5527 .mode
= COMMAND_ANY
,
5528 .help
= "translate a virtual address into a physical address",
5529 .usage
= "virtual_address",
5533 .handler
= handle_reg_command
,
5534 .mode
= COMMAND_EXEC
,
5535 .help
= "display or set a register; with no arguments, "
5536 "displays all registers and their values",
5537 .usage
= "[(register_name|register_number) [value]]",
5541 .handler
= handle_poll_command
,
5542 .mode
= COMMAND_EXEC
,
5543 .help
= "poll target state; or reconfigure background polling",
5544 .usage
= "['on'|'off']",
5547 .name
= "wait_halt",
5548 .handler
= handle_wait_halt_command
,
5549 .mode
= COMMAND_EXEC
,
5550 .help
= "wait up to the specified number of milliseconds "
5551 "(default 5) for a previously requested halt",
5552 .usage
= "[milliseconds]",
5556 .handler
= handle_halt_command
,
5557 .mode
= COMMAND_EXEC
,
5558 .help
= "request target to halt, then wait up to the specified"
5559 "number of milliseconds (default 5) for it to complete",
5560 .usage
= "[milliseconds]",
5564 .handler
= handle_resume_command
,
5565 .mode
= COMMAND_EXEC
,
5566 .help
= "resume target execution from current PC or address",
5567 .usage
= "[address]",
5571 .handler
= handle_reset_command
,
5572 .mode
= COMMAND_EXEC
,
5573 .usage
= "[run|halt|init]",
5574 .help
= "Reset all targets into the specified mode."
5575 "Default reset mode is run, if not given.",
5578 .name
= "soft_reset_halt",
5579 .handler
= handle_soft_reset_halt_command
,
5580 .mode
= COMMAND_EXEC
,
5582 .help
= "halt the target and do a soft reset",
5586 .handler
= handle_step_command
,
5587 .mode
= COMMAND_EXEC
,
5588 .help
= "step one instruction from current PC or address",
5589 .usage
= "[address]",
5593 .handler
= handle_md_command
,
5594 .mode
= COMMAND_EXEC
,
5595 .help
= "display memory words",
5596 .usage
= "['phys'] address [count]",
5600 .handler
= handle_md_command
,
5601 .mode
= COMMAND_EXEC
,
5602 .help
= "display memory half-words",
5603 .usage
= "['phys'] address [count]",
5607 .handler
= handle_md_command
,
5608 .mode
= COMMAND_EXEC
,
5609 .help
= "display memory bytes",
5610 .usage
= "['phys'] address [count]",
5614 .handler
= handle_mw_command
,
5615 .mode
= COMMAND_EXEC
,
5616 .help
= "write memory word",
5617 .usage
= "['phys'] address value [count]",
5621 .handler
= handle_mw_command
,
5622 .mode
= COMMAND_EXEC
,
5623 .help
= "write memory half-word",
5624 .usage
= "['phys'] address value [count]",
5628 .handler
= handle_mw_command
,
5629 .mode
= COMMAND_EXEC
,
5630 .help
= "write memory byte",
5631 .usage
= "['phys'] address value [count]",
5635 .handler
= handle_bp_command
,
5636 .mode
= COMMAND_EXEC
,
5637 .help
= "list or set hardware or software breakpoint",
5638 .usage
= "<address> [<asid>]<length> ['hw'|'hw_ctx']",
5642 .handler
= handle_rbp_command
,
5643 .mode
= COMMAND_EXEC
,
5644 .help
= "remove breakpoint",
5649 .handler
= handle_wp_command
,
5650 .mode
= COMMAND_EXEC
,
5651 .help
= "list (no params) or create watchpoints",
5652 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
5656 .handler
= handle_rwp_command
,
5657 .mode
= COMMAND_EXEC
,
5658 .help
= "remove watchpoint",
5662 .name
= "load_image",
5663 .handler
= handle_load_image_command
,
5664 .mode
= COMMAND_EXEC
,
5665 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5666 "[min_address] [max_length]",
5669 .name
= "dump_image",
5670 .handler
= handle_dump_image_command
,
5671 .mode
= COMMAND_EXEC
,
5672 .usage
= "filename address size",
5675 .name
= "verify_image",
5676 .handler
= handle_verify_image_command
,
5677 .mode
= COMMAND_EXEC
,
5678 .usage
= "filename [offset [type]]",
5681 .name
= "test_image",
5682 .handler
= handle_test_image_command
,
5683 .mode
= COMMAND_EXEC
,
5684 .usage
= "filename [offset [type]]",
5687 .name
= "mem2array",
5688 .mode
= COMMAND_EXEC
,
5689 .jim_handler
= jim_mem2array
,
5690 .help
= "read 8/16/32 bit memory and return as a TCL array "
5691 "for script processing",
5692 .usage
= "arrayname bitwidth address count",
5695 .name
= "array2mem",
5696 .mode
= COMMAND_EXEC
,
5697 .jim_handler
= jim_array2mem
,
5698 .help
= "convert a TCL array to memory locations "
5699 "and write the 8/16/32 bit values",
5700 .usage
= "arrayname bitwidth address count",
5703 .name
= "reset_nag",
5704 .handler
= handle_target_reset_nag
,
5705 .mode
= COMMAND_ANY
,
5706 .help
= "Nag after each reset about options that could have been "
5707 "enabled to improve performance. ",
5708 .usage
= "['enable'|'disable']",
5712 .handler
= handle_ps_command
,
5713 .mode
= COMMAND_EXEC
,
5714 .help
= "list all tasks ",
5718 COMMAND_REGISTRATION_DONE
5720 static int target_register_user_commands(struct command_context
*cmd_ctx
)
5722 int retval
= ERROR_OK
;
5723 retval
= target_request_register_commands(cmd_ctx
);
5724 if (retval
!= ERROR_OK
)
5727 retval
= trace_register_commands(cmd_ctx
);
5728 if (retval
!= ERROR_OK
)
5732 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);