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