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

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)