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

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)