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 * This program is free software; you can redistribute it and/or modify *
24 * it under the terms of the GNU General Public License as published by *
25 * the Free Software Foundation; either version 2 of the License, or *
26 * (at your option) any later version. *
28 * This program is distributed in the hope that it will be useful, *
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
31 * GNU General Public License for more details. *
33 * You should have received a copy of the GNU General Public License *
34 * along with this program; if not, write to the *
35 * Free Software Foundation, Inc., *
36 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
37 ***************************************************************************/
42 #include <helper/time_support.h>
43 #include <jtag/jtag.h>
44 #include <flash/nor/core.h>
47 #include "target_type.h"
48 #include "target_request.h"
49 #include "breakpoints.h"
53 #include "rtos/rtos.h"
56 static int target_read_buffer_default(struct target
*target
, uint32_t address
,
57 uint32_t size
, uint8_t *buffer
);
58 static int target_write_buffer_default(struct target
*target
, uint32_t address
,
59 uint32_t size
, const uint8_t *buffer
);
60 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
61 int argc
, Jim_Obj
*const *argv
);
62 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
63 int argc
, Jim_Obj
*const *argv
);
64 static int target_register_user_commands(struct command_context
*cmd_ctx
);
67 extern struct target_type arm7tdmi_target
;
68 extern struct target_type arm720t_target
;
69 extern struct target_type arm9tdmi_target
;
70 extern struct target_type arm920t_target
;
71 extern struct target_type arm966e_target
;
72 extern struct target_type arm946e_target
;
73 extern struct target_type arm926ejs_target
;
74 extern struct target_type fa526_target
;
75 extern struct target_type feroceon_target
;
76 extern struct target_type dragonite_target
;
77 extern struct target_type xscale_target
;
78 extern struct target_type cortexm3_target
;
79 extern struct target_type cortexa8_target
;
80 extern struct target_type arm11_target
;
81 extern struct target_type mips_m4k_target
;
82 extern struct target_type avr_target
;
83 extern struct target_type dsp563xx_target
;
84 extern struct target_type dsp5680xx_target
;
85 extern struct target_type testee_target
;
86 extern struct target_type avr32_ap7k_target
;
87 extern struct target_type stm32_stlink_target
;
89 static struct target_type
*target_types
[] =
111 &stm32_stlink_target
,
115 struct target
*all_targets
= NULL
;
116 static struct target_event_callback
*target_event_callbacks
= NULL
;
117 static struct target_timer_callback
*target_timer_callbacks
= NULL
;
118 static const int polling_interval
= 100;
120 static const Jim_Nvp nvp_assert
[] = {
121 { .name
= "assert", NVP_ASSERT
},
122 { .name
= "deassert", NVP_DEASSERT
},
123 { .name
= "T", NVP_ASSERT
},
124 { .name
= "F", NVP_DEASSERT
},
125 { .name
= "t", NVP_ASSERT
},
126 { .name
= "f", NVP_DEASSERT
},
127 { .name
= NULL
, .value
= -1 }
130 static const Jim_Nvp nvp_error_target
[] = {
131 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
132 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
133 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
134 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
135 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
136 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
137 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
138 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
139 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
140 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
141 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
142 { .value
= -1, .name
= NULL
}
145 static const char *target_strerror_safe(int err
)
149 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
150 if (n
->name
== NULL
) {
157 static const Jim_Nvp nvp_target_event
[] = {
158 { .value
= TARGET_EVENT_OLD_gdb_program_config
, .name
= "old-gdb_program_config" },
159 { .value
= TARGET_EVENT_OLD_pre_resume
, .name
= "old-pre_resume" },
161 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
162 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
163 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
164 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
165 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
167 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
168 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
170 /* historical name */
172 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
174 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
175 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
176 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
177 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
178 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
179 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
180 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
181 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
182 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
183 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
184 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
186 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
187 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
189 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
190 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
192 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
193 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
195 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
196 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
198 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
199 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
201 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
202 { .value
= TARGET_EVENT_RESUMED
, .name
= "resume-ok" },
203 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
205 { .name
= NULL
, .value
= -1 }
208 static const Jim_Nvp nvp_target_state
[] = {
209 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
210 { .name
= "running", .value
= TARGET_RUNNING
},
211 { .name
= "halted", .value
= TARGET_HALTED
},
212 { .name
= "reset", .value
= TARGET_RESET
},
213 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
214 { .name
= NULL
, .value
= -1 },
217 static const Jim_Nvp nvp_target_debug_reason
[] = {
218 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
219 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
220 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
221 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
222 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
223 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
224 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
225 { .name
= NULL
, .value
= -1 },
228 static const Jim_Nvp nvp_target_endian
[] = {
229 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
230 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
231 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
232 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
233 { .name
= NULL
, .value
= -1 },
236 static const Jim_Nvp nvp_reset_modes
[] = {
237 { .name
= "unknown", .value
= RESET_UNKNOWN
},
238 { .name
= "run" , .value
= RESET_RUN
},
239 { .name
= "halt" , .value
= RESET_HALT
},
240 { .name
= "init" , .value
= RESET_INIT
},
241 { .name
= NULL
, .value
= -1 },
244 const char *debug_reason_name(struct target
*t
)
248 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
249 t
->debug_reason
)->name
;
251 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
252 cp
= "(*BUG*unknown*BUG*)";
258 target_state_name( struct target
*t
)
261 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
263 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
264 cp
= "(*BUG*unknown*BUG*)";
269 /* determine the number of the new target */
270 static int new_target_number(void)
275 /* number is 0 based */
279 if (x
< t
->target_number
) {
280 x
= t
->target_number
;
287 /* read a uint32_t from a buffer in target memory endianness */
288 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
290 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
291 return le_to_h_u32(buffer
);
293 return be_to_h_u32(buffer
);
296 /* read a uint24_t from a buffer in target memory endianness */
297 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
299 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
300 return le_to_h_u24(buffer
);
302 return be_to_h_u24(buffer
);
305 /* read a uint16_t from a buffer in target memory endianness */
306 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
308 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
309 return le_to_h_u16(buffer
);
311 return be_to_h_u16(buffer
);
314 /* read a uint8_t from a buffer in target memory endianness */
315 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
317 return *buffer
& 0x0ff;
320 /* write a uint32_t to a buffer in target memory endianness */
321 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
323 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
324 h_u32_to_le(buffer
, value
);
326 h_u32_to_be(buffer
, value
);
329 /* write a uint24_t to a buffer in target memory endianness */
330 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
332 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
333 h_u24_to_le(buffer
, value
);
335 h_u24_to_be(buffer
, value
);
338 /* write a uint16_t to a buffer in target memory endianness */
339 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
341 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
342 h_u16_to_le(buffer
, value
);
344 h_u16_to_be(buffer
, value
);
347 /* write a uint8_t to a buffer in target memory endianness */
348 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
353 /* write a uint32_t array to a buffer in target memory endianness */
354 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
357 for(i
= 0; i
< count
; i
++)
358 dstbuf
[i
] = target_buffer_get_u32(target
,&buffer
[i
*4]);
361 /* write a uint16_t array to a buffer in target memory endianness */
362 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
365 for(i
= 0; i
< count
; i
++)
366 dstbuf
[i
] = target_buffer_get_u16(target
,&buffer
[i
*2]);
369 /* write a uint32_t array to a buffer in target memory endianness */
370 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, uint32_t *srcbuf
)
373 for(i
= 0; i
< count
; i
++)
374 target_buffer_set_u32(target
,&buffer
[i
*4],srcbuf
[i
]);
377 /* write a uint16_t array to a buffer in target memory endianness */
378 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, uint16_t *srcbuf
)
381 for(i
= 0; i
< count
; i
++)
382 target_buffer_set_u16(target
,&buffer
[i
*2],srcbuf
[i
]);
385 /* return a pointer to a configured target; id is name or number */
386 struct target
*get_target(const char *id
)
388 struct target
*target
;
390 /* try as tcltarget name */
391 for (target
= all_targets
; target
; target
= target
->next
) {
392 if (target
->cmd_name
== NULL
)
394 if (strcmp(id
, target
->cmd_name
) == 0)
398 /* It's OK to remove this fallback sometime after August 2010 or so */
400 /* no match, try as number */
402 if (parse_uint(id
, &num
) != ERROR_OK
)
405 for (target
= all_targets
; target
; target
= target
->next
) {
406 if (target
->target_number
== (int)num
) {
407 LOG_WARNING("use '%s' as target identifier, not '%u'",
408 target
->cmd_name
, num
);
416 /* returns a pointer to the n-th configured target */
417 static struct target
*get_target_by_num(int num
)
419 struct target
*target
= all_targets
;
422 if (target
->target_number
== num
) {
425 target
= target
->next
;
431 struct target
* get_current_target(struct command_context
*cmd_ctx
)
433 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
437 LOG_ERROR("BUG: current_target out of bounds");
444 int target_poll(struct target
*target
)
448 /* We can't poll until after examine */
449 if (!target_was_examined(target
))
451 /* Fail silently lest we pollute the log */
455 retval
= target
->type
->poll(target
);
456 if (retval
!= ERROR_OK
)
459 if (target
->halt_issued
)
461 if (target
->state
== TARGET_HALTED
)
463 target
->halt_issued
= false;
466 long long t
= timeval_ms() - target
->halt_issued_time
;
469 target
->halt_issued
= false;
470 LOG_INFO("Halt timed out, wake up GDB.");
471 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
479 int target_halt(struct target
*target
)
482 /* We can't poll until after examine */
483 if (!target_was_examined(target
))
485 LOG_ERROR("Target not examined yet");
489 retval
= target
->type
->halt(target
);
490 if (retval
!= ERROR_OK
)
493 target
->halt_issued
= true;
494 target
->halt_issued_time
= timeval_ms();
500 * Make the target (re)start executing using its saved execution
501 * context (possibly with some modifications).
503 * @param target Which target should start executing.
504 * @param current True to use the target's saved program counter instead
505 * of the address parameter
506 * @param address Optionally used as the program counter.
507 * @param handle_breakpoints True iff breakpoints at the resumption PC
508 * should be skipped. (For example, maybe execution was stopped by
509 * such a breakpoint, in which case it would be counterprodutive to
511 * @param debug_execution False if all working areas allocated by OpenOCD
512 * should be released and/or restored to their original contents.
513 * (This would for example be true to run some downloaded "helper"
514 * algorithm code, which resides in one such working buffer and uses
515 * another for data storage.)
517 * @todo Resolve the ambiguity about what the "debug_execution" flag
518 * signifies. For example, Target implementations don't agree on how
519 * it relates to invalidation of the register cache, or to whether
520 * breakpoints and watchpoints should be enabled. (It would seem wrong
521 * to enable breakpoints when running downloaded "helper" algorithms
522 * (debug_execution true), since the breakpoints would be set to match
523 * target firmware being debugged, not the helper algorithm.... and
524 * enabling them could cause such helpers to malfunction (for example,
525 * by overwriting data with a breakpoint instruction. On the other
526 * hand the infrastructure for running such helpers might use this
527 * procedure but rely on hardware breakpoint to detect termination.)
529 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
533 /* We can't poll until after examine */
534 if (!target_was_examined(target
))
536 LOG_ERROR("Target not examined yet");
540 /* note that resume *must* be asynchronous. The CPU can halt before
541 * we poll. The CPU can even halt at the current PC as a result of
542 * a software breakpoint being inserted by (a bug?) the application.
544 if ((retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
)) != ERROR_OK
)
550 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
555 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
556 if (n
->name
== NULL
) {
557 LOG_ERROR("invalid reset mode");
561 /* disable polling during reset to make reset event scripts
562 * more predictable, i.e. dr/irscan & pathmove in events will
563 * not have JTAG operations injected into the middle of a sequence.
565 bool save_poll
= jtag_poll_get_enabled();
567 jtag_poll_set_enabled(false);
569 sprintf(buf
, "ocd_process_reset %s", n
->name
);
570 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
572 jtag_poll_set_enabled(save_poll
);
574 if (retval
!= JIM_OK
) {
575 Jim_MakeErrorMessage(cmd_ctx
->interp
);
576 command_print(NULL
,"%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
580 /* We want any events to be processed before the prompt */
581 retval
= target_call_timer_callbacks_now();
583 struct target
*target
;
584 for (target
= all_targets
; target
; target
= target
->next
) {
585 target
->type
->check_reset(target
);
591 static int identity_virt2phys(struct target
*target
,
592 uint32_t virtual, uint32_t *physical
)
598 static int no_mmu(struct target
*target
, int *enabled
)
604 static int default_examine(struct target
*target
)
606 target_set_examined(target
);
610 /* no check by default */
611 static int default_check_reset(struct target
*target
)
616 int target_examine_one(struct target
*target
)
618 return target
->type
->examine(target
);
621 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
623 struct target
*target
= priv
;
625 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
628 jtag_unregister_event_callback(jtag_enable_callback
, target
);
629 return target_examine_one(target
);
633 /* Targets that correctly implement init + examine, i.e.
634 * no communication with target during init:
638 int target_examine(void)
640 int retval
= ERROR_OK
;
641 struct target
*target
;
643 for (target
= all_targets
; target
; target
= target
->next
)
645 /* defer examination, but don't skip it */
646 if (!target
->tap
->enabled
) {
647 jtag_register_event_callback(jtag_enable_callback
,
651 if ((retval
= target_examine_one(target
)) != ERROR_OK
)
656 const char *target_type_name(struct target
*target
)
658 return target
->type
->name
;
661 static int target_write_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
663 if (!target_was_examined(target
))
665 LOG_ERROR("Target not examined yet");
668 return target
->type
->write_memory_imp(target
, address
, size
, count
, buffer
);
671 static int target_read_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
673 if (!target_was_examined(target
))
675 LOG_ERROR("Target not examined yet");
678 return target
->type
->read_memory_imp(target
, address
, size
, count
, buffer
);
681 static int target_soft_reset_halt_imp(struct target
*target
)
683 if (!target_was_examined(target
))
685 LOG_ERROR("Target not examined yet");
688 if (!target
->type
->soft_reset_halt_imp
) {
689 LOG_ERROR("Target %s does not support soft_reset_halt",
690 target_name(target
));
693 return target
->type
->soft_reset_halt_imp(target
);
697 * Downloads a target-specific native code algorithm to the target,
698 * and executes it. * Note that some targets may need to set up, enable,
699 * and tear down a breakpoint (hard or * soft) to detect algorithm
700 * termination, while others may support lower overhead schemes where
701 * soft breakpoints embedded in the algorithm automatically terminate the
704 * @param target used to run the algorithm
705 * @param arch_info target-specific description of the algorithm.
707 int target_run_algorithm(struct target
*target
,
708 int num_mem_params
, struct mem_param
*mem_params
,
709 int num_reg_params
, struct reg_param
*reg_param
,
710 uint32_t entry_point
, uint32_t exit_point
,
711 int timeout_ms
, void *arch_info
)
713 int retval
= ERROR_FAIL
;
715 if (!target_was_examined(target
))
717 LOG_ERROR("Target not examined yet");
720 if (!target
->type
->run_algorithm
) {
721 LOG_ERROR("Target type '%s' does not support %s",
722 target_type_name(target
), __func__
);
726 target
->running_alg
= true;
727 retval
= target
->type
->run_algorithm(target
,
728 num_mem_params
, mem_params
,
729 num_reg_params
, reg_param
,
730 entry_point
, exit_point
, timeout_ms
, arch_info
);
731 target
->running_alg
= false;
738 * Downloads a target-specific native code algorithm to the target,
739 * executes and leaves it running.
741 * @param target used to run the algorithm
742 * @param arch_info target-specific description of the algorithm.
744 int target_start_algorithm(struct target
*target
,
745 int num_mem_params
, struct mem_param
*mem_params
,
746 int num_reg_params
, struct reg_param
*reg_params
,
747 uint32_t entry_point
, uint32_t exit_point
,
750 int retval
= ERROR_FAIL
;
752 if (!target_was_examined(target
))
754 LOG_ERROR("Target not examined yet");
757 if (!target
->type
->start_algorithm
) {
758 LOG_ERROR("Target type '%s' does not support %s",
759 target_type_name(target
), __func__
);
762 if (target
->running_alg
) {
763 LOG_ERROR("Target is already running an algorithm");
767 target
->running_alg
= true;
768 retval
= target
->type
->start_algorithm(target
,
769 num_mem_params
, mem_params
,
770 num_reg_params
, reg_params
,
771 entry_point
, exit_point
, arch_info
);
778 * Waits for an algorithm started with target_start_algorithm() to complete.
780 * @param target used to run the algorithm
781 * @param arch_info target-specific description of the algorithm.
783 int target_wait_algorithm(struct target
*target
,
784 int num_mem_params
, struct mem_param
*mem_params
,
785 int num_reg_params
, struct reg_param
*reg_params
,
786 uint32_t exit_point
, int timeout_ms
,
789 int retval
= ERROR_FAIL
;
791 if (!target
->type
->wait_algorithm
) {
792 LOG_ERROR("Target type '%s' does not support %s",
793 target_type_name(target
), __func__
);
796 if (!target
->running_alg
) {
797 LOG_ERROR("Target is not running an algorithm");
801 retval
= target
->type
->wait_algorithm(target
,
802 num_mem_params
, mem_params
,
803 num_reg_params
, reg_params
,
804 exit_point
, timeout_ms
, arch_info
);
805 if (retval
!= ERROR_TARGET_TIMEOUT
)
806 target
->running_alg
= false;
813 int target_read_memory(struct target
*target
,
814 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
816 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
819 static int target_read_phys_memory(struct target
*target
,
820 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
822 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
825 int target_write_memory(struct target
*target
,
826 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
828 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
831 static int target_write_phys_memory(struct target
*target
,
832 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
834 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
837 int target_bulk_write_memory(struct target
*target
,
838 uint32_t address
, uint32_t count
, const uint8_t *buffer
)
840 return target
->type
->bulk_write_memory(target
, address
, count
, buffer
);
843 int target_add_breakpoint(struct target
*target
,
844 struct breakpoint
*breakpoint
)
846 if ((target
->state
!= TARGET_HALTED
)&&(breakpoint
->type
!=BKPT_HARD
)) {
847 LOG_WARNING("target %s is not halted", target
->cmd_name
);
848 return ERROR_TARGET_NOT_HALTED
;
850 return target
->type
->add_breakpoint(target
, breakpoint
);
853 int target_add_context_breakpoint(struct target
*target
,
854 struct breakpoint
*breakpoint
)
856 if (target
->state
!= TARGET_HALTED
) {
857 LOG_WARNING("target %s is not halted", target
->cmd_name
);
858 return ERROR_TARGET_NOT_HALTED
;
860 return target
->type
->add_context_breakpoint(target
, breakpoint
);
863 int target_add_hybrid_breakpoint(struct target
*target
,
864 struct breakpoint
*breakpoint
)
866 if (target
->state
!= TARGET_HALTED
) {
867 LOG_WARNING("target %s is not halted", target
->cmd_name
);
868 return ERROR_TARGET_NOT_HALTED
;
870 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
873 int target_remove_breakpoint(struct target
*target
,
874 struct breakpoint
*breakpoint
)
876 return target
->type
->remove_breakpoint(target
, breakpoint
);
879 int target_add_watchpoint(struct target
*target
,
880 struct watchpoint
*watchpoint
)
882 if (target
->state
!= TARGET_HALTED
) {
883 LOG_WARNING("target %s is not halted", target
->cmd_name
);
884 return ERROR_TARGET_NOT_HALTED
;
886 return target
->type
->add_watchpoint(target
, watchpoint
);
888 int target_remove_watchpoint(struct target
*target
,
889 struct watchpoint
*watchpoint
)
891 return target
->type
->remove_watchpoint(target
, watchpoint
);
894 int target_get_gdb_reg_list(struct target
*target
,
895 struct reg
**reg_list
[], int *reg_list_size
)
897 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
);
899 int target_step(struct target
*target
,
900 int current
, uint32_t address
, int handle_breakpoints
)
902 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
907 * Reset the @c examined flag for the given target.
908 * Pure paranoia -- targets are zeroed on allocation.
910 static void target_reset_examined(struct target
*target
)
912 target
->examined
= false;
916 err_read_phys_memory(struct target
*target
, uint32_t address
,
917 uint32_t size
, uint32_t count
, uint8_t *buffer
)
919 LOG_ERROR("Not implemented: %s", __func__
);
924 err_write_phys_memory(struct target
*target
, uint32_t address
,
925 uint32_t size
, uint32_t count
, const uint8_t *buffer
)
927 LOG_ERROR("Not implemented: %s", __func__
);
931 static int handle_target(void *priv
);
933 static int target_init_one(struct command_context
*cmd_ctx
,
934 struct target
*target
)
936 target_reset_examined(target
);
938 struct target_type
*type
= target
->type
;
939 if (type
->examine
== NULL
)
940 type
->examine
= default_examine
;
942 if (type
->check_reset
== NULL
)
943 type
->check_reset
= default_check_reset
;
945 assert(type
->init_target
!= NULL
);
947 int retval
= type
->init_target(cmd_ctx
, target
);
948 if (ERROR_OK
!= retval
)
950 LOG_ERROR("target '%s' init failed", target_name(target
));
955 * @todo get rid of those *memory_imp() methods, now that all
956 * callers are using target_*_memory() accessors ... and make
957 * sure the "physical" paths handle the same issues.
959 /* a non-invasive way(in terms of patches) to add some code that
960 * runs before the type->write/read_memory implementation
962 type
->write_memory_imp
= target
->type
->write_memory
;
963 type
->write_memory
= target_write_memory_imp
;
965 type
->read_memory_imp
= target
->type
->read_memory
;
966 type
->read_memory
= target_read_memory_imp
;
968 type
->soft_reset_halt_imp
= target
->type
->soft_reset_halt
;
969 type
->soft_reset_halt
= target_soft_reset_halt_imp
;
971 /* Sanity-check MMU support ... stub in what we must, to help
972 * implement it in stages, but warn if we need to do so.
976 if (type
->write_phys_memory
== NULL
)
978 LOG_ERROR("type '%s' is missing write_phys_memory",
980 type
->write_phys_memory
= err_write_phys_memory
;
982 if (type
->read_phys_memory
== NULL
)
984 LOG_ERROR("type '%s' is missing read_phys_memory",
986 type
->read_phys_memory
= err_read_phys_memory
;
988 if (type
->virt2phys
== NULL
)
990 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
991 type
->virt2phys
= identity_virt2phys
;
996 /* Make sure no-MMU targets all behave the same: make no
997 * distinction between physical and virtual addresses, and
998 * ensure that virt2phys() is always an identity mapping.
1000 if (type
->write_phys_memory
|| type
->read_phys_memory
1003 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1007 type
->write_phys_memory
= type
->write_memory
;
1008 type
->read_phys_memory
= type
->read_memory
;
1009 type
->virt2phys
= identity_virt2phys
;
1012 if (target
->type
->read_buffer
== NULL
)
1013 target
->type
->read_buffer
= target_read_buffer_default
;
1015 if (target
->type
->write_buffer
== NULL
)
1016 target
->type
->write_buffer
= target_write_buffer_default
;
1021 static int target_init(struct command_context
*cmd_ctx
)
1023 struct target
*target
;
1026 for (target
= all_targets
; target
; target
= target
->next
)
1028 retval
= target_init_one(cmd_ctx
, target
);
1029 if (ERROR_OK
!= retval
)
1036 retval
= target_register_user_commands(cmd_ctx
);
1037 if (ERROR_OK
!= retval
)
1040 retval
= target_register_timer_callback(&handle_target
,
1041 polling_interval
, 1, cmd_ctx
->interp
);
1042 if (ERROR_OK
!= retval
)
1048 COMMAND_HANDLER(handle_target_init_command
)
1053 return ERROR_COMMAND_SYNTAX_ERROR
;
1055 static bool target_initialized
= false;
1056 if (target_initialized
)
1058 LOG_INFO("'target init' has already been called");
1061 target_initialized
= true;
1063 retval
= command_run_line(CMD_CTX
, "init_targets");
1064 if (ERROR_OK
!= retval
)
1067 LOG_DEBUG("Initializing targets...");
1068 return target_init(CMD_CTX
);
1071 int target_register_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
1073 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1075 if (callback
== NULL
)
1077 return ERROR_COMMAND_SYNTAX_ERROR
;
1082 while ((*callbacks_p
)->next
)
1083 callbacks_p
= &((*callbacks_p
)->next
);
1084 callbacks_p
= &((*callbacks_p
)->next
);
1087 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1088 (*callbacks_p
)->callback
= callback
;
1089 (*callbacks_p
)->priv
= priv
;
1090 (*callbacks_p
)->next
= NULL
;
1095 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1097 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1100 if (callback
== NULL
)
1102 return ERROR_COMMAND_SYNTAX_ERROR
;
1107 while ((*callbacks_p
)->next
)
1108 callbacks_p
= &((*callbacks_p
)->next
);
1109 callbacks_p
= &((*callbacks_p
)->next
);
1112 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1113 (*callbacks_p
)->callback
= callback
;
1114 (*callbacks_p
)->periodic
= periodic
;
1115 (*callbacks_p
)->time_ms
= time_ms
;
1117 gettimeofday(&now
, NULL
);
1118 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
1119 time_ms
-= (time_ms
% 1000);
1120 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
1121 if ((*callbacks_p
)->when
.tv_usec
> 1000000)
1123 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
1124 (*callbacks_p
)->when
.tv_sec
+= 1;
1127 (*callbacks_p
)->priv
= priv
;
1128 (*callbacks_p
)->next
= NULL
;
1133 int target_unregister_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
1135 struct target_event_callback
**p
= &target_event_callbacks
;
1136 struct target_event_callback
*c
= target_event_callbacks
;
1138 if (callback
== NULL
)
1140 return ERROR_COMMAND_SYNTAX_ERROR
;
1145 struct target_event_callback
*next
= c
->next
;
1146 if ((c
->callback
== callback
) && (c
->priv
== priv
))
1160 static int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1162 struct target_timer_callback
**p
= &target_timer_callbacks
;
1163 struct target_timer_callback
*c
= target_timer_callbacks
;
1165 if (callback
== NULL
)
1167 return ERROR_COMMAND_SYNTAX_ERROR
;
1172 struct target_timer_callback
*next
= c
->next
;
1173 if ((c
->callback
== callback
) && (c
->priv
== priv
))
1187 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1189 struct target_event_callback
*callback
= target_event_callbacks
;
1190 struct target_event_callback
*next_callback
;
1192 if (event
== TARGET_EVENT_HALTED
)
1194 /* execute early halted first */
1195 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1198 LOG_DEBUG("target event %i (%s)",
1200 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1202 target_handle_event(target
, event
);
1206 next_callback
= callback
->next
;
1207 callback
->callback(target
, event
, callback
->priv
);
1208 callback
= next_callback
;
1214 static int target_timer_callback_periodic_restart(
1215 struct target_timer_callback
*cb
, struct timeval
*now
)
1217 int time_ms
= cb
->time_ms
;
1218 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1219 time_ms
-= (time_ms
% 1000);
1220 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1221 if (cb
->when
.tv_usec
> 1000000)
1223 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1224 cb
->when
.tv_sec
+= 1;
1229 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1230 struct timeval
*now
)
1232 cb
->callback(cb
->priv
);
1235 return target_timer_callback_periodic_restart(cb
, now
);
1237 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1240 static int target_call_timer_callbacks_check_time(int checktime
)
1245 gettimeofday(&now
, NULL
);
1247 struct target_timer_callback
*callback
= target_timer_callbacks
;
1250 // cleaning up may unregister and free this callback
1251 struct target_timer_callback
*next_callback
= callback
->next
;
1253 bool call_it
= callback
->callback
&&
1254 ((!checktime
&& callback
->periodic
) ||
1255 now
.tv_sec
> callback
->when
.tv_sec
||
1256 (now
.tv_sec
== callback
->when
.tv_sec
&&
1257 now
.tv_usec
>= callback
->when
.tv_usec
));
1261 int retval
= target_call_timer_callback(callback
, &now
);
1262 if (retval
!= ERROR_OK
)
1266 callback
= next_callback
;
1272 int target_call_timer_callbacks(void)
1274 return target_call_timer_callbacks_check_time(1);
1277 /* invoke periodic callbacks immediately */
1278 int target_call_timer_callbacks_now(void)
1280 return target_call_timer_callbacks_check_time(0);
1283 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1285 struct working_area
*c
= target
->working_areas
;
1286 struct working_area
*new_wa
= NULL
;
1288 /* Reevaluate working area address based on MMU state*/
1289 if (target
->working_areas
== NULL
)
1294 retval
= target
->type
->mmu(target
, &enabled
);
1295 if (retval
!= ERROR_OK
)
1301 if (target
->working_area_phys_spec
) {
1302 LOG_DEBUG("MMU disabled, using physical "
1303 "address for working memory 0x%08x",
1304 (unsigned)target
->working_area_phys
);
1305 target
->working_area
= target
->working_area_phys
;
1307 LOG_ERROR("No working memory available. "
1308 "Specify -work-area-phys to target.");
1309 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1312 if (target
->working_area_virt_spec
) {
1313 LOG_DEBUG("MMU enabled, using virtual "
1314 "address for working memory 0x%08x",
1315 (unsigned)target
->working_area_virt
);
1316 target
->working_area
= target
->working_area_virt
;
1318 LOG_ERROR("No working memory available. "
1319 "Specify -work-area-virt to target.");
1320 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1325 /* only allocate multiples of 4 byte */
1328 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size
)));
1329 size
= (size
+ 3) & (~3);
1332 /* see if there's already a matching working area */
1335 if ((c
->free
) && (c
->size
== size
))
1343 /* if not, allocate a new one */
1346 struct working_area
**p
= &target
->working_areas
;
1347 uint32_t first_free
= target
->working_area
;
1348 uint32_t free_size
= target
->working_area_size
;
1350 c
= target
->working_areas
;
1353 first_free
+= c
->size
;
1354 free_size
-= c
->size
;
1359 if (free_size
< size
)
1361 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1364 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free
);
1366 new_wa
= malloc(sizeof(struct working_area
));
1367 new_wa
->next
= NULL
;
1368 new_wa
->size
= size
;
1369 new_wa
->address
= first_free
;
1371 if (target
->backup_working_area
)
1374 new_wa
->backup
= malloc(new_wa
->size
);
1375 if ((retval
= target_read_memory(target
, new_wa
->address
, 4, new_wa
->size
/ 4, new_wa
->backup
)) != ERROR_OK
)
1377 free(new_wa
->backup
);
1384 new_wa
->backup
= NULL
;
1387 /* put new entry in list */
1391 /* mark as used, and return the new (reused) area */
1392 new_wa
->free
= false;
1396 new_wa
->user
= area
;
1401 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1405 retval
= target_alloc_working_area_try(target
, size
, area
);
1406 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1408 LOG_WARNING("not enough working area available(requested %u)", (unsigned)(size
));
1414 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1419 if (restore
&& target
->backup_working_area
)
1422 if ((retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
)) != ERROR_OK
)
1428 /* mark user pointer invalid */
1435 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1437 return target_free_working_area_restore(target
, area
, 1);
1440 /* free resources and restore memory, if restoring memory fails,
1441 * free up resources anyway
1443 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1445 struct working_area
*c
= target
->working_areas
;
1449 struct working_area
*next
= c
->next
;
1450 target_free_working_area_restore(target
, c
, restore
);
1460 target
->working_areas
= NULL
;
1463 void target_free_all_working_areas(struct target
*target
)
1465 target_free_all_working_areas_restore(target
, 1);
1468 int target_arch_state(struct target
*target
)
1473 LOG_USER("No target has been configured");
1477 LOG_USER("target state: %s", target_state_name( target
));
1479 if (target
->state
!= TARGET_HALTED
)
1482 retval
= target
->type
->arch_state(target
);
1486 /* Single aligned words are guaranteed to use 16 or 32 bit access
1487 * mode respectively, otherwise data is handled as quickly as
1490 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
1492 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1493 (int)size
, (unsigned)address
);
1495 if (!target_was_examined(target
))
1497 LOG_ERROR("Target not examined yet");
1505 if ((address
+ size
- 1) < address
)
1507 /* GDB can request this when e.g. PC is 0xfffffffc*/
1508 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1514 return target
->type
->write_buffer(target
, address
, size
, buffer
);
1517 static int target_write_buffer_default(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
1519 int retval
= ERROR_OK
;
1521 if (((address
% 2) == 0) && (size
== 2))
1523 return target_write_memory(target
, address
, 2, 1, buffer
);
1526 /* handle unaligned head bytes */
1529 uint32_t unaligned
= 4 - (address
% 4);
1531 if (unaligned
> size
)
1534 if ((retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1537 buffer
+= unaligned
;
1538 address
+= unaligned
;
1542 /* handle aligned words */
1545 int aligned
= size
- (size
% 4);
1547 /* use bulk writes above a certain limit. This may have to be changed */
1550 if ((retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
)) != ERROR_OK
)
1555 if ((retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1564 /* handle tail writes of less than 4 bytes */
1567 if ((retval
= target_write_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1574 /* Single aligned words are guaranteed to use 16 or 32 bit access
1575 * mode respectively, otherwise data is handled as quickly as
1578 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1580 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1581 (int)size
, (unsigned)address
);
1583 if (!target_was_examined(target
))
1585 LOG_ERROR("Target not examined yet");
1593 if ((address
+ size
- 1) < address
)
1595 /* GDB can request this when e.g. PC is 0xfffffffc*/
1596 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1602 return target
->type
->read_buffer(target
, address
, size
, buffer
);
1605 static int target_read_buffer_default(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1607 int retval
= ERROR_OK
;
1609 if (((address
% 2) == 0) && (size
== 2))
1611 return target_read_memory(target
, address
, 2, 1, buffer
);
1614 /* handle unaligned head bytes */
1617 uint32_t unaligned
= 4 - (address
% 4);
1619 if (unaligned
> size
)
1622 if ((retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1625 buffer
+= unaligned
;
1626 address
+= unaligned
;
1630 /* handle aligned words */
1633 int aligned
= size
- (size
% 4);
1635 if ((retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1643 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1646 int aligned
= size
- (size
%2);
1647 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1648 if (retval
!= ERROR_OK
)
1655 /* handle tail writes of less than 4 bytes */
1658 if ((retval
= target_read_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1665 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1670 uint32_t checksum
= 0;
1671 if (!target_was_examined(target
))
1673 LOG_ERROR("Target not examined yet");
1677 if ((retval
= target
->type
->checksum_memory(target
, address
,
1678 size
, &checksum
)) != ERROR_OK
)
1680 buffer
= malloc(size
);
1683 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1684 return ERROR_COMMAND_SYNTAX_ERROR
;
1686 retval
= target_read_buffer(target
, address
, size
, buffer
);
1687 if (retval
!= ERROR_OK
)
1693 /* convert to target endianness */
1694 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++)
1696 uint32_t target_data
;
1697 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1698 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1701 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1710 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1713 if (!target_was_examined(target
))
1715 LOG_ERROR("Target not examined yet");
1719 if (target
->type
->blank_check_memory
== 0)
1720 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1722 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1727 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1729 uint8_t value_buf
[4];
1730 if (!target_was_examined(target
))
1732 LOG_ERROR("Target not examined yet");
1736 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1738 if (retval
== ERROR_OK
)
1740 *value
= target_buffer_get_u32(target
, value_buf
);
1741 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1748 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1755 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
1757 uint8_t value_buf
[2];
1758 if (!target_was_examined(target
))
1760 LOG_ERROR("Target not examined yet");
1764 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1766 if (retval
== ERROR_OK
)
1768 *value
= target_buffer_get_u16(target
, value_buf
);
1769 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1776 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1783 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
1785 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1786 if (!target_was_examined(target
))
1788 LOG_ERROR("Target not examined yet");
1792 if (retval
== ERROR_OK
)
1794 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1801 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1808 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
1811 uint8_t value_buf
[4];
1812 if (!target_was_examined(target
))
1814 LOG_ERROR("Target not examined yet");
1818 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1822 target_buffer_set_u32(target
, value_buf
, value
);
1823 if ((retval
= target_write_memory(target
, address
, 4, 1, value_buf
)) != ERROR_OK
)
1825 LOG_DEBUG("failed: %i", retval
);
1831 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
1834 uint8_t value_buf
[2];
1835 if (!target_was_examined(target
))
1837 LOG_ERROR("Target not examined yet");
1841 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
1845 target_buffer_set_u16(target
, value_buf
, value
);
1846 if ((retval
= target_write_memory(target
, address
, 2, 1, value_buf
)) != ERROR_OK
)
1848 LOG_DEBUG("failed: %i", retval
);
1854 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
1857 if (!target_was_examined(target
))
1859 LOG_ERROR("Target not examined yet");
1863 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1866 if ((retval
= target_write_memory(target
, address
, 1, 1, &value
)) != ERROR_OK
)
1868 LOG_DEBUG("failed: %i", retval
);
1874 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
1876 struct target
*target
= get_target(name
);
1877 if (target
== NULL
) {
1878 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
1881 if (!target
->tap
->enabled
) {
1882 LOG_USER("Target: TAP %s is disabled, "
1883 "can't be the current target\n",
1884 target
->tap
->dotted_name
);
1888 cmd_ctx
->current_target
= target
->target_number
;
1893 COMMAND_HANDLER(handle_targets_command
)
1895 int retval
= ERROR_OK
;
1898 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
1899 if (retval
== ERROR_OK
) {
1905 struct target
*target
= all_targets
;
1906 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
1907 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
1913 if (target
->tap
->enabled
)
1914 state
= target_state_name( target
);
1916 state
= "tap-disabled";
1918 if (CMD_CTX
->current_target
== target
->target_number
)
1921 /* keep columns lined up to match the headers above */
1922 command_print(CMD_CTX
,
1923 "%2d%c %-18s %-10s %-6s %-18s %s",
1924 target
->target_number
,
1926 target_name(target
),
1927 target_type_name(target
),
1928 Jim_Nvp_value2name_simple(nvp_target_endian
,
1929 target
->endianness
)->name
,
1930 target
->tap
->dotted_name
,
1932 target
= target
->next
;
1938 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1940 static int powerDropout
;
1941 static int srstAsserted
;
1943 static int runPowerRestore
;
1944 static int runPowerDropout
;
1945 static int runSrstAsserted
;
1946 static int runSrstDeasserted
;
1948 static int sense_handler(void)
1950 static int prevSrstAsserted
= 0;
1951 static int prevPowerdropout
= 0;
1954 if ((retval
= jtag_power_dropout(&powerDropout
)) != ERROR_OK
)
1958 powerRestored
= prevPowerdropout
&& !powerDropout
;
1961 runPowerRestore
= 1;
1964 long long current
= timeval_ms();
1965 static long long lastPower
= 0;
1966 int waitMore
= lastPower
+ 2000 > current
;
1967 if (powerDropout
&& !waitMore
)
1969 runPowerDropout
= 1;
1970 lastPower
= current
;
1973 if ((retval
= jtag_srst_asserted(&srstAsserted
)) != ERROR_OK
)
1977 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
1979 static long long lastSrst
= 0;
1980 waitMore
= lastSrst
+ 2000 > current
;
1981 if (srstDeasserted
&& !waitMore
)
1983 runSrstDeasserted
= 1;
1987 if (!prevSrstAsserted
&& srstAsserted
)
1989 runSrstAsserted
= 1;
1992 prevSrstAsserted
= srstAsserted
;
1993 prevPowerdropout
= powerDropout
;
1995 if (srstDeasserted
|| powerRestored
)
1997 /* Other than logging the event we can't do anything here.
1998 * Issuing a reset is a particularly bad idea as we might
1999 * be inside a reset already.
2006 static int backoff_times
= 0;
2007 static int backoff_count
= 0;
2009 /* process target state changes */
2010 static int handle_target(void *priv
)
2012 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2013 int retval
= ERROR_OK
;
2015 if (!is_jtag_poll_safe())
2017 /* polling is disabled currently */
2021 /* we do not want to recurse here... */
2022 static int recursive
= 0;
2027 /* danger! running these procedures can trigger srst assertions and power dropouts.
2028 * We need to avoid an infinite loop/recursion here and we do that by
2029 * clearing the flags after running these events.
2031 int did_something
= 0;
2032 if (runSrstAsserted
)
2034 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2035 Jim_Eval(interp
, "srst_asserted");
2038 if (runSrstDeasserted
)
2040 Jim_Eval(interp
, "srst_deasserted");
2043 if (runPowerDropout
)
2045 LOG_INFO("Power dropout detected, running power_dropout proc.");
2046 Jim_Eval(interp
, "power_dropout");
2049 if (runPowerRestore
)
2051 Jim_Eval(interp
, "power_restore");
2057 /* clear detect flags */
2061 /* clear action flags */
2063 runSrstAsserted
= 0;
2064 runSrstDeasserted
= 0;
2065 runPowerRestore
= 0;
2066 runPowerDropout
= 0;
2071 if (backoff_times
> backoff_count
)
2073 /* do not poll this time as we failed previously */
2079 /* Poll targets for state changes unless that's globally disabled.
2080 * Skip targets that are currently disabled.
2082 for (struct target
*target
= all_targets
;
2083 is_jtag_poll_safe() && target
;
2084 target
= target
->next
)
2086 if (!target
->tap
->enabled
)
2089 /* only poll target if we've got power and srst isn't asserted */
2090 if (!powerDropout
&& !srstAsserted
)
2092 /* polling may fail silently until the target has been examined */
2093 if ((retval
= target_poll(target
)) != ERROR_OK
)
2095 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2096 if (backoff_times
* polling_interval
< 5000)
2101 LOG_USER("Polling target failed, GDB will be halted. Polling again in %dms", backoff_times
* polling_interval
);
2103 /* Tell GDB to halt the debugger. This allows the user to
2104 * run monitor commands to handle the situation.
2106 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2109 /* Since we succeeded, we reset backoff count */
2110 if (backoff_times
> 0)
2112 LOG_USER("Polling succeeded again");
2121 COMMAND_HANDLER(handle_reg_command
)
2123 struct target
*target
;
2124 struct reg
*reg
= NULL
;
2130 target
= get_current_target(CMD_CTX
);
2132 /* list all available registers for the current target */
2135 struct reg_cache
*cache
= target
->reg_cache
;
2142 command_print(CMD_CTX
, "===== %s", cache
->name
);
2144 for (i
= 0, reg
= cache
->reg_list
;
2145 i
< cache
->num_regs
;
2146 i
++, reg
++, count
++)
2148 /* only print cached values if they are valid */
2150 value
= buf_to_str(reg
->value
,
2152 command_print(CMD_CTX
,
2153 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2161 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2166 cache
= cache
->next
;
2172 /* access a single register by its ordinal number */
2173 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9'))
2176 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2178 struct reg_cache
*cache
= target
->reg_cache
;
2183 for (i
= 0; i
< cache
->num_regs
; i
++)
2187 reg
= &cache
->reg_list
[i
];
2193 cache
= cache
->next
;
2198 command_print(CMD_CTX
, "%i is out of bounds, the current target has only %i registers (0 - %i)", num
, count
, count
- 1);
2201 } else /* access a single register by its name */
2203 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2207 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2212 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2214 /* display a register */
2215 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0') && (CMD_ARGV
[1][0] <= '9'))))
2217 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2220 if (reg
->valid
== 0)
2222 reg
->type
->get(reg
);
2224 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2225 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2230 /* set register value */
2233 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2236 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2238 reg
->type
->set(reg
, buf
);
2240 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2241 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2249 return ERROR_COMMAND_SYNTAX_ERROR
;
2252 COMMAND_HANDLER(handle_poll_command
)
2254 int retval
= ERROR_OK
;
2255 struct target
*target
= get_current_target(CMD_CTX
);
2259 command_print(CMD_CTX
, "background polling: %s",
2260 jtag_poll_get_enabled() ?
"on" : "off");
2261 command_print(CMD_CTX
, "TAP: %s (%s)",
2262 target
->tap
->dotted_name
,
2263 target
->tap
->enabled ?
"enabled" : "disabled");
2264 if (!target
->tap
->enabled
)
2266 if ((retval
= target_poll(target
)) != ERROR_OK
)
2268 if ((retval
= target_arch_state(target
)) != ERROR_OK
)
2271 else if (CMD_ARGC
== 1)
2274 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2275 jtag_poll_set_enabled(enable
);
2279 return ERROR_COMMAND_SYNTAX_ERROR
;
2285 COMMAND_HANDLER(handle_wait_halt_command
)
2288 return ERROR_COMMAND_SYNTAX_ERROR
;
2293 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2294 if (ERROR_OK
!= retval
)
2296 return ERROR_COMMAND_SYNTAX_ERROR
;
2298 // convert seconds (given) to milliseconds (needed)
2302 struct target
*target
= get_current_target(CMD_CTX
);
2303 return target_wait_state(target
, TARGET_HALTED
, ms
);
2306 /* wait for target state to change. The trick here is to have a low
2307 * latency for short waits and not to suck up all the CPU time
2310 * After 500ms, keep_alive() is invoked
2312 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2315 long long then
= 0, cur
;
2320 if ((retval
= target_poll(target
)) != ERROR_OK
)
2322 if (target
->state
== state
)
2330 then
= timeval_ms();
2331 LOG_DEBUG("waiting for target %s...",
2332 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2340 if ((cur
-then
) > ms
)
2342 LOG_ERROR("timed out while waiting for target %s",
2343 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2351 COMMAND_HANDLER(handle_halt_command
)
2355 struct target
*target
= get_current_target(CMD_CTX
);
2356 int retval
= target_halt(target
);
2357 if (ERROR_OK
!= retval
)
2362 unsigned wait_local
;
2363 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2364 if (ERROR_OK
!= retval
)
2365 return ERROR_COMMAND_SYNTAX_ERROR
;
2370 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2373 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2375 struct target
*target
= get_current_target(CMD_CTX
);
2377 LOG_USER("requesting target halt and executing a soft reset");
2379 target
->type
->soft_reset_halt(target
);
2384 COMMAND_HANDLER(handle_reset_command
)
2387 return ERROR_COMMAND_SYNTAX_ERROR
;
2389 enum target_reset_mode reset_mode
= RESET_RUN
;
2393 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2394 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
)) {
2395 return ERROR_COMMAND_SYNTAX_ERROR
;
2397 reset_mode
= n
->value
;
2400 /* reset *all* targets */
2401 return target_process_reset(CMD_CTX
, reset_mode
);
2405 COMMAND_HANDLER(handle_resume_command
)
2409 return ERROR_COMMAND_SYNTAX_ERROR
;
2411 struct target
*target
= get_current_target(CMD_CTX
);
2412 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
2414 /* with no CMD_ARGV, resume from current pc, addr = 0,
2415 * with one arguments, addr = CMD_ARGV[0],
2416 * handle breakpoints, not debugging */
2420 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2424 return target_resume(target
, current
, addr
, 1, 0);
2427 COMMAND_HANDLER(handle_step_command
)
2430 return ERROR_COMMAND_SYNTAX_ERROR
;
2434 /* with no CMD_ARGV, step from current pc, addr = 0,
2435 * with one argument addr = CMD_ARGV[0],
2436 * handle breakpoints, debugging */
2441 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2445 struct target
*target
= get_current_target(CMD_CTX
);
2447 return target
->type
->step(target
, current_pc
, addr
, 1);
2450 static void handle_md_output(struct command_context
*cmd_ctx
,
2451 struct target
*target
, uint32_t address
, unsigned size
,
2452 unsigned count
, const uint8_t *buffer
)
2454 const unsigned line_bytecnt
= 32;
2455 unsigned line_modulo
= line_bytecnt
/ size
;
2457 char output
[line_bytecnt
* 4 + 1];
2458 unsigned output_len
= 0;
2460 const char *value_fmt
;
2462 case 4: value_fmt
= "%8.8x "; break;
2463 case 2: value_fmt
= "%4.4x "; break;
2464 case 1: value_fmt
= "%2.2x "; break;
2466 /* "can't happen", caller checked */
2467 LOG_ERROR("invalid memory read size: %u", size
);
2471 for (unsigned i
= 0; i
< count
; i
++)
2473 if (i
% line_modulo
== 0)
2475 output_len
+= snprintf(output
+ output_len
,
2476 sizeof(output
) - output_len
,
2478 (unsigned)(address
+ (i
*size
)));
2482 const uint8_t *value_ptr
= buffer
+ i
* size
;
2484 case 4: value
= target_buffer_get_u32(target
, value_ptr
); break;
2485 case 2: value
= target_buffer_get_u16(target
, value_ptr
); break;
2486 case 1: value
= *value_ptr
;
2488 output_len
+= snprintf(output
+ output_len
,
2489 sizeof(output
) - output_len
,
2492 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1))
2494 command_print(cmd_ctx
, "%s", output
);
2500 COMMAND_HANDLER(handle_md_command
)
2503 return ERROR_COMMAND_SYNTAX_ERROR
;
2506 switch (CMD_NAME
[2]) {
2507 case 'w': size
= 4; break;
2508 case 'h': size
= 2; break;
2509 case 'b': size
= 1; break;
2510 default: return ERROR_COMMAND_SYNTAX_ERROR
;
2513 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2514 int (*fn
)(struct target
*target
,
2515 uint32_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
2520 fn
=target_read_phys_memory
;
2523 fn
=target_read_memory
;
2525 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2527 return ERROR_COMMAND_SYNTAX_ERROR
;
2531 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2535 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2537 uint8_t *buffer
= calloc(count
, size
);
2539 struct target
*target
= get_current_target(CMD_CTX
);
2540 int retval
= fn(target
, address
, size
, count
, buffer
);
2541 if (ERROR_OK
== retval
)
2542 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2549 typedef int (*target_write_fn
)(struct target
*target
,
2550 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
2552 static int target_write_memory_fast(struct target
*target
,
2553 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
2555 return target_write_buffer(target
, address
, size
* count
, buffer
);
2558 static int target_fill_mem(struct target
*target
,
2567 /* We have to write in reasonably large chunks to be able
2568 * to fill large memory areas with any sane speed */
2569 const unsigned chunk_size
= 16384;
2570 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
2571 if (target_buf
== NULL
)
2573 LOG_ERROR("Out of memory");
2577 for (unsigned i
= 0; i
< chunk_size
; i
++)
2582 target_buffer_set_u32(target
, target_buf
+ i
*data_size
, b
);
2585 target_buffer_set_u16(target
, target_buf
+ i
*data_size
, b
);
2588 target_buffer_set_u8(target
, target_buf
+ i
*data_size
, b
);
2595 int retval
= ERROR_OK
;
2597 for (unsigned x
= 0; x
< c
; x
+= chunk_size
)
2601 if (current
> chunk_size
)
2603 current
= chunk_size
;
2605 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
2606 if (retval
!= ERROR_OK
)
2610 /* avoid GDB timeouts */
2619 COMMAND_HANDLER(handle_mw_command
)
2623 return ERROR_COMMAND_SYNTAX_ERROR
;
2625 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2631 fn
=target_write_phys_memory
;
2634 fn
= target_write_memory_fast
;
2636 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2637 return ERROR_COMMAND_SYNTAX_ERROR
;
2640 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2643 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2647 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2649 struct target
*target
= get_current_target(CMD_CTX
);
2651 switch (CMD_NAME
[2])
2663 return ERROR_COMMAND_SYNTAX_ERROR
;
2666 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
2669 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2670 uint32_t *min_address
, uint32_t *max_address
)
2672 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2673 return ERROR_COMMAND_SYNTAX_ERROR
;
2675 /* a base address isn't always necessary,
2676 * default to 0x0 (i.e. don't relocate) */
2680 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2681 image
->base_address
= addr
;
2682 image
->base_address_set
= 1;
2685 image
->base_address_set
= 0;
2687 image
->start_address_set
= 0;
2691 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2695 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2696 // use size (given) to find max (required)
2697 *max_address
+= *min_address
;
2700 if (*min_address
> *max_address
)
2701 return ERROR_COMMAND_SYNTAX_ERROR
;
2706 COMMAND_HANDLER(handle_load_image_command
)
2710 uint32_t image_size
;
2711 uint32_t min_address
= 0;
2712 uint32_t max_address
= 0xffffffff;
2716 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2717 &image
, &min_address
, &max_address
);
2718 if (ERROR_OK
!= retval
)
2721 struct target
*target
= get_current_target(CMD_CTX
);
2723 struct duration bench
;
2724 duration_start(&bench
);
2726 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2733 for (i
= 0; i
< image
.num_sections
; i
++)
2735 buffer
= malloc(image
.sections
[i
].size
);
2738 command_print(CMD_CTX
,
2739 "error allocating buffer for section (%d bytes)",
2740 (int)(image
.sections
[i
].size
));
2744 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2750 uint32_t offset
= 0;
2751 uint32_t length
= buf_cnt
;
2753 /* DANGER!!! beware of unsigned comparision here!!! */
2755 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
2756 (image
.sections
[i
].base_address
< max_address
))
2758 if (image
.sections
[i
].base_address
< min_address
)
2760 /* clip addresses below */
2761 offset
+= min_address
-image
.sections
[i
].base_address
;
2765 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2767 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2770 if ((retval
= target_write_buffer(target
, image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
)) != ERROR_OK
)
2775 image_size
+= length
;
2776 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2777 (unsigned int)length
,
2778 image
.sections
[i
].base_address
+ offset
);
2784 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2786 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2787 "in %fs (%0.3f KiB/s)", image_size
,
2788 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2791 image_close(&image
);
2797 COMMAND_HANDLER(handle_dump_image_command
)
2799 struct fileio fileio
;
2800 uint8_t buffer
[560];
2801 int retval
, retvaltemp
;
2802 uint32_t address
, size
;
2803 struct duration bench
;
2804 struct target
*target
= get_current_target(CMD_CTX
);
2807 return ERROR_COMMAND_SYNTAX_ERROR
;
2809 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2810 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2812 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
2813 if (retval
!= ERROR_OK
)
2816 duration_start(&bench
);
2821 size_t size_written
;
2822 uint32_t this_run_size
= (size
> 560) ?
560 : size
;
2823 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2824 if (retval
!= ERROR_OK
)
2829 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2830 if (retval
!= ERROR_OK
)
2835 size
-= this_run_size
;
2836 address
+= this_run_size
;
2839 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2842 retval
= fileio_size(&fileio
, &filesize
);
2843 if (retval
!= ERROR_OK
)
2845 command_print(CMD_CTX
,
2846 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize
,
2847 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
2850 if ((retvaltemp
= fileio_close(&fileio
)) != ERROR_OK
)
2856 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
2860 uint32_t image_size
;
2863 uint32_t checksum
= 0;
2864 uint32_t mem_checksum
= 0;
2868 struct target
*target
= get_current_target(CMD_CTX
);
2872 return ERROR_COMMAND_SYNTAX_ERROR
;
2877 LOG_ERROR("no target selected");
2881 struct duration bench
;
2882 duration_start(&bench
);
2887 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2888 image
.base_address
= addr
;
2889 image
.base_address_set
= 1;
2893 image
.base_address_set
= 0;
2894 image
.base_address
= 0x0;
2897 image
.start_address_set
= 0;
2899 if ((retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
)) != ERROR_OK
)
2907 for (i
= 0; i
< image
.num_sections
; i
++)
2909 buffer
= malloc(image
.sections
[i
].size
);
2912 command_print(CMD_CTX
,
2913 "error allocating buffer for section (%d bytes)",
2914 (int)(image
.sections
[i
].size
));
2917 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2925 /* calculate checksum of image */
2926 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
2927 if (retval
!= ERROR_OK
)
2933 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
2934 if (retval
!= ERROR_OK
)
2940 if (checksum
!= mem_checksum
)
2942 /* failed crc checksum, fall back to a binary compare */
2947 LOG_ERROR("checksum mismatch - attempting binary compare");
2950 data
= (uint8_t*)malloc(buf_cnt
);
2952 /* Can we use 32bit word accesses? */
2954 int count
= buf_cnt
;
2955 if ((count
% 4) == 0)
2960 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
2961 if (retval
== ERROR_OK
)
2964 for (t
= 0; t
< buf_cnt
; t
++)
2966 if (data
[t
] != buffer
[t
])
2968 command_print(CMD_CTX
,
2969 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
2971 (unsigned)(t
+ image
.sections
[i
].base_address
),
2976 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
2989 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
2990 image
.sections
[i
].base_address
,
2995 image_size
+= buf_cnt
;
2999 command_print(CMD_CTX
, "No more differences found.");
3004 retval
= ERROR_FAIL
;
3006 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
3008 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3009 "in %fs (%0.3f KiB/s)", image_size
,
3010 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3013 image_close(&image
);
3018 COMMAND_HANDLER(handle_verify_image_command
)
3020 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
3023 COMMAND_HANDLER(handle_test_image_command
)
3025 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
3028 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3030 struct target
*target
= get_current_target(cmd_ctx
);
3031 struct breakpoint
*breakpoint
= target
->breakpoints
;
3034 if (breakpoint
->type
== BKPT_SOFT
)
3036 char* buf
= buf_to_str(breakpoint
->orig_instr
,
3037 breakpoint
->length
, 16);
3038 command_print(cmd_ctx
, "IVA breakpoint: 0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
3039 breakpoint
->address
,
3041 breakpoint
->set
, buf
);
3046 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3047 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3049 breakpoint
->length
, breakpoint
->set
);
3050 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0))
3052 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3053 breakpoint
->address
,
3054 breakpoint
->length
, breakpoint
->set
);
3055 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3059 command_print(cmd_ctx
, "Breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3060 breakpoint
->address
,
3061 breakpoint
->length
, breakpoint
->set
);
3064 breakpoint
= breakpoint
->next
;
3069 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
3070 uint32_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3072 struct target
*target
= get_current_target(cmd_ctx
);
3076 int retval
= breakpoint_add(target
, addr
, length
, hw
);
3077 if (ERROR_OK
== retval
)
3078 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
3081 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3087 int retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3088 if (ERROR_OK
== retval
)
3089 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3092 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3098 int retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3099 if(ERROR_OK
== retval
)
3100 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3103 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3110 COMMAND_HANDLER(handle_bp_command
)
3119 return handle_bp_command_list(CMD_CTX
);
3123 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3124 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3125 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3128 if(strcmp(CMD_ARGV
[2], "hw") == 0)
3131 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3133 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3136 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3138 else if(strcmp(CMD_ARGV
[2], "hw_ctx") == 0)
3141 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3142 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3144 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3149 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3150 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3151 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3152 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3155 return ERROR_COMMAND_SYNTAX_ERROR
;
3159 COMMAND_HANDLER(handle_rbp_command
)
3162 return ERROR_COMMAND_SYNTAX_ERROR
;
3165 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3167 struct target
*target
= get_current_target(CMD_CTX
);
3168 breakpoint_remove(target
, addr
);
3173 COMMAND_HANDLER(handle_wp_command
)
3175 struct target
*target
= get_current_target(CMD_CTX
);
3179 struct watchpoint
*watchpoint
= target
->watchpoints
;
3183 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
3184 ", len: 0x%8.8" PRIx32
3185 ", r/w/a: %i, value: 0x%8.8" PRIx32
3186 ", mask: 0x%8.8" PRIx32
,
3187 watchpoint
->address
,
3189 (int)watchpoint
->rw
,
3192 watchpoint
= watchpoint
->next
;
3197 enum watchpoint_rw type
= WPT_ACCESS
;
3199 uint32_t length
= 0;
3200 uint32_t data_value
= 0x0;
3201 uint32_t data_mask
= 0xffffffff;
3206 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3209 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3212 switch (CMD_ARGV
[2][0])