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