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