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