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