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

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)