target: add valid smp target check
[openocd.git] / src / target / target.c
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
10 * *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
13 * *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
16 * *
17 * Copyright (C) 2011 by Broadcom Corporation *
18 * Evan Hunter - ehunter@broadcom.com *
19 * *
20 * Copyright (C) ST-Ericsson SA 2011 *
21 * michel.jaouen@stericsson.com : smp minimum support *
22 * *
23 * Copyright (C) 2011 Andreas Fritiofson *
24 * andreas.fritiofson@gmail.com *
25 * *
26 * This program is free software; you can redistribute it and/or modify *
27 * it under the terms of the GNU General Public License as published by *
28 * the Free Software Foundation; either version 2 of the License, or *
29 * (at your option) any later version. *
30 * *
31 * This program is distributed in the hope that it will be useful, *
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
34 * GNU General Public License for more details. *
35 * *
36 * You should have received a copy of the GNU General Public License *
37 * along with this program; if not, write to the *
38 * Free Software Foundation, Inc., *
39 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
40 ***************************************************************************/
41
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45
46 #include <helper/time_support.h>
47 #include <jtag/jtag.h>
48 #include <flash/nor/core.h>
49
50 #include "target.h"
51 #include "target_type.h"
52 #include "target_request.h"
53 #include "breakpoints.h"
54 #include "register.h"
55 #include "trace.h"
56 #include "image.h"
57 #include "rtos/rtos.h"
58
59 static int target_read_buffer_default(struct target *target, uint32_t address,
60 uint32_t size, uint8_t *buffer);
61 static int target_write_buffer_default(struct target *target, uint32_t address,
62 uint32_t size, const uint8_t *buffer);
63 static int target_array2mem(Jim_Interp *interp, struct target *target,
64 int argc, Jim_Obj * const *argv);
65 static int target_mem2array(Jim_Interp *interp, struct target *target,
66 int argc, Jim_Obj * const *argv);
67 static int target_register_user_commands(struct command_context *cmd_ctx);
68
69 /* targets */
70 extern struct target_type arm7tdmi_target;
71 extern struct target_type arm720t_target;
72 extern struct target_type arm9tdmi_target;
73 extern struct target_type arm920t_target;
74 extern struct target_type arm966e_target;
75 extern struct target_type arm946e_target;
76 extern struct target_type arm926ejs_target;
77 extern struct target_type fa526_target;
78 extern struct target_type feroceon_target;
79 extern struct target_type dragonite_target;
80 extern struct target_type xscale_target;
81 extern struct target_type cortexm3_target;
82 extern struct target_type cortexa8_target;
83 extern struct target_type arm11_target;
84 extern struct target_type mips_m4k_target;
85 extern struct target_type avr_target;
86 extern struct target_type dsp563xx_target;
87 extern struct target_type dsp5680xx_target;
88 extern struct target_type testee_target;
89 extern struct target_type avr32_ap7k_target;
90 extern struct target_type stm32_stlink_target;
91
92 static struct target_type *target_types[] = {
93 &arm7tdmi_target,
94 &arm9tdmi_target,
95 &arm920t_target,
96 &arm720t_target,
97 &arm966e_target,
98 &arm946e_target,
99 &arm926ejs_target,
100 &fa526_target,
101 &feroceon_target,
102 &dragonite_target,
103 &xscale_target,
104 &cortexm3_target,
105 &cortexa8_target,
106 &arm11_target,
107 &mips_m4k_target,
108 &avr_target,
109 &dsp563xx_target,
110 &dsp5680xx_target,
111 &testee_target,
112 &avr32_ap7k_target,
113 &stm32_stlink_target,
114 NULL,
115 };
116
117 struct target *all_targets;
118 static struct target_event_callback *target_event_callbacks;
119 static struct target_timer_callback *target_timer_callbacks;
120 static const int polling_interval = 100;
121
122 static const Jim_Nvp nvp_assert[] = {
123 { .name = "assert", NVP_ASSERT },
124 { .name = "deassert", NVP_DEASSERT },
125 { .name = "T", NVP_ASSERT },
126 { .name = "F", NVP_DEASSERT },
127 { .name = "t", NVP_ASSERT },
128 { .name = "f", NVP_DEASSERT },
129 { .name = NULL, .value = -1 }
130 };
131
132 static const Jim_Nvp nvp_error_target[] = {
133 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
134 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
135 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
136 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
137 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
138 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
139 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
140 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
141 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
142 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
143 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
144 { .value = -1, .name = NULL }
145 };
146
147 static const char *target_strerror_safe(int err)
148 {
149 const Jim_Nvp *n;
150
151 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
152 if (n->name == NULL)
153 return "unknown";
154 else
155 return n->name;
156 }
157
158 static const Jim_Nvp nvp_target_event[] = {
159
160 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
161 { .value = TARGET_EVENT_HALTED, .name = "halted" },
162 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
163 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
164 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
165
166 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
167 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
168
169 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
170 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
171 { .value = TARGET_EVENT_RESET_ASSERT, .name = "reset-assert" },
172 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
173 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
174 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
175 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
176 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
177 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
178 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
179 { .value = TARGET_EVENT_RESET_INIT, .name = "reset-init" },
180 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
181
182 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
183 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
184
185 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
186 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
187
188 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
189 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
190
191 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
192 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
193
194 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
195 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
196
197 { .name = NULL, .value = -1 }
198 };
199
200 static const Jim_Nvp nvp_target_state[] = {
201 { .name = "unknown", .value = TARGET_UNKNOWN },
202 { .name = "running", .value = TARGET_RUNNING },
203 { .name = "halted", .value = TARGET_HALTED },
204 { .name = "reset", .value = TARGET_RESET },
205 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
206 { .name = NULL, .value = -1 },
207 };
208
209 static const Jim_Nvp nvp_target_debug_reason[] = {
210 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
211 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
212 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
213 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
214 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
215 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
216 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
217 { .name = NULL, .value = -1 },
218 };
219
220 static const Jim_Nvp nvp_target_endian[] = {
221 { .name = "big", .value = TARGET_BIG_ENDIAN },
222 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
223 { .name = "be", .value = TARGET_BIG_ENDIAN },
224 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
225 { .name = NULL, .value = -1 },
226 };
227
228 static const Jim_Nvp nvp_reset_modes[] = {
229 { .name = "unknown", .value = RESET_UNKNOWN },
230 { .name = "run" , .value = RESET_RUN },
231 { .name = "halt" , .value = RESET_HALT },
232 { .name = "init" , .value = RESET_INIT },
233 { .name = NULL , .value = -1 },
234 };
235
236 const char *debug_reason_name(struct target *t)
237 {
238 const char *cp;
239
240 cp = Jim_Nvp_value2name_simple(nvp_target_debug_reason,
241 t->debug_reason)->name;
242 if (!cp) {
243 LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
244 cp = "(*BUG*unknown*BUG*)";
245 }
246 return cp;
247 }
248
249 const char *target_state_name(struct target *t)
250 {
251 const char *cp;
252 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
253 if (!cp) {
254 LOG_ERROR("Invalid target state: %d", (int)(t->state));
255 cp = "(*BUG*unknown*BUG*)";
256 }
257 return cp;
258 }
259
260 /* determine the number of the new target */
261 static int new_target_number(void)
262 {
263 struct target *t;
264 int x;
265
266 /* number is 0 based */
267 x = -1;
268 t = all_targets;
269 while (t) {
270 if (x < t->target_number)
271 x = t->target_number;
272 t = t->next;
273 }
274 return x + 1;
275 }
276
277 /* read a uint32_t from a buffer in target memory endianness */
278 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
279 {
280 if (target->endianness == TARGET_LITTLE_ENDIAN)
281 return le_to_h_u32(buffer);
282 else
283 return be_to_h_u32(buffer);
284 }
285
286 /* read a uint24_t from a buffer in target memory endianness */
287 uint32_t target_buffer_get_u24(struct target *target, const uint8_t *buffer)
288 {
289 if (target->endianness == TARGET_LITTLE_ENDIAN)
290 return le_to_h_u24(buffer);
291 else
292 return be_to_h_u24(buffer);
293 }
294
295 /* read a uint16_t from a buffer in target memory endianness */
296 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
297 {
298 if (target->endianness == TARGET_LITTLE_ENDIAN)
299 return le_to_h_u16(buffer);
300 else
301 return be_to_h_u16(buffer);
302 }
303
304 /* read a uint8_t from a buffer in target memory endianness */
305 static uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
306 {
307 return *buffer & 0x0ff;
308 }
309
310 /* write a uint32_t to a buffer in target memory endianness */
311 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
312 {
313 if (target->endianness == TARGET_LITTLE_ENDIAN)
314 h_u32_to_le(buffer, value);
315 else
316 h_u32_to_be(buffer, value);
317 }
318
319 /* write a uint24_t to a buffer in target memory endianness */
320 void target_buffer_set_u24(struct target *target, uint8_t *buffer, uint32_t value)
321 {
322 if (target->endianness == TARGET_LITTLE_ENDIAN)
323 h_u24_to_le(buffer, value);
324 else
325 h_u24_to_be(buffer, value);
326 }
327
328 /* write a uint16_t to a buffer in target memory endianness */
329 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
330 {
331 if (target->endianness == TARGET_LITTLE_ENDIAN)
332 h_u16_to_le(buffer, value);
333 else
334 h_u16_to_be(buffer, value);
335 }
336
337 /* write a uint8_t to a buffer in target memory endianness */
338 static void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
339 {
340 *buffer = value;
341 }
342
343 /* write a uint32_t array to a buffer in target memory endianness */
344 void target_buffer_get_u32_array(struct target *target, const uint8_t *buffer, uint32_t count, uint32_t *dstbuf)
345 {
346 uint32_t i;
347 for (i = 0; i < count; i++)
348 dstbuf[i] = target_buffer_get_u32(target, &buffer[i * 4]);
349 }
350
351 /* write a uint16_t array to a buffer in target memory endianness */
352 void target_buffer_get_u16_array(struct target *target, const uint8_t *buffer, uint32_t count, uint16_t *dstbuf)
353 {
354 uint32_t i;
355 for (i = 0; i < count; i++)
356 dstbuf[i] = target_buffer_get_u16(target, &buffer[i * 2]);
357 }
358
359 /* write a uint32_t array to a buffer in target memory endianness */
360 void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, uint32_t *srcbuf)
361 {
362 uint32_t i;
363 for (i = 0; i < count; i++)
364 target_buffer_set_u32(target, &buffer[i * 4], srcbuf[i]);
365 }
366
367 /* write a uint16_t array to a buffer in target memory endianness */
368 void target_buffer_set_u16_array(struct target *target, uint8_t *buffer, uint32_t count, uint16_t *srcbuf)
369 {
370 uint32_t i;
371 for (i = 0; i < count; i++)
372 target_buffer_set_u16(target, &buffer[i * 2], srcbuf[i]);
373 }
374
375 /* return a pointer to a configured target; id is name or number */
376 struct target *get_target(const char *id)
377 {
378 struct target *target;
379
380 /* try as tcltarget name */
381 for (target = all_targets; target; target = target->next) {
382 if (target->cmd_name == NULL)
383 continue;
384 if (strcmp(id, target->cmd_name) == 0)
385 return target;
386 }
387
388 /* It's OK to remove this fallback sometime after August 2010 or so */
389
390 /* no match, try as number */
391 unsigned num;
392 if (parse_uint(id, &num) != ERROR_OK)
393 return NULL;
394
395 for (target = all_targets; target; target = target->next) {
396 if (target->target_number == (int)num) {
397 LOG_WARNING("use '%s' as target identifier, not '%u'",
398 target->cmd_name, num);
399 return target;
400 }
401 }
402
403 return NULL;
404 }
405
406 /* returns a pointer to the n-th configured target */
407 static struct target *get_target_by_num(int num)
408 {
409 struct target *target = all_targets;
410
411 while (target) {
412 if (target->target_number == num)
413 return target;
414 target = target->next;
415 }
416
417 return NULL;
418 }
419
420 struct target *get_current_target(struct command_context *cmd_ctx)
421 {
422 struct target *target = get_target_by_num(cmd_ctx->current_target);
423
424 if (target == NULL) {
425 LOG_ERROR("BUG: current_target out of bounds");
426 exit(-1);
427 }
428
429 return target;
430 }
431
432 int target_poll(struct target *target)
433 {
434 int retval;
435
436 /* We can't poll until after examine */
437 if (!target_was_examined(target)) {
438 /* Fail silently lest we pollute the log */
439 return ERROR_FAIL;
440 }
441
442 retval = target->type->poll(target);
443 if (retval != ERROR_OK)
444 return retval;
445
446 if (target->halt_issued) {
447 if (target->state == TARGET_HALTED)
448 target->halt_issued = false;
449 else {
450 long long t = timeval_ms() - target->halt_issued_time;
451 if (t > 1000) {
452 target->halt_issued = false;
453 LOG_INFO("Halt timed out, wake up GDB.");
454 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
455 }
456 }
457 }
458
459 return ERROR_OK;
460 }
461
462 int target_halt(struct target *target)
463 {
464 int retval;
465 /* We can't poll until after examine */
466 if (!target_was_examined(target)) {
467 LOG_ERROR("Target not examined yet");
468 return ERROR_FAIL;
469 }
470
471 retval = target->type->halt(target);
472 if (retval != ERROR_OK)
473 return retval;
474
475 target->halt_issued = true;
476 target->halt_issued_time = timeval_ms();
477
478 return ERROR_OK;
479 }
480
481 /**
482 * Make the target (re)start executing using its saved execution
483 * context (possibly with some modifications).
484 *
485 * @param target Which target should start executing.
486 * @param current True to use the target's saved program counter instead
487 * of the address parameter
488 * @param address Optionally used as the program counter.
489 * @param handle_breakpoints True iff breakpoints at the resumption PC
490 * should be skipped. (For example, maybe execution was stopped by
491 * such a breakpoint, in which case it would be counterprodutive to
492 * let it re-trigger.
493 * @param debug_execution False if all working areas allocated by OpenOCD
494 * should be released and/or restored to their original contents.
495 * (This would for example be true to run some downloaded "helper"
496 * algorithm code, which resides in one such working buffer and uses
497 * another for data storage.)
498 *
499 * @todo Resolve the ambiguity about what the "debug_execution" flag
500 * signifies. For example, Target implementations don't agree on how
501 * it relates to invalidation of the register cache, or to whether
502 * breakpoints and watchpoints should be enabled. (It would seem wrong
503 * to enable breakpoints when running downloaded "helper" algorithms
504 * (debug_execution true), since the breakpoints would be set to match
505 * target firmware being debugged, not the helper algorithm.... and
506 * enabling them could cause such helpers to malfunction (for example,
507 * by overwriting data with a breakpoint instruction. On the other
508 * hand the infrastructure for running such helpers might use this
509 * procedure but rely on hardware breakpoint to detect termination.)
510 */
511 int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
512 {
513 int retval;
514
515 /* We can't poll until after examine */
516 if (!target_was_examined(target)) {
517 LOG_ERROR("Target not examined yet");
518 return ERROR_FAIL;
519 }
520
521 target_call_event_callbacks(target, TARGET_EVENT_RESUME_START);
522
523 /* note that resume *must* be asynchronous. The CPU can halt before
524 * we poll. The CPU can even halt at the current PC as a result of
525 * a software breakpoint being inserted by (a bug?) the application.
526 */
527 retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution);
528 if (retval != ERROR_OK)
529 return retval;
530
531 target_call_event_callbacks(target, TARGET_EVENT_RESUME_END);
532
533 return retval;
534 }
535
536 static int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
537 {
538 char buf[100];
539 int retval;
540 Jim_Nvp *n;
541 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
542 if (n->name == NULL) {
543 LOG_ERROR("invalid reset mode");
544 return ERROR_FAIL;
545 }
546
547 /* disable polling during reset to make reset event scripts
548 * more predictable, i.e. dr/irscan & pathmove in events will
549 * not have JTAG operations injected into the middle of a sequence.
550 */
551 bool save_poll = jtag_poll_get_enabled();
552
553 jtag_poll_set_enabled(false);
554
555 sprintf(buf, "ocd_process_reset %s", n->name);
556 retval = Jim_Eval(cmd_ctx->interp, buf);
557
558 jtag_poll_set_enabled(save_poll);
559
560 if (retval != JIM_OK) {
561 Jim_MakeErrorMessage(cmd_ctx->interp);
562 command_print(NULL, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx->interp), NULL));
563 return ERROR_FAIL;
564 }
565
566 /* We want any events to be processed before the prompt */
567 retval = target_call_timer_callbacks_now();
568
569 struct target *target;
570 for (target = all_targets; target; target = target->next)
571 target->type->check_reset(target);
572
573 return retval;
574 }
575
576 static int identity_virt2phys(struct target *target,
577 uint32_t virtual, uint32_t *physical)
578 {
579 *physical = virtual;
580 return ERROR_OK;
581 }
582
583 static int no_mmu(struct target *target, int *enabled)
584 {
585 *enabled = 0;
586 return ERROR_OK;
587 }
588
589 static int default_examine(struct target *target)
590 {
591 target_set_examined(target);
592 return ERROR_OK;
593 }
594
595 /* no check by default */
596 static int default_check_reset(struct target *target)
597 {
598 return ERROR_OK;
599 }
600
601 int target_examine_one(struct target *target)
602 {
603 return target->type->examine(target);
604 }
605
606 static int jtag_enable_callback(enum jtag_event event, void *priv)
607 {
608 struct target *target = priv;
609
610 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
611 return ERROR_OK;
612
613 jtag_unregister_event_callback(jtag_enable_callback, target);
614
615 target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_START);
616
617 int retval = target_examine_one(target);
618 if (retval != ERROR_OK)
619 return retval;
620
621 target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_END);
622
623 return retval;
624 }
625
626 /* Targets that correctly implement init + examine, i.e.
627 * no communication with target during init:
628 *
629 * XScale
630 */
631 int target_examine(void)
632 {
633 int retval = ERROR_OK;
634 struct target *target;
635
636 for (target = all_targets; target; target = target->next) {
637 /* defer examination, but don't skip it */
638 if (!target->tap->enabled) {
639 jtag_register_event_callback(jtag_enable_callback,
640 target);
641 continue;
642 }
643
644 target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_START);
645
646 retval = target_examine_one(target);
647 if (retval != ERROR_OK)
648 return retval;
649
650 target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_END);
651 }
652 return retval;
653 }
654
655 const char *target_type_name(struct target *target)
656 {
657 return target->type->name;
658 }
659
660 static int target_write_memory_imp(struct target *target, uint32_t address,
661 uint32_t size, uint32_t count, const uint8_t *buffer)
662 {
663 if (!target_was_examined(target)) {
664 LOG_ERROR("Target not examined yet");
665 return ERROR_FAIL;
666 }
667 return target->type->write_memory_imp(target, address, size, count, buffer);
668 }
669
670 static int target_read_memory_imp(struct target *target, uint32_t address,
671 uint32_t size, uint32_t count, uint8_t *buffer)
672 {
673 if (!target_was_examined(target)) {
674 LOG_ERROR("Target not examined yet");
675 return ERROR_FAIL;
676 }
677 return target->type->read_memory_imp(target, address, size, count, buffer);
678 }
679
680 static int target_soft_reset_halt_imp(struct target *target)
681 {
682 if (!target_was_examined(target)) {
683 LOG_ERROR("Target not examined yet");
684 return ERROR_FAIL;
685 }
686 if (!target->type->soft_reset_halt_imp) {
687 LOG_ERROR("Target %s does not support soft_reset_halt",
688 target_name(target));
689 return ERROR_FAIL;
690 }
691 return target->type->soft_reset_halt_imp(target);
692 }
693
694 /**
695 * Downloads a target-specific native code algorithm to the target,
696 * and executes it. * Note that some targets may need to set up, enable,
697 * and tear down a breakpoint (hard or * soft) to detect algorithm
698 * termination, while others may support lower overhead schemes where
699 * soft breakpoints embedded in the algorithm automatically terminate the
700 * algorithm.
701 *
702 * @param target used to run the algorithm
703 * @param arch_info target-specific description of the algorithm.
704 */
705 int target_run_algorithm(struct target *target,
706 int num_mem_params, struct mem_param *mem_params,
707 int num_reg_params, struct reg_param *reg_param,
708 uint32_t entry_point, uint32_t exit_point,
709 int timeout_ms, void *arch_info)
710 {
711 int retval = ERROR_FAIL;
712
713 if (!target_was_examined(target)) {
714 LOG_ERROR("Target not examined yet");
715 goto done;
716 }
717 if (!target->type->run_algorithm) {
718 LOG_ERROR("Target type '%s' does not support %s",
719 target_type_name(target), __func__);
720 goto done;
721 }
722
723 target->running_alg = true;
724 retval = target->type->run_algorithm(target,
725 num_mem_params, mem_params,
726 num_reg_params, reg_param,
727 entry_point, exit_point, timeout_ms, arch_info);
728 target->running_alg = false;
729
730 done:
731 return retval;
732 }
733
734 /**
735 * Downloads a target-specific native code algorithm to the target,
736 * executes and leaves it running.
737 *
738 * @param target used to run the algorithm
739 * @param arch_info target-specific description of the algorithm.
740 */
741 int target_start_algorithm(struct target *target,
742 int num_mem_params, struct mem_param *mem_params,
743 int num_reg_params, struct reg_param *reg_params,
744 uint32_t entry_point, uint32_t exit_point,
745 void *arch_info)
746 {
747 int retval = ERROR_FAIL;
748
749 if (!target_was_examined(target)) {
750 LOG_ERROR("Target not examined yet");
751 goto done;
752 }
753 if (!target->type->start_algorithm) {
754 LOG_ERROR("Target type '%s' does not support %s",
755 target_type_name(target), __func__);
756 goto done;
757 }
758 if (target->running_alg) {
759 LOG_ERROR("Target is already running an algorithm");
760 goto done;
761 }
762
763 target->running_alg = true;
764 retval = target->type->start_algorithm(target,
765 num_mem_params, mem_params,
766 num_reg_params, reg_params,
767 entry_point, exit_point, arch_info);
768
769 done:
770 return retval;
771 }
772
773 /**
774 * Waits for an algorithm started with target_start_algorithm() to complete.
775 *
776 * @param target used to run the algorithm
777 * @param arch_info target-specific description of the algorithm.
778 */
779 int target_wait_algorithm(struct target *target,
780 int num_mem_params, struct mem_param *mem_params,
781 int num_reg_params, struct reg_param *reg_params,
782 uint32_t exit_point, int timeout_ms,
783 void *arch_info)
784 {
785 int retval = ERROR_FAIL;
786
787 if (!target->type->wait_algorithm) {
788 LOG_ERROR("Target type '%s' does not support %s",
789 target_type_name(target), __func__);
790 goto done;
791 }
792 if (!target->running_alg) {
793 LOG_ERROR("Target is not running an algorithm");
794 goto done;
795 }
796
797 retval = target->type->wait_algorithm(target,
798 num_mem_params, mem_params,
799 num_reg_params, reg_params,
800 exit_point, timeout_ms, arch_info);
801 if (retval != ERROR_TARGET_TIMEOUT)
802 target->running_alg = false;
803
804 done:
805 return retval;
806 }
807
808 /**
809 * Executes a target-specific native code algorithm in the target.
810 * It differs from target_run_algorithm in that the algorithm is asynchronous.
811 * Because of this it requires an compliant algorithm:
812 * see contrib/loaders/flash/stm32f1x.S for example.
813 *
814 * @param target used to run the algorithm
815 */
816
817 int target_run_flash_async_algorithm(struct target *target,
818 uint8_t *buffer, uint32_t count, int block_size,
819 int num_mem_params, struct mem_param *mem_params,
820 int num_reg_params, struct reg_param *reg_params,
821 uint32_t buffer_start, uint32_t buffer_size,
822 uint32_t entry_point, uint32_t exit_point, void *arch_info)
823 {
824 int retval;
825
826 /* Set up working area. First word is write pointer, second word is read pointer,
827 * rest is fifo data area. */
828 uint32_t wp_addr = buffer_start;
829 uint32_t rp_addr = buffer_start + 4;
830 uint32_t fifo_start_addr = buffer_start + 8;
831 uint32_t fifo_end_addr = buffer_start + buffer_size;
832
833 uint32_t wp = fifo_start_addr;
834 uint32_t rp = fifo_start_addr;
835
836 /* validate block_size is 2^n */
837 assert(!block_size || !(block_size & (block_size - 1)));
838
839 retval = target_write_u32(target, wp_addr, wp);
840 if (retval != ERROR_OK)
841 return retval;
842 retval = target_write_u32(target, rp_addr, rp);
843 if (retval != ERROR_OK)
844 return retval;
845
846 /* Start up algorithm on target and let it idle while writing the first chunk */
847 retval = target_start_algorithm(target, num_mem_params, mem_params,
848 num_reg_params, reg_params,
849 entry_point,
850 exit_point,
851 arch_info);
852
853 if (retval != ERROR_OK) {
854 LOG_ERROR("error starting target flash write algorithm");
855 return retval;
856 }
857
858 while (count > 0) {
859
860 retval = target_read_u32(target, rp_addr, &rp);
861 if (retval != ERROR_OK) {
862 LOG_ERROR("failed to get read pointer");
863 break;
864 }
865
866 LOG_DEBUG("count 0x%" PRIx32 " wp 0x%" PRIx32 " rp 0x%" PRIx32, count, wp, rp);
867
868 if (rp == 0) {
869 LOG_ERROR("flash write algorithm aborted by target");
870 retval = ERROR_FLASH_OPERATION_FAILED;
871 break;
872 }
873
874 if ((rp & (block_size - 1)) || rp < fifo_start_addr || rp >= fifo_end_addr) {
875 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32, rp);
876 break;
877 }
878
879 /* Count the number of bytes available in the fifo without
880 * crossing the wrap around. Make sure to not fill it completely,
881 * because that would make wp == rp and that's the empty condition. */
882 uint32_t thisrun_bytes;
883 if (rp > wp)
884 thisrun_bytes = rp - wp - block_size;
885 else if (rp > fifo_start_addr)
886 thisrun_bytes = fifo_end_addr - wp;
887 else
888 thisrun_bytes = fifo_end_addr - wp - block_size;
889
890 if (thisrun_bytes == 0) {
891 /* Throttle polling a bit if transfer is (much) faster than flash
892 * programming. The exact delay shouldn't matter as long as it's
893 * less than buffer size / flash speed. This is very unlikely to
894 * run when using high latency connections such as USB. */
895 alive_sleep(10);
896 continue;
897 }
898
899 /* Limit to the amount of data we actually want to write */
900 if (thisrun_bytes > count * block_size)
901 thisrun_bytes = count * block_size;
902
903 /* Write data to fifo */
904 retval = target_write_buffer(target, wp, thisrun_bytes, buffer);
905 if (retval != ERROR_OK)
906 break;
907
908 /* Update counters and wrap write pointer */
909 buffer += thisrun_bytes;
910 count -= thisrun_bytes / block_size;
911 wp += thisrun_bytes;
912 if (wp >= fifo_end_addr)
913 wp = fifo_start_addr;
914
915 /* Store updated write pointer to target */
916 retval = target_write_u32(target, wp_addr, wp);
917 if (retval != ERROR_OK)
918 break;
919 }
920
921 if (retval != ERROR_OK) {
922 /* abort flash write algorithm on target */
923 target_write_u32(target, wp_addr, 0);
924 }
925
926 int retval2 = target_wait_algorithm(target, num_mem_params, mem_params,
927 num_reg_params, reg_params,
928 exit_point,
929 10000,
930 arch_info);
931
932 if (retval2 != ERROR_OK) {
933 LOG_ERROR("error waiting for target flash write algorithm");
934 retval = retval2;
935 }
936
937 return retval;
938 }
939
940 int target_read_memory(struct target *target,
941 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
942 {
943 return target->type->read_memory(target, address, size, count, buffer);
944 }
945
946 static int target_read_phys_memory(struct target *target,
947 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
948 {
949 return target->type->read_phys_memory(target, address, size, count, buffer);
950 }
951
952 int target_write_memory(struct target *target,
953 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
954 {
955 return target->type->write_memory(target, address, size, count, buffer);
956 }
957
958 static int target_write_phys_memory(struct target *target,
959 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
960 {
961 return target->type->write_phys_memory(target, address, size, count, buffer);
962 }
963
964 int target_bulk_write_memory(struct target *target,
965 uint32_t address, uint32_t count, const uint8_t *buffer)
966 {
967 return target->type->bulk_write_memory(target, address, count, buffer);
968 }
969
970 int target_add_breakpoint(struct target *target,
971 struct breakpoint *breakpoint)
972 {
973 if ((target->state != TARGET_HALTED) && (breakpoint->type != BKPT_HARD)) {
974 LOG_WARNING("target %s is not halted", target->cmd_name);
975 return ERROR_TARGET_NOT_HALTED;
976 }
977 return target->type->add_breakpoint(target, breakpoint);
978 }
979
980 int target_add_context_breakpoint(struct target *target,
981 struct breakpoint *breakpoint)
982 {
983 if (target->state != TARGET_HALTED) {
984 LOG_WARNING("target %s is not halted", target->cmd_name);
985 return ERROR_TARGET_NOT_HALTED;
986 }
987 return target->type->add_context_breakpoint(target, breakpoint);
988 }
989
990 int target_add_hybrid_breakpoint(struct target *target,
991 struct breakpoint *breakpoint)
992 {
993 if (target->state != TARGET_HALTED) {
994 LOG_WARNING("target %s is not halted", target->cmd_name);
995 return ERROR_TARGET_NOT_HALTED;
996 }
997 return target->type->add_hybrid_breakpoint(target, breakpoint);
998 }
999
1000 int target_remove_breakpoint(struct target *target,
1001 struct breakpoint *breakpoint)
1002 {
1003 return target->type->remove_breakpoint(target, breakpoint);
1004 }
1005
1006 int target_add_watchpoint(struct target *target,
1007 struct watchpoint *watchpoint)
1008 {
1009 if (target->state != TARGET_HALTED) {
1010 LOG_WARNING("target %s is not halted", target->cmd_name);
1011 return ERROR_TARGET_NOT_HALTED;
1012 }
1013 return target->type->add_watchpoint(target, watchpoint);
1014 }
1015 int target_remove_watchpoint(struct target *target,
1016 struct watchpoint *watchpoint)
1017 {
1018 return target->type->remove_watchpoint(target, watchpoint);
1019 }
1020
1021 int target_get_gdb_reg_list(struct target *target,
1022 struct reg **reg_list[], int *reg_list_size)
1023 {
1024 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
1025 }
1026 int target_step(struct target *target,
1027 int current, uint32_t address, int handle_breakpoints)
1028 {
1029 return target->type->step(target, current, address, handle_breakpoints);
1030 }
1031
1032 /**
1033 * Reset the @c examined flag for the given target.
1034 * Pure paranoia -- targets are zeroed on allocation.
1035 */
1036 static void target_reset_examined(struct target *target)
1037 {
1038 target->examined = false;
1039 }
1040
1041 static int err_read_phys_memory(struct target *target, uint32_t address,
1042 uint32_t size, uint32_t count, uint8_t *buffer)
1043 {
1044 LOG_ERROR("Not implemented: %s", __func__);
1045 return ERROR_FAIL;
1046 }
1047
1048 static int err_write_phys_memory(struct target *target, uint32_t address,
1049 uint32_t size, uint32_t count, const uint8_t *buffer)
1050 {
1051 LOG_ERROR("Not implemented: %s", __func__);
1052 return ERROR_FAIL;
1053 }
1054
1055 static int handle_target(void *priv);
1056
1057 static int target_init_one(struct command_context *cmd_ctx,
1058 struct target *target)
1059 {
1060 target_reset_examined(target);
1061
1062 struct target_type *type = target->type;
1063 if (type->examine == NULL)
1064 type->examine = default_examine;
1065
1066 if (type->check_reset == NULL)
1067 type->check_reset = default_check_reset;
1068
1069 assert(type->init_target != NULL);
1070
1071 int retval = type->init_target(cmd_ctx, target);
1072 if (ERROR_OK != retval) {
1073 LOG_ERROR("target '%s' init failed", target_name(target));
1074 return retval;
1075 }
1076
1077 /**
1078 * @todo get rid of those *memory_imp() methods, now that all
1079 * callers are using target_*_memory() accessors ... and make
1080 * sure the "physical" paths handle the same issues.
1081 */
1082 /* a non-invasive way(in terms of patches) to add some code that
1083 * runs before the type->write/read_memory implementation
1084 */
1085 type->write_memory_imp = target->type->write_memory;
1086 type->write_memory = target_write_memory_imp;
1087
1088 type->read_memory_imp = target->type->read_memory;
1089 type->read_memory = target_read_memory_imp;
1090
1091 type->soft_reset_halt_imp = target->type->soft_reset_halt;
1092 type->soft_reset_halt = target_soft_reset_halt_imp;
1093
1094 /* Sanity-check MMU support ... stub in what we must, to help
1095 * implement it in stages, but warn if we need to do so.
1096 */
1097 if (type->mmu) {
1098 if (type->write_phys_memory == NULL) {
1099 LOG_ERROR("type '%s' is missing write_phys_memory",
1100 type->name);
1101 type->write_phys_memory = err_write_phys_memory;
1102 }
1103 if (type->read_phys_memory == NULL) {
1104 LOG_ERROR("type '%s' is missing read_phys_memory",
1105 type->name);
1106 type->read_phys_memory = err_read_phys_memory;
1107 }
1108 if (type->virt2phys == NULL) {
1109 LOG_ERROR("type '%s' is missing virt2phys", type->name);
1110 type->virt2phys = identity_virt2phys;
1111 }
1112 } else {
1113 /* Make sure no-MMU targets all behave the same: make no
1114 * distinction between physical and virtual addresses, and
1115 * ensure that virt2phys() is always an identity mapping.
1116 */
1117 if (type->write_phys_memory || type->read_phys_memory || type->virt2phys)
1118 LOG_WARNING("type '%s' has bad MMU hooks", type->name);
1119
1120 type->mmu = no_mmu;
1121 type->write_phys_memory = type->write_memory;
1122 type->read_phys_memory = type->read_memory;
1123 type->virt2phys = identity_virt2phys;
1124 }
1125
1126 if (target->type->read_buffer == NULL)
1127 target->type->read_buffer = target_read_buffer_default;
1128
1129 if (target->type->write_buffer == NULL)
1130 target->type->write_buffer = target_write_buffer_default;
1131
1132 return ERROR_OK;
1133 }
1134
1135 static int target_init(struct command_context *cmd_ctx)
1136 {
1137 struct target *target;
1138 int retval;
1139
1140 for (target = all_targets; target; target = target->next) {
1141 retval = target_init_one(cmd_ctx, target);
1142 if (ERROR_OK != retval)
1143 return retval;
1144 }
1145
1146 if (!all_targets)
1147 return ERROR_OK;
1148
1149 retval = target_register_user_commands(cmd_ctx);
1150 if (ERROR_OK != retval)
1151 return retval;
1152
1153 retval = target_register_timer_callback(&handle_target,
1154 polling_interval, 1, cmd_ctx->interp);
1155 if (ERROR_OK != retval)
1156 return retval;
1157
1158 return ERROR_OK;
1159 }
1160
1161 COMMAND_HANDLER(handle_target_init_command)
1162 {
1163 int retval;
1164
1165 if (CMD_ARGC != 0)
1166 return ERROR_COMMAND_SYNTAX_ERROR;
1167
1168 static bool target_initialized;
1169 if (target_initialized) {
1170 LOG_INFO("'target init' has already been called");
1171 return ERROR_OK;
1172 }
1173 target_initialized = true;
1174
1175 retval = command_run_line(CMD_CTX, "init_targets");
1176 if (ERROR_OK != retval)
1177 return retval;
1178
1179 retval = command_run_line(CMD_CTX, "init_board");
1180 if (ERROR_OK != retval)
1181 return retval;
1182
1183 LOG_DEBUG("Initializing targets...");
1184 return target_init(CMD_CTX);
1185 }
1186
1187 int target_register_event_callback(int (*callback)(struct target *target,
1188 enum target_event event, void *priv), void *priv)
1189 {
1190 struct target_event_callback **callbacks_p = &target_event_callbacks;
1191
1192 if (callback == NULL)
1193 return ERROR_COMMAND_SYNTAX_ERROR;
1194
1195 if (*callbacks_p) {
1196 while ((*callbacks_p)->next)
1197 callbacks_p = &((*callbacks_p)->next);
1198 callbacks_p = &((*callbacks_p)->next);
1199 }
1200
1201 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
1202 (*callbacks_p)->callback = callback;
1203 (*callbacks_p)->priv = priv;
1204 (*callbacks_p)->next = NULL;
1205
1206 return ERROR_OK;
1207 }
1208
1209 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
1210 {
1211 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
1212 struct timeval now;
1213
1214 if (callback == NULL)
1215 return ERROR_COMMAND_SYNTAX_ERROR;
1216
1217 if (*callbacks_p) {
1218 while ((*callbacks_p)->next)
1219 callbacks_p = &((*callbacks_p)->next);
1220 callbacks_p = &((*callbacks_p)->next);
1221 }
1222
1223 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
1224 (*callbacks_p)->callback = callback;
1225 (*callbacks_p)->periodic = periodic;
1226 (*callbacks_p)->time_ms = time_ms;
1227
1228 gettimeofday(&now, NULL);
1229 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
1230 time_ms -= (time_ms % 1000);
1231 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
1232 if ((*callbacks_p)->when.tv_usec > 1000000) {
1233 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
1234 (*callbacks_p)->when.tv_sec += 1;
1235 }
1236
1237 (*callbacks_p)->priv = priv;
1238 (*callbacks_p)->next = NULL;
1239
1240 return ERROR_OK;
1241 }
1242
1243 int target_unregister_event_callback(int (*callback)(struct target *target,
1244 enum target_event event, void *priv), void *priv)
1245 {
1246 struct target_event_callback **p = &target_event_callbacks;
1247 struct target_event_callback *c = target_event_callbacks;
1248
1249 if (callback == NULL)
1250 return ERROR_COMMAND_SYNTAX_ERROR;
1251
1252 while (c) {
1253 struct target_event_callback *next = c->next;
1254 if ((c->callback == callback) && (c->priv == priv)) {
1255 *p = next;
1256 free(c);
1257 return ERROR_OK;
1258 } else
1259 p = &(c->next);
1260 c = next;
1261 }
1262
1263 return ERROR_OK;
1264 }
1265
1266 static int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
1267 {
1268 struct target_timer_callback **p = &target_timer_callbacks;
1269 struct target_timer_callback *c = target_timer_callbacks;
1270
1271 if (callback == NULL)
1272 return ERROR_COMMAND_SYNTAX_ERROR;
1273
1274 while (c) {
1275 struct target_timer_callback *next = c->next;
1276 if ((c->callback == callback) && (c->priv == priv)) {
1277 *p = next;
1278 free(c);
1279 return ERROR_OK;
1280 } else
1281 p = &(c->next);
1282 c = next;
1283 }
1284
1285 return ERROR_OK;
1286 }
1287
1288 int target_call_event_callbacks(struct target *target, enum target_event event)
1289 {
1290 struct target_event_callback *callback = target_event_callbacks;
1291 struct target_event_callback *next_callback;
1292
1293 if (event == TARGET_EVENT_HALTED) {
1294 /* execute early halted first */
1295 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1296 }
1297
1298 LOG_DEBUG("target event %i (%s)", event,
1299 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
1300
1301 target_handle_event(target, event);
1302
1303 while (callback) {
1304 next_callback = callback->next;
1305 callback->callback(target, event, callback->priv);
1306 callback = next_callback;
1307 }
1308
1309 return ERROR_OK;
1310 }
1311
1312 static int target_timer_callback_periodic_restart(
1313 struct target_timer_callback *cb, struct timeval *now)
1314 {
1315 int time_ms = cb->time_ms;
1316 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
1317 time_ms -= (time_ms % 1000);
1318 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
1319 if (cb->when.tv_usec > 1000000) {
1320 cb->when.tv_usec = cb->when.tv_usec - 1000000;
1321 cb->when.tv_sec += 1;
1322 }
1323 return ERROR_OK;
1324 }
1325
1326 static int target_call_timer_callback(struct target_timer_callback *cb,
1327 struct timeval *now)
1328 {
1329 cb->callback(cb->priv);
1330
1331 if (cb->periodic)
1332 return target_timer_callback_periodic_restart(cb, now);
1333
1334 return target_unregister_timer_callback(cb->callback, cb->priv);
1335 }
1336
1337 static int target_call_timer_callbacks_check_time(int checktime)
1338 {
1339 keep_alive();
1340
1341 struct timeval now;
1342 gettimeofday(&now, NULL);
1343
1344 struct target_timer_callback *callback = target_timer_callbacks;
1345 while (callback) {
1346 /* cleaning up may unregister and free this callback */
1347 struct target_timer_callback *next_callback = callback->next;
1348
1349 bool call_it = callback->callback &&
1350 ((!checktime && callback->periodic) ||
1351 now.tv_sec > callback->when.tv_sec ||
1352 (now.tv_sec == callback->when.tv_sec &&
1353 now.tv_usec >= callback->when.tv_usec));
1354
1355 if (call_it) {
1356 int retval = target_call_timer_callback(callback, &now);
1357 if (retval != ERROR_OK)
1358 return retval;
1359 }
1360
1361 callback = next_callback;
1362 }
1363
1364 return ERROR_OK;
1365 }
1366
1367 int target_call_timer_callbacks(void)
1368 {
1369 return target_call_timer_callbacks_check_time(1);
1370 }
1371
1372 /* invoke periodic callbacks immediately */
1373 int target_call_timer_callbacks_now(void)
1374 {
1375 return target_call_timer_callbacks_check_time(0);
1376 }
1377
1378 /* Prints the working area layout for debug purposes */
1379 static void print_wa_layout(struct target *target)
1380 {
1381 struct working_area *c = target->working_areas;
1382
1383 while (c) {
1384 LOG_DEBUG("%c%c 0x%08"PRIx32"-0x%08"PRIx32" (%"PRIu32" bytes)",
1385 c->backup ? 'b' : ' ', c->free ? ' ' : '*',
1386 c->address, c->address + c->size - 1, c->size);
1387 c = c->next;
1388 }
1389 }
1390
1391 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1392 static void target_split_working_area(struct working_area *area, uint32_t size)
1393 {
1394 assert(area->free); /* Shouldn't split an allocated area */
1395 assert(size <= area->size); /* Caller should guarantee this */
1396
1397 /* Split only if not already the right size */
1398 if (size < area->size) {
1399 struct working_area *new_wa = malloc(sizeof(*new_wa));
1400
1401 if (new_wa == NULL)
1402 return;
1403
1404 new_wa->next = area->next;
1405 new_wa->size = area->size - size;
1406 new_wa->address = area->address + size;
1407 new_wa->backup = NULL;
1408 new_wa->user = NULL;
1409 new_wa->free = true;
1410
1411 area->next = new_wa;
1412 area->size = size;
1413
1414 /* If backup memory was allocated to this area, it has the wrong size
1415 * now so free it and it will be reallocated if/when needed */
1416 if (area->backup) {
1417 free(area->backup);
1418 area->backup = NULL;
1419 }
1420 }
1421 }
1422
1423 /* Merge all adjacent free areas into one */
1424 static void target_merge_working_areas(struct target *target)
1425 {
1426 struct working_area *c = target->working_areas;
1427
1428 while (c && c->next) {
1429 assert(c->next->address == c->address + c->size); /* This is an invariant */
1430
1431 /* Find two adjacent free areas */
1432 if (c->free && c->next->free) {
1433 /* Merge the last into the first */
1434 c->size += c->next->size;
1435
1436 /* Remove the last */
1437 struct working_area *to_be_freed = c->next;
1438 c->next = c->next->next;
1439 if (to_be_freed->backup)
1440 free(to_be_freed->backup);
1441 free(to_be_freed);
1442
1443 /* If backup memory was allocated to the remaining area, it's has
1444 * the wrong size now */
1445 if (c->backup) {
1446 free(c->backup);
1447 c->backup = NULL;
1448 }
1449 } else {
1450 c = c->next;
1451 }
1452 }
1453 }
1454
1455 int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
1456 {
1457 /* Reevaluate working area address based on MMU state*/
1458 if (target->working_areas == NULL) {
1459 int retval;
1460 int enabled;
1461
1462 retval = target->type->mmu(target, &enabled);
1463 if (retval != ERROR_OK)
1464 return retval;
1465
1466 if (!enabled) {
1467 if (target->working_area_phys_spec) {
1468 LOG_DEBUG("MMU disabled, using physical "
1469 "address for working memory 0x%08"PRIx32,
1470 target->working_area_phys);
1471 target->working_area = target->working_area_phys;
1472 } else {
1473 LOG_ERROR("No working memory available. "
1474 "Specify -work-area-phys to target.");
1475 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1476 }
1477 } else {
1478 if (target->working_area_virt_spec) {
1479 LOG_DEBUG("MMU enabled, using virtual "
1480 "address for working memory 0x%08"PRIx32,
1481 target->working_area_virt);
1482 target->working_area = target->working_area_virt;
1483 } else {
1484 LOG_ERROR("No working memory available. "
1485 "Specify -work-area-virt to target.");
1486 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1487 }
1488 }
1489
1490 /* Set up initial working area on first call */
1491 struct working_area *new_wa = malloc(sizeof(*new_wa));
1492 if (new_wa) {
1493 new_wa->next = NULL;
1494 new_wa->size = target->working_area_size & ~3UL; /* 4-byte align */
1495 new_wa->address = target->working_area;
1496 new_wa->backup = NULL;
1497 new_wa->user = NULL;
1498 new_wa->free = true;
1499 }
1500
1501 target->working_areas = new_wa;
1502 }
1503
1504 /* only allocate multiples of 4 byte */
1505 if (size % 4)
1506 size = (size + 3) & (~3UL);
1507
1508 struct working_area *c = target->working_areas;
1509
1510 /* Find the first large enough working area */
1511 while (c) {
1512 if (c->free && c->size >= size)
1513 break;
1514 c = c->next;
1515 }
1516
1517 if (c == NULL)
1518 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1519
1520 /* Split the working area into the requested size */
1521 target_split_working_area(c, size);
1522
1523 LOG_DEBUG("allocated new working area of %"PRIu32" bytes at address 0x%08"PRIx32, size, c->address);
1524
1525 if (target->backup_working_area) {
1526 if (c->backup == NULL) {
1527 c->backup = malloc(c->size);
1528 if (c->backup == NULL)
1529 return ERROR_FAIL;
1530 }
1531
1532 int retval = target_read_memory(target, c->address, 4, c->size / 4, c->backup);
1533 if (retval != ERROR_OK)
1534 return retval;
1535 }
1536
1537 /* mark as used, and return the new (reused) area */
1538 c->free = false;
1539 *area = c;
1540
1541 /* user pointer */
1542 c->user = area;
1543
1544 print_wa_layout(target);
1545
1546 return ERROR_OK;
1547 }
1548
1549 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1550 {
1551 int retval;
1552
1553 retval = target_alloc_working_area_try(target, size, area);
1554 if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
1555 LOG_WARNING("not enough working area available(requested %"PRIu32")", size);
1556 return retval;
1557
1558 }
1559
1560 static int target_restore_working_area(struct target *target, struct working_area *area)
1561 {
1562 int retval = ERROR_OK;
1563
1564 if (target->backup_working_area && area->backup != NULL) {
1565 retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup);
1566 if (retval != ERROR_OK)
1567 LOG_ERROR("failed to restore %"PRIu32" bytes of working area at address 0x%08"PRIx32,
1568 area->size, area->address);
1569 }
1570
1571 return retval;
1572 }
1573
1574 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1575 static int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1576 {
1577 int retval = ERROR_OK;
1578
1579 if (area->free)
1580 return retval;
1581
1582 if (restore) {
1583 retval = target_restore_working_area(target, area);
1584 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1585 if (retval != ERROR_OK)
1586 return retval;
1587 }
1588
1589 area->free = true;
1590
1591 LOG_DEBUG("freed %"PRIu32" bytes of working area at address 0x%08"PRIx32,
1592 area->size, area->address);
1593
1594 /* mark user pointer invalid */
1595 /* TODO: Is this really safe? It points to some previous caller's memory.
1596 * How could we know that the area pointer is still in that place and not
1597 * some other vital data? What's the purpose of this, anyway? */
1598 *area->user = NULL;
1599 area->user = NULL;
1600
1601 target_merge_working_areas(target);
1602
1603 print_wa_layout(target);
1604
1605 return retval;
1606 }
1607
1608 int target_free_working_area(struct target *target, struct working_area *area)
1609 {
1610 return target_free_working_area_restore(target, area, 1);
1611 }
1612
1613 /* free resources and restore memory, if restoring memory fails,
1614 * free up resources anyway
1615 */
1616 static void target_free_all_working_areas_restore(struct target *target, int restore)
1617 {
1618 struct working_area *c = target->working_areas;
1619
1620 LOG_DEBUG("freeing all working areas");
1621
1622 /* Loop through all areas, restoring the allocated ones and marking them as free */
1623 while (c) {
1624 if (!c->free) {
1625 if (restore)
1626 target_restore_working_area(target, c);
1627 c->free = true;
1628 *c->user = NULL; /* Same as above */
1629 c->user = NULL;
1630 }
1631 c = c->next;
1632 }
1633
1634 /* Run a merge pass to combine all areas into one */
1635 target_merge_working_areas(target);
1636
1637 print_wa_layout(target);
1638 }
1639
1640 void target_free_all_working_areas(struct target *target)
1641 {
1642 target_free_all_working_areas_restore(target, 1);
1643 }
1644
1645 /* Find the largest number of bytes that can be allocated */
1646 uint32_t target_get_working_area_avail(struct target *target)
1647 {
1648 struct working_area *c = target->working_areas;
1649 uint32_t max_size = 0;
1650
1651 if (c == NULL)
1652 return target->working_area_size;
1653
1654 while (c) {
1655 if (c->free && max_size < c->size)
1656 max_size = c->size;
1657
1658 c = c->next;
1659 }
1660
1661 return max_size;
1662 }
1663
1664 int target_arch_state(struct target *target)
1665 {
1666 int retval;
1667 if (target == NULL) {
1668 LOG_USER("No target has been configured");
1669 return ERROR_OK;
1670 }
1671
1672 LOG_USER("target state: %s", target_state_name(target));
1673
1674 if (target->state != TARGET_HALTED)
1675 return ERROR_OK;
1676
1677 retval = target->type->arch_state(target);
1678 return retval;
1679 }
1680
1681 /* Single aligned words are guaranteed to use 16 or 32 bit access
1682 * mode respectively, otherwise data is handled as quickly as
1683 * possible
1684 */
1685 int target_write_buffer(struct target *target, uint32_t address, uint32_t size, const uint8_t *buffer)
1686 {
1687 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1688 (int)size, (unsigned)address);
1689
1690 if (!target_was_examined(target)) {
1691 LOG_ERROR("Target not examined yet");
1692 return ERROR_FAIL;
1693 }
1694
1695 if (size == 0)
1696 return ERROR_OK;
1697
1698 if ((address + size - 1) < address) {
1699 /* GDB can request this when e.g. PC is 0xfffffffc*/
1700 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1701 (unsigned)address,
1702 (unsigned)size);
1703 return ERROR_FAIL;
1704 }
1705
1706 return target->type->write_buffer(target, address, size, buffer);
1707 }
1708
1709 static int target_write_buffer_default(struct target *target, uint32_t address, uint32_t size, const uint8_t *buffer)
1710 {
1711 int retval = ERROR_OK;
1712
1713 if (((address % 2) == 0) && (size == 2))
1714 return target_write_memory(target, address, 2, 1, buffer);
1715
1716 /* handle unaligned head bytes */
1717 if (address % 4) {
1718 uint32_t unaligned = 4 - (address % 4);
1719
1720 if (unaligned > size)
1721 unaligned = size;
1722
1723 retval = target_write_memory(target, address, 1, unaligned, buffer);
1724 if (retval != ERROR_OK)
1725 return retval;
1726
1727 buffer += unaligned;
1728 address += unaligned;
1729 size -= unaligned;
1730 }
1731
1732 /* handle aligned words */
1733 if (size >= 4) {
1734 int aligned = size - (size % 4);
1735
1736 /* use bulk writes above a certain limit. This may have to be changed */
1737 if (aligned > 128) {
1738 retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer);
1739 if (retval != ERROR_OK)
1740 return retval;
1741 } else {
1742 retval = target_write_memory(target, address, 4, aligned / 4, buffer);
1743 if (retval != ERROR_OK)
1744 return retval;
1745 }
1746
1747 buffer += aligned;
1748 address += aligned;
1749 size -= aligned;
1750 }
1751
1752 /* handle tail writes of less than 4 bytes */
1753 if (size > 0) {
1754 retval = target_write_memory(target, address, 1, size, buffer);
1755 if (retval != ERROR_OK)
1756 return retval;
1757 }
1758
1759 return retval;
1760 }
1761
1762 /* Single aligned words are guaranteed to use 16 or 32 bit access
1763 * mode respectively, otherwise data is handled as quickly as
1764 * possible
1765 */
1766 int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1767 {
1768 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1769 (int)size, (unsigned)address);
1770
1771 if (!target_was_examined(target)) {
1772 LOG_ERROR("Target not examined yet");
1773 return ERROR_FAIL;
1774 }
1775
1776 if (size == 0)
1777 return ERROR_OK;
1778
1779 if ((address + size - 1) < address) {
1780 /* GDB can request this when e.g. PC is 0xfffffffc*/
1781 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1782 address,
1783 size);
1784 return ERROR_FAIL;
1785 }
1786
1787 return target->type->read_buffer(target, address, size, buffer);
1788 }
1789
1790 static int target_read_buffer_default(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1791 {
1792 int retval = ERROR_OK;
1793
1794 if (((address % 2) == 0) && (size == 2))
1795 return target_read_memory(target, address, 2, 1, buffer);
1796
1797 /* handle unaligned head bytes */
1798 if (address % 4) {
1799 uint32_t unaligned = 4 - (address % 4);
1800
1801 if (unaligned > size)
1802 unaligned = size;
1803
1804 retval = target_read_memory(target, address, 1, unaligned, buffer);
1805 if (retval != ERROR_OK)
1806 return retval;
1807
1808 buffer += unaligned;
1809 address += unaligned;
1810 size -= unaligned;
1811 }
1812
1813 /* handle aligned words */
1814 if (size >= 4) {
1815 int aligned = size - (size % 4);
1816
1817 retval = target_read_memory(target, address, 4, aligned / 4, buffer);
1818 if (retval != ERROR_OK)
1819 return retval;
1820
1821 buffer += aligned;
1822 address += aligned;
1823 size -= aligned;
1824 }
1825
1826 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1827 if (size >= 2) {
1828 int aligned = size - (size % 2);
1829 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1830 if (retval != ERROR_OK)
1831 return retval;
1832
1833 buffer += aligned;
1834 address += aligned;
1835 size -= aligned;
1836 }
1837 /* handle tail writes of less than 4 bytes */
1838 if (size > 0) {
1839 retval = target_read_memory(target, address, 1, size, buffer);
1840 if (retval != ERROR_OK)
1841 return retval;
1842 }
1843
1844 return ERROR_OK;
1845 }
1846
1847 int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
1848 {
1849 uint8_t *buffer;
1850 int retval;
1851 uint32_t i;
1852 uint32_t checksum = 0;
1853 if (!target_was_examined(target)) {
1854 LOG_ERROR("Target not examined yet");
1855 return ERROR_FAIL;
1856 }
1857
1858 retval = target->type->checksum_memory(target, address, size, &checksum);
1859 if (retval != ERROR_OK) {
1860 buffer = malloc(size);
1861 if (buffer == NULL) {
1862 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1863 return ERROR_COMMAND_SYNTAX_ERROR;
1864 }
1865 retval = target_read_buffer(target, address, size, buffer);
1866 if (retval != ERROR_OK) {
1867 free(buffer);
1868 return retval;
1869 }
1870
1871 /* convert to target endianness */
1872 for (i = 0; i < (size/sizeof(uint32_t)); i++) {
1873 uint32_t target_data;
1874 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1875 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1876 }
1877
1878 retval = image_calculate_checksum(buffer, size, &checksum);
1879 free(buffer);
1880 }
1881
1882 *crc = checksum;
1883
1884 return retval;
1885 }
1886
1887 int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank)
1888 {
1889 int retval;
1890 if (!target_was_examined(target)) {
1891 LOG_ERROR("Target not examined yet");
1892 return ERROR_FAIL;
1893 }
1894
1895 if (target->type->blank_check_memory == 0)
1896 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1897
1898 retval = target->type->blank_check_memory(target, address, size, blank);
1899
1900 return retval;
1901 }
1902
1903 int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
1904 {
1905 uint8_t value_buf[4];
1906 if (!target_was_examined(target)) {
1907 LOG_ERROR("Target not examined yet");
1908 return ERROR_FAIL;
1909 }
1910
1911 int retval = target_read_memory(target, address, 4, 1, value_buf);
1912
1913 if (retval == ERROR_OK) {
1914 *value = target_buffer_get_u32(target, value_buf);
1915 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1916 address,
1917 *value);
1918 } else {
1919 *value = 0x0;
1920 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1921 address);
1922 }
1923
1924 return retval;
1925 }
1926
1927 int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
1928 {
1929 uint8_t value_buf[2];
1930 if (!target_was_examined(target)) {
1931 LOG_ERROR("Target not examined yet");
1932 return ERROR_FAIL;
1933 }
1934
1935 int retval = target_read_memory(target, address, 2, 1, value_buf);
1936
1937 if (retval == ERROR_OK) {
1938 *value = target_buffer_get_u16(target, value_buf);
1939 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1940 address,
1941 *value);
1942 } else {
1943 *value = 0x0;
1944 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1945 address);
1946 }
1947
1948 return retval;
1949 }
1950
1951 int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
1952 {
1953 int retval = target_read_memory(target, address, 1, 1, value);
1954 if (!target_was_examined(target)) {
1955 LOG_ERROR("Target not examined yet");
1956 return ERROR_FAIL;
1957 }
1958
1959 if (retval == ERROR_OK) {
1960 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1961 address,
1962 *value);
1963 } else {
1964 *value = 0x0;
1965 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1966 address);
1967 }
1968
1969 return retval;
1970 }
1971
1972 int target_write_u32(struct target *target, uint32_t address, uint32_t value)
1973 {
1974 int retval;
1975 uint8_t value_buf[4];
1976 if (!target_was_examined(target)) {
1977 LOG_ERROR("Target not examined yet");
1978 return ERROR_FAIL;
1979 }
1980
1981 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1982 address,
1983 value);
1984
1985 target_buffer_set_u32(target, value_buf, value);
1986 retval = target_write_memory(target, address, 4, 1, value_buf);
1987 if (retval != ERROR_OK)
1988 LOG_DEBUG("failed: %i", retval);
1989
1990 return retval;
1991 }
1992
1993 int target_write_u16(struct target *target, uint32_t address, uint16_t value)
1994 {
1995 int retval;
1996 uint8_t value_buf[2];
1997 if (!target_was_examined(target)) {
1998 LOG_ERROR("Target not examined yet");
1999 return ERROR_FAIL;
2000 }
2001
2002 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
2003 address,
2004 value);
2005
2006 target_buffer_set_u16(target, value_buf, value);
2007 retval = target_write_memory(target, address, 2, 1, value_buf);
2008 if (retval != ERROR_OK)
2009 LOG_DEBUG("failed: %i", retval);
2010
2011 return retval;
2012 }
2013
2014 int target_write_u8(struct target *target, uint32_t address, uint8_t value)
2015 {
2016 int retval;
2017 if (!target_was_examined(target)) {
2018 LOG_ERROR("Target not examined yet");
2019 return ERROR_FAIL;
2020 }
2021
2022 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
2023 address, value);
2024
2025 retval = target_write_memory(target, address, 1, 1, &value);
2026 if (retval != ERROR_OK)
2027 LOG_DEBUG("failed: %i", retval);
2028
2029 return retval;
2030 }
2031
2032 static int find_target(struct command_context *cmd_ctx, const char *name)
2033 {
2034 struct target *target = get_target(name);
2035 if (target == NULL) {
2036 LOG_ERROR("Target: %s is unknown, try one of:\n", name);
2037 return ERROR_FAIL;
2038 }
2039 if (!target->tap->enabled) {
2040 LOG_USER("Target: TAP %s is disabled, "
2041 "can't be the current target\n",
2042 target->tap->dotted_name);
2043 return ERROR_FAIL;
2044 }
2045
2046 cmd_ctx->current_target = target->target_number;
2047 return ERROR_OK;
2048 }
2049
2050
2051 COMMAND_HANDLER(handle_targets_command)
2052 {
2053 int retval = ERROR_OK;
2054 if (CMD_ARGC == 1) {
2055 retval = find_target(CMD_CTX, CMD_ARGV[0]);
2056 if (retval == ERROR_OK) {
2057 /* we're done! */
2058 return retval;
2059 }
2060 }
2061
2062 struct target *target = all_targets;
2063 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
2064 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
2065 while (target) {
2066 const char *state;
2067 char marker = ' ';
2068
2069 if (target->tap->enabled)
2070 state = target_state_name(target);
2071 else
2072 state = "tap-disabled";
2073
2074 if (CMD_CTX->current_target == target->target_number)
2075 marker = '*';
2076
2077 /* keep columns lined up to match the headers above */
2078 command_print(CMD_CTX,
2079 "%2d%c %-18s %-10s %-6s %-18s %s",
2080 target->target_number,
2081 marker,
2082 target_name(target),
2083 target_type_name(target),
2084 Jim_Nvp_value2name_simple(nvp_target_endian,
2085 target->endianness)->name,
2086 target->tap->dotted_name,
2087 state);
2088 target = target->next;
2089 }
2090
2091 return retval;
2092 }
2093
2094 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2095
2096 static int powerDropout;
2097 static int srstAsserted;
2098
2099 static int runPowerRestore;
2100 static int runPowerDropout;
2101 static int runSrstAsserted;
2102 static int runSrstDeasserted;
2103
2104 static int sense_handler(void)
2105 {
2106 static int prevSrstAsserted;
2107 static int prevPowerdropout;
2108
2109 int retval = jtag_power_dropout(&powerDropout);
2110 if (retval != ERROR_OK)
2111 return retval;
2112
2113 int powerRestored;
2114 powerRestored = prevPowerdropout && !powerDropout;
2115 if (powerRestored)
2116 runPowerRestore = 1;
2117
2118 long long current = timeval_ms();
2119 static long long lastPower;
2120 int waitMore = lastPower + 2000 > current;
2121 if (powerDropout && !waitMore) {
2122 runPowerDropout = 1;
2123 lastPower = current;
2124 }
2125
2126 retval = jtag_srst_asserted(&srstAsserted);
2127 if (retval != ERROR_OK)
2128 return retval;
2129
2130 int srstDeasserted;
2131 srstDeasserted = prevSrstAsserted && !srstAsserted;
2132
2133 static long long lastSrst;
2134 waitMore = lastSrst + 2000 > current;
2135 if (srstDeasserted && !waitMore) {
2136 runSrstDeasserted = 1;
2137 lastSrst = current;
2138 }
2139
2140 if (!prevSrstAsserted && srstAsserted)
2141 runSrstAsserted = 1;
2142
2143 prevSrstAsserted = srstAsserted;
2144 prevPowerdropout = powerDropout;
2145
2146 if (srstDeasserted || powerRestored) {
2147 /* Other than logging the event we can't do anything here.
2148 * Issuing a reset is a particularly bad idea as we might
2149 * be inside a reset already.
2150 */
2151 }
2152
2153 return ERROR_OK;
2154 }
2155
2156 static int backoff_times;
2157 static int backoff_count;
2158
2159 /* process target state changes */
2160 static int handle_target(void *priv)
2161 {
2162 Jim_Interp *interp = (Jim_Interp *)priv;
2163 int retval = ERROR_OK;
2164
2165 if (!is_jtag_poll_safe()) {
2166 /* polling is disabled currently */
2167 return ERROR_OK;
2168 }
2169
2170 /* we do not want to recurse here... */
2171 static int recursive;
2172 if (!recursive) {
2173 recursive = 1;
2174 sense_handler();
2175 /* danger! running these procedures can trigger srst assertions and power dropouts.
2176 * We need to avoid an infinite loop/recursion here and we do that by
2177 * clearing the flags after running these events.
2178 */
2179 int did_something = 0;
2180 if (runSrstAsserted) {
2181 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2182 Jim_Eval(interp, "srst_asserted");
2183 did_something = 1;
2184 }
2185 if (runSrstDeasserted) {
2186 Jim_Eval(interp, "srst_deasserted");
2187 did_something = 1;
2188 }
2189 if (runPowerDropout) {
2190 LOG_INFO("Power dropout detected, running power_dropout proc.");
2191 Jim_Eval(interp, "power_dropout");
2192 did_something = 1;
2193 }
2194 if (runPowerRestore) {
2195 Jim_Eval(interp, "power_restore");
2196 did_something = 1;
2197 }
2198
2199 if (did_something) {
2200 /* clear detect flags */
2201 sense_handler();
2202 }
2203
2204 /* clear action flags */
2205
2206 runSrstAsserted = 0;
2207 runSrstDeasserted = 0;
2208 runPowerRestore = 0;
2209 runPowerDropout = 0;
2210
2211 recursive = 0;
2212 }
2213
2214 if (backoff_times > backoff_count) {
2215 /* do not poll this time as we failed previously */
2216 backoff_count++;
2217 return ERROR_OK;
2218 }
2219 backoff_count = 0;
2220
2221 /* Poll targets for state changes unless that's globally disabled.
2222 * Skip targets that are currently disabled.
2223 */
2224 for (struct target *target = all_targets;
2225 is_jtag_poll_safe() && target;
2226 target = target->next) {
2227 if (!target->tap->enabled)
2228 continue;
2229
2230 /* only poll target if we've got power and srst isn't asserted */
2231 if (!powerDropout && !srstAsserted) {
2232 /* polling may fail silently until the target has been examined */
2233 retval = target_poll(target);
2234 if (retval != ERROR_OK) {
2235 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2236 if (backoff_times * polling_interval < 5000) {
2237 backoff_times *= 2;
2238 backoff_times++;
2239 }
2240 LOG_USER("Polling target failed, GDB will be halted. Polling again in %dms",
2241 backoff_times * polling_interval);
2242
2243 /* Tell GDB to halt the debugger. This allows the user to
2244 * run monitor commands to handle the situation.
2245 */
2246 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2247 return retval;
2248 }
2249 /* Since we succeeded, we reset backoff count */
2250 if (backoff_times > 0)
2251 LOG_USER("Polling succeeded again");
2252 backoff_times = 0;
2253 }
2254 }
2255
2256 return retval;
2257 }
2258
2259 COMMAND_HANDLER(handle_reg_command)
2260 {
2261 struct target *target;
2262 struct reg *reg = NULL;
2263 unsigned count = 0;
2264 char *value;
2265
2266 LOG_DEBUG("-");
2267
2268 target = get_current_target(CMD_CTX);
2269
2270 /* list all available registers for the current target */
2271 if (CMD_ARGC == 0) {
2272 struct reg_cache *cache = target->reg_cache;
2273
2274 count = 0;
2275 while (cache) {
2276 unsigned i;
2277
2278 command_print(CMD_CTX, "===== %s", cache->name);
2279
2280 for (i = 0, reg = cache->reg_list;
2281 i < cache->num_regs;
2282 i++, reg++, count++) {
2283 /* only print cached values if they are valid */
2284 if (reg->valid) {
2285 value = buf_to_str(reg->value,
2286 reg->size, 16);
2287 command_print(CMD_CTX,
2288 "(%i) %s (/%" PRIu32 "): 0x%s%s",
2289 count, reg->name,
2290 reg->size, value,
2291 reg->dirty
2292 ? " (dirty)"
2293 : "");
2294 free(value);
2295 } else {
2296 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
2297 count, reg->name,
2298 reg->size) ;
2299 }
2300 }
2301 cache = cache->next;
2302 }
2303
2304 return ERROR_OK;
2305 }
2306
2307 /* access a single register by its ordinal number */
2308 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9')) {
2309 unsigned num;
2310 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
2311
2312 struct reg_cache *cache = target->reg_cache;
2313 count = 0;
2314 while (cache) {
2315 unsigned i;
2316 for (i = 0; i < cache->num_regs; i++) {
2317 if (count++ == num) {
2318 reg = &cache->reg_list[i];
2319 break;
2320 }
2321 }
2322 if (reg)
2323 break;
2324 cache = cache->next;
2325 }
2326
2327 if (!reg) {
2328 command_print(CMD_CTX, "%i is out of bounds, the current target "
2329 "has only %i registers (0 - %i)", num, count, count - 1);
2330 return ERROR_OK;
2331 }
2332 } else {
2333 /* access a single register by its name */
2334 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
2335
2336 if (!reg) {
2337 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
2338 return ERROR_OK;
2339 }
2340 }
2341
2342 assert(reg != NULL); /* give clang a hint that we *know* reg is != NULL here */
2343
2344 /* display a register */
2345 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0')
2346 && (CMD_ARGV[1][0] <= '9')))) {
2347 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
2348 reg->valid = 0;
2349
2350 if (reg->valid == 0)
2351 reg->type->get(reg);
2352 value = buf_to_str(reg->value, reg->size, 16);
2353 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2354 free(value);
2355 return ERROR_OK;
2356 }
2357
2358 /* set register value */
2359 if (CMD_ARGC == 2) {
2360 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
2361 if (buf == NULL)
2362 return ERROR_FAIL;
2363 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
2364
2365 reg->type->set(reg, buf);
2366
2367 value = buf_to_str(reg->value, reg->size, 16);
2368 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2369 free(value);
2370
2371 free(buf);
2372
2373 return ERROR_OK;
2374 }
2375
2376 return ERROR_COMMAND_SYNTAX_ERROR;
2377 }
2378
2379 COMMAND_HANDLER(handle_poll_command)
2380 {
2381 int retval = ERROR_OK;
2382 struct target *target = get_current_target(CMD_CTX);
2383
2384 if (CMD_ARGC == 0) {
2385 command_print(CMD_CTX, "background polling: %s",
2386 jtag_poll_get_enabled() ? "on" : "off");
2387 command_print(CMD_CTX, "TAP: %s (%s)",
2388 target->tap->dotted_name,
2389 target->tap->enabled ? "enabled" : "disabled");
2390 if (!target->tap->enabled)
2391 return ERROR_OK;
2392 retval = target_poll(target);
2393 if (retval != ERROR_OK)
2394 return retval;
2395 retval = target_arch_state(target);
2396 if (retval != ERROR_OK)
2397 return retval;
2398 } else if (CMD_ARGC == 1) {
2399 bool enable;
2400 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
2401 jtag_poll_set_enabled(enable);
2402 } else
2403 return ERROR_COMMAND_SYNTAX_ERROR;
2404
2405 return retval;
2406 }
2407
2408 COMMAND_HANDLER(handle_wait_halt_command)
2409 {
2410 if (CMD_ARGC > 1)
2411 return ERROR_COMMAND_SYNTAX_ERROR;
2412
2413 unsigned ms = 5000;
2414 if (1 == CMD_ARGC) {
2415 int retval = parse_uint(CMD_ARGV[0], &ms);
2416 if (ERROR_OK != retval)
2417 return ERROR_COMMAND_SYNTAX_ERROR;
2418 /* convert seconds (given) to milliseconds (needed) */
2419 ms *= 1000;
2420 }
2421
2422 struct target *target = get_current_target(CMD_CTX);
2423 return target_wait_state(target, TARGET_HALTED, ms);
2424 }
2425
2426 /* wait for target state to change. The trick here is to have a low
2427 * latency for short waits and not to suck up all the CPU time
2428 * on longer waits.
2429 *
2430 * After 500ms, keep_alive() is invoked
2431 */
2432 int target_wait_state(struct target *target, enum target_state state, int ms)
2433 {
2434 int retval;
2435 long long then = 0, cur;
2436 int once = 1;
2437
2438 for (;;) {
2439 retval = target_poll(target);
2440 if (retval != ERROR_OK)
2441 return retval;
2442 if (target->state == state)
2443 break;
2444 cur = timeval_ms();
2445 if (once) {
2446 once = 0;
2447 then = timeval_ms();
2448 LOG_DEBUG("waiting for target %s...",
2449 Jim_Nvp_value2name_simple(nvp_target_state, state)->name);
2450 }
2451
2452 if (cur-then > 500)
2453 keep_alive();
2454
2455 if ((cur-then) > ms) {
2456 LOG_ERROR("timed out while waiting for target %s",
2457 Jim_Nvp_value2name_simple(nvp_target_state, state)->name);
2458 return ERROR_FAIL;
2459 }
2460 }
2461
2462 return ERROR_OK;
2463 }
2464
2465 COMMAND_HANDLER(handle_halt_command)
2466 {
2467 LOG_DEBUG("-");
2468
2469 struct target *target = get_current_target(CMD_CTX);
2470 int retval = target_halt(target);
2471 if (ERROR_OK != retval)
2472 return retval;
2473
2474 if (CMD_ARGC == 1) {
2475 unsigned wait_local;
2476 retval = parse_uint(CMD_ARGV[0], &wait_local);
2477 if (ERROR_OK != retval)
2478 return ERROR_COMMAND_SYNTAX_ERROR;
2479 if (!wait_local)
2480 return ERROR_OK;
2481 }
2482
2483 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2484 }
2485
2486 COMMAND_HANDLER(handle_soft_reset_halt_command)
2487 {
2488 struct target *target = get_current_target(CMD_CTX);
2489
2490 LOG_USER("requesting target halt and executing a soft reset");
2491
2492 target->type->soft_reset_halt(target);
2493
2494 return ERROR_OK;
2495 }
2496
2497 COMMAND_HANDLER(handle_reset_command)
2498 {
2499 if (CMD_ARGC > 1)
2500 return ERROR_COMMAND_SYNTAX_ERROR;
2501
2502 enum target_reset_mode reset_mode = RESET_RUN;
2503 if (CMD_ARGC == 1) {
2504 const Jim_Nvp *n;
2505 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
2506 if ((n->name == NULL) || (n->value == RESET_UNKNOWN))
2507 return ERROR_COMMAND_SYNTAX_ERROR;
2508 reset_mode = n->value;
2509 }
2510
2511 /* reset *all* targets */
2512 return target_process_reset(CMD_CTX, reset_mode);
2513 }
2514
2515
2516 COMMAND_HANDLER(handle_resume_command)
2517 {
2518 int current = 1;
2519 if (CMD_ARGC > 1)
2520 return ERROR_COMMAND_SYNTAX_ERROR;
2521
2522 struct target *target = get_current_target(CMD_CTX);
2523
2524 /* with no CMD_ARGV, resume from current pc, addr = 0,
2525 * with one arguments, addr = CMD_ARGV[0],
2526 * handle breakpoints, not debugging */
2527 uint32_t addr = 0;
2528 if (CMD_ARGC == 1) {
2529 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2530 current = 0;
2531 }
2532
2533 return target_resume(target, current, addr, 1, 0);
2534 }
2535
2536 COMMAND_HANDLER(handle_step_command)
2537 {
2538 if (CMD_ARGC > 1)
2539 return ERROR_COMMAND_SYNTAX_ERROR;
2540
2541 LOG_DEBUG("-");
2542
2543 /* with no CMD_ARGV, step from current pc, addr = 0,
2544 * with one argument addr = CMD_ARGV[0],
2545 * handle breakpoints, debugging */
2546 uint32_t addr = 0;
2547 int current_pc = 1;
2548 if (CMD_ARGC == 1) {
2549 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2550 current_pc = 0;
2551 }
2552
2553 struct target *target = get_current_target(CMD_CTX);
2554
2555 return target->type->step(target, current_pc, addr, 1);
2556 }
2557
2558 static void handle_md_output(struct command_context *cmd_ctx,
2559 struct target *target, uint32_t address, unsigned size,
2560 unsigned count, const uint8_t *buffer)
2561 {
2562 const unsigned line_bytecnt = 32;
2563 unsigned line_modulo = line_bytecnt / size;
2564
2565 char output[line_bytecnt * 4 + 1];
2566 unsigned output_len = 0;
2567
2568 const char *value_fmt;
2569 switch (size) {
2570 case 4:
2571 value_fmt = "%8.8x ";
2572 break;
2573 case 2:
2574 value_fmt = "%4.4x ";
2575 break;
2576 case 1:
2577 value_fmt = "%2.2x ";
2578 break;
2579 default:
2580 /* "can't happen", caller checked */
2581 LOG_ERROR("invalid memory read size: %u", size);
2582 return;
2583 }
2584
2585 for (unsigned i = 0; i < count; i++) {
2586 if (i % line_modulo == 0) {
2587 output_len += snprintf(output + output_len,
2588 sizeof(output) - output_len,
2589 "0x%8.8x: ",
2590 (unsigned)(address + (i*size)));
2591 }
2592
2593 uint32_t value = 0;
2594 const uint8_t *value_ptr = buffer + i * size;
2595 switch (size) {
2596 case 4:
2597 value = target_buffer_get_u32(target, value_ptr);
2598 break;
2599 case 2:
2600 value = target_buffer_get_u16(target, value_ptr);
2601 break;
2602 case 1:
2603 value = *value_ptr;
2604 }
2605 output_len += snprintf(output + output_len,
2606 sizeof(output) - output_len,
2607 value_fmt, value);
2608
2609 if ((i % line_modulo == line_modulo - 1) || (i == count - 1)) {
2610 command_print(cmd_ctx, "%s", output);
2611 output_len = 0;
2612 }
2613 }
2614 }
2615
2616 COMMAND_HANDLER(handle_md_command)
2617 {
2618 if (CMD_ARGC < 1)
2619 return ERROR_COMMAND_SYNTAX_ERROR;
2620
2621 unsigned size = 0;
2622 switch (CMD_NAME[2]) {
2623 case 'w':
2624 size = 4;
2625 break;
2626 case 'h':
2627 size = 2;
2628 break;
2629 case 'b':
2630 size = 1;
2631 break;
2632 default:
2633 return ERROR_COMMAND_SYNTAX_ERROR;
2634 }
2635
2636 bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
2637 int (*fn)(struct target *target,
2638 uint32_t address, uint32_t size_value, uint32_t count, uint8_t *buffer);
2639 if (physical) {
2640 CMD_ARGC--;
2641 CMD_ARGV++;
2642 fn = target_read_phys_memory;
2643 } else
2644 fn = target_read_memory;
2645 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
2646 return ERROR_COMMAND_SYNTAX_ERROR;
2647
2648 uint32_t address;
2649 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2650
2651 unsigned count = 1;
2652 if (CMD_ARGC == 2)
2653 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
2654
2655 uint8_t *buffer = calloc(count, size);
2656
2657 struct target *target = get_current_target(CMD_CTX);
2658 int retval = fn(target, address, size, count, buffer);
2659 if (ERROR_OK == retval)
2660 handle_md_output(CMD_CTX, target, address, size, count, buffer);
2661
2662 free(buffer);
2663
2664 return retval;
2665 }
2666
2667 typedef int (*target_write_fn)(struct target *target,
2668 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer);
2669
2670 static int target_write_memory_fast(struct target *target,
2671 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
2672 {
2673 return target_write_buffer(target, address, size * count, buffer);
2674 }
2675
2676 static int target_fill_mem(struct target *target,
2677 uint32_t address,
2678 target_write_fn fn,
2679 unsigned data_size,
2680 /* value */
2681 uint32_t b,
2682 /* count */
2683 unsigned c)
2684 {
2685 /* We have to write in reasonably large chunks to be able
2686 * to fill large memory areas with any sane speed */
2687 const unsigned chunk_size = 16384;
2688 uint8_t *target_buf = malloc(chunk_size * data_size);
2689 if (target_buf == NULL) {
2690 LOG_ERROR("Out of memory");
2691 return ERROR_FAIL;
2692 }
2693
2694 for (unsigned i = 0; i < chunk_size; i++) {
2695 switch (data_size) {
2696 case 4:
2697 target_buffer_set_u32(target, target_buf + i * data_size, b);
2698 break;
2699 case 2:
2700 target_buffer_set_u16(target, target_buf + i * data_size, b);
2701 break;
2702 case 1:
2703 target_buffer_set_u8(target, target_buf + i * data_size, b);
2704 break;
2705 default:
2706 exit(-1);
2707 }
2708 }
2709
2710 int retval = ERROR_OK;
2711
2712 for (unsigned x = 0; x < c; x += chunk_size) {
2713 unsigned current;
2714 current = c - x;
2715 if (current > chunk_size)
2716 current = chunk_size;
2717 retval = fn(target, address + x * data_size, data_size, current, target_buf);
2718 if (retval != ERROR_OK)
2719 break;
2720 /* avoid GDB timeouts */
2721 keep_alive();
2722 }
2723 free(target_buf);
2724
2725 return retval;
2726 }
2727
2728
2729 COMMAND_HANDLER(handle_mw_command)
2730 {
2731 if (CMD_ARGC < 2)
2732 return ERROR_COMMAND_SYNTAX_ERROR;
2733 bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
2734 target_write_fn fn;
2735 if (physical) {
2736 CMD_ARGC--;
2737 CMD_ARGV++;
2738 fn = target_write_phys_memory;
2739 } else
2740 fn = target_write_memory_fast;
2741 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
2742 return ERROR_COMMAND_SYNTAX_ERROR;
2743
2744 uint32_t address;
2745 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2746
2747 uint32_t value;
2748 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2749
2750 unsigned count = 1;
2751 if (CMD_ARGC == 3)
2752 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
2753
2754 struct target *target = get_current_target(CMD_CTX);
2755 unsigned wordsize;
2756 switch (CMD_NAME[2]) {
2757 case 'w':
2758 wordsize = 4;
2759 break;
2760 case 'h':
2761 wordsize = 2;
2762 break;
2763 case 'b':
2764 wordsize = 1;
2765 break;
2766 default:
2767 return ERROR_COMMAND_SYNTAX_ERROR;
2768 }
2769
2770 return target_fill_mem(target, address, fn, wordsize, value, count);
2771 }
2772
2773 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
2774 uint32_t *min_address, uint32_t *max_address)
2775 {
2776 if (CMD_ARGC < 1 || CMD_ARGC > 5)
2777 return ERROR_COMMAND_SYNTAX_ERROR;
2778
2779 /* a base address isn't always necessary,
2780 * default to 0x0 (i.e. don't relocate) */
2781 if (CMD_ARGC >= 2) {
2782 uint32_t addr;
2783 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2784 image->base_address = addr;
2785 image->base_address_set = 1;
2786 } else
2787 image->base_address_set = 0;
2788
2789 image->start_address_set = 0;
2790
2791 if (CMD_ARGC >= 4)
2792 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], *min_address);
2793 if (CMD_ARGC == 5) {
2794 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], *max_address);
2795 /* use size (given) to find max (required) */
2796 *max_address += *min_address;
2797 }
2798
2799 if (*min_address > *max_address)
2800 return ERROR_COMMAND_SYNTAX_ERROR;
2801
2802 return ERROR_OK;
2803 }
2804
2805 COMMAND_HANDLER(handle_load_image_command)
2806 {
2807 uint8_t *buffer;
2808 size_t buf_cnt;
2809 uint32_t image_size;
2810 uint32_t min_address = 0;
2811 uint32_t max_address = 0xffffffff;
2812 int i;
2813 struct image image;
2814
2815 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
2816 &image, &min_address, &max_address);
2817 if (ERROR_OK != retval)
2818 return retval;
2819
2820 struct target *target = get_current_target(CMD_CTX);
2821
2822 struct duration bench;
2823 duration_start(&bench);
2824
2825 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
2826 return ERROR_OK;
2827
2828 image_size = 0x0;
2829 retval = ERROR_OK;
2830 for (i = 0; i < image.num_sections; i++) {
2831 buffer = malloc(image.sections[i].size);
2832 if (buffer == NULL) {
2833 command_print(CMD_CTX,
2834 "error allocating buffer for section (%d bytes)",
2835 (int)(image.sections[i].size));
2836 break;
2837 }
2838
2839 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
2840 if (retval != ERROR_OK) {
2841 free(buffer);
2842 break;
2843 }
2844
2845 uint32_t offset = 0;
2846 uint32_t length = buf_cnt;
2847
2848 /* DANGER!!! beware of unsigned comparision here!!! */
2849
2850 if ((image.sections[i].base_address + buf_cnt >= min_address) &&
2851 (image.sections[i].base_address < max_address)) {
2852
2853 if (image.sections[i].base_address < min_address) {
2854 /* clip addresses below */
2855 offset += min_address-image.sections[i].base_address;
2856 length -= offset;
2857 }
2858
2859 if (image.sections[i].base_address + buf_cnt > max_address)
2860 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2861
2862 retval = target_write_buffer(target,
2863 image.sections[i].base_address + offset, length, buffer + offset);
2864 if (retval != ERROR_OK) {
2865 free(buffer);
2866 break;
2867 }
2868 image_size += length;
2869 command_print(CMD_CTX, "%u bytes written at address 0x%8.8" PRIx32 "",
2870 (unsigned int)length,
2871 image.sections[i].base_address + offset);
2872 }
2873
2874 free(buffer);
2875 }
2876
2877 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
2878 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
2879 "in %fs (%0.3f KiB/s)", image_size,
2880 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2881 }
2882
2883 image_close(&image);
2884
2885 return retval;
2886
2887 }
2888
2889 COMMAND_HANDLER(handle_dump_image_command)
2890 {
2891 struct fileio fileio;
2892 uint8_t *buffer;
2893 int retval, retvaltemp;
2894 uint32_t address, size;
2895 struct duration bench;
2896 struct target *target = get_current_target(CMD_CTX);
2897
2898 if (CMD_ARGC != 3)
2899 return ERROR_COMMAND_SYNTAX_ERROR;
2900
2901 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], address);
2902 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], size);
2903
2904 uint32_t buf_size = (size > 4096) ? 4096 : size;
2905 buffer = malloc(buf_size);
2906 if (!buffer)
2907 return ERROR_FAIL;
2908
2909 retval = fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY);
2910 if (retval != ERROR_OK) {
2911 free(buffer);
2912 return retval;
2913 }
2914
2915 duration_start(&bench);
2916
2917 while (size > 0) {
2918 size_t size_written;
2919 uint32_t this_run_size = (size > buf_size) ? buf_size : size;
2920 retval = target_read_buffer(target, address, this_run_size, buffer);
2921 if (retval != ERROR_OK)
2922 break;
2923
2924 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2925 if (retval != ERROR_OK)
2926 break;
2927
2928 size -= this_run_size;
2929 address += this_run_size;
2930 }
2931
2932 free(buffer);
2933
2934 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
2935 int filesize;
2936 retval = fileio_size(&fileio, &filesize);
2937 if (retval != ERROR_OK)
2938 return retval;
2939 command_print(CMD_CTX,
2940 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize,
2941 duration_elapsed(&bench), duration_kbps(&bench, filesize));
2942 }
2943
2944 retvaltemp = fileio_close(&fileio);
2945 if (retvaltemp != ERROR_OK)
2946 return retvaltemp;
2947
2948 return retval;
2949 }
2950
2951 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
2952 {
2953 uint8_t *buffer;
2954 size_t buf_cnt;
2955 uint32_t image_size;
2956 int i;
2957 int retval;
2958 uint32_t checksum = 0;
2959 uint32_t mem_checksum = 0;
2960
2961 struct image image;
2962
2963 struct target *target = get_current_target(CMD_CTX);
2964
2965 if (CMD_ARGC < 1)
2966 return ERROR_COMMAND_SYNTAX_ERROR;
2967
2968 if (!target) {
2969 LOG_ERROR("no target selected");
2970 return ERROR_FAIL;
2971 }
2972
2973 struct duration bench;
2974 duration_start(&bench);
2975
2976 if (CMD_ARGC >= 2) {
2977 uint32_t addr;
2978 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2979 image.base_address = addr;
2980 image.base_address_set = 1;
2981 } else {
2982 image.base_address_set = 0;
2983 image.base_address = 0x0;
2984 }
2985
2986 image.start_address_set = 0;
2987
2988 retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL);
2989 if (retval != ERROR_OK)
2990 return retval;
2991
2992 image_size = 0x0;
2993 int diffs = 0;
2994 retval = ERROR_OK;
2995 for (i = 0; i < image.num_sections; i++) {
2996 buffer = malloc(image.sections[i].size);
2997 if (buffer == NULL) {
2998 command_print(CMD_CTX,
2999 "error allocating buffer for section (%d bytes)",
3000 (int)(image.sections[i].size));
3001 break;
3002 }
3003 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
3004 if (retval != ERROR_OK) {
3005 free(buffer);
3006 break;
3007 }
3008
3009 if (verify) {
3010 /* calculate checksum of image */
3011 retval = image_calculate_checksum(buffer, buf_cnt, &checksum);
3012 if (retval != ERROR_OK) {
3013 free(buffer);
3014 break;
3015 }
3016
3017 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
3018 if (retval != ERROR_OK) {
3019 free(buffer);
3020 break;
3021 }
3022
3023 if (checksum != mem_checksum) {
3024 /* failed crc checksum, fall back to a binary compare */
3025 uint8_t *data;
3026
3027 if (diffs == 0)
3028 LOG_ERROR("checksum mismatch - attempting binary compare");
3029
3030 data = (uint8_t *)malloc(buf_cnt);
3031
3032 /* Can we use 32bit word accesses? */
3033 int size = 1;
3034 int count = buf_cnt;
3035 if ((count % 4) == 0) {
3036 size *= 4;
3037 count /= 4;
3038 }
3039 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
3040 if (retval == ERROR_OK) {
3041 uint32_t t;
3042 for (t = 0; t < buf_cnt; t++) {
3043 if (data[t] != buffer[t]) {
3044 command_print(CMD_CTX,
3045 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3046 diffs,
3047 (unsigned)(t + image.sections[i].base_address),
3048 data[t],
3049 buffer[t]);
3050 if (diffs++ >= 127) {
3051 command_print(CMD_CTX, "More than 128 errors, the rest are not printed.");
3052 free(data);
3053 free(buffer);
3054 goto done;
3055 }
3056 }
3057 keep_alive();
3058 }
3059 }
3060 free(data);
3061 }
3062 } else {
3063 command_print(CMD_CTX, "address 0x%08" PRIx32 " length 0x%08zx",
3064 image.sections[i].base_address,
3065 buf_cnt);
3066 }
3067
3068 free(buffer);
3069 image_size += buf_cnt;
3070 }
3071 if (diffs > 0)
3072 command_print(CMD_CTX, "No more differences found.");
3073 done:
3074 if (diffs > 0)
3075 retval = ERROR_FAIL;
3076 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
3077 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
3078 "in %fs (%0.3f KiB/s)", image_size,
3079 duration_elapsed(&bench), duration_kbps(&bench, image_size));
3080 }
3081
3082 image_close(&image);
3083
3084 return retval;
3085 }
3086
3087 COMMAND_HANDLER(handle_verify_image_command)
3088 {
3089 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 1);
3090 }
3091
3092 COMMAND_HANDLER(handle_test_image_command)
3093 {
3094 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 0);
3095 }
3096
3097 static int handle_bp_command_list(struct command_context *cmd_ctx)
3098 {
3099 struct target *target = get_current_target(cmd_ctx);
3100 struct breakpoint *breakpoint = target->breakpoints;
3101 while (breakpoint) {
3102 if (breakpoint->type == BKPT_SOFT) {
3103 char *buf = buf_to_str(breakpoint->orig_instr,
3104 breakpoint->length, 16);
3105 command_print(cmd_ctx, "IVA breakpoint: 0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
3106 breakpoint->address,
3107 breakpoint->length,
3108 breakpoint->set, buf);
3109 free(buf);
3110 } else {
3111 if ((breakpoint->address == 0) && (breakpoint->asid != 0))
3112 command_print(cmd_ctx, "Context breakpoint: 0x%8.8" PRIx32 ", 0x%x, %i",
3113 breakpoint->asid,
3114 breakpoint->length, breakpoint->set);
3115 else if ((breakpoint->address != 0) && (breakpoint->asid != 0)) {
3116 command_print(cmd_ctx, "Hybrid breakpoint(IVA): 0x%8.8" PRIx32 ", 0x%x, %i",
3117 breakpoint->address,
3118 breakpoint->length, breakpoint->set);
3119 command_print(cmd_ctx, "\t|--->linked with ContextID: 0x%8.8" PRIx32,
3120 breakpoint->asid);
3121 } else
3122 command_print(cmd_ctx, "Breakpoint(IVA): 0x%8.8" PRIx32 ", 0x%x, %i",
3123 breakpoint->address,
3124 breakpoint->length, breakpoint->set);
3125 }
3126
3127 breakpoint = breakpoint->next;
3128 }
3129 return ERROR_OK;
3130 }
3131
3132 static int handle_bp_command_set(struct command_context *cmd_ctx,
3133 uint32_t addr, uint32_t asid, uint32_t length, int hw)
3134 {
3135 struct target *target = get_current_target(cmd_ctx);
3136
3137 if (asid == 0) {
3138 int retval = breakpoint_add(target, addr, length, hw);
3139 if (ERROR_OK == retval)
3140 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
3141 else {
3142 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3143 return retval;
3144 }
3145 } else if (addr == 0) {
3146 int retval = context_breakpoint_add(target, asid, length, hw);
3147 if (ERROR_OK == retval)
3148 command_print(cmd_ctx, "Context breakpoint set at 0x%8.8" PRIx32 "", asid);
3149 else {
3150 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3151 return retval;
3152 }
3153 } else {
3154 int retval = hybrid_breakpoint_add(target, addr, asid, length, hw);
3155 if (ERROR_OK == retval)
3156 command_print(cmd_ctx, "Hybrid breakpoint set at 0x%8.8" PRIx32 "", asid);
3157 else {
3158 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3159 return retval;
3160 }
3161 }
3162 return ERROR_OK;
3163 }
3164
3165 COMMAND_HANDLER(handle_bp_command)
3166 {
3167 uint32_t addr;
3168 uint32_t asid;
3169 uint32_t length;
3170 int hw = BKPT_SOFT;
3171
3172 switch (CMD_ARGC) {
3173 case 0:
3174 return handle_bp_command_list(CMD_CTX);