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