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