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 cortexr4_target
;
84 extern struct target_type arm11_target
;
85 extern struct target_type mips_m4k_target
;
86 extern struct target_type avr_target
;
87 extern struct target_type dsp563xx_target
;
88 extern struct target_type dsp5680xx_target
;
89 extern struct target_type testee_target
;
90 extern struct target_type avr32_ap7k_target
;
91 extern struct target_type hla_target
;
93 static struct target_type
*target_types
[] = {
119 struct target
*all_targets
;
120 static struct target_event_callback
*target_event_callbacks
;
121 static struct target_timer_callback
*target_timer_callbacks
;
122 static const int polling_interval
= 100;
124 static const Jim_Nvp nvp_assert
[] = {
125 { .name
= "assert", NVP_ASSERT
},
126 { .name
= "deassert", NVP_DEASSERT
},
127 { .name
= "T", NVP_ASSERT
},
128 { .name
= "F", NVP_DEASSERT
},
129 { .name
= "t", NVP_ASSERT
},
130 { .name
= "f", NVP_DEASSERT
},
131 { .name
= NULL
, .value
= -1 }
134 static const Jim_Nvp nvp_error_target
[] = {
135 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
136 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
137 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
138 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
139 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
140 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
141 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
142 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
143 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
144 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
145 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
146 { .value
= -1, .name
= NULL
}
149 static const char *target_strerror_safe(int err
)
153 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
160 static const Jim_Nvp nvp_target_event
[] = {
162 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
163 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
164 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
165 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
166 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
168 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
169 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
171 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
172 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
173 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
174 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
175 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
176 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
177 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
178 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
179 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
180 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
181 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
182 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
184 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
185 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
187 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
188 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
190 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
191 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
193 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
194 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
196 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
197 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
199 { .name
= NULL
, .value
= -1 }
202 static const Jim_Nvp nvp_target_state
[] = {
203 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
204 { .name
= "running", .value
= TARGET_RUNNING
},
205 { .name
= "halted", .value
= TARGET_HALTED
},
206 { .name
= "reset", .value
= TARGET_RESET
},
207 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
208 { .name
= NULL
, .value
= -1 },
211 static const Jim_Nvp nvp_target_debug_reason
[] = {
212 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
213 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
214 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
215 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
216 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
217 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
218 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
219 { .name
= NULL
, .value
= -1 },
222 static const Jim_Nvp nvp_target_endian
[] = {
223 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
224 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
225 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
226 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
227 { .name
= NULL
, .value
= -1 },
230 static const Jim_Nvp nvp_reset_modes
[] = {
231 { .name
= "unknown", .value
= RESET_UNKNOWN
},
232 { .name
= "run" , .value
= RESET_RUN
},
233 { .name
= "halt" , .value
= RESET_HALT
},
234 { .name
= "init" , .value
= RESET_INIT
},
235 { .name
= NULL
, .value
= -1 },
238 const char *debug_reason_name(struct target
*t
)
242 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
243 t
->debug_reason
)->name
;
245 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
246 cp
= "(*BUG*unknown*BUG*)";
251 const char *target_state_name(struct target
*t
)
254 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
256 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
257 cp
= "(*BUG*unknown*BUG*)";
262 /* determine the number of the new target */
263 static int new_target_number(void)
268 /* number is 0 based */
272 if (x
< t
->target_number
)
273 x
= t
->target_number
;
279 /* read a uint32_t from a buffer in target memory endianness */
280 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
282 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
283 return le_to_h_u32(buffer
);
285 return be_to_h_u32(buffer
);
288 /* read a uint24_t from a buffer in target memory endianness */
289 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
291 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
292 return le_to_h_u24(buffer
);
294 return be_to_h_u24(buffer
);
297 /* read a uint16_t from a buffer in target memory endianness */
298 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
300 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
301 return le_to_h_u16(buffer
);
303 return be_to_h_u16(buffer
);
306 /* read a uint8_t from a buffer in target memory endianness */
307 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
309 return *buffer
& 0x0ff;
312 /* write a uint32_t to a buffer in target memory endianness */
313 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
315 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
316 h_u32_to_le(buffer
, value
);
318 h_u32_to_be(buffer
, value
);
321 /* write a uint24_t to a buffer in target memory endianness */
322 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
324 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
325 h_u24_to_le(buffer
, value
);
327 h_u24_to_be(buffer
, value
);
330 /* write a uint16_t to a buffer in target memory endianness */
331 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
333 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
334 h_u16_to_le(buffer
, value
);
336 h_u16_to_be(buffer
, value
);
339 /* write a uint8_t to a buffer in target memory endianness */
340 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
345 /* write a uint32_t array to a buffer in target memory endianness */
346 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
349 for (i
= 0; i
< count
; i
++)
350 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
353 /* write a uint16_t array to a buffer in target memory endianness */
354 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
357 for (i
= 0; i
< count
; i
++)
358 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
361 /* write a uint32_t array to a buffer in target memory endianness */
362 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, uint32_t *srcbuf
)
365 for (i
= 0; i
< count
; i
++)
366 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
369 /* write a uint16_t array to a buffer in target memory endianness */
370 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, uint16_t *srcbuf
)
373 for (i
= 0; i
< count
; i
++)
374 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
377 /* return a pointer to a configured target; id is name or number */
378 struct target
*get_target(const char *id
)
380 struct target
*target
;
382 /* try as tcltarget name */
383 for (target
= all_targets
; target
; target
= target
->next
) {
384 if (target_name(target
) == NULL
)
386 if (strcmp(id
, target_name(target
)) == 0)
390 /* It's OK to remove this fallback sometime after August 2010 or so */
392 /* no match, try as number */
394 if (parse_uint(id
, &num
) != ERROR_OK
)
397 for (target
= all_targets
; target
; target
= target
->next
) {
398 if (target
->target_number
== (int)num
) {
399 LOG_WARNING("use '%s' as target identifier, not '%u'",
400 target_name(target
), num
);
408 /* returns a pointer to the n-th configured target */
409 static struct target
*get_target_by_num(int num
)
411 struct target
*target
= all_targets
;
414 if (target
->target_number
== num
)
416 target
= target
->next
;
422 struct target
*get_current_target(struct command_context
*cmd_ctx
)
424 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
426 if (target
== NULL
) {
427 LOG_ERROR("BUG: current_target out of bounds");
434 int target_poll(struct target
*target
)
438 /* We can't poll until after examine */
439 if (!target_was_examined(target
)) {
440 /* Fail silently lest we pollute the log */
444 retval
= target
->type
->poll(target
);
445 if (retval
!= ERROR_OK
)
448 if (target
->halt_issued
) {
449 if (target
->state
== TARGET_HALTED
)
450 target
->halt_issued
= false;
452 long long t
= timeval_ms() - target
->halt_issued_time
;
454 target
->halt_issued
= false;
455 LOG_INFO("Halt timed out, wake up GDB.");
456 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
464 int target_halt(struct target
*target
)
467 /* We can't poll until after examine */
468 if (!target_was_examined(target
)) {
469 LOG_ERROR("Target not examined yet");
473 retval
= target
->type
->halt(target
);
474 if (retval
!= ERROR_OK
)
477 target
->halt_issued
= true;
478 target
->halt_issued_time
= timeval_ms();
484 * Make the target (re)start executing using its saved execution
485 * context (possibly with some modifications).
487 * @param target Which target should start executing.
488 * @param current True to use the target's saved program counter instead
489 * of the address parameter
490 * @param address Optionally used as the program counter.
491 * @param handle_breakpoints True iff breakpoints at the resumption PC
492 * should be skipped. (For example, maybe execution was stopped by
493 * such a breakpoint, in which case it would be counterprodutive to
495 * @param debug_execution False if all working areas allocated by OpenOCD
496 * should be released and/or restored to their original contents.
497 * (This would for example be true to run some downloaded "helper"
498 * algorithm code, which resides in one such working buffer and uses
499 * another for data storage.)
501 * @todo Resolve the ambiguity about what the "debug_execution" flag
502 * signifies. For example, Target implementations don't agree on how
503 * it relates to invalidation of the register cache, or to whether
504 * breakpoints and watchpoints should be enabled. (It would seem wrong
505 * to enable breakpoints when running downloaded "helper" algorithms
506 * (debug_execution true), since the breakpoints would be set to match
507 * target firmware being debugged, not the helper algorithm.... and
508 * enabling them could cause such helpers to malfunction (for example,
509 * by overwriting data with a breakpoint instruction. On the other
510 * hand the infrastructure for running such helpers might use this
511 * procedure but rely on hardware breakpoint to detect termination.)
513 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
517 /* We can't poll until after examine */
518 if (!target_was_examined(target
)) {
519 LOG_ERROR("Target not examined yet");
523 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
525 /* note that resume *must* be asynchronous. The CPU can halt before
526 * we poll. The CPU can even halt at the current PC as a result of
527 * a software breakpoint being inserted by (a bug?) the application.
529 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
530 if (retval
!= ERROR_OK
)
533 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
538 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
543 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
544 if (n
->name
== NULL
) {
545 LOG_ERROR("invalid reset mode");
549 /* disable polling during reset to make reset event scripts
550 * more predictable, i.e. dr/irscan & pathmove in events will
551 * not have JTAG operations injected into the middle of a sequence.
553 bool save_poll
= jtag_poll_get_enabled();
555 jtag_poll_set_enabled(false);
557 sprintf(buf
, "ocd_process_reset %s", n
->name
);
558 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
560 jtag_poll_set_enabled(save_poll
);
562 if (retval
!= JIM_OK
) {
563 Jim_MakeErrorMessage(cmd_ctx
->interp
);
564 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
568 /* We want any events to be processed before the prompt */
569 retval
= target_call_timer_callbacks_now();
571 struct target
*target
;
572 for (target
= all_targets
; target
; target
= target
->next
)
573 target
->type
->check_reset(target
);
578 static int identity_virt2phys(struct target
*target
,
579 uint32_t virtual, uint32_t *physical
)
585 static int no_mmu(struct target
*target
, int *enabled
)
591 static int default_examine(struct target
*target
)
593 target_set_examined(target
);
597 /* no check by default */
598 static int default_check_reset(struct target
*target
)
603 int target_examine_one(struct target
*target
)
605 return target
->type
->examine(target
);
608 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
610 struct target
*target
= priv
;
612 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
615 jtag_unregister_event_callback(jtag_enable_callback
, target
);
617 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
619 int retval
= target_examine_one(target
);
620 if (retval
!= ERROR_OK
)
623 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
628 /* Targets that correctly implement init + examine, i.e.
629 * no communication with target during init:
633 int target_examine(void)
635 int retval
= ERROR_OK
;
636 struct target
*target
;
638 for (target
= all_targets
; target
; target
= target
->next
) {
639 /* defer examination, but don't skip it */
640 if (!target
->tap
->enabled
) {
641 jtag_register_event_callback(jtag_enable_callback
,
646 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
648 retval
= target_examine_one(target
);
649 if (retval
!= ERROR_OK
)
652 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
657 const char *target_type_name(struct target
*target
)
659 return target
->type
->name
;
662 static int target_soft_reset_halt(struct target
*target
)
664 if (!target_was_examined(target
)) {
665 LOG_ERROR("Target not examined yet");
668 if (!target
->type
->soft_reset_halt
) {
669 LOG_ERROR("Target %s does not support soft_reset_halt",
670 target_name(target
));
673 return target
->type
->soft_reset_halt(target
);
677 * Downloads a target-specific native code algorithm to the target,
678 * and executes it. * Note that some targets may need to set up, enable,
679 * and tear down a breakpoint (hard or * soft) to detect algorithm
680 * termination, while others may support lower overhead schemes where
681 * soft breakpoints embedded in the algorithm automatically terminate the
684 * @param target used to run the algorithm
685 * @param arch_info target-specific description of the algorithm.
687 int target_run_algorithm(struct target
*target
,
688 int num_mem_params
, struct mem_param
*mem_params
,
689 int num_reg_params
, struct reg_param
*reg_param
,
690 uint32_t entry_point
, uint32_t exit_point
,
691 int timeout_ms
, void *arch_info
)
693 int retval
= ERROR_FAIL
;
695 if (!target_was_examined(target
)) {
696 LOG_ERROR("Target not examined yet");
699 if (!target
->type
->run_algorithm
) {
700 LOG_ERROR("Target type '%s' does not support %s",
701 target_type_name(target
), __func__
);
705 target
->running_alg
= true;
706 retval
= target
->type
->run_algorithm(target
,
707 num_mem_params
, mem_params
,
708 num_reg_params
, reg_param
,
709 entry_point
, exit_point
, timeout_ms
, arch_info
);
710 target
->running_alg
= false;
717 * Downloads a target-specific native code algorithm to the target,
718 * executes and leaves it running.
720 * @param target used to run the algorithm
721 * @param arch_info target-specific description of the algorithm.
723 int target_start_algorithm(struct target
*target
,
724 int num_mem_params
, struct mem_param
*mem_params
,
725 int num_reg_params
, struct reg_param
*reg_params
,
726 uint32_t entry_point
, uint32_t exit_point
,
729 int retval
= ERROR_FAIL
;
731 if (!target_was_examined(target
)) {
732 LOG_ERROR("Target not examined yet");
735 if (!target
->type
->start_algorithm
) {
736 LOG_ERROR("Target type '%s' does not support %s",
737 target_type_name(target
), __func__
);
740 if (target
->running_alg
) {
741 LOG_ERROR("Target is already running an algorithm");
745 target
->running_alg
= true;
746 retval
= target
->type
->start_algorithm(target
,
747 num_mem_params
, mem_params
,
748 num_reg_params
, reg_params
,
749 entry_point
, exit_point
, arch_info
);
756 * Waits for an algorithm started with target_start_algorithm() to complete.
758 * @param target used to run the algorithm
759 * @param arch_info target-specific description of the algorithm.
761 int target_wait_algorithm(struct target
*target
,
762 int num_mem_params
, struct mem_param
*mem_params
,
763 int num_reg_params
, struct reg_param
*reg_params
,
764 uint32_t exit_point
, int timeout_ms
,
767 int retval
= ERROR_FAIL
;
769 if (!target
->type
->wait_algorithm
) {
770 LOG_ERROR("Target type '%s' does not support %s",
771 target_type_name(target
), __func__
);
774 if (!target
->running_alg
) {
775 LOG_ERROR("Target is not running an algorithm");
779 retval
= target
->type
->wait_algorithm(target
,
780 num_mem_params
, mem_params
,
781 num_reg_params
, reg_params
,
782 exit_point
, timeout_ms
, arch_info
);
783 if (retval
!= ERROR_TARGET_TIMEOUT
)
784 target
->running_alg
= false;
791 * Executes a target-specific native code algorithm in the target.
792 * It differs from target_run_algorithm in that the algorithm is asynchronous.
793 * Because of this it requires an compliant algorithm:
794 * see contrib/loaders/flash/stm32f1x.S for example.
796 * @param target used to run the algorithm
799 int target_run_flash_async_algorithm(struct target
*target
,
800 uint8_t *buffer
, uint32_t count
, int block_size
,
801 int num_mem_params
, struct mem_param
*mem_params
,
802 int num_reg_params
, struct reg_param
*reg_params
,
803 uint32_t buffer_start
, uint32_t buffer_size
,
804 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
809 /* Set up working area. First word is write pointer, second word is read pointer,
810 * rest is fifo data area. */
811 uint32_t wp_addr
= buffer_start
;
812 uint32_t rp_addr
= buffer_start
+ 4;
813 uint32_t fifo_start_addr
= buffer_start
+ 8;
814 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
816 uint32_t wp
= fifo_start_addr
;
817 uint32_t rp
= fifo_start_addr
;
819 /* validate block_size is 2^n */
820 assert(!block_size
|| !(block_size
& (block_size
- 1)));
822 retval
= target_write_u32(target
, wp_addr
, wp
);
823 if (retval
!= ERROR_OK
)
825 retval
= target_write_u32(target
, rp_addr
, rp
);
826 if (retval
!= ERROR_OK
)
829 /* Start up algorithm on target and let it idle while writing the first chunk */
830 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
831 num_reg_params
, reg_params
,
836 if (retval
!= ERROR_OK
) {
837 LOG_ERROR("error starting target flash write algorithm");
843 retval
= target_read_u32(target
, rp_addr
, &rp
);
844 if (retval
!= ERROR_OK
) {
845 LOG_ERROR("failed to get read pointer");
849 LOG_DEBUG("count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
, count
, wp
, rp
);
852 LOG_ERROR("flash write algorithm aborted by target");
853 retval
= ERROR_FLASH_OPERATION_FAILED
;
857 if ((rp
& (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
858 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
862 /* Count the number of bytes available in the fifo without
863 * crossing the wrap around. Make sure to not fill it completely,
864 * because that would make wp == rp and that's the empty condition. */
865 uint32_t thisrun_bytes
;
867 thisrun_bytes
= rp
- wp
- block_size
;
868 else if (rp
> fifo_start_addr
)
869 thisrun_bytes
= fifo_end_addr
- wp
;
871 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
873 if (thisrun_bytes
== 0) {
874 /* Throttle polling a bit if transfer is (much) faster than flash
875 * programming. The exact delay shouldn't matter as long as it's
876 * less than buffer size / flash speed. This is very unlikely to
877 * run when using high latency connections such as USB. */
880 /* to stop an infinite loop on some targets check and increment a timeout
881 * this issue was observed on a stellaris using the new ICDI interface */
882 if (timeout
++ >= 500) {
883 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
884 return ERROR_FLASH_OPERATION_FAILED
;
889 /* reset our timeout */
892 /* Limit to the amount of data we actually want to write */
893 if (thisrun_bytes
> count
* block_size
)
894 thisrun_bytes
= count
* block_size
;
896 /* Write data to fifo */
897 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
898 if (retval
!= ERROR_OK
)
901 /* Update counters and wrap write pointer */
902 buffer
+= thisrun_bytes
;
903 count
-= thisrun_bytes
/ block_size
;
905 if (wp
>= fifo_end_addr
)
906 wp
= fifo_start_addr
;
908 /* Store updated write pointer to target */
909 retval
= target_write_u32(target
, wp_addr
, wp
);
910 if (retval
!= ERROR_OK
)
914 if (retval
!= ERROR_OK
) {
915 /* abort flash write algorithm on target */
916 target_write_u32(target
, wp_addr
, 0);
919 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
920 num_reg_params
, reg_params
,
925 if (retval2
!= ERROR_OK
) {
926 LOG_ERROR("error waiting for target flash write algorithm");
933 int target_read_memory(struct target
*target
,
934 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
936 if (!target_was_examined(target
)) {
937 LOG_ERROR("Target not examined yet");
940 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
943 int target_read_phys_memory(struct target
*target
,
944 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
946 if (!target_was_examined(target
)) {
947 LOG_ERROR("Target not examined yet");
950 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
953 int target_write_memory(struct target
*target
,
954 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
956 if (!target_was_examined(target
)) {
957 LOG_ERROR("Target not examined yet");
960 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
963 int target_write_phys_memory(struct target
*target
,
964 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
966 if (!target_was_examined(target
)) {
967 LOG_ERROR("Target not examined yet");
970 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
973 static int target_bulk_write_memory_default(struct target
*target
,
974 uint32_t address
, uint32_t count
, const uint8_t *buffer
)
976 return target_write_memory(target
, address
, 4, count
, buffer
);
979 int target_add_breakpoint(struct target
*target
,
980 struct breakpoint
*breakpoint
)
982 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
983 LOG_WARNING("target %s is not halted", target_name(target
));
984 return ERROR_TARGET_NOT_HALTED
;
986 return target
->type
->add_breakpoint(target
, breakpoint
);
989 int target_add_context_breakpoint(struct target
*target
,
990 struct breakpoint
*breakpoint
)
992 if (target
->state
!= TARGET_HALTED
) {
993 LOG_WARNING("target %s is not halted", target_name(target
));
994 return ERROR_TARGET_NOT_HALTED
;
996 return target
->type
->add_context_breakpoint(target
, breakpoint
);
999 int target_add_hybrid_breakpoint(struct target
*target
,
1000 struct breakpoint
*breakpoint
)
1002 if (target
->state
!= TARGET_HALTED
) {
1003 LOG_WARNING("target %s is not halted", target_name(target
));
1004 return ERROR_TARGET_NOT_HALTED
;
1006 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1009 int target_remove_breakpoint(struct target
*target
,
1010 struct breakpoint
*breakpoint
)
1012 return target
->type
->remove_breakpoint(target
, breakpoint
);
1015 int target_add_watchpoint(struct target
*target
,
1016 struct watchpoint
*watchpoint
)
1018 if (target
->state
!= TARGET_HALTED
) {
1019 LOG_WARNING("target %s is not halted", target_name(target
));
1020 return ERROR_TARGET_NOT_HALTED
;
1022 return target
->type
->add_watchpoint(target
, watchpoint
);
1024 int target_remove_watchpoint(struct target
*target
,
1025 struct watchpoint
*watchpoint
)
1027 return target
->type
->remove_watchpoint(target
, watchpoint
);
1030 int target_get_gdb_reg_list(struct target
*target
,
1031 struct reg
**reg_list
[], int *reg_list_size
)
1033 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
);
1035 int target_step(struct target
*target
,
1036 int current
, uint32_t address
, int handle_breakpoints
)
1038 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1042 * Reset the @c examined flag for the given target.
1043 * Pure paranoia -- targets are zeroed on allocation.
1045 static void target_reset_examined(struct target
*target
)
1047 target
->examined
= false;
1050 static int err_read_phys_memory(struct target
*target
, uint32_t address
,
1051 uint32_t size
, uint32_t count
, uint8_t *buffer
)
1053 LOG_ERROR("Not implemented: %s", __func__
);
1057 static int err_write_phys_memory(struct target
*target
, uint32_t address
,
1058 uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1060 LOG_ERROR("Not implemented: %s", __func__
);
1064 static int handle_target(void *priv
);
1066 static int target_init_one(struct command_context
*cmd_ctx
,
1067 struct target
*target
)
1069 target_reset_examined(target
);
1071 struct target_type
*type
= target
->type
;
1072 if (type
->examine
== NULL
)
1073 type
->examine
= default_examine
;
1075 if (type
->check_reset
== NULL
)
1076 type
->check_reset
= default_check_reset
;
1078 assert(type
->init_target
!= NULL
);
1080 int retval
= type
->init_target(cmd_ctx
, target
);
1081 if (ERROR_OK
!= retval
) {
1082 LOG_ERROR("target '%s' init failed", target_name(target
));
1086 /* Sanity-check MMU support ... stub in what we must, to help
1087 * implement it in stages, but warn if we need to do so.
1090 if (type
->write_phys_memory
== NULL
) {
1091 LOG_ERROR("type '%s' is missing write_phys_memory",
1093 type
->write_phys_memory
= err_write_phys_memory
;
1095 if (type
->read_phys_memory
== NULL
) {
1096 LOG_ERROR("type '%s' is missing read_phys_memory",
1098 type
->read_phys_memory
= err_read_phys_memory
;
1100 if (type
->virt2phys
== NULL
) {
1101 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1102 type
->virt2phys
= identity_virt2phys
;
1105 /* Make sure no-MMU targets all behave the same: make no
1106 * distinction between physical and virtual addresses, and
1107 * ensure that virt2phys() is always an identity mapping.
1109 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1110 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1113 type
->write_phys_memory
= type
->write_memory
;
1114 type
->read_phys_memory
= type
->read_memory
;
1115 type
->virt2phys
= identity_virt2phys
;
1118 if (target
->type
->read_buffer
== NULL
)
1119 target
->type
->read_buffer
= target_read_buffer_default
;
1121 if (target
->type
->write_buffer
== NULL
)
1122 target
->type
->write_buffer
= target_write_buffer_default
;
1124 if (target
->type
->bulk_write_memory
== NULL
)
1125 target
->type
->bulk_write_memory
= target_bulk_write_memory_default
;
1130 static int target_init(struct command_context
*cmd_ctx
)
1132 struct target
*target
;
1135 for (target
= all_targets
; target
; target
= target
->next
) {
1136 retval
= target_init_one(cmd_ctx
, target
);
1137 if (ERROR_OK
!= retval
)
1144 retval
= target_register_user_commands(cmd_ctx
);
1145 if (ERROR_OK
!= retval
)
1148 retval
= target_register_timer_callback(&handle_target
,
1149 polling_interval
, 1, cmd_ctx
->interp
);
1150 if (ERROR_OK
!= retval
)
1156 COMMAND_HANDLER(handle_target_init_command
)
1161 return ERROR_COMMAND_SYNTAX_ERROR
;
1163 static bool target_initialized
;
1164 if (target_initialized
) {
1165 LOG_INFO("'target init' has already been called");
1168 target_initialized
= true;
1170 retval
= command_run_line(CMD_CTX
, "init_targets");
1171 if (ERROR_OK
!= retval
)
1174 retval
= command_run_line(CMD_CTX
, "init_board");
1175 if (ERROR_OK
!= retval
)
1178 LOG_DEBUG("Initializing targets...");
1179 return target_init(CMD_CTX
);
1182 int target_register_event_callback(int (*callback
)(struct target
*target
,
1183 enum target_event event
, void *priv
), void *priv
)
1185 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1187 if (callback
== NULL
)
1188 return ERROR_COMMAND_SYNTAX_ERROR
;
1191 while ((*callbacks_p
)->next
)
1192 callbacks_p
= &((*callbacks_p
)->next
);
1193 callbacks_p
= &((*callbacks_p
)->next
);
1196 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1197 (*callbacks_p
)->callback
= callback
;
1198 (*callbacks_p
)->priv
= priv
;
1199 (*callbacks_p
)->next
= NULL
;
1204 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1206 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1209 if (callback
== NULL
)
1210 return ERROR_COMMAND_SYNTAX_ERROR
;
1213 while ((*callbacks_p
)->next
)
1214 callbacks_p
= &((*callbacks_p
)->next
);
1215 callbacks_p
= &((*callbacks_p
)->next
);
1218 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1219 (*callbacks_p
)->callback
= callback
;
1220 (*callbacks_p
)->periodic
= periodic
;
1221 (*callbacks_p
)->time_ms
= time_ms
;
1223 gettimeofday(&now
, NULL
);
1224 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
1225 time_ms
-= (time_ms
% 1000);
1226 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
1227 if ((*callbacks_p
)->when
.tv_usec
> 1000000) {
1228 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
1229 (*callbacks_p
)->when
.tv_sec
+= 1;
1232 (*callbacks_p
)->priv
= priv
;
1233 (*callbacks_p
)->next
= NULL
;
1238 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1239 enum target_event event
, void *priv
), void *priv
)
1241 struct target_event_callback
**p
= &target_event_callbacks
;
1242 struct target_event_callback
*c
= target_event_callbacks
;
1244 if (callback
== NULL
)
1245 return ERROR_COMMAND_SYNTAX_ERROR
;
1248 struct target_event_callback
*next
= c
->next
;
1249 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1261 static int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1263 struct target_timer_callback
**p
= &target_timer_callbacks
;
1264 struct target_timer_callback
*c
= target_timer_callbacks
;
1266 if (callback
== NULL
)
1267 return ERROR_COMMAND_SYNTAX_ERROR
;
1270 struct target_timer_callback
*next
= c
->next
;
1271 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1283 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1285 struct target_event_callback
*callback
= target_event_callbacks
;
1286 struct target_event_callback
*next_callback
;
1288 if (event
== TARGET_EVENT_HALTED
) {
1289 /* execute early halted first */
1290 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1293 LOG_DEBUG("target event %i (%s)", event
,
1294 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1296 target_handle_event(target
, event
);
1299 next_callback
= callback
->next
;
1300 callback
->callback(target
, event
, callback
->priv
);
1301 callback
= next_callback
;
1307 static int target_timer_callback_periodic_restart(
1308 struct target_timer_callback
*cb
, struct timeval
*now
)
1310 int time_ms
= cb
->time_ms
;
1311 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1312 time_ms
-= (time_ms
% 1000);
1313 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1314 if (cb
->when
.tv_usec
> 1000000) {
1315 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1316 cb
->when
.tv_sec
+= 1;
1321 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1322 struct timeval
*now
)
1324 cb
->callback(cb
->priv
);
1327 return target_timer_callback_periodic_restart(cb
, now
);
1329 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1332 static int target_call_timer_callbacks_check_time(int checktime
)
1337 gettimeofday(&now
, NULL
);
1339 struct target_timer_callback
*callback
= target_timer_callbacks
;
1341 /* cleaning up may unregister and free this callback */
1342 struct target_timer_callback
*next_callback
= callback
->next
;
1344 bool call_it
= callback
->callback
&&
1345 ((!checktime
&& callback
->periodic
) ||
1346 now
.tv_sec
> callback
->when
.tv_sec
||
1347 (now
.tv_sec
== callback
->when
.tv_sec
&&
1348 now
.tv_usec
>= callback
->when
.tv_usec
));
1351 int retval
= target_call_timer_callback(callback
, &now
);
1352 if (retval
!= ERROR_OK
)
1356 callback
= next_callback
;
1362 int target_call_timer_callbacks(void)
1364 return target_call_timer_callbacks_check_time(1);
1367 /* invoke periodic callbacks immediately */
1368 int target_call_timer_callbacks_now(void)
1370 return target_call_timer_callbacks_check_time(0);
1373 /* Prints the working area layout for debug purposes */
1374 static void print_wa_layout(struct target
*target
)
1376 struct working_area
*c
= target
->working_areas
;
1379 LOG_DEBUG("%c%c 0x%08"PRIx32
"-0x%08"PRIx32
" (%"PRIu32
" bytes)",
1380 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1381 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1386 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1387 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1389 assert(area
->free
); /* Shouldn't split an allocated area */
1390 assert(size
<= area
->size
); /* Caller should guarantee this */
1392 /* Split only if not already the right size */
1393 if (size
< area
->size
) {
1394 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1399 new_wa
->next
= area
->next
;
1400 new_wa
->size
= area
->size
- size
;
1401 new_wa
->address
= area
->address
+ size
;
1402 new_wa
->backup
= NULL
;
1403 new_wa
->user
= NULL
;
1404 new_wa
->free
= true;
1406 area
->next
= new_wa
;
1409 /* If backup memory was allocated to this area, it has the wrong size
1410 * now so free it and it will be reallocated if/when needed */
1413 area
->backup
= NULL
;
1418 /* Merge all adjacent free areas into one */
1419 static void target_merge_working_areas(struct target
*target
)
1421 struct working_area
*c
= target
->working_areas
;
1423 while (c
&& c
->next
) {
1424 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1426 /* Find two adjacent free areas */
1427 if (c
->free
&& c
->next
->free
) {
1428 /* Merge the last into the first */
1429 c
->size
+= c
->next
->size
;
1431 /* Remove the last */
1432 struct working_area
*to_be_freed
= c
->next
;
1433 c
->next
= c
->next
->next
;
1434 if (to_be_freed
->backup
)
1435 free(to_be_freed
->backup
);
1438 /* If backup memory was allocated to the remaining area, it's has
1439 * the wrong size now */
1450 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1452 /* Reevaluate working area address based on MMU state*/
1453 if (target
->working_areas
== NULL
) {
1457 retval
= target
->type
->mmu(target
, &enabled
);
1458 if (retval
!= ERROR_OK
)
1462 if (target
->working_area_phys_spec
) {
1463 LOG_DEBUG("MMU disabled, using physical "
1464 "address for working memory 0x%08"PRIx32
,
1465 target
->working_area_phys
);
1466 target
->working_area
= target
->working_area_phys
;
1468 LOG_ERROR("No working memory available. "
1469 "Specify -work-area-phys to target.");
1470 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1473 if (target
->working_area_virt_spec
) {
1474 LOG_DEBUG("MMU enabled, using virtual "
1475 "address for working memory 0x%08"PRIx32
,
1476 target
->working_area_virt
);
1477 target
->working_area
= target
->working_area_virt
;
1479 LOG_ERROR("No working memory available. "
1480 "Specify -work-area-virt to target.");
1481 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1485 /* Set up initial working area on first call */
1486 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1488 new_wa
->next
= NULL
;
1489 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1490 new_wa
->address
= target
->working_area
;
1491 new_wa
->backup
= NULL
;
1492 new_wa
->user
= NULL
;
1493 new_wa
->free
= true;
1496 target
->working_areas
= new_wa
;
1499 /* only allocate multiples of 4 byte */
1501 size
= (size
+ 3) & (~3UL);
1503 struct working_area
*c
= target
->working_areas
;
1505 /* Find the first large enough working area */
1507 if (c
->free
&& c
->size
>= size
)
1513 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1515 /* Split the working area into the requested size */
1516 target_split_working_area(c
, size
);
1518 LOG_DEBUG("allocated new working area of %"PRIu32
" bytes at address 0x%08"PRIx32
, size
, c
->address
);
1520 if (target
->backup_working_area
) {
1521 if (c
->backup
== NULL
) {
1522 c
->backup
= malloc(c
->size
);
1523 if (c
->backup
== NULL
)
1527 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1528 if (retval
!= ERROR_OK
)
1532 /* mark as used, and return the new (reused) area */
1539 print_wa_layout(target
);
1544 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1548 retval
= target_alloc_working_area_try(target
, size
, area
);
1549 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1550 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1555 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1557 int retval
= ERROR_OK
;
1559 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1560 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1561 if (retval
!= ERROR_OK
)
1562 LOG_ERROR("failed to restore %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1563 area
->size
, area
->address
);
1569 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1570 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1572 int retval
= ERROR_OK
;
1578 retval
= target_restore_working_area(target
, area
);
1579 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1580 if (retval
!= ERROR_OK
)
1586 LOG_DEBUG("freed %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1587 area
->size
, area
->address
);
1589 /* mark user pointer invalid */
1590 /* TODO: Is this really safe? It points to some previous caller's memory.
1591 * How could we know that the area pointer is still in that place and not
1592 * some other vital data? What's the purpose of this, anyway? */
1596 target_merge_working_areas(target
);
1598 print_wa_layout(target
);
1603 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1605 return target_free_working_area_restore(target
, area
, 1);
1608 /* free resources and restore memory, if restoring memory fails,
1609 * free up resources anyway
1611 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1613 struct working_area
*c
= target
->working_areas
;
1615 LOG_DEBUG("freeing all working areas");
1617 /* Loop through all areas, restoring the allocated ones and marking them as free */
1621 target_restore_working_area(target
, c
);
1623 *c
->user
= NULL
; /* Same as above */
1629 /* Run a merge pass to combine all areas into one */
1630 target_merge_working_areas(target
);
1632 print_wa_layout(target
);
1635 void target_free_all_working_areas(struct target
*target
)
1637 target_free_all_working_areas_restore(target
, 1);
1640 /* Find the largest number of bytes that can be allocated */
1641 uint32_t target_get_working_area_avail(struct target
*target
)
1643 struct working_area
*c
= target
->working_areas
;
1644 uint32_t max_size
= 0;
1647 return target
->working_area_size
;
1650 if (c
->free
&& max_size
< c
->size
)
1659 int target_arch_state(struct target
*target
)
1662 if (target
== NULL
) {
1663 LOG_USER("No target has been configured");
1667 LOG_USER("target state: %s", target_state_name(target
));
1669 if (target
->state
!= TARGET_HALTED
)
1672 retval
= target
->type
->arch_state(target
);
1676 /* Single aligned words are guaranteed to use 16 or 32 bit access
1677 * mode respectively, otherwise data is handled as quickly as
1680 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
1682 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1683 (int)size
, (unsigned)address
);
1685 if (!target_was_examined(target
)) {
1686 LOG_ERROR("Target not examined yet");
1693 if ((address
+ size
- 1) < address
) {
1694 /* GDB can request this when e.g. PC is 0xfffffffc*/
1695 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1701 return target
->type
->write_buffer(target
, address
, size
, buffer
);
1704 static int target_write_buffer_default(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
1706 int retval
= ERROR_OK
;
1708 if (((address
% 2) == 0) && (size
== 2))
1709 return target_write_memory(target
, address
, 2, 1, buffer
);
1711 /* handle unaligned head bytes */
1713 uint32_t unaligned
= 4 - (address
% 4);
1715 if (unaligned
> size
)
1718 retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
);
1719 if (retval
!= ERROR_OK
)
1722 buffer
+= unaligned
;
1723 address
+= unaligned
;
1727 /* handle aligned words */
1729 int aligned
= size
- (size
% 4);
1731 /* use bulk writes above a certain limit. This may have to be changed */
1732 if (aligned
> 128) {
1733 retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
);
1734 if (retval
!= ERROR_OK
)
1737 retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
);
1738 if (retval
!= ERROR_OK
)
1747 /* handle tail writes of less than 4 bytes */
1749 retval
= target_write_memory(target
, address
, 1, size
, buffer
);
1750 if (retval
!= ERROR_OK
)
1757 /* Single aligned words are guaranteed to use 16 or 32 bit access
1758 * mode respectively, otherwise data is handled as quickly as
1761 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1763 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1764 (int)size
, (unsigned)address
);
1766 if (!target_was_examined(target
)) {
1767 LOG_ERROR("Target not examined yet");
1774 if ((address
+ size
- 1) < address
) {
1775 /* GDB can request this when e.g. PC is 0xfffffffc*/
1776 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1782 return target
->type
->read_buffer(target
, address
, size
, buffer
);
1785 static int target_read_buffer_default(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1787 int retval
= ERROR_OK
;
1789 if (((address
% 2) == 0) && (size
== 2))
1790 return target_read_memory(target
, address
, 2, 1, buffer
);
1792 /* handle unaligned head bytes */
1794 uint32_t unaligned
= 4 - (address
% 4);
1796 if (unaligned
> size
)
1799 retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
);
1800 if (retval
!= ERROR_OK
)
1803 buffer
+= unaligned
;
1804 address
+= unaligned
;
1808 /* handle aligned words */
1810 int aligned
= size
- (size
% 4);
1812 retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
);
1813 if (retval
!= ERROR_OK
)
1821 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1823 int aligned
= size
- (size
% 2);
1824 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1825 if (retval
!= ERROR_OK
)
1832 /* handle tail writes of less than 4 bytes */
1834 retval
= target_read_memory(target
, address
, 1, size
, buffer
);
1835 if (retval
!= ERROR_OK
)
1842 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1847 uint32_t checksum
= 0;
1848 if (!target_was_examined(target
)) {
1849 LOG_ERROR("Target not examined yet");
1853 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
1854 if (retval
!= ERROR_OK
) {
1855 buffer
= malloc(size
);
1856 if (buffer
== NULL
) {
1857 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1858 return ERROR_COMMAND_SYNTAX_ERROR
;
1860 retval
= target_read_buffer(target
, address
, size
, buffer
);
1861 if (retval
!= ERROR_OK
) {
1866 /* convert to target endianness */
1867 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
1868 uint32_t target_data
;
1869 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1870 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1873 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1882 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1885 if (!target_was_examined(target
)) {
1886 LOG_ERROR("Target not examined yet");
1890 if (target
->type
->blank_check_memory
== 0)
1891 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1893 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1898 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1900 uint8_t value_buf
[4];
1901 if (!target_was_examined(target
)) {
1902 LOG_ERROR("Target not examined yet");
1906 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1908 if (retval
== ERROR_OK
) {
1909 *value
= target_buffer_get_u32(target
, value_buf
);
1910 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1915 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1922 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
1924 uint8_t value_buf
[2];
1925 if (!target_was_examined(target
)) {
1926 LOG_ERROR("Target not examined yet");
1930 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1932 if (retval
== ERROR_OK
) {
1933 *value
= target_buffer_get_u16(target
, value_buf
);
1934 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1939 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1946 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
1948 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1949 if (!target_was_examined(target
)) {
1950 LOG_ERROR("Target not examined yet");
1954 if (retval
== ERROR_OK
) {
1955 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1960 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1967 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
1970 uint8_t value_buf
[4];
1971 if (!target_was_examined(target
)) {
1972 LOG_ERROR("Target not examined yet");
1976 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1980 target_buffer_set_u32(target
, value_buf
, value
);
1981 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
1982 if (retval
!= ERROR_OK
)
1983 LOG_DEBUG("failed: %i", retval
);
1988 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
1991 uint8_t value_buf
[2];
1992 if (!target_was_examined(target
)) {
1993 LOG_ERROR("Target not examined yet");
1997 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
2001 target_buffer_set_u16(target
, value_buf
, value
);
2002 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2003 if (retval
!= ERROR_OK
)
2004 LOG_DEBUG("failed: %i", retval
);
2009 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
2012 if (!target_was_examined(target
)) {
2013 LOG_ERROR("Target not examined yet");
2017 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
2020 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2021 if (retval
!= ERROR_OK
)
2022 LOG_DEBUG("failed: %i", retval
);
2027 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2029 struct target
*target
= get_target(name
);
2030 if (target
== NULL
) {
2031 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2034 if (!target
->tap
->enabled
) {
2035 LOG_USER("Target: TAP %s is disabled, "
2036 "can't be the current target\n",
2037 target
->tap
->dotted_name
);
2041 cmd_ctx
->current_target
= target
->target_number
;
2046 COMMAND_HANDLER(handle_targets_command
)
2048 int retval
= ERROR_OK
;
2049 if (CMD_ARGC
== 1) {
2050 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2051 if (retval
== ERROR_OK
) {
2057 struct target
*target
= all_targets
;
2058 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2059 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2064 if (target
->tap
->enabled
)
2065 state
= target_state_name(target
);
2067 state
= "tap-disabled";
2069 if (CMD_CTX
->current_target
== target
->target_number
)
2072 /* keep columns lined up to match the headers above */
2073 command_print(CMD_CTX
,
2074 "%2d%c %-18s %-10s %-6s %-18s %s",
2075 target
->target_number
,
2077 target_name(target
),
2078 target_type_name(target
),
2079 Jim_Nvp_value2name_simple(nvp_target_endian
,
2080 target
->endianness
)->name
,
2081 target
->tap
->dotted_name
,
2083 target
= target
->next
;
2089 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2091 static int powerDropout
;
2092 static int srstAsserted
;
2094 static int runPowerRestore
;
2095 static int runPowerDropout
;
2096 static int runSrstAsserted
;
2097 static int runSrstDeasserted
;
2099 static int sense_handler(void)
2101 static int prevSrstAsserted
;
2102 static int prevPowerdropout
;
2104 int retval
= jtag_power_dropout(&powerDropout
);
2105 if (retval
!= ERROR_OK
)
2109 powerRestored
= prevPowerdropout
&& !powerDropout
;
2111 runPowerRestore
= 1;
2113 long long current
= timeval_ms();
2114 static long long lastPower
;
2115 int waitMore
= lastPower
+ 2000 > current
;
2116 if (powerDropout
&& !waitMore
) {
2117 runPowerDropout
= 1;
2118 lastPower
= current
;
2121 retval
= jtag_srst_asserted(&srstAsserted
);
2122 if (retval
!= ERROR_OK
)
2126 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2128 static long long lastSrst
;
2129 waitMore
= lastSrst
+ 2000 > current
;
2130 if (srstDeasserted
&& !waitMore
) {
2131 runSrstDeasserted
= 1;
2135 if (!prevSrstAsserted
&& srstAsserted
)
2136 runSrstAsserted
= 1;
2138 prevSrstAsserted
= srstAsserted
;
2139 prevPowerdropout
= powerDropout
;
2141 if (srstDeasserted
|| powerRestored
) {
2142 /* Other than logging the event we can't do anything here.
2143 * Issuing a reset is a particularly bad idea as we might
2144 * be inside a reset already.
2151 /* process target state changes */
2152 static int handle_target(void *priv
)
2154 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2155 int retval
= ERROR_OK
;
2157 if (!is_jtag_poll_safe()) {
2158 /* polling is disabled currently */
2162 /* we do not want to recurse here... */
2163 static int recursive
;
2167 /* danger! running these procedures can trigger srst assertions and power dropouts.
2168 * We need to avoid an infinite loop/recursion here and we do that by
2169 * clearing the flags after running these events.
2171 int did_something
= 0;
2172 if (runSrstAsserted
) {
2173 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2174 Jim_Eval(interp
, "srst_asserted");
2177 if (runSrstDeasserted
) {
2178 Jim_Eval(interp
, "srst_deasserted");
2181 if (runPowerDropout
) {
2182 LOG_INFO("Power dropout detected, running power_dropout proc.");
2183 Jim_Eval(interp
, "power_dropout");
2186 if (runPowerRestore
) {
2187 Jim_Eval(interp
, "power_restore");
2191 if (did_something
) {
2192 /* clear detect flags */
2196 /* clear action flags */
2198 runSrstAsserted
= 0;
2199 runSrstDeasserted
= 0;
2200 runPowerRestore
= 0;
2201 runPowerDropout
= 0;
2206 /* Poll targets for state changes unless that's globally disabled.
2207 * Skip targets that are currently disabled.
2209 for (struct target
*target
= all_targets
;
2210 is_jtag_poll_safe() && target
;
2211 target
= target
->next
) {
2212 if (!target
->tap
->enabled
)
2215 if (target
->backoff
.times
> target
->backoff
.count
) {
2216 /* do not poll this time as we failed previously */
2217 target
->backoff
.count
++;
2220 target
->backoff
.count
= 0;
2222 /* only poll target if we've got power and srst isn't asserted */
2223 if (!powerDropout
&& !srstAsserted
) {
2224 /* polling may fail silently until the target has been examined */
2225 retval
= target_poll(target
);
2226 if (retval
!= ERROR_OK
) {
2227 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2228 if (target
->backoff
.times
* polling_interval
< 5000) {
2229 target
->backoff
.times
*= 2;
2230 target
->backoff
.times
++;
2232 LOG_USER("Polling target %s failed, GDB will be halted. Polling again in %dms",
2233 target_name(target
),
2234 target
->backoff
.times
* polling_interval
);
2236 /* Tell GDB to halt the debugger. This allows the user to
2237 * run monitor commands to handle the situation.
2239 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2242 /* Since we succeeded, we reset backoff count */
2243 if (target
->backoff
.times
> 0)
2244 LOG_USER("Polling target %s succeeded again", target_name(target
));
2245 target
->backoff
.times
= 0;
2252 COMMAND_HANDLER(handle_reg_command
)
2254 struct target
*target
;
2255 struct reg
*reg
= NULL
;
2261 target
= get_current_target(CMD_CTX
);
2263 /* list all available registers for the current target */
2264 if (CMD_ARGC
== 0) {
2265 struct reg_cache
*cache
= target
->reg_cache
;
2271 command_print(CMD_CTX
, "===== %s", cache
->name
);
2273 for (i
= 0, reg
= cache
->reg_list
;
2274 i
< cache
->num_regs
;
2275 i
++, reg
++, count
++) {
2276 /* only print cached values if they are valid */
2278 value
= buf_to_str(reg
->value
,
2280 command_print(CMD_CTX
,
2281 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2289 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2294 cache
= cache
->next
;
2300 /* access a single register by its ordinal number */
2301 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2303 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2305 struct reg_cache
*cache
= target
->reg_cache
;
2309 for (i
= 0; i
< cache
->num_regs
; i
++) {
2310 if (count
++ == num
) {
2311 reg
= &cache
->reg_list
[i
];
2317 cache
= cache
->next
;
2321 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2322 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2326 /* access a single register by its name */
2327 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2330 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2335 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2337 /* display a register */
2338 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2339 && (CMD_ARGV
[1][0] <= '9')))) {
2340 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2343 if (reg
->valid
== 0)
2344 reg
->type
->get(reg
);
2345 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2346 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2351 /* set register value */
2352 if (CMD_ARGC
== 2) {
2353 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2356 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2358 reg
->type
->set(reg
, buf
);
2360 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2361 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2369 return ERROR_COMMAND_SYNTAX_ERROR
;
2372 COMMAND_HANDLER(handle_poll_command
)
2374 int retval
= ERROR_OK
;
2375 struct target
*target
= get_current_target(CMD_CTX
);
2377 if (CMD_ARGC
== 0) {
2378 command_print(CMD_CTX
, "background polling: %s",
2379 jtag_poll_get_enabled() ? "on" : "off");
2380 command_print(CMD_CTX
, "TAP: %s (%s)",
2381 target
->tap
->dotted_name
,
2382 target
->tap
->enabled
? "enabled" : "disabled");
2383 if (!target
->tap
->enabled
)
2385 retval
= target_poll(target
);
2386 if (retval
!= ERROR_OK
)
2388 retval
= target_arch_state(target
);
2389 if (retval
!= ERROR_OK
)
2391 } else if (CMD_ARGC
== 1) {
2393 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2394 jtag_poll_set_enabled(enable
);
2396 return ERROR_COMMAND_SYNTAX_ERROR
;
2401 COMMAND_HANDLER(handle_wait_halt_command
)
2404 return ERROR_COMMAND_SYNTAX_ERROR
;
2407 if (1 == CMD_ARGC
) {
2408 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2409 if (ERROR_OK
!= retval
)
2410 return ERROR_COMMAND_SYNTAX_ERROR
;
2411 /* convert seconds (given) to milliseconds (needed) */
2415 struct target
*target
= get_current_target(CMD_CTX
);
2416 return target_wait_state(target
, TARGET_HALTED
, ms
);
2419 /* wait for target state to change. The trick here is to have a low
2420 * latency for short waits and not to suck up all the CPU time
2423 * After 500ms, keep_alive() is invoked
2425 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2428 long long then
= 0, cur
;
2432 retval
= target_poll(target
);
2433 if (retval
!= ERROR_OK
)
2435 if (target
->state
== state
)
2440 then
= timeval_ms();
2441 LOG_DEBUG("waiting for target %s...",
2442 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2448 if ((cur
-then
) > ms
) {
2449 LOG_ERROR("timed out while waiting for target %s",
2450 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2458 COMMAND_HANDLER(handle_halt_command
)
2462 struct target
*target
= get_current_target(CMD_CTX
);
2463 int retval
= target_halt(target
);
2464 if (ERROR_OK
!= retval
)
2467 if (CMD_ARGC
== 1) {
2468 unsigned wait_local
;
2469 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2470 if (ERROR_OK
!= retval
)
2471 return ERROR_COMMAND_SYNTAX_ERROR
;
2476 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2479 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2481 struct target
*target
= get_current_target(CMD_CTX
);
2483 LOG_USER("requesting target halt and executing a soft reset");
2485 target_soft_reset_halt(target
);
2490 COMMAND_HANDLER(handle_reset_command
)
2493 return ERROR_COMMAND_SYNTAX_ERROR
;
2495 enum target_reset_mode reset_mode
= RESET_RUN
;
2496 if (CMD_ARGC
== 1) {
2498 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2499 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2500 return ERROR_COMMAND_SYNTAX_ERROR
;
2501 reset_mode
= n
->value
;
2504 /* reset *all* targets */
2505 return target_process_reset(CMD_CTX
, reset_mode
);
2509 COMMAND_HANDLER(handle_resume_command
)
2513 return ERROR_COMMAND_SYNTAX_ERROR
;
2515 struct target
*target
= get_current_target(CMD_CTX
);
2517 /* with no CMD_ARGV, resume from current pc, addr = 0,
2518 * with one arguments, addr = CMD_ARGV[0],
2519 * handle breakpoints, not debugging */
2521 if (CMD_ARGC
== 1) {
2522 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2526 return target_resume(target
, current
, addr
, 1, 0);
2529 COMMAND_HANDLER(handle_step_command
)
2532 return ERROR_COMMAND_SYNTAX_ERROR
;
2536 /* with no CMD_ARGV, step from current pc, addr = 0,
2537 * with one argument addr = CMD_ARGV[0],
2538 * handle breakpoints, debugging */
2541 if (CMD_ARGC
== 1) {
2542 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2546 struct target
*target
= get_current_target(CMD_CTX
);
2548 return target
->type
->step(target
, current_pc
, addr
, 1);
2551 static void handle_md_output(struct command_context
*cmd_ctx
,
2552 struct target
*target
, uint32_t address
, unsigned size
,
2553 unsigned count
, const uint8_t *buffer
)
2555 const unsigned line_bytecnt
= 32;
2556 unsigned line_modulo
= line_bytecnt
/ size
;
2558 char output
[line_bytecnt
* 4 + 1];
2559 unsigned output_len
= 0;
2561 const char *value_fmt
;
2564 value_fmt
= "%8.8x ";
2567 value_fmt
= "%4.4x ";
2570 value_fmt
= "%2.2x ";
2573 /* "can't happen", caller checked */
2574 LOG_ERROR("invalid memory read size: %u", size
);
2578 for (unsigned i
= 0; i
< count
; i
++) {
2579 if (i
% line_modulo
== 0) {
2580 output_len
+= snprintf(output
+ output_len
,
2581 sizeof(output
) - output_len
,
2583 (unsigned)(address
+ (i
*size
)));
2587 const uint8_t *value_ptr
= buffer
+ i
* size
;
2590 value
= target_buffer_get_u32(target
, value_ptr
);
2593 value
= target_buffer_get_u16(target
, value_ptr
);
2598 output_len
+= snprintf(output
+ output_len
,
2599 sizeof(output
) - output_len
,
2602 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
2603 command_print(cmd_ctx
, "%s", output
);
2609 COMMAND_HANDLER(handle_md_command
)
2612 return ERROR_COMMAND_SYNTAX_ERROR
;
2615 switch (CMD_NAME
[2]) {
2626 return ERROR_COMMAND_SYNTAX_ERROR
;
2629 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2630 int (*fn
)(struct target
*target
,
2631 uint32_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
2635 fn
= target_read_phys_memory
;
2637 fn
= target_read_memory
;
2638 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2639 return ERROR_COMMAND_SYNTAX_ERROR
;
2642 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2646 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2648 uint8_t *buffer
= calloc(count
, size
);
2650 struct target
*target
= get_current_target(CMD_CTX
);
2651 int retval
= fn(target
, address
, size
, count
, buffer
);
2652 if (ERROR_OK
== retval
)
2653 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2660 typedef int (*target_write_fn
)(struct target
*target
,
2661 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
2663 static int target_write_memory_fast(struct target
*target
,
2664 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
2666 return target_write_buffer(target
, address
, size
* count
, buffer
);
2669 static int target_fill_mem(struct target
*target
,
2678 /* We have to write in reasonably large chunks to be able
2679 * to fill large memory areas with any sane speed */
2680 const unsigned chunk_size
= 16384;
2681 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
2682 if (target_buf
== NULL
) {
2683 LOG_ERROR("Out of memory");
2687 for (unsigned i
= 0; i
< chunk_size
; i
++) {
2688 switch (data_size
) {
2690 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
2693 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
2696 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
2703 int retval
= ERROR_OK
;
2705 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
2708 if (current
> chunk_size
)
2709 current
= chunk_size
;
2710 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
2711 if (retval
!= ERROR_OK
)
2713 /* avoid GDB timeouts */
2722 COMMAND_HANDLER(handle_mw_command
)
2725 return ERROR_COMMAND_SYNTAX_ERROR
;
2726 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2731 fn
= target_write_phys_memory
;
2733 fn
= target_write_memory_fast
;
2734 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2735 return ERROR_COMMAND_SYNTAX_ERROR
;
2738 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2741 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2745 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2747 struct target
*target
= get_current_target(CMD_CTX
);
2749 switch (CMD_NAME
[2]) {
2760 return ERROR_COMMAND_SYNTAX_ERROR
;
2763 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
2766 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2767 uint32_t *min_address
, uint32_t *max_address
)
2769 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2770 return ERROR_COMMAND_SYNTAX_ERROR
;
2772 /* a base address isn't always necessary,
2773 * default to 0x0 (i.e. don't relocate) */
2774 if (CMD_ARGC
>= 2) {
2776 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2777 image
->base_address
= addr
;
2778 image
->base_address_set
= 1;
2780 image
->base_address_set
= 0;
2782 image
->start_address_set
= 0;
2785 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2786 if (CMD_ARGC
== 5) {
2787 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2788 /* use size (given) to find max (required) */
2789 *max_address
+= *min_address
;
2792 if (*min_address
> *max_address
)
2793 return ERROR_COMMAND_SYNTAX_ERROR
;
2798 COMMAND_HANDLER(handle_load_image_command
)
2802 uint32_t image_size
;
2803 uint32_t min_address
= 0;
2804 uint32_t max_address
= 0xffffffff;
2808 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2809 &image
, &min_address
, &max_address
);
2810 if (ERROR_OK
!= retval
)
2813 struct target
*target
= get_current_target(CMD_CTX
);
2815 struct duration bench
;
2816 duration_start(&bench
);
2818 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2823 for (i
= 0; i
< image
.num_sections
; i
++) {
2824 buffer
= malloc(image
.sections
[i
].size
);
2825 if (buffer
== NULL
) {
2826 command_print(CMD_CTX
,
2827 "error allocating buffer for section (%d bytes)",
2828 (int)(image
.sections
[i
].size
));
2832 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
2833 if (retval
!= ERROR_OK
) {
2838 uint32_t offset
= 0;
2839 uint32_t length
= buf_cnt
;
2841 /* DANGER!!! beware of unsigned comparision here!!! */
2843 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
2844 (image
.sections
[i
].base_address
< max_address
)) {
2846 if (image
.sections
[i
].base_address
< min_address
) {
2847 /* clip addresses below */
2848 offset
+= min_address
-image
.sections
[i
].base_address
;
2852 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2853 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2855 retval
= target_write_buffer(target
,
2856 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
2857 if (retval
!= ERROR_OK
) {
2861 image_size
+= length
;
2862 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2863 (unsigned int)length
,
2864 image
.sections
[i
].base_address
+ offset
);
2870 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
2871 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2872 "in %fs (%0.3f KiB/s)", image_size
,
2873 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2876 image_close(&image
);
2882 COMMAND_HANDLER(handle_dump_image_command
)
2884 struct fileio fileio
;
2886 int retval
, retvaltemp
;
2887 uint32_t address
, size
;
2888 struct duration bench
;
2889 struct target
*target
= get_current_target(CMD_CTX
);
2892 return ERROR_COMMAND_SYNTAX_ERROR
;
2894 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2895 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2897 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
2898 buffer
= malloc(buf_size
);
2902 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
2903 if (retval
!= ERROR_OK
) {
2908 duration_start(&bench
);
2911 size_t size_written
;
2912 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
2913 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2914 if (retval
!= ERROR_OK
)
2917 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2918 if (retval
!= ERROR_OK
)
2921 size
-= this_run_size
;
2922 address
+= this_run_size
;
2927 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
2929 retval
= fileio_size(&fileio
, &filesize
);
2930 if (retval
!= ERROR_OK
)
2932 command_print(CMD_CTX
,
2933 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize
,
2934 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
2937 retvaltemp
= fileio_close(&fileio
);
2938 if (retvaltemp
!= ERROR_OK
)
2944 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
2948 uint32_t image_size
;
2951 uint32_t checksum
= 0;
2952 uint32_t mem_checksum
= 0;
2956 struct target
*target
= get_current_target(CMD_CTX
);
2959 return ERROR_COMMAND_SYNTAX_ERROR
;
2962 LOG_ERROR("no target selected");
2966 struct duration bench
;
2967 duration_start(&bench
);
2969 if (CMD_ARGC
>= 2) {
2971 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2972 image
.base_address
= addr
;
2973 image
.base_address_set
= 1;
2975 image
.base_address_set
= 0;
2976 image
.base_address
= 0x0;
2979 image
.start_address_set
= 0;
2981 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
2982 if (retval
!= ERROR_OK
)
2988 for (i
= 0; i
< image
.num_sections
; i
++) {
2989 buffer
= malloc(image
.sections
[i
].size
);
2990 if (buffer
== NULL
) {
2991 command_print(CMD_CTX
,
2992 "error allocating buffer for section (%d bytes)",
2993 (int)(image
.sections
[i
].size
));
2996 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
2997 if (retval
!= ERROR_OK
) {
3003 /* calculate checksum of image */
3004 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3005 if (retval
!= ERROR_OK
) {
3010 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3011 if (retval
!= ERROR_OK
) {
3016 if (checksum
!= mem_checksum
) {
3017 /* failed crc checksum, fall back to a binary compare */
3021 LOG_ERROR("checksum mismatch - attempting binary compare");
3023 data
= (uint8_t *)malloc(buf_cnt
);
3025 /* Can we use 32bit word accesses? */
3027 int count
= buf_cnt
;
3028 if ((count
% 4) == 0) {
3032 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3033 if (retval
== ERROR_OK
) {
3035 for (t
= 0; t
< buf_cnt
; t
++) {
3036 if (data
[t
] != buffer
[t
]) {
3037 command_print(CMD_CTX
,
3038 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3040 (unsigned)(t
+ image
.sections
[i
].base_address
),
3043 if (diffs
++ >= 127) {
3044 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3056 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
3057 image
.sections
[i
].base_address
,
3062 image_size
+= buf_cnt
;
3065 command_print(CMD_CTX
, "No more differences found.");
3068 retval
= ERROR_FAIL
;
3069 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3070 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3071 "in %fs (%0.3f KiB/s)", image_size
,
3072 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3075 image_close(&image
);
3080 COMMAND_HANDLER(handle_verify_image_command
)
3082 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
3085 COMMAND_HANDLER(handle_test_image_command
)
3087 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
3090 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3092 struct target
*target
= get_current_target(cmd_ctx
);
3093 struct breakpoint
*breakpoint
= target
->breakpoints
;
3094 while (breakpoint
) {
3095 if (breakpoint
->type
== BKPT_SOFT
) {
3096 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3097 breakpoint
->length
, 16);
3098 command_print(cmd_ctx
, "IVA breakpoint: 0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
3099 breakpoint
->address
,
3101 breakpoint
->set
, buf
);
3104 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3105 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3107 breakpoint
->length
, breakpoint
->set
);
3108 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3109 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3110 breakpoint
->address
,
3111 breakpoint
->length
, breakpoint
->set
);
3112 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3115 command_print(cmd_ctx
, "Breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3116 breakpoint
->address
,
3117 breakpoint
->length
, breakpoint
->set
);
3120 breakpoint
= breakpoint
->next
;
3125 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
3126 uint32_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3128 struct target
*target
= get_current_target(cmd_ctx
);
3131 int retval
= breakpoint_add(target
, addr
, length
, hw
);
3132 if (ERROR_OK
== retval
)
3133 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
3135 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3138 } else if (addr
== 0) {
3139 int retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3140 if (ERROR_OK
== retval
)
3141 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3143 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3147 int retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3148 if (ERROR_OK
== retval
)
3149 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3151 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3158 COMMAND_HANDLER(handle_bp_command
)
3167 return handle_bp_command_list(CMD_CTX
);
3171 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3172 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3173 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3176 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3178 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3180 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3183 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3184 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
3186 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3187 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3189 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3194 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3195 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3196 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3197 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3200 return ERROR_COMMAND_SYNTAX_ERROR
;
3204 COMMAND_HANDLER(handle_rbp_command
)
3207 return ERROR_COMMAND_SYNTAX_ERROR
;
3210 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3212 struct target
*target
= get_current_target(CMD_CTX
);
3213 breakpoint_remove(target
, addr
);
3218 COMMAND_HANDLER(handle_wp_command
)
3220 struct target
*target
= get_current_target(CMD_CTX
);
3222 if (CMD_ARGC
== 0) {
3223 struct watchpoint
*watchpoint
= target
->watchpoints
;
3225 while (watchpoint
) {
3226 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
3227 ", len: 0x%8.8" PRIx32
3228 ", r/w/a: %i, value: 0x%8.8" PRIx32
3229 ", mask: 0x%8.8" PRIx32
,
3230 watchpoint
->address
,
3232 (int)watchpoint
->rw
,
3235 watchpoint
= watchpoint
->next
;
3240 enum watchpoint_rw type
= WPT_ACCESS
;
3242 uint32_t length
= 0;
3243 uint32_t data_value
= 0x0;
3244 uint32_t data_mask
= 0xffffffff;
3248 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3251 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3254 switch (CMD_ARGV
[2][0]) {
3265 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3266 return ERROR_COMMAND_SYNTAX_ERROR
;
3270 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3271 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3275 return ERROR_COMMAND_SYNTAX_ERROR
;
3278 int retval
= watchpoint_add(target
, addr
, length
, type
,
3279 data_value
, data_mask
);
3280 if (ERROR_OK
!= retval
)
3281 LOG_ERROR("Failure setting watchpoints");
3286 COMMAND_HANDLER(handle_rwp_command
)
3289 return ERROR_COMMAND_SYNTAX_ERROR
;
3292 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3294 struct target
*target
= get_current_target(CMD_CTX
);
3295 watchpoint_remove(target
, addr
);
3301 * Translate a virtual address to a physical address.
3303 * The low-level target implementation must have logged a detailed error
3304 * which is forwarded to telnet/GDB session.
3306 COMMAND_HANDLER(handle_virt2phys_command
)
3309 return ERROR_COMMAND_SYNTAX_ERROR
;
3312 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
3315 struct target
*target
= get_current_target(CMD_CTX
);
3316 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3317 if (retval
== ERROR_OK
)
3318 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
3323 static void writeData(FILE *f
, const void *data
, size_t len
)
3325 size_t written
= fwrite(data
, 1, len
, f
);
3327 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3330 static void writeLong(FILE *f
, int l
)
3333 for (i
= 0; i
< 4; i
++) {
3334 char c
= (l
>> (i
*8))&0xff;
3335 writeData(f
, &c
, 1);
3340 static void writeString(FILE *f
, char *s
)
3342 writeData(f
, s
, strlen(s
));
3345 /* Dump a gmon.out histogram file. */
3346 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
)
3349 FILE *f
= fopen(filename
, "w");
3352 writeString(f
, "gmon");
3353 writeLong(f
, 0x00000001); /* Version */
3354 writeLong(f
, 0); /* padding */
3355 writeLong(f
, 0); /* padding */
3356 writeLong(f
, 0); /* padding */
3358 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3359 writeData(f
, &zero
, 1);
3361 /* figure out bucket size */
3362 uint32_t min
= samples
[0];
3363 uint32_t max
= samples
[0];
3364 for (i
= 0; i
< sampleNum
; i
++) {
3365 if (min
> samples
[i
])
3367 if (max
< samples
[i
])
3371 int addressSpace
= (max
- min
+ 1);
3372 assert(addressSpace
>= 2);
3374 static const uint32_t maxBuckets
= 16 * 1024; /* maximum buckets. */
3375 uint32_t length
= addressSpace
;
3376 if (length
> maxBuckets
)
3377 length
= maxBuckets
;
3378 int *buckets
= malloc(sizeof(int)*length
);
3379 if (buckets
== NULL
) {
3383 memset(buckets
, 0, sizeof(int) * length
);
3384 for (i
= 0; i
< sampleNum
; i
++) {
3385 uint32_t address
= samples
[i
];
3386 long long a
= address
- min
;
3387 long long b
= length
- 1;
3388 long long c
= addressSpace
- 1;
3389 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
3393 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3394 writeLong(f
, min
); /* low_pc */
3395 writeLong(f
, max
); /* high_pc */
3396 writeLong(f
, length
); /* # of samples */
3397 writeLong(f
, 100); /* KLUDGE! We lie, ca. 100Hz best case. */
3398 writeString(f
, "seconds");
3399 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3400 writeData(f
, &zero
, 1);
3401 writeString(f
, "s");
3403 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3405 char *data
= malloc(2 * length
);
3407 for (i
= 0; i
< length
; i
++) {
3412 data
[i
* 2] = val
&0xff;
3413 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
3416 writeData(f
, data
, length
* 2);
3424 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3425 * which will be used as a random sampling of PC */
3426 COMMAND_HANDLER(handle_profile_command
)
3428 struct target
*target
= get_current_target(CMD_CTX
);
3429 struct timeval timeout
, now
;
3431 gettimeofday(&timeout
, NULL
);
3433 return ERROR_COMMAND_SYNTAX_ERROR
;
3435 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], offset
);
3437 timeval_add_time(&timeout
, offset
, 0);
3440 * @todo: Some cores let us sample the PC without the
3441 * annoying halt/resume step; for example, ARMv7 PCSR.
3442 * Provide a way to use that more efficient mechanism.
3445 command_print(CMD_CTX
, "Starting profiling. Halting and resuming the target as often as we can...");
3447 static const int maxSample
= 10000;
3448 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
3449 if (samples
== NULL
)
3453 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3454 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
3456 int retval
= ERROR_OK
;
3458 target_poll(target
);
3459 if (target
->state
== TARGET_HALTED
) {
3460 uint32_t t
= *((uint32_t *)reg
->value
);
3461 samples
[numSamples
++] = t
;
3462 /* current pc, addr = 0, do not handle breakpoints, not debugging */
3463 retval
= target_resume(target
, 1, 0, 0, 0);
3464 target_poll(target
);
3465 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3466 } else if (target
->state
== TARGET_RUNNING
) {
3467 /* We want to quickly sample the PC. */
3468 retval
= target_halt(target
);
3469 if (retval
!= ERROR_OK
) {
3474 command_print(CMD_CTX
, "Target not halted or running");
3478 if (retval
!= ERROR_OK
)
3481 gettimeofday(&now
, NULL
);
3482 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
)
3483 && (now
.tv_usec
>= timeout
.tv_usec
))) {
3484 command_print(CMD_CTX
, "Profiling completed. %d samples.", numSamples
);
3485 retval
= target_poll(target
);
3486 if (retval
!= ERROR_OK
) {
3490 if (target
->state
== TARGET_HALTED
) {
3491 /* current pc, addr = 0, do not handle
3492 * breakpoints, not debugging */
3493 target_resume(target
, 1, 0, 0, 0);
3495 retval
= target_poll(target
);
3496 if (retval
!= ERROR_OK
) {
3500 writeGmon(samples
, numSamples
, CMD_ARGV
[1]);
3501 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3510 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
3513 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3516 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3520 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3521 valObjPtr
= Jim_NewIntObj(interp
, val
);
3522 if (!nameObjPtr
|| !valObjPtr
) {
3527 Jim_IncrRefCount(nameObjPtr
);
3528 Jim_IncrRefCount(valObjPtr
);
3529 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3530 Jim_DecrRefCount(interp
, nameObjPtr
);
3531 Jim_DecrRefCount(interp
, valObjPtr
);
3533 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3537 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3539 struct command_context
*context
;
3540 struct target
*target
;
3542 context
= current_command_context(interp
);
3543 assert(context
!= NULL
);
3545 target
= get_current_target(context
);
3546 if (target
== NULL
) {
3547 LOG_ERROR("mem2array: no current target");
3551 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
3554 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3562 const char *varname
;
3566 /* argv[1] = name of array to receive the data
3567 * argv[2] = desired width
3568 * argv[3] = memory address
3569 * argv[4] = count of times to read
3572 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3575 varname
= Jim_GetString(argv
[0], &len
);
3576 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3578 e
= Jim_GetLong(interp
, argv
[1], &l
);
3583 e
= Jim_GetLong(interp
, argv
[2], &l
);
3587 e
= Jim_GetLong(interp
, argv
[3], &l
);
3602 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3603 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3607 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3608 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3611 if ((addr
+ (len
* width
)) < addr
) {
3612 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3613 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3616 /* absurd transfer size? */
3618 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3619 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3624 ((width
== 2) && ((addr
& 1) == 0)) ||
3625 ((width
== 4) && ((addr
& 3) == 0))) {
3629 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3630 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3633 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3642 size_t buffersize
= 4096;
3643 uint8_t *buffer
= malloc(buffersize
);
3650 /* Slurp... in buffer size chunks */
3652 count
= len
; /* in objects.. */
3653 if (count
> (buffersize
/ width
))
3654 count
= (buffersize
/ width
);
3656 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3657 if (retval
!= ERROR_OK
) {
3659 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3663 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3664 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3668 v
= 0; /* shut up gcc */
3669 for (i
= 0; i
< count
; i
++, n
++) {
3672 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3675 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3678 v
= buffer
[i
] & 0x0ff;
3681 new_int_array_element(interp
, varname
, n
, v
);
3689 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3694 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
3697 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3701 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3705 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3711 Jim_IncrRefCount(nameObjPtr
);
3712 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3713 Jim_DecrRefCount(interp
, nameObjPtr
);
3715 if (valObjPtr
== NULL
)
3718 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3719 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3724 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3726 struct command_context
*context
;
3727 struct target
*target
;
3729 context
= current_command_context(interp
);
3730 assert(context
!= NULL
);
3732 target
= get_current_target(context
);
3733 if (target
== NULL
) {
3734 LOG_ERROR("array2mem: no current target");
3738 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
3741 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
3742 int argc
, Jim_Obj
*const *argv
)
3750 const char *varname
;
3754 /* argv[1] = name of array to get the data
3755 * argv[2] = desired width
3756 * argv[3] = memory address
3757 * argv[4] = count to write
3760 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3763 varname
= Jim_GetString(argv
[0], &len
);
3764 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3766 e
= Jim_GetLong(interp
, argv
[1], &l
);
3771 e
= Jim_GetLong(interp
, argv
[2], &l
);
3775 e
= Jim_GetLong(interp
, argv
[3], &l
);
3790 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3791 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3792 "Invalid width param, must be 8/16/32", NULL
);
3796 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3797 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3798 "array2mem: zero width read?", NULL
);
3801 if ((addr
+ (len
* width
)) < addr
) {
3802 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3803 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3804 "array2mem: addr + len - wraps to zero?", NULL
);
3807 /* absurd transfer size? */
3809 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3810 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3811 "array2mem: absurd > 64K item request", NULL
);
3816 ((width
== 2) && ((addr
& 1) == 0)) ||
3817 ((width
== 4) && ((addr
& 3) == 0))) {
3821 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3822 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3825 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3836 size_t buffersize
= 4096;
3837 uint8_t *buffer
= malloc(buffersize
);
3842 /* Slurp... in buffer size chunks */
3844 count
= len
; /* in objects.. */
3845 if (count
> (buffersize
/ width
))
3846 count
= (buffersize
/ width
);
3848 v
= 0; /* shut up gcc */
3849 for (i
= 0; i
< count
; i
++, n
++) {
3850 get_int_array_element(interp
, varname
, n
, &v
);
3853 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
3856 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
3859 buffer
[i
] = v
& 0x0ff;
3865 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3866 if (retval
!= ERROR_OK
) {
3868 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3872 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3873 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3881 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3886 /* FIX? should we propagate errors here rather than printing them
3889 void target_handle_event(struct target
*target
, enum target_event e
)
3891 struct target_event_action
*teap
;
3893 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3894 if (teap
->event
== e
) {
3895 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3896 target
->target_number
,
3897 target_name(target
),
3898 target_type_name(target
),
3900 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3901 Jim_GetString(teap
->body
, NULL
));
3902 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
) {
3903 Jim_MakeErrorMessage(teap
->interp
);
3904 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
3911 * Returns true only if the target has a handler for the specified event.
3913 bool target_has_event_action(struct target
*target
, enum target_event event
)
3915 struct target_event_action
*teap
;
3917 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3918 if (teap
->event
== event
)
3924 enum target_cfg_param
{
3927 TCFG_WORK_AREA_VIRT
,
3928 TCFG_WORK_AREA_PHYS
,
3929 TCFG_WORK_AREA_SIZE
,
3930 TCFG_WORK_AREA_BACKUP
,
3934 TCFG_CHAIN_POSITION
,
3939 static Jim_Nvp nvp_config_opts
[] = {
3940 { .name
= "-type", .value
= TCFG_TYPE
},
3941 { .name
= "-event", .value
= TCFG_EVENT
},
3942 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3943 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3944 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3945 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3946 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3947 { .name
= "-variant", .value
= TCFG_VARIANT
},
3948 { .name
= "-coreid", .value
= TCFG_COREID
},
3949 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3950 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
3951 { .name
= "-rtos", .value
= TCFG_RTOS
},
3952 { .name
= NULL
, .value
= -1 }
3955 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
3963 /* parse config or cget options ... */
3964 while (goi
->argc
> 0) {
3965 Jim_SetEmptyResult(goi
->interp
);
3966 /* Jim_GetOpt_Debug(goi); */
3968 if (target
->type
->target_jim_configure
) {
3969 /* target defines a configure function */
3970 /* target gets first dibs on parameters */
3971 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
3980 /* otherwise we 'continue' below */
3982 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
3984 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
3990 if (goi
->isconfigure
) {
3991 Jim_SetResultFormatted(goi
->interp
,
3992 "not settable: %s", n
->name
);
3996 if (goi
->argc
!= 0) {
3997 Jim_WrongNumArgs(goi
->interp
,
3998 goi
->argc
, goi
->argv
,
4003 Jim_SetResultString(goi
->interp
,
4004 target_type_name(target
), -1);
4008 if (goi
->argc
== 0) {
4009 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
4013 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
4015 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
4019 if (goi
->isconfigure
) {
4020 if (goi
->argc
!= 1) {
4021 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
4025 if (goi
->argc
!= 0) {
4026 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
4032 struct target_event_action
*teap
;
4034 teap
= target
->event_action
;
4035 /* replace existing? */
4037 if (teap
->event
== (enum target_event
)n
->value
)
4042 if (goi
->isconfigure
) {
4043 bool replace
= true;
4046 teap
= calloc(1, sizeof(*teap
));
4049 teap
->event
= n
->value
;
4050 teap
->interp
= goi
->interp
;
4051 Jim_GetOpt_Obj(goi
, &o
);
4053 Jim_DecrRefCount(teap
->interp
, teap
->body
);
4054 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
4057 * Tcl/TK - "tk events" have a nice feature.
4058 * See the "BIND" command.
4059 * We should support that here.
4060 * You can specify %X and %Y in the event code.
4061 * The idea is: %T - target name.
4062 * The idea is: %N - target number
4063 * The idea is: %E - event name.
4065 Jim_IncrRefCount(teap
->body
);
4068 /* add to head of event list */
4069 teap
->next
= target
->event_action
;
4070 target
->event_action
= teap
;
4072 Jim_SetEmptyResult(goi
->interp
);
4076 Jim_SetEmptyResult(goi
->interp
);
4078 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
4084 case TCFG_WORK_AREA_VIRT
:
4085 if (goi
->isconfigure
) {
4086 target_free_all_working_areas(target
);
4087 e
= Jim_GetOpt_Wide(goi
, &w
);
4090 target
->working_area_virt
= w
;
4091 target
->working_area_virt_spec
= true;
4096 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
4100 case TCFG_WORK_AREA_PHYS
:
4101 if (goi
->isconfigure
) {
4102 target_free_all_working_areas(target
);
4103 e
= Jim_GetOpt_Wide(goi
, &w
);
4106 target
->working_area_phys
= w
;
4107 target
->working_area_phys_spec
= true;
4112 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
4116 case TCFG_WORK_AREA_SIZE
:
4117 if (goi
->isconfigure
) {
4118 target_free_all_working_areas(target
);
4119 e
= Jim_GetOpt_Wide(goi
, &w
);
4122 target
->working_area_size
= w
;
4127 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4131 case TCFG_WORK_AREA_BACKUP
:
4132 if (goi
->isconfigure
) {
4133 target_free_all_working_areas(target
);
4134 e
= Jim_GetOpt_Wide(goi
, &w
);
4137 /* make this exactly 1 or 0 */
4138 target
->backup_working_area
= (!!w
);
4143 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
4144 /* loop for more e*/
4149 if (goi
->isconfigure
) {
4150 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
4152 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
4155 target
->endianness
= n
->value
;
4160 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4161 if (n
->name
== NULL
) {
4162 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4163 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4165 Jim_SetResultString(goi
->interp
, n
->name
, -1);
4170 if (goi
->isconfigure
) {
4171 if (goi
->argc
< 1) {
4172 Jim_SetResultFormatted(goi
->interp
,
4177 if (target
->variant
)
4178 free((void *)(target
->variant
));
4179 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
4182 target
->variant
= strdup(cp
);
4187 Jim_SetResultString(goi
->interp
, target
->variant
, -1);
4192 if (goi
->isconfigure
) {
4193 e
= Jim_GetOpt_Wide(goi
, &w
);
4196 target
->coreid
= (int32_t)w
;
4201 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4205 case TCFG_CHAIN_POSITION
:
4206 if (goi
->isconfigure
) {
4208 struct jtag_tap
*tap
;
4209 target_free_all_working_areas(target
);
4210 e
= Jim_GetOpt_Obj(goi
, &o_t
);
4213 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
4216 /* make this exactly 1 or 0 */
4222 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
4223 /* loop for more e*/
4226 if (goi
->isconfigure
) {
4227 e
= Jim_GetOpt_Wide(goi
, &w
);
4230 target
->dbgbase
= (uint32_t)w
;
4231 target
->dbgbase_set
= true;
4236 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
4243 int result
= rtos_create(goi
, target
);
4244 if (result
!= JIM_OK
)
4250 } /* while (goi->argc) */
4253 /* done - we return */
4257 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4261 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4262 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4263 int need_args
= 1 + goi
.isconfigure
;
4264 if (goi
.argc
< need_args
) {
4265 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4267 ? "missing: -option VALUE ..."
4268 : "missing: -option ...");
4271 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4272 return target_configure(&goi
, target
);
4275 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4277 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4280 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4282 if (goi
.argc
< 2 || goi
.argc
> 4) {
4283 Jim_SetResultFormatted(goi
.interp
,
4284 "usage: %s [phys] <address> <data> [<count>]", cmd_name
);
4289 fn
= target_write_memory_fast
;
4292 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4294 struct Jim_Obj
*obj
;
4295 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4299 fn
= target_write_phys_memory
;
4303 e
= Jim_GetOpt_Wide(&goi
, &a
);
4308 e
= Jim_GetOpt_Wide(&goi
, &b
);
4313 if (goi
.argc
== 1) {
4314 e
= Jim_GetOpt_Wide(&goi
, &c
);
4319 /* all args must be consumed */
4323 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4325 if (strcasecmp(cmd_name
, "mww") == 0)
4327 else if (strcasecmp(cmd_name
, "mwh") == 0)
4329 else if (strcasecmp(cmd_name
, "mwb") == 0)
4332 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4336 return (target_fill_mem(target
, a
, fn
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4340 * @brief Reads an array of words/halfwords/bytes from target memory starting at specified address.
4342 * Usage: mdw [phys] <address> [<count>] - for 32 bit reads
4343 * mdh [phys] <address> [<count>] - for 16 bit reads
4344 * mdb [phys] <address> [<count>] - for 8 bit reads
4346 * Count defaults to 1.
4348 * Calls target_read_memory or target_read_phys_memory depending on
4349 * the presence of the "phys" argument
4350 * Reads the target memory in blocks of max. 32 bytes, and returns an array of ints formatted
4351 * to int representation in base16.
4352 * Also outputs read data in a human readable form using command_print
4354 * @param phys if present target_read_phys_memory will be used instead of target_read_memory
4355 * @param address address where to start the read. May be specified in decimal or hex using the standard "0x" prefix
4356 * @param count optional count parameter to read an array of values. If not specified, defaults to 1.
4357 * @returns: JIM_ERR on error or JIM_OK on success and sets the result string to an array of ascii formatted numbers
4358 * on success, with [<count>] number of elements.
4360 * In case of little endian target:
4361 * Example1: "mdw 0x00000000" returns "10123456"
4362 * Exmaple2: "mdh 0x00000000 1" returns "3456"
4363 * Example3: "mdb 0x00000000" returns "56"
4364 * Example4: "mdh 0x00000000 2" returns "3456 1012"
4365 * Example5: "mdb 0x00000000 3" returns "56 34 12"
4367 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4369 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4372 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4374 if ((goi
.argc
< 1) || (goi
.argc
> 3)) {
4375 Jim_SetResultFormatted(goi
.interp
,
4376 "usage: %s [phys] <address> [<count>]", cmd_name
);
4380 int (*fn
)(struct target
*target
,
4381 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
4382 fn
= target_read_memory
;
4385 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4387 struct Jim_Obj
*obj
;
4388 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4392 fn
= target_read_phys_memory
;
4395 /* Read address parameter */
4397 e
= Jim_GetOpt_Wide(&goi
, &addr
);
4401 /* If next parameter exists, read it out as the count parameter, if not, set it to 1 (default) */
4403 if (goi
.argc
== 1) {
4404 e
= Jim_GetOpt_Wide(&goi
, &count
);
4410 /* all args must be consumed */
4414 jim_wide dwidth
= 1; /* shut up gcc */
4415 if (strcasecmp(cmd_name
, "mdw") == 0)
4417 else if (strcasecmp(cmd_name
, "mdh") == 0)
4419 else if (strcasecmp(cmd_name
, "mdb") == 0)
4422 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4426 /* convert count to "bytes" */
4427 int bytes
= count
* dwidth
;
4429 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4430 uint8_t target_buf
[32];
4433 y
= (bytes
< 16) ? bytes
: 16; /* y = min(bytes, 16); */
4435 /* Try to read out next block */
4436 e
= fn(target
, addr
, dwidth
, y
/ dwidth
, target_buf
);
4438 if (e
!= ERROR_OK
) {
4439 Jim_SetResultFormatted(interp
, "error reading target @ 0x%08lx", (long)addr
);
4443 command_print_sameline(NULL
, "0x%08x ", (int)(addr
));
4446 for (x
= 0; x
< 16 && x
< y
; x
+= 4) {
4447 z
= target_buffer_get_u32(target
, &(target_buf
[x
]));
4448 command_print_sameline(NULL
, "%08x ", (int)(z
));
4450 for (; (x
< 16) ; x
+= 4)
4451 command_print_sameline(NULL
, " ");
4454 for (x
= 0; x
< 16 && x
< y
; x
+= 2) {
4455 z
= target_buffer_get_u16(target
, &(target_buf
[x
]));
4456 command_print_sameline(NULL
, "%04x ", (int)(z
));
4458 for (; (x
< 16) ; x
+= 2)
4459 command_print_sameline(NULL
, " ");
4463 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4464 z
= target_buffer_get_u8(target
, &(target_buf
[x
]));
4465 command_print_sameline(NULL
, "%02x ", (int)(z
));
4467 for (; (x
< 16) ; x
+= 1)
4468 command_print_sameline(NULL
, " ");
4471 /* ascii-ify the bytes */
4472 for (x
= 0 ; x
< y
; x
++) {
4473 if ((target_buf
[x
] >= 0x20) &&
4474 (target_buf
[x
] <= 0x7e)) {
4478 target_buf
[x
] = '.';
4483 target_buf
[x
] = ' ';
4488 /* print - with a newline */
4489 command_print_sameline(NULL
, "%s\n", target_buf
);
4497 static int jim_target_mem2array(Jim_Interp
*interp
,
4498 int argc
, Jim_Obj
*const *argv
)
4500 struct target
*target
= Jim_CmdPrivData(interp
);
4501 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4504 static int jim_target_array2mem(Jim_Interp
*interp
,
4505 int argc
, Jim_Obj
*const *argv
)
4507 struct target
*target
= Jim_CmdPrivData(interp
);
4508 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4511 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4513 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
4517 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4520 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4523 struct target
*target
= Jim_CmdPrivData(interp
);
4524 if (!target
->tap
->enabled
)
4525 return jim_target_tap_disabled(interp
);
4527 int e
= target
->type
->examine(target
);
4533 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4536 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4539 struct target
*target
= Jim_CmdPrivData(interp
);
4541 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
4547 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4550 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4553 struct target
*target
= Jim_CmdPrivData(interp
);
4554 if (!target
->tap
->enabled
)
4555 return jim_target_tap_disabled(interp
);
4558 if (!(target_was_examined(target
)))
4559 e
= ERROR_TARGET_NOT_EXAMINED
;
4561 e
= target
->type
->poll(target
);
4567 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4570 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4572 if (goi
.argc
!= 2) {
4573 Jim_WrongNumArgs(interp
, 0, argv
,
4574 "([tT]|[fF]|assert|deassert) BOOL");
4579 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4581 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4584 /* the halt or not param */
4586 e
= Jim_GetOpt_Wide(&goi
, &a
);
4590 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4591 if (!target
->tap
->enabled
)
4592 return jim_target_tap_disabled(interp
);
4593 if (!(target_was_examined(target
))) {
4594 LOG_ERROR("Target not examined yet");
4595 return ERROR_TARGET_NOT_EXAMINED
;
4597 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
4598 Jim_SetResultFormatted(interp
,
4599 "No target-specific reset for %s",
4600 target_name(target
));
4603 /* determine if we should halt or not. */
4604 target
->reset_halt
= !!a
;
4605 /* When this happens - all workareas are invalid. */
4606 target_free_all_working_areas_restore(target
, 0);
4609 if (n
->value
== NVP_ASSERT
)
4610 e
= target
->type
->assert_reset(target
);
4612 e
= target
->type
->deassert_reset(target
);
4613 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4616 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4619 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4622 struct target
*target
= Jim_CmdPrivData(interp
);
4623 if (!target
->tap
->enabled
)
4624 return jim_target_tap_disabled(interp
);
4625 int e
= target
->type
->halt(target
);
4626 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4629 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4632 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4634 /* params: <name> statename timeoutmsecs */
4635 if (goi
.argc
!= 2) {
4636 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4637 Jim_SetResultFormatted(goi
.interp
,
4638 "%s <state_name> <timeout_in_msec>", cmd_name
);
4643 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4645 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
4649 e
= Jim_GetOpt_Wide(&goi
, &a
);
4652 struct target
*target
= Jim_CmdPrivData(interp
);
4653 if (!target
->tap
->enabled
)
4654 return jim_target_tap_disabled(interp
);
4656 e
= target_wait_state(target
, n
->value
, a
);
4657 if (e
!= ERROR_OK
) {
4658 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
4659 Jim_SetResultFormatted(goi
.interp
,
4660 "target: %s wait %s fails (%#s) %s",
4661 target_name(target
), n
->name
,
4662 eObj
, target_strerror_safe(e
));
4663 Jim_FreeNewObj(interp
, eObj
);
4668 /* List for human, Events defined for this target.
4669 * scripts/programs should use 'name cget -event NAME'
4671 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4673 struct command_context
*cmd_ctx
= current_command_context(interp
);
4674 assert(cmd_ctx
!= NULL
);
4676 struct target
*target
= Jim_CmdPrivData(interp
);
4677 struct target_event_action
*teap
= target
->event_action
;
4678 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4679 target
->target_number
,
4680 target_name(target
));
4681 command_print(cmd_ctx
, "%-25s | Body", "Event");
4682 command_print(cmd_ctx
, "------------------------- | "
4683 "----------------------------------------");
4685 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
4686 command_print(cmd_ctx
, "%-25s | %s",
4687 opt
->name
, Jim_GetString(teap
->body
, NULL
));
4690 command_print(cmd_ctx
, "***END***");
4693 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4696 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4699 struct target
*target
= Jim_CmdPrivData(interp
);
4700 Jim_SetResultString(interp
, target_state_name(target
), -1);
4703 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4706 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4707 if (goi
.argc
!= 1) {
4708 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4709 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
4713 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4715 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4718 struct target
*target
= Jim_CmdPrivData(interp
);
4719 target_handle_event(target
, n
->value
);
4723 static const struct command_registration target_instance_command_handlers
[] = {
4725 .name
= "configure",
4726 .mode
= COMMAND_CONFIG
,
4727 .jim_handler
= jim_target_configure
,
4728 .help
= "configure a new target for use",
4729 .usage
= "[target_attribute ...]",
4733 .mode
= COMMAND_ANY
,
4734 .jim_handler
= jim_target_configure
,
4735 .help
= "returns the specified target attribute",
4736 .usage
= "target_attribute",
4740 .mode
= COMMAND_EXEC
,
4741 .jim_handler
= jim_target_mw
,
4742 .help
= "Write 32-bit word(s) to target memory",
4743 .usage
= "address data [count]",
4747 .mode
= COMMAND_EXEC
,
4748 .jim_handler
= jim_target_mw
,
4749 .help
= "Write 16-bit half-word(s) to target memory",
4750 .usage
= "address data [count]",
4754 .mode
= COMMAND_EXEC
,
4755 .jim_handler
= jim_target_mw
,
4756 .help
= "Write byte(s) to target memory",
4757 .usage
= "address data [count]",
4761 .mode
= COMMAND_EXEC
,
4762 .jim_handler
= jim_target_md
,
4763 .help
= "Display target memory as 32-bit words",
4764 .usage
= "address [count]",
4768 .mode
= COMMAND_EXEC
,
4769 .jim_handler
= jim_target_md
,
4770 .help
= "Display target memory as 16-bit half-words",
4771 .usage
= "address [count]",
4775 .mode
= COMMAND_EXEC
,
4776 .jim_handler
= jim_target_md
,
4777 .help
= "Display target memory as 8-bit bytes",
4778 .usage
= "address [count]",
4781 .name
= "array2mem",
4782 .mode
= COMMAND_EXEC
,
4783 .jim_handler
= jim_target_array2mem
,
4784 .help
= "Writes Tcl array of 8/16/32 bit numbers "
4786 .usage
= "arrayname bitwidth address count",
4789 .name
= "mem2array",
4790 .mode
= COMMAND_EXEC
,
4791 .jim_handler
= jim_target_mem2array
,
4792 .help
= "Loads Tcl array of 8/16/32 bit numbers "
4793 "from target memory",
4794 .usage
= "arrayname bitwidth address count",
4797 .name
= "eventlist",
4798 .mode
= COMMAND_EXEC
,
4799 .jim_handler
= jim_target_event_list
,
4800 .help
= "displays a table of events defined for this target",
4804 .mode
= COMMAND_EXEC
,
4805 .jim_handler
= jim_target_current_state
,
4806 .help
= "displays the current state of this target",
4809 .name
= "arp_examine",
4810 .mode
= COMMAND_EXEC
,
4811 .jim_handler
= jim_target_examine
,
4812 .help
= "used internally for reset processing",
4815 .name
= "arp_halt_gdb",
4816 .mode
= COMMAND_EXEC
,
4817 .jim_handler
= jim_target_halt_gdb
,
4818 .help
= "used internally for reset processing to halt GDB",
4822 .mode
= COMMAND_EXEC
,
4823 .jim_handler
= jim_target_poll
,
4824 .help
= "used internally for reset processing",
4827 .name
= "arp_reset",
4828 .mode
= COMMAND_EXEC
,
4829 .jim_handler
= jim_target_reset
,
4830 .help
= "used internally for reset processing",
4834 .mode
= COMMAND_EXEC
,
4835 .jim_handler
= jim_target_halt
,
4836 .help
= "used internally for reset processing",
4839 .name
= "arp_waitstate",
4840 .mode
= COMMAND_EXEC
,
4841 .jim_handler
= jim_target_wait_state
,
4842 .help
= "used internally for reset processing",
4845 .name
= "invoke-event",
4846 .mode
= COMMAND_EXEC
,
4847 .jim_handler
= jim_target_invoke_event
,
4848 .help
= "invoke handler for specified event",
4849 .usage
= "event_name",
4851 COMMAND_REGISTRATION_DONE
4854 static int target_create(Jim_GetOptInfo
*goi
)
4862 struct target
*target
;
4863 struct command_context
*cmd_ctx
;
4865 cmd_ctx
= current_command_context(goi
->interp
);
4866 assert(cmd_ctx
!= NULL
);
4868 if (goi
->argc
< 3) {
4869 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4874 Jim_GetOpt_Obj(goi
, &new_cmd
);
4875 /* does this command exist? */
4876 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4878 cp
= Jim_GetString(new_cmd
, NULL
);
4879 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
4884 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4888 /* now does target type exist */
4889 for (x
= 0 ; target_types
[x
] ; x
++) {
4890 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4895 /* check for deprecated name */
4896 if (target_types
[x
]->deprecated_name
) {
4897 if (0 == strcmp(cp
, target_types
[x
]->deprecated_name
)) {
4899 LOG_WARNING("target name is deprecated use: \'%s\'", target_types
[x
]->name
);
4904 if (target_types
[x
] == NULL
) {
4905 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4906 for (x
= 0 ; target_types
[x
] ; x
++) {
4907 if (target_types
[x
+ 1]) {
4908 Jim_AppendStrings(goi
->interp
,
4909 Jim_GetResult(goi
->interp
),
4910 target_types
[x
]->name
,
4913 Jim_AppendStrings(goi
->interp
,
4914 Jim_GetResult(goi
->interp
),
4916 target_types
[x
]->name
, NULL
);
4923 target
= calloc(1, sizeof(struct target
));
4924 /* set target number */
4925 target
->target_number
= new_target_number();
4927 /* allocate memory for each unique target type */
4928 target
->type
= (struct target_type
*)calloc(1, sizeof(struct target_type
));
4930 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
4932 /* will be set by "-endian" */
4933 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4935 /* default to first core, override with -coreid */
4938 target
->working_area
= 0x0;
4939 target
->working_area_size
= 0x0;
4940 target
->working_areas
= NULL
;
4941 target
->backup_working_area
= 0;
4943 target
->state
= TARGET_UNKNOWN
;
4944 target
->debug_reason
= DBG_REASON_UNDEFINED
;
4945 target
->reg_cache
= NULL
;
4946 target
->breakpoints
= NULL
;
4947 target
->watchpoints
= NULL
;
4948 target
->next
= NULL
;
4949 target
->arch_info
= NULL
;
4951 target
->display
= 1;
4953 target
->halt_issued
= false;
4955 /* initialize trace information */
4956 target
->trace_info
= malloc(sizeof(struct trace
));
4957 target
->trace_info
->num_trace_points
= 0;
4958 target
->trace_info
->trace_points_size
= 0;
4959 target
->trace_info
->trace_points
= NULL
;
4960 target
->trace_info
->trace_history_size
= 0;
4961 target
->trace_info
->trace_history
= NULL
;
4962 target
->trace_info
->trace_history_pos
= 0;
4963 target
->trace_info
->trace_history_overflowed
= 0;
4965 target
->dbgmsg
= NULL
;
4966 target
->dbg_msg_enabled
= 0;
4968 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4970 target
->rtos
= NULL
;
4971 target
->rtos_auto_detect
= false;
4973 /* Do the rest as "configure" options */
4974 goi
->isconfigure
= 1;
4975 e
= target_configure(goi
, target
);
4977 if (target
->tap
== NULL
) {
4978 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
4988 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
4989 /* default endian to little if not specified */
4990 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4993 /* incase variant is not set */
4994 if (!target
->variant
)
4995 target
->variant
= strdup("");
4997 cp
= Jim_GetString(new_cmd
, NULL
);
4998 target
->cmd_name
= strdup(cp
);
5000 /* create the target specific commands */
5001 if (target
->type
->commands
) {
5002 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
5004 LOG_ERROR("unable to register '%s' commands", cp
);
5006 if (target
->type
->target_create
)
5007 (*(target
->type
->target_create
))(target
, goi
->interp
);
5009 /* append to end of list */
5011 struct target
**tpp
;
5012 tpp
= &(all_targets
);
5014 tpp
= &((*tpp
)->next
);
5018 /* now - create the new target name command */
5019 const const struct command_registration target_subcommands
[] = {
5021 .chain
= target_instance_command_handlers
,
5024 .chain
= target
->type
->commands
,
5026 COMMAND_REGISTRATION_DONE
5028 const const struct command_registration target_commands
[] = {
5031 .mode
= COMMAND_ANY
,
5032 .help
= "target command group",
5034 .chain
= target_subcommands
,
5036 COMMAND_REGISTRATION_DONE
5038 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
5042 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
5044 command_set_handler_data(c
, target
);
5046 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
5049 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5052 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5055 struct command_context
*cmd_ctx
= current_command_context(interp
);
5056 assert(cmd_ctx
!= NULL
);
5058 Jim_SetResultString(interp
, target_name(get_current_target(cmd_ctx
)), -1);
5062 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5065 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5068 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5069 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
5070 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5071 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
5076 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5079 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5082 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5083 struct target
*target
= all_targets
;
5085 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5086 Jim_NewStringObj(interp
, target_name(target
), -1));
5087 target
= target
->next
;
5092 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5095 const char *targetname
;
5097 struct target
*target
= (struct target
*) NULL
;
5098 struct target_list
*head
, *curr
, *new;
5099 curr
= (struct target_list
*) NULL
;
5100 head
= (struct target_list
*) NULL
;
5103 LOG_DEBUG("%d", argc
);
5104 /* argv[1] = target to associate in smp
5105 * argv[2] = target to assoicate in smp
5109 for (i
= 1; i
< argc
; i
++) {
5111 targetname
= Jim_GetString(argv
[i
], &len
);
5112 target
= get_target(targetname
);
5113 LOG_DEBUG("%s ", targetname
);
5115 new = malloc(sizeof(struct target_list
));
5116 new->target
= target
;
5117 new->next
= (struct target_list
*)NULL
;
5118 if (head
== (struct target_list
*)NULL
) {
5127 /* now parse the list of cpu and put the target in smp mode*/
5130 while (curr
!= (struct target_list
*)NULL
) {
5131 target
= curr
->target
;
5133 target
->head
= head
;
5137 if (target
&& target
->rtos
)
5138 retval
= rtos_smp_init(head
->target
);
5144 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5147 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5149 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5150 "<name> <target_type> [<target_options> ...]");
5153 return target_create(&goi
);
5156 static int jim_target_number(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5159 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5161 /* It's OK to remove this mechanism sometime after August 2010 or so */
5162 LOG_WARNING("don't use numbers as target identifiers; use names");
5163 if (goi
.argc
!= 1) {
5164 Jim_SetResultFormatted(goi
.interp
, "usage: target number <number>");
5168 int e
= Jim_GetOpt_Wide(&goi
, &w
);
5172 struct target
*target
;
5173 for (target
= all_targets
; NULL
!= target
; target
= target
->next
) {
5174 if (target
->target_number
!= w
)
5177 Jim_SetResultString(goi
.interp
, target_name(target
), -1);
5181 Jim_Obj
*wObj
= Jim_NewIntObj(goi
.interp
, w
);
5182 Jim_SetResultFormatted(goi
.interp
,
5183 "Target: number %#s does not exist", wObj
);
5184 Jim_FreeNewObj(interp
, wObj
);
5189 static int jim_target_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5192 Jim_WrongNumArgs(interp
, 1, argv
, "<no parameters>");
5196 struct target
*target
= all_targets
;
5197 while (NULL
!= target
) {
5198 target
= target
->next
;
5201 Jim_SetResult(interp
, Jim_NewIntObj(interp
, count
));
5205 static const struct command_registration target_subcommand_handlers
[] = {
5208 .mode
= COMMAND_CONFIG
,
5209 .handler
= handle_target_init_command
,
5210 .help
= "initialize targets",
5214 /* REVISIT this should be COMMAND_CONFIG ... */
5215 .mode
= COMMAND_ANY
,
5216 .jim_handler
= jim_target_create
,
5217 .usage
= "name type '-chain-position' name [options ...]",
5218 .help
= "Creates and selects a new target",
5222 .mode
= COMMAND_ANY
,
5223 .jim_handler
= jim_target_current
,
5224 .help
= "Returns the currently selected target",
5228 .mode
= COMMAND_ANY
,
5229 .jim_handler
= jim_target_types
,
5230 .help
= "Returns the available target types as "
5231 "a list of strings",
5235 .mode
= COMMAND_ANY
,
5236 .jim_handler
= jim_target_names
,
5237 .help
= "Returns the names of all targets as a list of strings",
5241 .mode
= COMMAND_ANY
,
5242 .jim_handler
= jim_target_number
,
5244 .help
= "Returns the name of the numbered target "
5249 .mode
= COMMAND_ANY
,
5250 .jim_handler
= jim_target_count
,
5251 .help
= "Returns the number of targets as an integer "
5256 .mode
= COMMAND_ANY
,
5257 .jim_handler
= jim_target_smp
,
5258 .usage
= "targetname1 targetname2 ...",
5259 .help
= "gather several target in a smp list"
5262 COMMAND_REGISTRATION_DONE
5272 static int fastload_num
;
5273 static struct FastLoad
*fastload
;
5275 static void free_fastload(void)
5277 if (fastload
!= NULL
) {
5279 for (i
= 0; i
< fastload_num
; i
++) {
5280 if (fastload
[i
].data
)
5281 free(fastload
[i
].data
);
5288 COMMAND_HANDLER(handle_fast_load_image_command
)
5292 uint32_t image_size
;
5293 uint32_t min_address
= 0;
5294 uint32_t max_address
= 0xffffffff;
5299 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5300 &image
, &min_address
, &max_address
);
5301 if (ERROR_OK
!= retval
)
5304 struct duration bench
;
5305 duration_start(&bench
);
5307 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5308 if (retval
!= ERROR_OK
)
5313 fastload_num
= image
.num_sections
;
5314 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5315 if (fastload
== NULL
) {
5316 command_print(CMD_CTX
, "out of memory");
5317 image_close(&image
);
5320 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5321 for (i
= 0; i
< image
.num_sections
; i
++) {
5322 buffer
= malloc(image
.sections
[i
].size
);
5323 if (buffer
== NULL
) {
5324 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5325 (int)(image
.sections
[i
].size
));
5326 retval
= ERROR_FAIL
;
5330 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5331 if (retval
!= ERROR_OK
) {
5336 uint32_t offset
= 0;
5337 uint32_t length
= buf_cnt
;
5339 /* DANGER!!! beware of unsigned comparision here!!! */
5341 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5342 (image
.sections
[i
].base_address
< max_address
)) {
5343 if (image
.sections
[i
].base_address
< min_address
) {
5344 /* clip addresses below */
5345 offset
+= min_address
-image
.sections
[i
].base_address
;
5349 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5350 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5352 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5353 fastload
[i
].data
= malloc(length
);
5354 if (fastload
[i
].data
== NULL
) {
5356 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5358 retval
= ERROR_FAIL
;
5361 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5362 fastload
[i
].length
= length
;
5364 image_size
+= length
;
5365 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
5366 (unsigned int)length
,
5367 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5373 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
5374 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
5375 "in %fs (%0.3f KiB/s)", image_size
,
5376 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5378 command_print(CMD_CTX
,
5379 "WARNING: image has not been loaded to target!"
5380 "You can issue a 'fast_load' to finish loading.");
5383 image_close(&image
);
5385 if (retval
!= ERROR_OK
)
5391 COMMAND_HANDLER(handle_fast_load_command
)
5394 return ERROR_COMMAND_SYNTAX_ERROR
;
5395 if (fastload
== NULL
) {
5396 LOG_ERROR("No image in memory");
5400 int ms
= timeval_ms();
5402 int retval
= ERROR_OK
;
5403 for (i
= 0; i
< fastload_num
; i
++) {
5404 struct target
*target
= get_current_target(CMD_CTX
);
5405 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
5406 (unsigned int)(fastload
[i
].address
),
5407 (unsigned int)(fastload
[i
].length
));
5408 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5409 if (retval
!= ERROR_OK
)
5411 size
+= fastload
[i
].length
;
5413 if (retval
== ERROR_OK
) {
5414 int after
= timeval_ms();
5415 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5420 static const struct command_registration target_command_handlers
[] = {
5423 .handler
= handle_targets_command
,
5424 .mode
= COMMAND_ANY
,
5425 .help
= "change current default target (one parameter) "
5426 "or prints table of all targets (no parameters)",
5427 .usage
= "[target]",
5431 .mode
= COMMAND_CONFIG
,
5432 .help
= "configure target",
5434 .chain
= target_subcommand_handlers
,
5436 COMMAND_REGISTRATION_DONE
5439 int target_register_commands(struct command_context
*cmd_ctx
)
5441 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5444 static bool target_reset_nag
= true;
5446 bool get_target_reset_nag(void)
5448 return target_reset_nag
;
5451 COMMAND_HANDLER(handle_target_reset_nag
)
5453 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5454 &target_reset_nag
, "Nag after each reset about options to improve "
5458 COMMAND_HANDLER(handle_ps_command
)
5460 struct target
*target
= get_current_target(CMD_CTX
);
5462 if (target
->state
!= TARGET_HALTED
) {
5463 LOG_INFO("target not halted !!");
5467 if ((target
->rtos
) && (target
->rtos
->type
)
5468 && (target
->rtos
->type
->ps_command
)) {
5469 display
= target
->rtos
->type
->ps_command(target
);
5470 command_print(CMD_CTX
, "%s", display
);
5475 return ERROR_TARGET_FAILURE
;
5479 static const struct command_registration target_exec_command_handlers
[] = {
5481 .name
= "fast_load_image",
5482 .handler
= handle_fast_load_image_command
,
5483 .mode
= COMMAND_ANY
,
5484 .help
= "Load image into server memory for later use by "
5485 "fast_load; primarily for profiling",
5486 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5487 "[min_address [max_length]]",
5490 .name
= "fast_load",
5491 .handler
= handle_fast_load_command
,
5492 .mode
= COMMAND_EXEC
,
5493 .help
= "loads active fast load image to current target "
5494 "- mainly for profiling purposes",
5499 .handler
= handle_profile_command
,
5500 .mode
= COMMAND_EXEC
,
5501 .usage
= "seconds filename",
5502 .help
= "profiling samples the CPU PC",
5504 /** @todo don't register virt2phys() unless target supports it */
5506 .name
= "virt2phys",
5507 .handler
= handle_virt2phys_command
,
5508 .mode
= COMMAND_ANY
,
5509 .help
= "translate a virtual address into a physical address",
5510 .usage
= "virtual_address",
5514 .handler
= handle_reg_command
,
5515 .mode
= COMMAND_EXEC
,
5516 .help
= "display or set a register; with no arguments, "
5517 "displays all registers and their values",
5518 .usage
= "[(register_name|register_number) [value]]",
5522 .handler
= handle_poll_command
,
5523 .mode
= COMMAND_EXEC
,
5524 .help
= "poll target state; or reconfigure background polling",
5525 .usage
= "['on'|'off']",
5528 .name
= "wait_halt",
5529 .handler
= handle_wait_halt_command
,
5530 .mode
= COMMAND_EXEC
,
5531 .help
= "wait up to the specified number of milliseconds "
5532 "(default 5) for a previously requested halt",
5533 .usage
= "[milliseconds]",
5537 .handler
= handle_halt_command
,
5538 .mode
= COMMAND_EXEC
,
5539 .help
= "request target to halt, then wait up to the specified"
5540 "number of milliseconds (default 5) for it to complete",
5541 .usage
= "[milliseconds]",
5545 .handler
= handle_resume_command
,
5546 .mode
= COMMAND_EXEC
,
5547 .help
= "resume target execution from current PC or address",
5548 .usage
= "[address]",
5552 .handler
= handle_reset_command
,
5553 .mode
= COMMAND_EXEC
,
5554 .usage
= "[run|halt|init]",
5555 .help
= "Reset all targets into the specified mode."
5556 "Default reset mode is run, if not given.",
5559 .name
= "soft_reset_halt",
5560 .handler
= handle_soft_reset_halt_command
,
5561 .mode
= COMMAND_EXEC
,
5563 .help
= "halt the target and do a soft reset",
5567 .handler
= handle_step_command
,
5568 .mode
= COMMAND_EXEC
,
5569 .help
= "step one instruction from current PC or address",
5570 .usage
= "[address]",
5574 .handler
= handle_md_command
,
5575 .mode
= COMMAND_EXEC
,
5576 .help
= "display memory words",
5577 .usage
= "['phys'] address [count]",
5581 .handler
= handle_md_command
,
5582 .mode
= COMMAND_EXEC
,
5583 .help
= "display memory half-words",
5584 .usage
= "['phys'] address [count]",
5588 .handler
= handle_md_command
,
5589 .mode
= COMMAND_EXEC
,
5590 .help
= "display memory bytes",
5591 .usage
= "['phys'] address [count]",
5595 .handler
= handle_mw_command
,
5596 .mode
= COMMAND_EXEC
,
5597 .help
= "write memory word",
5598 .usage
= "['phys'] address value [count]",
5602 .handler
= handle_mw_command
,
5603 .mode
= COMMAND_EXEC
,
5604 .help
= "write memory half-word",
5605 .usage
= "['phys'] address value [count]",
5609 .handler
= handle_mw_command
,
5610 .mode
= COMMAND_EXEC
,
5611 .help
= "write memory byte",
5612 .usage
= "['phys'] address value [count]",
5616 .handler
= handle_bp_command
,
5617 .mode
= COMMAND_EXEC
,
5618 .help
= "list or set hardware or software breakpoint",
5619 .usage
= "<address> [<asid>]<length> ['hw'|'hw_ctx']",
5623 .handler
= handle_rbp_command
,
5624 .mode
= COMMAND_EXEC
,
5625 .help
= "remove breakpoint",
5630 .handler
= handle_wp_command
,
5631 .mode
= COMMAND_EXEC
,
5632 .help
= "list (no params) or create watchpoints",
5633 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
5637 .handler
= handle_rwp_command
,
5638 .mode
= COMMAND_EXEC
,
5639 .help
= "remove watchpoint",
5643 .name
= "load_image",
5644 .handler
= handle_load_image_command
,
5645 .mode
= COMMAND_EXEC
,
5646 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5647 "[min_address] [max_length]",
5650 .name
= "dump_image",
5651 .handler
= handle_dump_image_command
,
5652 .mode
= COMMAND_EXEC
,
5653 .usage
= "filename address size",
5656 .name
= "verify_image",
5657 .handler
= handle_verify_image_command
,
5658 .mode
= COMMAND_EXEC
,
5659 .usage
= "filename [offset [type]]",
5662 .name
= "test_image",
5663 .handler
= handle_test_image_command
,
5664 .mode
= COMMAND_EXEC
,
5665 .usage
= "filename [offset [type]]",
5668 .name
= "mem2array",
5669 .mode
= COMMAND_EXEC
,
5670 .jim_handler
= jim_mem2array
,
5671 .help
= "read 8/16/32 bit memory and return as a TCL array "
5672 "for script processing",
5673 .usage
= "arrayname bitwidth address count",
5676 .name
= "array2mem",
5677 .mode
= COMMAND_EXEC
,
5678 .jim_handler
= jim_array2mem
,
5679 .help
= "convert a TCL array to memory locations "
5680 "and write the 8/16/32 bit values",
5681 .usage
= "arrayname bitwidth address count",
5684 .name
= "reset_nag",
5685 .handler
= handle_target_reset_nag
,
5686 .mode
= COMMAND_ANY
,
5687 .help
= "Nag after each reset about options that could have been "
5688 "enabled to improve performance. ",
5689 .usage
= "['enable'|'disable']",
5693 .handler
= handle_ps_command
,
5694 .mode
= COMMAND_EXEC
,
5695 .help
= "list all tasks ",
5699 COMMAND_REGISTRATION_DONE
5701 static int target_register_user_commands(struct command_context
*cmd_ctx
)
5703 int retval
= ERROR_OK
;
5704 retval
= target_request_register_commands(cmd_ctx
);
5705 if (retval
!= ERROR_OK
)
5708 retval
= trace_register_commands(cmd_ctx
);
5709 if (retval
!= ERROR_OK
)
5713 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);