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