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