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 int target_bulk_write_memory(struct target
*target
,
974 uint32_t address
, uint32_t count
, const uint8_t *buffer
)
976 return target
->type
->bulk_write_memory(target
, address
, 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
;
1127 static int target_init(struct command_context
*cmd_ctx
)
1129 struct target
*target
;
1132 for (target
= all_targets
; target
; target
= target
->next
) {
1133 retval
= target_init_one(cmd_ctx
, target
);
1134 if (ERROR_OK
!= retval
)
1141 retval
= target_register_user_commands(cmd_ctx
);
1142 if (ERROR_OK
!= retval
)
1145 retval
= target_register_timer_callback(&handle_target
,
1146 polling_interval
, 1, cmd_ctx
->interp
);
1147 if (ERROR_OK
!= retval
)
1153 COMMAND_HANDLER(handle_target_init_command
)
1158 return ERROR_COMMAND_SYNTAX_ERROR
;
1160 static bool target_initialized
;
1161 if (target_initialized
) {
1162 LOG_INFO("'target init' has already been called");
1165 target_initialized
= true;
1167 retval
= command_run_line(CMD_CTX
, "init_targets");
1168 if (ERROR_OK
!= retval
)
1171 retval
= command_run_line(CMD_CTX
, "init_board");
1172 if (ERROR_OK
!= retval
)
1175 LOG_DEBUG("Initializing targets...");
1176 return target_init(CMD_CTX
);
1179 int target_register_event_callback(int (*callback
)(struct target
*target
,
1180 enum target_event event
, void *priv
), void *priv
)
1182 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1184 if (callback
== NULL
)
1185 return ERROR_COMMAND_SYNTAX_ERROR
;
1188 while ((*callbacks_p
)->next
)
1189 callbacks_p
= &((*callbacks_p
)->next
);
1190 callbacks_p
= &((*callbacks_p
)->next
);
1193 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1194 (*callbacks_p
)->callback
= callback
;
1195 (*callbacks_p
)->priv
= priv
;
1196 (*callbacks_p
)->next
= NULL
;
1201 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1203 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1206 if (callback
== NULL
)
1207 return ERROR_COMMAND_SYNTAX_ERROR
;
1210 while ((*callbacks_p
)->next
)
1211 callbacks_p
= &((*callbacks_p
)->next
);
1212 callbacks_p
= &((*callbacks_p
)->next
);
1215 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1216 (*callbacks_p
)->callback
= callback
;
1217 (*callbacks_p
)->periodic
= periodic
;
1218 (*callbacks_p
)->time_ms
= time_ms
;
1220 gettimeofday(&now
, NULL
);
1221 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
1222 time_ms
-= (time_ms
% 1000);
1223 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
1224 if ((*callbacks_p
)->when
.tv_usec
> 1000000) {
1225 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
1226 (*callbacks_p
)->when
.tv_sec
+= 1;
1229 (*callbacks_p
)->priv
= priv
;
1230 (*callbacks_p
)->next
= NULL
;
1235 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1236 enum target_event event
, void *priv
), void *priv
)
1238 struct target_event_callback
**p
= &target_event_callbacks
;
1239 struct target_event_callback
*c
= target_event_callbacks
;
1241 if (callback
== NULL
)
1242 return ERROR_COMMAND_SYNTAX_ERROR
;
1245 struct target_event_callback
*next
= c
->next
;
1246 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1258 static int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1260 struct target_timer_callback
**p
= &target_timer_callbacks
;
1261 struct target_timer_callback
*c
= target_timer_callbacks
;
1263 if (callback
== NULL
)
1264 return ERROR_COMMAND_SYNTAX_ERROR
;
1267 struct target_timer_callback
*next
= c
->next
;
1268 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1280 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1282 struct target_event_callback
*callback
= target_event_callbacks
;
1283 struct target_event_callback
*next_callback
;
1285 if (event
== TARGET_EVENT_HALTED
) {
1286 /* execute early halted first */
1287 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1290 LOG_DEBUG("target event %i (%s)", event
,
1291 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1293 target_handle_event(target
, event
);
1296 next_callback
= callback
->next
;
1297 callback
->callback(target
, event
, callback
->priv
);
1298 callback
= next_callback
;
1304 static int target_timer_callback_periodic_restart(
1305 struct target_timer_callback
*cb
, struct timeval
*now
)
1307 int time_ms
= cb
->time_ms
;
1308 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1309 time_ms
-= (time_ms
% 1000);
1310 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1311 if (cb
->when
.tv_usec
> 1000000) {
1312 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1313 cb
->when
.tv_sec
+= 1;
1318 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1319 struct timeval
*now
)
1321 cb
->callback(cb
->priv
);
1324 return target_timer_callback_periodic_restart(cb
, now
);
1326 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1329 static int target_call_timer_callbacks_check_time(int checktime
)
1334 gettimeofday(&now
, NULL
);
1336 struct target_timer_callback
*callback
= target_timer_callbacks
;
1338 /* cleaning up may unregister and free this callback */
1339 struct target_timer_callback
*next_callback
= callback
->next
;
1341 bool call_it
= callback
->callback
&&
1342 ((!checktime
&& callback
->periodic
) ||
1343 now
.tv_sec
> callback
->when
.tv_sec
||
1344 (now
.tv_sec
== callback
->when
.tv_sec
&&
1345 now
.tv_usec
>= callback
->when
.tv_usec
));
1348 int retval
= target_call_timer_callback(callback
, &now
);
1349 if (retval
!= ERROR_OK
)
1353 callback
= next_callback
;
1359 int target_call_timer_callbacks(void)
1361 return target_call_timer_callbacks_check_time(1);
1364 /* invoke periodic callbacks immediately */
1365 int target_call_timer_callbacks_now(void)
1367 return target_call_timer_callbacks_check_time(0);
1370 /* Prints the working area layout for debug purposes */
1371 static void print_wa_layout(struct target
*target
)
1373 struct working_area
*c
= target
->working_areas
;
1376 LOG_DEBUG("%c%c 0x%08"PRIx32
"-0x%08"PRIx32
" (%"PRIu32
" bytes)",
1377 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1378 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1383 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1384 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1386 assert(area
->free
); /* Shouldn't split an allocated area */
1387 assert(size
<= area
->size
); /* Caller should guarantee this */
1389 /* Split only if not already the right size */
1390 if (size
< area
->size
) {
1391 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1396 new_wa
->next
= area
->next
;
1397 new_wa
->size
= area
->size
- size
;
1398 new_wa
->address
= area
->address
+ size
;
1399 new_wa
->backup
= NULL
;
1400 new_wa
->user
= NULL
;
1401 new_wa
->free
= true;
1403 area
->next
= new_wa
;
1406 /* If backup memory was allocated to this area, it has the wrong size
1407 * now so free it and it will be reallocated if/when needed */
1410 area
->backup
= NULL
;
1415 /* Merge all adjacent free areas into one */
1416 static void target_merge_working_areas(struct target
*target
)
1418 struct working_area
*c
= target
->working_areas
;
1420 while (c
&& c
->next
) {
1421 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1423 /* Find two adjacent free areas */
1424 if (c
->free
&& c
->next
->free
) {
1425 /* Merge the last into the first */
1426 c
->size
+= c
->next
->size
;
1428 /* Remove the last */
1429 struct working_area
*to_be_freed
= c
->next
;
1430 c
->next
= c
->next
->next
;
1431 if (to_be_freed
->backup
)
1432 free(to_be_freed
->backup
);
1435 /* If backup memory was allocated to the remaining area, it's has
1436 * the wrong size now */
1447 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1449 /* Reevaluate working area address based on MMU state*/
1450 if (target
->working_areas
== NULL
) {
1454 retval
= target
->type
->mmu(target
, &enabled
);
1455 if (retval
!= ERROR_OK
)
1459 if (target
->working_area_phys_spec
) {
1460 LOG_DEBUG("MMU disabled, using physical "
1461 "address for working memory 0x%08"PRIx32
,
1462 target
->working_area_phys
);
1463 target
->working_area
= target
->working_area_phys
;
1465 LOG_ERROR("No working memory available. "
1466 "Specify -work-area-phys to target.");
1467 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1470 if (target
->working_area_virt_spec
) {
1471 LOG_DEBUG("MMU enabled, using virtual "
1472 "address for working memory 0x%08"PRIx32
,
1473 target
->working_area_virt
);
1474 target
->working_area
= target
->working_area_virt
;
1476 LOG_ERROR("No working memory available. "
1477 "Specify -work-area-virt to target.");
1478 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1482 /* Set up initial working area on first call */
1483 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1485 new_wa
->next
= NULL
;
1486 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1487 new_wa
->address
= target
->working_area
;
1488 new_wa
->backup
= NULL
;
1489 new_wa
->user
= NULL
;
1490 new_wa
->free
= true;
1493 target
->working_areas
= new_wa
;
1496 /* only allocate multiples of 4 byte */
1498 size
= (size
+ 3) & (~3UL);
1500 struct working_area
*c
= target
->working_areas
;
1502 /* Find the first large enough working area */
1504 if (c
->free
&& c
->size
>= size
)
1510 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1512 /* Split the working area into the requested size */
1513 target_split_working_area(c
, size
);
1515 LOG_DEBUG("allocated new working area of %"PRIu32
" bytes at address 0x%08"PRIx32
, size
, c
->address
);
1517 if (target
->backup_working_area
) {
1518 if (c
->backup
== NULL
) {
1519 c
->backup
= malloc(c
->size
);
1520 if (c
->backup
== NULL
)
1524 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1525 if (retval
!= ERROR_OK
)
1529 /* mark as used, and return the new (reused) area */
1536 print_wa_layout(target
);
1541 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1545 retval
= target_alloc_working_area_try(target
, size
, area
);
1546 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1547 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1552 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1554 int retval
= ERROR_OK
;
1556 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1557 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1558 if (retval
!= ERROR_OK
)
1559 LOG_ERROR("failed to restore %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1560 area
->size
, area
->address
);
1566 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1567 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1569 int retval
= ERROR_OK
;
1575 retval
= target_restore_working_area(target
, area
);
1576 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1577 if (retval
!= ERROR_OK
)
1583 LOG_DEBUG("freed %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1584 area
->size
, area
->address
);
1586 /* mark user pointer invalid */
1587 /* TODO: Is this really safe? It points to some previous caller's memory.
1588 * How could we know that the area pointer is still in that place and not
1589 * some other vital data? What's the purpose of this, anyway? */
1593 target_merge_working_areas(target
);
1595 print_wa_layout(target
);
1600 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1602 return target_free_working_area_restore(target
, area
, 1);
1605 /* free resources and restore memory, if restoring memory fails,
1606 * free up resources anyway
1608 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1610 struct working_area
*c
= target
->working_areas
;
1612 LOG_DEBUG("freeing all working areas");
1614 /* Loop through all areas, restoring the allocated ones and marking them as free */
1618 target_restore_working_area(target
, c
);
1620 *c
->user
= NULL
; /* Same as above */
1626 /* Run a merge pass to combine all areas into one */
1627 target_merge_working_areas(target
);
1629 print_wa_layout(target
);
1632 void target_free_all_working_areas(struct target
*target
)
1634 target_free_all_working_areas_restore(target
, 1);
1637 /* Find the largest number of bytes that can be allocated */
1638 uint32_t target_get_working_area_avail(struct target
*target
)
1640 struct working_area
*c
= target
->working_areas
;
1641 uint32_t max_size
= 0;
1644 return target
->working_area_size
;
1647 if (c
->free
&& max_size
< c
->size
)
1656 int target_arch_state(struct target
*target
)
1659 if (target
== NULL
) {
1660 LOG_USER("No target has been configured");
1664 LOG_USER("target state: %s", target_state_name(target
));
1666 if (target
->state
!= TARGET_HALTED
)
1669 retval
= target
->type
->arch_state(target
);
1673 /* Single aligned words are guaranteed to use 16 or 32 bit access
1674 * mode respectively, otherwise data is handled as quickly as
1677 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
1679 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1680 (int)size
, (unsigned)address
);
1682 if (!target_was_examined(target
)) {
1683 LOG_ERROR("Target not examined yet");
1690 if ((address
+ size
- 1) < address
) {
1691 /* GDB can request this when e.g. PC is 0xfffffffc*/
1692 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1698 return target
->type
->write_buffer(target
, address
, size
, buffer
);
1701 static int target_write_buffer_default(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
1703 int retval
= ERROR_OK
;
1705 if (((address
% 2) == 0) && (size
== 2))
1706 return target_write_memory(target
, address
, 2, 1, buffer
);
1708 /* handle unaligned head bytes */
1710 uint32_t unaligned
= 4 - (address
% 4);
1712 if (unaligned
> size
)
1715 retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
);
1716 if (retval
!= ERROR_OK
)
1719 buffer
+= unaligned
;
1720 address
+= unaligned
;
1724 /* handle aligned words */
1726 int aligned
= size
- (size
% 4);
1728 /* use bulk writes above a certain limit. This may have to be changed */
1729 if (aligned
> 128) {
1730 retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
);
1731 if (retval
!= ERROR_OK
)
1734 retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
);
1735 if (retval
!= ERROR_OK
)
1744 /* handle tail writes of less than 4 bytes */
1746 retval
= target_write_memory(target
, address
, 1, size
, buffer
);
1747 if (retval
!= ERROR_OK
)
1754 /* Single aligned words are guaranteed to use 16 or 32 bit access
1755 * mode respectively, otherwise data is handled as quickly as
1758 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1760 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1761 (int)size
, (unsigned)address
);
1763 if (!target_was_examined(target
)) {
1764 LOG_ERROR("Target not examined yet");
1771 if ((address
+ size
- 1) < address
) {
1772 /* GDB can request this when e.g. PC is 0xfffffffc*/
1773 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1779 return target
->type
->read_buffer(target
, address
, size
, buffer
);
1782 static int target_read_buffer_default(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1784 int retval
= ERROR_OK
;
1786 if (((address
% 2) == 0) && (size
== 2))
1787 return target_read_memory(target
, address
, 2, 1, buffer
);
1789 /* handle unaligned head bytes */
1791 uint32_t unaligned
= 4 - (address
% 4);
1793 if (unaligned
> size
)
1796 retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
);
1797 if (retval
!= ERROR_OK
)
1800 buffer
+= unaligned
;
1801 address
+= unaligned
;
1805 /* handle aligned words */
1807 int aligned
= size
- (size
% 4);
1809 retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
);
1810 if (retval
!= ERROR_OK
)
1818 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1820 int aligned
= size
- (size
% 2);
1821 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1822 if (retval
!= ERROR_OK
)
1829 /* handle tail writes of less than 4 bytes */
1831 retval
= target_read_memory(target
, address
, 1, size
, buffer
);
1832 if (retval
!= ERROR_OK
)
1839 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1844 uint32_t checksum
= 0;
1845 if (!target_was_examined(target
)) {
1846 LOG_ERROR("Target not examined yet");
1850 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
1851 if (retval
!= ERROR_OK
) {
1852 buffer
= malloc(size
);
1853 if (buffer
== NULL
) {
1854 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1855 return ERROR_COMMAND_SYNTAX_ERROR
;
1857 retval
= target_read_buffer(target
, address
, size
, buffer
);
1858 if (retval
!= ERROR_OK
) {
1863 /* convert to target endianness */
1864 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
1865 uint32_t target_data
;
1866 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1867 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1870 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1879 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1882 if (!target_was_examined(target
)) {
1883 LOG_ERROR("Target not examined yet");
1887 if (target
->type
->blank_check_memory
== 0)
1888 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1890 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1895 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1897 uint8_t value_buf
[4];
1898 if (!target_was_examined(target
)) {
1899 LOG_ERROR("Target not examined yet");
1903 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1905 if (retval
== ERROR_OK
) {
1906 *value
= target_buffer_get_u32(target
, value_buf
);
1907 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1912 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1919 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
1921 uint8_t value_buf
[2];
1922 if (!target_was_examined(target
)) {
1923 LOG_ERROR("Target not examined yet");
1927 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1929 if (retval
== ERROR_OK
) {
1930 *value
= target_buffer_get_u16(target
, value_buf
);
1931 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1936 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1943 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
1945 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1946 if (!target_was_examined(target
)) {
1947 LOG_ERROR("Target not examined yet");
1951 if (retval
== ERROR_OK
) {
1952 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1957 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1964 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
1967 uint8_t value_buf
[4];
1968 if (!target_was_examined(target
)) {
1969 LOG_ERROR("Target not examined yet");
1973 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1977 target_buffer_set_u32(target
, value_buf
, value
);
1978 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
1979 if (retval
!= ERROR_OK
)
1980 LOG_DEBUG("failed: %i", retval
);
1985 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
1988 uint8_t value_buf
[2];
1989 if (!target_was_examined(target
)) {
1990 LOG_ERROR("Target not examined yet");
1994 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
1998 target_buffer_set_u16(target
, value_buf
, value
);
1999 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2000 if (retval
!= ERROR_OK
)
2001 LOG_DEBUG("failed: %i", retval
);
2006 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
2009 if (!target_was_examined(target
)) {
2010 LOG_ERROR("Target not examined yet");
2014 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
2017 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2018 if (retval
!= ERROR_OK
)
2019 LOG_DEBUG("failed: %i", retval
);
2024 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2026 struct target
*target
= get_target(name
);
2027 if (target
== NULL
) {
2028 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2031 if (!target
->tap
->enabled
) {
2032 LOG_USER("Target: TAP %s is disabled, "
2033 "can't be the current target\n",
2034 target
->tap
->dotted_name
);
2038 cmd_ctx
->current_target
= target
->target_number
;
2043 COMMAND_HANDLER(handle_targets_command
)
2045 int retval
= ERROR_OK
;
2046 if (CMD_ARGC
== 1) {
2047 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2048 if (retval
== ERROR_OK
) {
2054 struct target
*target
= all_targets
;
2055 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2056 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2061 if (target
->tap
->enabled
)
2062 state
= target_state_name(target
);
2064 state
= "tap-disabled";
2066 if (CMD_CTX
->current_target
== target
->target_number
)
2069 /* keep columns lined up to match the headers above */
2070 command_print(CMD_CTX
,
2071 "%2d%c %-18s %-10s %-6s %-18s %s",
2072 target
->target_number
,
2074 target_name(target
),
2075 target_type_name(target
),
2076 Jim_Nvp_value2name_simple(nvp_target_endian
,
2077 target
->endianness
)->name
,
2078 target
->tap
->dotted_name
,
2080 target
= target
->next
;
2086 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2088 static int powerDropout
;
2089 static int srstAsserted
;
2091 static int runPowerRestore
;
2092 static int runPowerDropout
;
2093 static int runSrstAsserted
;
2094 static int runSrstDeasserted
;
2096 static int sense_handler(void)
2098 static int prevSrstAsserted
;
2099 static int prevPowerdropout
;
2101 int retval
= jtag_power_dropout(&powerDropout
);
2102 if (retval
!= ERROR_OK
)
2106 powerRestored
= prevPowerdropout
&& !powerDropout
;
2108 runPowerRestore
= 1;
2110 long long current
= timeval_ms();
2111 static long long lastPower
;
2112 int waitMore
= lastPower
+ 2000 > current
;
2113 if (powerDropout
&& !waitMore
) {
2114 runPowerDropout
= 1;
2115 lastPower
= current
;
2118 retval
= jtag_srst_asserted(&srstAsserted
);
2119 if (retval
!= ERROR_OK
)
2123 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2125 static long long lastSrst
;
2126 waitMore
= lastSrst
+ 2000 > current
;
2127 if (srstDeasserted
&& !waitMore
) {
2128 runSrstDeasserted
= 1;
2132 if (!prevSrstAsserted
&& srstAsserted
)
2133 runSrstAsserted
= 1;
2135 prevSrstAsserted
= srstAsserted
;
2136 prevPowerdropout
= powerDropout
;
2138 if (srstDeasserted
|| powerRestored
) {
2139 /* Other than logging the event we can't do anything here.
2140 * Issuing a reset is a particularly bad idea as we might
2141 * be inside a reset already.
2148 /* process target state changes */
2149 static int handle_target(void *priv
)
2151 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2152 int retval
= ERROR_OK
;
2154 if (!is_jtag_poll_safe()) {
2155 /* polling is disabled currently */
2159 /* we do not want to recurse here... */
2160 static int recursive
;
2164 /* danger! running these procedures can trigger srst assertions and power dropouts.
2165 * We need to avoid an infinite loop/recursion here and we do that by
2166 * clearing the flags after running these events.
2168 int did_something
= 0;
2169 if (runSrstAsserted
) {
2170 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2171 Jim_Eval(interp
, "srst_asserted");
2174 if (runSrstDeasserted
) {
2175 Jim_Eval(interp
, "srst_deasserted");
2178 if (runPowerDropout
) {
2179 LOG_INFO("Power dropout detected, running power_dropout proc.");
2180 Jim_Eval(interp
, "power_dropout");
2183 if (runPowerRestore
) {
2184 Jim_Eval(interp
, "power_restore");
2188 if (did_something
) {
2189 /* clear detect flags */
2193 /* clear action flags */
2195 runSrstAsserted
= 0;
2196 runSrstDeasserted
= 0;
2197 runPowerRestore
= 0;
2198 runPowerDropout
= 0;
2203 /* Poll targets for state changes unless that's globally disabled.
2204 * Skip targets that are currently disabled.
2206 for (struct target
*target
= all_targets
;
2207 is_jtag_poll_safe() && target
;
2208 target
= target
->next
) {
2209 if (!target
->tap
->enabled
)
2212 if (target
->backoff
.times
> target
->backoff
.count
) {
2213 /* do not poll this time as we failed previously */
2214 target
->backoff
.count
++;
2217 target
->backoff
.count
= 0;
2219 /* only poll target if we've got power and srst isn't asserted */
2220 if (!powerDropout
&& !srstAsserted
) {
2221 /* polling may fail silently until the target has been examined */
2222 retval
= target_poll(target
);
2223 if (retval
!= ERROR_OK
) {
2224 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2225 if (target
->backoff
.times
* polling_interval
< 5000) {
2226 target
->backoff
.times
*= 2;
2227 target
->backoff
.times
++;
2229 LOG_USER("Polling target %s failed, GDB will be halted. Polling again in %dms",
2230 target_name(target
),
2231 target
->backoff
.times
* polling_interval
);
2233 /* Tell GDB to halt the debugger. This allows the user to
2234 * run monitor commands to handle the situation.
2236 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2239 /* Since we succeeded, we reset backoff count */
2240 if (target
->backoff
.times
> 0)
2241 LOG_USER("Polling target %s succeeded again", target_name(target
));
2242 target
->backoff
.times
= 0;
2249 COMMAND_HANDLER(handle_reg_command
)
2251 struct target
*target
;
2252 struct reg
*reg
= NULL
;
2258 target
= get_current_target(CMD_CTX
);
2260 /* list all available registers for the current target */
2261 if (CMD_ARGC
== 0) {
2262 struct reg_cache
*cache
= target
->reg_cache
;
2268 command_print(CMD_CTX
, "===== %s", cache
->name
);
2270 for (i
= 0, reg
= cache
->reg_list
;
2271 i
< cache
->num_regs
;
2272 i
++, reg
++, count
++) {
2273 /* only print cached values if they are valid */
2275 value
= buf_to_str(reg
->value
,
2277 command_print(CMD_CTX
,
2278 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2286 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2291 cache
= cache
->next
;
2297 /* access a single register by its ordinal number */
2298 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2300 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2302 struct reg_cache
*cache
= target
->reg_cache
;
2306 for (i
= 0; i
< cache
->num_regs
; i
++) {
2307 if (count
++ == num
) {
2308 reg
= &cache
->reg_list
[i
];
2314 cache
= cache
->next
;
2318 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2319 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2323 /* access a single register by its name */
2324 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2327 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2332 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2334 /* display a register */
2335 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2336 && (CMD_ARGV
[1][0] <= '9')))) {
2337 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2340 if (reg
->valid
== 0)
2341 reg
->type
->get(reg
);
2342 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2343 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2348 /* set register value */
2349 if (CMD_ARGC
== 2) {
2350 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2353 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2355 reg
->type
->set(reg
, buf
);
2357 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2358 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2366 return ERROR_COMMAND_SYNTAX_ERROR
;
2369 COMMAND_HANDLER(handle_poll_command
)
2371 int retval
= ERROR_OK
;
2372 struct target
*target
= get_current_target(CMD_CTX
);
2374 if (CMD_ARGC
== 0) {
2375 command_print(CMD_CTX
, "background polling: %s",
2376 jtag_poll_get_enabled() ? "on" : "off");
2377 command_print(CMD_CTX
, "TAP: %s (%s)",
2378 target
->tap
->dotted_name
,
2379 target
->tap
->enabled
? "enabled" : "disabled");
2380 if (!target
->tap
->enabled
)
2382 retval
= target_poll(target
);
2383 if (retval
!= ERROR_OK
)
2385 retval
= target_arch_state(target
);
2386 if (retval
!= ERROR_OK
)
2388 } else if (CMD_ARGC
== 1) {
2390 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2391 jtag_poll_set_enabled(enable
);
2393 return ERROR_COMMAND_SYNTAX_ERROR
;
2398 COMMAND_HANDLER(handle_wait_halt_command
)
2401 return ERROR_COMMAND_SYNTAX_ERROR
;
2404 if (1 == CMD_ARGC
) {
2405 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2406 if (ERROR_OK
!= retval
)
2407 return ERROR_COMMAND_SYNTAX_ERROR
;
2408 /* convert seconds (given) to milliseconds (needed) */
2412 struct target
*target
= get_current_target(CMD_CTX
);
2413 return target_wait_state(target
, TARGET_HALTED
, ms
);
2416 /* wait for target state to change. The trick here is to have a low
2417 * latency for short waits and not to suck up all the CPU time
2420 * After 500ms, keep_alive() is invoked
2422 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2425 long long then
= 0, cur
;
2429 retval
= target_poll(target
);
2430 if (retval
!= ERROR_OK
)
2432 if (target
->state
== state
)
2437 then
= timeval_ms();
2438 LOG_DEBUG("waiting for target %s...",
2439 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2445 if ((cur
-then
) > ms
) {
2446 LOG_ERROR("timed out while waiting for target %s",
2447 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2455 COMMAND_HANDLER(handle_halt_command
)
2459 struct target
*target
= get_current_target(CMD_CTX
);
2460 int retval
= target_halt(target
);
2461 if (ERROR_OK
!= retval
)
2464 if (CMD_ARGC
== 1) {
2465 unsigned wait_local
;
2466 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2467 if (ERROR_OK
!= retval
)
2468 return ERROR_COMMAND_SYNTAX_ERROR
;
2473 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2476 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2478 struct target
*target
= get_current_target(CMD_CTX
);
2480 LOG_USER("requesting target halt and executing a soft reset");
2482 target_soft_reset_halt(target
);
2487 COMMAND_HANDLER(handle_reset_command
)
2490 return ERROR_COMMAND_SYNTAX_ERROR
;
2492 enum target_reset_mode reset_mode
= RESET_RUN
;
2493 if (CMD_ARGC
== 1) {
2495 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2496 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2497 return ERROR_COMMAND_SYNTAX_ERROR
;
2498 reset_mode
= n
->value
;
2501 /* reset *all* targets */
2502 return target_process_reset(CMD_CTX
, reset_mode
);
2506 COMMAND_HANDLER(handle_resume_command
)
2510 return ERROR_COMMAND_SYNTAX_ERROR
;
2512 struct target
*target
= get_current_target(CMD_CTX
);
2514 /* with no CMD_ARGV, resume from current pc, addr = 0,
2515 * with one arguments, addr = CMD_ARGV[0],
2516 * handle breakpoints, not debugging */
2518 if (CMD_ARGC
== 1) {
2519 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2523 return target_resume(target
, current
, addr
, 1, 0);
2526 COMMAND_HANDLER(handle_step_command
)
2529 return ERROR_COMMAND_SYNTAX_ERROR
;
2533 /* with no CMD_ARGV, step from current pc, addr = 0,
2534 * with one argument addr = CMD_ARGV[0],
2535 * handle breakpoints, debugging */
2538 if (CMD_ARGC
== 1) {
2539 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2543 struct target
*target
= get_current_target(CMD_CTX
);
2545 return target
->type
->step(target
, current_pc
, addr
, 1);
2548 static void handle_md_output(struct command_context
*cmd_ctx
,
2549 struct target
*target
, uint32_t address
, unsigned size
,
2550 unsigned count
, const uint8_t *buffer
)
2552 const unsigned line_bytecnt
= 32;
2553 unsigned line_modulo
= line_bytecnt
/ size
;
2555 char output
[line_bytecnt
* 4 + 1];
2556 unsigned output_len
= 0;
2558 const char *value_fmt
;
2561 value_fmt
= "%8.8x ";
2564 value_fmt
= "%4.4x ";
2567 value_fmt
= "%2.2x ";
2570 /* "can't happen", caller checked */
2571 LOG_ERROR("invalid memory read size: %u", size
);
2575 for (unsigned i
= 0; i
< count
; i
++) {
2576 if (i
% line_modulo
== 0) {
2577 output_len
+= snprintf(output
+ output_len
,
2578 sizeof(output
) - output_len
,
2580 (unsigned)(address
+ (i
*size
)));
2584 const uint8_t *value_ptr
= buffer
+ i
* size
;
2587 value
= target_buffer_get_u32(target
, value_ptr
);
2590 value
= target_buffer_get_u16(target
, value_ptr
);
2595 output_len
+= snprintf(output
+ output_len
,
2596 sizeof(output
) - output_len
,
2599 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
2600 command_print(cmd_ctx
, "%s", output
);
2606 COMMAND_HANDLER(handle_md_command
)
2609 return ERROR_COMMAND_SYNTAX_ERROR
;
2612 switch (CMD_NAME
[2]) {
2623 return ERROR_COMMAND_SYNTAX_ERROR
;
2626 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2627 int (*fn
)(struct target
*target
,
2628 uint32_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
2632 fn
= target_read_phys_memory
;
2634 fn
= target_read_memory
;
2635 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2636 return ERROR_COMMAND_SYNTAX_ERROR
;
2639 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2643 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2645 uint8_t *buffer
= calloc(count
, size
);
2647 struct target
*target
= get_current_target(CMD_CTX
);
2648 int retval
= fn(target
, address
, size
, count
, buffer
);
2649 if (ERROR_OK
== retval
)
2650 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2657 typedef int (*target_write_fn
)(struct target
*target
,
2658 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
2660 static int target_write_memory_fast(struct target
*target
,
2661 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
2663 return target_write_buffer(target
, address
, size
* count
, buffer
);
2666 static int target_fill_mem(struct target
*target
,
2675 /* We have to write in reasonably large chunks to be able
2676 * to fill large memory areas with any sane speed */
2677 const unsigned chunk_size
= 16384;
2678 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
2679 if (target_buf
== NULL
) {
2680 LOG_ERROR("Out of memory");
2684 for (unsigned i
= 0; i
< chunk_size
; i
++) {
2685 switch (data_size
) {
2687 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
2690 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
2693 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
2700 int retval
= ERROR_OK
;
2702 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
2705 if (current
> chunk_size
)
2706 current
= chunk_size
;
2707 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
2708 if (retval
!= ERROR_OK
)
2710 /* avoid GDB timeouts */
2719 COMMAND_HANDLER(handle_mw_command
)
2722 return ERROR_COMMAND_SYNTAX_ERROR
;
2723 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2728 fn
= target_write_phys_memory
;
2730 fn
= target_write_memory_fast
;
2731 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2732 return ERROR_COMMAND_SYNTAX_ERROR
;
2735 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2738 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2742 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2744 struct target
*target
= get_current_target(CMD_CTX
);
2746 switch (CMD_NAME
[2]) {
2757 return ERROR_COMMAND_SYNTAX_ERROR
;
2760 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
2763 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2764 uint32_t *min_address
, uint32_t *max_address
)
2766 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2767 return ERROR_COMMAND_SYNTAX_ERROR
;
2769 /* a base address isn't always necessary,
2770 * default to 0x0 (i.e. don't relocate) */
2771 if (CMD_ARGC
>= 2) {
2773 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2774 image
->base_address
= addr
;
2775 image
->base_address_set
= 1;
2777 image
->base_address_set
= 0;
2779 image
->start_address_set
= 0;
2782 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2783 if (CMD_ARGC
== 5) {
2784 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2785 /* use size (given) to find max (required) */
2786 *max_address
+= *min_address
;
2789 if (*min_address
> *max_address
)
2790 return ERROR_COMMAND_SYNTAX_ERROR
;
2795 COMMAND_HANDLER(handle_load_image_command
)
2799 uint32_t image_size
;
2800 uint32_t min_address
= 0;
2801 uint32_t max_address
= 0xffffffff;
2805 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2806 &image
, &min_address
, &max_address
);
2807 if (ERROR_OK
!= retval
)
2810 struct target
*target
= get_current_target(CMD_CTX
);
2812 struct duration bench
;
2813 duration_start(&bench
);
2815 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2820 for (i
= 0; i
< image
.num_sections
; i
++) {
2821 buffer
= malloc(image
.sections
[i
].size
);
2822 if (buffer
== NULL
) {
2823 command_print(CMD_CTX
,
2824 "error allocating buffer for section (%d bytes)",
2825 (int)(image
.sections
[i
].size
));
2829 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
2830 if (retval
!= ERROR_OK
) {
2835 uint32_t offset
= 0;
2836 uint32_t length
= buf_cnt
;
2838 /* DANGER!!! beware of unsigned comparision here!!! */
2840 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
2841 (image
.sections
[i
].base_address
< max_address
)) {
2843 if (image
.sections
[i
].base_address
< min_address
) {
2844 /* clip addresses below */
2845 offset
+= min_address
-image
.sections
[i
].base_address
;
2849 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2850 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2852 retval
= target_write_buffer(target
,
2853 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
2854 if (retval
!= ERROR_OK
) {
2858 image_size
+= length
;
2859 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2860 (unsigned int)length
,
2861 image
.sections
[i
].base_address
+ offset
);
2867 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
2868 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2869 "in %fs (%0.3f KiB/s)", image_size
,
2870 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2873 image_close(&image
);
2879 COMMAND_HANDLER(handle_dump_image_command
)
2881 struct fileio fileio
;
2883 int retval
, retvaltemp
;
2884 uint32_t address
, size
;
2885 struct duration bench
;
2886 struct target
*target
= get_current_target(CMD_CTX
);
2889 return ERROR_COMMAND_SYNTAX_ERROR
;
2891 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2892 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2894 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
2895 buffer
= malloc(buf_size
);
2899 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
2900 if (retval
!= ERROR_OK
) {
2905 duration_start(&bench
);
2908 size_t size_written
;
2909 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
2910 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2911 if (retval
!= ERROR_OK
)
2914 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2915 if (retval
!= ERROR_OK
)
2918 size
-= this_run_size
;
2919 address
+= this_run_size
;
2924 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
2926 retval
= fileio_size(&fileio
, &filesize
);
2927 if (retval
!= ERROR_OK
)
2929 command_print(CMD_CTX
,
2930 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize
,
2931 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
2934 retvaltemp
= fileio_close(&fileio
);
2935 if (retvaltemp
!= ERROR_OK
)
2941 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
2945 uint32_t image_size
;
2948 uint32_t checksum
= 0;
2949 uint32_t mem_checksum
= 0;
2953 struct target
*target
= get_current_target(CMD_CTX
);
2956 return ERROR_COMMAND_SYNTAX_ERROR
;
2959 LOG_ERROR("no target selected");
2963 struct duration bench
;
2964 duration_start(&bench
);
2966 if (CMD_ARGC
>= 2) {
2968 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2969 image
.base_address
= addr
;
2970 image
.base_address_set
= 1;
2972 image
.base_address_set
= 0;
2973 image
.base_address
= 0x0;
2976 image
.start_address_set
= 0;
2978 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
2979 if (retval
!= ERROR_OK
)
2985 for (i
= 0; i
< image
.num_sections
; i
++) {
2986 buffer
= malloc(image
.sections
[i
].size
);
2987 if (buffer
== NULL
) {
2988 command_print(CMD_CTX
,
2989 "error allocating buffer for section (%d bytes)",
2990 (int)(image
.sections
[i
].size
));
2993 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
2994 if (retval
!= ERROR_OK
) {
3000 /* calculate checksum of image */
3001 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3002 if (retval
!= ERROR_OK
) {
3007 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3008 if (retval
!= ERROR_OK
) {
3013 if (checksum
!= mem_checksum
) {
3014 /* failed crc checksum, fall back to a binary compare */
3018 LOG_ERROR("checksum mismatch - attempting binary compare");
3020 data
= (uint8_t *)malloc(buf_cnt
);
3022 /* Can we use 32bit word accesses? */
3024 int count
= buf_cnt
;
3025 if ((count
% 4) == 0) {
3029 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3030 if (retval
== ERROR_OK
) {
3032 for (t
= 0; t
< buf_cnt
; t
++) {
3033 if (data
[t
] != buffer
[t
]) {
3034 command_print(CMD_CTX
,
3035 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3037 (unsigned)(t
+ image
.sections
[i
].base_address
),
3040 if (diffs
++ >= 127) {
3041 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3053 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
3054 image
.sections
[i
].base_address
,
3059 image_size
+= buf_cnt
;
3062 command_print(CMD_CTX
, "No more differences found.");
3065 retval
= ERROR_FAIL
;
3066 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3067 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3068 "in %fs (%0.3f KiB/s)", image_size
,
3069 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3072 image_close(&image
);
3077 COMMAND_HANDLER(handle_verify_image_command
)
3079 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
3082 COMMAND_HANDLER(handle_test_image_command
)
3084 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
3087 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3089 struct target
*target
= get_current_target(cmd_ctx
);
3090 struct breakpoint
*breakpoint
= target
->breakpoints
;
3091 while (breakpoint
) {
3092 if (breakpoint
->type
== BKPT_SOFT
) {
3093 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3094 breakpoint
->length
, 16);
3095 command_print(cmd_ctx
, "IVA breakpoint: 0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
3096 breakpoint
->address
,
3098 breakpoint
->set
, buf
);
3101 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3102 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3104 breakpoint
->length
, breakpoint
->set
);
3105 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3106 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3107 breakpoint
->address
,
3108 breakpoint
->length
, breakpoint
->set
);
3109 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3112 command_print(cmd_ctx
, "Breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3113 breakpoint
->address
,
3114 breakpoint
->length
, breakpoint
->set
);
3117 breakpoint
= breakpoint
->next
;
3122 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
3123 uint32_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3125 struct target
*target
= get_current_target(cmd_ctx
);
3128 int retval
= breakpoint_add(target
, addr
, length
, hw
);
3129 if (ERROR_OK
== retval
)
3130 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
3132 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3135 } else if (addr
== 0) {
3136 int retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3137 if (ERROR_OK
== retval
)
3138 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3140 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3144 int retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3145 if (ERROR_OK
== retval
)
3146 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3148 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3155 COMMAND_HANDLER(handle_bp_command
)
3164 return handle_bp_command_list(CMD_CTX
);
3168 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3169 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3170 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3173 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3175 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3177 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3180 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3181 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
3183 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3184 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3186 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3191 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3192 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3193 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3194 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3197 return ERROR_COMMAND_SYNTAX_ERROR
;
3201 COMMAND_HANDLER(handle_rbp_command
)
3204 return ERROR_COMMAND_SYNTAX_ERROR
;
3207 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3209 struct target
*target
= get_current_target(CMD_CTX
);
3210 breakpoint_remove(target
, addr
);
3215 COMMAND_HANDLER(handle_wp_command
)
3217 struct target
*target
= get_current_target(CMD_CTX
);
3219 if (CMD_ARGC
== 0) {
3220 struct watchpoint
*watchpoint
= target
->watchpoints
;
3222 while (watchpoint
) {
3223 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
3224 ", len: 0x%8.8" PRIx32
3225 ", r/w/a: %i, value: 0x%8.8" PRIx32
3226 ", mask: 0x%8.8" PRIx32
,
3227 watchpoint
->address
,
3229 (int)watchpoint
->rw
,
3232 watchpoint
= watchpoint
->next
;
3237 enum watchpoint_rw type
= WPT_ACCESS
;
3239 uint32_t length
= 0;
3240 uint32_t data_value
= 0x0;
3241 uint32_t data_mask
= 0xffffffff;
3245 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3248 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3251 switch (CMD_ARGV
[2][0]) {
3262 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3263 return ERROR_COMMAND_SYNTAX_ERROR
;
3267 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3268 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3272 return ERROR_COMMAND_SYNTAX_ERROR
;
3275 int retval
= watchpoint_add(target
, addr
, length
, type
,
3276 data_value
, data_mask
);
3277 if (ERROR_OK
!= retval
)
3278 LOG_ERROR("Failure setting watchpoints");
3283 COMMAND_HANDLER(handle_rwp_command
)
3286 return ERROR_COMMAND_SYNTAX_ERROR
;
3289 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3291 struct target
*target
= get_current_target(CMD_CTX
);
3292 watchpoint_remove(target
, addr
);
3298 * Translate a virtual address to a physical address.
3300 * The low-level target implementation must have logged a detailed error
3301 * which is forwarded to telnet/GDB session.
3303 COMMAND_HANDLER(handle_virt2phys_command
)
3306 return ERROR_COMMAND_SYNTAX_ERROR
;
3309 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
3312 struct target
*target
= get_current_target(CMD_CTX
);
3313 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3314 if (retval
== ERROR_OK
)
3315 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
3320 static void writeData(FILE *f
, const void *data
, size_t len
)
3322 size_t written
= fwrite(data
, 1, len
, f
);
3324 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3327 static void writeLong(FILE *f
, int l
)
3330 for (i
= 0; i
< 4; i
++) {
3331 char c
= (l
>> (i
*8))&0xff;
3332 writeData(f
, &c
, 1);
3337 static void writeString(FILE *f
, char *s
)
3339 writeData(f
, s
, strlen(s
));
3342 /* Dump a gmon.out histogram file. */
3343 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
)
3346 FILE *f
= fopen(filename
, "w");
3349 writeString(f
, "gmon");
3350 writeLong(f
, 0x00000001); /* Version */
3351 writeLong(f
, 0); /* padding */
3352 writeLong(f
, 0); /* padding */
3353 writeLong(f
, 0); /* padding */
3355 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3356 writeData(f
, &zero
, 1);
3358 /* figure out bucket size */
3359 uint32_t min
= samples
[0];
3360 uint32_t max
= samples
[0];
3361 for (i
= 0; i
< sampleNum
; i
++) {
3362 if (min
> samples
[i
])
3364 if (max
< samples
[i
])
3368 int addressSpace
= (max
- min
+ 1);
3369 assert(addressSpace
>= 2);
3371 static const uint32_t maxBuckets
= 16 * 1024; /* maximum buckets. */
3372 uint32_t length
= addressSpace
;
3373 if (length
> maxBuckets
)
3374 length
= maxBuckets
;
3375 int *buckets
= malloc(sizeof(int)*length
);
3376 if (buckets
== NULL
) {
3380 memset(buckets
, 0, sizeof(int) * length
);
3381 for (i
= 0; i
< sampleNum
; i
++) {
3382 uint32_t address
= samples
[i
];
3383 long long a
= address
- min
;
3384 long long b
= length
- 1;
3385 long long c
= addressSpace
- 1;
3386 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
3390 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3391 writeLong(f
, min
); /* low_pc */
3392 writeLong(f
, max
); /* high_pc */
3393 writeLong(f
, length
); /* # of samples */
3394 writeLong(f
, 100); /* KLUDGE! We lie, ca. 100Hz best case. */
3395 writeString(f
, "seconds");
3396 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3397 writeData(f
, &zero
, 1);
3398 writeString(f
, "s");
3400 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3402 char *data
= malloc(2 * length
);
3404 for (i
= 0; i
< length
; i
++) {
3409 data
[i
* 2] = val
&0xff;
3410 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
3413 writeData(f
, data
, length
* 2);
3421 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3422 * which will be used as a random sampling of PC */
3423 COMMAND_HANDLER(handle_profile_command
)
3425 struct target
*target
= get_current_target(CMD_CTX
);
3426 struct timeval timeout
, now
;
3428 gettimeofday(&timeout
, NULL
);
3430 return ERROR_COMMAND_SYNTAX_ERROR
;
3432 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], offset
);
3434 timeval_add_time(&timeout
, offset
, 0);
3437 * @todo: Some cores let us sample the PC without the
3438 * annoying halt/resume step; for example, ARMv7 PCSR.
3439 * Provide a way to use that more efficient mechanism.
3442 command_print(CMD_CTX
, "Starting profiling. Halting and resuming the target as often as we can...");
3444 static const int maxSample
= 10000;
3445 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
3446 if (samples
== NULL
)
3450 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3451 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
3453 int retval
= ERROR_OK
;
3455 target_poll(target
);
3456 if (target
->state
== TARGET_HALTED
) {
3457 uint32_t t
= *((uint32_t *)reg
->value
);
3458 samples
[numSamples
++] = t
;
3459 /* current pc, addr = 0, do not handle breakpoints, not debugging */
3460 retval
= target_resume(target
, 1, 0, 0, 0);
3461 target_poll(target
);
3462 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3463 } else if (target
->state
== TARGET_RUNNING
) {
3464 /* We want to quickly sample the PC. */
3465 retval
= target_halt(target
);
3466 if (retval
!= ERROR_OK
) {
3471 command_print(CMD_CTX
, "Target not halted or running");
3475 if (retval
!= ERROR_OK
)
3478 gettimeofday(&now
, NULL
);
3479 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
)
3480 && (now
.tv_usec
>= timeout
.tv_usec
))) {
3481 command_print(CMD_CTX
, "Profiling completed. %d samples.", numSamples
);
3482 retval
= target_poll(target
);
3483 if (retval
!= ERROR_OK
) {
3487 if (target
->state
== TARGET_HALTED
) {
3488 /* current pc, addr = 0, do not handle
3489 * breakpoints, not debugging */
3490 target_resume(target
, 1, 0, 0, 0);
3492 retval
= target_poll(target
);
3493 if (retval
!= ERROR_OK
) {
3497 writeGmon(samples
, numSamples
, CMD_ARGV
[1]);
3498 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3507 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
3510 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3513 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3517 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3518 valObjPtr
= Jim_NewIntObj(interp
, val
);
3519 if (!nameObjPtr
|| !valObjPtr
) {
3524 Jim_IncrRefCount(nameObjPtr
);
3525 Jim_IncrRefCount(valObjPtr
);
3526 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3527 Jim_DecrRefCount(interp
, nameObjPtr
);
3528 Jim_DecrRefCount(interp
, valObjPtr
);
3530 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3534 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3536 struct command_context
*context
;
3537 struct target
*target
;
3539 context
= current_command_context(interp
);
3540 assert(context
!= NULL
);
3542 target
= get_current_target(context
);
3543 if (target
== NULL
) {
3544 LOG_ERROR("mem2array: no current target");
3548 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
3551 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3559 const char *varname
;
3563 /* argv[1] = name of array to receive the data
3564 * argv[2] = desired width
3565 * argv[3] = memory address
3566 * argv[4] = count of times to read
3569 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3572 varname
= Jim_GetString(argv
[0], &len
);
3573 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3575 e
= Jim_GetLong(interp
, argv
[1], &l
);
3580 e
= Jim_GetLong(interp
, argv
[2], &l
);
3584 e
= Jim_GetLong(interp
, argv
[3], &l
);
3599 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3600 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3604 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3605 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3608 if ((addr
+ (len
* width
)) < addr
) {
3609 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3610 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3613 /* absurd transfer size? */
3615 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3616 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3621 ((width
== 2) && ((addr
& 1) == 0)) ||
3622 ((width
== 4) && ((addr
& 3) == 0))) {
3626 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3627 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3630 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3639 size_t buffersize
= 4096;
3640 uint8_t *buffer
= malloc(buffersize
);
3647 /* Slurp... in buffer size chunks */
3649 count
= len
; /* in objects.. */
3650 if (count
> (buffersize
/ width
))
3651 count
= (buffersize
/ width
);
3653 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3654 if (retval
!= ERROR_OK
) {
3656 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3660 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3661 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3665 v
= 0; /* shut up gcc */
3666 for (i
= 0; i
< count
; i
++, n
++) {
3669 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3672 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3675 v
= buffer
[i
] & 0x0ff;
3678 new_int_array_element(interp
, varname
, n
, v
);
3686 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3691 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
3694 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3698 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3702 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3708 Jim_IncrRefCount(nameObjPtr
);
3709 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3710 Jim_DecrRefCount(interp
, nameObjPtr
);
3712 if (valObjPtr
== NULL
)
3715 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3716 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3721 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3723 struct command_context
*context
;
3724 struct target
*target
;
3726 context
= current_command_context(interp
);
3727 assert(context
!= NULL
);
3729 target
= get_current_target(context
);
3730 if (target
== NULL
) {
3731 LOG_ERROR("array2mem: no current target");
3735 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
3738 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
3739 int argc
, Jim_Obj
*const *argv
)
3747 const char *varname
;
3751 /* argv[1] = name of array to get the data
3752 * argv[2] = desired width
3753 * argv[3] = memory address
3754 * argv[4] = count to write
3757 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3760 varname
= Jim_GetString(argv
[0], &len
);
3761 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3763 e
= Jim_GetLong(interp
, argv
[1], &l
);
3768 e
= Jim_GetLong(interp
, argv
[2], &l
);
3772 e
= Jim_GetLong(interp
, argv
[3], &l
);
3787 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3788 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3789 "Invalid width param, must be 8/16/32", NULL
);
3793 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3794 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3795 "array2mem: zero width read?", NULL
);
3798 if ((addr
+ (len
* width
)) < addr
) {
3799 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3800 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3801 "array2mem: addr + len - wraps to zero?", NULL
);
3804 /* absurd transfer size? */
3806 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3807 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3808 "array2mem: absurd > 64K item request", NULL
);
3813 ((width
== 2) && ((addr
& 1) == 0)) ||
3814 ((width
== 4) && ((addr
& 3) == 0))) {
3818 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3819 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3822 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3833 size_t buffersize
= 4096;
3834 uint8_t *buffer
= malloc(buffersize
);
3839 /* Slurp... in buffer size chunks */
3841 count
= len
; /* in objects.. */
3842 if (count
> (buffersize
/ width
))
3843 count
= (buffersize
/ width
);
3845 v
= 0; /* shut up gcc */
3846 for (i
= 0; i
< count
; i
++, n
++) {
3847 get_int_array_element(interp
, varname
, n
, &v
);
3850 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
3853 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
3856 buffer
[i
] = v
& 0x0ff;
3862 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3863 if (retval
!= ERROR_OK
) {
3865 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3869 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3870 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3878 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3883 /* FIX? should we propagate errors here rather than printing them
3886 void target_handle_event(struct target
*target
, enum target_event e
)
3888 struct target_event_action
*teap
;
3890 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3891 if (teap
->event
== e
) {
3892 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3893 target
->target_number
,
3894 target_name(target
),
3895 target_type_name(target
),
3897 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3898 Jim_GetString(teap
->body
, NULL
));
3899 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
) {
3900 Jim_MakeErrorMessage(teap
->interp
);
3901 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
3908 * Returns true only if the target has a handler for the specified event.
3910 bool target_has_event_action(struct target
*target
, enum target_event event
)
3912 struct target_event_action
*teap
;
3914 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3915 if (teap
->event
== event
)
3921 enum target_cfg_param
{
3924 TCFG_WORK_AREA_VIRT
,
3925 TCFG_WORK_AREA_PHYS
,
3926 TCFG_WORK_AREA_SIZE
,
3927 TCFG_WORK_AREA_BACKUP
,
3931 TCFG_CHAIN_POSITION
,
3936 static Jim_Nvp nvp_config_opts
[] = {
3937 { .name
= "-type", .value
= TCFG_TYPE
},
3938 { .name
= "-event", .value
= TCFG_EVENT
},
3939 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3940 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3941 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3942 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3943 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3944 { .name
= "-variant", .value
= TCFG_VARIANT
},
3945 { .name
= "-coreid", .value
= TCFG_COREID
},
3946 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3947 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
3948 { .name
= "-rtos", .value
= TCFG_RTOS
},
3949 { .name
= NULL
, .value
= -1 }
3952 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
3960 /* parse config or cget options ... */
3961 while (goi
->argc
> 0) {
3962 Jim_SetEmptyResult(goi
->interp
);
3963 /* Jim_GetOpt_Debug(goi); */
3965 if (target
->type
->target_jim_configure
) {
3966 /* target defines a configure function */
3967 /* target gets first dibs on parameters */
3968 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
3977 /* otherwise we 'continue' below */
3979 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
3981 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
3987 if (goi
->isconfigure
) {
3988 Jim_SetResultFormatted(goi
->interp
,
3989 "not settable: %s", n
->name
);
3993 if (goi
->argc
!= 0) {
3994 Jim_WrongNumArgs(goi
->interp
,
3995 goi
->argc
, goi
->argv
,
4000 Jim_SetResultString(goi
->interp
,
4001 target_type_name(target
), -1);
4005 if (goi
->argc
== 0) {
4006 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
4010 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
4012 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
4016 if (goi
->isconfigure
) {
4017 if (goi
->argc
!= 1) {
4018 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
4022 if (goi
->argc
!= 0) {
4023 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
4029 struct target_event_action
*teap
;
4031 teap
= target
->event_action
;
4032 /* replace existing? */
4034 if (teap
->event
== (enum target_event
)n
->value
)
4039 if (goi
->isconfigure
) {
4040 bool replace
= true;
4043 teap
= calloc(1, sizeof(*teap
));
4046 teap
->event
= n
->value
;
4047 teap
->interp
= goi
->interp
;
4048 Jim_GetOpt_Obj(goi
, &o
);
4050 Jim_DecrRefCount(teap
->interp
, teap
->body
);
4051 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
4054 * Tcl/TK - "tk events" have a nice feature.
4055 * See the "BIND" command.
4056 * We should support that here.
4057 * You can specify %X and %Y in the event code.
4058 * The idea is: %T - target name.
4059 * The idea is: %N - target number
4060 * The idea is: %E - event name.
4062 Jim_IncrRefCount(teap
->body
);
4065 /* add to head of event list */
4066 teap
->next
= target
->event_action
;
4067 target
->event_action
= teap
;
4069 Jim_SetEmptyResult(goi
->interp
);
4073 Jim_SetEmptyResult(goi
->interp
);
4075 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
4081 case TCFG_WORK_AREA_VIRT
:
4082 if (goi
->isconfigure
) {
4083 target_free_all_working_areas(target
);
4084 e
= Jim_GetOpt_Wide(goi
, &w
);
4087 target
->working_area_virt
= w
;
4088 target
->working_area_virt_spec
= true;
4093 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
4097 case TCFG_WORK_AREA_PHYS
:
4098 if (goi
->isconfigure
) {
4099 target_free_all_working_areas(target
);
4100 e
= Jim_GetOpt_Wide(goi
, &w
);
4103 target
->working_area_phys
= w
;
4104 target
->working_area_phys_spec
= true;
4109 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
4113 case TCFG_WORK_AREA_SIZE
:
4114 if (goi
->isconfigure
) {
4115 target_free_all_working_areas(target
);
4116 e
= Jim_GetOpt_Wide(goi
, &w
);
4119 target
->working_area_size
= w
;
4124 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4128 case TCFG_WORK_AREA_BACKUP
:
4129 if (goi
->isconfigure
) {
4130 target_free_all_working_areas(target
);
4131 e
= Jim_GetOpt_Wide(goi
, &w
);
4134 /* make this exactly 1 or 0 */
4135 target
->backup_working_area
= (!!w
);
4140 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
4141 /* loop for more e*/
4146 if (goi
->isconfigure
) {
4147 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
4149 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
4152 target
->endianness
= n
->value
;
4157 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4158 if (n
->name
== NULL
) {
4159 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4160 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4162 Jim_SetResultString(goi
->interp
, n
->name
, -1);
4167 if (goi
->isconfigure
) {
4168 if (goi
->argc
< 1) {
4169 Jim_SetResultFormatted(goi
->interp
,
4174 if (target
->variant
)
4175 free((void *)(target
->variant
));
4176 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
4179 target
->variant
= strdup(cp
);
4184 Jim_SetResultString(goi
->interp
, target
->variant
, -1);
4189 if (goi
->isconfigure
) {
4190 e
= Jim_GetOpt_Wide(goi
, &w
);
4193 target
->coreid
= (int32_t)w
;
4198 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4202 case TCFG_CHAIN_POSITION
:
4203 if (goi
->isconfigure
) {
4205 struct jtag_tap
*tap
;
4206 target_free_all_working_areas(target
);
4207 e
= Jim_GetOpt_Obj(goi
, &o_t
);
4210 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
4213 /* make this exactly 1 or 0 */
4219 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
4220 /* loop for more e*/
4223 if (goi
->isconfigure
) {
4224 e
= Jim_GetOpt_Wide(goi
, &w
);
4227 target
->dbgbase
= (uint32_t)w
;
4228 target
->dbgbase_set
= true;
4233 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
4240 int result
= rtos_create(goi
, target
);
4241 if (result
!= JIM_OK
)
4247 } /* while (goi->argc) */
4250 /* done - we return */
4254 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4258 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4259 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4260 int need_args
= 1 + goi
.isconfigure
;
4261 if (goi
.argc
< need_args
) {
4262 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4264 ? "missing: -option VALUE ..."
4265 : "missing: -option ...");
4268 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4269 return target_configure(&goi
, target
);
4272 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4274 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4277 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4279 if (goi
.argc
< 2 || goi
.argc
> 4) {
4280 Jim_SetResultFormatted(goi
.interp
,
4281 "usage: %s [phys] <address> <data> [<count>]", cmd_name
);
4286 fn
= target_write_memory_fast
;
4289 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4291 struct Jim_Obj
*obj
;
4292 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4296 fn
= target_write_phys_memory
;
4300 e
= Jim_GetOpt_Wide(&goi
, &a
);
4305 e
= Jim_GetOpt_Wide(&goi
, &b
);
4310 if (goi
.argc
== 1) {
4311 e
= Jim_GetOpt_Wide(&goi
, &c
);
4316 /* all args must be consumed */
4320 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4322 if (strcasecmp(cmd_name
, "mww") == 0)
4324 else if (strcasecmp(cmd_name
, "mwh") == 0)
4326 else if (strcasecmp(cmd_name
, "mwb") == 0)
4329 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4333 return (target_fill_mem(target
, a
, fn
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4337 * @brief Reads an array of words/halfwords/bytes from target memory starting at specified address.
4339 * Usage: mdw [phys] <address> [<count>] - for 32 bit reads
4340 * mdh [phys] <address> [<count>] - for 16 bit reads
4341 * mdb [phys] <address> [<count>] - for 8 bit reads
4343 * Count defaults to 1.
4345 * Calls target_read_memory or target_read_phys_memory depending on
4346 * the presence of the "phys" argument
4347 * Reads the target memory in blocks of max. 32 bytes, and returns an array of ints formatted
4348 * to int representation in base16.
4349 * Also outputs read data in a human readable form using command_print
4351 * @param phys if present target_read_phys_memory will be used instead of target_read_memory
4352 * @param address address where to start the read. May be specified in decimal or hex using the standard "0x" prefix
4353 * @param count optional count parameter to read an array of values. If not specified, defaults to 1.
4354 * @returns: JIM_ERR on error or JIM_OK on success and sets the result string to an array of ascii formatted numbers
4355 * on success, with [<count>] number of elements.
4357 * In case of little endian target:
4358 * Example1: "mdw 0x00000000" returns "10123456"
4359 * Exmaple2: "mdh 0x00000000 1" returns "3456"
4360 * Example3: "mdb 0x00000000" returns "56"
4361 * Example4: "mdh 0x00000000 2" returns "3456 1012"
4362 * Example5: "mdb 0x00000000 3" returns "56 34 12"
4364 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4366 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4369 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4371 if ((goi
.argc
< 1) || (goi
.argc
> 3)) {
4372 Jim_SetResultFormatted(goi
.interp
,
4373 "usage: %s [phys] <address> [<count>]", cmd_name
);
4377 int (*fn
)(struct target
*target
,
4378 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
4379 fn
= target_read_memory
;
4382 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4384 struct Jim_Obj
*obj
;
4385 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4389 fn
= target_read_phys_memory
;
4392 /* Read address parameter */
4394 e
= Jim_GetOpt_Wide(&goi
, &addr
);
4398 /* If next parameter exists, read it out as the count parameter, if not, set it to 1 (default) */
4400 if (goi
.argc
== 1) {
4401 e
= Jim_GetOpt_Wide(&goi
, &count
);
4407 /* all args must be consumed */
4411 jim_wide dwidth
= 1; /* shut up gcc */
4412 if (strcasecmp(cmd_name
, "mdw") == 0)
4414 else if (strcasecmp(cmd_name
, "mdh") == 0)
4416 else if (strcasecmp(cmd_name
, "mdb") == 0)
4419 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4423 /* convert count to "bytes" */
4424 int bytes
= count
* dwidth
;
4426 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4427 uint8_t target_buf
[32];
4430 y
= (bytes
< 16) ? bytes
: 16; /* y = min(bytes, 16); */
4432 /* Try to read out next block */
4433 e
= fn(target
, addr
, dwidth
, y
/ dwidth
, target_buf
);
4435 if (e
!= ERROR_OK
) {
4436 Jim_SetResultFormatted(interp
, "error reading target @ 0x%08lx", (long)addr
);
4440 command_print_sameline(NULL
, "0x%08x ", (int)(addr
));
4443 for (x
= 0; x
< 16 && x
< y
; x
+= 4) {
4444 z
= target_buffer_get_u32(target
, &(target_buf
[x
]));
4445 command_print_sameline(NULL
, "%08x ", (int)(z
));
4447 for (; (x
< 16) ; x
+= 4)
4448 command_print_sameline(NULL
, " ");
4451 for (x
= 0; x
< 16 && x
< y
; x
+= 2) {
4452 z
= target_buffer_get_u16(target
, &(target_buf
[x
]));
4453 command_print_sameline(NULL
, "%04x ", (int)(z
));
4455 for (; (x
< 16) ; x
+= 2)
4456 command_print_sameline(NULL
, " ");
4460 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4461 z
= target_buffer_get_u8(target
, &(target_buf
[x
]));
4462 command_print_sameline(NULL
, "%02x ", (int)(z
));
4464 for (; (x
< 16) ; x
+= 1)
4465 command_print_sameline(NULL
, " ");
4468 /* ascii-ify the bytes */
4469 for (x
= 0 ; x
< y
; x
++) {
4470 if ((target_buf
[x
] >= 0x20) &&
4471 (target_buf
[x
] <= 0x7e)) {
4475 target_buf
[x
] = '.';
4480 target_buf
[x
] = ' ';
4485 /* print - with a newline */
4486 command_print_sameline(NULL
, "%s\n", target_buf
);
4494 static int jim_target_mem2array(Jim_Interp
*interp
,
4495 int argc
, Jim_Obj
*const *argv
)
4497 struct target
*target
= Jim_CmdPrivData(interp
);
4498 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4501 static int jim_target_array2mem(Jim_Interp
*interp
,
4502 int argc
, Jim_Obj
*const *argv
)
4504 struct target
*target
= Jim_CmdPrivData(interp
);
4505 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4508 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4510 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
4514 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4517 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4520 struct target
*target
= Jim_CmdPrivData(interp
);
4521 if (!target
->tap
->enabled
)
4522 return jim_target_tap_disabled(interp
);
4524 int e
= target
->type
->examine(target
);
4530 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4533 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4536 struct target
*target
= Jim_CmdPrivData(interp
);
4538 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
4544 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4547 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4550 struct target
*target
= Jim_CmdPrivData(interp
);
4551 if (!target
->tap
->enabled
)
4552 return jim_target_tap_disabled(interp
);
4555 if (!(target_was_examined(target
)))
4556 e
= ERROR_TARGET_NOT_EXAMINED
;
4558 e
= target
->type
->poll(target
);
4564 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4567 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4569 if (goi
.argc
!= 2) {
4570 Jim_WrongNumArgs(interp
, 0, argv
,
4571 "([tT]|[fF]|assert|deassert) BOOL");
4576 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4578 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4581 /* the halt or not param */
4583 e
= Jim_GetOpt_Wide(&goi
, &a
);
4587 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4588 if (!target
->tap
->enabled
)
4589 return jim_target_tap_disabled(interp
);
4590 if (!(target_was_examined(target
))) {
4591 LOG_ERROR("Target not examined yet");
4592 return ERROR_TARGET_NOT_EXAMINED
;
4594 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
4595 Jim_SetResultFormatted(interp
,
4596 "No target-specific reset for %s",
4597 target_name(target
));
4600 /* determine if we should halt or not. */
4601 target
->reset_halt
= !!a
;
4602 /* When this happens - all workareas are invalid. */
4603 target_free_all_working_areas_restore(target
, 0);
4606 if (n
->value
== NVP_ASSERT
)
4607 e
= target
->type
->assert_reset(target
);
4609 e
= target
->type
->deassert_reset(target
);
4610 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4613 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4616 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4619 struct target
*target
= Jim_CmdPrivData(interp
);
4620 if (!target
->tap
->enabled
)
4621 return jim_target_tap_disabled(interp
);
4622 int e
= target
->type
->halt(target
);
4623 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4626 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4629 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4631 /* params: <name> statename timeoutmsecs */
4632 if (goi
.argc
!= 2) {
4633 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4634 Jim_SetResultFormatted(goi
.interp
,
4635 "%s <state_name> <timeout_in_msec>", cmd_name
);
4640 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4642 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
4646 e
= Jim_GetOpt_Wide(&goi
, &a
);
4649 struct target
*target
= Jim_CmdPrivData(interp
);
4650 if (!target
->tap
->enabled
)
4651 return jim_target_tap_disabled(interp
);
4653 e
= target_wait_state(target
, n
->value
, a
);
4654 if (e
!= ERROR_OK
) {
4655 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
4656 Jim_SetResultFormatted(goi
.interp
,
4657 "target: %s wait %s fails (%#s) %s",
4658 target_name(target
), n
->name
,
4659 eObj
, target_strerror_safe(e
));
4660 Jim_FreeNewObj(interp
, eObj
);
4665 /* List for human, Events defined for this target.
4666 * scripts/programs should use 'name cget -event NAME'
4668 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4670 struct command_context
*cmd_ctx
= current_command_context(interp
);
4671 assert(cmd_ctx
!= NULL
);
4673 struct target
*target
= Jim_CmdPrivData(interp
);
4674 struct target_event_action
*teap
= target
->event_action
;
4675 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4676 target
->target_number
,
4677 target_name(target
));
4678 command_print(cmd_ctx
, "%-25s | Body", "Event");
4679 command_print(cmd_ctx
, "------------------------- | "
4680 "----------------------------------------");
4682 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
4683 command_print(cmd_ctx
, "%-25s | %s",
4684 opt
->name
, Jim_GetString(teap
->body
, NULL
));
4687 command_print(cmd_ctx
, "***END***");
4690 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4693 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4696 struct target
*target
= Jim_CmdPrivData(interp
);
4697 Jim_SetResultString(interp
, target_state_name(target
), -1);
4700 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4703 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4704 if (goi
.argc
!= 1) {
4705 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4706 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
4710 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4712 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4715 struct target
*target
= Jim_CmdPrivData(interp
);
4716 target_handle_event(target
, n
->value
);
4720 static const struct command_registration target_instance_command_handlers
[] = {
4722 .name
= "configure",
4723 .mode
= COMMAND_CONFIG
,
4724 .jim_handler
= jim_target_configure
,
4725 .help
= "configure a new target for use",
4726 .usage
= "[target_attribute ...]",
4730 .mode
= COMMAND_ANY
,
4731 .jim_handler
= jim_target_configure
,
4732 .help
= "returns the specified target attribute",
4733 .usage
= "target_attribute",
4737 .mode
= COMMAND_EXEC
,
4738 .jim_handler
= jim_target_mw
,
4739 .help
= "Write 32-bit word(s) to target memory",
4740 .usage
= "address data [count]",
4744 .mode
= COMMAND_EXEC
,
4745 .jim_handler
= jim_target_mw
,
4746 .help
= "Write 16-bit half-word(s) to target memory",
4747 .usage
= "address data [count]",
4751 .mode
= COMMAND_EXEC
,
4752 .jim_handler
= jim_target_mw
,
4753 .help
= "Write byte(s) to target memory",
4754 .usage
= "address data [count]",
4758 .mode
= COMMAND_EXEC
,
4759 .jim_handler
= jim_target_md
,
4760 .help
= "Display target memory as 32-bit words",
4761 .usage
= "address [count]",
4765 .mode
= COMMAND_EXEC
,
4766 .jim_handler
= jim_target_md
,
4767 .help
= "Display target memory as 16-bit half-words",
4768 .usage
= "address [count]",
4772 .mode
= COMMAND_EXEC
,
4773 .jim_handler
= jim_target_md
,
4774 .help
= "Display target memory as 8-bit bytes",
4775 .usage
= "address [count]",
4778 .name
= "array2mem",
4779 .mode
= COMMAND_EXEC
,
4780 .jim_handler
= jim_target_array2mem
,
4781 .help
= "Writes Tcl array of 8/16/32 bit numbers "
4783 .usage
= "arrayname bitwidth address count",
4786 .name
= "mem2array",
4787 .mode
= COMMAND_EXEC
,
4788 .jim_handler
= jim_target_mem2array
,
4789 .help
= "Loads Tcl array of 8/16/32 bit numbers "
4790 "from target memory",
4791 .usage
= "arrayname bitwidth address count",
4794 .name
= "eventlist",
4795 .mode
= COMMAND_EXEC
,
4796 .jim_handler
= jim_target_event_list
,
4797 .help
= "displays a table of events defined for this target",
4801 .mode
= COMMAND_EXEC
,
4802 .jim_handler
= jim_target_current_state
,
4803 .help
= "displays the current state of this target",
4806 .name
= "arp_examine",
4807 .mode
= COMMAND_EXEC
,
4808 .jim_handler
= jim_target_examine
,
4809 .help
= "used internally for reset processing",
4812 .name
= "arp_halt_gdb",
4813 .mode
= COMMAND_EXEC
,
4814 .jim_handler
= jim_target_halt_gdb
,
4815 .help
= "used internally for reset processing to halt GDB",
4819 .mode
= COMMAND_EXEC
,
4820 .jim_handler
= jim_target_poll
,
4821 .help
= "used internally for reset processing",
4824 .name
= "arp_reset",
4825 .mode
= COMMAND_EXEC
,
4826 .jim_handler
= jim_target_reset
,
4827 .help
= "used internally for reset processing",
4831 .mode
= COMMAND_EXEC
,
4832 .jim_handler
= jim_target_halt
,
4833 .help
= "used internally for reset processing",
4836 .name
= "arp_waitstate",
4837 .mode
= COMMAND_EXEC
,
4838 .jim_handler
= jim_target_wait_state
,
4839 .help
= "used internally for reset processing",
4842 .name
= "invoke-event",
4843 .mode
= COMMAND_EXEC
,
4844 .jim_handler
= jim_target_invoke_event
,
4845 .help
= "invoke handler for specified event",
4846 .usage
= "event_name",
4848 COMMAND_REGISTRATION_DONE
4851 static int target_create(Jim_GetOptInfo
*goi
)
4859 struct target
*target
;
4860 struct command_context
*cmd_ctx
;
4862 cmd_ctx
= current_command_context(goi
->interp
);
4863 assert(cmd_ctx
!= NULL
);
4865 if (goi
->argc
< 3) {
4866 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4871 Jim_GetOpt_Obj(goi
, &new_cmd
);
4872 /* does this command exist? */
4873 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4875 cp
= Jim_GetString(new_cmd
, NULL
);
4876 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
4881 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4885 /* now does target type exist */
4886 for (x
= 0 ; target_types
[x
] ; x
++) {
4887 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4892 /* check for deprecated name */
4893 if (target_types
[x
]->deprecated_name
) {
4894 if (0 == strcmp(cp
, target_types
[x
]->deprecated_name
)) {
4896 LOG_WARNING("target name is deprecated use: \'%s\'", target_types
[x
]->name
);
4901 if (target_types
[x
] == NULL
) {
4902 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4903 for (x
= 0 ; target_types
[x
] ; x
++) {
4904 if (target_types
[x
+ 1]) {
4905 Jim_AppendStrings(goi
->interp
,
4906 Jim_GetResult(goi
->interp
),
4907 target_types
[x
]->name
,
4910 Jim_AppendStrings(goi
->interp
,
4911 Jim_GetResult(goi
->interp
),
4913 target_types
[x
]->name
, NULL
);
4920 target
= calloc(1, sizeof(struct target
));
4921 /* set target number */
4922 target
->target_number
= new_target_number();
4924 /* allocate memory for each unique target type */
4925 target
->type
= (struct target_type
*)calloc(1, sizeof(struct target_type
));
4927 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
4929 /* will be set by "-endian" */
4930 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4932 /* default to first core, override with -coreid */
4935 target
->working_area
= 0x0;
4936 target
->working_area_size
= 0x0;
4937 target
->working_areas
= NULL
;
4938 target
->backup_working_area
= 0;
4940 target
->state
= TARGET_UNKNOWN
;
4941 target
->debug_reason
= DBG_REASON_UNDEFINED
;
4942 target
->reg_cache
= NULL
;
4943 target
->breakpoints
= NULL
;
4944 target
->watchpoints
= NULL
;
4945 target
->next
= NULL
;
4946 target
->arch_info
= NULL
;
4948 target
->display
= 1;
4950 target
->halt_issued
= false;
4952 /* initialize trace information */
4953 target
->trace_info
= malloc(sizeof(struct trace
));
4954 target
->trace_info
->num_trace_points
= 0;
4955 target
->trace_info
->trace_points_size
= 0;
4956 target
->trace_info
->trace_points
= NULL
;
4957 target
->trace_info
->trace_history_size
= 0;
4958 target
->trace_info
->trace_history
= NULL
;
4959 target
->trace_info
->trace_history_pos
= 0;
4960 target
->trace_info
->trace_history_overflowed
= 0;
4962 target
->dbgmsg
= NULL
;
4963 target
->dbg_msg_enabled
= 0;
4965 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4967 target
->rtos
= NULL
;
4968 target
->rtos_auto_detect
= false;
4970 /* Do the rest as "configure" options */
4971 goi
->isconfigure
= 1;
4972 e
= target_configure(goi
, target
);
4974 if (target
->tap
== NULL
) {
4975 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
4985 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
4986 /* default endian to little if not specified */
4987 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4990 /* incase variant is not set */
4991 if (!target
->variant
)
4992 target
->variant
= strdup("");
4994 cp
= Jim_GetString(new_cmd
, NULL
);
4995 target
->cmd_name
= strdup(cp
);
4997 /* create the target specific commands */
4998 if (target
->type
->commands
) {
4999 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
5001 LOG_ERROR("unable to register '%s' commands", cp
);
5003 if (target
->type
->target_create
)
5004 (*(target
->type
->target_create
))(target
, goi
->interp
);
5006 /* append to end of list */
5008 struct target
**tpp
;
5009 tpp
= &(all_targets
);
5011 tpp
= &((*tpp
)->next
);
5015 /* now - create the new target name command */
5016 const const struct command_registration target_subcommands
[] = {
5018 .chain
= target_instance_command_handlers
,
5021 .chain
= target
->type
->commands
,
5023 COMMAND_REGISTRATION_DONE
5025 const const struct command_registration target_commands
[] = {
5028 .mode
= COMMAND_ANY
,
5029 .help
= "target command group",
5031 .chain
= target_subcommands
,
5033 COMMAND_REGISTRATION_DONE
5035 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
5039 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
5041 command_set_handler_data(c
, target
);
5043 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
5046 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5049 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5052 struct command_context
*cmd_ctx
= current_command_context(interp
);
5053 assert(cmd_ctx
!= NULL
);
5055 Jim_SetResultString(interp
, target_name(get_current_target(cmd_ctx
)), -1);
5059 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5062 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5065 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5066 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
5067 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5068 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
5073 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5076 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5079 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5080 struct target
*target
= all_targets
;
5082 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5083 Jim_NewStringObj(interp
, target_name(target
), -1));
5084 target
= target
->next
;
5089 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5092 const char *targetname
;
5094 struct target
*target
= (struct target
*) NULL
;
5095 struct target_list
*head
, *curr
, *new;
5096 curr
= (struct target_list
*) NULL
;
5097 head
= (struct target_list
*) NULL
;
5100 LOG_DEBUG("%d", argc
);
5101 /* argv[1] = target to associate in smp
5102 * argv[2] = target to assoicate in smp
5106 for (i
= 1; i
< argc
; i
++) {
5108 targetname
= Jim_GetString(argv
[i
], &len
);
5109 target
= get_target(targetname
);
5110 LOG_DEBUG("%s ", targetname
);
5112 new = malloc(sizeof(struct target_list
));
5113 new->target
= target
;
5114 new->next
= (struct target_list
*)NULL
;
5115 if (head
== (struct target_list
*)NULL
) {
5124 /* now parse the list of cpu and put the target in smp mode*/
5127 while (curr
!= (struct target_list
*)NULL
) {
5128 target
= curr
->target
;
5130 target
->head
= head
;
5134 if (target
&& target
->rtos
)
5135 retval
= rtos_smp_init(head
->target
);
5141 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5144 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5146 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5147 "<name> <target_type> [<target_options> ...]");
5150 return target_create(&goi
);
5153 static int jim_target_number(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5156 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5158 /* It's OK to remove this mechanism sometime after August 2010 or so */
5159 LOG_WARNING("don't use numbers as target identifiers; use names");
5160 if (goi
.argc
!= 1) {
5161 Jim_SetResultFormatted(goi
.interp
, "usage: target number <number>");
5165 int e
= Jim_GetOpt_Wide(&goi
, &w
);
5169 struct target
*target
;
5170 for (target
= all_targets
; NULL
!= target
; target
= target
->next
) {
5171 if (target
->target_number
!= w
)
5174 Jim_SetResultString(goi
.interp
, target_name(target
), -1);
5178 Jim_Obj
*wObj
= Jim_NewIntObj(goi
.interp
, w
);
5179 Jim_SetResultFormatted(goi
.interp
,
5180 "Target: number %#s does not exist", wObj
);
5181 Jim_FreeNewObj(interp
, wObj
);
5186 static int jim_target_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5189 Jim_WrongNumArgs(interp
, 1, argv
, "<no parameters>");
5193 struct target
*target
= all_targets
;
5194 while (NULL
!= target
) {
5195 target
= target
->next
;
5198 Jim_SetResult(interp
, Jim_NewIntObj(interp
, count
));
5202 static const struct command_registration target_subcommand_handlers
[] = {
5205 .mode
= COMMAND_CONFIG
,
5206 .handler
= handle_target_init_command
,
5207 .help
= "initialize targets",
5211 /* REVISIT this should be COMMAND_CONFIG ... */
5212 .mode
= COMMAND_ANY
,
5213 .jim_handler
= jim_target_create
,
5214 .usage
= "name type '-chain-position' name [options ...]",
5215 .help
= "Creates and selects a new target",
5219 .mode
= COMMAND_ANY
,
5220 .jim_handler
= jim_target_current
,
5221 .help
= "Returns the currently selected target",
5225 .mode
= COMMAND_ANY
,
5226 .jim_handler
= jim_target_types
,
5227 .help
= "Returns the available target types as "
5228 "a list of strings",
5232 .mode
= COMMAND_ANY
,
5233 .jim_handler
= jim_target_names
,
5234 .help
= "Returns the names of all targets as a list of strings",
5238 .mode
= COMMAND_ANY
,
5239 .jim_handler
= jim_target_number
,
5241 .help
= "Returns the name of the numbered target "
5246 .mode
= COMMAND_ANY
,
5247 .jim_handler
= jim_target_count
,
5248 .help
= "Returns the number of targets as an integer "
5253 .mode
= COMMAND_ANY
,
5254 .jim_handler
= jim_target_smp
,
5255 .usage
= "targetname1 targetname2 ...",
5256 .help
= "gather several target in a smp list"
5259 COMMAND_REGISTRATION_DONE
5269 static int fastload_num
;
5270 static struct FastLoad
*fastload
;
5272 static void free_fastload(void)
5274 if (fastload
!= NULL
) {
5276 for (i
= 0; i
< fastload_num
; i
++) {
5277 if (fastload
[i
].data
)
5278 free(fastload
[i
].data
);
5285 COMMAND_HANDLER(handle_fast_load_image_command
)
5289 uint32_t image_size
;
5290 uint32_t min_address
= 0;
5291 uint32_t max_address
= 0xffffffff;
5296 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5297 &image
, &min_address
, &max_address
);
5298 if (ERROR_OK
!= retval
)
5301 struct duration bench
;
5302 duration_start(&bench
);
5304 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5305 if (retval
!= ERROR_OK
)
5310 fastload_num
= image
.num_sections
;
5311 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5312 if (fastload
== NULL
) {
5313 command_print(CMD_CTX
, "out of memory");
5314 image_close(&image
);
5317 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5318 for (i
= 0; i
< image
.num_sections
; i
++) {
5319 buffer
= malloc(image
.sections
[i
].size
);
5320 if (buffer
== NULL
) {
5321 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5322 (int)(image
.sections
[i
].size
));
5323 retval
= ERROR_FAIL
;
5327 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5328 if (retval
!= ERROR_OK
) {
5333 uint32_t offset
= 0;
5334 uint32_t length
= buf_cnt
;
5336 /* DANGER!!! beware of unsigned comparision here!!! */
5338 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5339 (image
.sections
[i
].base_address
< max_address
)) {
5340 if (image
.sections
[i
].base_address
< min_address
) {
5341 /* clip addresses below */
5342 offset
+= min_address
-image
.sections
[i
].base_address
;
5346 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5347 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5349 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5350 fastload
[i
].data
= malloc(length
);
5351 if (fastload
[i
].data
== NULL
) {
5353 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5355 retval
= ERROR_FAIL
;
5358 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5359 fastload
[i
].length
= length
;
5361 image_size
+= length
;
5362 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
5363 (unsigned int)length
,
5364 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5370 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
5371 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
5372 "in %fs (%0.3f KiB/s)", image_size
,
5373 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5375 command_print(CMD_CTX
,
5376 "WARNING: image has not been loaded to target!"
5377 "You can issue a 'fast_load' to finish loading.");
5380 image_close(&image
);
5382 if (retval
!= ERROR_OK
)
5388 COMMAND_HANDLER(handle_fast_load_command
)
5391 return ERROR_COMMAND_SYNTAX_ERROR
;
5392 if (fastload
== NULL
) {
5393 LOG_ERROR("No image in memory");
5397 int ms
= timeval_ms();
5399 int retval
= ERROR_OK
;
5400 for (i
= 0; i
< fastload_num
; i
++) {
5401 struct target
*target
= get_current_target(CMD_CTX
);
5402 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
5403 (unsigned int)(fastload
[i
].address
),
5404 (unsigned int)(fastload
[i
].length
));
5405 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5406 if (retval
!= ERROR_OK
)
5408 size
+= fastload
[i
].length
;
5410 if (retval
== ERROR_OK
) {
5411 int after
= timeval_ms();
5412 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5417 static const struct command_registration target_command_handlers
[] = {
5420 .handler
= handle_targets_command
,
5421 .mode
= COMMAND_ANY
,
5422 .help
= "change current default target (one parameter) "
5423 "or prints table of all targets (no parameters)",
5424 .usage
= "[target]",
5428 .mode
= COMMAND_CONFIG
,
5429 .help
= "configure target",
5431 .chain
= target_subcommand_handlers
,
5433 COMMAND_REGISTRATION_DONE
5436 int target_register_commands(struct command_context
*cmd_ctx
)
5438 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5441 static bool target_reset_nag
= true;
5443 bool get_target_reset_nag(void)
5445 return target_reset_nag
;
5448 COMMAND_HANDLER(handle_target_reset_nag
)
5450 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5451 &target_reset_nag
, "Nag after each reset about options to improve "
5455 COMMAND_HANDLER(handle_ps_command
)
5457 struct target
*target
= get_current_target(CMD_CTX
);
5459 if (target
->state
!= TARGET_HALTED
) {
5460 LOG_INFO("target not halted !!");
5464 if ((target
->rtos
) && (target
->rtos
->type
)
5465 && (target
->rtos
->type
->ps_command
)) {
5466 display
= target
->rtos
->type
->ps_command(target
);
5467 command_print(CMD_CTX
, "%s", display
);
5472 return ERROR_TARGET_FAILURE
;
5476 static const struct command_registration target_exec_command_handlers
[] = {
5478 .name
= "fast_load_image",
5479 .handler
= handle_fast_load_image_command
,
5480 .mode
= COMMAND_ANY
,
5481 .help
= "Load image into server memory for later use by "
5482 "fast_load; primarily for profiling",
5483 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5484 "[min_address [max_length]]",
5487 .name
= "fast_load",
5488 .handler
= handle_fast_load_command
,
5489 .mode
= COMMAND_EXEC
,
5490 .help
= "loads active fast load image to current target "
5491 "- mainly for profiling purposes",
5496 .handler
= handle_profile_command
,
5497 .mode
= COMMAND_EXEC
,
5498 .usage
= "seconds filename",
5499 .help
= "profiling samples the CPU PC",
5501 /** @todo don't register virt2phys() unless target supports it */
5503 .name
= "virt2phys",
5504 .handler
= handle_virt2phys_command
,
5505 .mode
= COMMAND_ANY
,
5506 .help
= "translate a virtual address into a physical address",
5507 .usage
= "virtual_address",
5511 .handler
= handle_reg_command
,
5512 .mode
= COMMAND_EXEC
,
5513 .help
= "display or set a register; with no arguments, "
5514 "displays all registers and their values",
5515 .usage
= "[(register_name|register_number) [value]]",
5519 .handler
= handle_poll_command
,
5520 .mode
= COMMAND_EXEC
,
5521 .help
= "poll target state; or reconfigure background polling",
5522 .usage
= "['on'|'off']",
5525 .name
= "wait_halt",
5526 .handler
= handle_wait_halt_command
,
5527 .mode
= COMMAND_EXEC
,
5528 .help
= "wait up to the specified number of milliseconds "
5529 "(default 5) for a previously requested halt",
5530 .usage
= "[milliseconds]",
5534 .handler
= handle_halt_command
,
5535 .mode
= COMMAND_EXEC
,
5536 .help
= "request target to halt, then wait up to the specified"
5537 "number of milliseconds (default 5) for it to complete",
5538 .usage
= "[milliseconds]",
5542 .handler
= handle_resume_command
,
5543 .mode
= COMMAND_EXEC
,
5544 .help
= "resume target execution from current PC or address",
5545 .usage
= "[address]",
5549 .handler
= handle_reset_command
,
5550 .mode
= COMMAND_EXEC
,
5551 .usage
= "[run|halt|init]",
5552 .help
= "Reset all targets into the specified mode."
5553 "Default reset mode is run, if not given.",
5556 .name
= "soft_reset_halt",
5557 .handler
= handle_soft_reset_halt_command
,
5558 .mode
= COMMAND_EXEC
,
5560 .help
= "halt the target and do a soft reset",
5564 .handler
= handle_step_command
,
5565 .mode
= COMMAND_EXEC
,
5566 .help
= "step one instruction from current PC or address",
5567 .usage
= "[address]",
5571 .handler
= handle_md_command
,
5572 .mode
= COMMAND_EXEC
,
5573 .help
= "display memory words",
5574 .usage
= "['phys'] address [count]",
5578 .handler
= handle_md_command
,
5579 .mode
= COMMAND_EXEC
,
5580 .help
= "display memory half-words",
5581 .usage
= "['phys'] address [count]",
5585 .handler
= handle_md_command
,
5586 .mode
= COMMAND_EXEC
,
5587 .help
= "display memory bytes",
5588 .usage
= "['phys'] address [count]",
5592 .handler
= handle_mw_command
,
5593 .mode
= COMMAND_EXEC
,
5594 .help
= "write memory word",
5595 .usage
= "['phys'] address value [count]",
5599 .handler
= handle_mw_command
,
5600 .mode
= COMMAND_EXEC
,
5601 .help
= "write memory half-word",
5602 .usage
= "['phys'] address value [count]",
5606 .handler
= handle_mw_command
,
5607 .mode
= COMMAND_EXEC
,
5608 .help
= "write memory byte",
5609 .usage
= "['phys'] address value [count]",
5613 .handler
= handle_bp_command
,
5614 .mode
= COMMAND_EXEC
,
5615 .help
= "list or set hardware or software breakpoint",
5616 .usage
= "<address> [<asid>]<length> ['hw'|'hw_ctx']",
5620 .handler
= handle_rbp_command
,
5621 .mode
= COMMAND_EXEC
,
5622 .help
= "remove breakpoint",
5627 .handler
= handle_wp_command
,
5628 .mode
= COMMAND_EXEC
,
5629 .help
= "list (no params) or create watchpoints",
5630 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
5634 .handler
= handle_rwp_command
,
5635 .mode
= COMMAND_EXEC
,
5636 .help
= "remove watchpoint",
5640 .name
= "load_image",
5641 .handler
= handle_load_image_command
,
5642 .mode
= COMMAND_EXEC
,
5643 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5644 "[min_address] [max_length]",
5647 .name
= "dump_image",
5648 .handler
= handle_dump_image_command
,
5649 .mode
= COMMAND_EXEC
,
5650 .usage
= "filename address size",
5653 .name
= "verify_image",
5654 .handler
= handle_verify_image_command
,
5655 .mode
= COMMAND_EXEC
,
5656 .usage
= "filename [offset [type]]",
5659 .name
= "test_image",
5660 .handler
= handle_test_image_command
,
5661 .mode
= COMMAND_EXEC
,
5662 .usage
= "filename [offset [type]]",
5665 .name
= "mem2array",
5666 .mode
= COMMAND_EXEC
,
5667 .jim_handler
= jim_mem2array
,
5668 .help
= "read 8/16/32 bit memory and return as a TCL array "
5669 "for script processing",
5670 .usage
= "arrayname bitwidth address count",
5673 .name
= "array2mem",
5674 .mode
= COMMAND_EXEC
,
5675 .jim_handler
= jim_array2mem
,
5676 .help
= "convert a TCL array to memory locations "
5677 "and write the 8/16/32 bit values",
5678 .usage
= "arrayname bitwidth address count",
5681 .name
= "reset_nag",
5682 .handler
= handle_target_reset_nag
,
5683 .mode
= COMMAND_ANY
,
5684 .help
= "Nag after each reset about options that could have been "
5685 "enabled to improve performance. ",
5686 .usage
= "['enable'|'disable']",
5690 .handler
= handle_ps_command
,
5691 .mode
= COMMAND_EXEC
,
5692 .help
= "list all tasks ",
5696 COMMAND_REGISTRATION_DONE
5698 static int target_register_user_commands(struct command_context
*cmd_ctx
)
5700 int retval
= ERROR_OK
;
5701 retval
= target_request_register_commands(cmd_ctx
);
5702 if (retval
!= ERROR_OK
)
5705 retval
= trace_register_commands(cmd_ctx
);
5706 if (retval
!= ERROR_OK
)
5710 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);