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

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)