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