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

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)