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