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