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