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