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