OpenOCD
target.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2005 by Dominic Rath *
5  * Dominic.Rath@gmx.de *
6  * *
7  * Copyright (C) 2007-2010 Øyvind Harboe *
8  * oyvind.harboe@zylin.com *
9  * *
10  * Copyright (C) 2008, Duane Ellis *
11  * openocd@duaneeellis.com *
12  * *
13  * Copyright (C) 2008 by Spencer Oliver *
14  * spen@spen-soft.co.uk *
15  * *
16  * Copyright (C) 2008 by Rick Altherr *
17  * kc8apf@kc8apf.net> *
18  * *
19  * Copyright (C) 2011 by Broadcom Corporation *
20  * Evan Hunter - ehunter@broadcom.com *
21  * *
22  * Copyright (C) ST-Ericsson SA 2011 *
23  * michel.jaouen@stericsson.com : smp minimum support *
24  * *
25  * Copyright (C) 2011 Andreas Fritiofson *
26  * andreas.fritiofson@gmail.com *
27  ***************************************************************************/
28 
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 
33 #include <stdlib.h>
34 #include <helper/align.h>
35 #include <helper/list.h>
36 #include <helper/nvp.h>
37 #include <helper/time_support.h>
38 #include <jtag/jtag.h>
39 #include <flash/nor/core.h>
40 #include <target/oocd_capstone.h>
41 
42 #include "target.h"
43 #include "target_type.h"
44 #include "target_request.h"
45 #include "breakpoints.h"
46 #include "register.h"
47 #include "trace.h"
48 #include "image.h"
49 #include "rtos/rtos.h"
50 #include "transport/transport.h"
51 #include "arm_cti.h"
52 #include "smp.h"
53 #include "semihosting_common.h"
54 
55 /* default halt wait timeout (ms) */
56 #define DEFAULT_HALT_TIMEOUT 5000
57 
59  enum target_event event;
60  Jim_Interp *interp;
61  Jim_Obj *body;
62  struct list_head list;
63 };
64 
66  uint32_t count, uint8_t *buffer);
68  uint32_t count, const uint8_t *buffer);
69 static int target_register_user_commands(struct command_context *cmd_ctx);
71  struct gdb_fileio_info *fileio_info);
72 static int target_gdb_fileio_end_default(struct target *target, int retcode,
73  int fileio_errno, bool ctrl_c);
74 
75 static struct target_type *target_types[] = {
76  // Keep in alphabetic order this list of targets
78  &arcv2_target,
79  &arm11_target,
89  &avr_target,
99  &esp32_target,
100  &fa526_target,
102  &hla_target,
104  &mem_ap_target,
107  &or1k_target,
110  &riscv_target,
111  &stm8_target,
112  &testee_target,
113  &xscale_target,
115 };
116 
121 static OOCD_LIST_HEAD(target_reset_callback_list);
122 static OOCD_LIST_HEAD(target_trace_callback_list);
124 static OOCD_LIST_HEAD(empty_smp_targets);
125 
129 };
130 
131 static const struct nvp nvp_assert[] = {
132  { .name = "assert", NVP_ASSERT },
133  { .name = "deassert", NVP_DEASSERT },
134  { .name = "T", NVP_ASSERT },
135  { .name = "F", NVP_DEASSERT },
136  { .name = "t", NVP_ASSERT },
137  { .name = "f", NVP_DEASSERT },
138  { .name = NULL, .value = -1 }
139 };
140 
141 static const struct nvp nvp_error_target[] = {
142  { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
143  { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
144  { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
145  { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
146  { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
147  { .value = ERROR_TARGET_UNALIGNED_ACCESS, .name = "err-unaligned-access" },
148  { .value = ERROR_TARGET_DATA_ABORT, .name = "err-data-abort" },
149  { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE, .name = "err-resource-not-available" },
150  { .value = ERROR_TARGET_TRANSLATION_FAULT, .name = "err-translation-fault" },
151  { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
152  { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
153  { .value = -1, .name = NULL }
154 };
155 
156 static const char *target_strerror_safe(int err)
157 {
158  const struct nvp *n;
159 
161  if (!n->name)
162  return "unknown";
163  else
164  return n->name;
165 }
166 
167 static const struct nvp nvp_target_event[] = {
168 
169  { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
170  { .value = TARGET_EVENT_HALTED, .name = "halted" },
171  { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
172  { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
173  { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
174  { .value = TARGET_EVENT_STEP_START, .name = "step-start" },
175  { .value = TARGET_EVENT_STEP_END, .name = "step-end" },
176 
177  { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
178  { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
179 
180  { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
181  { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
182  { .value = TARGET_EVENT_RESET_ASSERT, .name = "reset-assert" },
183  { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
184  { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
185  { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
186  { .value = TARGET_EVENT_RESET_INIT, .name = "reset-init" },
187  { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
188 
189  { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
190  { .value = TARGET_EVENT_EXAMINE_FAIL, .name = "examine-fail" },
191  { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
192 
193  { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
194  { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
195 
196  { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
197  { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
198 
199  { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
200  { .value = TARGET_EVENT_GDB_FLASH_WRITE_END, .name = "gdb-flash-write-end" },
201 
202  { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
203  { .value = TARGET_EVENT_GDB_FLASH_ERASE_END, .name = "gdb-flash-erase-end" },
204 
205  { .value = TARGET_EVENT_TRACE_CONFIG, .name = "trace-config" },
206 
207  { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X100, .name = "semihosting-user-cmd-0x100" },
208  { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X101, .name = "semihosting-user-cmd-0x101" },
209  { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X102, .name = "semihosting-user-cmd-0x102" },
210  { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X103, .name = "semihosting-user-cmd-0x103" },
211  { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X104, .name = "semihosting-user-cmd-0x104" },
212  { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X105, .name = "semihosting-user-cmd-0x105" },
213  { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X106, .name = "semihosting-user-cmd-0x106" },
214  { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X107, .name = "semihosting-user-cmd-0x107" },
215 
216  { .name = NULL, .value = -1 }
217 };
218 
219 static const struct nvp nvp_target_state[] = {
220  { .name = "unknown", .value = TARGET_UNKNOWN },
221  { .name = "running", .value = TARGET_RUNNING },
222  { .name = "halted", .value = TARGET_HALTED },
223  { .name = "reset", .value = TARGET_RESET },
224  { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
225  { .name = "unavailable", .value = TARGET_UNAVAILABLE },
226  { .name = NULL, .value = -1 },
227 };
228 
229 static const struct nvp nvp_target_debug_reason[] = {
230  { .name = "debug-request", .value = DBG_REASON_DBGRQ },
231  { .name = "breakpoint", .value = DBG_REASON_BREAKPOINT },
232  { .name = "watchpoint", .value = DBG_REASON_WATCHPOINT },
233  { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
234  { .name = "single-step", .value = DBG_REASON_SINGLESTEP },
235  { .name = "target-not-halted", .value = DBG_REASON_NOTHALTED },
236  { .name = "program-exit", .value = DBG_REASON_EXIT },
237  { .name = "exception-catch", .value = DBG_REASON_EXC_CATCH },
238  { .name = "undefined", .value = DBG_REASON_UNDEFINED },
239  { .name = NULL, .value = -1 },
240 };
241 
242 static const struct nvp nvp_target_endian[] = {
243  { .name = "big", .value = TARGET_BIG_ENDIAN },
244  { .name = "little", .value = TARGET_LITTLE_ENDIAN },
245  { .name = "be", .value = TARGET_BIG_ENDIAN },
246  { .name = "le", .value = TARGET_LITTLE_ENDIAN },
247  { .name = NULL, .value = -1 },
248 };
249 
250 static const struct nvp nvp_reset_modes[] = {
251  { .name = "unknown", .value = RESET_UNKNOWN },
252  { .name = "run", .value = RESET_RUN },
253  { .name = "halt", .value = RESET_HALT },
254  { .name = "init", .value = RESET_INIT },
255  { .name = NULL, .value = -1 },
256 };
257 
258 const char *debug_reason_name(const struct target *t)
259 {
260  const char *cp;
261 
263  t->debug_reason)->name;
264  if (!cp) {
265  LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
266  cp = "(*BUG*unknown*BUG*)";
267  }
268  return cp;
269 }
270 
271 const char *target_state_name(const struct target *t)
272 {
273  const char *cp;
275  if (!cp) {
276  LOG_ERROR("Invalid target state: %d", (int)(t->state));
277  cp = "(*BUG*unknown*BUG*)";
278  }
279 
280  if (!target_was_examined(t) && t->defer_examine)
281  cp = "examine deferred";
282 
283  return cp;
284 }
285 
286 const char *target_event_name(enum target_event event)
287 {
288  const char *cp;
289  cp = nvp_value2name(nvp_target_event, event)->name;
290  if (!cp) {
291  LOG_ERROR("Invalid target event: %d", (int)(event));
292  cp = "(*BUG*unknown*BUG*)";
293  }
294  return cp;
295 }
296 
297 const char *target_reset_mode_name(enum target_reset_mode reset_mode)
298 {
299  const char *cp;
300  cp = nvp_value2name(nvp_reset_modes, reset_mode)->name;
301  if (!cp) {
302  LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode));
303  cp = "(*BUG*unknown*BUG*)";
304  }
305  return cp;
306 }
307 
309 {
310  struct target **t = &all_targets;
311 
312  while (*t)
313  t = &((*t)->next);
314  *t = target;
315 }
316 
317 /* read a uint64_t from a buffer in target memory endianness */
318 uint64_t target_buffer_get_u64(struct target *target, const uint8_t *buffer)
319 {
321  return le_to_h_u64(buffer);
322  else
323  return be_to_h_u64(buffer);
324 }
325 
326 /* read a uint32_t from a buffer in target memory endianness */
327 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
328 {
330  return le_to_h_u32(buffer);
331  else
332  return be_to_h_u32(buffer);
333 }
334 
335 /* read a uint24_t from a buffer in target memory endianness */
336 uint32_t target_buffer_get_u24(struct target *target, const uint8_t *buffer)
337 {
339  return le_to_h_u24(buffer);
340  else
341  return be_to_h_u24(buffer);
342 }
343 
344 /* read a uint16_t from a buffer in target memory endianness */
345 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
346 {
348  return le_to_h_u16(buffer);
349  else
350  return be_to_h_u16(buffer);
351 }
352 
353 /* write a uint64_t to a buffer in target memory endianness */
354 void target_buffer_set_u64(struct target *target, uint8_t *buffer, uint64_t value)
355 {
357  h_u64_to_le(buffer, value);
358  else
359  h_u64_to_be(buffer, value);
360 }
361 
362 /* write a uint32_t to a buffer in target memory endianness */
363 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
364 {
366  h_u32_to_le(buffer, value);
367  else
368  h_u32_to_be(buffer, value);
369 }
370 
371 /* write a uint24_t to a buffer in target memory endianness */
372 void target_buffer_set_u24(struct target *target, uint8_t *buffer, uint32_t value)
373 {
375  h_u24_to_le(buffer, value);
376  else
377  h_u24_to_be(buffer, value);
378 }
379 
380 /* write a uint16_t to a buffer in target memory endianness */
381 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
382 {
384  h_u16_to_le(buffer, value);
385  else
386  h_u16_to_be(buffer, value);
387 }
388 
389 /* write a uint8_t to a buffer in target memory endianness */
390 static void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
391 {
392  *buffer = value;
393 }
394 
395 /* write a uint64_t array to a buffer in target memory endianness */
396 void target_buffer_get_u64_array(struct target *target, const uint8_t *buffer, uint32_t count, uint64_t *dstbuf)
397 {
398  uint32_t i;
399  for (i = 0; i < count; i++)
400  dstbuf[i] = target_buffer_get_u64(target, &buffer[i * 8]);
401 }
402 
403 /* write a uint32_t array to a buffer in target memory endianness */
404 void target_buffer_get_u32_array(struct target *target, const uint8_t *buffer, uint32_t count, uint32_t *dstbuf)
405 {
406  uint32_t i;
407  for (i = 0; i < count; i++)
408  dstbuf[i] = target_buffer_get_u32(target, &buffer[i * 4]);
409 }
410 
411 /* write a uint16_t array to a buffer in target memory endianness */
412 void target_buffer_get_u16_array(struct target *target, const uint8_t *buffer, uint32_t count, uint16_t *dstbuf)
413 {
414  uint32_t i;
415  for (i = 0; i < count; i++)
416  dstbuf[i] = target_buffer_get_u16(target, &buffer[i * 2]);
417 }
418 
419 /* write a uint64_t array to a buffer in target memory endianness */
420 void target_buffer_set_u64_array(struct target *target, uint8_t *buffer, uint32_t count, const uint64_t *srcbuf)
421 {
422  uint32_t i;
423  for (i = 0; i < count; i++)
424  target_buffer_set_u64(target, &buffer[i * 8], srcbuf[i]);
425 }
426 
427 /* write a uint32_t array to a buffer in target memory endianness */
428 void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, const uint32_t *srcbuf)
429 {
430  uint32_t i;
431  for (i = 0; i < count; i++)
432  target_buffer_set_u32(target, &buffer[i * 4], srcbuf[i]);
433 }
434 
435 /* write a uint16_t array to a buffer in target memory endianness */
436 void target_buffer_set_u16_array(struct target *target, uint8_t *buffer, uint32_t count, const uint16_t *srcbuf)
437 {
438  uint32_t i;
439  for (i = 0; i < count; i++)
440  target_buffer_set_u16(target, &buffer[i * 2], srcbuf[i]);
441 }
442 
443 /* return a pointer to a configured target; id is name or index in all_targets */
444 struct target *get_target(const char *id)
445 {
446  struct target *target;
447 
448  /* try as tcltarget name */
449  for (target = all_targets; target; target = target->next) {
450  if (!target_name(target))
451  continue;
452  if (strcmp(id, target_name(target)) == 0)
453  return target;
454  }
455 
456  /* try as index */
457  unsigned int index, counter;
458  if (parse_uint(id, &index) != ERROR_OK)
459  return NULL;
460 
461  for (target = all_targets, counter = index;
462  target && counter;
463  target = target->next, --counter)
464  ;
465 
466  return target;
467 }
468 
469 struct target *get_current_target(struct command_context *cmd_ctx)
470 {
471  struct target *target = get_current_target_or_null(cmd_ctx);
472 
473  if (!target) {
474  LOG_ERROR("BUG: current_target out of bounds");
475  exit(-1);
476  }
477 
478  return target;
479 }
480 
482 {
483  return cmd_ctx->current_target_override
484  ? cmd_ctx->current_target_override
485  : cmd_ctx->current_target;
486 }
487 
489 {
490  int retval;
491 
492  /* We can't poll until after examine */
493  if (!target_was_examined(target)) {
494  /* Fail silently lest we pollute the log */
496  }
497 
498  retval = target->type->poll(target);
499  if (retval != ERROR_OK)
500  return retval;
501 
502  if (target->halt_issued) {
503  if (target->state == TARGET_HALTED)
504  target->halt_issued = false;
505  else {
506  int64_t t = timeval_ms() - target->halt_issued_time;
507  if (t > DEFAULT_HALT_TIMEOUT) {
508  target->halt_issued = false;
509  LOG_INFO("Halt timed out, wake up GDB.");
511  }
512  }
513  }
514 
515  return ERROR_OK;
516 }
517 
519 {
520  int retval;
521 
522  if (!target_was_examined(target)) {
523  LOG_TARGET_ERROR(target, "not examined");
525  }
526 
527  retval = target->type->halt(target);
528  if (retval != ERROR_OK)
529  return retval;
530 
531  target->halt_issued = true;
533 
534  return ERROR_OK;
535 }
536 
567 int target_resume(struct target *target, bool current, target_addr_t address,
568  bool handle_breakpoints, bool debug_execution)
569 {
570  int retval;
571 
572  if (!target_was_examined(target)) {
573  LOG_TARGET_ERROR(target, "not examined");
575  }
576 
578 
579  /* note that resume *must* be asynchronous. The CPU can halt before
580  * we poll. The CPU can even halt at the current PC as a result of
581  * a software breakpoint being inserted by (a bug?) the application.
582  */
583  /*
584  * resume() triggers the event 'resumed'. The execution of TCL commands
585  * in the event handler causes the polling of targets. If the target has
586  * already halted for a breakpoint, polling will run the 'halted' event
587  * handler before the pending 'resumed' handler.
588  * Disable polling during resume() to guarantee the execution of handlers
589  * in the correct order.
590  */
591  bool save_poll_mask = jtag_poll_mask();
592  retval = target->type->resume(target, current, address, handle_breakpoints,
593  debug_execution);
594  jtag_poll_unmask(save_poll_mask);
595 
596  if (retval != ERROR_OK)
597  return retval;
598 
600 
601  return retval;
602 }
603 
604 static int target_process_reset(struct command_invocation *cmd, enum target_reset_mode reset_mode)
605 {
606  char buf[100];
607  int retval;
608  const struct nvp *n;
609  n = nvp_value2name(nvp_reset_modes, reset_mode);
610  if (!n->name) {
611  LOG_ERROR("invalid reset mode");
612  return ERROR_FAIL;
613  }
614 
615  struct target *target;
617  target_call_reset_callbacks(target, reset_mode);
618 
619  /* disable polling during reset to make reset event scripts
620  * more predictable, i.e. dr/irscan & pathmove in events will
621  * not have JTAG operations injected into the middle of a sequence.
622  */
623  bool save_poll_mask = jtag_poll_mask();
624 
625  sprintf(buf, "ocd_process_reset %s", n->name);
626  retval = Jim_Eval(cmd->ctx->interp, buf);
627 
628  jtag_poll_unmask(save_poll_mask);
629 
630  if (retval != JIM_OK) {
631  Jim_MakeErrorMessage(cmd->ctx->interp);
632  command_print(cmd, "%s", Jim_GetString(Jim_GetResult(cmd->ctx->interp), NULL));
633  return ERROR_FAIL;
634  }
635 
636  /* We want any events to be processed before the prompt */
638 
639  for (target = all_targets; target; target = target->next) {
641  target->running_alg = false;
642  }
643 
644  return retval;
645 }
646 
647 static int identity_virt2phys(struct target *target,
648  target_addr_t virtual, target_addr_t *physical)
649 {
650  *physical = virtual;
651  return ERROR_OK;
652 }
653 
654 static int no_mmu(struct target *target, bool *enabled)
655 {
656  *enabled = false;
657  return ERROR_OK;
658 }
659 
663 static inline void target_reset_examined(struct target *target)
664 {
665  target->examined = false;
666 }
667 
668 static inline void target_reset_active_polled(struct target *target)
669 {
670  target->active_polled = false;
671 }
672 
673 static int default_examine(struct target *target)
674 {
675  return ERROR_OK;
676 }
677 
678 /* no check by default */
679 static int default_check_reset(struct target *target)
680 {
681  return ERROR_OK;
682 }
683 
684 /* Equivalent Tcl code arp_examine_one is in src/target/startup.tcl
685  * Keep in sync */
687 {
688  LOG_TARGET_DEBUG(target, "Examination started");
689 
691 
692  int retval = target->type->examine(target);
693  if (retval != ERROR_OK) {
694  LOG_TARGET_ERROR(target, "Examination failed");
695  LOG_TARGET_DEBUG(target, "examine() returned error code %d", retval);
698  return retval;
699  }
700 
703 
704  LOG_TARGET_INFO(target, "Examination succeed");
705  return ERROR_OK;
706 }
707 
708 static int jtag_enable_callback(enum jtag_event event, void *priv)
709 {
710  struct target *target = priv;
711 
712  if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
713  return ERROR_OK;
714 
716 
717  return target_examine_one(target);
718 }
719 
720 /* Targets that correctly implement init + examine, i.e.
721  * no communication with target during init:
722  *
723  * XScale
724  */
725 int target_examine(void)
726 {
727  int retval = ERROR_OK;
728  struct target *target;
729 
730  for (target = all_targets; target; target = target->next) {
731  /* defer examination, but don't skip it */
732  if (!target->tap->enabled) {
734  target);
735  continue;
736  }
737 
738  if (target->defer_examine)
739  continue;
740 
741  int retval2 = target_examine_one(target);
742  if (retval2 != ERROR_OK) {
743  LOG_WARNING("target %s examination failed", target_name(target));
744  retval = retval2;
745  }
746  }
747  return retval;
748 }
749 
750 const char *target_type_name(const struct target *target)
751 {
752  return target->type->name;
753 }
754 
756 {
757  if (!target_was_examined(target)) {
758  LOG_TARGET_ERROR(target, "not examined");
760  }
761  if (!target->type->soft_reset_halt) {
762  LOG_ERROR("Target %s does not support soft_reset_halt",
764  return ERROR_FAIL;
765  }
766  return target->type->soft_reset_halt(target);
767 }
768 
788  int num_mem_params, struct mem_param *mem_params,
789  int num_reg_params, struct reg_param *reg_param,
790  target_addr_t entry_point, target_addr_t exit_point,
791  unsigned int timeout_ms, void *arch_info)
792 {
793  int retval = ERROR_FAIL;
794 
795  if (!target_was_examined(target)) {
796  LOG_TARGET_ERROR(target, "not examined");
797  retval = ERROR_TARGET_NOT_EXAMINED;
798  goto done;
799  }
800  if (!target->type->run_algorithm) {
801  LOG_ERROR("Target type '%s' does not support %s",
802  target_type_name(target), __func__);
803  goto done;
804  }
805 
806  target->running_alg = true;
807  retval = target->type->run_algorithm(target,
808  num_mem_params, mem_params,
809  num_reg_params, reg_param,
810  entry_point, exit_point, timeout_ms, arch_info);
811  target->running_alg = false;
812 
813 done:
814  return retval;
815 }
816 
830  int num_mem_params, struct mem_param *mem_params,
831  int num_reg_params, struct reg_param *reg_params,
832  target_addr_t entry_point, target_addr_t exit_point,
833  void *arch_info)
834 {
835  int retval = ERROR_FAIL;
836 
837  if (!target_was_examined(target)) {
838  LOG_TARGET_ERROR(target, "not examined");
839  retval = ERROR_TARGET_NOT_EXAMINED;
840  goto done;
841  }
842  if (!target->type->start_algorithm) {
843  LOG_ERROR("Target type '%s' does not support %s",
844  target_type_name(target), __func__);
845  goto done;
846  }
847  if (target->running_alg) {
848  LOG_ERROR("Target is already running an algorithm");
849  goto done;
850  }
851 
852  target->running_alg = true;
853  retval = target->type->start_algorithm(target,
854  num_mem_params, mem_params,
855  num_reg_params, reg_params,
856  entry_point, exit_point, arch_info);
857 
858 done:
859  return retval;
860 }
861 
875  int num_mem_params, struct mem_param *mem_params,
876  int num_reg_params, struct reg_param *reg_params,
877  target_addr_t exit_point, unsigned int timeout_ms,
878  void *arch_info)
879 {
880  int retval = ERROR_FAIL;
881 
882  if (!target->type->wait_algorithm) {
883  LOG_ERROR("Target type '%s' does not support %s",
884  target_type_name(target), __func__);
885  goto done;
886  }
887  if (!target->running_alg) {
888  LOG_ERROR("Target is not running an algorithm");
889  goto done;
890  }
891 
892  retval = target->type->wait_algorithm(target,
893  num_mem_params, mem_params,
894  num_reg_params, reg_params,
895  exit_point, timeout_ms, arch_info);
896  if (retval != ERROR_TARGET_TIMEOUT)
897  target->running_alg = false;
898 
899 done:
900  return retval;
901 }
902 
947  const uint8_t *buffer, uint32_t count, int block_size,
948  int num_mem_params, struct mem_param *mem_params,
949  int num_reg_params, struct reg_param *reg_params,
950  uint32_t buffer_start, uint32_t buffer_size,
951  uint32_t entry_point, uint32_t exit_point, void *arch_info)
952 {
953  int retval;
954  int timeout = 0;
955 
956  const uint8_t *buffer_orig = buffer;
957 
958  /* Set up working area. First word is write pointer, second word is read pointer,
959  * rest is fifo data area. */
960  uint32_t wp_addr = buffer_start;
961  uint32_t rp_addr = buffer_start + 4;
962  uint32_t fifo_start_addr = buffer_start + 8;
963  uint32_t fifo_end_addr = buffer_start + buffer_size;
964 
965  uint32_t wp = fifo_start_addr;
966  uint32_t rp = fifo_start_addr;
967 
968  /* validate block_size is 2^n */
969  assert(IS_PWR_OF_2(block_size));
970 
971  retval = target_write_u32(target, wp_addr, wp);
972  if (retval != ERROR_OK)
973  return retval;
974  retval = target_write_u32(target, rp_addr, rp);
975  if (retval != ERROR_OK)
976  return retval;
977 
978  /* Start up algorithm on target and let it idle while writing the first chunk */
979  retval = target_start_algorithm(target, num_mem_params, mem_params,
980  num_reg_params, reg_params,
981  entry_point,
982  exit_point,
983  arch_info);
984 
985  if (retval != ERROR_OK) {
986  LOG_ERROR("error starting target flash write algorithm");
987  return retval;
988  }
989 
990  while (count > 0) {
991 
992  retval = target_read_u32(target, rp_addr, &rp);
993  if (retval != ERROR_OK) {
994  LOG_ERROR("failed to get read pointer");
995  break;
996  }
997 
998  LOG_DEBUG("offs 0x%zx count 0x%" PRIx32 " wp 0x%" PRIx32 " rp 0x%" PRIx32,
999  (size_t) (buffer - buffer_orig), count, wp, rp);
1000 
1001  if (rp == 0) {
1002  LOG_ERROR("flash write algorithm aborted by target");
1004  break;
1005  }
1006 
1007  if (!IS_ALIGNED(rp - fifo_start_addr, block_size) || rp < fifo_start_addr || rp >= fifo_end_addr) {
1008  LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32, rp);
1009  break;
1010  }
1011 
1012  /* Count the number of bytes available in the fifo without
1013  * crossing the wrap around. Make sure to not fill it completely,
1014  * because that would make wp == rp and that's the empty condition. */
1015  uint32_t thisrun_bytes;
1016  if (rp > wp)
1017  thisrun_bytes = rp - wp - block_size;
1018  else if (rp > fifo_start_addr)
1019  thisrun_bytes = fifo_end_addr - wp;
1020  else
1021  thisrun_bytes = fifo_end_addr - wp - block_size;
1022 
1023  if (thisrun_bytes == 0) {
1024  /* Throttle polling a bit if transfer is (much) faster than flash
1025  * programming. The exact delay shouldn't matter as long as it's
1026  * less than buffer size / flash speed. This is very unlikely to
1027  * run when using high latency connections such as USB. */
1028  alive_sleep(2);
1029 
1030  /* to stop an infinite loop on some targets check and increment a timeout
1031  * this issue was observed on a stellaris using the new ICDI interface */
1032  if (timeout++ >= 2500) {
1033  LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1035  }
1036  continue;
1037  }
1038 
1039  /* reset our timeout */
1040  timeout = 0;
1041 
1042  /* Limit to the amount of data we actually want to write */
1043  if (thisrun_bytes > count * block_size)
1044  thisrun_bytes = count * block_size;
1045 
1046  /* Force end of large blocks to be word aligned */
1047  if (thisrun_bytes >= 16)
1048  thisrun_bytes -= (rp + thisrun_bytes) & 0x03;
1049 
1050  /* Write data to fifo */
1051  retval = target_write_buffer(target, wp, thisrun_bytes, buffer);
1052  if (retval != ERROR_OK)
1053  break;
1054 
1055  /* Update counters and wrap write pointer */
1056  buffer += thisrun_bytes;
1057  count -= thisrun_bytes / block_size;
1058  wp += thisrun_bytes;
1059  if (wp >= fifo_end_addr)
1060  wp = fifo_start_addr;
1061 
1062  /* Store updated write pointer to target */
1063  retval = target_write_u32(target, wp_addr, wp);
1064  if (retval != ERROR_OK)
1065  break;
1066 
1067  /* Avoid GDB timeouts */
1068  keep_alive();
1069  }
1070 
1071  if (retval != ERROR_OK) {
1072  /* abort flash write algorithm on target */
1073  target_write_u32(target, wp_addr, 0);
1074  }
1075 
1076  int retval2 = target_wait_algorithm(target, num_mem_params, mem_params,
1077  num_reg_params, reg_params,
1078  exit_point,
1079  10000,
1080  arch_info);
1081 
1082  if (retval2 != ERROR_OK) {
1083  LOG_ERROR("error waiting for target flash write algorithm");
1084  retval = retval2;
1085  }
1086 
1087  if (retval == ERROR_OK) {
1088  /* check if algorithm set rp = 0 after fifo writer loop finished */
1089  retval = target_read_u32(target, rp_addr, &rp);
1090  if (retval == ERROR_OK && rp == 0) {
1091  LOG_ERROR("flash write algorithm aborted by target");
1093  }
1094  }
1095 
1096  return retval;
1097 }
1098 
1100  uint8_t *buffer, uint32_t count, int block_size,
1101  int num_mem_params, struct mem_param *mem_params,
1102  int num_reg_params, struct reg_param *reg_params,
1103  uint32_t buffer_start, uint32_t buffer_size,
1104  uint32_t entry_point, uint32_t exit_point, void *arch_info)
1105 {
1106  int retval;
1107  int timeout = 0;
1108 
1109  const uint8_t *buffer_orig = buffer;
1110 
1111  /* Set up working area. First word is write pointer, second word is read pointer,
1112  * rest is fifo data area. */
1113  uint32_t wp_addr = buffer_start;
1114  uint32_t rp_addr = buffer_start + 4;
1115  uint32_t fifo_start_addr = buffer_start + 8;
1116  uint32_t fifo_end_addr = buffer_start + buffer_size;
1117 
1118  uint32_t wp = fifo_start_addr;
1119  uint32_t rp = fifo_start_addr;
1120 
1121  /* validate block_size is 2^n */
1122  assert(IS_PWR_OF_2(block_size));
1123 
1124  retval = target_write_u32(target, wp_addr, wp);
1125  if (retval != ERROR_OK)
1126  return retval;
1127  retval = target_write_u32(target, rp_addr, rp);
1128  if (retval != ERROR_OK)
1129  return retval;
1130 
1131  /* Start up algorithm on target */
1132  retval = target_start_algorithm(target, num_mem_params, mem_params,
1133  num_reg_params, reg_params,
1134  entry_point,
1135  exit_point,
1136  arch_info);
1137 
1138  if (retval != ERROR_OK) {
1139  LOG_ERROR("error starting target flash read algorithm");
1140  return retval;
1141  }
1142 
1143  while (count > 0) {
1144  retval = target_read_u32(target, wp_addr, &wp);
1145  if (retval != ERROR_OK) {
1146  LOG_ERROR("failed to get write pointer");
1147  break;
1148  }
1149 
1150  LOG_DEBUG("offs 0x%zx count 0x%" PRIx32 " wp 0x%" PRIx32 " rp 0x%" PRIx32,
1151  (size_t)(buffer - buffer_orig), count, wp, rp);
1152 
1153  if (wp == 0) {
1154  LOG_ERROR("flash read algorithm aborted by target");
1156  break;
1157  }
1158 
1159  if (!IS_ALIGNED(wp - fifo_start_addr, block_size) || wp < fifo_start_addr || wp >= fifo_end_addr) {
1160  LOG_ERROR("corrupted fifo write pointer 0x%" PRIx32, wp);
1161  break;
1162  }
1163 
1164  /* Count the number of bytes available in the fifo without
1165  * crossing the wrap around. */
1166  uint32_t thisrun_bytes;
1167  if (wp >= rp)
1168  thisrun_bytes = wp - rp;
1169  else
1170  thisrun_bytes = fifo_end_addr - rp;
1171 
1172  if (thisrun_bytes == 0) {
1173  /* Throttle polling a bit if transfer is (much) faster than flash
1174  * reading. The exact delay shouldn't matter as long as it's
1175  * less than buffer size / flash speed. This is very unlikely to
1176  * run when using high latency connections such as USB. */
1177  alive_sleep(2);
1178 
1179  /* to stop an infinite loop on some targets check and increment a timeout
1180  * this issue was observed on a stellaris using the new ICDI interface */
1181  if (timeout++ >= 2500) {
1182  LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1184  }
1185  continue;
1186  }
1187 
1188  /* Reset our timeout */
1189  timeout = 0;
1190 
1191  /* Limit to the amount of data we actually want to read */
1192  if (thisrun_bytes > count * block_size)
1193  thisrun_bytes = count * block_size;
1194 
1195  /* Force end of large blocks to be word aligned */
1196  if (thisrun_bytes >= 16)
1197  thisrun_bytes -= (rp + thisrun_bytes) & 0x03;
1198 
1199  /* Read data from fifo */
1200  retval = target_read_buffer(target, rp, thisrun_bytes, buffer);
1201  if (retval != ERROR_OK)
1202  break;
1203 
1204  /* Update counters and wrap write pointer */
1205  buffer += thisrun_bytes;
1206  count -= thisrun_bytes / block_size;
1207  rp += thisrun_bytes;
1208  if (rp >= fifo_end_addr)
1209  rp = fifo_start_addr;
1210 
1211  /* Store updated write pointer to target */
1212  retval = target_write_u32(target, rp_addr, rp);
1213  if (retval != ERROR_OK)
1214  break;
1215 
1216  /* Avoid GDB timeouts */
1217  keep_alive();
1218 
1220  retval = ERROR_SERVER_INTERRUPTED;
1221  break;
1222  }
1223  }
1224 
1225  if (retval != ERROR_OK) {
1226  /* abort flash write algorithm on target */
1227  target_write_u32(target, rp_addr, 0);
1228  }
1229 
1230  int retval2 = target_wait_algorithm(target, num_mem_params, mem_params,
1231  num_reg_params, reg_params,
1232  exit_point,
1233  10000,
1234  arch_info);
1235 
1236  if (retval2 != ERROR_OK) {
1237  LOG_ERROR("error waiting for target flash write algorithm");
1238  retval = retval2;
1239  }
1240 
1241  if (retval == ERROR_OK) {
1242  /* check if algorithm set wp = 0 after fifo writer loop finished */
1243  retval = target_read_u32(target, wp_addr, &wp);
1244  if (retval == ERROR_OK && wp == 0) {
1245  LOG_ERROR("flash read algorithm aborted by target");
1247  }
1248  }
1249 
1250  return retval;
1251 }
1252 
1254 {
1255  if (target->type->memory_ready)
1256  return target->type->memory_ready(target);
1257 
1258  return target_was_examined(target);
1259 }
1260 
1262  target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
1263 {
1264  if (!target_memory_ready(target)) {
1265  LOG_TARGET_ERROR(target, "Memory not ready");
1266  return ERROR_FAIL;
1267  }
1268  if (!target->type->read_memory) {
1269  LOG_TARGET_ERROR(target, "doesn't support read_memory");
1270  return ERROR_FAIL;
1271  }
1273 }
1274 
1276  target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
1277 {
1278  if (!target_memory_ready(target)) {
1279  LOG_TARGET_ERROR(target, "Memory not ready");
1280  return ERROR_FAIL;
1281  }
1282  if (!target->type->read_phys_memory) {
1283  LOG_TARGET_ERROR(target, "doesn't support read_phys_memory");
1284  return ERROR_FAIL;
1285  }
1287 }
1288 
1290  target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
1291 {
1292  if (!target_memory_ready(target)) {
1293  LOG_TARGET_ERROR(target, "Memory not ready");
1294  return ERROR_FAIL;
1295  }
1296  if (!target->type->write_memory) {
1297  LOG_TARGET_ERROR(target, "doesn't support write_memory");
1298  return ERROR_FAIL;
1299  }
1301 }
1302 
1304  target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
1305 {
1306  if (!target_memory_ready(target)) {
1307  LOG_TARGET_ERROR(target, "Memory not ready");
1308  return ERROR_FAIL;
1309  }
1310  if (!target->type->write_phys_memory) {
1311  LOG_TARGET_ERROR(target, "doesn't support write_phys_memory");
1312  return ERROR_FAIL;
1313  }
1315 }
1316 
1318  struct breakpoint *breakpoint)
1319 {
1320  if ((target->state != TARGET_HALTED) && (breakpoint->type != BKPT_HARD)) {
1321  LOG_TARGET_ERROR(target, "not halted (add breakpoint)");
1322  return ERROR_TARGET_NOT_HALTED;
1323  }
1325 }
1326 
1328  struct breakpoint *breakpoint)
1329 {
1330  if (target->state != TARGET_HALTED) {
1331  LOG_TARGET_ERROR(target, "not halted (add context breakpoint)");
1332  return ERROR_TARGET_NOT_HALTED;
1333  }
1335 }
1336 
1338  struct breakpoint *breakpoint)
1339 {
1340  if (target->state != TARGET_HALTED) {
1341  LOG_TARGET_ERROR(target, "not halted (add hybrid breakpoint)");
1342  return ERROR_TARGET_NOT_HALTED;
1343  }
1345 }
1346 
1348  struct breakpoint *breakpoint)
1349 {
1351 }
1352 
1354  struct watchpoint *watchpoint)
1355 {
1356  if (target->state != TARGET_HALTED) {
1357  LOG_TARGET_ERROR(target, "not halted (add watchpoint)");
1358  return ERROR_TARGET_NOT_HALTED;
1359  }
1361 }
1363  struct watchpoint *watchpoint)
1364 {
1366 }
1368  struct watchpoint **hit_watchpoint)
1369 {
1370  if (target->state != TARGET_HALTED) {
1371  LOG_TARGET_ERROR(target, "not halted (hit watchpoint)");
1372  return ERROR_TARGET_NOT_HALTED;
1373  }
1374 
1375  if (!target->type->hit_watchpoint) {
1376  /* For backward compatible, if hit_watchpoint is not implemented,
1377  * return error such that gdb_server will not take the nonsense
1378  * information. */
1379  return ERROR_NOT_IMPLEMENTED;
1380  }
1381 
1382  return target->type->hit_watchpoint(target, hit_watchpoint);
1383 }
1384 
1385 const char *target_get_gdb_arch(const struct target *target)
1386 {
1387  if (!target->type->get_gdb_arch)
1388  return NULL;
1389  return target->type->get_gdb_arch(target);
1390 }
1391 
1393  struct reg **reg_list[], int *reg_list_size,
1394  enum target_register_class reg_class)
1395 {
1396  int result = ERROR_FAIL;
1397 
1398  if (!target_was_examined(target)) {
1399  LOG_TARGET_ERROR(target, "not examined");
1400  result = ERROR_TARGET_NOT_EXAMINED;
1401  goto done;
1402  }
1403 
1404  result = target->type->get_gdb_reg_list(target, reg_list,
1405  reg_list_size, reg_class);
1406 
1407 done:
1408  if (result != ERROR_OK) {
1409  *reg_list = NULL;
1410  *reg_list_size = 0;
1411  }
1412  return result;
1413 }
1414 
1416  struct reg **reg_list[], int *reg_list_size,
1417  enum target_register_class reg_class)
1418 {
1421  reg_list_size, reg_class) == ERROR_OK)
1422  return ERROR_OK;
1423  return target_get_gdb_reg_list(target, reg_list, reg_list_size, reg_class);
1424 }
1425 
1427 {
1428  /*
1429  * exclude all the targets that don't provide get_gdb_reg_list
1430  * or that have explicit gdb_max_connection == 0
1431  */
1433 }
1434 
1436  bool current, target_addr_t address, bool handle_breakpoints)
1437 {
1438  int retval;
1439 
1440  if (!target_was_examined(target)) {
1441  LOG_TARGET_ERROR(target, "not examined");
1443  }
1444 
1446 
1447  retval = target->type->step(target, current, address, handle_breakpoints);
1448  if (retval != ERROR_OK)
1449  return retval;
1450 
1452 
1453  return retval;
1454 }
1455 
1457 {
1458  if (target->state != TARGET_HALTED) {
1459  LOG_TARGET_ERROR(target, "not halted (gdb fileio)");
1460  return ERROR_TARGET_NOT_HALTED;
1461  }
1463 }
1464 
1465 int target_gdb_fileio_end(struct target *target, int retcode, int fileio_errno, bool ctrl_c)
1466 {
1467  if (target->state != TARGET_HALTED) {
1468  LOG_TARGET_ERROR(target, "not halted (gdb fileio end)");
1469  return ERROR_TARGET_NOT_HALTED;
1470  }
1471  return target->type->gdb_fileio_end(target, retcode, fileio_errno, ctrl_c);
1472 }
1473 
1475 {
1476  unsigned int bits = target_address_bits(target);
1477  if (sizeof(target_addr_t) * 8 == bits)
1478  return (target_addr_t) -1;
1479  else
1480  return (((target_addr_t) 1) << bits) - 1;
1481 }
1482 
1483 unsigned int target_address_bits(struct target *target)
1484 {
1485  if (target->type->address_bits)
1486  return target->type->address_bits(target);
1487  return 32;
1488 }
1489 
1490 unsigned int target_data_bits(struct target *target)
1491 {
1492  if (target->type->data_bits)
1493  return target->type->data_bits(target);
1494  return 32;
1495 }
1496 
1497 static int target_profiling(struct target *target, uint32_t *samples,
1498  uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
1499 {
1500  return target->type->profiling(target, samples, max_num_samples,
1501  num_samples, seconds);
1502 }
1503 
1504 static int handle_target(void *priv);
1505 
1506 static int target_init_one(struct command_context *cmd_ctx,
1507  struct target *target)
1508 {
1511 
1512  struct target_type *type = target->type;
1513  if (!type->examine)
1514  type->examine = default_examine;
1515 
1516  if (!type->check_reset)
1517  type->check_reset = default_check_reset;
1518 
1519  assert(type->init_target);
1520 
1521  int retval = type->init_target(cmd_ctx, target);
1522  if (retval != ERROR_OK) {
1523  LOG_ERROR("target '%s' init failed", target_name(target));
1524  return retval;
1525  }
1526 
1527  /* Sanity-check MMU support ... stub in what we must, to help
1528  * implement it in stages, but warn if we need to do so.
1529  */
1530  if (type->mmu) {
1531  if (!type->virt2phys) {
1532  LOG_ERROR("type '%s' is missing virt2phys", target_name(target));
1533  type->virt2phys = identity_virt2phys;
1534  }
1535  } else {
1536  /* Make sure no-MMU targets all behave the same: make no
1537  * distinction between physical and virtual addresses, and
1538  * ensure that virt2phys() is always an identity mapping.
1539  */
1540  if (type->write_phys_memory || type->read_phys_memory || type->virt2phys)
1541  LOG_WARNING("type '%s' has bad MMU hooks", target_name(target));
1542 
1543  type->mmu = no_mmu;
1544  type->write_phys_memory = type->write_memory;
1545  type->read_phys_memory = type->read_memory;
1546  type->virt2phys = identity_virt2phys;
1547  }
1548 
1549  if (!target->type->read_buffer)
1551 
1552  if (!target->type->write_buffer)
1554 
1557 
1558  if (!target->type->gdb_fileio_end)
1560 
1561  if (!target->type->profiling)
1563 
1564  return ERROR_OK;
1565 }
1566 
1567 static int target_init(struct command_context *cmd_ctx)
1568 {
1569  struct target *target;
1570  int retval;
1571 
1572  for (target = all_targets; target; target = target->next) {
1573  retval = target_init_one(cmd_ctx, target);
1574  if (retval != ERROR_OK)
1575  return retval;
1576  }
1577 
1578  if (!all_targets)
1579  return ERROR_OK;
1580 
1581  retval = target_register_user_commands(cmd_ctx);
1582  if (retval != ERROR_OK)
1583  return retval;
1584 
1587  if (retval != ERROR_OK)
1588  return retval;
1589 
1590  return ERROR_OK;
1591 }
1592 
1593 COMMAND_HANDLER(handle_target_init_command)
1594 {
1595  int retval;
1596 
1597  if (CMD_ARGC != 0)
1599 
1600  static bool target_initialized;
1601  if (target_initialized) {
1602  LOG_INFO("'target init' has already been called");
1603  return ERROR_OK;
1604  }
1605  target_initialized = true;
1606 
1607  retval = command_run_line(CMD_CTX, "init_targets");
1608  if (retval != ERROR_OK)
1609  return retval;
1610 
1611  retval = command_run_line(CMD_CTX, "init_target_events");
1612  if (retval != ERROR_OK)
1613  return retval;
1614 
1615  retval = command_run_line(CMD_CTX, "init_board");
1616  if (retval != ERROR_OK)
1617  return retval;
1618 
1619  LOG_DEBUG("Initializing targets...");
1620  return target_init(CMD_CTX);
1621 }
1622 
1623 int target_register_event_callback(int (*callback)(struct target *target,
1624  enum target_event event, void *priv), void *priv)
1625 {
1626  struct target_event_callback **callbacks_p = &target_event_callbacks;
1627 
1628  if (!callback)
1630 
1631  if (*callbacks_p) {
1632  while ((*callbacks_p)->next)
1633  callbacks_p = &((*callbacks_p)->next);
1634  callbacks_p = &((*callbacks_p)->next);
1635  }
1636 
1637  (*callbacks_p) = malloc(sizeof(struct target_event_callback));
1638  (*callbacks_p)->callback = callback;
1639  (*callbacks_p)->priv = priv;
1640  (*callbacks_p)->next = NULL;
1641 
1642  return ERROR_OK;
1643 }
1644 
1646  enum target_reset_mode reset_mode, void *priv), void *priv)
1647 {
1648  struct target_reset_callback *entry;
1649 
1650  if (!callback)
1652 
1653  entry = malloc(sizeof(struct target_reset_callback));
1654  if (!entry) {
1655  LOG_ERROR("error allocating buffer for reset callback entry");
1657  }
1658 
1659  entry->callback = callback;
1660  entry->priv = priv;
1661  list_add(&entry->list, &target_reset_callback_list);
1662 
1663 
1664  return ERROR_OK;
1665 }
1666 
1668  size_t len, uint8_t *data, void *priv), void *priv)
1669 {
1670  struct target_trace_callback *entry;
1671 
1672  if (!callback)
1674 
1675  entry = malloc(sizeof(struct target_trace_callback));
1676  if (!entry) {
1677  LOG_ERROR("error allocating buffer for trace callback entry");
1679  }
1680 
1681  entry->callback = callback;
1682  entry->priv = priv;
1683  list_add(&entry->list, &target_trace_callback_list);
1684 
1685 
1686  return ERROR_OK;
1687 }
1688 
1689 static int target_timer_callback_set_period(struct target_timer_callback *cb, unsigned int time_ms)
1690 {
1691  if (!cb)
1692  return ERROR_FAIL;
1693 
1694  cb->time_ms = time_ms;
1695  cb->when = timeval_ms() + time_ms;
1697 
1698  return ERROR_OK;
1699 }
1700 
1702  unsigned int time_ms, enum target_timer_type type, void *priv)
1703 {
1704  struct target_timer_callback **callbacks_p = &target_timer_callbacks;
1705 
1706  if (!callback)
1708 
1709  if (*callbacks_p) {
1710  while ((*callbacks_p)->next)
1711  callbacks_p = &((*callbacks_p)->next);
1712  callbacks_p = &((*callbacks_p)->next);
1713  }
1714 
1715  (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
1716  (*callbacks_p)->callback = callback;
1717  (*callbacks_p)->type = type;
1718  (*callbacks_p)->removed = false;
1719 
1720  (*callbacks_p)->priv = priv;
1721  (*callbacks_p)->next = NULL;
1722 
1723  return target_timer_callback_set_period(*callbacks_p, time_ms);
1724 }
1725 
1727  enum target_event event, void *priv), void *priv)
1728 {
1731 
1732  if (!callback)
1734 
1735  while (c) {
1736  struct target_event_callback *next = c->next;
1737  if ((c->callback == callback) && (c->priv == priv)) {
1738  *p = next;
1739  free(c);
1740  return ERROR_OK;
1741  } else
1742  p = &(c->next);
1743  c = next;
1744  }
1745 
1746  return ERROR_OK;
1747 }
1748 
1750  enum target_reset_mode reset_mode, void *priv), void *priv)
1751 {
1752  struct target_reset_callback *entry;
1753 
1754  if (!callback)
1756 
1757  list_for_each_entry(entry, &target_reset_callback_list, list) {
1758  if (entry->callback == callback && entry->priv == priv) {
1759  list_del(&entry->list);
1760  free(entry);
1761  break;
1762  }
1763  }
1764 
1765  return ERROR_OK;
1766 }
1767 
1769  size_t len, uint8_t *data, void *priv), void *priv)
1770 {
1771  struct target_trace_callback *entry;
1772 
1773  if (!callback)
1775 
1776  list_for_each_entry(entry, &target_trace_callback_list, list) {
1777  if (entry->callback == callback && entry->priv == priv) {
1778  list_del(&entry->list);
1779  free(entry);
1780  break;
1781  }
1782  }
1783 
1784  return ERROR_OK;
1785 }
1786 
1788  void *priv)
1789 {
1790  if (!callback)
1791  return NULL;
1792 
1794  c; c = c->next) {
1795  if (c->callback == callback && c->priv == priv)
1796  return c;
1797  }
1798 
1799  return NULL;
1800 }
1801 
1803 {
1804  if (!callback)
1806 
1808  if (cb) {
1809  cb->removed = true;
1810  return ERROR_OK;
1811  }
1812 
1813  return ERROR_FAIL;
1814 }
1815 
1817 {
1819  struct target_event_callback *next_callback;
1820 
1821  if (event == TARGET_EVENT_HALTED) {
1822  /* execute early halted first */
1824  }
1825 
1826  LOG_DEBUG("target event %i (%s) for core %s", event,
1827  target_event_name(event),
1828  target_name(target));
1829 
1830  target_handle_event(target, event);
1831 
1832  while (callback) {
1833  next_callback = callback->next;
1834  callback->callback(target, event, callback->priv);
1835  callback = next_callback;
1836  }
1837 
1838  return ERROR_OK;
1839 }
1840 
1842 {
1844 
1845  LOG_DEBUG("target reset %i (%s)", reset_mode,
1846  nvp_value2name(nvp_reset_modes, reset_mode)->name);
1847 
1848  list_for_each_entry(callback, &target_reset_callback_list, list)
1849  callback->callback(target, reset_mode, callback->priv);
1850 
1851  return ERROR_OK;
1852 }
1853 
1854 int target_call_trace_callbacks(struct target *target, size_t len, uint8_t *data)
1855 {
1857 
1858  list_for_each_entry(callback, &target_trace_callback_list, list)
1859  callback->callback(target, len, data, callback->priv);
1860 
1861  return ERROR_OK;
1862 }
1863 
1865  struct target_timer_callback *cb, int64_t *now)
1866 {
1867  cb->when = *now + cb->time_ms;
1868  return ERROR_OK;
1869 }
1870 
1872  int64_t *now)
1873 {
1874  cb->callback(cb->priv);
1875 
1876  if (cb->type == TARGET_TIMER_TYPE_PERIODIC)
1878 
1880 }
1881 
1883 {
1884  static bool callback_processing;
1885 
1886  /* Do not allow nesting */
1887  if (callback_processing)
1888  return ERROR_OK;
1889 
1890  callback_processing = true;
1891 
1892  keep_alive();
1893 
1894  int64_t now = timeval_ms();
1895 
1896  /* Initialize to a default value that's a ways into the future.
1897  * The loop below will make it closer to now if there are
1898  * callbacks that want to be called sooner. */
1899  target_timer_next_event_value = now + 1000;
1900 
1901  /* Store an address of the place containing a pointer to the
1902  * next item; initially, that's a standalone "root of the
1903  * list" variable. */
1905  while (callback && *callback) {
1906  if ((*callback)->removed) {
1907  struct target_timer_callback *p = *callback;
1908  *callback = (*callback)->next;
1909  free(p);
1910  continue;
1911  }
1912 
1913  bool call_it = (*callback)->callback &&
1914  ((!checktime && (*callback)->type == TARGET_TIMER_TYPE_PERIODIC) ||
1915  now >= (*callback)->when);
1916 
1917  if (call_it)
1919 
1920  if (!(*callback)->removed && (*callback)->when < target_timer_next_event_value)
1921  target_timer_next_event_value = (*callback)->when;
1922 
1923  callback = &(*callback)->next;
1924  }
1925 
1926  callback_processing = false;
1927  return ERROR_OK;
1928 }
1929 
1931 {
1933 }
1934 
1935 /* invoke periodic callbacks immediately */
1937 {
1939 }
1940 
1942 {
1944 }
1945 
1946 /* Prints the working area layout for debug purposes */
1947 static void print_wa_layout(struct target *target)
1948 {
1949  struct working_area *c = target->working_areas;
1950 
1951  while (c) {
1952  LOG_DEBUG("%c%c " TARGET_ADDR_FMT "-" TARGET_ADDR_FMT " (%" PRIu32 " bytes)",
1953  c->backup ? 'b' : ' ', c->free ? ' ' : '*',
1954  c->address, c->address + c->size - 1, c->size);
1955  c = c->next;
1956  }
1957 }
1958 
1959 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1960 static void target_split_working_area(struct working_area *area, uint32_t size)
1961 {
1962  assert(area->free); /* Shouldn't split an allocated area */
1963  assert(size <= area->size); /* Caller should guarantee this */
1964 
1965  /* Split only if not already the right size */
1966  if (size < area->size) {
1967  struct working_area *new_wa = malloc(sizeof(*new_wa));
1968 
1969  if (!new_wa)
1970  return;
1971 
1972  new_wa->next = area->next;
1973  new_wa->size = area->size - size;
1974  new_wa->address = area->address + size;
1975  new_wa->backup = NULL;
1976  new_wa->user = NULL;
1977  new_wa->free = true;
1978 
1979  area->next = new_wa;
1980  area->size = size;
1981 
1982  /* If backup memory was allocated to this area, it has the wrong size
1983  * now so free it and it will be reallocated if/when needed */
1984  free(area->backup);
1985  area->backup = NULL;
1986  }
1987 }
1988 
1989 /* Merge all adjacent free areas into one */
1991 {
1992  struct working_area *c = target->working_areas;
1993 
1994  while (c && c->next) {
1995  assert(c->next->address == c->address + c->size); /* This is an invariant */
1996 
1997  /* Find two adjacent free areas */
1998  if (c->free && c->next->free) {
1999  /* Merge the last into the first */
2000  c->size += c->next->size;
2001 
2002  /* Remove the last */
2003  struct working_area *to_be_freed = c->next;
2004  c->next = c->next->next;
2005  free(to_be_freed->backup);
2006  free(to_be_freed);
2007 
2008  /* If backup memory was allocated to the remaining area, it's has
2009  * the wrong size now */
2010  free(c->backup);
2011  c->backup = NULL;
2012  } else {
2013  c = c->next;
2014  }
2015  }
2016 }
2017 
2018 int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
2019 {
2020  /* Reevaluate working area address based on MMU state*/
2021  if (!target->working_areas) {
2022  int retval;
2023  bool enabled;
2024 
2025  retval = target->type->mmu(target, &enabled);
2026  if (retval != ERROR_OK)
2027  return retval;
2028 
2029  if (!enabled) {
2031  LOG_DEBUG("MMU disabled, using physical "
2032  "address for working memory " TARGET_ADDR_FMT,
2035  } else {
2036  LOG_ERROR("No working memory available. "
2037  "Specify -work-area-phys to target.");
2039  }
2040  } else {
2042  LOG_DEBUG("MMU enabled, using virtual "
2043  "address for working memory " TARGET_ADDR_FMT,
2046  } else {
2047  LOG_ERROR("No working memory available. "
2048  "Specify -work-area-virt to target.");
2050  }
2051  }
2052 
2053  /* Set up initial working area on first call */
2054  struct working_area *new_wa = malloc(sizeof(*new_wa));
2055  if (new_wa) {
2056  new_wa->next = NULL;
2057  new_wa->size = ALIGN_DOWN(target->working_area_size, 4); /* 4-byte align */
2058  new_wa->address = target->working_area;
2059  new_wa->backup = NULL;
2060  new_wa->user = NULL;
2061  new_wa->free = true;
2062  }
2063 
2064  target->working_areas = new_wa;
2065  }
2066 
2067  /* only allocate multiples of 4 byte */
2068  size = ALIGN_UP(size, 4);
2069 
2070  struct working_area *c = target->working_areas;
2071 
2072  /* Find the first large enough working area */
2073  while (c) {
2074  if (c->free && c->size >= size)
2075  break;
2076  c = c->next;
2077  }
2078 
2079  if (!c)
2081 
2082  /* Split the working area into the requested size */
2084 
2085  LOG_DEBUG("allocated new working area of %" PRIu32 " bytes at address " TARGET_ADDR_FMT,
2086  size, c->address);
2087 
2088  if (target->backup_working_area) {
2089  if (!c->backup) {
2090  c->backup = malloc(c->size);
2091  if (!c->backup) {
2092  LOG_TARGET_ERROR(target, "No memory for working area backup");
2094  return ERROR_FAIL;
2095  }
2096  }
2097 
2098  int retval = target_read_memory(target, c->address, 4, c->size / 4, c->backup);
2099  if (retval != ERROR_OK) {
2100  LOG_TARGET_ERROR(target, "Working area backup failed");
2102  return retval;
2103  }
2104  }
2105 
2106  /* mark as used, and return the new (reused) area */
2107  c->free = false;
2108  *area = c;
2109 
2110  /* user pointer */
2111  c->user = area;
2112 
2114 
2115  return ERROR_OK;
2116 }
2117 
2118 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
2119 {
2120  int retval;
2121 
2122  retval = target_alloc_working_area_try(target, size, area);
2124  LOG_WARNING("not enough working area available(requested %"PRIu32")", size);
2125  return retval;
2126 
2127 }
2128 
2129 static int target_restore_working_area(struct target *target, struct working_area *area)
2130 {
2131  if (!target->backup_working_area || !area->backup)
2132  return ERROR_OK;
2133 
2134  int retval = target_write_memory(target, area->address, 4,
2135  area->size / 4, area->backup);
2136  if (retval != ERROR_OK) {
2137  LOG_TARGET_ERROR(target, "failed to restore %" PRIu32
2138  " bytes of working area at address " TARGET_ADDR_FMT,
2139  area->size, area->address);
2140  LOG_TARGET_INFO(target, "'resume' would fail, reset the target");
2141  }
2142  return retval;
2143 }
2144 
2145 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
2146 static int target_free_working_area_restore(struct target *target, struct working_area *area, bool restore)
2147 {
2148  if (!area || area->free)
2149  return ERROR_OK;
2150 
2151  int retval = ERROR_OK;
2152  if (restore)
2153  retval = target_restore_working_area(target, area);
2154 
2155  area->free = true;
2156 
2157  LOG_DEBUG("freed %" PRIu32 " bytes of working area at address " TARGET_ADDR_FMT,
2158  area->size, area->address);
2159 
2160  /* mark user pointer invalid */
2161  /* TODO: Is this really safe? It points to some previous caller's memory.
2162  * How could we know that the area pointer is still in that place and not
2163  * some other vital data? What's the purpose of this, anyway? */
2164  *area->user = NULL;
2165  area->user = NULL;
2166 
2168 
2170 
2171  return retval;
2172 }
2173 
2175 {
2176  return target_free_working_area_restore(target, area, true);
2177 }
2178 
2179 /* free resources and restore memory, if restoring memory fails,
2180  * free up resources anyway
2181  */
2182 static void target_free_all_working_areas_restore(struct target *target, bool restore)
2183 {
2184  struct working_area *c = target->working_areas;
2185 
2186  LOG_DEBUG("freeing all working areas");
2187 
2188  /* Loop through all areas, restoring the allocated ones and marking them as free */
2189  while (c) {
2190  if (!c->free) {
2191  if (restore)
2193  c->free = true;
2194  *c->user = NULL; /* Same as above */
2195  c->user = NULL;
2196  }
2197  c = c->next;
2198  }
2199 
2200  /* Run a merge pass to combine all areas into one */
2202 
2204 }
2205 
2207 {
2209 
2210  /* Now we have none or only one working area marked as free */
2211  if (target->working_areas) {
2212  /* Free the last one to allow on-the-fly moving and resizing */
2216  }
2217 }
2218 
2219 /* Find the largest number of bytes that can be allocated */
2221 {
2222  struct working_area *c = target->working_areas;
2223  uint32_t max_size = 0;
2224 
2225  if (!c)
2226  return ALIGN_DOWN(target->working_area_size, 4);
2227 
2228  while (c) {
2229  if (c->free && max_size < c->size)
2230  max_size = c->size;
2231 
2232  c = c->next;
2233  }
2234 
2235  return max_size;
2236 }
2237 
2238 static void free_smp_target_list(struct list_head *smp_targets)
2239 {
2240  assert(smp_targets);
2241  if (smp_targets == &empty_smp_targets)
2242  return;
2243 
2244  struct target_list *head, *tmp;
2245  list_for_each_entry_safe(head, tmp, smp_targets, lh) {
2246  list_del(&head->lh);
2247  head->target->smp = false;
2248  head->target->smp_id = 0;
2249  head->target->smp_targets = &empty_smp_targets;
2250  free(head);
2251  }
2252  free(smp_targets);
2253 }
2254 
2255 static void target_destroy(struct target *target)
2256 {
2259 
2260  if (target->type->deinit_target)
2262 
2263  if (target->semihosting)
2264  free(target->semihosting->basedir);
2265  free(target->semihosting);
2266 
2268 
2269  struct target_event_action *teap, *temp;
2271  list_del(&teap->list);
2272  Jim_DecrRefCount(teap->interp, teap->body);
2273  free(teap);
2274  }
2275 
2277 
2279 
2281 
2282  free(target->gdb_port_override);
2283  free(target->type);
2284  free(target->trace_info);
2285  free(target->fileio_info);
2286  free(target->cmd_name);
2287  free(target);
2288 }
2289 
2290 void target_quit(void)
2291 {
2293  while (pe) {
2294  struct target_event_callback *t = pe->next;
2295  free(pe);
2296  pe = t;
2297  }
2299 
2301  while (pt) {
2302  struct target_timer_callback *t = pt->next;
2303  free(pt);
2304  pt = t;
2305  }
2307 
2308  for (struct target *target = all_targets; target;) {
2309  struct target *tmp;
2310 
2311  tmp = target->next;
2313  target = tmp;
2314  }
2315 
2316  all_targets = NULL;
2317 }
2318 
2320 {
2321  if (!target) {
2322  LOG_WARNING("No target has been configured");
2323  return ERROR_OK;
2324  }
2325 
2326  if (target->state != TARGET_HALTED)
2327  return ERROR_OK;
2328 
2329  return target->type->arch_state(target);
2330 }
2331 
2333  struct gdb_fileio_info *fileio_info)
2334 {
2335  /* If target does not support semi-hosting function, target
2336  has no need to provide .get_gdb_fileio_info callback.
2337  It just return ERROR_FAIL and gdb_server will return "Txx"
2338  as target halted every time. */
2339  return ERROR_FAIL;
2340 }
2341 
2343  int retcode, int fileio_errno, bool ctrl_c)
2344 {
2345  return ERROR_OK;
2346 }
2347 
2348 int target_profiling_default(struct target *target, uint32_t *samples,
2349  uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
2350 {
2351  int64_t then = timeval_ms() + seconds * 1000LL;
2352 
2353  LOG_INFO("Starting profiling. Halting and resuming the"
2354  " target as often as we can...");
2355 
2356  uint32_t sample_count = 0;
2357  /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2358  struct reg *reg = register_get_by_name(target->reg_cache, "pc", true);
2359 
2360  int retval = ERROR_OK;
2361  for (;;) {
2363  if (target->state == TARGET_HALTED) {
2364  uint32_t t = buf_get_u32(reg->value, 0, 32);
2365  samples[sample_count++] = t;
2366  /* current pc, addr = 0, do not handle breakpoints, not debugging */
2367  retval = target_resume(target, true, 0, false, false);
2369  alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2370  } else if (target->state == TARGET_RUNNING) {
2371  /* We want to quickly sample the PC. */
2372  retval = target_halt(target);
2373  } else {
2374  LOG_INFO("Target not halted or running");
2375  retval = ERROR_OK;
2376  break;
2377  }
2378 
2379  if (retval != ERROR_OK)
2380  break;
2381 
2382  if (sample_count >= max_num_samples || timeval_ms() >= then) {
2383  LOG_INFO("Profiling completed. %" PRIu32 " samples.", sample_count);
2384  break;
2385  }
2386  }
2387 
2388  *num_samples = sample_count;
2389  return retval;
2390 }
2391 
2393  const char **insn_set)
2394 {
2395  if (target->type->insn_set)
2396  return target->type->insn_set(cmd, target, insn_set);
2397 
2398  command_print(cmd, "Instruction-set detection not implemented on target %s",
2399  target_name(target));
2400  command_print(cmd, "Change target or specify one of the instruction set:");
2402 
2403  return ERROR_NOT_IMPLEMENTED;
2404 }
2405 
2406 /* Single aligned words are guaranteed to use 16 or 32 bit access
2407  * mode respectively, otherwise data is handled as quickly as
2408  * possible
2409  */
2410 int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
2411 {
2412  LOG_DEBUG("writing buffer of %" PRIu32 " byte at " TARGET_ADDR_FMT,
2413  size, address);
2414 
2415  if (!target_memory_ready(target)) {
2416  LOG_TARGET_ERROR(target, "Memory not ready");
2417  return ERROR_FAIL;
2418  }
2419 
2420  if (size == 0)
2421  return ERROR_OK;
2422 
2423  if ((address + size - 1) < address) {
2424  /* GDB can request this when e.g. PC is 0xfffffffc */
2425  LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT ", 0x%08" PRIx32 ")",
2426  address,
2427  size);
2428  return ERROR_FAIL;
2429  }
2430 
2432 }
2433 
2435  target_addr_t address, uint32_t count, const uint8_t *buffer)
2436 {
2437  uint32_t size;
2438  unsigned int data_bytes = target_data_bits(target) / 8;
2439 
2440  /* Align up to maximum bytes. The loop condition makes sure the next pass
2441  * will have something to do with the size we leave to it. */
2442  for (size = 1;
2443  size < data_bytes && count >= size * 2 + (address & size);
2444  size *= 2) {
2445  if (address & size) {
2446  int retval = target_write_memory(target, address, size, 1, buffer);
2447  if (retval != ERROR_OK)
2448  return retval;
2449  address += size;
2450  count -= size;
2451  buffer += size;
2452  }
2453  }
2454 
2455  /* Write the data with as large access size as possible. */
2456  for (; size > 0; size /= 2) {
2457  uint32_t aligned = count - count % size;
2458  if (aligned > 0) {
2459  int retval = target_write_memory(target, address, size, aligned / size, buffer);
2460  if (retval != ERROR_OK)
2461  return retval;
2462  address += aligned;
2463  count -= aligned;
2464  buffer += aligned;
2465  }
2466  }
2467 
2468  return ERROR_OK;
2469 }
2470 
2471 /* Single aligned words are guaranteed to use 16 or 32 bit access
2472  * mode respectively, otherwise data is handled as quickly as
2473  * possible
2474  */
2476 {
2477  LOG_DEBUG("reading buffer of %" PRIu32 " byte at " TARGET_ADDR_FMT,
2478  size, address);
2479 
2480  if (!target_memory_ready(target)) {
2481  LOG_TARGET_ERROR(target, "Memory not ready");
2482  return ERROR_FAIL;
2483  }
2484 
2485  if (size == 0)
2486  return ERROR_OK;
2487 
2488  if ((address + size - 1) < address) {
2489  /* GDB can request this when e.g. PC is 0xfffffffc */
2490  LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT ", 0x%08" PRIx32 ")",
2491  address,
2492  size);
2493  return ERROR_FAIL;
2494  }
2495 
2497 }
2498 
2500 {
2501  uint32_t size;
2502  unsigned int data_bytes = target_data_bits(target) / 8;
2503 
2504  /* Align up to maximum bytes. The loop condition makes sure the next pass
2505  * will have something to do with the size we leave to it. */
2506  for (size = 1;
2507  size < data_bytes && count >= size * 2 + (address & size);
2508  size *= 2) {
2509  if (address & size) {
2510  int retval = target_read_memory(target, address, size, 1, buffer);
2511  if (retval != ERROR_OK)
2512  return retval;
2513  address += size;
2514  count -= size;
2515  buffer += size;
2516  }
2517  }
2518 
2519  /* Read the data with as large access size as possible. */
2520  for (; size > 0; size /= 2) {
2521  uint32_t aligned = count - count % size;
2522  if (aligned > 0) {
2523  int retval = target_read_memory(target, address, size, aligned / size, buffer);
2524  if (retval != ERROR_OK)
2525  return retval;
2526  address += aligned;
2527  count -= aligned;
2528  buffer += aligned;
2529  }
2530  }
2531 
2532  return ERROR_OK;
2533 }
2534 
2535 int target_checksum_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t *crc)
2536 {
2537  int retval;
2538  if (!target_was_examined(target)) {
2539  LOG_TARGET_ERROR(target, "not examined");
2541  }
2542 
2543  if (target->type->checksum_memory) {
2544  retval = target->type->checksum_memory(target, address, size, crc);
2545  if (retval == ERROR_OK)
2546  return ERROR_OK;
2547  } else {
2548  LOG_TARGET_INFO(target, "doesn't support fast checksum_memory, using slow read memory");
2549  }
2550 
2551  uint8_t *buffer = malloc(size);
2552  if (!buffer) {
2553  LOG_ERROR("error allocating buffer for section (%" PRIu32 " bytes)", size);
2554  return ERROR_FAIL;
2555  }
2556 
2558 
2559  if (retval == ERROR_OK)
2560  retval = image_calculate_checksum(buffer, size, crc);
2561 
2562  free(buffer);
2563  return retval;
2564 }
2565 
2567  struct target_memory_check_block *blocks, unsigned int num_blocks,
2568  uint8_t erased_value, unsigned int *checked)
2569 {
2570  if (!target_was_examined(target)) {
2571  LOG_TARGET_ERROR(target, "not examined");
2573  }
2574 
2576  return ERROR_NOT_IMPLEMENTED;
2577 
2578  return target->type->blank_check_memory(target, blocks, num_blocks,
2579  erased_value, checked);
2580 }
2581 
2583 {
2584  uint8_t value_buf[8];
2585 
2586  int retval = target_read_memory(target, address, 8, 1, value_buf);
2587 
2588  if (retval == ERROR_OK) {
2589  *value = target_buffer_get_u64(target, value_buf);
2590  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64,
2591  address,
2592  *value);
2593  } else {
2594  *value = 0x0;
2595  LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2596  address);
2597  }
2598 
2599  return retval;
2600 }
2601 
2603 {
2604  uint8_t value_buf[4];
2605 
2606  int retval = target_read_memory(target, address, 4, 1, value_buf);
2607 
2608  if (retval == ERROR_OK) {
2609  *value = target_buffer_get_u32(target, value_buf);
2610  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32,
2611  address,
2612  *value);
2613  } else {
2614  *value = 0x0;
2615  LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2616  address);
2617  }
2618 
2619  return retval;
2620 }
2621 
2623 {
2624  uint8_t value_buf[2];
2625 
2626  int retval = target_read_memory(target, address, 2, 1, value_buf);
2627 
2628  if (retval == ERROR_OK) {
2629  *value = target_buffer_get_u16(target, value_buf);
2630  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%4.4" PRIx16,
2631  address,
2632  *value);
2633  } else {
2634  *value = 0x0;
2635  LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2636  address);
2637  }
2638 
2639  return retval;
2640 }
2641 
2643 {
2644  int retval = target_read_memory(target, address, 1, 1, value);
2645 
2646  if (retval == ERROR_OK) {
2647  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2648  address,
2649  *value);
2650  } else {
2651  *value = 0x0;
2652  LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2653  address);
2654  }
2655 
2656  return retval;
2657 }
2658 
2660 {
2661  int retval;
2662  uint8_t value_buf[8];
2663 
2664  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64,
2665  address,
2666  value);
2667 
2668  target_buffer_set_u64(target, value_buf, value);
2669  retval = target_write_memory(target, address, 8, 1, value_buf);
2670  if (retval != ERROR_OK)
2671  LOG_DEBUG("failed: %i", retval);
2672 
2673  return retval;
2674 }
2675 
2677 {
2678  int retval;
2679  uint8_t value_buf[4];
2680 
2681  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32,
2682  address,
2683  value);
2684 
2685  target_buffer_set_u32(target, value_buf, value);
2686  retval = target_write_memory(target, address, 4, 1, value_buf);
2687  if (retval != ERROR_OK)
2688  LOG_DEBUG("failed: %i", retval);
2689 
2690  return retval;
2691 }
2692 
2694 {
2695  int retval;
2696  uint8_t value_buf[2];
2697 
2698  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx16,
2699  address,
2700  value);
2701 
2702  target_buffer_set_u16(target, value_buf, value);
2703  retval = target_write_memory(target, address, 2, 1, value_buf);
2704  if (retval != ERROR_OK)
2705  LOG_DEBUG("failed: %i", retval);
2706 
2707  return retval;
2708 }
2709 
2711 {
2712  int retval;
2713 
2714  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2715  address, value);
2716 
2717  retval = target_write_memory(target, address, 1, 1, &value);
2718  if (retval != ERROR_OK)
2719  LOG_DEBUG("failed: %i", retval);
2720 
2721  return retval;
2722 }
2723 
2725 {
2726  int retval;
2727  uint8_t value_buf[8];
2728 
2729  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64,
2730  address,
2731  value);
2732 
2733  target_buffer_set_u64(target, value_buf, value);
2734  retval = target_write_phys_memory(target, address, 8, 1, value_buf);
2735  if (retval != ERROR_OK)
2736  LOG_DEBUG("failed: %i", retval);
2737 
2738  return retval;
2739 }
2740 
2742 {
2743  int retval;
2744  uint8_t value_buf[4];
2745 
2746  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32,
2747  address,
2748  value);
2749 
2750  target_buffer_set_u32(target, value_buf, value);
2751  retval = target_write_phys_memory(target, address, 4, 1, value_buf);
2752  if (retval != ERROR_OK)
2753  LOG_DEBUG("failed: %i", retval);
2754 
2755  return retval;
2756 }
2757 
2759 {
2760  int retval;
2761  uint8_t value_buf[2];
2762 
2763  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx16,
2764  address,
2765  value);
2766 
2767  target_buffer_set_u16(target, value_buf, value);
2768  retval = target_write_phys_memory(target, address, 2, 1, value_buf);
2769  if (retval != ERROR_OK)
2770  LOG_DEBUG("failed: %i", retval);
2771 
2772  return retval;
2773 }
2774 
2776 {
2777  int retval;
2778 
2779  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2780  address, value);
2781 
2782  retval = target_write_phys_memory(target, address, 1, 1, &value);
2783  if (retval != ERROR_OK)
2784  LOG_DEBUG("failed: %i", retval);
2785 
2786  return retval;
2787 }
2788 
2789 static int find_target(struct command_invocation *cmd, const char *name)
2790 {
2791  struct target *target = get_target(name);
2792  if (!target) {
2793  command_print(cmd, "Target: %s is unknown, try one of:\n", name);
2794  return ERROR_FAIL;
2795  }
2796  if (!target->tap->enabled) {
2797  command_print(cmd, "Target: TAP %s is disabled, "
2798  "can't be the current target\n",
2799  target->tap->dotted_name);
2800  return ERROR_FAIL;
2801  }
2802 
2803  cmd->ctx->current_target = target;
2804  if (cmd->ctx->current_target_override)
2805  cmd->ctx->current_target_override = target;
2806 
2807  return ERROR_OK;
2808 }
2809 
2810 
2811 COMMAND_HANDLER(handle_targets_command)
2812 {
2813  int retval = ERROR_OK;
2814  if (CMD_ARGC == 1) {
2815  retval = find_target(CMD, CMD_ARGV[0]);
2816  if (retval == ERROR_OK) {
2817  /* we're done! */
2818  return retval;
2819  }
2820  }
2821 
2822  unsigned int index = 0;
2823  command_print(CMD, " TargetName Type Endian TapName State ");
2824  command_print(CMD, "-- ------------------ ---------- ------ ------------------ ------------");
2825  for (struct target *target = all_targets; target; target = target->next, ++index) {
2826  const char *state;
2827  char marker = ' ';
2828 
2829  if (target->tap->enabled)
2831  else
2832  state = "tap-disabled";
2833 
2834  if (CMD_CTX->current_target == target)
2835  marker = '*';
2836 
2837  /* keep columns lined up to match the headers above */
2839  "%2d%c %-18s %-10s %-6s %-18s %s",
2840  index,
2841  marker,
2846  state);
2847  }
2848 
2849  return retval;
2850 }
2851 
2852 /* every polling_interval we check for reset & powerdropout */
2853 
2856 
2861 
2862 static int sense_handler(void)
2863 {
2864  static int prev_srst_asserted;
2865  static int prev_power_dropout;
2866 
2868  if (retval != ERROR_OK)
2869  return retval;
2870 
2871  int power_restored;
2872  power_restored = prev_power_dropout && !sensed_power_dropout;
2873  if (power_restored)
2874  run_power_restore = 1;
2875 
2876  int64_t current = timeval_ms();
2877  static int64_t last_power;
2878  bool wait_more = last_power + 2000 > current;
2879  if (sensed_power_dropout && !wait_more) {
2880  run_power_dropout = 1;
2881  last_power = current;
2882  }
2883 
2885  if (retval != ERROR_OK)
2886  return retval;
2887 
2888  int srst_deasserted;
2889  srst_deasserted = prev_srst_asserted && !sensed_srst_asserted;
2890 
2891  static int64_t last_srst;
2892  wait_more = last_srst + 2000 > current;
2893  if (srst_deasserted && !wait_more) {
2894  run_srst_deasserted = 1;
2895  last_srst = current;
2896  }
2897 
2898  if (!prev_srst_asserted && sensed_srst_asserted)
2899  run_srst_asserted = 1;
2900 
2901  prev_srst_asserted = sensed_srst_asserted;
2902  prev_power_dropout = sensed_power_dropout;
2903 
2904  if (srst_deasserted || power_restored) {
2905  /* Other than logging the event we can't do anything here.
2906  * Issuing a reset is a particularly bad idea as we might
2907  * be inside a reset already.
2908  */
2909  }
2910 
2911  return ERROR_OK;
2912 }
2913 
2914 static int handle_one_target(struct target *target)
2915 {
2917  return ERROR_OK;
2918 
2919  int res = target_poll(target);
2920  if (res == ERROR_OK)
2921  return res;
2922 
2923  LOG_TARGET_ERROR(target, "Polling failed, trying to reexamine");
2925  return target_examine_one(target);
2926 }
2927 
2928 /* process target state changes */
2929 static int handle_target(void *priv)
2930 {
2931  Jim_Interp *interp = (Jim_Interp *)priv;
2932 
2933  if (!is_jtag_poll_safe()) {
2934  /* polling is disabled currently */
2935  return ERROR_OK;
2936  }
2937 
2938  /* we do not want to recurse here... */
2939  static int recursive;
2940  if (!recursive) {
2941  recursive = 1;
2942  sense_handler();
2943  /* danger! running these procedures can trigger srst assertions and power dropouts.
2944  * We need to avoid an infinite loop/recursion here and we do that by
2945  * clearing the flags after running these events.
2946  */
2947  int did_something = 0;
2948  if (run_srst_asserted) {
2949  LOG_INFO("srst asserted detected, running srst_asserted proc.");
2950  Jim_Eval(interp, "srst_asserted");
2951  did_something = 1;
2952  }
2953  if (run_srst_deasserted) {
2954  Jim_Eval(interp, "srst_deasserted");
2955  did_something = 1;
2956  }
2957  if (run_power_dropout) {
2958  LOG_INFO("Power dropout detected, running power_dropout proc.");
2959  Jim_Eval(interp, "power_dropout");
2960  did_something = 1;
2961  }
2962  if (run_power_restore) {
2963  Jim_Eval(interp, "power_restore");
2964  did_something = 1;
2965  }
2966 
2967  if (did_something) {
2968  /* clear detect flags */
2969  sense_handler();
2970  }
2971 
2972  /* clear action flags */
2973 
2974  run_srst_asserted = 0;
2975  run_srst_deasserted = 0;
2976  run_power_restore = 0;
2977  run_power_dropout = 0;
2978 
2979  recursive = 0;
2980  }
2981 
2982  /* FIXME: sensed SRST state should be treated similarly as
2983  * active SRST control and honour reset config RESET_SRST_NO_GATING */
2985  return ERROR_OK;
2986 
2987  int retval = ERROR_OK;
2988  /* Poll targets for state changes unless that's globally disabled.
2989  * Skip targets that are currently disabled.
2990  */
2991  for (struct target *target = all_targets;
2993  target = target->next) {
2994  /* This function only gets called every polling_interval, so
2995  * allow some slack in the time comparison. Otherwise, if we
2996  * schedule for now+polling_interval, the next poll won't
2997  * actually happen until a polling_interval later. */
2999  continue;
3000 
3001  int tgt_res = handle_one_target(target);
3002  if (tgt_res != ERROR_OK) {
3003  retval = tgt_res;
3006  } else {
3008  }
3010  LOG_TARGET_DEBUG_IO(target, "target_poll() -> %d, next attempt in %ums",
3011  tgt_res, target->backoff.interval);
3012  }
3013 
3014  return retval;
3015 }
3016 
3017 COMMAND_HANDLER(handle_reg_command)
3018 {
3019  LOG_DEBUG("-");
3020 
3022  if (!target_was_examined(target)) {
3023  command_print(CMD, "Error: [%s] not examined", target_name(target));
3025  }
3026  struct reg *reg = NULL;
3027 
3028  /* list all available registers for the current target */
3029  if (CMD_ARGC == 0) {
3030  struct reg_cache *cache = target->reg_cache;
3031 
3032  unsigned int count = 0;
3033  while (cache) {
3034  unsigned int i;
3035 
3036  command_print(CMD, "===== %s", cache->name);
3037 
3038  for (i = 0, reg = cache->reg_list;
3039  i < cache->num_regs;
3040  i++, reg++, count++) {
3041  if (!reg->exist || reg->hidden)
3042  continue;
3043  /* only print cached values if they are valid */
3044  if (reg->valid) {
3045  char *value = buf_to_hex_str(reg->value,
3046  reg->size);
3048  "(%i) %s (/%" PRIu32 "): 0x%s%s",
3049  count, reg->name,
3050  reg->size, value,
3051  reg->dirty
3052  ? " (dirty)"
3053  : "");
3054  free(value);
3055  } else {
3056  command_print(CMD, "(%i) %s (/%" PRIu32 ")",
3057  count, reg->name,
3058  reg->size);
3059  }
3060  }
3061  cache = cache->next;
3062  }
3063 
3064  return ERROR_OK;
3065  }
3066 
3067  /* access a single register by its ordinal number */
3068  if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9')) {
3069  unsigned int num;
3070  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
3071 
3072  struct reg_cache *cache = target->reg_cache;
3073  unsigned int count = 0;
3074  while (cache) {
3075  unsigned int i;
3076  for (i = 0; i < cache->num_regs; i++) {
3077  if (count++ == num) {
3078  reg = &cache->reg_list[i];
3079  break;
3080  }
3081  }
3082  if (reg)
3083  break;
3084  cache = cache->next;
3085  }
3086 
3087  if (!reg) {
3088  command_print(CMD, "%i is out of bounds, the current target "
3089  "has only %i registers (0 - %i)", num, count, count - 1);
3090  return ERROR_FAIL;
3091  }
3092  } else {
3093  /* access a single register by its name */
3095 
3096  if (!reg)
3097  goto not_found;
3098  }
3099 
3100  assert(reg); /* give clang a hint that we *know* reg is != NULL here */
3101 
3102  if (!reg->exist)
3103  goto not_found;
3104 
3105  /* display a register */
3106  if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0')
3107  && (CMD_ARGV[1][0] <= '9')))) {
3108  if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
3109  reg->valid = false;
3110 
3111  if (!reg->valid) {
3112  int retval = reg->type->get(reg);
3113  if (retval != ERROR_OK) {
3114  LOG_ERROR("Could not read register '%s'", reg->name);
3115  return retval;
3116  }
3117  }
3118  char *value = buf_to_hex_str(reg->value, reg->size);
3119  command_print(CMD, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
3120  free(value);
3121  return ERROR_OK;
3122  }
3123 
3124  /* set register value */
3125  if (CMD_ARGC == 2) {
3126  uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
3127  if (!buf) {
3128  LOG_ERROR("Failed to allocate memory");
3129  return ERROR_FAIL;
3130  }
3131 
3132  int retval = CALL_COMMAND_HANDLER(command_parse_str_to_buf, CMD_ARGV[1], buf, reg->size);
3133  if (retval != ERROR_OK) {
3134  free(buf);
3135  return retval;
3136  }
3137 
3138  retval = reg->type->set(reg, buf);
3139  if (retval != ERROR_OK) {
3140  LOG_ERROR("Could not write to register '%s'", reg->name);
3141  } else {
3142  char *value = buf_to_hex_str(reg->value, reg->size);
3143  command_print(CMD, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
3144  free(value);
3145  }
3146 
3147  free(buf);
3148 
3149  return retval;
3150  }
3151 
3153 
3154 not_found:
3155  command_print(CMD, "register %s not found in current target", CMD_ARGV[0]);
3156  return ERROR_FAIL;
3157 }
3158 
3159 COMMAND_HANDLER(handle_poll_command)
3160 {
3161  int retval = ERROR_OK;
3163 
3164  if (CMD_ARGC == 0) {
3165  command_print(CMD, "background polling: %s",
3166  jtag_poll_get_enabled() ? "on" : "off");
3167  command_print(CMD, "TAP: %s (%s)",
3169  target->tap->enabled ? "enabled" : "disabled");
3170  if (!target->tap->enabled)
3171  return ERROR_OK;
3172  retval = target_poll(target);
3173  if (retval != ERROR_OK)
3174  return retval;
3175  retval = target_arch_state(target);
3176  if (retval != ERROR_OK)
3177  return retval;
3178  } else if (CMD_ARGC == 1) {
3179  bool enable;
3180  COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
3181  jtag_poll_set_enabled(enable);
3182  } else
3184 
3185  return retval;
3186 }
3187 
3188 COMMAND_HANDLER(handle_poll_interval_command)
3189 {
3190  int retval;
3191  unsigned int ms;
3192 
3193  switch (CMD_ARGC) {
3194  case 0:
3196  break;
3197  case 1:
3198  retval = parse_uint(CMD_ARGV[0], &ms);
3199  if (retval != ERROR_OK)
3201 
3202  /* If the timer callback has been registered, update the timer callback period */
3204  if (cb) {
3205  retval = target_timer_callback_set_period(cb, ms);
3206  if (retval != ERROR_OK)
3207  return retval;
3208  }
3209  polling_interval = ms;
3210  break;
3211  default:
3213  }
3214 
3215  return ERROR_OK;
3216 }
3217 
3218 COMMAND_HANDLER(handle_wait_halt_command)
3219 {
3220  if (CMD_ARGC > 1)
3222 
3223  unsigned int ms = DEFAULT_HALT_TIMEOUT;
3224  if (1 == CMD_ARGC) {
3225  int retval = parse_uint(CMD_ARGV[0], &ms);
3226  if (retval != ERROR_OK)
3228  }
3229 
3231  return target_wait_state(target, TARGET_HALTED, ms);
3232 }
3233 
3234 /* wait for target state to change. The trick here is to have a low
3235  * latency for short waits and not to suck up all the CPU time
3236  * on longer waits.
3237  */
3238 int target_wait_state(struct target *target, enum target_state state, unsigned int ms)
3239 {
3240  int retval;
3241  int64_t then = 0, cur;
3242  bool once = true;
3243 
3244  for (;;) {
3245  retval = target_poll(target);
3246  if (retval != ERROR_OK)
3247  return retval;
3248  if (target->state == state)
3249  break;
3250  cur = timeval_ms();
3251  if (once) {
3252  once = false;
3253  then = timeval_ms();
3254  LOG_DEBUG("waiting for target %s...",
3256  }
3257 
3258  keep_alive();
3260  return ERROR_SERVER_INTERRUPTED;
3261 
3262  if ((cur-then) > ms) {
3263  LOG_ERROR("timed out while waiting for target %s",
3265  return ERROR_FAIL;
3266  }
3267  }
3268 
3269  return ERROR_OK;
3270 }
3271 
3272 COMMAND_HANDLER(handle_halt_command)
3273 {
3274  LOG_DEBUG("-");
3275 
3277 
3278  target->verbose_halt_msg = true;
3279 
3280  int retval = target_halt(target);
3281  if (retval != ERROR_OK)
3282  return retval;
3283 
3284  if (CMD_ARGC == 1) {
3285  unsigned int wait_local;
3286  retval = parse_uint(CMD_ARGV[0], &wait_local);
3287  if (retval != ERROR_OK)
3289  if (!wait_local)
3290  return ERROR_OK;
3291  }
3292 
3293  return CALL_COMMAND_HANDLER(handle_wait_halt_command);
3294 }
3295 
3296 COMMAND_HANDLER(handle_soft_reset_halt_command)
3297 {
3299 
3300  LOG_TARGET_INFO(target, "requesting target halt and executing a soft reset");
3301 
3303 
3304  return ERROR_OK;
3305 }
3306 
3307 COMMAND_HANDLER(handle_reset_command)
3308 {
3309  if (CMD_ARGC > 1)
3311 
3312  enum target_reset_mode reset_mode = RESET_RUN;
3313  if (CMD_ARGC == 1) {
3314  const struct nvp *n;
3316  if ((!n->name) || (n->value == RESET_UNKNOWN))
3318  reset_mode = n->value;
3319  }
3320 
3321  /* reset *all* targets */
3322  return target_process_reset(CMD, reset_mode);
3323 }
3324 
3325 
3326 COMMAND_HANDLER(handle_resume_command)
3327 {
3328  bool current = true;
3329  if (CMD_ARGC > 1)
3331 
3333 
3334  /* with no CMD_ARGV, resume from current pc, addr = 0,
3335  * with one arguments, addr = CMD_ARGV[0],
3336  * handle breakpoints, not debugging */
3337  target_addr_t addr = 0;
3338  if (CMD_ARGC == 1) {
3340  current = false;
3341  }
3342 
3343  return target_resume(target, current, addr, true, false);
3344 }
3345 
3346 COMMAND_HANDLER(handle_step_command)
3347 {
3348  if (CMD_ARGC > 1)
3350 
3351  LOG_DEBUG("-");
3352 
3353  /* with no CMD_ARGV, step from current pc, addr = 0,
3354  * with one argument addr = CMD_ARGV[0],
3355  * handle breakpoints, debugging */
3356  target_addr_t addr = 0;
3357  int current_pc = 1;
3358  if (CMD_ARGC == 1) {
3360  current_pc = 0;
3361  }
3362 
3364 
3365  return target_step(target, current_pc, addr, true);
3366 }
3367 
3369  struct target *target, target_addr_t address, unsigned int size,
3370  unsigned int count, const uint8_t *buffer, bool include_address)
3371 {
3372  const unsigned int line_bytecnt = 32;
3373  unsigned int line_modulo = line_bytecnt / size;
3374 
3375  char output[line_bytecnt * 4 + 1];
3376  unsigned int output_len = 0;
3377 
3378  const char *value_fmt;
3379  switch (size) {
3380  case 8:
3381  value_fmt = "%16.16"PRIx64" ";
3382  break;
3383  case 4:
3384  value_fmt = "%8.8"PRIx64" ";
3385  break;
3386  case 2:
3387  value_fmt = "%4.4"PRIx64" ";
3388  break;
3389  case 1:
3390  value_fmt = "%2.2"PRIx64" ";
3391  break;
3392  default:
3393  /* "can't happen", caller checked */
3394  LOG_ERROR("invalid memory read size: %u", size);
3395  return;
3396  }
3397 
3398  for (unsigned int i = 0; i < count; i++) {
3399  if (include_address && i % line_modulo == 0) {
3400  output_len += snprintf(output + output_len,
3401  sizeof(output) - output_len,
3402  TARGET_ADDR_FMT ": ",
3403  (address + (i * size)));
3404  }
3405 
3406  uint64_t value = 0;
3407  const uint8_t *value_ptr = buffer + i * size;
3408  switch (size) {
3409  case 8:
3410  value = target_buffer_get_u64(target, value_ptr);
3411  break;
3412  case 4:
3413  value = target_buffer_get_u32(target, value_ptr);
3414  break;
3415  case 2:
3416  value = target_buffer_get_u16(target, value_ptr);
3417  break;
3418  case 1:
3419  value = *value_ptr;
3420  }
3421  output_len += snprintf(output + output_len,
3422  sizeof(output) - output_len,
3423  value_fmt, value);
3424 
3425  if ((i % line_modulo == line_modulo - 1) || (i == count - 1)) {
3426  command_print(cmd, "%s", output);
3427  output_len = 0;
3428  }
3429  }
3430 }
3431 
3432 COMMAND_HANDLER(handle_md_command)
3433 {
3434  if (CMD_ARGC < 1)
3436 
3437  unsigned int size = 0;
3438  switch (CMD_NAME[2]) {
3439  case 'd':
3440  size = 8;
3441  break;
3442  case 'w':
3443  size = 4;
3444  break;
3445  case 'h':
3446  size = 2;
3447  break;
3448  case 'b':
3449  size = 1;
3450  break;
3451  default:
3453  }
3454 
3455  bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
3456  int (*fn)(struct target *target,
3457  target_addr_t address, uint32_t size_value, uint32_t count, uint8_t *buffer);
3458  if (physical) {
3459  CMD_ARGC--;
3460  CMD_ARGV++;
3462  } else
3463  fn = target_read_memory;
3464  if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
3466 
3469 
3470  unsigned int count = 1;
3471  if (CMD_ARGC == 2)
3472  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
3473 
3474  uint8_t *buffer = calloc(count, size);
3475  if (!buffer) {
3476  LOG_ERROR("Failed to allocate md read buffer");
3477  return ERROR_FAIL;
3478  }
3479 
3481  int retval = fn(target, address, size, count, buffer);
3482  if (retval == ERROR_OK)
3484 
3485  free(buffer);
3486 
3487  return retval;
3488 }
3489 
3490 typedef int (*target_write_fn)(struct target *target,
3491  target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer);
3492 
3493 static int target_fill_mem(struct target *target,
3495  target_write_fn fn,
3496  unsigned int data_size,
3497  /* value */
3498  uint64_t b,
3499  /* count */
3500  unsigned int c)
3501 {
3502  /* We have to write in reasonably large chunks to be able
3503  * to fill large memory areas with any sane speed */
3504  const unsigned int chunk_size = 16384;
3505  uint8_t *target_buf = malloc(chunk_size * data_size);
3506  if (!target_buf) {
3507  LOG_ERROR("Out of memory");
3508  return ERROR_FAIL;
3509  }
3510 
3511  int retval = ERROR_OK;
3512 
3513  for (unsigned int i = 0; i < chunk_size; i++) {
3514  switch (data_size) {
3515  case 8:
3516  target_buffer_set_u64(target, target_buf + i * data_size, b);
3517  break;
3518  case 4:
3519  target_buffer_set_u32(target, target_buf + i * data_size, b);
3520  break;
3521  case 2:
3522  target_buffer_set_u16(target, target_buf + i * data_size, b);
3523  break;
3524  case 1:
3525  target_buffer_set_u8(target, target_buf + i * data_size, b);
3526  break;
3527  default:
3528  LOG_ERROR("Unsupported data size %u", data_size);
3529  retval = ERROR_FAIL;
3530  goto err;
3531  }
3532  }
3533 
3534  for (unsigned int x = 0; x < c; x += chunk_size) {
3535  unsigned int current;
3536  current = c - x;
3537  if (current > chunk_size)
3538  current = chunk_size;
3539  retval = fn(target, address + x * data_size, data_size, current, target_buf);
3540  if (retval != ERROR_OK)
3541  break;
3542  /* avoid GDB timeouts */
3543  keep_alive();
3544 
3546  retval = ERROR_SERVER_INTERRUPTED;
3547  break;
3548  }
3549  }
3550 err:
3551  free(target_buf);
3552 
3553  return retval;
3554 }
3555 
3556 
3557 COMMAND_HANDLER(handle_mw_command)
3558 {
3559  if (CMD_ARGC < 2)
3561  bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
3562  target_write_fn fn;
3563  if (physical) {
3564  CMD_ARGC--;
3565  CMD_ARGV++;
3567  } else
3568  fn = target_write_memory;
3569  if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
3571 
3574 
3575  uint64_t value;
3576  COMMAND_PARSE_NUMBER(u64, CMD_ARGV[1], value);
3577 
3578  unsigned int count = 1;
3579  if (CMD_ARGC == 3)
3580  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
3581 
3583  unsigned int wordsize;
3584  switch (CMD_NAME[2]) {
3585  case 'd':
3586  wordsize = 8;
3587  break;
3588  case 'w':
3589  wordsize = 4;
3590  break;
3591  case 'h':
3592  wordsize = 2;
3593  break;
3594  case 'b':
3595  wordsize = 1;
3596  break;
3597  default:
3599  }
3600 
3601  return target_fill_mem(target, address, fn, wordsize, value, count);
3602 }
3603 
3604 static COMMAND_HELPER(parse_load_image_command, struct image *image,
3605  target_addr_t *min_address, target_addr_t *max_address)
3606 {
3607  if (CMD_ARGC < 1 || CMD_ARGC > 5)
3609 
3610  /* a base address isn't always necessary,
3611  * default to 0x0 (i.e. don't relocate) */
3612  if (CMD_ARGC >= 2) {
3615  image->base_address = addr;
3616  image->base_address_set = true;
3617  } else
3618  image->base_address_set = false;
3619 
3620  image->start_address_set = false;
3621 
3622  if (CMD_ARGC >= 4)
3623  COMMAND_PARSE_ADDRESS(CMD_ARGV[3], *min_address);
3624  if (CMD_ARGC == 5) {
3625  COMMAND_PARSE_ADDRESS(CMD_ARGV[4], *max_address);
3626  /* use size (given) to find max (required) */
3627  *max_address += *min_address;
3628  }
3629 
3630  if (*min_address > *max_address)
3632 
3633  return ERROR_OK;
3634 }
3635 
3636 COMMAND_HANDLER(handle_load_image_command)
3637 {
3638  uint8_t *buffer;
3639  size_t buf_cnt;
3640  uint32_t image_size;
3641  target_addr_t min_address = 0;
3642  target_addr_t max_address = -1;
3643  struct image image;
3644 
3645  int retval = CALL_COMMAND_HANDLER(parse_load_image_command,
3646  &image, &min_address, &max_address);
3647  if (retval != ERROR_OK)
3648  return retval;
3649 
3651 
3652  struct duration bench;
3653  duration_start(&bench);
3654 
3655  if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
3656  return ERROR_FAIL;
3657 
3658  image_size = 0x0;
3659  retval = ERROR_OK;
3660  for (unsigned int i = 0; i < image.num_sections; i++) {
3661  buffer = malloc(image.sections[i].size);
3662  if (!buffer) {
3664  "error allocating buffer for section (%d bytes)",
3665  (int)(image.sections[i].size));
3666  retval = ERROR_FAIL;
3667  break;
3668  }
3669 
3670  retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
3671  if (retval != ERROR_OK) {
3672  free(buffer);
3673  break;
3674  }
3675 
3676  uint32_t offset = 0;
3677  uint32_t length = buf_cnt;
3678 
3679  /* DANGER!!! beware of unsigned comparison here!!! */
3680 
3681  if ((image.sections[i].base_address + buf_cnt >= min_address) &&
3682  (image.sections[i].base_address < max_address)) {
3683 
3684  if (image.sections[i].base_address < min_address) {
3685  /* clip addresses below */
3686  offset += min_address-image.sections[i].base_address;
3687  length -= offset;
3688  }
3689 
3690  if (image.sections[i].base_address + buf_cnt > max_address)
3691  length -= (image.sections[i].base_address + buf_cnt)-max_address;
3692 
3693  retval = target_write_buffer(target,
3695  if (retval != ERROR_OK) {
3696  free(buffer);
3697  break;
3698  }
3699  image_size += length;
3700  command_print(CMD, "%u bytes written at address " TARGET_ADDR_FMT "",
3701  (unsigned int)length,
3703  }
3704 
3705  free(buffer);
3706  }
3707 
3708  if (retval == ERROR_OK && duration_measure(&bench) == ERROR_OK) {
3709  command_print(CMD, "downloaded %" PRIu32 " bytes "
3710  "in %fs (%0.3f KiB/s)", image_size,
3711  duration_elapsed(&bench), duration_kbps(&bench, image_size));
3712  }
3713 
3714  image_close(&image);
3715 
3716  return retval;
3717 
3718 }
3719 
3720 COMMAND_HANDLER(handle_dump_image_command)
3721 {
3722  struct fileio *fileio;
3723  uint8_t *buffer;
3724  int retval, retvaltemp;
3726  struct duration bench;
3728 
3729  if (CMD_ARGC != 3)
3731 
3734 
3735  uint32_t buf_size = (size > 4096) ? 4096 : size;
3736  buffer = malloc(buf_size);
3737  if (!buffer)
3738  return ERROR_FAIL;
3739 
3741  if (retval != ERROR_OK) {
3742  free(buffer);
3743  return retval;
3744  }
3745 
3746  duration_start(&bench);
3747 
3748  while (size > 0) {
3749  size_t size_written;
3750  uint32_t this_run_size = (size > buf_size) ? buf_size : size;
3751  retval = target_read_buffer(target, address, this_run_size, buffer);
3752  if (retval != ERROR_OK)
3753  break;
3754 
3755  retval = fileio_write(fileio, this_run_size, buffer, &size_written);
3756  if (retval != ERROR_OK)
3757  break;
3758 
3759  size -= this_run_size;
3760  address += this_run_size;
3761  }
3762 
3763  free(buffer);
3764 
3765  if (retval == ERROR_OK && duration_measure(&bench) == ERROR_OK) {
3766  size_t filesize;
3767  retval = fileio_size(fileio, &filesize);
3768  if (retval != ERROR_OK)
3769  return retval;
3771  "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize,
3772  duration_elapsed(&bench), duration_kbps(&bench, filesize));
3773  }
3774 
3775  retvaltemp = fileio_close(fileio);
3776  if (retvaltemp != ERROR_OK)
3777  return retvaltemp;
3778 
3779  return retval;
3780 }
3781 
3786 };
3787 
3788 static COMMAND_HELPER(handle_verify_image_command_internal, enum verify_mode verify)
3789 {
3790  uint8_t *buffer;
3791  size_t buf_cnt;
3792  uint32_t image_size;
3793  int retval;
3794  uint32_t checksum = 0;
3795  uint32_t mem_checksum = 0;
3796 
3797  struct image image;
3798 
3800 
3801  if (CMD_ARGC < 1)
3803 
3804  if (!target) {
3805  LOG_ERROR("no target selected");
3806  return ERROR_FAIL;
3807  }
3808 
3809  struct duration bench;
3810  duration_start(&bench);
3811 
3812  if (CMD_ARGC >= 2) {
3816  image.base_address_set = true;
3817  } else {
3818  image.base_address_set = false;
3819  image.base_address = 0x0;
3820  }
3821 
3822  image.start_address_set = false;
3823 
3824  retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL);
3825  if (retval != ERROR_OK)
3826  return retval;
3827 
3828  image_size = 0x0;
3829  int diffs = 0;
3830  retval = ERROR_OK;
3831  for (unsigned int i = 0; i < image.num_sections; i++) {
3832  buffer = malloc(image.sections[i].size);
3833  if (!buffer) {
3835  "error allocating buffer for section (%" PRIu32 " bytes)",
3836  image.sections[i].size);
3837  break;
3838  }
3839  retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
3840  if (retval != ERROR_OK) {
3841  free(buffer);
3842  break;
3843  }
3844 
3845  if (verify >= IMAGE_VERIFY) {
3846  /* calculate checksum of image */
3847  retval = image_calculate_checksum(buffer, buf_cnt, &checksum);
3848  if (retval != ERROR_OK) {
3849  free(buffer);
3850  break;
3851  }
3852 
3853  retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
3854  if (retval != ERROR_OK) {
3855  free(buffer);
3856  break;
3857  }
3858  if ((checksum != mem_checksum) && (verify == IMAGE_CHECKSUM_ONLY)) {
3859  LOG_ERROR("checksum mismatch");
3860  free(buffer);
3861  retval = ERROR_FAIL;
3862  goto done;
3863  }
3864  if (checksum != mem_checksum) {
3865  /* failed crc checksum, fall back to a binary compare */
3866  uint8_t *data;
3867 
3868  if (diffs == 0)
3869  LOG_ERROR("checksum mismatch - attempting binary compare");
3870 
3871  data = malloc(buf_cnt);
3872 
3873  retval = target_read_buffer(target, image.sections[i].base_address, buf_cnt, data);
3874  if (retval == ERROR_OK) {
3875  uint32_t t;
3876  for (t = 0; t < buf_cnt; t++) {
3877  if (data[t] != buffer[t]) {
3879  "diff %d address " TARGET_ADDR_FMT ". Was 0x%02" PRIx8 " instead of 0x%02" PRIx8,
3880  diffs,
3881  t + image.sections[i].base_address,
3882  data[t],
3883  buffer[t]);
3884  if (diffs++ >= 127) {
3885  command_print(CMD, "More than 128 errors, the rest are not printed.");
3886  free(data);
3887  free(buffer);
3888  goto done;
3889  }
3890  }
3891  keep_alive();
3893  retval = ERROR_SERVER_INTERRUPTED;
3894  free(data);
3895  free(buffer);
3896  goto done;
3897  }
3898  }
3899  }
3900  free(data);
3901  }
3902  } else {
3903  command_print(CMD, "address " TARGET_ADDR_FMT " length 0x%08zx",
3905  buf_cnt);
3906  }
3907 
3908  free(buffer);
3909  image_size += buf_cnt;
3910  }
3911  if (diffs > 0)
3912  command_print(CMD, "No more differences found.");
3913 done:
3914  if (diffs > 0)
3915  retval = ERROR_FAIL;
3916  if (retval == ERROR_OK && duration_measure(&bench) == ERROR_OK) {
3917  command_print(CMD, "verified %" PRIu32 " bytes "
3918  "in %fs (%0.3f KiB/s)", image_size,
3919  duration_elapsed(&bench), duration_kbps(&bench, image_size));
3920  }
3921 
3922  image_close(&image);
3923 
3924  return retval;
3925 }
3926 
3927 COMMAND_HANDLER(handle_verify_image_checksum_command)
3928 {
3929  return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_CHECKSUM_ONLY);
3930 }
3931 
3932 COMMAND_HANDLER(handle_verify_image_command)
3933 {
3934  return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_VERIFY);
3935 }
3936 
3937 COMMAND_HANDLER(handle_test_image_command)
3938 {
3939  return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_TEST);
3940 }
3941 
3943 {
3944  struct target *target = get_current_target(cmd->ctx);
3946  while (breakpoint) {
3947  if (breakpoint->type == BKPT_SOFT) {
3948  char *buf = buf_to_hex_str(breakpoint->orig_instr,
3949  breakpoint->length * 8);
3950  command_print(cmd, "Software breakpoint(IVA): addr=" TARGET_ADDR_FMT ", len=0x%x, orig_instr=0x%s",
3952  breakpoint->length,
3953  buf);
3954  free(buf);
3955  } else {
3956  if ((breakpoint->address == 0) && (breakpoint->asid != 0))
3957  command_print(cmd, "Context breakpoint: asid=0x%8.8" PRIx32 ", len=0x%x, num=%u",
3958  breakpoint->asid,
3960  else if ((breakpoint->address != 0) && (breakpoint->asid != 0)) {
3961  command_print(cmd, "Hybrid breakpoint(IVA): addr=" TARGET_ADDR_FMT ", len=0x%x, num=%u",
3964  command_print(cmd, "\t|--->linked with ContextID: 0x%8.8" PRIx32,
3965  breakpoint->asid);
3966  } else
3967  command_print(cmd, "Hardware breakpoint(IVA): addr=" TARGET_ADDR_FMT ", len=0x%x, num=%u",
3970  }
3971 
3973  }
3974  return ERROR_OK;
3975 }
3976 
3978  target_addr_t addr, uint32_t asid, unsigned int length, int hw)
3979 {
3980  struct target *target = get_current_target(cmd->ctx);
3981  int retval;
3982 
3983  if (asid == 0) {
3984  retval = breakpoint_add(target, addr, length, hw);
3985  /* error is always logged in breakpoint_add(), do not print it again */
3986  if (retval == ERROR_OK)
3987  command_print(cmd, "breakpoint set at " TARGET_ADDR_FMT "", addr);
3988 
3989  } else if (addr == 0) {
3991  LOG_TARGET_ERROR(target, "Context breakpoint not available");
3993  }
3994  retval = context_breakpoint_add(target, asid, length, hw);
3995  /* error is always logged in context_breakpoint_add(), do not print it again */
3996  if (retval == ERROR_OK)
3997  command_print(cmd, "Context breakpoint set at 0x%8.8" PRIx32, asid);
3998 
3999  } else {
4001  LOG_TARGET_ERROR(target, "Hybrid breakpoint not available");
4003  }
4004  retval = hybrid_breakpoint_add(target, addr, asid, length, hw);
4005  /* error is always logged in hybrid_breakpoint_add(), do not print it again */
4006  if (retval == ERROR_OK)
4007  command_print(cmd, "Hybrid breakpoint set at 0x%8.8" PRIx32, asid);
4008  }
4009  return retval;
4010 }
4011 
4012 COMMAND_HANDLER(handle_bp_command)
4013 {
4015  uint32_t asid;
4016  uint32_t length;
4017  int hw = BKPT_SOFT;
4018 
4019  switch (CMD_ARGC) {
4020  case 0:
4021  return handle_bp_command_list(CMD);
4022 
4023  case 2:
4024  asid = 0;
4027  return handle_bp_command_set(CMD, addr, asid, length, hw);
4028 
4029  case 3:
4030  if (strcmp(CMD_ARGV[2], "hw") == 0) {
4031  hw = BKPT_HARD;
4034  asid = 0;
4035  return handle_bp_command_set(CMD, addr, asid, length, hw);
4036  } else if (strcmp(CMD_ARGV[2], "hw_ctx") == 0) {
4037  hw = BKPT_HARD;
4038  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], asid);
4040  addr = 0;
4041  return handle_bp_command_set(CMD, addr, asid, length, hw);
4042  }
4043  /* fallthrough */
4044  case 4:
4045  hw = BKPT_HARD;
4047  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], asid);
4049  return handle_bp_command_set(CMD, addr, asid, length, hw);
4050 
4051  default:
4053  }
4054 }
4055 
4056 COMMAND_HANDLER(handle_rbp_command)
4057 {
4058  int retval;
4059 
4060  if (CMD_ARGC != 1)
4062 
4064 
4065  if (!strcmp(CMD_ARGV[0], "all")) {
4066  retval = breakpoint_remove_all(target);
4067 
4068  if (retval != ERROR_OK) {
4069  command_print(CMD, "Error encountered during removal of all breakpoints.");
4070  command_print(CMD, "Some breakpoints may have remained set.");
4071  }
4072  } else {
4075 
4076  retval = breakpoint_remove(target, addr);
4077 
4078  if (retval != ERROR_OK)
4079  command_print(CMD, "Error during removal of breakpoint at address " TARGET_ADDR_FMT, addr);
4080  }
4081 
4082  return retval;
4083 }
4084 
4085 COMMAND_HANDLER(handle_wp_command)
4086 {
4088 
4089  if (CMD_ARGC == 0) {
4091 
4092  while (watchpoint) {
4093  char wp_type = (watchpoint->rw == WPT_READ ? 'r' : (watchpoint->rw == WPT_WRITE ? 'w' : 'a'));
4094  command_print(CMD, "address: " TARGET_ADDR_FMT
4095  ", len: 0x%8.8x"
4096  ", r/w/a: %c, value: 0x%8.8" PRIx64
4097  ", mask: 0x%8.8" PRIx64,
4099  watchpoint->length,
4100  wp_type,
4101  watchpoint->value,
4102  watchpoint->mask);
4104  }
4105  return ERROR_OK;
4106  }
4107 
4108  enum watchpoint_rw type = WPT_ACCESS;
4109  target_addr_t addr = 0;
4110  uint32_t length = 0;
4111  uint64_t data_value = 0x0;
4112  uint64_t data_mask = WATCHPOINT_IGNORE_DATA_VALUE_MASK;
4113  bool mask_specified = false;
4114 
4115  switch (CMD_ARGC) {
4116  case 5:
4117  COMMAND_PARSE_NUMBER(u64, CMD_ARGV[4], data_mask);
4118  mask_specified = true;
4119  /* fall through */
4120  case 4:
4121  COMMAND_PARSE_NUMBER(u64, CMD_ARGV[3], data_value);
4122  // if user specified only data value without mask - the mask should be 0
4123  if (!mask_specified)
4124  data_mask = 0;
4125  /* fall through */
4126  case 3:
4127  switch (CMD_ARGV[2][0]) {
4128  case 'r':
4129  type = WPT_READ;
4130  break;
4131  case 'w':
4132  type = WPT_WRITE;
4133  break;
4134  case 'a':
4135  type = WPT_ACCESS;
4136  break;
4137  default:
4138  LOG_TARGET_ERROR(target, "invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
4140  }
4141  /* fall through */
4142  case 2:
4145  break;
4146 
4147  default:
4149  }
4150 
4151  int retval = watchpoint_add(target, addr, length, type,
4152  data_value, data_mask);
4153  if (retval != ERROR_OK)
4154  LOG_TARGET_ERROR(target, "Failure setting watchpoints");
4155 
4156  return retval;
4157 }
4158 
4159 COMMAND_HANDLER(handle_rwp_command)
4160 {
4161  int retval;
4162 
4163  if (CMD_ARGC != 1)
4165 
4167  if (!strcmp(CMD_ARGV[0], "all")) {
4168  retval = watchpoint_remove_all(target);
4169 
4170  if (retval != ERROR_OK) {
4171  command_print(CMD, "Error encountered during removal of all watchpoints.");
4172  command_print(CMD, "Some watchpoints may have remained set.");
4173  }
4174  } else {
4177 
4178  retval = watchpoint_remove(target, addr);
4179 
4180  if (retval != ERROR_OK)
4181  command_print(CMD, "Error during removal of watchpoint at address " TARGET_ADDR_FMT, addr);
4182  }
4183 
4184  return retval;
4185 }
4186 
4193 COMMAND_HANDLER(handle_virt2phys_command)
4194 {
4195  if (CMD_ARGC != 1)
4197 
4198  target_addr_t va;
4200  target_addr_t pa;
4201 
4203  int retval = target->type->virt2phys(target, va, &pa);
4204  if (retval == ERROR_OK)
4205  command_print(CMD, "Physical address " TARGET_ADDR_FMT "", pa);
4206 
4207  return retval;
4208 }
4209 
4210 static void write_data(FILE *f, const void *data, size_t len)
4211 {
4212  size_t written = fwrite(data, 1, len, f);
4213  if (written != len)
4214  LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
4215 }
4216 
4217 static void write_long(FILE *f, int l, struct target *target)
4218 {
4219  uint8_t val[4];
4220 
4221  target_buffer_set_u32(target, val, l);
4222  write_data(f, val, 4);
4223 }
4224 
4225 static void write_string(FILE *f, char *s)
4226 {
4227  write_data(f, s, strlen(s));
4228 }
4229 
4230 typedef unsigned char UNIT[2]; /* unit of profiling */
4231 
4232 static void write_gmon_hist(FILE *f, const uint32_t *samples, uint32_t sample_num,
4233  float sample_rate, struct target *target)
4234 {
4235  uint32_t min = samples[0];
4236  uint32_t max = samples[sample_num - 1];
4237 
4238  /* max should be (largest sample + 1)
4239  * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
4240  max++;
4241 
4242  /* The ratio ((double)((max - min) / 2) / num_buckets) must match across
4243  * all histograms in this file. To avoid trunction in the /2, we must have
4244  * an even length address space for compatibility with binutils <=2.44.
4245  * Refer to binutils/gprof/hist.c (calculation of n_hist_scale)*/
4246  if ((max - min) % 2)
4247  max++;
4248  uint32_t address_space = max - min;
4249 
4250  uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
4251  write_data(f, &zero, 1);
4252 
4253  /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
4254  write_long(f, min, target); /* low_pc */
4255  write_long(f, max, target); /* high_pc */
4256  write_long(f, address_space / sizeof(UNIT), target); /* # of buckets */
4257  write_long(f, sample_rate, target);
4258  write_string(f, "seconds");
4259  for (size_t i = strlen("seconds"); i < 15; i++)
4260  write_data(f, &zero, 1);
4261  write_string(f, "s");
4262 
4263  /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
4264  bool saturated_once = false;
4265  for (uint32_t i = 0, bidx = 0; bidx < address_space; bidx += sizeof(UNIT)) {
4266  uint32_t val = i;
4267  uint32_t bmax = min + bidx + sizeof(UNIT);
4268  while (i < sample_num && samples[i] < bmax)
4269  ++i;
4270  val = i - val;
4271 
4272  if (val > UINT16_MAX) {
4273  val = UINT16_MAX;
4274  if (!saturated_once)
4275  LOG_WARNING("profiler bucket saturated, will read as 65535");
4276  saturated_once = true;
4277  }
4278 
4279  uint8_t data[2];
4280  h_u16_to_le(data, val);
4281  write_data(f, data, 2);
4282  }
4283 }
4284 
4285 /* Dump a gmon.out histogram file. */
4286 static void write_gmon(const uint32_t *samples, uint32_t sample_num, const char *filename,
4287  struct target *target, uint32_t duration_ms)
4288 {
4289  float sample_rate = sample_num / (duration_ms / 1000.0);
4290  FILE *f = fopen(filename, "wb");
4291  if (!f)
4292  return;
4293  write_string(f, "gmon");
4294  write_long(f, 0x00000001, target); /* Version */
4295  write_long(f, 0, target); /* padding */
4296  write_long(f, 0, target); /* padding */
4297  write_long(f, 0, target); /* padding */
4298 
4299  while (sample_num) {
4300  /* if address gap exceeds this, make another histogram */
4301  const uint32_t MAX_GAP = 32;
4302 
4303  /* figure out bucket size */
4304  uint32_t max = samples[0];
4305  uint32_t this_pass = 1;
4306  while (this_pass < sample_num && samples[this_pass] - max < MAX_GAP)
4307  max = samples[this_pass++];
4308 
4309  write_gmon_hist(f, samples, this_pass, sample_rate, target);
4310 
4311  samples += this_pass;
4312  sample_num -= this_pass;
4313  }
4314 
4315  fclose(f);
4316 }
4317 
4318 // comparison function for qsort(). Returns -1, 0 or +1
4319 static int compare_pc32(const void *p1, const void *p2)
4320 {
4321  uint32_t lhs = *(const uint32_t *)p1;
4322  uint32_t rhs = *(const uint32_t *)p2;
4323  return (lhs > rhs) - (lhs < rhs);
4324 }
4325 
4326 /* profiling samples the CPU PC as quickly as OpenOCD is able,
4327  * which will be used as a random sampling of PC */
4328 COMMAND_HANDLER(handle_profile_command)
4329 {
4331 
4332  if ((CMD_ARGC != 2) && (CMD_ARGC != 4))
4334 
4335  const uint32_t MAX_PROFILE_SAMPLE_NUM = 1000000;
4336  uint32_t offset;
4337  uint32_t num_of_samples;
4338  int retval = ERROR_OK;
4339  bool halted_before_profiling = target->state == TARGET_HALTED;
4340 
4342 
4343  uint32_t start_address = 0;
4344  uint32_t end_address = 0;
4345  bool with_range = false;
4346  if (CMD_ARGC == 4) {
4347  with_range = true;
4348  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], start_address);
4349  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], end_address);
4350  if (start_address > end_address || (end_address - start_address) < 2) {
4351  command_print(CMD, "Error: end - start < 2");
4353  }
4354  }
4355 
4356  uint32_t *samples = malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM);
4357  if (!samples) {
4358  LOG_ERROR("No memory to store samples.");
4359  return ERROR_FAIL;
4360  }
4361 
4362  uint64_t timestart_ms = timeval_ms();
4368  retval = target_profiling(target, samples, MAX_PROFILE_SAMPLE_NUM,
4369  &num_of_samples, offset);
4370  if (retval != ERROR_OK) {
4371  free(samples);
4372  return retval;
4373  }
4374  uint64_t duration_ms = timeval_ms() - timestart_ms;
4375 
4376  assert(num_of_samples <= MAX_PROFILE_SAMPLE_NUM);
4377 
4378  retval = target_poll(target);
4379  if (retval != ERROR_OK) {
4380  free(samples);
4381  return retval;
4382  }
4383 
4384  if (target->state == TARGET_RUNNING && halted_before_profiling) {
4385  /* The target was halted before we started and is running now. Halt it,
4386  * for consistency. */
4387  retval = target_halt(target);
4388  if (retval != ERROR_OK) {
4389  free(samples);
4390  return retval;
4391  }
4392  } else if (target->state == TARGET_HALTED && !halted_before_profiling) {
4393  /* The target was running before we started and is halted now. Resume
4394  * it, for consistency. */
4395  retval = target_resume(target, true, 0, false, false);
4396  if (retval != ERROR_OK) {
4397  free(samples);
4398  return retval;
4399  }
4400  }
4401 
4402  retval = target_poll(target);
4403  if (retval != ERROR_OK) {
4404  free(samples);
4405  return retval;
4406  }
4407 
4408  if (!num_of_samples) {
4409  command_print(CMD, "Wrote no samples");
4410  free(samples);
4411  return ERROR_OK;
4412  }
4413 
4414  if (with_range) {
4415  uint32_t num_filtered_samples = 0;
4416  for (uint32_t in = 0; in < num_of_samples; ++in) {
4417  uint32_t sample = samples[in];
4418  if (sample >= start_address && sample < end_address)
4419  samples[num_filtered_samples++] = sample;
4420  }
4421  duration_ms = (duration_ms * num_filtered_samples + num_of_samples / 2) / num_of_samples;
4422  if (duration_ms < 1)
4423  duration_ms = 0;
4424  num_of_samples = num_filtered_samples;
4425 
4426  if (!num_of_samples) {
4427  command_print(CMD, "Wrote no samples in the requested range");
4428  free(samples);
4429  return ERROR_OK;
4430  }
4431  }
4432 
4433  qsort(samples, num_of_samples, sizeof(samples[0]), compare_pc32);
4434 
4435  write_gmon(samples, num_of_samples, CMD_ARGV[1], target, duration_ms);
4436  command_print(CMD, "Wrote %s", CMD_ARGV[1]);
4437 
4438  free(samples);
4439  return ERROR_OK;
4440 }
4441 
4442 COMMAND_HANDLER(handle_target_read_memory)
4443 {
4444  /*
4445  * CMD_ARGV[0] = memory address
4446  * CMD_ARGV[1] = desired element width in bits
4447  * CMD_ARGV[2] = number of elements to read
4448  * CMD_ARGV[3] = optional "phys"
4449  */
4450 
4451  if (CMD_ARGC < 3 || CMD_ARGC > 4)
4453 
4454  /* Arg 1: Memory address. */
4457 
4458  /* Arg 2: Bit width of one element. */
4459  unsigned int width_bits;
4460  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], width_bits);
4461 
4462  /* Arg 3: Number of elements to read. */
4463  unsigned int count;
4464  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
4465 
4466  /* Arg 4: Optional 'phys'. */
4467  bool is_phys = false;
4468  if (CMD_ARGC == 4) {
4469  if (strcmp(CMD_ARGV[3], "phys")) {
4470  command_print(CMD, "invalid argument '%s', must be 'phys'", CMD_ARGV[3]);
4472  }
4473 
4474  is_phys = true;
4475  }
4476 
4477  switch (width_bits) {
4478  case 8:
4479  case 16:
4480  case 32:
4481  case 64:
4482  break;
4483  default:
4484  command_print(CMD, "invalid width, must be 8, 16, 32 or 64");
4486  }
4487 
4488  if (count > 65536) {
4489  command_print(CMD, "too large read request, exceeds 64K elements");
4491  }
4492 
4493  const unsigned int width = width_bits / 8;
4494  /* -1 is needed to handle cases when (addr + count * width) results in zero
4495  * due to overflow.
4496  */
4497  if ((addr + count * width - 1) < addr) {
4498  command_print(CMD, "memory region wraps over address zero");
4500  }
4501 
4503 
4504  const size_t buffersize = 4096;
4505  uint8_t *buffer = malloc(buffersize);
4506 
4507  if (!buffer) {
4508  LOG_ERROR("Failed to allocate memory");
4509  return ERROR_FAIL;
4510  }
4511 
4512  char *separator = "";
4513  while (count > 0) {
4514  const unsigned int max_chunk_len = buffersize / width;
4515  const size_t chunk_len = MIN(count, max_chunk_len);
4516 
4517  int retval;
4518 
4519  if (is_phys)
4520  retval = target_read_phys_memory(target, addr, width, chunk_len, buffer);
4521  else
4522  retval = target_read_memory(target, addr, width, chunk_len, buffer);
4523 
4524  if (retval != ERROR_OK) {
4525  LOG_DEBUG("read at " TARGET_ADDR_FMT " with width=%u and count=%zu failed",
4526  addr, width_bits, chunk_len);
4527  /*
4528  * FIXME: we append the errmsg to the list of value already read.
4529  * Add a way to flush and replace old output, but LOG_DEBUG() it
4530  */
4531  command_print(CMD, "failed to read memory");
4532  free(buffer);
4533  return retval;
4534  }
4535 
4536  for (size_t i = 0; i < chunk_len ; i++) {
4537  uint64_t v = 0;
4538 
4539  switch (width) {
4540  case 8:
4542  break;
4543  case 4:
4545  break;
4546  case 2:
4548  break;
4549  case 1:
4550  v = buffer[i];
4551  break;
4552  }
4553 
4554  command_print_sameline(CMD, "%s0x%" PRIx64, separator, v);
4555  separator = " ";
4556  }
4557 
4558  count -= chunk_len;
4559  addr += chunk_len * width;
4560  }
4561 
4562  free(buffer);
4563 
4564  return ERROR_OK;
4565 }
4566 
4567 COMMAND_HANDLER(handle_target_write_memory)
4568 {
4569  /*
4570  * CMD_ARGV[0] = memory address
4571  * CMD_ARGV[1] = desired element width in bits
4572  * CMD_ARGV[2] = list of data to write
4573  * CMD_ARGV[3] = optional "phys"
4574  */
4575 
4576  if (CMD_ARGC < 3 || CMD_ARGC > 4)
4578 
4579  /* Arg 1: Memory address. */
4582 
4583  /* Arg 2: Bit width of one element. */
4584  unsigned int width_bits;
4585  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], width_bits);
4586 
4587  /* Arg 3: Elements to write. */
4588  size_t count = Jim_ListLength(CMD_CTX->interp, CMD_JIMTCL_ARGV[2]);
4589 
4590  /* Arg 4: Optional 'phys'. */
4591  bool is_phys = false;
4592 
4593  if (CMD_ARGC == 4) {
4594  if (strcmp(CMD_ARGV[3], "phys")) {
4595  command_print(CMD, "invalid argument '%s', must be 'phys'", CMD_ARGV[3]);
4597  }
4598 
4599  is_phys = true;
4600  }
4601 
4602  switch (width_bits) {
4603  case 8:
4604  case 16:
4605  case 32:
4606  case 64:
4607  break;
4608  default:
4609  command_print(CMD, "invalid width, must be 8, 16, 32 or 64");
4611  }
4612 
4613  if (count > 65536) {
4614  command_print(CMD, "too large memory write request, exceeds 64K elements");
4616  }
4617 
4618  const unsigned int width = width_bits / 8;
4619  /* -1 is needed to handle cases when (addr + count * width) results in zero
4620  * due to overflow.
4621  */
4622  if ((addr + count * width - 1) < addr) {
4623  command_print(CMD, "memory region wraps over address zero");
4625  }
4626 
4628 
4629  const size_t buffersize = 4096;
4630  uint8_t *buffer = malloc(buffersize);
4631 
4632  if (!buffer) {
4633  LOG_ERROR("Failed to allocate memory");
4634  return ERROR_FAIL;
4635  }
4636 
4637  size_t j = 0;
4638 
4639  while (count > 0) {
4640  const unsigned int max_chunk_len = buffersize / width;
4641  const size_t chunk_len = MIN(count, max_chunk_len);
4642 
4643  for (size_t i = 0; i < chunk_len; i++, j++) {
4644  Jim_Obj *tmp = Jim_ListGetIndex(CMD_CTX->interp, CMD_JIMTCL_ARGV[2], j);
4645  jim_wide element_wide;
4646  int jimretval = Jim_GetWide(CMD_CTX->interp, tmp, &element_wide);
4647  if (jimretval != JIM_OK) {
4648  command_print(CMD, "invalid value \"%s\"", Jim_GetString(tmp, NULL));
4649  free(buffer);
4651  }
4652 
4653  const uint64_t v = element_wide;
4654 
4655  switch (width) {
4656  case 8:
4658  break;
4659  case 4:
4661  break;
4662  case 2:
4664  break;
4665  case 1:
4666  buffer[i] = v & 0x0ff;
4667  break;
4668  }
4669  }
4670 
4671  count -= chunk_len;
4672 
4673  int retval;
4674 
4675  if (is_phys)
4676  retval = target_write_phys_memory(target, addr, width, chunk_len, buffer);
4677  else
4678  retval = target_write_memory(target, addr, width, chunk_len, buffer);
4679 
4680  if (retval != ERROR_OK) {
4681  LOG_DEBUG("write at " TARGET_ADDR_FMT " with width=%u and count=%zu failed",
4682  addr, width_bits, chunk_len);
4683  command_print(CMD, "failed to write memory");
4684  free(buffer);
4685  return retval;
4686  }
4687 
4688  addr += chunk_len * width;
4689  }
4690 
4691  free(buffer);
4692 
4693  return ERROR_OK;
4694 }
4695 
4696 /* FIX? should we propagate errors here rather than printing them
4697  * and continuing?
4698  */
4700 {
4701  struct target_event_action *teap, *tmp;
4702  int retval;
4703 
4705  if (teap->event == e) {
4706  /*
4707  * The event can be destroyed by its own handler.
4708  * Make a local copy and use it in place of the original.
4709  */
4710  struct target_event_action local_teap = *teap;
4711  teap = &local_teap;
4712 
4713  LOG_DEBUG("target: %s (%s) event: %d (%s) action: %s",
4716  e,
4717  target_event_name(e),
4718  Jim_GetString(teap->body, NULL));
4719 
4720  /* Override current target by the target an event
4721  * is issued from (lot of scripts need it).
4722  * Return back to previous override as soon
4723  * as the handler processing is done */
4724  struct command_context *cmd_ctx = current_command_context(teap->interp);
4725  struct target *saved_target_override = cmd_ctx->current_target_override;
4726  cmd_ctx->current_target_override = target;
4727 
4728  /*
4729  * The event can be destroyed by its own handler.
4730  * Prevent the body to get deallocated by Jim.
4731  */
4732  Jim_IncrRefCount(teap->body);
4733  retval = Jim_EvalObj(teap->interp, teap->body);
4734  Jim_DecrRefCount(teap->interp, teap->body);
4735 
4736  cmd_ctx->current_target_override = saved_target_override;
4737 
4738  if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
4739  return;
4740 
4741  if (retval == JIM_RETURN)
4742  retval = teap->interp->returnCode;
4743 
4744  if (retval != JIM_OK) {
4745  Jim_MakeErrorMessage(teap->interp);
4746  LOG_TARGET_ERROR(target, "Execution of event %s failed:\n%s",
4747  target_event_name(e),
4748  Jim_GetString(Jim_GetResult(teap->interp), NULL));
4749  /* clean both error code and stacktrace before return */
4750  Jim_Eval(teap->interp, "error \"\" \"\"");
4751  }
4752  }
4753  }
4754 }
4755 
4756 COMMAND_HANDLER(handle_target_get_reg)
4757 {
4758  if (CMD_ARGC < 1 || CMD_ARGC > 2)
4760 
4761  bool force = false;
4762  Jim_Obj *next_argv = CMD_JIMTCL_ARGV[0];
4763 
4764  if (CMD_ARGC == 2) {
4765  if (strcmp(CMD_ARGV[0], "-force")) {
4766  command_print(CMD, "invalid argument '%s', must be '-force'", CMD_ARGV[0]);
4768  }
4769 
4770  force = true;
4771  next_argv = CMD_JIMTCL_ARGV[1];
4772  }
4773 
4774  const int length = Jim_ListLength(CMD_CTX->interp, next_argv);
4775 
4776  const struct target *target = get_current_target(CMD_CTX);
4777  if (target->state != TARGET_HALTED) {
4778  command_print(CMD, "Error: [%s] not halted", target_name(target));
4779  return ERROR_TARGET_NOT_HALTED;
4780  }
4781 
4782  for (int i = 0; i < length; i++) {
4783  Jim_Obj *elem = Jim_ListGetIndex(CMD_CTX->interp, next_argv, i);
4784 
4785  const char *reg_name = Jim_String(elem);
4786 
4787  struct reg *reg = register_get_by_name(target->reg_cache, reg_name, true);
4788 
4789  if (!reg || !reg->exist) {
4790  command_print(CMD, "unknown register '%s'", reg_name);
4792  }
4793 
4794  if (force || !reg->valid) {
4795  int retval = reg->type->get(reg);
4796 
4797  if (retval != ERROR_OK) {
4798  command_print(CMD, "failed to read register '%s'", reg_name);
4799  return retval;
4800  }
4801  }
4802 
4803  char *reg_value = buf_to_hex_str(reg->value, reg->size);
4804 
4805  if (!reg_value) {
4806  LOG_ERROR("Failed to allocate memory");
4807  return ERROR_FAIL;
4808  }
4809 
4810  command_print(CMD, "%s 0x%s", reg_name, reg_value);
4811 
4812  free(reg_value);
4813  }
4814 
4815  return ERROR_OK;
4816 }
4817 
4818 COMMAND_HANDLER(handle_set_reg_command)
4819 {
4820  if (CMD_ARGC != 1)
4822 
4823  int tmp;
4824 #if JIM_VERSION >= 80
4825  Jim_Obj **dict = Jim_DictPairs(CMD_CTX->interp, CMD_JIMTCL_ARGV[0], &tmp);
4826 
4827  if (!dict)
4828  return ERROR_FAIL;
4829 #else
4830  Jim_Obj **dict;
4831  int ret = Jim_DictPairs(CMD_CTX->interp, CMD_JIMTCL_ARGV[0], &dict, &tmp);
4832 
4833  if (ret != JIM_OK)
4834  return ERROR_FAIL;
4835 #endif
4836 
4837  const unsigned int length = tmp;
4838 
4839  const struct target *target = get_current_target(CMD_CTX);
4840  assert(target);
4841  if (target->state != TARGET_HALTED) {
4842  command_print(CMD, "Error: [%s] not halted", target_name(target));
4843  return ERROR_TARGET_NOT_HALTED;
4844  }
4845 
4846 
4847  for (unsigned int i = 0; i < length; i += 2) {
4848  const char *reg_name = Jim_String(dict[i]);
4849  const char *reg_value = Jim_String(dict[i + 1]);
4850  struct reg *reg = register_get_by_name(target->reg_cache, reg_name, true);
4851 
4852  if (!reg || !reg->exist) {
4853  command_print(CMD, "unknown register '%s'", reg_name);
4854  return ERROR_FAIL;
4855  }
4856 
4857  uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
4858  if (!buf) {
4859  LOG_ERROR("Failed to allocate memory");
4860  return ERROR_FAIL;
4861  }
4862 
4863  int retval = CALL_COMMAND_HANDLER(command_parse_str_to_buf, reg_value, buf, reg->size);
4864  if (retval != ERROR_OK) {
4865  free(buf);
4866  return retval;
4867  }
4868 
4869  retval = reg->type->set(reg, buf);
4870  free(buf);
4871 
4872  if (retval != ERROR_OK) {
4873  command_print(CMD, "failed to set '%s' to register '%s'",
4874  reg_value, reg_name);
4875  return retval;
4876  }
4877  }
4878 
4879  return ERROR_OK;
4880 }
4881 
4885 bool target_has_event_action(const struct target *target, enum target_event event)
4886 {
4887  struct target_event_action *teap;
4888 
4890  if (teap->event == event)
4891  return true;
4892  }
4893  return false;
4894 }
4895 
4912 };
4913 
4914 static struct nvp nvp_config_opts[] = {
4915  { .name = "-type", .value = TCFG_TYPE },
4916  { .name = "-event", .value = TCFG_EVENT },
4917  { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
4918  { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
4919  { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
4920  { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
4921  { .name = "-endian", .value = TCFG_ENDIAN },
4922  { .name = "-coreid", .value = TCFG_COREID },
4923  { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
4924  { .name = "-tap", .value = TCFG_TAP },
4925  { .name = "-dbgbase", .value = TCFG_DBGBASE },
4926  { .name = "-rtos", .value = TCFG_RTOS },
4927  { .name = "-defer-examine", .value = TCFG_DEFER_EXAMINE },
4928  { .name = "-gdb-port", .value = TCFG_GDB_PORT },
4929  { .name = "-gdb-max-connections", .value = TCFG_GDB_MAX_CONNECTIONS },
4930  { .name = NULL, .value = -1 }
4931 };
4932 
4933 static COMMAND_HELPER(target_configure, struct target *target, unsigned int index, bool is_configure)
4934 {
4935  const struct nvp *n;
4936  int retval;
4937 
4938  /* parse config or cget options ... */
4939  while (index < CMD_ARGC) {
4941  /* target defines a configure function */
4942  /* target gets first dibs on parameters */
4943  struct jim_getopt_info goi;
4944  jim_getopt_setup(&goi, CMD_CTX->interp, CMD_ARGC - index, CMD_JIMTCL_ARGV + index);
4945  goi.is_configure = is_configure;
4946  int e = (*target->type->target_jim_configure)(target, &goi);
4947  index = CMD_ARGC - goi.argc;
4948 
4949  int reslen;
4950  const char *result = Jim_GetString(Jim_GetResult(CMD_CTX->interp), &reslen);
4951  if (reslen > 0)
4952  command_print(CMD, "%s", result);
4953 
4954  if (e == JIM_OK) {
4955  /* more? */
4956  continue;
4957  }
4958  if (e == JIM_ERR) {
4959  /* An error */
4960  return ERROR_FAIL;
4961  }
4962  /* otherwise we 'continue' below */
4963  }
4965  if (!n->name) {
4968  }
4969  index++;
4970  switch (n->value) {
4971  case TCFG_TYPE:
4972  /* not settable */
4973  if (is_configure) {
4974  command_print(CMD, "not settable: %s", n->name);
4976  }
4977  if (index != CMD_ARGC)
4980  /* loop for more */
4981  break;
4982 
4983  case TCFG_EVENT:
4984  if (index == CMD_ARGC) {
4985  command_print(CMD, "expecting %s event-name event-body",
4986  CMD_ARGV[index - 1]);
4988  }
4989 
4991  if (!n->name) {
4994  }
4995  index++;
4996 
4997  if (is_configure) {
4998  if (index == CMD_ARGC) {
4999  command_print(CMD, "expecting %s %s event-body",
5000  CMD_ARGV[index - 2], CMD_ARGV[index - 1]);
5002  }
5003  }
5004 
5005  {
5006  struct target_event_action *teap;
5007 
5008  /* replace existing? */
5010  if (teap->event == (enum target_event)n->value)
5011  break;
5012 
5013  /* not found! */
5014  if (&teap->list == &target->events_action)
5015  teap = NULL;
5016 
5017  if (is_configure) {
5018  /* START_DEPRECATED_TPIU */
5019  if (n->value == TARGET_EVENT_TRACE_CONFIG)
5020  LOG_INFO("DEPRECATED target event %s; use TPIU events {pre,post}-{enable,disable}", n->name);
5021  /* END_DEPRECATED_TPIU */
5022 
5023  if (strlen(CMD_ARGV[index]) == 0) {
5024  /* empty action, drop existing one */
5025  if (teap) {
5026  list_del(&teap->list);
5027  Jim_DecrRefCount(teap->interp, teap->body);
5028  free(teap);
5029  }
5030  index++;
5031  break;
5032  }
5033 
5034  bool replace = true;
5035  if (!teap) {
5036  /* create new */
5037  teap = calloc(1, sizeof(*teap));
5038  replace = false;
5039  }
5040  teap->event = n->value;
5041  teap->interp = CMD_CTX->interp;
5042  if (teap->body)
5043  Jim_DecrRefCount(teap->interp, teap->body);
5044  /* use jim object to keep its reference on tcl file and line */
5045  /* TODO: need duplicate? isn't IncrRefCount enough? */
5046  teap->body = Jim_DuplicateObj(teap->interp, CMD_JIMTCL_ARGV[index++]);
5047  /*
5048  * FIXME:
5049  * Tcl/TK - "tk events" have a nice feature.
5050  * See the "BIND" command.
5051  * We should support that here.
5052  * You can specify %X and %Y in the event code.
5053  * The idea is: %T - target name.
5054  * The idea is: %N - target number
5055  * The idea is: %E - event name.
5056  */
5057  Jim_IncrRefCount(teap->body);
5058 
5059  if (!replace) {
5060  /* add to head of event list */
5061  list_add(&teap->list, &target->events_action);
5062  }
5063  } else {
5064  /* cget */
5065  if (index != CMD_ARGC)
5067 
5068  if (teap)
5069  command_print(CMD, "%s", Jim_GetString(teap->body, NULL));
5070  }
5071  }
5072  /* loop for more */
5073  break;
5074 
5075  case TCFG_WORK_AREA_VIRT:
5076  if (is_configure) {
5077  if (index == CMD_ARGC) {
5078  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5080  }
5082  index++;
5085  } else {
5086  if (index != CMD_ARGC)
5089  }
5090  /* loop for more */
5091  break;
5092 
5093  case TCFG_WORK_AREA_PHYS:
5094  if (is_configure) {
5095  if (index == CMD_ARGC) {
5096  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5098  }
5100  index++;
5103  } else {
5104  if (index != CMD_ARGC)
5107  }
5108  /* loop for more */
5109  break;
5110 
5111  case TCFG_WORK_AREA_SIZE:
5112  if (is_configure) {
5113  if (index == CMD_ARGC) {
5114  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5116  }
5118  index++;
5120  } else {
5121  if (index != CMD_ARGC)
5123  command_print(CMD, "0x%08" PRIx32, target->working_area_size);
5124  }
5125  /* loop for more */
5126  break;
5127 
5128  case TCFG_WORK_AREA_BACKUP:
5129  if (is_configure) {
5130  if (index == CMD_ARGC) {
5131  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5133  }
5135  if (retval != ERROR_OK)
5136  return retval;
5137  index++;
5139  } else {
5140  if (index != CMD_ARGC)
5143  }
5144  /* loop for more */
5145  break;
5146 
5147  case TCFG_ENDIAN:
5148  if (is_configure) {
5149  if (index == CMD_ARGC) {
5150  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5152  }
5154  if (!n->name) {
5157  }
5158  index++;
5159  target->endianness = n->value;
5160  } else {
5161  if (index != CMD_ARGC)
5164  if (!n->name) {
5167  }
5168  command_print(CMD, "%s", n->name);
5169  }
5170  /* loop for more */
5171  break;
5172 
5173  case TCFG_COREID:
5174  if (is_configure) {
5175  if (index == CMD_ARGC) {
5176  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5178  }
5179  COMMAND_PARSE_NUMBER(s32, CMD_ARGV[index], target->coreid);
5180  index++;
5181  } else {
5182  if (index != CMD_ARGC)
5184  command_print(CMD, "%" PRIi32, target->coreid);
5185  }
5186  /* loop for more */
5187  break;
5188 
5189  case TCFG_CHAIN_POSITION:
5190  LOG_TARGET_WARNING(target, "DEPRECATED! '-chain-position' will be removed in the future, use '-tap' instead");
5191  /* fallthrough */
5192  case TCFG_TAP:
5193  if (is_configure) {
5194  if (target->has_dap) {
5195  command_print(CMD, "target requires -dap parameter instead of -tap");
5197  }
5198 
5199  if (index == CMD_ARGC) {
5200  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5202  }
5203  struct jtag_tap *tap = jtag_tap_by_string(CMD_ARGV[index]);
5204  if (!tap) {
5205  command_print(CMD, "Tap '%s' could not be found", CMD_ARGV[index]);
5207  }
5208  index++;
5209  target->tap = tap;
5210  target->tap_configured = true;
5211  } else {
5212  if (index != CMD_ARGC)
5215  }
5216  /* loop for more */
5217  break;
5218 
5219  case TCFG_DBGBASE:
5220  if (is_configure) {
5221  if (index == CMD_ARGC) {
5222  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5224  }
5226  index++;
5227  target->dbgbase_set = true;
5228  } else {
5229  if (index != CMD_ARGC)
5231  command_print(CMD, "0x%08" PRIx32, target->dbgbase);
5232  }
5233  /* loop for more */
5234  break;
5235 
5236  case TCFG_RTOS:
5237  if (is_configure) {
5238  if (index == CMD_ARGC) {
5239  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5241  }
5242  retval = rtos_create(CMD, target, CMD_ARGV[index]);
5243  if (retval != ERROR_OK)
5244  return retval;
5245  index++;
5246  } else {
5247  if (index != CMD_ARGC)
5249  if (target->rtos)
5250  command_print(CMD, "%s", target->rtos->type->name);
5251  }
5252  /* loop for more */
5253  break;
5254 
5255  case TCFG_DEFER_EXAMINE:
5256  if (is_configure)
5257  target->defer_examine = true;
5258  else
5259  command_print(CMD, "%s", target->defer_examine ? "true" : "false");
5260  /* loop for more */
5261  break;
5262 
5263  case TCFG_GDB_PORT:
5264  if (is_configure) {
5265  if (index == CMD_ARGC) {
5266  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5268  }
5269 
5270  /* TODO: generalize test of COMMAND_CONFIG */
5271  if (CMD_CTX->mode != COMMAND_CONFIG) {
5272  command_print(CMD, "-gdb-port must be configured before 'init'");
5274  }
5275 
5276  char *s = strdup(CMD_ARGV[index]);
5277  if (!s) {
5278  LOG_ERROR("Out of memory");
5279  return ERROR_FAIL;
5280  }
5281  free(target->gdb_port_override);
5283  index++;
5284  } else {
5285  if (index != CMD_ARGC)
5288  }
5289  /* loop for more */
5290  break;
5291 
5293  if (is_configure) {
5294  if (index == CMD_ARGC) {
5295  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5297  }
5298 
5299  if (CMD_CTX->mode != COMMAND_CONFIG) {
5300  command_print(CMD, "-gdb-max-connections must be configured before 'init'");
5302  }
5303 
5305  index++;
5306  if (target->gdb_max_connections < 0)
5308  } else {
5309  if (index != CMD_ARGC)
5312  }
5313  /* loop for more */
5314  break;
5315  }
5316  }
5317 
5318  return ERROR_OK;
5319 }
5320 
5321 COMMAND_HANDLER(handle_target_configure)
5322 {
5323  if (!CMD_ARGC)
5325 
5326  bool is_configure = !strcmp(CMD_NAME, "configure");
5327 
5329 
5330  return CALL_COMMAND_HANDLER(target_configure, target, 0, is_configure);
5331 }
5332 
5333 COMMAND_HANDLER(handle_target_examine)
5334 {
5335  bool allow_defer = false;
5336 
5337  if (CMD_ARGC > 1)
5339 
5340  if (CMD_ARGC == 1) {
5341  if (strcmp(CMD_ARGV[0], "allow-defer"))
5343  allow_defer = true;
5344  }
5345 
5347  if (!target->tap->enabled) {
5348  command_print(CMD, "[TAP is disabled]");
5349  return ERROR_FAIL;
5350  }
5351 
5352  if (allow_defer && target->defer_examine) {
5353  LOG_INFO("Deferring arp_examine of %s", target_name(target));
5354  LOG_INFO("Use arp_examine command to examine it manually!");
5355  return ERROR_OK;
5356  }
5357 
5358  int retval = target->type->examine(target);
5359  if (retval != ERROR_OK) {
5361  return retval;
5362  }
5363 
5365 
5366  return ERROR_OK;
5367 }
5368 
5369 COMMAND_HANDLER(handle_target_was_examined)
5370 {
5371  if (CMD_ARGC != 0)
5373 
5375 
5376  command_print(CMD, "%d", target_was_examined(target) ? 1 : 0);
5377 
5378  return ERROR_OK;
5379 }
5380 
5381 COMMAND_HANDLER(handle_target_examine_deferred)
5382 {
5383  if (CMD_ARGC != 0)
5385 
5387 
5388  command_print(CMD, "%d", target->defer_examine ? 1 : 0);
5389 
5390  return ERROR_OK;
5391 }
5392 
5393 COMMAND_HANDLER(handle_target_halt_gdb)
5394 {
5395  if (CMD_ARGC != 0)
5397 
5399 
5401 }
5402 
5403 COMMAND_HANDLER(handle_target_poll)
5404 {
5405  if (CMD_ARGC != 0)
5407 
5409  if (!target->tap->enabled) {
5410  command_print(CMD, "[TAP is disabled]");
5411  return ERROR_FAIL;
5412  }
5413 
5414  if (!(target_was_examined(target)))
5416 
5417  return target->type->poll(target);
5418 }
5419 
5420 COMMAND_HANDLER(handle_target_reset)
5421 {
5422  if (CMD_ARGC != 2)
5424 
5425  const struct nvp *n = nvp_name2value(nvp_assert, CMD_ARGV[0]);
5426  if (!n->name) {
5429  }
5430 
5431  /* the halt or not param */
5432  int a;
5433  COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], a);
5434 
5436  if (!target->tap->enabled) {
5437  command_print(CMD, "[TAP is disabled]");
5438  return ERROR_FAIL;
5439  }
5440 
5442  command_print(CMD, "No target-specific reset for %s", target_name(target));
5443  return ERROR_FAIL;
5444  }
5445 
5446  /* determine if we should halt or not. */
5447  target->reset_halt = (a != 0);
5448  /* When this happens - all workareas are invalid. */
5450 
5451  /* do the assert */
5452  if (n->value == NVP_ASSERT) {
5453  int retval = target->type->assert_reset(target);
5454  if (target->defer_examine) {
5457  }
5458  return retval;
5459  }
5460 
5461  return target->type->deassert_reset(target);
5462 }
5463 
5464 COMMAND_HANDLER(handle_target_halt)
5465 {
5466  if (CMD_ARGC != 0)
5468 
5470  if (!target->tap->enabled) {
5471  command_print(CMD, "[TAP is disabled]");
5472  return ERROR_FAIL;
5473  }
5474 
5475  return target->type->halt(target);
5476 }
5477 
5478 COMMAND_HANDLER(handle_target_wait_state)
5479 {
5480  if (CMD_ARGC != 2)
5482 
5483  const struct nvp *n = nvp_name2value(nvp_target_state, CMD_ARGV[0]);
5484  if (!n->name) {
5487  }
5488 
5489  unsigned int a;
5490  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], a);
5491 
5493  if (!target->tap->enabled) {
5494  command_print(CMD, "[TAP is disabled]");
5495  return ERROR_FAIL;
5496  }
5497 
5498  int retval = target_wait_state(target, n->value, a);
5499  if (retval != ERROR_OK) {
5501  "target: %s wait %s fails (%d) %s",
5502  target_name(target), n->name,
5503  retval, target_strerror_safe(retval));
5504  return retval;
5505  }
5506  return ERROR_OK;
5507 }
5508 /* List for human, Events defined for this target.
5509  * scripts/programs should use 'name cget -event NAME'
5510  */
5511 COMMAND_HANDLER(handle_target_event_list)
5512 {
5514  struct target_event_action *teap;
5515 
5516  command_print(CMD, "Event actions for target %s\n",
5517  target_name(target));
5518  command_print(CMD, "%-25s | Body", "Event");
5519  command_print(CMD, "------------------------- | "
5520  "----------------------------------------");
5521 
5523  command_print(CMD, "%-25s | %s",
5524  target_event_name(teap->event),
5525  Jim_GetString(teap->body, NULL));
5526 
5527  command_print(CMD, "***END***");
5528  return ERROR_OK;
5529 }
5530 
5531 COMMAND_HANDLER(handle_target_current_state)
5532 {
5533  if (CMD_ARGC != 0)
5535 
5537 
5539 
5540  return ERROR_OK;
5541 }
5542 
5543 COMMAND_HANDLER(handle_target_debug_reason)
5544 {
5545  if (CMD_ARGC != 0)
5547 
5549 
5550 
5553 
5554  if (!debug_reason) {
5555  command_print(CMD, "bug: invalid debug reason (%d)",
5556  target->debug_reason);
5557  return ERROR_FAIL;
5558  }
5559 
5560  command_print(CMD, "%s", debug_reason);
5561 
5562  return ERROR_OK;
5563 }
5564 
5565 COMMAND_HANDLER(handle_target_invoke_event)
5566 {
5567  if (CMD_ARGC != 1)
5569 
5570  const struct nvp *n = nvp_name2value(nvp_target_event, CMD_ARGV[0]);
5571  if (!n->name) {
5574  }
5575 
5578  return ERROR_OK;
5579 }
5580 
5581 COMMAND_HANDLER(handle_target_disassemble)
5582 {
5584 
5585  if (CMD_ARGC < 1 || CMD_ARGC > 3)
5587 
5588  if (CMD_ARGC == 1 && !strcmp("list", CMD_ARGV[0]))
5589  return oocd_cs_list_insn_types(CMD);
5590 
5593 
5594  unsigned int count = 1;
5595  if (CMD_ARGC > 1)
5596  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
5597 
5598  const char *insn_set;
5599  if (CMD_ARGC > 2) {
5600  insn_set = CMD_ARGV[2];
5601  } else {
5602  int retval = target_insn_set(CMD, target, &insn_set);
5603  if (retval != ERROR_OK)
5604  return retval;
5605  LOG_TARGET_DEBUG(target, "instruction set \"%s\"", insn_set);
5606  }
5607 
5608  return oocd_cs_disassemble(CMD, target, address, count, insn_set);
5609 }
5610 
5612  {
5613  .name = "configure",
5614  .mode = COMMAND_ANY,
5615  .handler = handle_target_configure,
5616  .help = "configure a new target for use",
5617  .usage = "[target_attribute ...]",
5618  },
5619  {
5620  .name = "cget",
5621  .mode = COMMAND_ANY,
5622  .handler = handle_target_configure,
5623  .help = "returns the specified target attribute",
5624  .usage = "target_attribute",
5625  },
5626  {
5627  .name = "mwd",
5628  .handler = handle_mw_command,
5629  .mode = COMMAND_EXEC,
5630  .help = "Write 64-bit word(s) to target memory",
5631  .usage = "address data [count]",
5632  },
5633  {
5634  .name = "mww",
5635  .handler = handle_mw_command,
5636  .mode = COMMAND_EXEC,
5637  .help = "Write 32-bit word(s) to target memory",
5638  .usage = "address data [count]",
5639  },
5640  {
5641  .name = "mwh",
5642  .handler = handle_mw_command,
5643  .mode = COMMAND_EXEC,
5644  .help = "Write 16-bit half-word(s) to target memory",
5645  .usage = "address data [count]",
5646  },
5647  {
5648  .name = "mwb",
5649  .handler = handle_mw_command,
5650  .mode = COMMAND_EXEC,
5651  .help = "Write byte(s) to target memory",
5652  .usage = "address data [count]",
5653  },
5654  {
5655  .name = "mdd",
5656  .handler = handle_md_command,
5657  .mode = COMMAND_EXEC,
5658  .help = "Display target memory as 64-bit words",
5659  .usage = "address [count]",
5660  },
5661  {
5662  .name = "mdw",
5663  .handler = handle_md_command,
5664  .mode = COMMAND_EXEC,
5665  .help = "Display target memory as 32-bit words",
5666  .usage = "address [count]",
5667  },
5668  {
5669  .name = "mdh",
5670  .handler = handle_md_command,
5671  .mode = COMMAND_EXEC,
5672  .help = "Display target memory as 16-bit half-words",
5673  .usage = "address [count]",
5674  },
5675  {
5676  .name = "mdb",
5677  .handler = handle_md_command,
5678  .mode = COMMAND_EXEC,
5679  .help = "Display target memory as 8-bit bytes",
5680  .usage = "address [count]",
5681  },
5682  {
5683  .name = "get_reg",
5684  .mode = COMMAND_EXEC,
5685  .handler = handle_target_get_reg,
5686  .help = "Get register values from the target",
5687  .usage = "[-force] list",
5688  },
5689  {
5690  .name = "set_reg",
5691  .mode = COMMAND_EXEC,
5692  .handler = handle_set_reg_command,
5693  .help = "Set target register values",
5694  .usage = "dict",
5695  },
5696  {
5697  .name = "read_memory",
5698  .mode = COMMAND_EXEC,
5699  .handler = handle_target_read_memory,
5700  .help = "Read Tcl list of 8/16/32/64 bit numbers from target memory",
5701  .usage = "address width count ['phys']",
5702  },
5703  {
5704  .name = "write_memory",
5705  .mode = COMMAND_EXEC,
5706  .handler = handle_target_write_memory,
5707  .help = "Write Tcl list of 8/16/32/64 bit numbers to target memory",
5708  .usage = "address width data ['phys']",
5709  },
5710  {
5711  .name = "eventlist",
5712  .handler = handle_target_event_list,
5713  .mode = COMMAND_EXEC,
5714  .help = "displays a table of events defined for this target",
5715  .usage = "",
5716  },
5717  {
5718  .name = "curstate",
5719  .mode = COMMAND_EXEC,
5720  .handler = handle_target_current_state,
5721  .help = "displays the current state of this target",
5722  .usage = "",
5723  },
5724  {
5725  .name = "debug_reason",
5726  .mode = COMMAND_EXEC,
5727  .handler = handle_target_debug_reason,
5728  .help = "displays the debug reason of this target",
5729  .usage = "",
5730  },
5731  {
5732  .name = "arp_examine",
5733  .mode = COMMAND_EXEC,
5734  .handler = handle_target_examine,
5735  .help = "used internally for reset processing",
5736  .usage = "['allow-defer']",
5737  },
5738  {
5739  .name = "was_examined",
5740  .mode = COMMAND_EXEC,
5741  .handler = handle_target_was_examined,
5742  .help = "used internally for reset processing",
5743  .usage = "",
5744  },
5745  {
5746  .name = "examine_deferred",
5747  .mode = COMMAND_EXEC,
5748  .handler = handle_target_examine_deferred,
5749  .help = "used internally for reset processing",
5750  .usage = "",
5751  },
5752  {
5753  .name = "arp_halt_gdb",
5754  .mode = COMMAND_EXEC,
5755  .handler = handle_target_halt_gdb,
5756  .help = "used internally for reset processing to halt GDB",
5757  .usage = "",
5758  },
5759  {
5760  .name = "arp_poll",
5761  .mode = COMMAND_EXEC,
5762  .handler = handle_target_poll,
5763  .help = "used internally for reset processing",
5764  .usage = "",
5765  },
5766  {
5767  .name = "arp_reset",
5768  .mode = COMMAND_EXEC,
5769  .handler = handle_target_reset,
5770  .help = "used internally for reset processing",
5771  .usage = "'assert'|'deassert' halt",
5772  },
5773  {
5774  .name = "arp_halt",
5775  .mode = COMMAND_EXEC,
5776  .handler = handle_target_halt,
5777  .help = "used internally for reset processing",
5778  .usage = "",
5779  },
5780  {
5781  .name = "arp_waitstate",
5782  .mode = COMMAND_EXEC,
5783  .handler = handle_target_wait_state,
5784  .help = "used internally for reset processing",
5785  .usage = "statename timeoutmsecs",
5786  },
5787  {
5788  .name = "invoke-event",
5789  .mode = COMMAND_EXEC,
5790  .handler = handle_target_invoke_event,
5791  .help = "invoke handler for specified event",
5792  .usage = "event_name",
5793  },
5794  {
5795  .name = "disassemble",
5796  .mode = COMMAND_EXEC,
5797  .handler = handle_target_disassemble,
5798  .help = "disassemble instructions",
5799  .usage = "list | address [count [instruction_set]]",
5800  },
5802 };
5803 
5804 COMMAND_HANDLER(handle_target_create)
5805 {
5806  int retval = ERROR_OK;
5807 
5808  if (CMD_ARGC < 2)
5810 
5811  /* check if the target name clashes with an existing command name */
5812  Jim_Cmd *jimcmd = Jim_GetCommand(CMD_CTX->interp, CMD_JIMTCL_ARGV[0], JIM_NONE);
5813  if (jimcmd) {
5814  command_print(CMD, "Command/target: %s Exists", CMD_ARGV[0]);
5815  return ERROR_FAIL;
5816  }
5817 
5818  /* TYPE */
5819  const char *cp = CMD_ARGV[1];
5820  struct transport *tr = get_current_transport();
5821  if (tr && tr->override_target) {
5822  retval = tr->override_target(&cp);
5823  if (retval != ERROR_OK) {
5824  command_print(CMD, "The selected transport doesn't support this target");
5825  return retval;
5826  }
5827  LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
5828  }
5829  /* now does target type exist */
5830  size_t x;
5831  for (x = 0 ; x < ARRAY_SIZE(target_types) ; x++) {
5832  if (strcmp(cp, target_types[x]->name) == 0) {
5833  /* found */
5834  break;
5835  }
5836  }
5837  if (x == ARRAY_SIZE(target_types)) {
5838  char *all = NULL;
5839  for (x = 0 ; x < ARRAY_SIZE(target_types) ; x++) {
5840  char *prev = all;
5841  if (all)
5842  all = alloc_printf("%s, %s", all, target_types[x]->name);
5843  else
5844  all = alloc_printf("%s", target_types[x]->name);
5845  free(prev);
5846  if (!all) {
5847  LOG_ERROR("Out of memory");
5848  return ERROR_FAIL;
5849  }
5850  }
5851  command_print(CMD, "Unknown target type %s, try one of %s", cp, all);
5852  free(all);
5853  return ERROR_FAIL;
5854  }
5855 
5856  /* Create it */
5857  struct target *target = calloc(1, sizeof(struct target));
5858  if (!target) {
5859  LOG_ERROR("Out of memory");
5860  return ERROR_FAIL;
5861  }
5862 
5863  /* set empty smp cluster */
5864  target->smp_targets = &empty_smp_targets;
5865 
5866  /* allocate memory for each unique target type */
5867  target->type = malloc(sizeof(struct target_type));
5868  if (!target->type) {
5869  LOG_ERROR("Out of memory");
5870  free(target);
5871  return ERROR_FAIL;
5872  }
5873 
5874  memcpy(target->type, target_types[x], sizeof(struct target_type));
5875 
5876  /* default to first core, override with -coreid */
5877  target->coreid = 0;
5878 
5879  target->working_area = 0x0;
5880  target->working_area_size = 0x0;
5882  target->backup_working_area = false;
5883 
5886  target->reg_cache = NULL;
5887  target->breakpoints = NULL;
5888  target->watchpoints = NULL;
5889  target->next = NULL;
5890  target->arch_info = NULL;
5891 
5892  target->verbose_halt_msg = true;
5893 
5894  target->halt_issued = false;
5895 
5897 
5898  /* initialize trace information */
5899  target->trace_info = calloc(1, sizeof(struct trace));
5900  if (!target->trace_info) {
5901  LOG_ERROR("Out of memory");
5902  free(target->type);
5903  free(target);
5904  return ERROR_FAIL;
5905  }
5906 
5907  target->dbgmsg = NULL;
5908  target->dbg_msg_enabled = false;
5909 
5911 
5912  target->rtos = NULL;
5913  target->rtos_auto_detect = false;
5914 
5917 
5918  target->cmd_name = strdup(CMD_ARGV[0]);
5919  if (!target->cmd_name) {
5920  LOG_ERROR("Out of memory");
5921  free(target->trace_info);
5922  free(target->type);
5923  free(target);
5924  return ERROR_FAIL;
5925  }
5926 
5927  /* Do the rest as "configure" options */
5928  bool is_configure = true;
5929  retval = CALL_COMMAND_HANDLER(target_configure, target, 2, is_configure);
5930  if (retval == ERROR_OK) {
5931  if (target->has_dap) {
5932  if (!target->dap_configured) {
5933  command_print(CMD, "-dap ?name? required when creating target");
5935  }
5936  } else {
5937  if (!target->tap_configured) {
5938  command_print(CMD, "-tap ?name? required when creating target");
5940  }
5941  }
5942  /* tap must be set after target was configured */
5943  if (!target->tap)
5945  }
5946 
5947  if (retval != ERROR_OK) {
5949  free(target->gdb_port_override);
5950  free(target->trace_info);
5951  free(target->type);
5952  free(target->private_config);
5953  free(target);
5954  return retval;
5955  }
5956 
5958  /* default endian to little if not specified */
5960  }
5961 
5962  if (target->type->target_create) {
5963  retval = (*target->type->target_create)(target);
5964  if (retval != ERROR_OK) {
5965  LOG_DEBUG("target_create failed");
5966  free(target->cmd_name);
5968  free(target->gdb_port_override);
5969  free(target->trace_info);
5970  free(target->type);
5971  free(target->private_config);
5972  free(target);
5973  return retval;
5974  }
5975  }
5976 
5977  /* create the target specific commands */
5978  if (target->type->commands) {
5980  if (retval != ERROR_OK)
5981  LOG_ERROR("unable to register '%s' commands", CMD_ARGV[0]);
5982  }
5983 
5984  /* now - create the new target name command */
5985  const struct command_registration target_subcommands[] = {
5986  {
5988  },
5989  {
5990  .chain = target->type->commands,
5991  },
5993  };
5994  const struct command_registration target_commands[] = {
5995  {
5996  .name = CMD_ARGV[0],
5997  .mode = COMMAND_ANY,
5998  .help = "target command group",
5999  .usage = "",
6000  .chain = target_subcommands,
6001  },
6003  };
6004  retval = register_commands_override_target(CMD_CTX, NULL, target_commands, target);
6005  if (retval != ERROR_OK) {
6006  if (target->type->deinit_target)
6008  free(target->cmd_name);
6010  free(target->gdb_port_override);
6011  free(target->trace_info);
6012  free(target->type);
6013  free(target);
6014  return retval;
6015  }
6016 
6017  /* append to end of list */
6019 
6020  CMD_CTX->current_target = target;
6021  return ERROR_OK;
6022 }
6023 
6024 COMMAND_HANDLER(handle_target_current)
6025 {
6026  if (CMD_ARGC != 0)
6028 
6030  if (target)
6032 
6033  return ERROR_OK;
6034 }
6035 
6036 COMMAND_HANDLER(handle_target_types)
6037 {
6038  if (CMD_ARGC != 0)
6040 
6041  for (size_t x = 0; x < ARRAY_SIZE(target_types); x++)
6042  command_print(CMD, "%s", target_types[x]->name);
6043 
6044  return ERROR_OK;
6045 }
6046 
6047 COMMAND_HANDLER(handle_target_names)
6048 {
6049  if (CMD_ARGC != 0)
6051 
6052  struct target *target = all_targets;
6053  while (target) {
6055  target = target->next;
6056  }
6057 
6058  return ERROR_OK;
6059 }
6060 
6061 static struct target_list *
6062 __attribute__((warn_unused_result))
6063 create_target_list_node(const char *targetname)
6064 {
6065  struct target *target = get_target(targetname);
6066  LOG_DEBUG("%s ", targetname);
6067  if (!target)
6068  return NULL;
6069 
6070  struct target_list *new = malloc(sizeof(struct target_list));
6071  if (!new) {
6072  LOG_ERROR("Out of memory");
6073  return new;
6074  }
6075 
6076  new->target = target;
6077  return new;
6078 }
6079 
6081  struct list_head *lh, struct target **result)
6082 {
6083  struct target *target = NULL;
6084  struct target_list *curr;
6085  foreach_smp_target(curr, lh) {
6086  struct rtos *curr_rtos = curr->target->rtos;
6087  if (curr_rtos) {
6088  if (target && target->rtos && target->rtos->type != curr_rtos->type) {
6089  command_print(cmd, "Different rtos types in members of one smp target!");
6090  return ERROR_FAIL;
6091  }
6092  target = curr->target;
6093  }
6094  }
6095  *result = target;
6096  return ERROR_OK;
6097 }
6098 
6099 COMMAND_HANDLER(handle_target_smp)
6100 {
6101  static unsigned int smp_group = 1;
6102 
6103  if (CMD_ARGC == 0) {
6104  LOG_DEBUG("Empty SMP target");
6105  return ERROR_OK;
6106  }
6107  LOG_DEBUG("%d", CMD_ARGC);
6108  /* CMD_ARGC[0] = target to associate in smp
6109  * CMD_ARGC[1] = target to associate in smp
6110  * CMD_ARGC[2] ...
6111  */
6112 
6113  struct list_head *lh = malloc(sizeof(*lh));
6114  if (!lh) {
6115  LOG_ERROR("Out of memory");
6116  return ERROR_FAIL;
6117  }
6118  INIT_LIST_HEAD(lh);
6119 
6120  for (unsigned int i = 0; i < CMD_ARGC; i++) {
6121  struct target_list *new = create_target_list_node(CMD_ARGV[i]);
6122  if (new)
6123  list_add_tail(&new->lh, lh);
6124  }
6125  struct target_list *curr;
6126  foreach_smp_target(curr, lh) {
6127  struct target *target = curr->target;
6129  }
6130  foreach_smp_target(curr, lh) {
6131  struct target *target = curr->target;
6132  target->smp = true;
6133  target->smp_id = smp_group;
6134  target->smp_targets = lh;
6135  }
6136  smp_group++;
6137 
6138  struct target *rtos_target;
6139  int retval = get_target_with_common_rtos_type(CMD, lh, &rtos_target);
6140  if (retval == ERROR_OK && rtos_target)
6141  retval = rtos_smp_init(rtos_target);
6142 
6143  if (retval != ERROR_OK)
6145 
6146  return retval;
6147 }
6148 
6149 static const struct command_registration target_subcommand_handlers[] = {
6150  {
6151  .name = "init",
6152  .mode = COMMAND_CONFIG,
6153  .handler = handle_target_init_command,
6154  .help = "initialize targets",
6155  .usage = "",
6156  },
6157  {
6158  .name = "create",
6159  .mode = COMMAND_CONFIG,
6160  .handler = handle_target_create,
6161  .usage = "name type [options ...]",
6162  .help = "Creates and selects a new target",
6163  },
6164  {
6165  .name = "current",
6166  .mode = COMMAND_ANY,
6167  .handler = handle_target_current,
6168  .help = "Returns the currently selected target",
6169  .usage = "",
6170  },
6171  {
6172  .name = "types",
6173  .mode = COMMAND_ANY,
6174  .handler = handle_target_types,
6175  .help = "Returns the available target types as "
6176  "a list of strings",
6177  .usage = "",
6178  },
6179  {
6180  .name = "names",
6181  .mode = COMMAND_ANY,
6182  .handler = handle_target_names,
6183  .help = "Returns the names of all targets as a list of strings",
6184  .usage = "",
6185  },
6186  {
6187  .name = "smp",
6188  .mode = COMMAND_ANY,
6189  .handler = handle_target_smp,
6190  .usage = "targetname1 targetname2 ...",
6191  .help = "gather several target in a smp list"
6192  },
6193 
6195 };
6196 
6197 struct fast_load {
6199  uint8_t *data;
6200  int length;
6201 
6202 };
6203 
6204 static int fastload_num;
6205 static struct fast_load *fastload;
6206 
6207 static void free_fastload(void)
6208 {
6209  if (fastload) {
6210  for (int i = 0; i < fastload_num; i++)
6211  free(fastload[i].data);
6212  free(fastload);
6213  fastload = NULL;
6214  }
6215 }
6216 
6217 COMMAND_HANDLER(handle_fast_load_image_command)
6218 {
6219  uint8_t *buffer;
6220  size_t buf_cnt;
6221  uint32_t image_size;
6222  target_addr_t min_address = 0;
6223  target_addr_t max_address = -1;
6224 
6225  struct image image;
6226 
6227  int retval = CALL_COMMAND_HANDLER(parse_load_image_command,
6228  &image, &min_address, &max_address);
6229  if (retval != ERROR_OK)
6230  return retval;
6231 
6232  struct duration bench;
6233  duration_start(&bench);
6234 
6235  retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL);
6236  if (retval != ERROR_OK)
6237  return retval;
6238 
6239  image_size = 0x0;
6240  retval = ERROR_OK;
6242  fastload = malloc(sizeof(struct fast_load)*image.num_sections);
6243  if (!fastload) {
6244  command_print(CMD, "out of memory");
6245  image_close(&image);
6246  return ERROR_FAIL;
6247  }
6248  memset(fastload, 0, sizeof(struct fast_load)*image.num_sections);
6249  for (unsigned int i = 0; i < image.num_sections; i++) {
6250  buffer = malloc(image.sections[i].size);
6251  if (!buffer) {
6252  command_print(CMD, "error allocating buffer for section (%d bytes)",
6253  (int)(image.sections[i].size));
6254  retval = ERROR_FAIL;
6255  break;
6256  }
6257 
6258  retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
6259  if (retval != ERROR_OK) {
6260  free(buffer);
6261  break;
6262  }
6263 
6264  uint32_t offset = 0;
6265  uint32_t length = buf_cnt;
6266 
6267  /* DANGER!!! beware of unsigned comparison here!!! */
6268 
6269  if ((image.sections[i].base_address + buf_cnt >= min_address) &&
6270  (image.sections[i].base_address < max_address)) {
6271  if (image.sections[i].base_address < min_address) {
6272  /* clip addresses below */
6273  offset += min_address-image.sections[i].base_address;
6274  length -= offset;
6275  }
6276 
6277  if (image.sections[i].base_address + buf_cnt > max_address)
6278  length -= (image.sections[i].base_address + buf_cnt)-max_address;
6279 
6281  fastload[i].data = malloc(length);
6282  if (!fastload[i].data) {
6283  free(buffer);
6284  command_print(CMD, "error allocating buffer for section (%" PRIu32 " bytes)",
6285  length);
6286  retval = ERROR_FAIL;
6287  break;
6288  }
6289  memcpy(fastload[i].data, buffer + offset, length);
6290  fastload[i].length = length;
6291 
6292  image_size += length;
6293  command_print(CMD, "%u bytes written at address 0x%8.8x",
6294  (unsigned int)length,
6295  ((unsigned int)(image.sections[i].base_address + offset)));
6296  }
6297 
6298  free(buffer);
6299  }
6300 
6301  if (retval == ERROR_OK && duration_measure(&bench) == ERROR_OK) {
6302  command_print(CMD, "Loaded %" PRIu32 " bytes "
6303  "in %fs (%0.3f KiB/s)", image_size,
6304  duration_elapsed(&bench), duration_kbps(&bench, image_size));
6305 
6307  "WARNING: image has not been loaded to target!"
6308  "You can issue a 'fast_load' to finish loading.");
6309  }
6310 
6311  image_close(&image);
6312 
6313  if (retval != ERROR_OK)
6314  free_fastload();
6315 
6316  return retval;
6317 }
6318 
6319 COMMAND_HANDLER(handle_fast_load_command)
6320 {
6321  if (CMD_ARGC > 0)
6323  if (!fastload) {
6324  LOG_ERROR("No image in memory");
6325  return ERROR_FAIL;
6326  }
6327  int i;
6328  int64_t ms = timeval_ms();
6329  int size = 0;
6330  int retval = ERROR_OK;
6331  for (i = 0; i < fastload_num; i++) {
6333  command_print(CMD, "Write to 0x%08x, length 0x%08x",
6334  (unsigned int)(fastload[i].address),
6335  (unsigned int)(fastload[i].length));
6336  retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
6337  if (retval != ERROR_OK)
6338  break;
6339  size += fastload[i].length;
6340  }
6341  if (retval == ERROR_OK) {
6342  int64_t after = timeval_ms();
6343  command_print(CMD, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
6344  }
6345  return retval;
6346 }
6347 
6348 static const struct command_registration target_command_handlers[] = {
6349  {
6350  .name = "targets",
6351  .handler = handle_targets_command,
6352  .mode = COMMAND_ANY,
6353  .help = "change current default target (one parameter) "
6354  "or prints table of all targets (no parameters)",
6355  .usage = "[target]",
6356  },
6357  {
6358  .name = "target",
6359  .mode = COMMAND_CONFIG,
6360  .help = "configure target",
6361  .chain = target_subcommand_handlers,
6362  .usage = "",
6363  },
6364  {
6365  .name = "poll_interval",
6366  .handler = handle_poll_interval_command,
6367  .mode = COMMAND_ANY,
6368  .help = "print or set the target state polling interval",
6369  .usage = "[milliseconds]",
6370  },
6372 };
6373 
6375 {
6377 }
6378 
6379 static bool target_reset_nag = true;
6380 
6382 {
6383  return target_reset_nag;
6384 }
6385 
6386 COMMAND_HANDLER(handle_target_reset_nag)
6387 {
6388  return CALL_COMMAND_HANDLER(handle_command_parse_bool,
6389  &target_reset_nag, "Nag after each reset about options to improve "
6390  "performance");
6391 }
6392 
6393 COMMAND_HANDLER(handle_ps_command)
6394 {
6396  char *display;
6397  if (target->state != TARGET_HALTED) {
6398  command_print(CMD, "Error: [%s] not halted", target_name(target));
6399  return ERROR_TARGET_NOT_HALTED;
6400  }
6401 
6402  if ((target->rtos) && (target->rtos->type)
6403  && (target->rtos->type->ps_command)) {
6404  display = target->rtos->type->ps_command(target);
6405  command_print(CMD, "%s", display);
6406  free(display);
6407  return ERROR_OK;
6408  } else {
6409  LOG_INFO("failed");
6410  return ERROR_TARGET_FAILURE;
6411  }
6412 }
6413 
6414 static void binprint(struct command_invocation *cmd, const char *text, const uint8_t *buf, int size)
6415 {
6416  if (text)
6417  command_print_sameline(cmd, "%s", text);
6418  for (int i = 0; i < size; i++)
6419  command_print_sameline(cmd, " %02x", buf[i]);
6420  command_print(cmd, " ");
6421 }
6422 
6423 COMMAND_HANDLER(handle_test_mem_access_command)
6424 {
6426  uint32_t test_size;
6427  int retval = ERROR_OK;
6428 
6429  if (target->state != TARGET_HALTED) {
6430  command_print(CMD, "Error: [%s] not halted", target_name(target));
6431  return ERROR_TARGET_NOT_HALTED;
6432  }
6433 
6434  if (CMD_ARGC != 1)
6436 
6437  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], test_size);
6438 
6439  /* Test reads */
6440  size_t num_bytes = test_size + 4;
6441 
6442  struct working_area *wa = NULL;
6443  retval = target_alloc_working_area(target, num_bytes, &wa);
6444  if (retval != ERROR_OK) {
6445  LOG_ERROR("Not enough working area");
6446  return ERROR_FAIL;
6447  }
6448 
6449  uint8_t *test_pattern = malloc(num_bytes);
6450 
6451  for (size_t i = 0; i < num_bytes; i++)
6452  test_pattern[i] = rand();
6453 
6454  retval = target_write_memory(target, wa->address, 1, num_bytes, test_pattern);
6455  if (retval != ERROR_OK) {
6456  LOG_ERROR("Test pattern write failed");
6457  goto out;
6458  }
6459 
6460  for (int host_offset = 0; host_offset <= 1; host_offset++) {
6461  for (int size = 1; size <= 4; size *= 2) {
6462  for (int offset = 0; offset < 4; offset++) {
6463  uint32_t count = test_size / size;
6464  size_t host_bufsiz = (count + 2) * size + host_offset;
6465  uint8_t *read_ref = malloc(host_bufsiz);
6466  uint8_t *read_buf = malloc(host_bufsiz);
6467 
6468  for (size_t i = 0; i < host_bufsiz; i++) {
6469  read_ref[i] = rand();
6470  read_buf[i] = read_ref[i];
6471  }
6473  "Test read %" PRIu32 " x %d @ %d to %saligned buffer: ", count,
6474  size, offset, host_offset ? "un" : "");
6475 
6476  struct duration bench;
6477  duration_start(&bench);
6478 
6479  retval = target_read_memory(target, wa->address + offset, size, count,
6480  read_buf + size + host_offset);
6481 
6482  duration_measure(&bench);
6483 
6484  if (retval == ERROR_TARGET_UNALIGNED_ACCESS) {
6485  command_print(CMD, "Unsupported alignment");
6486  goto next;
6487  } else if (retval != ERROR_OK) {
6488  command_print(CMD, "Memory read failed");
6489  goto next;
6490  }
6491 
6492  /* replay on host */
6493  memcpy(read_ref + size + host_offset, test_pattern + offset, count * size);
6494 
6495  /* check result */
6496  int result = memcmp(read_ref, read_buf, host_bufsiz);
6497  if (result == 0) {
6498  command_print(CMD, "Pass in %fs (%0.3f KiB/s)",
6499  duration_elapsed(&bench),
6500  duration_kbps(&bench, count * size));
6501  } else {
6502  command_print(CMD, "Compare failed");
6503  binprint(CMD, "ref:", read_ref, host_bufsiz);
6504  binprint(CMD, "buf:", read_buf, host_bufsiz);
6505  }
6506 next:
6507  free(read_ref);
6508  free(read_buf);
6509  }
6510  }
6511  }
6512 
6513 out:
6514  free(test_pattern);
6515 
6517 
6518  /* Test writes */
6519  num_bytes = test_size + 4 + 4 + 4;
6520 
6521  retval = target_alloc_working_area(target, num_bytes, &wa);
6522  if (retval != ERROR_OK) {
6523  LOG_ERROR("Not enough working area");
6524  return ERROR_FAIL;
6525  }
6526 
6527  test_pattern = malloc(num_bytes);
6528 
6529  for (size_t i = 0; i < num_bytes; i++)
6530  test_pattern[i] = rand();
6531 
6532  for (int host_offset = 0; host_offset <= 1; host_offset++) {
6533  for (int size = 1; size <= 4; size *= 2) {
6534  for (int offset = 0; offset < 4; offset++) {
6535  uint32_t count = test_size / size;
6536  size_t host_bufsiz = count * size + host_offset;
6537  uint8_t *read_ref = malloc(num_bytes);
6538  uint8_t *read_buf = malloc(num_bytes);
6539  uint8_t *write_buf = malloc(host_bufsiz);
6540 
6541  for (size_t i = 0; i < host_bufsiz; i++)
6542  write_buf[i] = rand();
6544  "Test write %" PRIu32 " x %d @ %d from %saligned buffer: ", count,
6545  size, offset, host_offset ? "un" : "");
6546 
6547  retval = target_write_memory(target, wa->address, 1, num_bytes, test_pattern);
6548  if (retval != ERROR_OK) {
6549  command_print(CMD, "Test pattern write failed");
6550  goto nextw;
6551  }
6552 
6553  /* replay on host */
6554  memcpy(read_ref, test_pattern, num_bytes);
6555  memcpy(read_ref + size + offset, write_buf + host_offset, count * size);
6556 
6557  struct duration bench;
6558  duration_start(&bench);
6559 
6560  retval = target_write_memory(target, wa->address + size + offset, size, count,
6561  write_buf + host_offset);
6562 
6563  duration_measure(&bench);
6564 
6565  if (retval == ERROR_TARGET_UNALIGNED_ACCESS) {
6566  command_print(CMD, "Unsupported alignment");
6567  goto nextw;
6568  } else if (retval != ERROR_OK) {
6569  command_print(CMD, "Memory write failed");
6570  goto nextw;
6571  }
6572 
6573  /* read back */
6574  retval = target_read_memory(target, wa->address, 1, num_bytes, read_buf);
6575  if (retval != ERROR_OK) {
6576  command_print(CMD, "Test pattern write failed");
6577  goto nextw;
6578  }
6579 
6580  /* check result */
6581  int result = memcmp(read_ref, read_buf, num_bytes);
6582  if (result == 0) {
6583  command_print(CMD, "Pass in %fs (%0.3f KiB/s)",
6584  duration_elapsed(&bench),
6585  duration_kbps(&bench, count * size));
6586  } else {
6587  command_print(CMD, "Compare failed");
6588  binprint(CMD, "ref:", read_ref, num_bytes);
6589  binprint(CMD, "buf:", read_buf, num_bytes);
6590  }
6591 nextw:
6592  free(read_ref);
6593  free(read_buf);
6594  }
6595  }
6596  }
6597 
6598  free(test_pattern);
6599 
6601  return retval;
6602 }
6603 
6604 static const struct command_registration target_exec_command_handlers[] = {
6605  {
6606  .name = "fast_load_image",
6607  .handler = handle_fast_load_image_command,
6608  .mode = COMMAND_ANY,
6609  .help = "Load image into server memory for later use by "
6610  "fast_load; primarily for profiling",
6611  .usage = "filename [address ['bin'|'ihex'|'elf'|'s19' "
6612  "[min_address [max_length]]]]",
6613  },
6614  {
6615  .name = "fast_load",
6616  .handler = handle_fast_load_command,
6617  .mode = COMMAND_EXEC,
6618  .help = "loads active fast load image to current target "
6619  "- mainly for profiling purposes",
6620  .usage = "",
6621  },
6622  {
6623  .name = "profile",
6624  .handler = handle_profile_command,
6625  .mode = COMMAND_EXEC,
6626  .usage = "seconds filename [start end]",
6627  .help = "profiling samples the CPU PC",
6628  },
6630  {
6631  .name = "virt2phys",
6632  .handler = handle_virt2phys_command,
6633  .mode = COMMAND_ANY,
6634  .help = "translate a virtual address into a physical address",
6635  .usage = "virtual_address",
6636  },
6637  {
6638  .name = "reg",
6639  .handler = handle_reg_command,
6640  .mode = COMMAND_EXEC,
6641  .help = "display (reread from target with \"force\") or set a register; "
6642  "with no arguments, displays all registers and their values",
6643  .usage = "[(register_number|register_name) [(value|'force')]]",
6644  },
6645  {
6646  .name = "poll",
6647  .handler = handle_poll_command,
6648  .mode = COMMAND_EXEC,
6649  .help = "poll target state; or reconfigure background polling",
6650  .usage = "['on'|'off']",
6651  },
6652  {
6653  .name = "wait_halt",
6654  .handler = handle_wait_halt_command,
6655  .mode = COMMAND_EXEC,
6656  .help = "wait up to the specified number of milliseconds "
6657  "(default 5000) for a previously requested halt",
6658  .usage = "[milliseconds]",
6659  },
6660  {
6661  .name = "halt",
6662  .handler = handle_halt_command,
6663  .mode = COMMAND_EXEC,
6664  .help = "request target to halt, then wait up to the specified "
6665  "number of milliseconds (default 5000) for it to complete",
6666  .usage = "[milliseconds]",
6667  },
6668  {
6669  .name = "resume",
6670  .handler = handle_resume_command,
6671  .mode = COMMAND_EXEC,
6672  .help = "resume target execution from current PC or address",
6673  .usage = "[address]",
6674  },
6675  {
6676  .name = "reset",
6677  .handler = handle_reset_command,
6678  .mode = COMMAND_EXEC,
6679  .usage = "[run|halt|init]",
6680  .help = "Reset all targets into the specified mode. "
6681  "Default reset mode is run, if not given.",
6682  },
6683  {
6684  .name = "soft_reset_halt",
6685  .handler = handle_soft_reset_halt_command,
6686  .mode = COMMAND_EXEC,
6687  .usage = "",
6688  .help = "halt the target and do a soft reset",
6689  },
6690  {
6691  .name = "step",
6692  .handler = handle_step_command,
6693  .mode = COMMAND_EXEC,
6694  .help = "step one instruction from current PC or address",
6695  .usage = "[address]",
6696  },
6697  {
6698  .name = "mdd",
6699  .handler = handle_md_command,
6700  .mode = COMMAND_EXEC,
6701  .help = "display memory double-words",
6702  .usage = "['phys'] address [count]",
6703  },
6704  {
6705  .name = "mdw",
6706  .handler = handle_md_command,
6707  .mode = COMMAND_EXEC,
6708  .help = "display memory words",
6709  .usage = "['phys'] address [count]",
6710  },
6711  {
6712  .name = "mdh",
6713  .handler = handle_md_command,
6714  .mode = COMMAND_EXEC,
6715  .help = "display memory half-words",
6716  .usage = "['phys'] address [count]",
6717  },
6718  {
6719  .name = "mdb",
6720  .handler = handle_md_command,
6721  .mode = COMMAND_EXEC,
6722  .help = "display memory bytes",
6723  .usage = "['phys'] address [count]",
6724  },
6725  {
6726  .name = "mwd",
6727  .handler = handle_mw_command,
6728  .mode = COMMAND_EXEC,
6729  .help = "write memory double-word",
6730  .usage = "['phys'] address value [count]",
6731  },
6732  {
6733  .name = "mww",
6734  .handler = handle_mw_command,
6735  .mode = COMMAND_EXEC,
6736  .help = "write memory word",
6737  .usage = "['phys'] address value [count]",
6738  },
6739  {
6740  .name = "mwh",
6741  .handler = handle_mw_command,
6742  .mode = COMMAND_EXEC,
6743  .help = "write memory half-word",
6744  .usage = "['phys'] address value [count]",
6745  },
6746  {
6747  .name = "mwb",
6748  .handler = handle_mw_command,
6749  .mode = COMMAND_EXEC,
6750  .help = "write memory byte",
6751  .usage = "['phys'] address value [count]",
6752  },
6753  {
6754  .name = "bp",
6755  .handler = handle_bp_command,
6756  .mode = COMMAND_EXEC,
6757  .help = "list or set hardware or software breakpoint",
6758  .usage = "[<address> [<asid>] <length> ['hw'|'hw_ctx']]",
6759  },
6760  {
6761  .name = "rbp",
6762  .handler = handle_rbp_command,
6763  .mode = COMMAND_EXEC,
6764  .help = "remove breakpoint",
6765  .usage = "'all' | address",
6766  },
6767  {
6768  .name = "wp",
6769  .handler = handle_wp_command,
6770  .mode = COMMAND_EXEC,
6771  .help = "list (no params) or create watchpoints",
6772  .usage = "[address length [('r'|'w'|'a') [value [mask]]]]",
6773  },
6774  {
6775  .name = "rwp",
6776  .handler = handle_rwp_command,
6777  .mode = COMMAND_EXEC,
6778  .help = "remove watchpoint",
6779  .usage = "'all' | address",
6780  },
6781  {
6782  .name = "load_image",
6783  .handler = handle_load_image_command,
6784  .mode = COMMAND_EXEC,
6785  .usage = "filename [address ['bin'|'ihex'|'elf'|'s19' "
6786  "[min_address [max_length]]]]",
6787  },
6788  {
6789  .name = "dump_image",
6790  .handler = handle_dump_image_command,
6791  .mode = COMMAND_EXEC,
6792  .usage = "filename address size",
6793  },
6794  {
6795  .name = "verify_image_checksum",
6796  .handler = handle_verify_image_checksum_command,
6797  .mode = COMMAND_EXEC,
6798  .usage = "filename [offset [type]]",
6799  },
6800  {
6801  .name = "verify_image",
6802  .handler = handle_verify_image_command,
6803  .mode = COMMAND_EXEC,
6804  .usage = "filename [offset [type]]",
6805  },
6806  {
6807  .name = "test_image",
6808  .handler = handle_test_image_command,
6809  .mode = COMMAND_EXEC,
6810  .usage = "filename [offset [type]]",
6811  },
6812  {
6813  .name = "get_reg",
6814  .mode = COMMAND_EXEC,
6815  .handler = handle_target_get_reg,
6816  .help = "Get register values from the target",
6817  .usage = "[-force] list",
6818  },
6819  {
6820  .name = "set_reg",
6821  .mode = COMMAND_EXEC,
6822  .handler = handle_set_reg_command,
6823  .help = "Set target register values",
6824  .usage = "dict",
6825  },
6826  {
6827  .name = "read_memory",
6828  .mode = COMMAND_EXEC,
6829  .handler = handle_target_read_memory,
6830  .help = "Read Tcl list of 8/16/32/64 bit numbers from target memory",
6831  .usage = "address width count ['phys']",
6832  },
6833  {
6834  .name = "write_memory",
6835  .mode = COMMAND_EXEC,
6836  .handler = handle_target_write_memory,
6837  .help = "Write Tcl list of 8/16/32/64 bit numbers to target memory",
6838  .usage = "address width data ['phys']",
6839  },
6840  {
6841  .name = "debug_reason",
6842  .mode = COMMAND_EXEC,
6843  .handler = handle_target_debug_reason,
6844  .help = "displays the debug reason of this target",
6845  .usage = "",
6846  },
6847  {
6848  .name = "reset_nag",
6849  .handler = handle_target_reset_nag,
6850  .mode = COMMAND_ANY,
6851  .help = "Nag after each reset about options that could have been "
6852  "enabled to improve performance.",
6853  .usage = "['enable'|'disable']",
6854  },
6855  {
6856  .name = "ps",
6857  .handler = handle_ps_command,
6858  .mode = COMMAND_EXEC,
6859  .help = "list all tasks",
6860  .usage = "",
6861  },
6862  {
6863  .name = "test_mem_access",
6864  .handler = handle_test_mem_access_command,
6865  .mode = COMMAND_EXEC,
6866  .help = "Test the target's memory access functions",
6867  .usage = "size",
6868  },
6869  {
6870  .name = "disassemble",
6871  .mode = COMMAND_EXEC,
6872  .handler = handle_target_disassemble,
6873  .help = "disassemble instructions",
6874  .usage = "list | address [count [instruction_set]]",
6875  },
6876 
6878 };
6880 {
6881  int retval = ERROR_OK;
6882  retval = target_request_register_commands(cmd_ctx);
6883  if (retval != ERROR_OK)
6884  return retval;
6885 
6886  retval = trace_register_commands(cmd_ctx);
6887  if (retval != ERROR_OK)
6888  return retval;
6889 
6890 
6892 }
6893 
6895 {
6896  switch (reason) {
6897  case DBG_REASON_DBGRQ:
6898  return "DBGRQ";
6899  case DBG_REASON_BREAKPOINT:
6900  return "BREAKPOINT";
6901  case DBG_REASON_WATCHPOINT:
6902  return "WATCHPOINT";
6903  case DBG_REASON_WPTANDBKPT:
6904  return "WPTANDBKPT";
6905  case DBG_REASON_SINGLESTEP:
6906  return "SINGLESTEP";
6907  case DBG_REASON_NOTHALTED:
6908  return "NOTHALTED";
6909  case DBG_REASON_EXIT:
6910  return "EXIT";
6911  case DBG_REASON_EXC_CATCH:
6912  return "EXC_CATCH";
6913  case DBG_REASON_UNDEFINED:
6914  return "UNDEFINED";
6915  default:
6916  return "UNKNOWN!";
6917  }
6918 }
struct target_type aarch64_target
Definition: aarch64.c:3297
struct target_type armv8r_target
Definition: aarch64.c:3340
#define IS_ALIGNED(x, a)
Definition: align.h:22
#define IS_PWR_OF_2(x)
Definition: align.h:24
#define ALIGN_DOWN(x, a)
Definition: align.h:21
#define ALIGN_UP(x, a)
Definition: align.h:20
struct target_type arcv2_target
Definition: arc.c:2319
struct target_type arm11_target
Holds methods for ARM11xx targets.
Definition: arm11.c:1339
struct target_type arm720t_target
Holds methods for ARM720 targets.
Definition: arm720t.c:459
struct target_type arm7tdmi_target
Holds methods for ARM7TDMI targets.
Definition: arm7tdmi.c:684
struct target_type arm920t_target
Holds methods for ARM920 targets.
Definition: arm920t.c:1586
struct target_type arm926ejs_target
Holds methods for ARM926 targets.
Definition: arm926ejs.c:786
struct target_type arm946e_target
Holds methods for ARM946 targets.
Definition: arm946e.c:738
struct target_type arm966e_target
Holds methods for ARM966 targets.
Definition: arm966e.c:245
struct target_type arm9tdmi_target
Holds methods for ARM9TDMI targets.
Definition: arm9tdmi.c:888
const char * name
Definition: armv4_5.c:75
struct target_type avr32_ap7k_target
Definition: avr32_ap7k.c:577
struct target_type avr_target
Definition: avrt.c:39
char * buf_to_hex_str(const void *_buf, unsigned int buf_len)
Definition: binarybuffer.c:178
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
Definition: binarybuffer.h:104
int watchpoint_add(struct target *target, target_addr_t address, unsigned int length, enum watchpoint_rw rw, uint64_t value, uint64_t mask)
Definition: breakpoints.c:551
int breakpoint_remove(struct target *target, target_addr_t address)
Definition: breakpoints.c:346
int watchpoint_remove(struct target *target, target_addr_t address)
Definition: breakpoints.c:588
int breakpoint_add(struct target *target, target_addr_t address, unsigned int length, enum breakpoint_type type)
Definition: breakpoints.c:216
int context_breakpoint_add(struct target *target, uint32_t asid, unsigned int length, enum breakpoint_type type)
Definition: breakpoints.c:236
int watchpoint_remove_all(struct target *target)
Definition: breakpoints.c:467
int breakpoint_remove_all(struct target *target)
Definition: breakpoints.c:462
int hybrid_breakpoint_add(struct target *target, target_addr_t address, uint32_t asid, unsigned int length, enum breakpoint_type type)
Definition: breakpoints.c:257
@ BKPT_HARD
Definition: breakpoints.h:18
@ BKPT_SOFT
Definition: breakpoints.h:19
#define WATCHPOINT_IGNORE_DATA_VALUE_MASK
Definition: breakpoints.h:39
watchpoint_rw
Definition: breakpoints.h:22
@ WPT_ACCESS
Definition: breakpoints.h:23
@ WPT_READ
Definition: breakpoints.h:23
@ WPT_WRITE
Definition: breakpoints.h:23
struct command_context * current_command_context(Jim_Interp *interp)
Definition: command.c:86
int command_parse_bool_arg(const char *in, bool *out)
Definition: command.c:1306
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
Definition: command.c:378
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:389
int command_run_line(struct command_context *context, char *line)
Definition: command.c:497
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:146
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:123
#define CMD_NAME
Use this macro to access the name of the command being handled, rather than accessing the variable di...
Definition: command.h:171
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:161
#define COMMAND_PARSE_ADDRESS(in, out)
Definition: command.h:455
#define COMMAND_PARSE_ON_OFF(in, out)
parses an on/off command argument
Definition: command.h:533
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:405
static int register_commands_override_target(struct command_context *cmd_ctx, const char *cmd_prefix, const struct command_registration *cmds, struct target *target)
Register one or more commands, as register_commands(), plus specify that command should override the ...
Definition: command.h:296
#define ERROR_COMMAND_CLOSE_CONNECTION
Definition: command.h:404
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:156
#define CMD_JIMTCL_ARGV
Use this macro to access the jimtcl arguments for the command being handled, rather than accessing th...
Definition: command.h:166
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:445
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:151
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:407
static int register_commands(struct command_context *cmd_ctx, const char *cmd_prefix, const struct command_registration *cmds)
Register one or more commands in the specified context, as children of parent (or top-level commends,...
Definition: command.h:277
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
struct target_type cortexr4_target
Definition: cortex_a.c:3549
struct target_type cortexa_target
Definition: cortex_a.c:3467
struct target_type cortexm_target
Definition: cortex_m.c:3468
struct target_type dsp563xx_target
Holds methods for DSP563XX targets.
Definition: dsp563xx.c:2243
struct target_type dsp5680xx_target
Holds methods for dsp5680xx targets.
Definition: dsp5680xx.c:2269
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
uint32_t buffer_size
Size of dw_spi_program::buffer.
Definition: dw-spi-helper.h:5
uint32_t address
Starting address. Sector aligned.
Definition: dw-spi-helper.h:0
unsigned short width
Definition: embeddedice.c:47
struct target_type esirisc_target
Definition: esirisc.c:1832
struct target_type esp32_target
Holds methods for Xtensa targets.
Definition: esp32.c:468
struct target_type esp32s2_target
Definition: esp32s2.c:505
struct target_type esp32s3_target
Holds methods for Xtensa targets.
Definition: esp32s3.c:389
uint8_t type
Definition: esp_usb_jtag.c:0
static struct esp_usb_jtag * priv
Definition: esp_usb_jtag.c:219
uint8_t length
Definition: esp_usb_jtag.c:1
struct target_type fa526_target
Holds methods for FA526 targets.
Definition: fa526.c:350
struct target_type dragonite_target
Definition: feroceon.c:732
struct target_type feroceon_target
Definition: feroceon.c:691
#define ERROR_FLASH_OPERATION_FAILED
Definition: flash/common.h:30
static uint16_t output
Definition: ftdi.c:156
int fileio_write(struct fileio *fileio, size_t size, const void *buffer, size_t *size_written)
int fileio_close(struct fileio *fileio)
int fileio_size(struct fileio *fileio, size_t *size)
FIX!!!!
int fileio_open(struct fileio **fileio, const char *url, enum fileio_access access_type, enum fileio_type type)
@ FILEIO_WRITE
Definition: helper/fileio.h:29
@ FILEIO_BINARY
Definition: helper/fileio.h:23
struct target_type hla_target
Definition: hla_target.c:645
void image_close(struct image *image)
Definition: image.c:1210
int image_read_section(struct image *image, int section, target_addr_t offset, uint32_t size, uint8_t *buffer, size_t *size_read)
Definition: image.c:1078
int image_calculate_checksum(const uint8_t *buffer, uint32_t nbytes, uint32_t *checksum)
Definition: image.c:1267
int image_open(struct image *image, const char *url, const char *type_string)
Definition: image.c:956
int jim_getopt_setup(struct jim_getopt_info *p, Jim_Interp *interp, int argc, Jim_Obj *const *argv)
GetOpt - how to.
Definition: jim-nvp.c:149
int jtag_unregister_event_callback(jtag_event_handler_t callback, void *priv)
Definition: jtag/core.c:290
void jtag_poll_unmask(bool saved)
Restore saved mask for polling.
Definition: jtag/core.c:178
struct jtag_tap * jtag_tap_by_string(const char *s)
Definition: jtag/core.c:238
void jtag_poll_set_enabled(bool value)
Assign flag reporting whether JTAG polling is disallowed.
Definition: jtag/core.c:166
int jtag_srst_asserted(int *srst_asserted)
Definition: jtag/core.c:1721
bool is_jtag_poll_safe(void)
Return true if it's safe for a background polling task to access the JTAG scan chain.
Definition: jtag/core.c:146
int jtag_power_dropout(int *dropout)
Definition: jtag/core.c:1706
int jtag_register_event_callback(jtag_event_handler_t callback, void *priv)
Definition: jtag/core.c:269
bool jtag_poll_get_enabled(void)
Return flag reporting whether JTAG polling is disallowed.
Definition: jtag/core.c:161
bool jtag_poll_mask(void)
Mask (disable) polling and return the current mask status that should be feed to jtag_poll_unmask() t...
Definition: jtag/core.c:171
The JTAG interface can be implemented with a software or hardware fifo.
jtag_event
Definition: jtag.h:179
@ JTAG_TAP_EVENT_ENABLE
Definition: jtag.h:182
static void list_add(struct list_head *new, struct list_head *head)
Definition: list.h:197
static void list_add_tail(struct list_head *new, struct list_head *head)
Definition: list.h:203
#define list_for_each_entry_safe(p, n, h, field)
Definition: list.h:159
#define list_for_each_entry(p, h, field)
Definition: list.h:155
static void list_del(struct list_head *entry)
Definition: list.h:88
static void INIT_LIST_HEAD(struct list_head *list)
Definition: list.h:54
void alive_sleep(uint64_t ms)
Definition: log.c:478
void keep_alive(void)
Definition: log.c:437
char * alloc_printf(const char *format,...)
Definition: log.c:386
#define LOG_TARGET_INFO(target, fmt_str,...)
Definition: log.h:167
#define LOG_TARGET_WARNING(target, fmt_str,...)
Definition: log.h:173
#define ERROR_NOT_IMPLEMENTED
Definition: log.h:192
#define LOG_WARNING(expr ...)
Definition: log.h:144
#define ERROR_FAIL
Definition: log.h:188
#define LOG_TARGET_ERROR(target, fmt_str,...)
Definition: log.h:176
#define LOG_TARGET_DEBUG(target, fmt_str,...)
Definition: log.h:164
#define LOG_TARGET_DEBUG_IO(target, fmt_str,...)
Definition: log.h:161
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_INFO(expr ...)
Definition: log.h:141
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
struct target_type ls1_sap_target
Definition: ls1_sap.c:216
struct target_type mem_ap_target
Definition: mem_ap.c:264
#define zero
Definition: mips32.c:181
struct target_type mips_m4k_target
Definition: mips_m4k.c:1451
struct target_type mips_mips64_target
Definition: mips_mips64.c:1151
Upper level NOR flash interfaces.
void nvp_unknown_command_print(struct command_invocation *cmd, const struct nvp *nvp, const char *param_name, const char *param_value)
Definition: nvp.c:49
const struct nvp * nvp_name2value(const struct nvp *p, const char *name)
Definition: nvp.c:29
const struct nvp * nvp_value2name(const struct nvp *p, int value)
Definition: nvp.c:39
int oocd_cs_disassemble(struct command_invocation *cmd, struct target *target, uint64_t address, unsigned int count, const char *insn_set)
Definition: oocd_capstone.c:77
int oocd_cs_list_insn_types(struct command_invocation *cmd)
Definition: oocd_capstone.c:56
static uint32_t lh(unsigned int rd, unsigned int base, int16_t offset) __attribute__((unused))
Definition: opcodes.h:172
struct target_type or1k_target
Definition: or1k.c:1411
uint8_t bits[QN908X_FLASH_MAX_BLOCKS *QN908X_FLASH_PAGES_PER_BLOCK/8]
Definition: qn908x.c:0
struct target_type quark_d20xx_target
Definition: quark_d20xx.c:79
struct target_type quark_x10xx_target
Definition: quark_x10xx.c:57
struct reg * register_get_by_name(struct reg_cache *first, const char *name, bool search_all)
Definition: register.c:50
#define MIN(a, b)
Definition: replacements.h:22
#define MAX(a, b)
Definition: replacements.h:25
struct target_type riscv_target
Definition: riscv.c:5977
int rtos_create(struct command_invocation *cmd, struct target *target, const char *rtos_name)
Definition: rtos.c:100
void rtos_destroy(struct target *target)
Definition: rtos.c:145
int rtos_smp_init(struct target *target)
Definition: rtos.c:40
target_addr_t addr
Start address to search for the control block.
Definition: rtt/rtt.c:28
struct target * target
Definition: rtt/rtt.c:26
bool openocd_is_shutdown_pending(void)
Definition: server.c:792
#define CONNECTION_LIMIT_UNLIMITED
Definition: server.h:34
#define ERROR_SERVER_INTERRUPTED
Definition: server.h:134
#define foreach_smp_target(pos, head)
Definition: smp.h:15
struct target_type stm8_target
Definition: stm8.c:2197
int64_t next_attempt
Definition: target.h:108
unsigned int interval
Definition: target.h:109
struct breakpoint * next
Definition: breakpoints.h:34
unsigned int length
Definition: breakpoints.h:29
uint8_t * orig_instr
Definition: breakpoints.h:33
enum breakpoint_type type
Definition: breakpoints.h:30
unsigned int number
Definition: breakpoints.h:32
uint32_t asid
Definition: breakpoints.h:28
target_addr_t address
Definition: breakpoints.h:27
Jim_Interp * interp
Definition: command.h:53
struct target * current_target_override
Definition: command.h:57
struct target * current_target
Definition: command.h:55
When run_command is called, a new instance will be created on the stack, filled with the proper value...
Definition: command.h:76
const char * name
Definition: command.h:239
const struct command_registration * chain
If non-NULL, the commands in chain will be registered in the same context and scope of this registrat...
Definition: command.h:252
const char * usage
a string listing the options and arguments, required or optional
Definition: command.h:244
int length
Definition: target.c:6200
uint8_t * data
Definition: target.c:6199
target_addr_t address
Definition: target.c:6198
Definition: image.h:48
unsigned int num_sections
Definition: image.h:51
bool start_address_set
Definition: image.h:55
struct imagesection * sections
Definition: image.h:52
long long base_address
Definition: image.h:54
bool base_address_set
Definition: image.h:53
target_addr_t base_address
Definition: image.h:42
uint32_t size
Definition: image.h:43
A TCL -ish GetOpt like code.
Definition: jim-nvp.h:136
bool is_configure
Definition: jim-nvp.h:140
Definition: jtag.h:101
bool enabled
Is this TAP currently enabled?
Definition: jtag.h:109
char * dotted_name
Definition: jtag.h:104
Definition: list.h:41
Name Value Pairs, aka: NVP.
Definition: nvp.h:61
int value
Definition: nvp.h:63
const char * name
Definition: nvp.h:62
int(* get)(struct reg *reg)
Definition: register.h:152
int(* set)(struct reg *reg, uint8_t *buf)
Definition: register.h:153
const char * name
Definition: register.h:145
unsigned int num_regs
Definition: register.h:148
struct reg * reg_list
Definition: register.h:147
struct reg_cache * next
Definition: register.h:146
Definition: register.h:111
bool valid
Definition: register.h:126
bool exist
Definition: register.h:128
uint32_t size
Definition: register.h:132
uint8_t * value
Definition: register.h:122
bool hidden
Definition: register.h:130
bool dirty
Definition: register.h:124
const struct reg_arch_type * type
Definition: register.h:141
const char * name
Definition: register.h:113
const char * name
Definition: rtos.h:60
char *(* ps_command)(struct target *target)
Definition: rtos.h:74
Definition: rtos.h:36
const struct rtos_type * type
Definition: rtos.h:37
char * basedir
Base directory for semihosting I/O operations.
Jim_Interp * interp
Definition: target.c:60
Jim_Obj * body
Definition: target.c:61
struct list_head list
Definition: target.c:62
enum target_event event
Definition: target.c:59
int(* callback)(struct target *target, enum target_event event, void *priv)
Definition: target.h:315
struct target_event_callback * next
Definition: target.h:317
struct list_head lh
Definition: target.h:227
struct target * target
Definition: target.h:228
int(* callback)(struct target *target, enum target_reset_mode reset_mode, void *priv)
Definition: target.h:323
struct list_head list
Definition: target.h:321
int(* callback)(void *priv)
Definition: target.h:338
struct target_timer_callback * next
Definition: target.h:344
unsigned int time_ms
Definition: target.h:339
enum target_timer_type type
Definition: target.h:340
int(* callback)(struct target *target, size_t len, uint8_t *data, void *priv)
Definition: target.h:329
struct list_head list
Definition: target.h:327
This holds methods shared between all instances of a given target type.
Definition: target_type.h:27
int(* add_context_breakpoint)(struct target *target, struct breakpoint *breakpoint)
Definition: target_type.h:161
int(* add_breakpoint)(struct target *target, struct breakpoint *breakpoint)
Definition: target_type.h:160
int(* write_memory)(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Target memory write callback.
Definition: target_type.h:131
int(* hit_watchpoint)(struct target *target, struct watchpoint **hit_watchpoint)
Definition: target_type.h:182
const char * name
Name of this type of target.
Definition: target_type.h:32
int(* deassert_reset)(struct target *target)
The implementation is responsible for polling the target such that target->state reflects the state c...
Definition: target_type.h:77
int(* get_gdb_reg_list)(struct target *target, struct reg **reg_list[], int *reg_list_size, enum target_register_class reg_class)
Target register access for GDB.
Definition: target_type.h:100
int(* insn_set)(struct command_invocation *cmd, struct target *target, const char **insn_set)
Definition: target_type.h:324
int(* resume)(struct target *target, bool current, target_addr_t address, bool handle_breakpoints, bool debug_execution)
Definition: target_type.h:46
int(* blank_check_memory)(struct target *target, struct target_memory_check_block *blocks, unsigned int num_blocks, uint8_t erased_value, unsigned int *checked)
Definition: target_type.h:144
void(* deinit_target)(struct target *target)
Free all the resources allocated by the target.
Definition: target_type.h:250
int(* halt)(struct target *target)
Definition: target_type.h:44
int(* check_reset)(struct target *target)
Definition: target_type.h:282
int(* gdb_fileio_end)(struct target *target, int retcode, int fileio_errno, bool ctrl_c)
Definition: target_type.h:290
int(* assert_reset)(struct target *target)
Definition: target_type.h:65
int(* run_algorithm)(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, target_addr_t entry_point, target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
Target algorithm support.
Definition: target_type.h:188
int(* wait_algorithm)(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
Definition: target_type.h:196
const struct command_registration * commands
Definition: target_type.h:201
int(* profiling)(struct target *target, uint32_t *samples, uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
Definition: target_type.h:303
int(* soft_reset_halt)(struct target *target)
Definition: target_type.h:78
const char *(* get_gdb_arch)(const struct target *target)
Target architecture for GDB.
Definition: target_type.h:87
int(* arch_state)(struct target *target)
Definition: target_type.h:38
unsigned int(* address_bits)(struct target *target)
Definition: target_type.h:309
int(* read_memory)(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Target memory read callback.
Definition: target_type.h:125
int(* get_gdb_fileio_info)(struct target *target, struct gdb_fileio_info *fileio_info)
Definition: target_type.h:286
unsigned int(* data_bits)(struct target *target)
Definition: target_type.h:314
int(* target_jim_configure)(struct target *target, struct jim_getopt_info *goi)
Definition: target_type.h:209
int(* step)(struct target *target, bool current, target_addr_t address, bool handle_breakpoints)
Definition: target_type.h:48
int(* read_phys_memory)(struct target *target, target_addr_t phys_address, uint32_t size, uint32_t count, uint8_t *buffer)
Definition: target_type.h:265
int(* get_gdb_reg_list_noread)(struct target *target, struct reg **reg_list[], int *reg_list_size, enum target_register_class reg_class)
Same as get_gdb_reg_list, but doesn't read the register values.
Definition: target_type.h:106
int(* start_algorithm)(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, target_addr_t entry_point, target_addr_t exit_point, void *arch_info)
Definition: target_type.h:192
int(* read_buffer)(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: target_type.h:135
int(* add_watchpoint)(struct target *target, struct watchpoint *watchpoint)
Definition: target_type.h:171
int(* target_create)(struct target *target)
Definition: target_type.h:204
bool(* memory_ready)(struct target *target)
Returns true if target memory is read to read/write.
Definition: target_type.h:120
int(* write_buffer)(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: target_type.h:139
int(* poll)(struct target *target)
Definition: target_type.h:35
int(* mmu)(struct target *target, bool *enabled)
Definition: target_type.h:274
int(* add_hybrid_breakpoint)(struct target *target, struct breakpoint *breakpoint)
Definition: target_type.h:162
int(* examine)(struct target *target)
This method is used to perform target setup that requires JTAG access.
Definition: target_type.h:225
int(* write_phys_memory)(struct target *target, target_addr_t phys_address, uint32_t size, uint32_t count, const uint8_t *buffer)
Definition: target_type.h:271
int(* remove_breakpoint)(struct target *target, struct breakpoint *breakpoint)
Definition: target_type.h:168
int(* virt2phys)(struct target *target, target_addr_t address, target_addr_t *physical)
Definition: target_type.h:255
int(* checksum_memory)(struct target *target, target_addr_t address, uint32_t count, uint32_t *checksum)
Definition: target_type.h:142
int(* remove_watchpoint)(struct target *target, struct watchpoint *watchpoint)
Definition: target_type.h:177
Definition: target.h:119
int32_t coreid
Definition: target.h:123
struct semihosting * semihosting
Definition: target.h:223
target_addr_t working_area
Definition: target.h:155
target_addr_t working_area_virt
Definition: target.h:158
uint32_t working_area_size
Definition: target.h:161
struct jtag_tap * tap
Definition: target.h:122
bool dbgbase_set
Definition: target.h:184
struct trace * trace_info
Definition: target.h:171
bool dbg_msg_enabled
Definition: target.h:173
enum target_debug_reason debug_reason
Definition: target.h:164
enum target_state state
Definition: target.h:167
bool active_polled
The flag is set after a successful examine call.
Definition: target.h:141
uint32_t dbgbase
Definition: target.h:185
void * private_config
Definition: target.h:175
char * gdb_port_override
Definition: target.h:218
enum target_endianness endianness
Definition: target.h:165
struct reg_cache * reg_cache
Definition: target.h:168
bool backup_working_area
Definition: target.h:162
bool halt_issued
Definition: target.h:180
struct list_head * smp_targets
Definition: target.h:202
struct breakpoint * breakpoints
Definition: target.h:169
bool smp
Definition: target.h:200
struct working_area * working_areas
Definition: target.h:163
bool verbose_halt_msg
Definition: target.h:178
bool dap_configured
Definition: target.h:189
struct rtos * rtos
Definition: target.h:193
struct gdb_fileio_info * fileio_info
Definition: target.h:216
struct debug_msg_receiver * dbgmsg
Definition: target.h:172
bool rtos_auto_detect
Definition: target.h:194
int64_t halt_issued_time
Definition: target.h:181
struct list_head events_action
Definition: target.h:152
unsigned int smp_id
Definition: target.h:201
struct target_type * type
Definition: target.h:120
struct backoff_timer backoff
Definition: target.h:199
target_addr_t working_area_phys
Definition: target.h:160
bool has_dap
Definition: target.h:188
bool tap_configured
Definition: target.h:190
struct watchpoint * watchpoints
Definition: target.h:170
bool working_area_phys_spec
Definition: target.h:159
bool running_alg
true if the target is currently running a downloaded "algorithm" instead of arbitrary user code.
Definition: target.h:150
void * arch_info
Definition: target.h:174
int gdb_max_connections
Definition: target.h:220
bool working_area_virt_spec
Definition: target.h:157
bool reset_halt
Definition: target.h:154
bool examined
Indicates whether this target has been examined, remembers the last result of examine call.
Definition: target.h:135
char * cmd_name
Definition: target.h:121
bool defer_examine
Should we defer examine to later.
Definition: target.h:126
struct target * next
Definition: target.h:176
Definition: psoc6.c:83
Definition: trace.h:21
Wrapper for transport lifecycle operations.
Definition: transport.h:55
int(* override_target)(const char **targetname)
Optional.
Definition: transport.h:85
uint64_t mask
Definition: breakpoints.h:44
enum watchpoint_rw rw
Definition: breakpoints.h:46
struct watchpoint * next
Definition: breakpoints.h:49
unsigned int length
Definition: breakpoints.h:43
uint64_t value
Definition: breakpoints.h:45
target_addr_t address
Definition: breakpoints.h:42
uint32_t size
Definition: target.h:90
bool free
Definition: target.h:91
struct working_area * next
Definition: target.h:94
target_addr_t address
Definition: target.h:89
struct working_area ** user
Definition: target.h:93
uint8_t * backup
Definition: target.h:92
COMMAND_HANDLER(handle_target_init_command)
Definition: target.c:1593
static bool target_reset_nag
Definition: target.c:6379
void target_buffer_get_u16_array(struct target *target, const uint8_t *buffer, uint32_t count, uint16_t *dstbuf)
Definition: target.c:412
void target_buffer_set_u24(struct target *target, uint8_t *buffer, uint32_t value)
Definition: target.c:372
int target_get_gdb_fileio_info(struct target *target, struct gdb_fileio_info *fileio_info)
Obtain file-I/O information from target for GDB to do syscall.
Definition: target.c:1456
unsigned char UNIT[2]
Definition: target.c:4230
static int run_srst_deasserted
Definition: target.c:2860
int target_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
Add the watchpoint for target.
Definition: target.c:1353
static int target_call_timer_callback(struct target_timer_callback *cb, int64_t *now)
Definition: target.c:1871
static int no_mmu(struct target *target, bool *enabled)
Definition: target.c:654
struct target * all_targets
Definition: target.c:117
static int target_get_gdb_fileio_info_default(struct target *target, struct gdb_fileio_info *fileio_info)
Definition: target.c:2332
int target_run_read_async_algorithm(struct target *target, uint8_t *buffer, uint32_t count, int block_size, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_params, uint32_t buffer_start, uint32_t buffer_size, uint32_t entry_point, uint32_t exit_point, void *arch_info)
This routine is a wrapper for asynchronous algorithms.
Definition: target.c:1099
int target_write_phys_u32(struct target *target, target_addr_t address, uint32_t value)
Definition: target.c:2741
static int compare_pc32(const void *p1, const void *p2)
Definition: target.c:4319
static int sensed_power_dropout
Definition: target.c:2854
uint64_t target_buffer_get_u64(struct target *target, const uint8_t *buffer)
Definition: target.c:318
int target_call_event_callbacks(struct target *target, enum target_event event)
Definition: target.c:1816
struct target * get_target(const char *id)
Definition: target.c:444
void target_free_all_working_areas(struct target *target)
Definition: target.c:2206
int target_unregister_reset_callback(int(*callback)(struct target *target, enum target_reset_mode reset_mode, void *priv), void *priv)
Definition: target.c:1749
int target_write_phys_u64(struct target *target, target_addr_t address, uint64_t value)
Definition: target.c:2724
static const struct nvp nvp_target_event[]
Definition: target.c:167
static OOCD_LIST_HEAD(target_reset_callback_list)
static int target_write_buffer_default(struct target *target, target_addr_t address, uint32_t count, const uint8_t *buffer)
Definition: target.c:2434
int target_unregister_event_callback(int(*callback)(struct target *target, enum target_event event, void *priv), void *priv)
Definition: target.c:1726
static void write_long(FILE *f, int l, struct target *target)
Definition: target.c:4217
int target_read_phys_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Definition: target.c:1275
static void target_free_all_working_areas_restore(struct target *target, bool restore)
Definition: target.c:2182
static void binprint(struct command_invocation *cmd, const char *text, const uint8_t *buf, int size)
Definition: target.c:6414
int target_register_event_callback(int(*callback)(struct target *target, enum target_event event, void *priv), void *priv)
Definition: target.c:1623
static const struct command_registration target_command_handlers[]
Definition: target.c:6348
int target_write_phys_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Definition: target.c:1303
static int run_power_restore
Definition: target.c:2857
int target_halt(struct target *target)
Definition: target.c:518
static struct target_timer_callback * target_timer_callbacks
Definition: target.c:119
int target_get_gdb_reg_list_noread(struct target *target, struct reg **reg_list[], int *reg_list_size, enum target_register_class reg_class)
Obtain the registers for GDB, but don't read register values from the target.
Definition: target.c:1415
static int target_timer_callback_set_period(struct target_timer_callback *cb, unsigned int time_ms)
Definition: target.c:1689
bool target_supports_gdb_connection(const struct target *target)
Check if target allows GDB connections.
Definition: target.c:1426
int target_arch_state(struct target *target)
Definition: target.c:2319
void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
Definition: target.c:381
int target_call_timer_callbacks_now(void)
Invoke this to ensure that e.g.
Definition: target.c:1936
int target_remove_breakpoint(struct target *target, struct breakpoint *breakpoint)
Remove the breakpoint for target.
Definition: target.c:1347
static int target_profiling(struct target *target, uint32_t *samples, uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
Definition: target.c:1497
void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
Definition: target.c:363
int target_register_commands(struct command_context *cmd_ctx)
Definition: target.c:6374
static const struct nvp nvp_target_debug_reason[]
Definition: target.c:229
static int target_read_buffer_default(struct target *target, target_addr_t address, uint32_t count, uint8_t *buffer)
Definition: target.c:2499
int target_checksum_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t *crc)
Definition: target.c:2535
static void target_merge_working_areas(struct target *target)
Definition: target.c:1990
static const struct nvp nvp_target_state[]
Definition: target.c:219
static unsigned int polling_interval
Definition: target.c:123
static int handle_bp_command_list(struct command_invocation *cmd)
Definition: target.c:3942
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: target.c:2410
static int get_target_with_common_rtos_type(struct command_invocation *cmd, struct list_head *lh, struct target **result)
Definition: target.c:6080
int target_add_hybrid_breakpoint(struct target *target, struct breakpoint *breakpoint)
Add the ContextID & IVA breakpoint for target.
Definition: target.c:1337
static int default_examine(struct target *target)
Definition: target.c:673
int target_write_u8(struct target *target, target_addr_t address, uint8_t value)
Definition: target.c:2710
int target_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
Add the breakpoint for target.
Definition: target.c:1317
target_addr_t target_address_max(struct target *target)
Return the highest accessible address for this target.
Definition: target.c:1474
int target_write_u16(struct target *target, target_addr_t address, uint16_t value)
Definition: target.c:2693
static void free_smp_target_list(struct list_head *smp_targets)
Definition: target.c:2238
int target_unregister_timer_callback(int(*callback)(void *priv), void *priv)
Definition: target.c:1802
int target_gdb_fileio_end(struct target *target, int retcode, int fileio_errno, bool ctrl_c)
Pass GDB file-I/O response to target after finishing host syscall.
Definition: target.c:1465
int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: target.c:2475
int target_unregister_trace_callback(int(*callback)(struct target *target, size_t len, uint8_t *data, void *priv), void *priv)
Definition: target.c:1768
int target_read_u8(struct target *target, target_addr_t address, uint8_t *value)
Definition: target.c:2642
static void write_string(FILE *f, char *s)
Definition: target.c:4225
int target_run_algorithm(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, target_addr_t entry_point, target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
Downloads a target-specific native code algorithm to the target, and executes it.
Definition: target.c:787
static void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
Definition: target.c:390
unsigned int target_address_bits(struct target *target)
Return the number of address bits this target supports.
Definition: target.c:1483
static struct target_list * __attribute__((warn_unused_result))
Definition: target.c:6062
int target_profiling_default(struct target *target, uint32_t *samples, uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
Definition: target.c:2348
static const struct command_registration target_subcommand_handlers[]
Definition: target.c:6149
int target_write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Write count items of size bytes to the memory of target at the address given.
Definition: target.c:1289
static struct target_timer_callback * target_find_timer_callback(int(*callback)(void *priv), void *priv)
Definition: target.c:1787
static int jtag_enable_callback(enum jtag_event event, void *priv)
Definition: target.c:708
int target_get_gdb_reg_list(struct target *target, struct reg **reg_list[], int *reg_list_size, enum target_register_class reg_class)
Obtain the registers for GDB.
Definition: target.c:1392
static int target_insn_set(struct command_invocation *cmd, struct target *target, const char **insn_set)
Definition: target.c:2392
static const struct nvp nvp_error_target[]
Definition: target.c:141
int target_call_timer_callbacks(void)
Definition: target.c:1930
int target_write_u64(struct target *target, target_addr_t address, uint64_t value)
Definition: target.c:2659
static struct target_event_callback * target_event_callbacks
Definition: target.c:118
static COMMAND_HELPER(parse_load_image_command, struct image *image, target_addr_t *min_address, target_addr_t *max_address)
Definition: target.c:3604
struct target * get_current_target_or_null(struct command_context *cmd_ctx)
Definition: target.c:481
static void target_split_working_area(struct working_area *area, uint32_t size)
Definition: target.c:1960
const char * target_debug_reason_str(enum target_debug_reason reason)
Definition: target.c:6894
static int target_init(struct command_context *cmd_ctx)
Definition: target.c:1567
int target_hit_watchpoint(struct target *target, struct watchpoint **hit_watchpoint)
Find out the just hit watchpoint for target.
Definition: target.c:1367
int target_call_reset_callbacks(struct target *target, enum target_reset_mode reset_mode)
Definition: target.c:1841
uint32_t target_get_working_area_avail(struct target *target)
Definition: target.c:2220
target_cfg_param
Definition: target.c:4896
@ TCFG_GDB_MAX_CONNECTIONS
Definition: target.c:4911
@ TCFG_CHAIN_POSITION
Definition: target.c:4905
@ TCFG_GDB_PORT
Definition: target.c:4910
@ TCFG_WORK_AREA_VIRT
Definition: target.c:4899
@ TCFG_TYPE
Definition: target.c:4897
@ TCFG_WORK_AREA_BACKUP
Definition: target.c:4902
@ TCFG_RTOS
Definition: target.c:4908
@ TCFG_DBGBASE
Definition: target.c:4907
@ TCFG_WORK_AREA_PHYS
Definition: target.c:4900
@ TCFG_TAP
Definition: target.c:4906
@ TCFG_ENDIAN
Definition: target.c:4903
@ TCFG_WORK_AREA_SIZE
Definition: target.c:4901
@ TCFG_EVENT
Definition: target.c:4898
@ TCFG_DEFER_EXAMINE
Definition: target.c:4909
@ TCFG_COREID
Definition: target.c:4904
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2118
static void write_gmon_hist(FILE *f, const uint32_t *samples, uint32_t sample_num, float sample_rate, struct target *target)
Definition: target.c:4232
static const struct command_registration target_instance_command_handlers[]
Definition: target.c:5611
bool get_target_reset_nag(void)
Definition: target.c:6381
unsigned int target_data_bits(struct target *target)
Return the number of data bits this target supports.
Definition: target.c:1490
static int find_target(struct command_invocation *cmd, const char *name)
Definition: target.c:2789
int target_remove_watchpoint(struct target *target, struct watchpoint *watchpoint)
Remove the watchpoint for target.
Definition: target.c:1362
const char * target_event_name(enum target_event event)
Return the name of a target event enumeration value.
Definition: target.c:286
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
Definition: target.c:2676
int target_examine_one(struct target *target)
Examine the specified target, letting it perform any Initialisation that requires JTAG access.
Definition: target.c:686
bool target_memory_ready(struct target *target)
Returns true if target memory is ready to read/write.
Definition: target.c:1253
const char * target_state_name(const struct target *t)
Return the name of this targets current state.
Definition: target.c:271
int(* target_write_fn)(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Definition: target.c:3490
static void print_wa_layout(struct target *target)
Definition: target.c:1947
#define DEFAULT_HALT_TIMEOUT
Definition: target.c:56
static int handle_one_target(struct target *target)
Definition: target.c:2914
int target_poll(struct target *target)
Definition: target.c:488
static int target_call_timer_callbacks_check_time(int checktime)
Definition: target.c:1882
static int sense_handler(void)
Definition: target.c:2862
static int target_timer_callback_periodic_restart(struct target_timer_callback *cb, int64_t *now)
Definition: target.c:1864
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
Definition: target.c:2174
static int fastload_num
Definition: target.c:6204
static const struct nvp nvp_target_endian[]
Definition: target.c:242
int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2018
static int sensed_srst_asserted
Definition: target.c:2855
int target_register_timer_callback(int(*callback)(void *priv), unsigned int time_ms, enum target_timer_type type, void *priv)
The period is very approximate, the callback can happen much more often or much more rarely than spec...
Definition: target.c:1701
static struct nvp nvp_config_opts[]
Definition: target.c:4914
int target_read_u16(struct target *target, target_addr_t address, uint16_t *value)
Definition: target.c:2622
int target_run_flash_async_algorithm(struct target *target, const uint8_t *buffer, uint32_t count, int block_size, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_params, uint32_t buffer_start, uint32_t buffer_size, uint32_t entry_point, uint32_t exit_point, void *arch_info)
Streams data to a circular buffer on target intended for consumption by code running asynchronously o...
Definition: target.c:946
void target_buffer_set_u64(struct target *target, uint8_t *buffer, uint64_t value)
Definition: target.c:354
void target_buffer_set_u64_array(struct target *target, uint8_t *buffer, uint32_t count, const uint64_t *srcbuf)
Definition: target.c:420
static void target_reset_examined(struct target *target)
Reset the examined flag for the given target.
Definition: target.c:663
int target_add_context_breakpoint(struct target *target, struct breakpoint *breakpoint)
Add the ContextID breakpoint for target.
Definition: target.c:1327
static int target_init_one(struct command_context *cmd_ctx, struct target *target)
Definition: target.c:1506
static int run_power_dropout
Definition: target.c:2858
int target_resume(struct target *target, bool current, target_addr_t address, bool handle_breakpoints, bool debug_execution)
Make the target (re)start executing using its saved execution context (possibly with some modificatio...
Definition: target.c:567
int target_blank_check_memory(struct target *target, struct target_memory_check_block *blocks, unsigned int num_blocks, uint8_t erased_value, unsigned int *checked)
Definition: target.c:2566
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
Definition: target.c:2602
uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
Definition: target.c:345
int target_read_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Read count items of size bytes from the memory of target at the address given.
Definition: target.c:1261
bool target_has_event_action(const struct target *target, enum target_event event)
Returns true only if the target has a handler for the specified event.
Definition: target.c:4885
static const struct command_registration target_exec_command_handlers[]
Definition: target.c:6604
void target_buffer_get_u64_array(struct target *target, const uint8_t *buffer, uint32_t count, uint64_t *dstbuf)
Definition: target.c:396
static const struct nvp nvp_reset_modes[]
Definition: target.c:250
const char * debug_reason_name(const struct target *t)
Definition: target.c:258
static int default_check_reset(struct target *target)
Definition: target.c:679
void target_buffer_set_u16_array(struct target *target, uint8_t *buffer, uint32_t count, const uint16_t *srcbuf)
Definition: target.c:436
static struct fast_load * fastload
Definition: target.c:6205
int target_register_reset_callback(int(*callback)(struct target *target, enum target_reset_mode reset_mode, void *priv), void *priv)
Definition: target.c:1645
uint32_t target_buffer_get_u24(struct target *target, const uint8_t *buffer)
Definition: target.c:336
static int run_srst_asserted
Definition: target.c:2859
void target_buffer_get_u32_array(struct target *target, const uint8_t *buffer, uint32_t count, uint32_t *dstbuf)
Definition: target.c:404
int64_t target_timer_next_event(void)
Returns when the next registered event will take place.
Definition: target.c:1941
verify_mode
Definition: target.c:3782
@ IMAGE_TEST
Definition: target.c:3783
@ IMAGE_VERIFY
Definition: target.c:3784
@ IMAGE_CHECKSUM_ONLY
Definition: target.c:3785
int target_write_phys_u16(struct target *target, target_addr_t address, uint16_t value)
Definition: target.c:2758
static void free_fastload(void)
Definition: target.c:6207
static void write_gmon(const uint32_t *samples, uint32_t sample_num, const char *filename, struct target *target, uint32_t duration_ms)
Definition: target.c:4286
static int handle_target(void *priv)
Definition: target.c:2929
const char * target_get_gdb_arch(const struct target *target)
Obtain the architecture for GDB.
Definition: target.c:1385
static int target_restore_working_area(struct target *target, struct working_area *area)
Definition: target.c:2129
nvp_assert
Definition: target.c:126
@ NVP_ASSERT
Definition: target.c:128
@ NVP_DEASSERT
Definition: target.c:127
static int target_gdb_fileio_end_default(struct target *target, int retcode, int fileio_errno, bool ctrl_c)
Definition: target.c:2342
int target_wait_algorithm(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_params, target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
Waits for an algorithm started with target_start_algorithm() to complete.
Definition: target.c:874
static int target_fill_mem(struct target *target, target_addr_t address, target_write_fn fn, unsigned int data_size, uint64_t b, unsigned int c)
Definition: target.c:3493
static void target_destroy(struct target *target)
Definition: target.c:2255
static void target_reset_active_polled(struct target *target)
Definition: target.c:668
int target_wait_state(struct target *target, enum target_state state, unsigned int ms)
Definition: target.c:3238
int target_step(struct target *target, bool current, target_addr_t address, bool handle_breakpoints)
Step the target.
Definition: target.c:1435
int target_examine(void)
Definition: target.c:725
int target_register_trace_callback(int(*callback)(struct target *target, size_t len, uint8_t *data, void *priv), void *priv)
Definition: target.c:1667
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:469
void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, const uint32_t *srcbuf)
Definition: target.c:428
static int target_free_working_area_restore(struct target *target, struct working_area *area, bool restore)
Definition: target.c:2146
static int handle_bp_command_set(struct command_invocation *cmd, target_addr_t addr, uint32_t asid, unsigned int length, int hw)
Definition: target.c:3977
static int identity_virt2phys(struct target *target, target_addr_t virtual, target_addr_t *physical)
Definition: target.c:647
static int target_register_user_commands(struct command_context *cmd_ctx)
Definition: target.c:6879
static void append_to_list_all_targets(struct target *target)
Definition: target.c:308
void target_handle_event(struct target *target, enum target_event e)
Definition: target.c:4699
uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
Definition: target.c:327
static int target_soft_reset_halt(struct target *target)
Definition: target.c:755
const char * target_type_name(const struct target *target)
Get the target type name.
Definition: target.c:750
int target_call_trace_callbacks(struct target *target, size_t len, uint8_t *data)
Definition: target.c:1854
static int target_process_reset(struct command_invocation *cmd, enum target_reset_mode reset_mode)
Definition: target.c:604
static const char * target_strerror_safe(int err)
Definition: target.c:156
static int64_t target_timer_next_event_value
Definition: target.c:120
int target_start_algorithm(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_params, target_addr_t entry_point, target_addr_t exit_point, void *arch_info)
Executes a target-specific native code algorithm and leaves it running.
Definition: target.c:829
static void write_data(FILE *f, const void *data, size_t len)
Definition: target.c:4210
void target_quit(void)
Free all the resources allocated by targets and the target layer.
Definition: target.c:2290
int target_write_phys_u8(struct target *target, target_addr_t address, uint8_t value)
Definition: target.c:2775
static struct target_type * target_types[]
Definition: target.c:75
void target_handle_md_output(struct command_invocation *cmd, struct target *target, target_addr_t address, unsigned int size, unsigned int count, const uint8_t *buffer, bool include_address)
Definition: target.c:3368
int target_read_u64(struct target *target, target_addr_t address, uint64_t *value)
Definition: target.c:2582
const char * target_reset_mode_name(enum target_reset_mode reset_mode)
Return the name of a target reset reason enumeration value.
Definition: target.c:297
target_debug_reason
Definition: target.h:71
@ DBG_REASON_WPTANDBKPT
Definition: target.h:75
@ DBG_REASON_UNDEFINED
Definition: target.h:80
@ DBG_REASON_EXIT
Definition: target.h:78
@ DBG_REASON_NOTHALTED
Definition: target.h:77
@ DBG_REASON_DBGRQ
Definition: target.h:72
@ DBG_REASON_SINGLESTEP
Definition: target.h:76
@ DBG_REASON_WATCHPOINT
Definition: target.h:74
@ DBG_REASON_EXC_CATCH
Definition: target.h:79
@ DBG_REASON_BREAKPOINT
Definition: target.h:73
target_reset_mode
Definition: target.h:64
@ RESET_RUN
Definition: target.h:66
@ RESET_HALT
Definition: target.h:67
@ RESET_UNKNOWN
Definition: target.h:65
@ RESET_INIT
Definition: target.h:68
target_register_class
Definition: target.h:113
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:818
#define ERROR_TARGET_INIT_FAILED
Definition: target.h:816
static bool target_was_examined(const struct target *target)
Definition: target.h:444
static bool target_active_polled(const struct target *target)
Definition: target.h:452
#define ERROR_TARGET_UNALIGNED_ACCESS
Definition: target.h:820
#define ERROR_TARGET_INVALID
Definition: target.h:815
target_timer_type
Definition: target.h:332
@ TARGET_TIMER_TYPE_PERIODIC
Definition: target.h:334
target_event
Definition: target.h:254
@ TARGET_EVENT_DEBUG_RESUMED
Definition: target.h:286
@ TARGET_EVENT_EXAMINE_START
Definition: target.h:288
@ TARGET_EVENT_RESET_START
Definition: target.h:276
@ TARGET_EVENT_SEMIHOSTING_USER_CMD_0X106
Definition: target.h:308
@ TARGET_EVENT_GDB_FLASH_WRITE_END
Definition: target.h:298
@ TARGET_EVENT_RESET_END
Definition: target.h:283
@ TARGET_EVENT_RESET_ASSERT_POST
Definition: target.h:279
@ TARGET_EVENT_RESET_DEASSERT_POST
Definition: target.h:281
@ TARGET_EVENT_HALTED
Definition: target.h:266
@ TARGET_EVENT_RESUMED
Definition: target.h:267
@ TARGET_EVENT_SEMIHOSTING_USER_CMD_0X102
Definition: target.h:304
@ TARGET_EVENT_SEMIHOSTING_USER_CMD_0X107
Definition: target.h:309
@ TARGET_EVENT_SEMIHOSTING_USER_CMD_0X105
Definition: target.h:307
@ TARGET_EVENT_EXAMINE_FAIL
Definition: target.h:289
@ TARGET_EVENT_GDB_START
Definition: target.h:273
@ TARGET_EVENT_EXAMINE_END
Definition: target.h:290
@ TARGET_EVENT_SEMIHOSTING_USER_CMD_0X104
Definition: target.h:306
@ TARGET_EVENT_RESET_INIT
Definition: target.h:282
@ TARGET_EVENT_GDB_END
Definition: target.h:274
@ TARGET_EVENT_RESET_DEASSERT_PRE
Definition: target.h:280
@ TARGET_EVENT_GDB_FLASH_ERASE_START
Definition: target.h:295
@ TARGET_EVENT_SEMIHOSTING_USER_CMD_0X103
Definition: target.h:305
@ TARGET_EVENT_DEBUG_HALTED
Definition: target.h:285
@ TARGET_EVENT_RESET_ASSERT_PRE
Definition: target.h:277
@ TARGET_EVENT_RESET_ASSERT
Definition: target.h:278
@ TARGET_EVENT_GDB_FLASH_WRITE_START
Definition: target.h:297
@ TARGET_EVENT_RESUME_START
Definition: target.h:268
@ TARGET_EVENT_STEP_END
Definition: target.h:271
@ TARGET_EVENT_STEP_START
Definition: target.h:270
@ TARGET_EVENT_GDB_ATTACH
Definition: target.h:292
@ TARGET_EVENT_SEMIHOSTING_USER_CMD_0X100
Definition: target.h:302
@ TARGET_EVENT_SEMIHOSTING_USER_CMD_0X101
Definition: target.h:303
@ TARGET_EVENT_RESUME_END
Definition: target.h:269
@ TARGET_EVENT_GDB_FLASH_ERASE_END
Definition: target.h:296
@ TARGET_EVENT_GDB_DETACH
Definition: target.h:293
@ TARGET_EVENT_TRACE_CONFIG
Definition: target.h:300
@ TARGET_EVENT_GDB_HALT
Definition: target.h:265
static const char * target_name(const struct target *target)
Returns the instance-specific name of the specified target.
Definition: target.h:247
target_state
Definition: target.h:55
@ TARGET_RESET
Definition: target.h:59
@ TARGET_DEBUG_RUNNING
Definition: target.h:60
@ TARGET_UNKNOWN
Definition: target.h:56
@ TARGET_UNAVAILABLE
Definition: target.h:61
@ TARGET_HALTED
Definition: target.h:58
@ TARGET_RUNNING
Definition: target.h:57
#define ERROR_TARGET_NOT_EXAMINED
Definition: target.h:825
@ TARGET_BIG_ENDIAN
Definition: target.h:85
@ TARGET_ENDIAN_UNKNOWN
Definition: target.h:84
@ TARGET_LITTLE_ENDIAN
Definition: target.h:85
#define TARGET_DEFAULT_POLLING_INTERVAL
Definition: target.h:833
#define ERROR_TARGET_TIMEOUT
Definition: target.h:817
#define TARGET_MAX_POLLING_INTERVAL_MS
Definition: target.h:834
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:822
static void target_set_examined(struct target *target)
Sets the examined and active_polled flags for the given target.
Definition: target.h:461
#define ERROR_TARGET_NOT_RUNNING
Definition: target.h:824
#define ERROR_TARGET_DATA_ABORT
Definition: target.h:821
#define ERROR_TARGET_FAILURE
Definition: target.h:819
#define ERROR_TARGET_TRANSLATION_FAULT
Definition: target.h:823
int target_request_register_commands(struct command_context *cmd_ctx)
struct target_type testee_target
Definition: testee.c:57
struct target_type xtensa_chip_target
Methods for generic example of Xtensa-based chip-level targets.
Definition: xtensa_chip.c:151
struct target_type xscale_target
Definition: xscale.c:3694
float duration_elapsed(const struct duration *duration)
Definition: time_support.c:46
int duration_measure(struct duration *duration)
Update the duration->elapsed field to finish the duration measurement.
Definition: time_support.c:34
int duration_start(struct duration *duration)
Update the duration->start field to start the duration measurement.
Definition: time_support.c:22
float duration_kbps(const struct duration *duration, size_t count)
Definition: time_support.c:51
int64_t timeval_ms(void)
int trace_register_commands(struct command_context *cmd_ctx)
Definition: trace.c:159
struct transport * get_current_transport(void)
Returns the transport currently being used by this debug or programming session.
Definition: transport.c:252
static void h_u32_to_be(uint8_t *buf, uint32_t val)
Definition: types.h:186
static uint64_t le_to_h_u64(const uint8_t *buf)
Definition: types.h:100
static uint32_t be_to_h_u24(const uint8_t *buf)
Definition: types.h:144
static void h_u16_to_be(uint8_t *buf, uint16_t val)
Definition: types.h:214
static uint64_t be_to_h_u64(const uint8_t *buf)
Definition: types.h:127
static uint16_t le_to_h_u16(const uint8_t *buf)
Definition: types.h:122
static uint32_t le_to_h_u24(const uint8_t *buf)
Definition: types.h:117
#define TARGET_ADDR_FMT
Definition: types.h:286
static void h_u32_to_le(uint8_t *buf, uint32_t val)
Definition: types.h:178
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
Definition: types.h:79
static uint32_t be_to_h_u32(const uint8_t *buf)
Definition: types.h:139
uint64_t target_addr_t
Definition: types.h:279
static void h_u24_to_le(uint8_t *buf, unsigned int val)
Definition: types.h:194
static void h_u24_to_be(uint8_t *buf, unsigned int val)
Definition: types.h:201
static uint16_t be_to_h_u16(const uint8_t *buf)
Definition: types.h:149
static void h_u16_to_le(uint8_t *buf, uint16_t val)
Definition: types.h:208
static uint32_t le_to_h_u32(const uint8_t *buf)
Definition: types.h:112
static void h_u64_to_be(uint8_t *buf, uint64_t val)
Definition: types.h:166
static void h_u64_to_le(uint8_t *buf, uint64_t val)
Definition: types.h:154
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t state[4]
Definition: vdebug.c:21
uint8_t count[4]
Definition: vdebug.c:22