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