1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
17 * Copyright (C) 2011 by Broadcom Corporation *
18 * Evan Hunter - ehunter@broadcom.com *
20 * Copyright (C) ST-Ericsson SA 2011 *
21 * michel.jaouen@stericsson.com : smp minimum support *
23 * Copyright (C) 2011 Andreas Fritiofson *
24 * andreas.fritiofson@gmail.com *
26 * This program is free software; you can redistribute it and/or modify *
27 * it under the terms of the GNU General Public License as published by *
28 * the Free Software Foundation; either version 2 of the License, or *
29 * (at your option) any later version. *
31 * This program is distributed in the hope that it will be useful, *
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
34 * GNU General Public License for more details. *
36 * You should have received a copy of the GNU General Public License *
37 * along with this program; if not, write to the *
38 * Free Software Foundation, Inc., *
39 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
40 ***************************************************************************/
46 #include <helper/time_support.h>
47 #include <jtag/jtag.h>
48 #include <flash/nor/core.h>
51 #include "target_type.h"
52 #include "target_request.h"
53 #include "breakpoints.h"
57 #include "rtos/rtos.h"
59 static int target_read_buffer_default(struct target
*target
, uint32_t address
,
60 uint32_t size
, uint8_t *buffer
);
61 static int target_write_buffer_default(struct target
*target
, uint32_t address
,
62 uint32_t size
, const uint8_t *buffer
);
63 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
64 int argc
, Jim_Obj
* const *argv
);
65 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
66 int argc
, Jim_Obj
* const *argv
);
67 static int target_register_user_commands(struct command_context
*cmd_ctx
);
70 extern struct target_type arm7tdmi_target
;
71 extern struct target_type arm720t_target
;
72 extern struct target_type arm9tdmi_target
;
73 extern struct target_type arm920t_target
;
74 extern struct target_type arm966e_target
;
75 extern struct target_type arm946e_target
;
76 extern struct target_type arm926ejs_target
;
77 extern struct target_type fa526_target
;
78 extern struct target_type feroceon_target
;
79 extern struct target_type dragonite_target
;
80 extern struct target_type xscale_target
;
81 extern struct target_type cortexm3_target
;
82 extern struct target_type cortexa8_target
;
83 extern struct target_type arm11_target
;
84 extern struct target_type mips_m4k_target
;
85 extern struct target_type avr_target
;
86 extern struct target_type dsp563xx_target
;
87 extern struct target_type dsp5680xx_target
;
88 extern struct target_type testee_target
;
89 extern struct target_type avr32_ap7k_target
;
90 extern struct target_type stm32_stlink_target
;
92 static struct target_type
*target_types
[] = {
113 &stm32_stlink_target
,
117 struct target
*all_targets
;
118 static struct target_event_callback
*target_event_callbacks
;
119 static struct target_timer_callback
*target_timer_callbacks
;
120 static const int polling_interval
= 100;
122 static const Jim_Nvp nvp_assert
[] = {
123 { .name
= "assert", NVP_ASSERT
},
124 { .name
= "deassert", NVP_DEASSERT
},
125 { .name
= "T", NVP_ASSERT
},
126 { .name
= "F", NVP_DEASSERT
},
127 { .name
= "t", NVP_ASSERT
},
128 { .name
= "f", NVP_DEASSERT
},
129 { .name
= NULL
, .value
= -1 }
132 static const Jim_Nvp nvp_error_target
[] = {
133 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
134 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
135 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
136 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
137 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
138 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
139 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
140 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
141 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
142 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
143 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
144 { .value
= -1, .name
= NULL
}
147 static const char *target_strerror_safe(int err
)
151 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
158 static const Jim_Nvp nvp_target_event
[] = {
160 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
161 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
162 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
163 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
164 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
166 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
167 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
169 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
170 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
171 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
172 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
173 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
174 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
175 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
176 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
177 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
178 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
179 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
180 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
182 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
183 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
185 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
186 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
188 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
189 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
191 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
192 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
194 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
195 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
197 { .name
= NULL
, .value
= -1 }
200 static const Jim_Nvp nvp_target_state
[] = {
201 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
202 { .name
= "running", .value
= TARGET_RUNNING
},
203 { .name
= "halted", .value
= TARGET_HALTED
},
204 { .name
= "reset", .value
= TARGET_RESET
},
205 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
206 { .name
= NULL
, .value
= -1 },
209 static const Jim_Nvp nvp_target_debug_reason
[] = {
210 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
211 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
212 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
213 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
214 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
215 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
216 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
217 { .name
= NULL
, .value
= -1 },
220 static const Jim_Nvp nvp_target_endian
[] = {
221 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
222 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
223 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
224 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
225 { .name
= NULL
, .value
= -1 },
228 static const Jim_Nvp nvp_reset_modes
[] = {
229 { .name
= "unknown", .value
= RESET_UNKNOWN
},
230 { .name
= "run" , .value
= RESET_RUN
},
231 { .name
= "halt" , .value
= RESET_HALT
},
232 { .name
= "init" , .value
= RESET_INIT
},
233 { .name
= NULL
, .value
= -1 },
236 const char *debug_reason_name(struct target
*t
)
240 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
241 t
->debug_reason
)->name
;
243 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
244 cp
= "(*BUG*unknown*BUG*)";
249 const char *target_state_name(struct target
*t
)
252 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
254 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
255 cp
= "(*BUG*unknown*BUG*)";
260 /* determine the number of the new target */
261 static int new_target_number(void)
266 /* number is 0 based */
270 if (x
< t
->target_number
)
271 x
= t
->target_number
;
277 /* read a uint32_t from a buffer in target memory endianness */
278 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
280 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
281 return le_to_h_u32(buffer
);
283 return be_to_h_u32(buffer
);
286 /* read a uint24_t from a buffer in target memory endianness */
287 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
289 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
290 return le_to_h_u24(buffer
);
292 return be_to_h_u24(buffer
);
295 /* read a uint16_t from a buffer in target memory endianness */
296 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
298 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
299 return le_to_h_u16(buffer
);
301 return be_to_h_u16(buffer
);
304 /* read a uint8_t from a buffer in target memory endianness */
305 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
307 return *buffer
& 0x0ff;
310 /* write a uint32_t to a buffer in target memory endianness */
311 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
313 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
314 h_u32_to_le(buffer
, value
);
316 h_u32_to_be(buffer
, value
);
319 /* write a uint24_t to a buffer in target memory endianness */
320 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
322 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
323 h_u24_to_le(buffer
, value
);
325 h_u24_to_be(buffer
, value
);
328 /* write a uint16_t to a buffer in target memory endianness */
329 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
331 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
332 h_u16_to_le(buffer
, value
);
334 h_u16_to_be(buffer
, value
);
337 /* write a uint8_t to a buffer in target memory endianness */
338 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
343 /* write a uint32_t array to a buffer in target memory endianness */
344 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
347 for (i
= 0; i
< count
; i
++)
348 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
351 /* write a uint16_t array to a buffer in target memory endianness */
352 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
355 for (i
= 0; i
< count
; i
++)
356 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
359 /* write a uint32_t array to a buffer in target memory endianness */
360 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, uint32_t *srcbuf
)
363 for (i
= 0; i
< count
; i
++)
364 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
367 /* write a uint16_t array to a buffer in target memory endianness */
368 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, uint16_t *srcbuf
)
371 for (i
= 0; i
< count
; i
++)
372 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
375 /* return a pointer to a configured target; id is name or number */
376 struct target
*get_target(const char *id
)
378 struct target
*target
;
380 /* try as tcltarget name */
381 for (target
= all_targets
; target
; target
= target
->next
) {
382 if (target
->cmd_name
== NULL
)
384 if (strcmp(id
, target
->cmd_name
) == 0)
388 /* It's OK to remove this fallback sometime after August 2010 or so */
390 /* no match, try as number */
392 if (parse_uint(id
, &num
) != ERROR_OK
)
395 for (target
= all_targets
; target
; target
= target
->next
) {
396 if (target
->target_number
== (int)num
) {
397 LOG_WARNING("use '%s' as target identifier, not '%u'",
398 target
->cmd_name
, num
);
406 /* returns a pointer to the n-th configured target */
407 static struct target
*get_target_by_num(int num
)
409 struct target
*target
= all_targets
;
412 if (target
->target_number
== num
)
414 target
= target
->next
;
420 struct target
*get_current_target(struct command_context
*cmd_ctx
)
422 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
424 if (target
== NULL
) {
425 LOG_ERROR("BUG: current_target out of bounds");
432 int target_poll(struct target
*target
)
436 /* We can't poll until after examine */
437 if (!target_was_examined(target
)) {
438 /* Fail silently lest we pollute the log */
442 retval
= target
->type
->poll(target
);
443 if (retval
!= ERROR_OK
)
446 if (target
->halt_issued
) {
447 if (target
->state
== TARGET_HALTED
)
448 target
->halt_issued
= false;
450 long long t
= timeval_ms() - target
->halt_issued_time
;
452 target
->halt_issued
= false;
453 LOG_INFO("Halt timed out, wake up GDB.");
454 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
462 int target_halt(struct target
*target
)
465 /* We can't poll until after examine */
466 if (!target_was_examined(target
)) {
467 LOG_ERROR("Target not examined yet");
471 retval
= target
->type
->halt(target
);
472 if (retval
!= ERROR_OK
)
475 target
->halt_issued
= true;
476 target
->halt_issued_time
= timeval_ms();
482 * Make the target (re)start executing using its saved execution
483 * context (possibly with some modifications).
485 * @param target Which target should start executing.
486 * @param current True to use the target's saved program counter instead
487 * of the address parameter
488 * @param address Optionally used as the program counter.
489 * @param handle_breakpoints True iff breakpoints at the resumption PC
490 * should be skipped. (For example, maybe execution was stopped by
491 * such a breakpoint, in which case it would be counterprodutive to
493 * @param debug_execution False if all working areas allocated by OpenOCD
494 * should be released and/or restored to their original contents.
495 * (This would for example be true to run some downloaded "helper"
496 * algorithm code, which resides in one such working buffer and uses
497 * another for data storage.)
499 * @todo Resolve the ambiguity about what the "debug_execution" flag
500 * signifies. For example, Target implementations don't agree on how
501 * it relates to invalidation of the register cache, or to whether
502 * breakpoints and watchpoints should be enabled. (It would seem wrong
503 * to enable breakpoints when running downloaded "helper" algorithms
504 * (debug_execution true), since the breakpoints would be set to match
505 * target firmware being debugged, not the helper algorithm.... and
506 * enabling them could cause such helpers to malfunction (for example,
507 * by overwriting data with a breakpoint instruction. On the other
508 * hand the infrastructure for running such helpers might use this
509 * procedure but rely on hardware breakpoint to detect termination.)
511 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
515 /* We can't poll until after examine */
516 if (!target_was_examined(target
)) {
517 LOG_ERROR("Target not examined yet");
521 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
523 /* note that resume *must* be asynchronous. The CPU can halt before
524 * we poll. The CPU can even halt at the current PC as a result of
525 * a software breakpoint being inserted by (a bug?) the application.
527 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
528 if (retval
!= ERROR_OK
)
531 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
536 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
541 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
542 if (n
->name
== NULL
) {
543 LOG_ERROR("invalid reset mode");
547 /* disable polling during reset to make reset event scripts
548 * more predictable, i.e. dr/irscan & pathmove in events will
549 * not have JTAG operations injected into the middle of a sequence.
551 bool save_poll
= jtag_poll_get_enabled();
553 jtag_poll_set_enabled(false);
555 sprintf(buf
, "ocd_process_reset %s", n
->name
);
556 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
558 jtag_poll_set_enabled(save_poll
);
560 if (retval
!= JIM_OK
) {
561 Jim_MakeErrorMessage(cmd_ctx
->interp
);
562 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
566 /* We want any events to be processed before the prompt */
567 retval
= target_call_timer_callbacks_now();
569 struct target
*target
;
570 for (target
= all_targets
; target
; target
= target
->next
)
571 target
->type
->check_reset(target
);
576 static int identity_virt2phys(struct target
*target
,
577 uint32_t virtual, uint32_t *physical
)
583 static int no_mmu(struct target
*target
, int *enabled
)
589 static int default_examine(struct target
*target
)
591 target_set_examined(target
);
595 /* no check by default */
596 static int default_check_reset(struct target
*target
)
601 int target_examine_one(struct target
*target
)
603 return target
->type
->examine(target
);
606 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
608 struct target
*target
= priv
;
610 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
613 jtag_unregister_event_callback(jtag_enable_callback
, target
);
615 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
617 int retval
= target_examine_one(target
);
618 if (retval
!= ERROR_OK
)
621 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
626 /* Targets that correctly implement init + examine, i.e.
627 * no communication with target during init:
631 int target_examine(void)
633 int retval
= ERROR_OK
;
634 struct target
*target
;
636 for (target
= all_targets
; target
; target
= target
->next
) {
637 /* defer examination, but don't skip it */
638 if (!target
->tap
->enabled
) {
639 jtag_register_event_callback(jtag_enable_callback
,
644 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
646 retval
= target_examine_one(target
);
647 if (retval
!= ERROR_OK
)
650 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
655 const char *target_type_name(struct target
*target
)
657 return target
->type
->name
;
660 static int target_write_memory_imp(struct target
*target
, uint32_t address
,
661 uint32_t size
, uint32_t count
, const uint8_t *buffer
)
663 if (!target_was_examined(target
)) {
664 LOG_ERROR("Target not examined yet");
667 return target
->type
->write_memory_imp(target
, address
, size
, count
, buffer
);
670 static int target_read_memory_imp(struct target
*target
, uint32_t address
,
671 uint32_t size
, uint32_t count
, uint8_t *buffer
)
673 if (!target_was_examined(target
)) {
674 LOG_ERROR("Target not examined yet");
677 return target
->type
->read_memory_imp(target
, address
, size
, count
, buffer
);
680 static int target_soft_reset_halt_imp(struct target
*target
)
682 if (!target_was_examined(target
)) {
683 LOG_ERROR("Target not examined yet");
686 if (!target
->type
->soft_reset_halt_imp
) {
687 LOG_ERROR("Target %s does not support soft_reset_halt",
688 target_name(target
));
691 return target
->type
->soft_reset_halt_imp(target
);
695 * Downloads a target-specific native code algorithm to the target,
696 * and executes it. * Note that some targets may need to set up, enable,
697 * and tear down a breakpoint (hard or * soft) to detect algorithm
698 * termination, while others may support lower overhead schemes where
699 * soft breakpoints embedded in the algorithm automatically terminate the
702 * @param target used to run the algorithm
703 * @param arch_info target-specific description of the algorithm.
705 int target_run_algorithm(struct target
*target
,
706 int num_mem_params
, struct mem_param
*mem_params
,
707 int num_reg_params
, struct reg_param
*reg_param
,
708 uint32_t entry_point
, uint32_t exit_point
,
709 int timeout_ms
, void *arch_info
)
711 int retval
= ERROR_FAIL
;
713 if (!target_was_examined(target
)) {
714 LOG_ERROR("Target not examined yet");
717 if (!target
->type
->run_algorithm
) {
718 LOG_ERROR("Target type '%s' does not support %s",
719 target_type_name(target
), __func__
);
723 target
->running_alg
= true;
724 retval
= target
->type
->run_algorithm(target
,
725 num_mem_params
, mem_params
,
726 num_reg_params
, reg_param
,
727 entry_point
, exit_point
, timeout_ms
, arch_info
);
728 target
->running_alg
= false;
735 * Downloads a target-specific native code algorithm to the target,
736 * executes and leaves it running.
738 * @param target used to run the algorithm
739 * @param arch_info target-specific description of the algorithm.
741 int target_start_algorithm(struct target
*target
,
742 int num_mem_params
, struct mem_param
*mem_params
,
743 int num_reg_params
, struct reg_param
*reg_params
,
744 uint32_t entry_point
, uint32_t exit_point
,
747 int retval
= ERROR_FAIL
;
749 if (!target_was_examined(target
)) {
750 LOG_ERROR("Target not examined yet");
753 if (!target
->type
->start_algorithm
) {
754 LOG_ERROR("Target type '%s' does not support %s",
755 target_type_name(target
), __func__
);
758 if (target
->running_alg
) {
759 LOG_ERROR("Target is already running an algorithm");
763 target
->running_alg
= true;
764 retval
= target
->type
->start_algorithm(target
,
765 num_mem_params
, mem_params
,
766 num_reg_params
, reg_params
,
767 entry_point
, exit_point
, arch_info
);
774 * Waits for an algorithm started with target_start_algorithm() to complete.
776 * @param target used to run the algorithm
777 * @param arch_info target-specific description of the algorithm.
779 int target_wait_algorithm(struct target
*target
,
780 int num_mem_params
, struct mem_param
*mem_params
,
781 int num_reg_params
, struct reg_param
*reg_params
,
782 uint32_t exit_point
, int timeout_ms
,
785 int retval
= ERROR_FAIL
;
787 if (!target
->type
->wait_algorithm
) {
788 LOG_ERROR("Target type '%s' does not support %s",
789 target_type_name(target
), __func__
);
792 if (!target
->running_alg
) {
793 LOG_ERROR("Target is not running an algorithm");
797 retval
= target
->type
->wait_algorithm(target
,
798 num_mem_params
, mem_params
,
799 num_reg_params
, reg_params
,
800 exit_point
, timeout_ms
, arch_info
);
801 if (retval
!= ERROR_TARGET_TIMEOUT
)
802 target
->running_alg
= false;
809 * Executes a target-specific native code algorithm in the target.
810 * It differs from target_run_algorithm in that the algorithm is asynchronous.
811 * Because of this it requires an compliant algorithm:
812 * see contrib/loaders/flash/stm32f1x.S for example.
814 * @param target used to run the algorithm
817 int target_run_flash_async_algorithm(struct target
*target
,
818 uint8_t *buffer
, uint32_t count
, int block_size
,
819 int num_mem_params
, struct mem_param
*mem_params
,
820 int num_reg_params
, struct reg_param
*reg_params
,
821 uint32_t buffer_start
, uint32_t buffer_size
,
822 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
826 /* Set up working area. First word is write pointer, second word is read pointer,
827 * rest is fifo data area. */
828 uint32_t wp_addr
= buffer_start
;
829 uint32_t rp_addr
= buffer_start
+ 4;
830 uint32_t fifo_start_addr
= buffer_start
+ 8;
831 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
833 uint32_t wp
= fifo_start_addr
;
834 uint32_t rp
= fifo_start_addr
;
836 /* validate block_size is 2^n */
837 assert(!block_size
|| !(block_size
& (block_size
- 1)));
839 retval
= target_write_u32(target
, wp_addr
, wp
);
840 if (retval
!= ERROR_OK
)
842 retval
= target_write_u32(target
, rp_addr
, rp
);
843 if (retval
!= ERROR_OK
)
846 /* Start up algorithm on target and let it idle while writing the first chunk */
847 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
848 num_reg_params
, reg_params
,
853 if (retval
!= ERROR_OK
) {
854 LOG_ERROR("error starting target flash write algorithm");
860 retval
= target_read_u32(target
, rp_addr
, &rp
);
861 if (retval
!= ERROR_OK
) {
862 LOG_ERROR("failed to get read pointer");
866 LOG_DEBUG("count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
, count
, wp
, rp
);
869 LOG_ERROR("flash write algorithm aborted by target");
870 retval
= ERROR_FLASH_OPERATION_FAILED
;
874 if ((rp
& (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
875 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
879 /* Count the number of bytes available in the fifo without
880 * crossing the wrap around. Make sure to not fill it completely,
881 * because that would make wp == rp and that's the empty condition. */
882 uint32_t thisrun_bytes
;
884 thisrun_bytes
= rp
- wp
- block_size
;
885 else if (rp
> fifo_start_addr
)
886 thisrun_bytes
= fifo_end_addr
- wp
;
888 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
890 if (thisrun_bytes
== 0) {
891 /* Throttle polling a bit if transfer is (much) faster than flash
892 * programming. The exact delay shouldn't matter as long as it's
893 * less than buffer size / flash speed. This is very unlikely to
894 * run when using high latency connections such as USB. */
899 /* Limit to the amount of data we actually want to write */
900 if (thisrun_bytes
> count
* block_size
)
901 thisrun_bytes
= count
* block_size
;
903 /* Write data to fifo */
904 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
905 if (retval
!= ERROR_OK
)
908 /* Update counters and wrap write pointer */
909 buffer
+= thisrun_bytes
;
910 count
-= thisrun_bytes
/ block_size
;
912 if (wp
>= fifo_end_addr
)
913 wp
= fifo_start_addr
;
915 /* Store updated write pointer to target */
916 retval
= target_write_u32(target
, wp_addr
, wp
);
917 if (retval
!= ERROR_OK
)
921 if (retval
!= ERROR_OK
) {
922 /* abort flash write algorithm on target */
923 target_write_u32(target
, wp_addr
, 0);
926 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
927 num_reg_params
, reg_params
,
932 if (retval2
!= ERROR_OK
) {
933 LOG_ERROR("error waiting for target flash write algorithm");
940 int target_read_memory(struct target
*target
,
941 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
943 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
946 static int target_read_phys_memory(struct target
*target
,
947 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
949 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
952 int target_write_memory(struct target
*target
,
953 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
955 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
958 static int target_write_phys_memory(struct target
*target
,
959 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
961 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
964 int target_bulk_write_memory(struct target
*target
,
965 uint32_t address
, uint32_t count
, const uint8_t *buffer
)
967 return target
->type
->bulk_write_memory(target
, address
, count
, buffer
);
970 int target_add_breakpoint(struct target
*target
,
971 struct breakpoint
*breakpoint
)
973 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
974 LOG_WARNING("target %s is not halted", target
->cmd_name
);
975 return ERROR_TARGET_NOT_HALTED
;
977 return target
->type
->add_breakpoint(target
, breakpoint
);
980 int target_add_context_breakpoint(struct target
*target
,
981 struct breakpoint
*breakpoint
)
983 if (target
->state
!= TARGET_HALTED
) {
984 LOG_WARNING("target %s is not halted", target
->cmd_name
);
985 return ERROR_TARGET_NOT_HALTED
;
987 return target
->type
->add_context_breakpoint(target
, breakpoint
);
990 int target_add_hybrid_breakpoint(struct target
*target
,
991 struct breakpoint
*breakpoint
)
993 if (target
->state
!= TARGET_HALTED
) {
994 LOG_WARNING("target %s is not halted", target
->cmd_name
);
995 return ERROR_TARGET_NOT_HALTED
;
997 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1000 int target_remove_breakpoint(struct target
*target
,
1001 struct breakpoint
*breakpoint
)
1003 return target
->type
->remove_breakpoint(target
, breakpoint
);
1006 int target_add_watchpoint(struct target
*target
,
1007 struct watchpoint
*watchpoint
)
1009 if (target
->state
!= TARGET_HALTED
) {
1010 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1011 return ERROR_TARGET_NOT_HALTED
;
1013 return target
->type
->add_watchpoint(target
, watchpoint
);
1015 int target_remove_watchpoint(struct target
*target
,
1016 struct watchpoint
*watchpoint
)
1018 return target
->type
->remove_watchpoint(target
, watchpoint
);
1021 int target_get_gdb_reg_list(struct target
*target
,
1022 struct reg
**reg_list
[], int *reg_list_size
)
1024 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
);
1026 int target_step(struct target
*target
,
1027 int current
, uint32_t address
, int handle_breakpoints
)
1029 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1033 * Reset the @c examined flag for the given target.
1034 * Pure paranoia -- targets are zeroed on allocation.
1036 static void target_reset_examined(struct target
*target
)
1038 target
->examined
= false;
1041 static int err_read_phys_memory(struct target
*target
, uint32_t address
,
1042 uint32_t size
, uint32_t count
, uint8_t *buffer
)
1044 LOG_ERROR("Not implemented: %s", __func__
);
1048 static int err_write_phys_memory(struct target
*target
, uint32_t address
,
1049 uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1051 LOG_ERROR("Not implemented: %s", __func__
);
1055 static int handle_target(void *priv
);
1057 static int target_init_one(struct command_context
*cmd_ctx
,
1058 struct target
*target
)
1060 target_reset_examined(target
);
1062 struct target_type
*type
= target
->type
;
1063 if (type
->examine
== NULL
)
1064 type
->examine
= default_examine
;
1066 if (type
->check_reset
== NULL
)
1067 type
->check_reset
= default_check_reset
;
1069 assert(type
->init_target
!= NULL
);
1071 int retval
= type
->init_target(cmd_ctx
, target
);
1072 if (ERROR_OK
!= retval
) {
1073 LOG_ERROR("target '%s' init failed", target_name(target
));
1078 * @todo get rid of those *memory_imp() methods, now that all
1079 * callers are using target_*_memory() accessors ... and make
1080 * sure the "physical" paths handle the same issues.
1082 /* a non-invasive way(in terms of patches) to add some code that
1083 * runs before the type->write/read_memory implementation
1085 type
->write_memory_imp
= target
->type
->write_memory
;
1086 type
->write_memory
= target_write_memory_imp
;
1088 type
->read_memory_imp
= target
->type
->read_memory
;
1089 type
->read_memory
= target_read_memory_imp
;
1091 type
->soft_reset_halt_imp
= target
->type
->soft_reset_halt
;
1092 type
->soft_reset_halt
= target_soft_reset_halt_imp
;
1094 /* Sanity-check MMU support ... stub in what we must, to help
1095 * implement it in stages, but warn if we need to do so.
1098 if (type
->write_phys_memory
== NULL
) {
1099 LOG_ERROR("type '%s' is missing write_phys_memory",
1101 type
->write_phys_memory
= err_write_phys_memory
;
1103 if (type
->read_phys_memory
== NULL
) {
1104 LOG_ERROR("type '%s' is missing read_phys_memory",
1106 type
->read_phys_memory
= err_read_phys_memory
;
1108 if (type
->virt2phys
== NULL
) {
1109 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1110 type
->virt2phys
= identity_virt2phys
;
1113 /* Make sure no-MMU targets all behave the same: make no
1114 * distinction between physical and virtual addresses, and
1115 * ensure that virt2phys() is always an identity mapping.
1117 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1118 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1121 type
->write_phys_memory
= type
->write_memory
;
1122 type
->read_phys_memory
= type
->read_memory
;
1123 type
->virt2phys
= identity_virt2phys
;
1126 if (target
->type
->read_buffer
== NULL
)
1127 target
->type
->read_buffer
= target_read_buffer_default
;
1129 if (target
->type
->write_buffer
== NULL
)
1130 target
->type
->write_buffer
= target_write_buffer_default
;
1135 static int target_init(struct command_context
*cmd_ctx
)
1137 struct target
*target
;
1140 for (target
= all_targets
; target
; target
= target
->next
) {
1141 retval
= target_init_one(cmd_ctx
, target
);
1142 if (ERROR_OK
!= retval
)
1149 retval
= target_register_user_commands(cmd_ctx
);
1150 if (ERROR_OK
!= retval
)
1153 retval
= target_register_timer_callback(&handle_target
,
1154 polling_interval
, 1, cmd_ctx
->interp
);
1155 if (ERROR_OK
!= retval
)
1161 COMMAND_HANDLER(handle_target_init_command
)
1166 return ERROR_COMMAND_SYNTAX_ERROR
;
1168 static bool target_initialized
;
1169 if (target_initialized
) {
1170 LOG_INFO("'target init' has already been called");
1173 target_initialized
= true;
1175 retval
= command_run_line(CMD_CTX
, "init_targets");
1176 if (ERROR_OK
!= retval
)
1179 retval
= command_run_line(CMD_CTX
, "init_board");
1180 if (ERROR_OK
!= retval
)
1183 LOG_DEBUG("Initializing targets...");
1184 return target_init(CMD_CTX
);
1187 int target_register_event_callback(int (*callback
)(struct target
*target
,
1188 enum target_event event
, void *priv
), void *priv
)
1190 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1192 if (callback
== NULL
)
1193 return ERROR_COMMAND_SYNTAX_ERROR
;
1196 while ((*callbacks_p
)->next
)
1197 callbacks_p
= &((*callbacks_p
)->next
);
1198 callbacks_p
= &((*callbacks_p
)->next
);
1201 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1202 (*callbacks_p
)->callback
= callback
;
1203 (*callbacks_p
)->priv
= priv
;
1204 (*callbacks_p
)->next
= NULL
;
1209 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1211 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1214 if (callback
== NULL
)
1215 return ERROR_COMMAND_SYNTAX_ERROR
;
1218 while ((*callbacks_p
)->next
)
1219 callbacks_p
= &((*callbacks_p
)->next
);
1220 callbacks_p
= &((*callbacks_p
)->next
);
1223 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1224 (*callbacks_p
)->callback
= callback
;
1225 (*callbacks_p
)->periodic
= periodic
;
1226 (*callbacks_p
)->time_ms
= time_ms
;
1228 gettimeofday(&now
, NULL
);
1229 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
1230 time_ms
-= (time_ms
% 1000);
1231 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
1232 if ((*callbacks_p
)->when
.tv_usec
> 1000000) {
1233 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
1234 (*callbacks_p
)->when
.tv_sec
+= 1;
1237 (*callbacks_p
)->priv
= priv
;
1238 (*callbacks_p
)->next
= NULL
;
1243 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1244 enum target_event event
, void *priv
), void *priv
)
1246 struct target_event_callback
**p
= &target_event_callbacks
;
1247 struct target_event_callback
*c
= target_event_callbacks
;
1249 if (callback
== NULL
)
1250 return ERROR_COMMAND_SYNTAX_ERROR
;
1253 struct target_event_callback
*next
= c
->next
;
1254 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1266 static int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1268 struct target_timer_callback
**p
= &target_timer_callbacks
;
1269 struct target_timer_callback
*c
= target_timer_callbacks
;
1271 if (callback
== NULL
)
1272 return ERROR_COMMAND_SYNTAX_ERROR
;
1275 struct target_timer_callback
*next
= c
->next
;
1276 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1288 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1290 struct target_event_callback
*callback
= target_event_callbacks
;
1291 struct target_event_callback
*next_callback
;
1293 if (event
== TARGET_EVENT_HALTED
) {
1294 /* execute early halted first */
1295 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1298 LOG_DEBUG("target event %i (%s)", event
,
1299 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1301 target_handle_event(target
, event
);
1304 next_callback
= callback
->next
;
1305 callback
->callback(target
, event
, callback
->priv
);
1306 callback
= next_callback
;
1312 static int target_timer_callback_periodic_restart(
1313 struct target_timer_callback
*cb
, struct timeval
*now
)
1315 int time_ms
= cb
->time_ms
;
1316 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1317 time_ms
-= (time_ms
% 1000);
1318 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1319 if (cb
->when
.tv_usec
> 1000000) {
1320 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1321 cb
->when
.tv_sec
+= 1;
1326 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1327 struct timeval
*now
)
1329 cb
->callback(cb
->priv
);
1332 return target_timer_callback_periodic_restart(cb
, now
);
1334 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1337 static int target_call_timer_callbacks_check_time(int checktime
)
1342 gettimeofday(&now
, NULL
);
1344 struct target_timer_callback
*callback
= target_timer_callbacks
;
1346 /* cleaning up may unregister and free this callback */
1347 struct target_timer_callback
*next_callback
= callback
->next
;
1349 bool call_it
= callback
->callback
&&
1350 ((!checktime
&& callback
->periodic
) ||
1351 now
.tv_sec
> callback
->when
.tv_sec
||
1352 (now
.tv_sec
== callback
->when
.tv_sec
&&
1353 now
.tv_usec
>= callback
->when
.tv_usec
));
1356 int retval
= target_call_timer_callback(callback
, &now
);
1357 if (retval
!= ERROR_OK
)
1361 callback
= next_callback
;
1367 int target_call_timer_callbacks(void)
1369 return target_call_timer_callbacks_check_time(1);
1372 /* invoke periodic callbacks immediately */
1373 int target_call_timer_callbacks_now(void)
1375 return target_call_timer_callbacks_check_time(0);
1378 /* Prints the working area layout for debug purposes */
1379 static void print_wa_layout(struct target
*target
)
1381 struct working_area
*c
= target
->working_areas
;
1384 LOG_DEBUG("%c%c 0x%08"PRIx32
"-0x%08"PRIx32
" (%"PRIu32
" bytes)",
1385 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1386 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1391 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1392 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1394 assert(area
->free
); /* Shouldn't split an allocated area */
1395 assert(size
<= area
->size
); /* Caller should guarantee this */
1397 /* Split only if not already the right size */
1398 if (size
< area
->size
) {
1399 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1404 new_wa
->next
= area
->next
;
1405 new_wa
->size
= area
->size
- size
;
1406 new_wa
->address
= area
->address
+ size
;
1407 new_wa
->backup
= NULL
;
1408 new_wa
->user
= NULL
;
1409 new_wa
->free
= true;
1411 area
->next
= new_wa
;
1414 /* If backup memory was allocated to this area, it has the wrong size
1415 * now so free it and it will be reallocated if/when needed */
1418 area
->backup
= NULL
;
1423 /* Merge all adjacent free areas into one */
1424 static void target_merge_working_areas(struct target
*target
)
1426 struct working_area
*c
= target
->working_areas
;
1428 while (c
&& c
->next
) {
1429 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1431 /* Find two adjacent free areas */
1432 if (c
->free
&& c
->next
->free
) {
1433 /* Merge the last into the first */
1434 c
->size
+= c
->next
->size
;
1436 /* Remove the last */
1437 struct working_area
*to_be_freed
= c
->next
;
1438 c
->next
= c
->next
->next
;
1439 if (to_be_freed
->backup
)
1440 free(to_be_freed
->backup
);
1443 /* If backup memory was allocated to the remaining area, it's has
1444 * the wrong size now */
1455 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1457 /* Reevaluate working area address based on MMU state*/
1458 if (target
->working_areas
== NULL
) {
1462 retval
= target
->type
->mmu(target
, &enabled
);
1463 if (retval
!= ERROR_OK
)
1467 if (target
->working_area_phys_spec
) {
1468 LOG_DEBUG("MMU disabled, using physical "
1469 "address for working memory 0x%08"PRIx32
,
1470 target
->working_area_phys
);
1471 target
->working_area
= target
->working_area_phys
;
1473 LOG_ERROR("No working memory available. "
1474 "Specify -work-area-phys to target.");
1475 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1478 if (target
->working_area_virt_spec
) {
1479 LOG_DEBUG("MMU enabled, using virtual "
1480 "address for working memory 0x%08"PRIx32
,
1481 target
->working_area_virt
);
1482 target
->working_area
= target
->working_area_virt
;
1484 LOG_ERROR("No working memory available. "
1485 "Specify -work-area-virt to target.");
1486 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1490 /* Set up initial working area on first call */
1491 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1493 new_wa
->next
= NULL
;
1494 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1495 new_wa
->address
= target
->working_area
;
1496 new_wa
->backup
= NULL
;
1497 new_wa
->user
= NULL
;
1498 new_wa
->free
= true;
1501 target
->working_areas
= new_wa
;
1504 /* only allocate multiples of 4 byte */
1506 size
= (size
+ 3) & (~3UL);
1508 struct working_area
*c
= target
->working_areas
;
1510 /* Find the first large enough working area */
1512 if (c
->free
&& c
->size
>= size
)
1518 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1520 /* Split the working area into the requested size */
1521 target_split_working_area(c
, size
);
1523 LOG_DEBUG("allocated new working area of %"PRIu32
" bytes at address 0x%08"PRIx32
, size
, c
->address
);
1525 if (target
->backup_working_area
) {
1526 if (c
->backup
== NULL
) {
1527 c
->backup
= malloc(c
->size
);
1528 if (c
->backup
== NULL
)
1532 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1533 if (retval
!= ERROR_OK
)
1537 /* mark as used, and return the new (reused) area */
1544 print_wa_layout(target
);
1549 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1553 retval
= target_alloc_working_area_try(target
, size
, area
);
1554 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1555 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1560 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1562 int retval
= ERROR_OK
;
1564 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1565 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1566 if (retval
!= ERROR_OK
)
1567 LOG_ERROR("failed to restore %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1568 area
->size
, area
->address
);
1574 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1575 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1577 int retval
= ERROR_OK
;
1583 retval
= target_restore_working_area(target
, area
);
1584 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1585 if (retval
!= ERROR_OK
)
1591 LOG_DEBUG("freed %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1592 area
->size
, area
->address
);
1594 /* mark user pointer invalid */
1595 /* TODO: Is this really safe? It points to some previous caller's memory.
1596 * How could we know that the area pointer is still in that place and not
1597 * some other vital data? What's the purpose of this, anyway? */
1601 target_merge_working_areas(target
);
1603 print_wa_layout(target
);
1608 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1610 return target_free_working_area_restore(target
, area
, 1);
1613 /* free resources and restore memory, if restoring memory fails,
1614 * free up resources anyway
1616 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1618 struct working_area
*c
= target
->working_areas
;
1620 LOG_DEBUG("freeing all working areas");
1622 /* Loop through all areas, restoring the allocated ones and marking them as free */
1626 target_restore_working_area(target
, c
);
1628 *c
->user
= NULL
; /* Same as above */
1634 /* Run a merge pass to combine all areas into one */
1635 target_merge_working_areas(target
);
1637 print_wa_layout(target
);
1640 void target_free_all_working_areas(struct target
*target
)
1642 target_free_all_working_areas_restore(target
, 1);
1645 /* Find the largest number of bytes that can be allocated */
1646 uint32_t target_get_working_area_avail(struct target
*target
)
1648 struct working_area
*c
= target
->working_areas
;
1649 uint32_t max_size
= 0;
1652 return target
->working_area_size
;
1655 if (c
->free
&& max_size
< c
->size
)
1664 int target_arch_state(struct target
*target
)
1667 if (target
== NULL
) {
1668 LOG_USER("No target has been configured");
1672 LOG_USER("target state: %s", target_state_name(target
));
1674 if (target
->state
!= TARGET_HALTED
)
1677 retval
= target
->type
->arch_state(target
);
1681 /* Single aligned words are guaranteed to use 16 or 32 bit access
1682 * mode respectively, otherwise data is handled as quickly as
1685 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
1687 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1688 (int)size
, (unsigned)address
);
1690 if (!target_was_examined(target
)) {
1691 LOG_ERROR("Target not examined yet");
1698 if ((address
+ size
- 1) < address
) {
1699 /* GDB can request this when e.g. PC is 0xfffffffc*/
1700 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1706 return target
->type
->write_buffer(target
, address
, size
, buffer
);
1709 static int target_write_buffer_default(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
1711 int retval
= ERROR_OK
;
1713 if (((address
% 2) == 0) && (size
== 2))
1714 return target_write_memory(target
, address
, 2, 1, buffer
);
1716 /* handle unaligned head bytes */
1718 uint32_t unaligned
= 4 - (address
% 4);
1720 if (unaligned
> size
)
1723 retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
);
1724 if (retval
!= ERROR_OK
)
1727 buffer
+= unaligned
;
1728 address
+= unaligned
;
1732 /* handle aligned words */
1734 int aligned
= size
- (size
% 4);
1736 /* use bulk writes above a certain limit. This may have to be changed */
1737 if (aligned
> 128) {
1738 retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
);
1739 if (retval
!= ERROR_OK
)
1742 retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
);
1743 if (retval
!= ERROR_OK
)
1752 /* handle tail writes of less than 4 bytes */
1754 retval
= target_write_memory(target
, address
, 1, size
, buffer
);
1755 if (retval
!= ERROR_OK
)
1762 /* Single aligned words are guaranteed to use 16 or 32 bit access
1763 * mode respectively, otherwise data is handled as quickly as
1766 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1768 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1769 (int)size
, (unsigned)address
);
1771 if (!target_was_examined(target
)) {
1772 LOG_ERROR("Target not examined yet");
1779 if ((address
+ size
- 1) < address
) {
1780 /* GDB can request this when e.g. PC is 0xfffffffc*/
1781 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1787 return target
->type
->read_buffer(target
, address
, size
, buffer
);
1790 static int target_read_buffer_default(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1792 int retval
= ERROR_OK
;
1794 if (((address
% 2) == 0) && (size
== 2))
1795 return target_read_memory(target
, address
, 2, 1, buffer
);
1797 /* handle unaligned head bytes */
1799 uint32_t unaligned
= 4 - (address
% 4);
1801 if (unaligned
> size
)
1804 retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
);
1805 if (retval
!= ERROR_OK
)
1808 buffer
+= unaligned
;
1809 address
+= unaligned
;
1813 /* handle aligned words */
1815 int aligned
= size
- (size
% 4);
1817 retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
);
1818 if (retval
!= ERROR_OK
)
1826 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1828 int aligned
= size
- (size
% 2);
1829 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1830 if (retval
!= ERROR_OK
)
1837 /* handle tail writes of less than 4 bytes */
1839 retval
= target_read_memory(target
, address
, 1, size
, buffer
);
1840 if (retval
!= ERROR_OK
)
1847 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1852 uint32_t checksum
= 0;
1853 if (!target_was_examined(target
)) {
1854 LOG_ERROR("Target not examined yet");
1858 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
1859 if (retval
!= ERROR_OK
) {
1860 buffer
= malloc(size
);
1861 if (buffer
== NULL
) {
1862 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1863 return ERROR_COMMAND_SYNTAX_ERROR
;
1865 retval
= target_read_buffer(target
, address
, size
, buffer
);
1866 if (retval
!= ERROR_OK
) {
1871 /* convert to target endianness */
1872 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
1873 uint32_t target_data
;
1874 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1875 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1878 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1887 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1890 if (!target_was_examined(target
)) {
1891 LOG_ERROR("Target not examined yet");
1895 if (target
->type
->blank_check_memory
== 0)
1896 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1898 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1903 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1905 uint8_t value_buf
[4];
1906 if (!target_was_examined(target
)) {
1907 LOG_ERROR("Target not examined yet");
1911 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1913 if (retval
== ERROR_OK
) {
1914 *value
= target_buffer_get_u32(target
, value_buf
);
1915 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1920 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1927 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
1929 uint8_t value_buf
[2];
1930 if (!target_was_examined(target
)) {
1931 LOG_ERROR("Target not examined yet");
1935 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1937 if (retval
== ERROR_OK
) {
1938 *value
= target_buffer_get_u16(target
, value_buf
);
1939 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1944 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1951 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
1953 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1954 if (!target_was_examined(target
)) {
1955 LOG_ERROR("Target not examined yet");
1959 if (retval
== ERROR_OK
) {
1960 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1965 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1972 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
1975 uint8_t value_buf
[4];
1976 if (!target_was_examined(target
)) {
1977 LOG_ERROR("Target not examined yet");
1981 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1985 target_buffer_set_u32(target
, value_buf
, value
);
1986 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
1987 if (retval
!= ERROR_OK
)
1988 LOG_DEBUG("failed: %i", retval
);
1993 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
1996 uint8_t value_buf
[2];
1997 if (!target_was_examined(target
)) {
1998 LOG_ERROR("Target not examined yet");
2002 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
2006 target_buffer_set_u16(target
, value_buf
, value
);
2007 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2008 if (retval
!= ERROR_OK
)
2009 LOG_DEBUG("failed: %i", retval
);
2014 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
2017 if (!target_was_examined(target
)) {
2018 LOG_ERROR("Target not examined yet");
2022 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
2025 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2026 if (retval
!= ERROR_OK
)
2027 LOG_DEBUG("failed: %i", retval
);
2032 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2034 struct target
*target
= get_target(name
);
2035 if (target
== NULL
) {
2036 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2039 if (!target
->tap
->enabled
) {
2040 LOG_USER("Target: TAP %s is disabled, "
2041 "can't be the current target\n",
2042 target
->tap
->dotted_name
);
2046 cmd_ctx
->current_target
= target
->target_number
;
2051 COMMAND_HANDLER(handle_targets_command
)
2053 int retval
= ERROR_OK
;
2054 if (CMD_ARGC
== 1) {
2055 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2056 if (retval
== ERROR_OK
) {
2062 struct target
*target
= all_targets
;
2063 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2064 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2069 if (target
->tap
->enabled
)
2070 state
= target_state_name(target
);
2072 state
= "tap-disabled";
2074 if (CMD_CTX
->current_target
== target
->target_number
)
2077 /* keep columns lined up to match the headers above */
2078 command_print(CMD_CTX
,
2079 "%2d%c %-18s %-10s %-6s %-18s %s",
2080 target
->target_number
,
2082 target_name(target
),
2083 target_type_name(target
),
2084 Jim_Nvp_value2name_simple(nvp_target_endian
,
2085 target
->endianness
)->name
,
2086 target
->tap
->dotted_name
,
2088 target
= target
->next
;
2094 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2096 static int powerDropout
;
2097 static int srstAsserted
;
2099 static int runPowerRestore
;
2100 static int runPowerDropout
;
2101 static int runSrstAsserted
;
2102 static int runSrstDeasserted
;
2104 static int sense_handler(void)
2106 static int prevSrstAsserted
;
2107 static int prevPowerdropout
;
2109 int retval
= jtag_power_dropout(&powerDropout
);
2110 if (retval
!= ERROR_OK
)
2114 powerRestored
= prevPowerdropout
&& !powerDropout
;
2116 runPowerRestore
= 1;
2118 long long current
= timeval_ms();
2119 static long long lastPower
;
2120 int waitMore
= lastPower
+ 2000 > current
;
2121 if (powerDropout
&& !waitMore
) {
2122 runPowerDropout
= 1;
2123 lastPower
= current
;
2126 retval
= jtag_srst_asserted(&srstAsserted
);
2127 if (retval
!= ERROR_OK
)
2131 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2133 static long long lastSrst
;
2134 waitMore
= lastSrst
+ 2000 > current
;
2135 if (srstDeasserted
&& !waitMore
) {
2136 runSrstDeasserted
= 1;
2140 if (!prevSrstAsserted
&& srstAsserted
)
2141 runSrstAsserted
= 1;
2143 prevSrstAsserted
= srstAsserted
;
2144 prevPowerdropout
= powerDropout
;
2146 if (srstDeasserted
|| powerRestored
) {
2147 /* Other than logging the event we can't do anything here.
2148 * Issuing a reset is a particularly bad idea as we might
2149 * be inside a reset already.
2156 static int backoff_times
;
2157 static int backoff_count
;
2159 /* process target state changes */
2160 static int handle_target(void *priv
)
2162 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2163 int retval
= ERROR_OK
;
2165 if (!is_jtag_poll_safe()) {
2166 /* polling is disabled currently */
2170 /* we do not want to recurse here... */
2171 static int recursive
;
2175 /* danger! running these procedures can trigger srst assertions and power dropouts.
2176 * We need to avoid an infinite loop/recursion here and we do that by
2177 * clearing the flags after running these events.
2179 int did_something
= 0;
2180 if (runSrstAsserted
) {
2181 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2182 Jim_Eval(interp
, "srst_asserted");
2185 if (runSrstDeasserted
) {
2186 Jim_Eval(interp
, "srst_deasserted");
2189 if (runPowerDropout
) {
2190 LOG_INFO("Power dropout detected, running power_dropout proc.");
2191 Jim_Eval(interp
, "power_dropout");
2194 if (runPowerRestore
) {
2195 Jim_Eval(interp
, "power_restore");
2199 if (did_something
) {
2200 /* clear detect flags */
2204 /* clear action flags */
2206 runSrstAsserted
= 0;
2207 runSrstDeasserted
= 0;
2208 runPowerRestore
= 0;
2209 runPowerDropout
= 0;
2214 if (backoff_times
> backoff_count
) {
2215 /* do not poll this time as we failed previously */
2221 /* Poll targets for state changes unless that's globally disabled.
2222 * Skip targets that are currently disabled.
2224 for (struct target
*target
= all_targets
;
2225 is_jtag_poll_safe() && target
;
2226 target
= target
->next
) {
2227 if (!target
->tap
->enabled
)
2230 /* only poll target if we've got power and srst isn't asserted */
2231 if (!powerDropout
&& !srstAsserted
) {
2232 /* polling may fail silently until the target has been examined */
2233 retval
= target_poll(target
);
2234 if (retval
!= ERROR_OK
) {
2235 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2236 if (backoff_times
* polling_interval
< 5000) {
2240 LOG_USER("Polling target failed, GDB will be halted. Polling again in %dms",
2241 backoff_times
* polling_interval
);
2243 /* Tell GDB to halt the debugger. This allows the user to
2244 * run monitor commands to handle the situation.
2246 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2249 /* Since we succeeded, we reset backoff count */
2250 if (backoff_times
> 0)
2251 LOG_USER("Polling succeeded again");
2259 COMMAND_HANDLER(handle_reg_command
)
2261 struct target
*target
;
2262 struct reg
*reg
= NULL
;
2268 target
= get_current_target(CMD_CTX
);
2270 /* list all available registers for the current target */
2271 if (CMD_ARGC
== 0) {
2272 struct reg_cache
*cache
= target
->reg_cache
;
2278 command_print(CMD_CTX
, "===== %s", cache
->name
);
2280 for (i
= 0, reg
= cache
->reg_list
;
2281 i
< cache
->num_regs
;
2282 i
++, reg
++, count
++) {
2283 /* only print cached values if they are valid */
2285 value
= buf_to_str(reg
->value
,
2287 command_print(CMD_CTX
,
2288 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2296 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2301 cache
= cache
->next
;
2307 /* access a single register by its ordinal number */
2308 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2310 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2312 struct reg_cache
*cache
= target
->reg_cache
;
2316 for (i
= 0; i
< cache
->num_regs
; i
++) {
2317 if (count
++ == num
) {
2318 reg
= &cache
->reg_list
[i
];
2324 cache
= cache
->next
;
2328 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2329 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2333 /* access a single register by its name */
2334 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2337 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2342 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2344 /* display a register */
2345 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2346 && (CMD_ARGV
[1][0] <= '9')))) {
2347 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2350 if (reg
->valid
== 0)
2351 reg
->type
->get(reg
);
2352 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2353 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2358 /* set register value */
2359 if (CMD_ARGC
== 2) {
2360 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2363 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2365 reg
->type
->set(reg
, buf
);
2367 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2368 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2376 return ERROR_COMMAND_SYNTAX_ERROR
;
2379 COMMAND_HANDLER(handle_poll_command
)
2381 int retval
= ERROR_OK
;
2382 struct target
*target
= get_current_target(CMD_CTX
);
2384 if (CMD_ARGC
== 0) {
2385 command_print(CMD_CTX
, "background polling: %s",
2386 jtag_poll_get_enabled() ? "on" : "off");
2387 command_print(CMD_CTX
, "TAP: %s (%s)",
2388 target
->tap
->dotted_name
,
2389 target
->tap
->enabled
? "enabled" : "disabled");
2390 if (!target
->tap
->enabled
)
2392 retval
= target_poll(target
);
2393 if (retval
!= ERROR_OK
)
2395 retval
= target_arch_state(target
);
2396 if (retval
!= ERROR_OK
)
2398 } else if (CMD_ARGC
== 1) {
2400 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2401 jtag_poll_set_enabled(enable
);
2403 return ERROR_COMMAND_SYNTAX_ERROR
;
2408 COMMAND_HANDLER(handle_wait_halt_command
)
2411 return ERROR_COMMAND_SYNTAX_ERROR
;
2414 if (1 == CMD_ARGC
) {
2415 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2416 if (ERROR_OK
!= retval
)
2417 return ERROR_COMMAND_SYNTAX_ERROR
;
2418 /* convert seconds (given) to milliseconds (needed) */
2422 struct target
*target
= get_current_target(CMD_CTX
);
2423 return target_wait_state(target
, TARGET_HALTED
, ms
);
2426 /* wait for target state to change. The trick here is to have a low
2427 * latency for short waits and not to suck up all the CPU time
2430 * After 500ms, keep_alive() is invoked
2432 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2435 long long then
= 0, cur
;
2439 retval
= target_poll(target
);
2440 if (retval
!= ERROR_OK
)
2442 if (target
->state
== state
)
2447 then
= timeval_ms();
2448 LOG_DEBUG("waiting for target %s...",
2449 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2455 if ((cur
-then
) > ms
) {
2456 LOG_ERROR("timed out while waiting for target %s",
2457 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2465 COMMAND_HANDLER(handle_halt_command
)
2469 struct target
*target
= get_current_target(CMD_CTX
);
2470 int retval
= target_halt(target
);
2471 if (ERROR_OK
!= retval
)
2474 if (CMD_ARGC
== 1) {
2475 unsigned wait_local
;
2476 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2477 if (ERROR_OK
!= retval
)
2478 return ERROR_COMMAND_SYNTAX_ERROR
;
2483 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2486 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2488 struct target
*target
= get_current_target(CMD_CTX
);
2490 LOG_USER("requesting target halt and executing a soft reset");
2492 target
->type
->soft_reset_halt(target
);
2497 COMMAND_HANDLER(handle_reset_command
)
2500 return ERROR_COMMAND_SYNTAX_ERROR
;
2502 enum target_reset_mode reset_mode
= RESET_RUN
;
2503 if (CMD_ARGC
== 1) {
2505 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2506 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2507 return ERROR_COMMAND_SYNTAX_ERROR
;
2508 reset_mode
= n
->value
;
2511 /* reset *all* targets */
2512 return target_process_reset(CMD_CTX
, reset_mode
);
2516 COMMAND_HANDLER(handle_resume_command
)
2520 return ERROR_COMMAND_SYNTAX_ERROR
;
2522 struct target
*target
= get_current_target(CMD_CTX
);
2524 /* with no CMD_ARGV, resume from current pc, addr = 0,
2525 * with one arguments, addr = CMD_ARGV[0],
2526 * handle breakpoints, not debugging */
2528 if (CMD_ARGC
== 1) {
2529 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2533 return target_resume(target
, current
, addr
, 1, 0);
2536 COMMAND_HANDLER(handle_step_command
)
2539 return ERROR_COMMAND_SYNTAX_ERROR
;
2543 /* with no CMD_ARGV, step from current pc, addr = 0,
2544 * with one argument addr = CMD_ARGV[0],
2545 * handle breakpoints, debugging */
2548 if (CMD_ARGC
== 1) {
2549 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2553 struct target
*target
= get_current_target(CMD_CTX
);
2555 return target
->type
->step(target
, current_pc
, addr
, 1);
2558 static void handle_md_output(struct command_context
*cmd_ctx
,
2559 struct target
*target
, uint32_t address
, unsigned size
,
2560 unsigned count
, const uint8_t *buffer
)
2562 const unsigned line_bytecnt
= 32;
2563 unsigned line_modulo
= line_bytecnt
/ size
;
2565 char output
[line_bytecnt
* 4 + 1];
2566 unsigned output_len
= 0;
2568 const char *value_fmt
;
2571 value_fmt
= "%8.8x ";
2574 value_fmt
= "%4.4x ";
2577 value_fmt
= "%2.2x ";
2580 /* "can't happen", caller checked */
2581 LOG_ERROR("invalid memory read size: %u", size
);
2585 for (unsigned i
= 0; i
< count
; i
++) {
2586 if (i
% line_modulo
== 0) {
2587 output_len
+= snprintf(output
+ output_len
,
2588 sizeof(output
) - output_len
,
2590 (unsigned)(address
+ (i
*size
)));
2594 const uint8_t *value_ptr
= buffer
+ i
* size
;
2597 value
= target_buffer_get_u32(target
, value_ptr
);
2600 value
= target_buffer_get_u16(target
, value_ptr
);
2605 output_len
+= snprintf(output
+ output_len
,
2606 sizeof(output
) - output_len
,
2609 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
2610 command_print(cmd_ctx
, "%s", output
);
2616 COMMAND_HANDLER(handle_md_command
)
2619 return ERROR_COMMAND_SYNTAX_ERROR
;
2622 switch (CMD_NAME
[2]) {
2633 return ERROR_COMMAND_SYNTAX_ERROR
;
2636 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2637 int (*fn
)(struct target
*target
,
2638 uint32_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
2642 fn
= target_read_phys_memory
;
2644 fn
= target_read_memory
;
2645 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2646 return ERROR_COMMAND_SYNTAX_ERROR
;
2649 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2653 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2655 uint8_t *buffer
= calloc(count
, size
);
2657 struct target
*target
= get_current_target(CMD_CTX
);
2658 int retval
= fn(target
, address
, size
, count
, buffer
);
2659 if (ERROR_OK
== retval
)
2660 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2667 typedef int (*target_write_fn
)(struct target
*target
,
2668 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
2670 static int target_write_memory_fast(struct target
*target
,
2671 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
2673 return target_write_buffer(target
, address
, size
* count
, buffer
);
2676 static int target_fill_mem(struct target
*target
,
2685 /* We have to write in reasonably large chunks to be able
2686 * to fill large memory areas with any sane speed */
2687 const unsigned chunk_size
= 16384;
2688 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
2689 if (target_buf
== NULL
) {
2690 LOG_ERROR("Out of memory");
2694 for (unsigned i
= 0; i
< chunk_size
; i
++) {
2695 switch (data_size
) {
2697 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
2700 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
2703 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
2710 int retval
= ERROR_OK
;
2712 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
2715 if (current
> chunk_size
)
2716 current
= chunk_size
;
2717 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
2718 if (retval
!= ERROR_OK
)
2720 /* avoid GDB timeouts */
2729 COMMAND_HANDLER(handle_mw_command
)
2732 return ERROR_COMMAND_SYNTAX_ERROR
;
2733 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2738 fn
= target_write_phys_memory
;
2740 fn
= target_write_memory_fast
;
2741 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2742 return ERROR_COMMAND_SYNTAX_ERROR
;
2745 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2748 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2752 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2754 struct target
*target
= get_current_target(CMD_CTX
);
2756 switch (CMD_NAME
[2]) {
2767 return ERROR_COMMAND_SYNTAX_ERROR
;
2770 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
2773 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2774 uint32_t *min_address
, uint32_t *max_address
)
2776 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2777 return ERROR_COMMAND_SYNTAX_ERROR
;
2779 /* a base address isn't always necessary,
2780 * default to 0x0 (i.e. don't relocate) */
2781 if (CMD_ARGC
>= 2) {
2783 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2784 image
->base_address
= addr
;
2785 image
->base_address_set
= 1;
2787 image
->base_address_set
= 0;
2789 image
->start_address_set
= 0;
2792 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2793 if (CMD_ARGC
== 5) {
2794 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2795 /* use size (given) to find max (required) */
2796 *max_address
+= *min_address
;
2799 if (*min_address
> *max_address
)
2800 return ERROR_COMMAND_SYNTAX_ERROR
;
2805 COMMAND_HANDLER(handle_load_image_command
)
2809 uint32_t image_size
;
2810 uint32_t min_address
= 0;
2811 uint32_t max_address
= 0xffffffff;
2815 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2816 &image
, &min_address
, &max_address
);
2817 if (ERROR_OK
!= retval
)
2820 struct target
*target
= get_current_target(CMD_CTX
);
2822 struct duration bench
;
2823 duration_start(&bench
);
2825 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2830 for (i
= 0; i
< image
.num_sections
; i
++) {
2831 buffer
= malloc(image
.sections
[i
].size
);
2832 if (buffer
== NULL
) {
2833 command_print(CMD_CTX
,
2834 "error allocating buffer for section (%d bytes)",
2835 (int)(image
.sections
[i
].size
));
2839 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
2840 if (retval
!= ERROR_OK
) {
2845 uint32_t offset
= 0;
2846 uint32_t length
= buf_cnt
;
2848 /* DANGER!!! beware of unsigned comparision here!!! */
2850 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
2851 (image
.sections
[i
].base_address
< max_address
)) {
2853 if (image
.sections
[i
].base_address
< min_address
) {
2854 /* clip addresses below */
2855 offset
+= min_address
-image
.sections
[i
].base_address
;
2859 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2860 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2862 retval
= target_write_buffer(target
,
2863 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
2864 if (retval
!= ERROR_OK
) {
2868 image_size
+= length
;
2869 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2870 (unsigned int)length
,
2871 image
.sections
[i
].base_address
+ offset
);
2877 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
2878 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2879 "in %fs (%0.3f KiB/s)", image_size
,
2880 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2883 image_close(&image
);
2889 COMMAND_HANDLER(handle_dump_image_command
)
2891 struct fileio fileio
;
2893 int retval
, retvaltemp
;
2894 uint32_t address
, size
;
2895 struct duration bench
;
2896 struct target
*target
= get_current_target(CMD_CTX
);
2899 return ERROR_COMMAND_SYNTAX_ERROR
;
2901 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2902 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2904 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
2905 buffer
= malloc(buf_size
);
2909 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
2910 if (retval
!= ERROR_OK
) {
2915 duration_start(&bench
);
2918 size_t size_written
;
2919 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
2920 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2921 if (retval
!= ERROR_OK
)
2924 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2925 if (retval
!= ERROR_OK
)
2928 size
-= this_run_size
;
2929 address
+= this_run_size
;
2934 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
2936 retval
= fileio_size(&fileio
, &filesize
);
2937 if (retval
!= ERROR_OK
)
2939 command_print(CMD_CTX
,
2940 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize
,
2941 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
2944 retvaltemp
= fileio_close(&fileio
);
2945 if (retvaltemp
!= ERROR_OK
)
2951 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
2955 uint32_t image_size
;
2958 uint32_t checksum
= 0;
2959 uint32_t mem_checksum
= 0;
2963 struct target
*target
= get_current_target(CMD_CTX
);
2966 return ERROR_COMMAND_SYNTAX_ERROR
;
2969 LOG_ERROR("no target selected");
2973 struct duration bench
;
2974 duration_start(&bench
);
2976 if (CMD_ARGC
>= 2) {
2978 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2979 image
.base_address
= addr
;
2980 image
.base_address_set
= 1;
2982 image
.base_address_set
= 0;
2983 image
.base_address
= 0x0;
2986 image
.start_address_set
= 0;
2988 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
2989 if (retval
!= ERROR_OK
)
2995 for (i
= 0; i
< image
.num_sections
; i
++) {
2996 buffer
= malloc(image
.sections
[i
].size
);
2997 if (buffer
== NULL
) {
2998 command_print(CMD_CTX
,
2999 "error allocating buffer for section (%d bytes)",
3000 (int)(image
.sections
[i
].size
));
3003 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3004 if (retval
!= ERROR_OK
) {
3010 /* calculate checksum of image */
3011 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3012 if (retval
!= ERROR_OK
) {
3017 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3018 if (retval
!= ERROR_OK
) {
3023 if (checksum
!= mem_checksum
) {
3024 /* failed crc checksum, fall back to a binary compare */
3028 LOG_ERROR("checksum mismatch - attempting binary compare");
3030 data
= (uint8_t *)malloc(buf_cnt
);
3032 /* Can we use 32bit word accesses? */
3034 int count
= buf_cnt
;
3035 if ((count
% 4) == 0) {
3039 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3040 if (retval
== ERROR_OK
) {
3042 for (t
= 0; t
< buf_cnt
; t
++) {
3043 if (data
[t
] != buffer
[t
]) {
3044 command_print(CMD_CTX
,
3045 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3047 (unsigned)(t
+ image
.sections
[i
].base_address
),
3050 if (diffs
++ >= 127) {
3051 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3063 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
3064 image
.sections
[i
].base_address
,
3069 image_size
+= buf_cnt
;
3072 command_print(CMD_CTX
, "No more differences found.");
3075 retval
= ERROR_FAIL
;
3076 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3077 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3078 "in %fs (%0.3f KiB/s)", image_size
,
3079 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3082 image_close(&image
);
3087 COMMAND_HANDLER(handle_verify_image_command
)
3089 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
3092 COMMAND_HANDLER(handle_test_image_command
)
3094 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
3097 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3099 struct target
*target
= get_current_target(cmd_ctx
);
3100 struct breakpoint
*breakpoint
= target
->breakpoints
;
3101 while (breakpoint
) {
3102 if (breakpoint
->type
== BKPT_SOFT
) {
3103 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3104 breakpoint
->length
, 16);
3105 command_print(cmd_ctx
, "IVA breakpoint: 0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
3106 breakpoint
->address
,
3108 breakpoint
->set
, buf
);
3111 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3112 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3114 breakpoint
->length
, breakpoint
->set
);
3115 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3116 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3117 breakpoint
->address
,
3118 breakpoint
->length
, breakpoint
->set
);
3119 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3122 command_print(cmd_ctx
, "Breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3123 breakpoint
->address
,
3124 breakpoint
->length
, breakpoint
->set
);
3127 breakpoint
= breakpoint
->next
;
3132 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
3133 uint32_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3135 struct target
*target
= get_current_target(cmd_ctx
);
3138 int retval
= breakpoint_add(target
, addr
, length
, hw
);
3139 if (ERROR_OK
== retval
)
3140 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
3142 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3145 } else if (addr
== 0) {
3146 int retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3147 if (ERROR_OK
== retval
)
3148 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3150 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3154 int retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3155 if (ERROR_OK
== retval
)
3156 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3158 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3165 COMMAND_HANDLER(handle_bp_command
)
3174 return handle_bp_command_list(CMD_CTX
);
3178 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3179 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3180 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3183 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3185 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3187 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3190 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3191 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
3193 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3194 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3196 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3201 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3202 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3203 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3204 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3207 return ERROR_COMMAND_SYNTAX_ERROR
;
3211 COMMAND_HANDLER(handle_rbp_command
)
3214 return ERROR_COMMAND_SYNTAX_ERROR
;
3217 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3219 struct target
*target
= get_current_target(CMD_CTX
);
3220 breakpoint_remove(target
, addr
);
3225 COMMAND_HANDLER(handle_wp_command
)
3227 struct target
*target
= get_current_target(CMD_CTX
);
3229 if (CMD_ARGC
== 0) {
3230 struct watchpoint
*watchpoint
= target
->watchpoints
;
3232 while (watchpoint
) {
3233 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
3234 ", len: 0x%8.8" PRIx32
3235 ", r/w/a: %i, value: 0x%8.8" PRIx32
3236 ", mask: 0x%8.8" PRIx32
,
3237 watchpoint
->address
,
3239 (int)watchpoint
->rw
,
3242 watchpoint
= watchpoint
->next
;
3247 enum watchpoint_rw type
= WPT_ACCESS
;
3249 uint32_t length
= 0;
3250 uint32_t data_value
= 0x0;
3251 uint32_t data_mask
= 0xffffffff;
3255 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3258 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3261 switch (CMD_ARGV
[2][0]) {
3272 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3273 return ERROR_COMMAND_SYNTAX_ERROR
;
3277 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3278 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3282 return ERROR_COMMAND_SYNTAX_ERROR
;
3285 int retval
= watchpoint_add(target
, addr
, length
, type
,
3286 data_value
, data_mask
);
3287 if (ERROR_OK
!= retval
)
3288 LOG_ERROR("Failure setting watchpoints");
3293 COMMAND_HANDLER(handle_rwp_command
)
3296 return ERROR_COMMAND_SYNTAX_ERROR
;
3299 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3301 struct target
*target
= get_current_target(CMD_CTX
);
3302 watchpoint_remove(target
, addr
);
3308 * Translate a virtual address to a physical address.
3310 * The low-level target implementation must have logged a detailed error
3311 * which is forwarded to telnet/GDB session.
3313 COMMAND_HANDLER(handle_virt2phys_command
)
3316 return ERROR_COMMAND_SYNTAX_ERROR
;
3319 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
3322 struct target
*target
= get_current_target(CMD_CTX
);
3323 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3324 if (retval
== ERROR_OK
)
3325 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
3330 static void writeData(FILE *f
, const void *data
, size_t len
)
3332 size_t written
= fwrite(data
, 1, len
, f
);
3334 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3337 static void writeLong(FILE *f
, int l
)
3340 for (i
= 0; i
< 4; i
++) {
3341 char c
= (l
>> (i
*8))&0xff;
3342 writeData(f
, &c
, 1);
3347 static void writeString(FILE *f
, char *s
)
3349 writeData(f
, s
, strlen(s
));
3352 /* Dump a gmon.out histogram file. */
3353 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
)
3356 FILE *f
= fopen(filename
, "w");
3359 writeString(f
, "gmon");
3360 writeLong(f
, 0x00000001); /* Version */
3361 writeLong(f
, 0); /* padding */
3362 writeLong(f
, 0); /* padding */
3363 writeLong(f
, 0); /* padding */
3365 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3366 writeData(f
, &zero
, 1);
3368 /* figure out bucket size */
3369 uint32_t min
= samples
[0];
3370 uint32_t max
= samples
[0];
3371 for (i
= 0; i
< sampleNum
; i
++) {
3372 if (min
> samples
[i
])
3374 if (max
< samples
[i
])
3378 int addressSpace
= (max
- min
+ 1);
3379 assert(addressSpace
>= 2);
3381 static const uint32_t maxBuckets
= 16 * 1024; /* maximum buckets. */
3382 uint32_t length
= addressSpace
;
3383 if (length
> maxBuckets
)
3384 length
= maxBuckets
;
3385 int *buckets
= malloc(sizeof(int)*length
);
3386 if (buckets
== NULL
) {
3390 memset(buckets
, 0, sizeof(int) * length
);
3391 for (i
= 0; i
< sampleNum
; i
++) {
3392 uint32_t address
= samples
[i
];
3393 long long a
= address
- min
;
3394 long long b
= length
- 1;
3395 long long c
= addressSpace
- 1;
3396 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
3400 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3401 writeLong(f
, min
); /* low_pc */
3402 writeLong(f
, max
); /* high_pc */
3403 writeLong(f
, length
); /* # of samples */
3404 writeLong(f
, 100); /* KLUDGE! We lie, ca. 100Hz best case. */
3405 writeString(f
, "seconds");
3406 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3407 writeData(f
, &zero
, 1);
3408 writeString(f
, "s");
3410 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3412 char *data
= malloc(2 * length
);
3414 for (i
= 0; i
< length
; i
++) {
3419 data
[i
* 2] = val
&0xff;
3420 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
3423 writeData(f
, data
, length
* 2);
3431 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3432 * which will be used as a random sampling of PC */
3433 COMMAND_HANDLER(handle_profile_command
)
3435 struct target
*target
= get_current_target(CMD_CTX
);
3436 struct timeval timeout
, now
;
3438 gettimeofday(&timeout
, NULL
);
3440 return ERROR_COMMAND_SYNTAX_ERROR
;
3442 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], offset
);
3444 timeval_add_time(&timeout
, offset
, 0);
3447 * @todo: Some cores let us sample the PC without the
3448 * annoying halt/resume step; for example, ARMv7 PCSR.
3449 * Provide a way to use that more efficient mechanism.
3452 command_print(CMD_CTX
, "Starting profiling. Halting and resuming the target as often as we can...");
3454 static const int maxSample
= 10000;
3455 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
3456 if (samples
== NULL
)
3460 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3461 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
3463 int retval
= ERROR_OK
;
3465 target_poll(target
);
3466 if (target
->state
== TARGET_HALTED
) {
3467 uint32_t t
= *((uint32_t *)reg
->value
);
3468 samples
[numSamples
++] = t
;
3469 /* current pc, addr = 0, do not handle breakpoints, not debugging */
3470 retval
= target_resume(target
, 1, 0, 0, 0);
3471 target_poll(target
);
3472 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3473 } else if (target
->state
== TARGET_RUNNING
) {
3474 /* We want to quickly sample the PC. */
3475 retval
= target_halt(target
);
3476 if (retval
!= ERROR_OK
) {
3481 command_print(CMD_CTX
, "Target not halted or running");
3485 if (retval
!= ERROR_OK
)
3488 gettimeofday(&now
, NULL
);
3489 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
)
3490 && (now
.tv_usec
>= timeout
.tv_usec
))) {
3491 command_print(CMD_CTX
, "Profiling completed. %d samples.", numSamples
);
3492 retval
= target_poll(target
);
3493 if (retval
!= ERROR_OK
) {
3497 if (target
->state
== TARGET_HALTED
) {
3498 /* current pc, addr = 0, do not handle
3499 * breakpoints, not debugging */
3500 target_resume(target
, 1, 0, 0, 0);
3502 retval
= target_poll(target
);
3503 if (retval
!= ERROR_OK
) {
3507 writeGmon(samples
, numSamples
, CMD_ARGV
[1]);
3508 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3517 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
3520 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3523 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3527 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3528 valObjPtr
= Jim_NewIntObj(interp
, val
);
3529 if (!nameObjPtr
|| !valObjPtr
) {
3534 Jim_IncrRefCount(nameObjPtr
);
3535 Jim_IncrRefCount(valObjPtr
);
3536 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3537 Jim_DecrRefCount(interp
, nameObjPtr
);
3538 Jim_DecrRefCount(interp
, valObjPtr
);
3540 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3544 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3546 struct command_context
*context
;
3547 struct target
*target
;
3549 context
= current_command_context(interp
);
3550 assert(context
!= NULL
);
3552 target
= get_current_target(context
);
3553 if (target
== NULL
) {
3554 LOG_ERROR("mem2array: no current target");
3558 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
3561 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3569 const char *varname
;
3573 /* argv[1] = name of array to receive the data
3574 * argv[2] = desired width
3575 * argv[3] = memory address
3576 * argv[4] = count of times to read
3579 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3582 varname
= Jim_GetString(argv
[0], &len
);
3583 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3585 e
= Jim_GetLong(interp
, argv
[1], &l
);
3590 e
= Jim_GetLong(interp
, argv
[2], &l
);
3594 e
= Jim_GetLong(interp
, argv
[3], &l
);
3609 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3610 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3614 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3615 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3618 if ((addr
+ (len
* width
)) < addr
) {
3619 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3620 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3623 /* absurd transfer size? */
3625 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3626 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3631 ((width
== 2) && ((addr
& 1) == 0)) ||
3632 ((width
== 4) && ((addr
& 3) == 0))) {
3636 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3637 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3640 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3649 size_t buffersize
= 4096;
3650 uint8_t *buffer
= malloc(buffersize
);
3657 /* Slurp... in buffer size chunks */
3659 count
= len
; /* in objects.. */
3660 if (count
> (buffersize
/ width
))
3661 count
= (buffersize
/ width
);
3663 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3664 if (retval
!= ERROR_OK
) {
3666 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3670 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3671 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3675 v
= 0; /* shut up gcc */
3676 for (i
= 0; i
< count
; i
++, n
++) {
3679 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3682 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3685 v
= buffer
[i
] & 0x0ff;
3688 new_int_array_element(interp
, varname
, n
, v
);
3696 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3701 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
3704 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3708 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3712 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3718 Jim_IncrRefCount(nameObjPtr
);
3719 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3720 Jim_DecrRefCount(interp
, nameObjPtr
);
3722 if (valObjPtr
== NULL
)
3725 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3726 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3731 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3733 struct command_context
*context
;
3734 struct target
*target
;
3736 context
= current_command_context(interp
);
3737 assert(context
!= NULL
);
3739 target
= get_current_target(context
);
3740 if (target
== NULL
) {
3741 LOG_ERROR("array2mem: no current target");
3745 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
3748 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
3749 int argc
, Jim_Obj
*const *argv
)
3757 const char *varname
;
3761 /* argv[1] = name of array to get the data
3762 * argv[2] = desired width
3763 * argv[3] = memory address
3764 * argv[4] = count to write
3767 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3770 varname
= Jim_GetString(argv
[0], &len
);
3771 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3773 e
= Jim_GetLong(interp
, argv
[1], &l
);
3778 e
= Jim_GetLong(interp
, argv
[2], &l
);
3782 e
= Jim_GetLong(interp
, argv
[3], &l
);
3797 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3798 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3799 "Invalid width param, must be 8/16/32", NULL
);
3803 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3804 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3805 "array2mem: zero width read?", NULL
);
3808 if ((addr
+ (len
* width
)) < addr
) {
3809 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3810 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3811 "array2mem: addr + len - wraps to zero?", NULL
);
3814 /* absurd transfer size? */
3816 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3817 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3818 "array2mem: absurd > 64K item request", NULL
);
3823 ((width
== 2) && ((addr
& 1) == 0)) ||
3824 ((width
== 4) && ((addr
& 3) == 0))) {
3828 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3829 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3832 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3843 size_t buffersize
= 4096;
3844 uint8_t *buffer
= malloc(buffersize
);
3849 /* Slurp... in buffer size chunks */
3851 count
= len
; /* in objects.. */
3852 if (count
> (buffersize
/ width
))
3853 count
= (buffersize
/ width
);
3855 v
= 0; /* shut up gcc */
3856 for (i
= 0; i
< count
; i
++, n
++) {
3857 get_int_array_element(interp
, varname
, n
, &v
);
3860 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
3863 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
3866 buffer
[i
] = v
& 0x0ff;
3872 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3873 if (retval
!= ERROR_OK
) {
3875 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3879 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3880 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3888 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3893 /* FIX? should we propagate errors here rather than printing them
3896 void target_handle_event(struct target
*target
, enum target_event e
)
3898 struct target_event_action
*teap
;
3900 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3901 if (teap
->event
== e
) {
3902 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3903 target
->target_number
,
3904 target_name(target
),
3905 target_type_name(target
),
3907 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3908 Jim_GetString(teap
->body
, NULL
));
3909 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
) {
3910 Jim_MakeErrorMessage(teap
->interp
);
3911 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
3918 * Returns true only if the target has a handler for the specified event.
3920 bool target_has_event_action(struct target
*target
, enum target_event event
)
3922 struct target_event_action
*teap
;
3924 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3925 if (teap
->event
== event
)
3931 enum target_cfg_param
{
3934 TCFG_WORK_AREA_VIRT
,
3935 TCFG_WORK_AREA_PHYS
,
3936 TCFG_WORK_AREA_SIZE
,
3937 TCFG_WORK_AREA_BACKUP
,
3941 TCFG_CHAIN_POSITION
,
3946 static Jim_Nvp nvp_config_opts
[] = {
3947 { .name
= "-type", .value
= TCFG_TYPE
},
3948 { .name
= "-event", .value
= TCFG_EVENT
},
3949 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3950 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3951 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3952 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3953 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3954 { .name
= "-variant", .value
= TCFG_VARIANT
},
3955 { .name
= "-coreid", .value
= TCFG_COREID
},
3956 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3957 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
3958 { .name
= "-rtos", .value
= TCFG_RTOS
},
3959 { .name
= NULL
, .value
= -1 }
3962 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
3970 /* parse config or cget options ... */
3971 while (goi
->argc
> 0) {
3972 Jim_SetEmptyResult(goi
->interp
);
3973 /* Jim_GetOpt_Debug(goi); */
3975 if (target
->type
->target_jim_configure
) {
3976 /* target defines a configure function */
3977 /* target gets first dibs on parameters */
3978 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
3987 /* otherwise we 'continue' below */
3989 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
3991 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
3997 if (goi
->isconfigure
) {
3998 Jim_SetResultFormatted(goi
->interp
,
3999 "not settable: %s", n
->name
);
4003 if (goi
->argc
!= 0) {
4004 Jim_WrongNumArgs(goi
->interp
,
4005 goi
->argc
, goi
->argv
,
4010 Jim_SetResultString(goi
->interp
,
4011 target_type_name(target
), -1);
4015 if (goi
->argc
== 0) {
4016 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
4020 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
4022 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
4026 if (goi
->isconfigure
) {
4027 if (goi
->argc
!= 1) {
4028 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
4032 if (goi
->argc
!= 0) {
4033 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
4039 struct target_event_action
*teap
;
4041 teap
= target
->event_action
;
4042 /* replace existing? */
4044 if (teap
->event
== (enum target_event
)n
->value
)
4049 if (goi
->isconfigure
) {
4050 bool replace
= true;
4053 teap
= calloc(1, sizeof(*teap
));
4056 teap
->event
= n
->value
;
4057 teap
->interp
= goi
->interp
;
4058 Jim_GetOpt_Obj(goi
, &o
);
4060 Jim_DecrRefCount(teap
->interp
, teap
->body
);
4061 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
4064 * Tcl/TK - "tk events" have a nice feature.
4065 * See the "BIND" command.
4066 * We should support that here.
4067 * You can specify %X and %Y in the event code.
4068 * The idea is: %T - target name.
4069 * The idea is: %N - target number
4070 * The idea is: %E - event name.
4072 Jim_IncrRefCount(teap
->body
);
4075 /* add to head of event list */
4076 teap
->next
= target
->event_action
;
4077 target
->event_action
= teap
;
4079 Jim_SetEmptyResult(goi
->interp
);
4083 Jim_SetEmptyResult(goi
->interp
);
4085 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
4091 case TCFG_WORK_AREA_VIRT
:
4092 if (goi
->isconfigure
) {
4093 target_free_all_working_areas(target
);
4094 e
= Jim_GetOpt_Wide(goi
, &w
);
4097 target
->working_area_virt
= w
;
4098 target
->working_area_virt_spec
= true;
4103 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
4107 case TCFG_WORK_AREA_PHYS
:
4108 if (goi
->isconfigure
) {
4109 target_free_all_working_areas(target
);
4110 e
= Jim_GetOpt_Wide(goi
, &w
);
4113 target
->working_area_phys
= w
;
4114 target
->working_area_phys_spec
= true;
4119 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
4123 case TCFG_WORK_AREA_SIZE
:
4124 if (goi
->isconfigure
) {
4125 target_free_all_working_areas(target
);
4126 e
= Jim_GetOpt_Wide(goi
, &w
);
4129 target
->working_area_size
= w
;
4134 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4138 case TCFG_WORK_AREA_BACKUP
:
4139 if (goi
->isconfigure
) {
4140 target_free_all_working_areas(target
);
4141 e
= Jim_GetOpt_Wide(goi
, &w
);
4144 /* make this exactly 1 or 0 */
4145 target
->backup_working_area
= (!!w
);
4150 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
4151 /* loop for more e*/
4156 if (goi
->isconfigure
) {
4157 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
4159 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
4162 target
->endianness
= n
->value
;
4167 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4168 if (n
->name
== NULL
) {
4169 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4170 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4172 Jim_SetResultString(goi
->interp
, n
->name
, -1);
4177 if (goi
->isconfigure
) {
4178 if (goi
->argc
< 1) {
4179 Jim_SetResultFormatted(goi
->interp
,
4184 if (target
->variant
)
4185 free((void *)(target
->variant
));
4186 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
4189 target
->variant
= strdup(cp
);
4194 Jim_SetResultString(goi
->interp
, target
->variant
, -1);
4199 if (goi
->isconfigure
) {
4200 e
= Jim_GetOpt_Wide(goi
, &w
);
4203 target
->coreid
= (int32_t)w
;
4208 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4212 case TCFG_CHAIN_POSITION
:
4213 if (goi
->isconfigure
) {
4215 struct jtag_tap
*tap
;
4216 target_free_all_working_areas(target
);
4217 e
= Jim_GetOpt_Obj(goi
, &o_t
);
4220 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
4223 /* make this exactly 1 or 0 */
4229 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
4230 /* loop for more e*/
4233 if (goi
->isconfigure
) {
4234 e
= Jim_GetOpt_Wide(goi
, &w
);
4237 target
->dbgbase
= (uint32_t)w
;
4238 target
->dbgbase_set
= true;
4243 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
4250 int result
= rtos_create(goi
, target
);
4251 if (result
!= JIM_OK
)
4257 } /* while (goi->argc) */
4260 /* done - we return */
4264 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4268 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4269 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4270 int need_args
= 1 + goi
.isconfigure
;
4271 if (goi
.argc
< need_args
) {
4272 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4274 ? "missing: -option VALUE ..."
4275 : "missing: -option ...");
4278 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4279 return target_configure(&goi
, target
);
4282 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4284 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4287 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4289 if (goi
.argc
< 2 || goi
.argc
> 4) {
4290 Jim_SetResultFormatted(goi
.interp
,
4291 "usage: %s [phys] <address> <data> [<count>]", cmd_name
);
4296 fn
= target_write_memory_fast
;
4299 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4301 struct Jim_Obj
*obj
;
4302 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4306 fn
= target_write_phys_memory
;
4310 e
= Jim_GetOpt_Wide(&goi
, &a
);
4315 e
= Jim_GetOpt_Wide(&goi
, &b
);
4320 if (goi
.argc
== 1) {
4321 e
= Jim_GetOpt_Wide(&goi
, &c
);
4326 /* all args must be consumed */
4330 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4332 if (strcasecmp(cmd_name
, "mww") == 0)
4334 else if (strcasecmp(cmd_name
, "mwh") == 0)
4336 else if (strcasecmp(cmd_name
, "mwb") == 0)
4339 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4343 return (target_fill_mem(target
, a
, fn
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4346 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4348 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4351 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4353 if ((goi
.argc
< 1) || (goi
.argc
> 3)) {
4354 Jim_SetResultFormatted(goi
.interp
,
4355 "usage: %s [phys] <address> [<count>]", cmd_name
);
4359 int (*fn
)(struct target
*target
,
4360 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
4361 fn
= target_read_memory
;
4364 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4366 struct Jim_Obj
*obj
;
4367 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4371 fn
= target_read_phys_memory
;
4375 e
= Jim_GetOpt_Wide(&goi
, &a
);
4379 if (goi
.argc
== 1) {
4380 e
= Jim_GetOpt_Wide(&goi
, &c
);
4386 /* all args must be consumed */
4390 jim_wide b
= 1; /* shut up gcc */
4391 if (strcasecmp(cmd_name
, "mdw") == 0)
4393 else if (strcasecmp(cmd_name
, "mdh") == 0)
4395 else if (strcasecmp(cmd_name
, "mdb") == 0)
4398 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4402 /* convert count to "bytes" */
4405 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4406 uint8_t target_buf
[32];
4412 e
= fn(target
, a
, b
, y
/ b
, target_buf
);
4413 if (e
!= ERROR_OK
) {
4415 snprintf(tmp
, sizeof(tmp
), "%08lx", (long)a
);
4416 Jim_SetResultFormatted(interp
, "error reading target @ 0x%s", tmp
);
4420 command_print(NULL
, "0x%08x ", (int)(a
));
4423 for (x
= 0; x
< 16 && x
< y
; x
+= 4) {
4424 z
= target_buffer_get_u32(target
, &(target_buf
[x
]));
4425 command_print(NULL
, "%08x ", (int)(z
));
4427 for (; (x
< 16) ; x
+= 4)
4428 command_print(NULL
, " ");
4431 for (x
= 0; x
< 16 && x
< y
; x
+= 2) {
4432 z
= target_buffer_get_u16(target
, &(target_buf
[x
]));
4433 command_print(NULL
, "%04x ", (int)(z
));
4435 for (; (x
< 16) ; x
+= 2)
4436 command_print(NULL
, " ");
4440 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4441 z
= target_buffer_get_u8(target
, &(target_buf
[x
]));
4442 command_print(NULL
, "%02x ", (int)(z
));
4444 for (; (x
< 16) ; x
+= 1)
4445 command_print(NULL
, " ");
4448 /* ascii-ify the bytes */
4449 for (x
= 0 ; x
< y
; x
++) {
4450 if ((target_buf
[x
] >= 0x20) &&
4451 (target_buf
[x
] <= 0x7e)) {
4455 target_buf
[x
] = '.';
4460 target_buf
[x
] = ' ';
4465 /* print - with a newline */
4466 command_print(NULL
, "%s\n", target_buf
);
4474 static int jim_target_mem2array(Jim_Interp
*interp
,
4475 int argc
, Jim_Obj
*const *argv
)
4477 struct target
*target
= Jim_CmdPrivData(interp
);
4478 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4481 static int jim_target_array2mem(Jim_Interp
*interp
,
4482 int argc
, Jim_Obj
*const *argv
)
4484 struct target
*target
= Jim_CmdPrivData(interp
);
4485 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4488 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4490 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
4494 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4497 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4500 struct target
*target
= Jim_CmdPrivData(interp
);
4501 if (!target
->tap
->enabled
)
4502 return jim_target_tap_disabled(interp
);
4504 int e
= target
->type
->examine(target
);
4510 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4513 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4516 struct target
*target
= Jim_CmdPrivData(interp
);
4518 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
4524 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4527 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4530 struct target
*target
= Jim_CmdPrivData(interp
);
4531 if (!target
->tap
->enabled
)
4532 return jim_target_tap_disabled(interp
);
4535 if (!(target_was_examined(target
)))
4536 e
= ERROR_TARGET_NOT_EXAMINED
;
4538 e
= target
->type
->poll(target
);
4544 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4547 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4549 if (goi
.argc
!= 2) {
4550 Jim_WrongNumArgs(interp
, 0, argv
,
4551 "([tT]|[fF]|assert|deassert) BOOL");
4556 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4558 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4561 /* the halt or not param */
4563 e
= Jim_GetOpt_Wide(&goi
, &a
);
4567 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4568 if (!target
->tap
->enabled
)
4569 return jim_target_tap_disabled(interp
);
4570 if (!(target_was_examined(target
))) {
4571 LOG_ERROR("Target not examined yet");
4572 return ERROR_TARGET_NOT_EXAMINED
;
4574 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
4575 Jim_SetResultFormatted(interp
,
4576 "No target-specific reset for %s",
4577 target_name(target
));
4580 /* determine if we should halt or not. */
4581 target
->reset_halt
= !!a
;
4582 /* When this happens - all workareas are invalid. */
4583 target_free_all_working_areas_restore(target
, 0);
4586 if (n
->value
== NVP_ASSERT
)
4587 e
= target
->type
->assert_reset(target
);
4589 e
= target
->type
->deassert_reset(target
);
4590 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4593 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4596 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4599 struct target
*target
= Jim_CmdPrivData(interp
);
4600 if (!target
->tap
->enabled
)
4601 return jim_target_tap_disabled(interp
);
4602 int e
= target
->type
->halt(target
);
4603 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4606 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4609 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4611 /* params: <name> statename timeoutmsecs */
4612 if (goi
.argc
!= 2) {
4613 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4614 Jim_SetResultFormatted(goi
.interp
,
4615 "%s <state_name> <timeout_in_msec>", cmd_name
);
4620 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4622 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
4626 e
= Jim_GetOpt_Wide(&goi
, &a
);
4629 struct target
*target
= Jim_CmdPrivData(interp
);
4630 if (!target
->tap
->enabled
)
4631 return jim_target_tap_disabled(interp
);
4633 e
= target_wait_state(target
, n
->value
, a
);
4634 if (e
!= ERROR_OK
) {
4635 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
4636 Jim_SetResultFormatted(goi
.interp
,
4637 "target: %s wait %s fails (%#s) %s",
4638 target_name(target
), n
->name
,
4639 eObj
, target_strerror_safe(e
));
4640 Jim_FreeNewObj(interp
, eObj
);
4645 /* List for human, Events defined for this target.
4646 * scripts/programs should use 'name cget -event NAME'
4648 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4650 struct command_context
*cmd_ctx
= current_command_context(interp
);
4651 assert(cmd_ctx
!= NULL
);
4653 struct target
*target
= Jim_CmdPrivData(interp
);
4654 struct target_event_action
*teap
= target
->event_action
;
4655 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4656 target
->target_number
,
4657 target_name(target
));
4658 command_print(cmd_ctx
, "%-25s | Body", "Event");
4659 command_print(cmd_ctx
, "------------------------- | "
4660 "----------------------------------------");
4662 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
4663 command_print(cmd_ctx
, "%-25s | %s",
4664 opt
->name
, Jim_GetString(teap
->body
, NULL
));
4667 command_print(cmd_ctx
, "***END***");
4670 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4673 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4676 struct target
*target
= Jim_CmdPrivData(interp
);
4677 Jim_SetResultString(interp
, target_state_name(target
), -1);
4680 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4683 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4684 if (goi
.argc
!= 1) {
4685 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4686 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
4690 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4692 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4695 struct target
*target
= Jim_CmdPrivData(interp
);
4696 target_handle_event(target
, n
->value
);
4700 static const struct command_registration target_instance_command_handlers
[] = {
4702 .name
= "configure",
4703 .mode
= COMMAND_CONFIG
,
4704 .jim_handler
= jim_target_configure
,
4705 .help
= "configure a new target for use",
4706 .usage
= "[target_attribute ...]",
4710 .mode
= COMMAND_ANY
,
4711 .jim_handler
= jim_target_configure
,
4712 .help
= "returns the specified target attribute",
4713 .usage
= "target_attribute",
4717 .mode
= COMMAND_EXEC
,
4718 .jim_handler
= jim_target_mw
,
4719 .help
= "Write 32-bit word(s) to target memory",
4720 .usage
= "address data [count]",
4724 .mode
= COMMAND_EXEC
,
4725 .jim_handler
= jim_target_mw
,
4726 .help
= "Write 16-bit half-word(s) to target memory",
4727 .usage
= "address data [count]",
4731 .mode
= COMMAND_EXEC
,
4732 .jim_handler
= jim_target_mw
,
4733 .help
= "Write byte(s) to target memory",
4734 .usage
= "address data [count]",
4738 .mode
= COMMAND_EXEC
,
4739 .jim_handler
= jim_target_md
,
4740 .help
= "Display target memory as 32-bit words",
4741 .usage
= "address [count]",
4745 .mode
= COMMAND_EXEC
,
4746 .jim_handler
= jim_target_md
,
4747 .help
= "Display target memory as 16-bit half-words",
4748 .usage
= "address [count]",
4752 .mode
= COMMAND_EXEC
,
4753 .jim_handler
= jim_target_md
,
4754 .help
= "Display target memory as 8-bit bytes",
4755 .usage
= "address [count]",
4758 .name
= "array2mem",
4759 .mode
= COMMAND_EXEC
,
4760 .jim_handler
= jim_target_array2mem
,
4761 .help
= "Writes Tcl array of 8/16/32 bit numbers "
4763 .usage
= "arrayname bitwidth address count",
4766 .name
= "mem2array",
4767 .mode
= COMMAND_EXEC
,
4768 .jim_handler
= jim_target_mem2array
,
4769 .help
= "Loads Tcl array of 8/16/32 bit numbers "
4770 "from target memory",
4771 .usage
= "arrayname bitwidth address count",
4774 .name
= "eventlist",
4775 .mode
= COMMAND_EXEC
,
4776 .jim_handler
= jim_target_event_list
,
4777 .help
= "displays a table of events defined for this target",
4781 .mode
= COMMAND_EXEC
,
4782 .jim_handler
= jim_target_current_state
,
4783 .help
= "displays the current state of this target",
4786 .name
= "arp_examine",
4787 .mode
= COMMAND_EXEC
,
4788 .jim_handler
= jim_target_examine
,
4789 .help
= "used internally for reset processing",
4792 .name
= "arp_halt_gdb",
4793 .mode
= COMMAND_EXEC
,
4794 .jim_handler
= jim_target_halt_gdb
,
4795 .help
= "used internally for reset processing to halt GDB",
4799 .mode
= COMMAND_EXEC
,
4800 .jim_handler
= jim_target_poll
,
4801 .help
= "used internally for reset processing",
4804 .name
= "arp_reset",
4805 .mode
= COMMAND_EXEC
,
4806 .jim_handler
= jim_target_reset
,
4807 .help
= "used internally for reset processing",
4811 .mode
= COMMAND_EXEC
,
4812 .jim_handler
= jim_target_halt
,
4813 .help
= "used internally for reset processing",
4816 .name
= "arp_waitstate",
4817 .mode
= COMMAND_EXEC
,
4818 .jim_handler
= jim_target_wait_state
,
4819 .help
= "used internally for reset processing",
4822 .name
= "invoke-event",
4823 .mode
= COMMAND_EXEC
,
4824 .jim_handler
= jim_target_invoke_event
,
4825 .help
= "invoke handler for specified event",
4826 .usage
= "event_name",
4828 COMMAND_REGISTRATION_DONE
4831 static int target_create(Jim_GetOptInfo
*goi
)
4839 struct target
*target
;
4840 struct command_context
*cmd_ctx
;
4842 cmd_ctx
= current_command_context(goi
->interp
);
4843 assert(cmd_ctx
!= NULL
);
4845 if (goi
->argc
< 3) {
4846 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4851 Jim_GetOpt_Obj(goi
, &new_cmd
);
4852 /* does this command exist? */
4853 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4855 cp
= Jim_GetString(new_cmd
, NULL
);
4856 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
4861 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4865 /* now does target type exist */
4866 for (x
= 0 ; target_types
[x
] ; x
++) {
4867 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4872 if (target_types
[x
] == NULL
) {
4873 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4874 for (x
= 0 ; target_types
[x
] ; x
++) {
4875 if (target_types
[x
+ 1]) {
4876 Jim_AppendStrings(goi
->interp
,
4877 Jim_GetResult(goi
->interp
),
4878 target_types
[x
]->name
,
4881 Jim_AppendStrings(goi
->interp
,
4882 Jim_GetResult(goi
->interp
),
4884 target_types
[x
]->name
, NULL
);
4891 target
= calloc(1, sizeof(struct target
));
4892 /* set target number */
4893 target
->target_number
= new_target_number();
4895 /* allocate memory for each unique target type */
4896 target
->type
= (struct target_type
*)calloc(1, sizeof(struct target_type
));
4898 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
4900 /* will be set by "-endian" */
4901 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4903 /* default to first core, override with -coreid */
4906 target
->working_area
= 0x0;
4907 target
->working_area_size
= 0x0;
4908 target
->working_areas
= NULL
;
4909 target
->backup_working_area
= 0;
4911 target
->state
= TARGET_UNKNOWN
;
4912 target
->debug_reason
= DBG_REASON_UNDEFINED
;
4913 target
->reg_cache
= NULL
;
4914 target
->breakpoints
= NULL
;
4915 target
->watchpoints
= NULL
;
4916 target
->next
= NULL
;
4917 target
->arch_info
= NULL
;
4919 target
->display
= 1;
4921 target
->halt_issued
= false;
4923 /* initialize trace information */
4924 target
->trace_info
= malloc(sizeof(struct trace
));
4925 target
->trace_info
->num_trace_points
= 0;
4926 target
->trace_info
->trace_points_size
= 0;
4927 target
->trace_info
->trace_points
= NULL
;
4928 target
->trace_info
->trace_history_size
= 0;
4929 target
->trace_info
->trace_history
= NULL
;
4930 target
->trace_info
->trace_history_pos
= 0;
4931 target
->trace_info
->trace_history_overflowed
= 0;
4933 target
->dbgmsg
= NULL
;
4934 target
->dbg_msg_enabled
= 0;
4936 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4938 target
->rtos
= NULL
;
4939 target
->rtos_auto_detect
= false;
4941 /* Do the rest as "configure" options */
4942 goi
->isconfigure
= 1;
4943 e
= target_configure(goi
, target
);
4945 if (target
->tap
== NULL
) {
4946 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
4956 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
4957 /* default endian to little if not specified */
4958 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4961 /* incase variant is not set */
4962 if (!target
->variant
)
4963 target
->variant
= strdup("");
4965 cp
= Jim_GetString(new_cmd
, NULL
);
4966 target
->cmd_name
= strdup(cp
);
4968 /* create the target specific commands */
4969 if (target
->type
->commands
) {
4970 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
4972 LOG_ERROR("unable to register '%s' commands", cp
);
4974 if (target
->type
->target_create
)
4975 (*(target
->type
->target_create
))(target
, goi
->interp
);
4977 /* append to end of list */
4979 struct target
**tpp
;
4980 tpp
= &(all_targets
);
4982 tpp
= &((*tpp
)->next
);
4986 /* now - create the new target name command */
4987 const const struct command_registration target_subcommands
[] = {
4989 .chain
= target_instance_command_handlers
,
4992 .chain
= target
->type
->commands
,
4994 COMMAND_REGISTRATION_DONE
4996 const const struct command_registration target_commands
[] = {
4999 .mode
= COMMAND_ANY
,
5000 .help
= "target command group",
5002 .chain
= target_subcommands
,
5004 COMMAND_REGISTRATION_DONE
5006 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
5010 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
5012 command_set_handler_data(c
, target
);
5014 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
5017 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5020 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5023 struct command_context
*cmd_ctx
= current_command_context(interp
);
5024 assert(cmd_ctx
!= NULL
);
5026 Jim_SetResultString(interp
, get_current_target(cmd_ctx
)->cmd_name
, -1);
5030 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5033 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5036 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5037 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
5038 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5039 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
5044 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5047 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5050 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5051 struct target
*target
= all_targets
;
5053 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5054 Jim_NewStringObj(interp
, target_name(target
), -1));
5055 target
= target
->next
;
5060 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5063 const char *targetname
;
5065 struct target
*target
= (struct target
*) NULL
;
5066 struct target_list
*head
, *curr
, *new;
5067 curr
= (struct target_list
*) NULL
;
5068 head
= (struct target_list
*) NULL
;
5071 LOG_DEBUG("%d", argc
);
5072 /* argv[1] = target to associate in smp
5073 * argv[2] = target to assoicate in smp
5077 for (i
= 1; i
< argc
; i
++) {
5079 targetname
= Jim_GetString(argv
[i
], &len
);
5080 target
= get_target(targetname
);
5081 LOG_DEBUG("%s ", targetname
);
5083 new = malloc(sizeof(struct target_list
));
5084 new->target
= target
;
5085 new->next
= (struct target_list
*)NULL
;
5086 if (head
== (struct target_list
*)NULL
) {
5095 /* now parse the list of cpu and put the target in smp mode*/
5098 while (curr
!= (struct target_list
*)NULL
) {
5099 target
= curr
->target
;
5101 target
->head
= head
;
5105 retval
= rtos_smp_init(head
->target
);
5110 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5113 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5115 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5116 "<name> <target_type> [<target_options> ...]");
5119 return target_create(&goi
);
5122 static int jim_target_number(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5125 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5127 /* It's OK to remove this mechanism sometime after August 2010 or so */
5128 LOG_WARNING("don't use numbers as target identifiers; use names");
5129 if (goi
.argc
!= 1) {
5130 Jim_SetResultFormatted(goi
.interp
, "usage: target number <number>");
5134 int e
= Jim_GetOpt_Wide(&goi
, &w
);
5138 struct target
*target
;
5139 for (target
= all_targets
; NULL
!= target
; target
= target
->next
) {
5140 if (target
->target_number
!= w
)
5143 Jim_SetResultString(goi
.interp
, target_name(target
), -1);
5147 Jim_Obj
*wObj
= Jim_NewIntObj(goi
.interp
, w
);
5148 Jim_SetResultFormatted(goi
.interp
,
5149 "Target: number %#s does not exist", wObj
);
5150 Jim_FreeNewObj(interp
, wObj
);
5155 static int jim_target_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5158 Jim_WrongNumArgs(interp
, 1, argv
, "<no parameters>");
5162 struct target
*target
= all_targets
;
5163 while (NULL
!= target
) {
5164 target
= target
->next
;
5167 Jim_SetResult(interp
, Jim_NewIntObj(interp
, count
));
5171 static const struct command_registration target_subcommand_handlers
[] = {
5174 .mode
= COMMAND_CONFIG
,
5175 .handler
= handle_target_init_command
,
5176 .help
= "initialize targets",
5180 /* REVISIT this should be COMMAND_CONFIG ... */
5181 .mode
= COMMAND_ANY
,
5182 .jim_handler
= jim_target_create
,
5183 .usage
= "name type '-chain-position' name [options ...]",
5184 .help
= "Creates and selects a new target",
5188 .mode
= COMMAND_ANY
,
5189 .jim_handler
= jim_target_current
,
5190 .help
= "Returns the currently selected target",
5194 .mode
= COMMAND_ANY
,
5195 .jim_handler
= jim_target_types
,
5196 .help
= "Returns the available target types as "
5197 "a list of strings",
5201 .mode
= COMMAND_ANY
,
5202 .jim_handler
= jim_target_names
,
5203 .help
= "Returns the names of all targets as a list of strings",
5207 .mode
= COMMAND_ANY
,
5208 .jim_handler
= jim_target_number
,
5210 .help
= "Returns the name of the numbered target "
5215 .mode
= COMMAND_ANY
,
5216 .jim_handler
= jim_target_count
,
5217 .help
= "Returns the number of targets as an integer "
5222 .mode
= COMMAND_ANY
,
5223 .jim_handler
= jim_target_smp
,
5224 .usage
= "targetname1 targetname2 ...",
5225 .help
= "gather several target in a smp list"
5228 COMMAND_REGISTRATION_DONE
5238 static int fastload_num
;
5239 static struct FastLoad
*fastload
;
5241 static void free_fastload(void)
5243 if (fastload
!= NULL
) {
5245 for (i
= 0; i
< fastload_num
; i
++) {
5246 if (fastload
[i
].data
)
5247 free(fastload
[i
].data
);
5254 COMMAND_HANDLER(handle_fast_load_image_command
)
5258 uint32_t image_size
;
5259 uint32_t min_address
= 0;
5260 uint32_t max_address
= 0xffffffff;
5265 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5266 &image
, &min_address
, &max_address
);
5267 if (ERROR_OK
!= retval
)
5270 struct duration bench
;
5271 duration_start(&bench
);
5273 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5274 if (retval
!= ERROR_OK
)
5279 fastload_num
= image
.num_sections
;
5280 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5281 if (fastload
== NULL
) {
5282 command_print(CMD_CTX
, "out of memory");
5283 image_close(&image
);
5286 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5287 for (i
= 0; i
< image
.num_sections
; i
++) {
5288 buffer
= malloc(image
.sections
[i
].size
);
5289 if (buffer
== NULL
) {
5290 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5291 (int)(image
.sections
[i
].size
));
5292 retval
= ERROR_FAIL
;
5296 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5297 if (retval
!= ERROR_OK
) {
5302 uint32_t offset
= 0;
5303 uint32_t length
= buf_cnt
;
5305 /* DANGER!!! beware of unsigned comparision here!!! */
5307 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5308 (image
.sections
[i
].base_address
< max_address
)) {
5309 if (image
.sections
[i
].base_address
< min_address
) {
5310 /* clip addresses below */
5311 offset
+= min_address
-image
.sections
[i
].base_address
;
5315 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5316 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5318 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5319 fastload
[i
].data
= malloc(length
);
5320 if (fastload
[i
].data
== NULL
) {
5322 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5324 retval
= ERROR_FAIL
;
5327 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5328 fastload
[i
].length
= length
;
5330 image_size
+= length
;
5331 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
5332 (unsigned int)length
,
5333 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5339 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
5340 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
5341 "in %fs (%0.3f KiB/s)", image_size
,
5342 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5344 command_print(CMD_CTX
,
5345 "WARNING: image has not been loaded to target!"
5346 "You can issue a 'fast_load' to finish loading.");
5349 image_close(&image
);
5351 if (retval
!= ERROR_OK
)
5357 COMMAND_HANDLER(handle_fast_load_command
)
5360 return ERROR_COMMAND_SYNTAX_ERROR
;
5361 if (fastload
== NULL
) {
5362 LOG_ERROR("No image in memory");
5366 int ms
= timeval_ms();
5368 int retval
= ERROR_OK
;
5369 for (i
= 0; i
< fastload_num
; i
++) {
5370 struct target
*target
= get_current_target(CMD_CTX
);
5371 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
5372 (unsigned int)(fastload
[i
].address
),
5373 (unsigned int)(fastload
[i
].length
));
5374 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5375 if (retval
!= ERROR_OK
)
5377 size
+= fastload
[i
].length
;
5379 if (retval
== ERROR_OK
) {
5380 int after
= timeval_ms();
5381 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5386 static const struct command_registration target_command_handlers
[] = {
5389 .handler
= handle_targets_command
,
5390 .mode
= COMMAND_ANY
,
5391 .help
= "change current default target (one parameter) "
5392 "or prints table of all targets (no parameters)",
5393 .usage
= "[target]",
5397 .mode
= COMMAND_CONFIG
,
5398 .help
= "configure target",
5400 .chain
= target_subcommand_handlers
,
5402 COMMAND_REGISTRATION_DONE
5405 int target_register_commands(struct command_context
*cmd_ctx
)
5407 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5410 static bool target_reset_nag
= true;
5412 bool get_target_reset_nag(void)
5414 return target_reset_nag
;
5417 COMMAND_HANDLER(handle_target_reset_nag
)
5419 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5420 &target_reset_nag
, "Nag after each reset about options to improve "
5424 COMMAND_HANDLER(handle_ps_command
)
5426 struct target
*target
= get_current_target(CMD_CTX
);
5428 if (target
->state
!= TARGET_HALTED
) {
5429 LOG_INFO("target not halted !!");
5433 if ((target
->rtos
) && (target
->rtos
->type
)
5434 && (target
->rtos
->type
->ps_command
)) {
5435 display
= target
->rtos
->type
->ps_command(target
);
5436 command_print(CMD_CTX
, "%s", display
);
5441 return ERROR_TARGET_FAILURE
;
5445 static const struct command_registration target_exec_command_handlers
[] = {
5447 .name
= "fast_load_image",
5448 .handler
= handle_fast_load_image_command
,
5449 .mode
= COMMAND_ANY
,
5450 .help
= "Load image into server memory for later use by "
5451 "fast_load; primarily for profiling",
5452 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5453 "[min_address [max_length]]",
5456 .name
= "fast_load",
5457 .handler
= handle_fast_load_command
,
5458 .mode
= COMMAND_EXEC
,
5459 .help
= "loads active fast load image to current target "
5460 "- mainly for profiling purposes",
5465 .handler
= handle_profile_command
,
5466 .mode
= COMMAND_EXEC
,
5467 .usage
= "seconds filename",
5468 .help
= "profiling samples the CPU PC",
5470 /** @todo don't register virt2phys() unless target supports it */
5472 .name
= "virt2phys",
5473 .handler
= handle_virt2phys_command
,
5474 .mode
= COMMAND_ANY
,
5475 .help
= "translate a virtual address into a physical address",
5476 .usage
= "virtual_address",
5480 .handler
= handle_reg_command
,
5481 .mode
= COMMAND_EXEC
,
5482 .help
= "display or set a register; with no arguments, "
5483 "displays all registers and their values",
5484 .usage
= "[(register_name|register_number) [value]]",
5488 .handler
= handle_poll_command
,
5489 .mode
= COMMAND_EXEC
,
5490 .help
= "poll target state; or reconfigure background polling",
5491 .usage
= "['on'|'off']",
5494 .name
= "wait_halt",
5495 .handler
= handle_wait_halt_command
,
5496 .mode
= COMMAND_EXEC
,
5497 .help
= "wait up to the specified number of milliseconds "
5498 "(default 5) for a previously requested halt",
5499 .usage
= "[milliseconds]",
5503 .handler
= handle_halt_command
,
5504 .mode
= COMMAND_EXEC
,
5505 .help
= "request target to halt, then wait up to the specified"
5506 "number of milliseconds (default 5) for it to complete",
5507 .usage
= "[milliseconds]",
5511 .handler
= handle_resume_command
,
5512 .mode
= COMMAND_EXEC
,
5513 .help
= "resume target execution from current PC or address",
5514 .usage
= "[address]",
5518 .handler
= handle_reset_command
,
5519 .mode
= COMMAND_EXEC
,
5520 .usage
= "[run|halt|init]",
5521 .help
= "Reset all targets into the specified mode."
5522 "Default reset mode is run, if not given.",
5525 .name
= "soft_reset_halt",
5526 .handler
= handle_soft_reset_halt_command
,
5527 .mode
= COMMAND_EXEC
,
5529 .help
= "halt the target and do a soft reset",
5533 .handler
= handle_step_command
,
5534 .mode
= COMMAND_EXEC
,
5535 .help
= "step one instruction from current PC or address",
5536 .usage
= "[address]",
5540 .handler
= handle_md_command
,
5541 .mode
= COMMAND_EXEC
,
5542 .help
= "display memory words",
5543 .usage
= "['phys'] address [count]",
5547 .handler
= handle_md_command
,
5548 .mode
= COMMAND_EXEC
,
5549 .help
= "display memory half-words",
5550 .usage
= "['phys'] address [count]",
5554 .handler
= handle_md_command
,
5555 .mode
= COMMAND_EXEC
,
5556 .help
= "display memory bytes",
5557 .usage
= "['phys'] address [count]",
5561 .handler
= handle_mw_command
,
5562 .mode
= COMMAND_EXEC
,
5563 .help
= "write memory word",
5564 .usage
= "['phys'] address value [count]",
5568 .handler
= handle_mw_command
,
5569 .mode
= COMMAND_EXEC
,
5570 .help
= "write memory half-word",
5571 .usage
= "['phys'] address value [count]",
5575 .handler
= handle_mw_command
,
5576 .mode
= COMMAND_EXEC
,
5577 .help
= "write memory byte",
5578 .usage
= "['phys'] address value [count]",
5582 .handler
= handle_bp_command
,
5583 .mode
= COMMAND_EXEC
,
5584 .help
= "list or set hardware or software breakpoint",
5585 .usage
= "<address> [<asid>]<length> ['hw'|'hw_ctx']",
5589 .handler
= handle_rbp_command
,
5590 .mode
= COMMAND_EXEC
,
5591 .help
= "remove breakpoint",
5596 .handler
= handle_wp_command
,
5597 .mode
= COMMAND_EXEC
,
5598 .help
= "list (no params) or create watchpoints",
5599 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
5603 .handler
= handle_rwp_command
,
5604 .mode
= COMMAND_EXEC
,
5605 .help
= "remove watchpoint",
5609 .name
= "load_image",
5610 .handler
= handle_load_image_command
,
5611 .mode
= COMMAND_EXEC
,
5612 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5613 "[min_address] [max_length]",
5616 .name
= "dump_image",
5617 .handler
= handle_dump_image_command
,
5618 .mode
= COMMAND_EXEC
,
5619 .usage
= "filename address size",
5622 .name
= "verify_image",
5623 .handler
= handle_verify_image_command
,
5624 .mode
= COMMAND_EXEC
,
5625 .usage
= "filename [offset [type]]",
5628 .name
= "test_image",
5629 .handler
= handle_test_image_command
,
5630 .mode
= COMMAND_EXEC
,
5631 .usage
= "filename [offset [type]]",
5634 .name
= "mem2array",
5635 .mode
= COMMAND_EXEC
,
5636 .jim_handler
= jim_mem2array
,
5637 .help
= "read 8/16/32 bit memory and return as a TCL array "
5638 "for script processing",
5639 .usage
= "arrayname bitwidth address count",
5642 .name
= "array2mem",
5643 .mode
= COMMAND_EXEC
,
5644 .jim_handler
= jim_array2mem
,
5645 .help
= "convert a TCL array to memory locations "
5646 "and write the 8/16/32 bit values",
5647 .usage
= "arrayname bitwidth address count",
5650 .name
= "reset_nag",
5651 .handler
= handle_target_reset_nag
,
5652 .mode
= COMMAND_ANY
,
5653 .help
= "Nag after each reset about options that could have been "
5654 "enabled to improve performance. ",
5655 .usage
= "['enable'|'disable']",
5659 .handler
= handle_ps_command
,
5660 .mode
= COMMAND_EXEC
,
5661 .help
= "list all tasks ",
5665 COMMAND_REGISTRATION_DONE
5667 static int target_register_user_commands(struct command_context
*cmd_ctx
)
5669 int retval
= ERROR_OK
;
5670 retval
= target_request_register_commands(cmd_ctx
);
5671 if (retval
!= ERROR_OK
)
5674 retval
= trace_register_commands(cmd_ctx
);
5675 if (retval
!= ERROR_OK
)
5679 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);