bf3669192ea7dc92f65ed2848d89fcfde0b19b62
[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
1052 if (retval != ERROR_OK) {
1053 /* abort flash write algorithm on target */
1054 target_write_u32(target, wp_addr, 0);
1055 }
1056
1057 int retval2 = target_wait_algorithm(target, num_mem_params, mem_params,
1058 num_reg_params, reg_params,
1059 exit_point,
1060 10000,
1061 arch_info);
1062
1063 if (retval2 != ERROR_OK) {
1064 LOG_ERROR("error waiting for target flash write algorithm");
1065 retval = retval2;
1066 }
1067
1068 if (retval == ERROR_OK) {
1069 /* check if algorithm set rp = 0 after fifo writer loop finished */
1070 retval = target_read_u32(target, rp_addr, &rp);
1071 if (retval == ERROR_OK && rp == 0) {
1072 LOG_ERROR("flash write algorithm aborted by target");
1073 retval = ERROR_FLASH_OPERATION_FAILED;
1074 }
1075 }
1076
1077 return retval;
1078 }
1079
1080 int target_read_memory(struct target *target,
1081 target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
1082 {
1083 if (!target_was_examined(target)) {
1084 LOG_ERROR("Target not examined yet");
1085 return ERROR_FAIL;
1086 }
1087 if (!target->type->read_memory) {
1088 LOG_ERROR("Target %s doesn't support read_memory", target_name(target));
1089 return ERROR_FAIL;
1090 }
1091 return target->type->read_memory(target, address, size, count, buffer);
1092 }
1093
1094 int target_read_phys_memory(struct target *target,
1095 target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
1096 {
1097 if (!target_was_examined(target)) {
1098 LOG_ERROR("Target not examined yet");
1099 return ERROR_FAIL;
1100 }
1101 if (!target->type->read_phys_memory) {
1102 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target));
1103 return ERROR_FAIL;
1104 }
1105 return target->type->read_phys_memory(target, address, size, count, buffer);
1106 }
1107
1108 int target_write_memory(struct target *target,
1109 target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
1110 {
1111 if (!target_was_examined(target)) {
1112 LOG_ERROR("Target not examined yet");
1113 return ERROR_FAIL;
1114 }
1115 if (!target->type->write_memory) {
1116 LOG_ERROR("Target %s doesn't support write_memory", target_name(target));
1117 return ERROR_FAIL;
1118 }
1119 return target->type->write_memory(target, address, size, count, buffer);
1120 }
1121
1122 int target_write_phys_memory(struct target *target,
1123 target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
1124 {
1125 if (!target_was_examined(target)) {
1126 LOG_ERROR("Target not examined yet");
1127 return ERROR_FAIL;
1128 }
1129 if (!target->type->write_phys_memory) {
1130 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target));
1131 return ERROR_FAIL;
1132 }
1133 return target->type->write_phys_memory(target, address, size, count, buffer);
1134 }
1135
1136 int target_add_breakpoint(struct target *target,
1137 struct breakpoint *breakpoint)
1138 {
1139 if ((target->state != TARGET_HALTED) && (breakpoint->type != BKPT_HARD)) {
1140 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target));
1141 return ERROR_TARGET_NOT_HALTED;
1142 }
1143 return target->type->add_breakpoint(target, breakpoint);
1144 }
1145
1146 int target_add_context_breakpoint(struct target *target,
1147 struct breakpoint *breakpoint)
1148 {
1149 if (target->state != TARGET_HALTED) {
1150 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target));
1151 return ERROR_TARGET_NOT_HALTED;
1152 }
1153 return target->type->add_context_breakpoint(target, breakpoint);
1154 }
1155
1156 int target_add_hybrid_breakpoint(struct target *target,
1157 struct breakpoint *breakpoint)
1158 {
1159 if (target->state != TARGET_HALTED) {
1160 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target));
1161 return ERROR_TARGET_NOT_HALTED;
1162 }
1163 return target->type->add_hybrid_breakpoint(target, breakpoint);
1164 }
1165
1166 int target_remove_breakpoint(struct target *target,
1167 struct breakpoint *breakpoint)
1168 {
1169 return target->type->remove_breakpoint(target, breakpoint);
1170 }
1171
1172 int target_add_watchpoint(struct target *target,
1173 struct watchpoint *watchpoint)
1174 {
1175 if (target->state != TARGET_HALTED) {
1176 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target));
1177 return ERROR_TARGET_NOT_HALTED;
1178 }
1179 return target->type->add_watchpoint(target, watchpoint);
1180 }
1181 int target_remove_watchpoint(struct target *target,
1182 struct watchpoint *watchpoint)
1183 {
1184 return target->type->remove_watchpoint(target, watchpoint);
1185 }
1186 int target_hit_watchpoint(struct target *target,
1187 struct watchpoint **hit_watchpoint)
1188 {
1189 if (target->state != TARGET_HALTED) {
1190 LOG_WARNING("target %s is not halted (hit watchpoint)", target->cmd_name);
1191 return ERROR_TARGET_NOT_HALTED;
1192 }
1193
1194 if (target->type->hit_watchpoint == NULL) {
1195 /* For backward compatible, if hit_watchpoint is not implemented,
1196 * return ERROR_FAIL such that gdb_server will not take the nonsense
1197 * information. */
1198 return ERROR_FAIL;
1199 }
1200
1201 return target->type->hit_watchpoint(target, hit_watchpoint);
1202 }
1203
1204 const char *target_get_gdb_arch(struct target *target)
1205 {
1206 if (target->type->get_gdb_arch == NULL)
1207 return NULL;
1208 return target->type->get_gdb_arch(target);
1209 }
1210
1211 int target_get_gdb_reg_list(struct target *target,
1212 struct reg **reg_list[], int *reg_list_size,
1213 enum target_register_class reg_class)
1214 {
1215 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size, reg_class);
1216 }
1217
1218 bool target_supports_gdb_connection(struct target *target)
1219 {
1220 /*
1221 * based on current code, we can simply exclude all the targets that
1222 * don't provide get_gdb_reg_list; this could change with new targets.
1223 */
1224 return !!target->type->get_gdb_reg_list;
1225 }
1226
1227 int target_step(struct target *target,
1228 int current, target_addr_t address, int handle_breakpoints)
1229 {
1230 return target->type->step(target, current, address, handle_breakpoints);
1231 }
1232
1233 int target_get_gdb_fileio_info(struct target *target, struct gdb_fileio_info *fileio_info)
1234 {
1235 if (target->state != TARGET_HALTED) {
1236 LOG_WARNING("target %s is not halted (gdb fileio)", target->cmd_name);
1237 return ERROR_TARGET_NOT_HALTED;
1238 }
1239 return target->type->get_gdb_fileio_info(target, fileio_info);
1240 }
1241
1242 int target_gdb_fileio_end(struct target *target, int retcode, int fileio_errno, bool ctrl_c)
1243 {
1244 if (target->state != TARGET_HALTED) {
1245 LOG_WARNING("target %s is not halted (gdb fileio end)", target->cmd_name);
1246 return ERROR_TARGET_NOT_HALTED;
1247 }
1248 return target->type->gdb_fileio_end(target, retcode, fileio_errno, ctrl_c);
1249 }
1250
1251 int target_profiling(struct target *target, uint32_t *samples,
1252 uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
1253 {
1254 if (target->state != TARGET_HALTED) {
1255 LOG_WARNING("target %s is not halted (profiling)", target->cmd_name);
1256 return ERROR_TARGET_NOT_HALTED;
1257 }
1258 return target->type->profiling(target, samples, max_num_samples,
1259 num_samples, seconds);
1260 }
1261
1262 /**
1263 * Reset the @c examined flag for the given target.
1264 * Pure paranoia -- targets are zeroed on allocation.
1265 */
1266 static void target_reset_examined(struct target *target)
1267 {
1268 target->examined = false;
1269 }
1270
1271 static int handle_target(void *priv);
1272
1273 static int target_init_one(struct command_context *cmd_ctx,
1274 struct target *target)
1275 {
1276 target_reset_examined(target);
1277
1278 struct target_type *type = target->type;
1279 if (type->examine == NULL)
1280 type->examine = default_examine;
1281
1282 if (type->check_reset == NULL)
1283 type->check_reset = default_check_reset;
1284
1285 assert(type->init_target != NULL);
1286
1287 int retval = type->init_target(cmd_ctx, target);
1288 if (ERROR_OK != retval) {
1289 LOG_ERROR("target '%s' init failed", target_name(target));
1290 return retval;
1291 }
1292
1293 /* Sanity-check MMU support ... stub in what we must, to help
1294 * implement it in stages, but warn if we need to do so.
1295 */
1296 if (type->mmu) {
1297 if (type->virt2phys == NULL) {
1298 LOG_ERROR("type '%s' is missing virt2phys", type->name);
1299 type->virt2phys = identity_virt2phys;
1300 }
1301 } else {
1302 /* Make sure no-MMU targets all behave the same: make no
1303 * distinction between physical and virtual addresses, and
1304 * ensure that virt2phys() is always an identity mapping.
1305 */
1306 if (type->write_phys_memory || type->read_phys_memory || type->virt2phys)
1307 LOG_WARNING("type '%s' has bad MMU hooks", type->name);
1308
1309 type->mmu = no_mmu;
1310 type->write_phys_memory = type->write_memory;
1311 type->read_phys_memory = type->read_memory;
1312 type->virt2phys = identity_virt2phys;
1313 }
1314
1315 if (target->type->read_buffer == NULL)
1316 target->type->read_buffer = target_read_buffer_default;
1317
1318 if (target->type->write_buffer == NULL)
1319 target->type->write_buffer = target_write_buffer_default;
1320
1321 if (target->type->get_gdb_fileio_info == NULL)
1322 target->type->get_gdb_fileio_info = target_get_gdb_fileio_info_default;
1323
1324 if (target->type->gdb_fileio_end == NULL)
1325 target->type->gdb_fileio_end = target_gdb_fileio_end_default;
1326
1327 if (target->type->profiling == NULL)
1328 target->type->profiling = target_profiling_default;
1329
1330 return ERROR_OK;
1331 }
1332
1333 static int target_init(struct command_context *cmd_ctx)
1334 {
1335 struct target *target;
1336 int retval;
1337
1338 for (target = all_targets; target; target = target->next) {
1339 retval = target_init_one(cmd_ctx, target);
1340 if (ERROR_OK != retval)
1341 return retval;
1342 }
1343
1344 if (!all_targets)
1345 return ERROR_OK;
1346
1347 retval = target_register_user_commands(cmd_ctx);
1348 if (ERROR_OK != retval)
1349 return retval;
1350
1351 retval = target_register_timer_callback(&handle_target,
1352 polling_interval, 1, cmd_ctx->interp);
1353 if (ERROR_OK != retval)
1354 return retval;
1355
1356 return ERROR_OK;
1357 }
1358
1359 COMMAND_HANDLER(handle_target_init_command)
1360 {
1361 int retval;
1362
1363 if (CMD_ARGC != 0)
1364 return ERROR_COMMAND_SYNTAX_ERROR;
1365
1366 static bool target_initialized;
1367 if (target_initialized) {
1368 LOG_INFO("'target init' has already been called");
1369 return ERROR_OK;
1370 }
1371 target_initialized = true;
1372
1373 retval = command_run_line(CMD_CTX, "init_targets");
1374 if (ERROR_OK != retval)
1375 return retval;
1376
1377 retval = command_run_line(CMD_CTX, "init_target_events");
1378 if (ERROR_OK != retval)
1379 return retval;
1380
1381 retval = command_run_line(CMD_CTX, "init_board");
1382 if (ERROR_OK != retval)
1383 return retval;
1384
1385 LOG_DEBUG("Initializing targets...");
1386 return target_init(CMD_CTX);
1387 }
1388
1389 int target_register_event_callback(int (*callback)(struct target *target,
1390 enum target_event event, void *priv), void *priv)
1391 {
1392 struct target_event_callback **callbacks_p = &target_event_callbacks;
1393
1394 if (callback == NULL)
1395 return ERROR_COMMAND_SYNTAX_ERROR;
1396
1397 if (*callbacks_p) {
1398 while ((*callbacks_p)->next)
1399 callbacks_p = &((*callbacks_p)->next);
1400 callbacks_p = &((*callbacks_p)->next);
1401 }
1402
1403 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
1404 (*callbacks_p)->callback = callback;
1405 (*callbacks_p)->priv = priv;
1406 (*callbacks_p)->next = NULL;
1407
1408 return ERROR_OK;
1409 }
1410
1411 int target_register_reset_callback(int (*callback)(struct target *target,
1412 enum target_reset_mode reset_mode, void *priv), void *priv)
1413 {
1414 struct target_reset_callback *entry;
1415
1416 if (callback == NULL)
1417 return ERROR_COMMAND_SYNTAX_ERROR;
1418
1419 entry = malloc(sizeof(struct target_reset_callback));
1420 if (entry == NULL) {
1421 LOG_ERROR("error allocating buffer for reset callback entry");
1422 return ERROR_COMMAND_SYNTAX_ERROR;
1423 }
1424
1425 entry->callback = callback;
1426 entry->priv = priv;
1427 list_add(&entry->list, &target_reset_callback_list);
1428
1429
1430 return ERROR_OK;
1431 }
1432
1433 int target_register_trace_callback(int (*callback)(struct target *target,
1434 size_t len, uint8_t *data, void *priv), void *priv)
1435 {
1436 struct target_trace_callback *entry;
1437
1438 if (callback == NULL)
1439 return ERROR_COMMAND_SYNTAX_ERROR;
1440
1441 entry = malloc(sizeof(struct target_trace_callback));
1442 if (entry == NULL) {
1443 LOG_ERROR("error allocating buffer for trace callback entry");
1444 return ERROR_COMMAND_SYNTAX_ERROR;
1445 }
1446
1447 entry->callback = callback;
1448 entry->priv = priv;
1449 list_add(&entry->list, &target_trace_callback_list);
1450
1451
1452 return ERROR_OK;
1453 }
1454
1455 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
1456 {
1457 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
1458
1459 if (callback == NULL)
1460 return ERROR_COMMAND_SYNTAX_ERROR;
1461
1462 if (*callbacks_p) {
1463 while ((*callbacks_p)->next)
1464 callbacks_p = &((*callbacks_p)->next);
1465 callbacks_p = &((*callbacks_p)->next);
1466 }
1467
1468 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
1469 (*callbacks_p)->callback = callback;
1470 (*callbacks_p)->periodic = periodic;
1471 (*callbacks_p)->time_ms = time_ms;
1472 (*callbacks_p)->removed = false;
1473
1474 gettimeofday(&(*callbacks_p)->when, NULL);
1475 timeval_add_time(&(*callbacks_p)->when, 0, time_ms * 1000);
1476
1477 (*callbacks_p)->priv = priv;
1478 (*callbacks_p)->next = NULL;
1479
1480 return ERROR_OK;
1481 }
1482
1483 int target_unregister_event_callback(int (*callback)(struct target *target,
1484 enum target_event event, void *priv), void *priv)
1485 {
1486 struct target_event_callback **p = &target_event_callbacks;
1487 struct target_event_callback *c = target_event_callbacks;
1488
1489 if (callback == NULL)
1490 return ERROR_COMMAND_SYNTAX_ERROR;
1491
1492 while (c) {
1493 struct target_event_callback *next = c->next;
1494 if ((c->callback == callback) && (c->priv == priv)) {
1495 *p = next;
1496 free(c);
1497 return ERROR_OK;
1498 } else
1499 p = &(c->next);
1500 c = next;
1501 }
1502
1503 return ERROR_OK;
1504 }
1505
1506 int target_unregister_reset_callback(int (*callback)(struct target *target,
1507 enum target_reset_mode reset_mode, void *priv), void *priv)
1508 {
1509 struct target_reset_callback *entry;
1510
1511 if (callback == NULL)
1512 return ERROR_COMMAND_SYNTAX_ERROR;
1513
1514 list_for_each_entry(entry, &target_reset_callback_list, list) {
1515 if (entry->callback == callback && entry->priv == priv) {
1516 list_del(&entry->list);
1517 free(entry);
1518 break;
1519 }
1520 }
1521
1522 return ERROR_OK;
1523 }
1524
1525 int target_unregister_trace_callback(int (*callback)(struct target *target,
1526 size_t len, uint8_t *data, void *priv), void *priv)
1527 {
1528 struct target_trace_callback *entry;
1529
1530 if (callback == NULL)
1531 return ERROR_COMMAND_SYNTAX_ERROR;
1532
1533 list_for_each_entry(entry, &target_trace_callback_list, list) {
1534 if (entry->callback == callback && entry->priv == priv) {
1535 list_del(&entry->list);
1536 free(entry);
1537 break;
1538 }
1539 }
1540
1541 return ERROR_OK;
1542 }
1543
1544 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
1545 {
1546 if (callback == NULL)
1547 return ERROR_COMMAND_SYNTAX_ERROR;
1548
1549 for (struct target_timer_callback *c = target_timer_callbacks;
1550 c; c = c->next) {
1551 if ((c->callback == callback) && (c->priv == priv)) {
1552 c->removed = true;
1553 return ERROR_OK;
1554 }
1555 }
1556
1557 return ERROR_FAIL;
1558 }
1559
1560 int target_call_event_callbacks(struct target *target, enum target_event event)
1561 {
1562 struct target_event_callback *callback = target_event_callbacks;
1563 struct target_event_callback *next_callback;
1564
1565 if (event == TARGET_EVENT_HALTED) {
1566 /* execute early halted first */
1567 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1568 }
1569
1570 LOG_DEBUG("target event %i (%s)", event,
1571 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
1572
1573 target_handle_event(target, event);
1574
1575 while (callback) {
1576 next_callback = callback->next;
1577 callback->callback(target, event, callback->priv);
1578 callback = next_callback;
1579 }
1580
1581 return ERROR_OK;
1582 }
1583
1584 int target_call_reset_callbacks(struct target *target, enum target_reset_mode reset_mode)
1585 {
1586 struct target_reset_callback *callback;
1587
1588 LOG_DEBUG("target reset %i (%s)", reset_mode,
1589 Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode)->name);
1590
1591 list_for_each_entry(callback, &target_reset_callback_list, list)
1592 callback->callback(target, reset_mode, callback->priv);
1593
1594 return ERROR_OK;
1595 }
1596
1597 int target_call_trace_callbacks(struct target *target, size_t len, uint8_t *data)
1598 {
1599 struct target_trace_callback *callback;
1600
1601 list_for_each_entry(callback, &target_trace_callback_list, list)
1602 callback->callback(target, len, data, callback->priv);
1603
1604 return ERROR_OK;
1605 }
1606
1607 static int target_timer_callback_periodic_restart(
1608 struct target_timer_callback *cb, struct timeval *now)
1609 {
1610 cb->when = *now;
1611 timeval_add_time(&cb->when, 0, cb->time_ms * 1000L);
1612 return ERROR_OK;
1613 }
1614
1615 static int target_call_timer_callback(struct target_timer_callback *cb,
1616 struct timeval *now)
1617 {
1618 cb->callback(cb->priv);
1619
1620 if (cb->periodic)
1621 return target_timer_callback_periodic_restart(cb, now);
1622
1623 return target_unregister_timer_callback(cb->callback, cb->priv);
1624 }
1625
1626 static int target_call_timer_callbacks_check_time(int checktime)
1627 {
1628 static bool callback_processing;
1629
1630 /* Do not allow nesting */
1631 if (callback_processing)
1632 return ERROR_OK;
1633
1634 callback_processing = true;
1635
1636 keep_alive();
1637
1638 struct timeval now;
1639 gettimeofday(&now, NULL);
1640
1641 /* Store an address of the place containing a pointer to the
1642 * next item; initially, that's a standalone "root of the
1643 * list" variable. */
1644 struct target_timer_callback **callback = &target_timer_callbacks;
1645 while (*callback) {
1646 if ((*callback)->removed) {
1647 struct target_timer_callback *p = *callback;
1648 *callback = (*callback)->next;
1649 free(p);
1650 continue;
1651 }
1652
1653 bool call_it = (*callback)->callback &&
1654 ((!checktime && (*callback)->periodic) ||
1655 timeval_compare(&now, &(*callback)->when) >= 0);
1656
1657 if (call_it)
1658 target_call_timer_callback(*callback, &now);
1659
1660 callback = &(*callback)->next;
1661 }
1662
1663 callback_processing = false;
1664 return ERROR_OK;
1665 }
1666
1667 int target_call_timer_callbacks(void)
1668 {
1669 return target_call_timer_callbacks_check_time(1);
1670 }
1671
1672 /* invoke periodic callbacks immediately */
1673 int target_call_timer_callbacks_now(void)
1674 {
1675 return target_call_timer_callbacks_check_time(0);
1676 }
1677
1678 /* Prints the working area layout for debug purposes */
1679 static void print_wa_layout(struct target *target)
1680 {
1681 struct working_area *c = target->working_areas;
1682
1683 while (c) {
1684 LOG_DEBUG("%c%c " TARGET_ADDR_FMT "-" TARGET_ADDR_FMT " (%" PRIu32 " bytes)",
1685 c->backup ? 'b' : ' ', c->free ? ' ' : '*',
1686 c->address, c->address + c->size - 1, c->size);
1687 c = c->next;
1688 }
1689 }
1690
1691 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1692 static void target_split_working_area(struct working_area *area, uint32_t size)
1693 {
1694 assert(area->free); /* Shouldn't split an allocated area */
1695 assert(size <= area->size); /* Caller should guarantee this */
1696
1697 /* Split only if not already the right size */
1698 if (size < area->size) {
1699 struct working_area *new_wa = malloc(sizeof(*new_wa));
1700
1701 if (new_wa == NULL)
1702 return;
1703
1704 new_wa->next = area->next;
1705 new_wa->size = area->size - size;
1706 new_wa->address = area->address + size;
1707 new_wa->backup = NULL;
1708 new_wa->user = NULL;
1709 new_wa->free = true;
1710
1711 area->next = new_wa;
1712 area->size = size;
1713
1714 /* If backup memory was allocated to this area, it has the wrong size
1715 * now so free it and it will be reallocated if/when needed */
1716 if (area->backup) {
1717 free(area->backup);
1718 area->backup = NULL;
1719 }
1720 }
1721 }
1722
1723 /* Merge all adjacent free areas into one */
1724 static void target_merge_working_areas(struct target *target)
1725 {
1726 struct working_area *c = target->working_areas;
1727
1728 while (c && c->next) {
1729 assert(c->next->address == c->address + c->size); /* This is an invariant */
1730
1731 /* Find two adjacent free areas */
1732 if (c->free && c->next->free) {
1733 /* Merge the last into the first */
1734 c->size += c->next->size;
1735
1736 /* Remove the last */
1737 struct working_area *to_be_freed = c->next;
1738 c->next = c->next->next;
1739 if (to_be_freed->backup)
1740 free(to_be_freed->backup);
1741 free(to_be_freed);
1742
1743 /* If backup memory was allocated to the remaining area, it's has
1744 * the wrong size now */
1745 if (c->backup) {
1746 free(c->backup);
1747 c->backup = NULL;
1748 }
1749 } else {
1750 c = c->next;
1751 }
1752 }
1753 }
1754
1755 int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
1756 {
1757 /* Reevaluate working area address based on MMU state*/
1758 if (target->working_areas == NULL) {
1759 int retval;
1760 int enabled;
1761
1762 retval = target->type->mmu(target, &enabled);
1763 if (retval != ERROR_OK)
1764 return retval;
1765
1766 if (!enabled) {
1767 if (target->working_area_phys_spec) {
1768 LOG_DEBUG("MMU disabled, using physical "
1769 "address for working memory " TARGET_ADDR_FMT,
1770 target->working_area_phys);
1771 target->working_area = target->working_area_phys;
1772 } else {
1773 LOG_ERROR("No working memory available. "
1774 "Specify -work-area-phys to target.");
1775 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1776 }
1777 } else {
1778 if (target->working_area_virt_spec) {
1779 LOG_DEBUG("MMU enabled, using virtual "
1780 "address for working memory " TARGET_ADDR_FMT,
1781 target->working_area_virt);
1782 target->working_area = target->working_area_virt;
1783 } else {
1784 LOG_ERROR("No working memory available. "
1785 "Specify -work-area-virt to target.");
1786 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1787 }
1788 }
1789
1790 /* Set up initial working area on first call */
1791 struct working_area *new_wa = malloc(sizeof(*new_wa));
1792 if (new_wa) {
1793 new_wa->next = NULL;
1794 new_wa->size = target->working_area_size & ~3UL; /* 4-byte align */
1795 new_wa->address = target->working_area;
1796 new_wa->backup = NULL;
1797 new_wa->user = NULL;
1798 new_wa->free = true;
1799 }
1800
1801 target->working_areas = new_wa;
1802 }
1803
1804 /* only allocate multiples of 4 byte */
1805 if (size % 4)
1806 size = (size + 3) & (~3UL);
1807
1808 struct working_area *c = target->working_areas;
1809
1810 /* Find the first large enough working area */
1811 while (c) {
1812 if (c->free && c->size >= size)
1813 break;
1814 c = c->next;
1815 }
1816
1817 if (c == NULL)
1818 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1819
1820 /* Split the working area into the requested size */
1821 target_split_working_area(c, size);
1822
1823 LOG_DEBUG("allocated new working area of %" PRIu32 " bytes at address " TARGET_ADDR_FMT,
1824 size, c->address);
1825
1826 if (target->backup_working_area) {
1827 if (c->backup == NULL) {
1828 c->backup = malloc(c->size);
1829 if (c->backup == NULL)
1830 return ERROR_FAIL;
1831 }
1832
1833 int retval = target_read_memory(target, c->address, 4, c->size / 4, c->backup);
1834 if (retval != ERROR_OK)
1835 return retval;
1836 }
1837
1838 /* mark as used, and return the new (reused) area */
1839 c->free = false;
1840 *area = c;
1841
1842 /* user pointer */
1843 c->user = area;
1844
1845 print_wa_layout(target);
1846
1847 return ERROR_OK;
1848 }
1849
1850 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1851 {
1852 int retval;
1853
1854 retval = target_alloc_working_area_try(target, size, area);
1855 if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
1856 LOG_WARNING("not enough working area available(requested %"PRIu32")", size);
1857 return retval;
1858
1859 }
1860
1861 static int target_restore_working_area(struct target *target, struct working_area *area)
1862 {
1863 int retval = ERROR_OK;
1864
1865 if (target->backup_working_area && area->backup != NULL) {
1866 retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup);
1867 if (retval != ERROR_OK)
1868 LOG_ERROR("failed to restore %" PRIu32 " bytes of working area at address " TARGET_ADDR_FMT,
1869 area->size, area->address);
1870 }
1871
1872 return retval;
1873 }
1874
1875 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1876 static int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1877 {
1878 int retval = ERROR_OK;
1879
1880 if (area->free)
1881 return retval;
1882
1883 if (restore) {
1884 retval = target_restore_working_area(target, area);
1885 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1886 if (retval != ERROR_OK)
1887 return retval;
1888 }
1889
1890 area->free = true;
1891
1892 LOG_DEBUG("freed %" PRIu32 " bytes of working area at address " TARGET_ADDR_FMT,
1893 area->size, area->address);
1894
1895 /* mark user pointer invalid */
1896 /* TODO: Is this really safe? It points to some previous caller's memory.
1897 * How could we know that the area pointer is still in that place and not
1898 * some other vital data? What's the purpose of this, anyway? */
1899 *area->user = NULL;
1900 area->user = NULL;
1901
1902 target_merge_working_areas(target);
1903
1904 print_wa_layout(target);
1905
1906 return retval;
1907 }
1908
1909 int target_free_working_area(struct target *target, struct working_area *area)
1910 {
1911 return target_free_working_area_restore(target, area, 1);
1912 }
1913
1914 static void target_destroy(struct target *target)
1915 {
1916 if (target->type->deinit_target)
1917 target->type->deinit_target(target);
1918
1919 if (target->semihosting)
1920 free(target->semihosting);
1921
1922 jtag_unregister_event_callback(jtag_enable_callback, target);
1923
1924 struct target_event_action *teap = target->event_action;
1925 while (teap) {
1926 struct target_event_action *next = teap->next;
1927 Jim_DecrRefCount(teap->interp, teap->body);
1928 free(teap);
1929 teap = next;
1930 }
1931
1932 target_free_all_working_areas(target);
1933 /* Now we have none or only one working area marked as free */
1934 if (target->working_areas) {
1935 free(target->working_areas->backup);
1936 free(target->working_areas);
1937 }
1938
1939 /* release the targets SMP list */
1940 if (target->smp) {
1941 struct target_list *head = target->head;
1942 while (head != NULL) {
1943 struct target_list *pos = head->next;
1944 head->target->smp = 0;
1945 free(head);
1946 head = pos;
1947 }
1948 target->smp = 0;
1949 }
1950
1951 free(target->gdb_port_override);
1952 free(target->type);
1953 free(target->trace_info);
1954 free(target->fileio_info);
1955 free(target->cmd_name);
1956 free(target);
1957 }
1958
1959 void target_quit(void)
1960 {
1961 struct target_event_callback *pe = target_event_callbacks;
1962 while (pe) {
1963 struct target_event_callback *t = pe->next;
1964 free(pe);
1965 pe = t;
1966 }
1967 target_event_callbacks = NULL;
1968
1969 struct target_timer_callback *pt = target_timer_callbacks;
1970 while (pt) {
1971 struct target_timer_callback *t = pt->next;
1972 free(pt);
1973 pt = t;
1974 }
1975 target_timer_callbacks = NULL;
1976
1977 for (struct target *target = all_targets; target;) {
1978 struct target *tmp;
1979
1980 tmp = target->next;
1981 target_destroy(target);
1982 target = tmp;
1983 }
1984
1985 all_targets = NULL;
1986 }
1987
1988 /* free resources and restore memory, if restoring memory fails,
1989 * free up resources anyway
1990 */
1991 static void target_free_all_working_areas_restore(struct target *target, int restore)
1992 {
1993 struct working_area *c = target->working_areas;
1994
1995 LOG_DEBUG("freeing all working areas");
1996
1997 /* Loop through all areas, restoring the allocated ones and marking them as free */
1998 while (c) {
1999 if (!c->free) {
2000 if (restore)
2001 target_restore_working_area(target, c);
2002 c->free = true;
2003 *c->user = NULL; /* Same as above */
2004 c->user = NULL;
2005 }
2006 c = c->next;
2007 }
2008
2009 /* Run a merge pass to combine all areas into one */
2010 target_merge_working_areas(target);
2011
2012 print_wa_layout(target);
2013 }
2014
2015 void target_free_all_working_areas(struct target *target)
2016 {
2017 target_free_all_working_areas_restore(target, 1);
2018 }
2019
2020 /* Find the largest number of bytes that can be allocated */
2021 uint32_t target_get_working_area_avail(struct target *target)
2022 {
2023 struct working_area *c = target->working_areas;
2024 uint32_t max_size = 0;
2025
2026 if (c == NULL)
2027 return target->working_area_size;
2028
2029 while (c) {
2030 if (c->free && max_size < c->size)
2031 max_size = c->size;
2032
2033 c = c->next;
2034 }
2035
2036 return max_size;
2037 }
2038
2039 int target_arch_state(struct target *target)
2040 {
2041 int retval;
2042 if (target == NULL) {
2043 LOG_WARNING("No target has been configured");
2044 return ERROR_OK;
2045 }
2046
2047 if (target->state != TARGET_HALTED)
2048 return ERROR_OK;
2049
2050 retval = target->type->arch_state(target);
2051 return retval;
2052 }
2053
2054 static int target_get_gdb_fileio_info_default(struct target *target,
2055 struct gdb_fileio_info *fileio_info)
2056 {
2057 /* If target does not support semi-hosting function, target
2058 has no need to provide .get_gdb_fileio_info callback.
2059 It just return ERROR_FAIL and gdb_server will return "Txx"
2060 as target halted every time. */
2061 return ERROR_FAIL;
2062 }
2063
2064 static int target_gdb_fileio_end_default(struct target *target,
2065 int retcode, int fileio_errno, bool ctrl_c)
2066 {
2067 return ERROR_OK;
2068 }
2069
2070 static int target_profiling_default(struct target *target, uint32_t *samples,
2071 uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
2072 {
2073 struct timeval timeout, now;
2074
2075 gettimeofday(&timeout, NULL);
2076 timeval_add_time(&timeout, seconds, 0);
2077
2078 LOG_INFO("Starting profiling. Halting and resuming the"
2079 " target as often as we can...");
2080
2081 uint32_t sample_count = 0;
2082 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2083 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
2084
2085 int retval = ERROR_OK;
2086 for (;;) {
2087 target_poll(target);
2088 if (target->state == TARGET_HALTED) {
2089 uint32_t t = buf_get_u32(reg->value, 0, 32);
2090 samples[sample_count++] = t;
2091 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2092 retval = target_resume(target, 1, 0, 0, 0);
2093 target_poll(target);
2094 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2095 } else if (target->state == TARGET_RUNNING) {
2096 /* We want to quickly sample the PC. */
2097 retval = target_halt(target);
2098 } else {
2099 LOG_INFO("Target not halted or running");
2100 retval = ERROR_OK;
2101 break;
2102 }
2103
2104 if (retval != ERROR_OK)
2105 break;
2106
2107 gettimeofday(&now, NULL);
2108 if ((sample_count >= max_num_samples) || timeval_compare(&now, &timeout) >= 0) {
2109 LOG_INFO("Profiling completed. %" PRIu32 " samples.", sample_count);
2110 break;
2111 }
2112 }
2113
2114 *num_samples = sample_count;
2115 return retval;
2116 }
2117
2118 /* Single aligned words are guaranteed to use 16 or 32 bit access
2119 * mode respectively, otherwise data is handled as quickly as
2120 * possible
2121 */
2122 int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
2123 {
2124 LOG_DEBUG("writing buffer of %" PRIi32 " byte at " TARGET_ADDR_FMT,
2125 size, address);
2126
2127 if (!target_was_examined(target)) {
2128 LOG_ERROR("Target not examined yet");
2129 return ERROR_FAIL;
2130 }
2131
2132 if (size == 0)
2133 return ERROR_OK;
2134
2135 if ((address + size - 1) < address) {
2136 /* GDB can request this when e.g. PC is 0xfffffffc */
2137 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT ", 0x%08" PRIx32 ")",
2138 address,
2139 size);
2140 return ERROR_FAIL;
2141 }
2142
2143 return target->type->write_buffer(target, address, size, buffer);
2144 }
2145
2146 static int target_write_buffer_default(struct target *target,
2147 target_addr_t address, uint32_t count, const uint8_t *buffer)
2148 {
2149 uint32_t size;
2150
2151 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2152 * will have something to do with the size we leave to it. */
2153 for (size = 1; size < 4 && count >= size * 2 + (address & size); size *= 2) {
2154 if (address & size) {
2155 int retval = target_write_memory(target, address, size, 1, buffer);
2156 if (retval != ERROR_OK)
2157 return retval;
2158 address += size;
2159 count -= size;
2160 buffer += size;
2161 }
2162 }
2163
2164 /* Write the data with as large access size as possible. */
2165 for (; size > 0; size /= 2) {
2166 uint32_t aligned = count - count % size;
2167 if (aligned > 0) {
2168 int retval = target_write_memory(target, address, size, aligned / size, buffer);
2169 if (retval != ERROR_OK)
2170 return retval;
2171 address += aligned;
2172 count -= aligned;
2173 buffer += aligned;
2174 }
2175 }
2176
2177 return ERROR_OK;
2178 }
2179
2180 /* Single aligned words are guaranteed to use 16 or 32 bit access
2181 * mode respectively, otherwise data is handled as quickly as
2182 * possible
2183 */
2184 int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
2185 {
2186 LOG_DEBUG("reading buffer of %" PRIi32 " byte at " TARGET_ADDR_FMT,
2187 size, address);
2188
2189 if (!target_was_examined(target)) {
2190 LOG_ERROR("Target not examined yet");
2191 return ERROR_FAIL;
2192 }
2193
2194 if (size == 0)
2195 return ERROR_OK;
2196
2197 if ((address + size - 1) < address) {
2198 /* GDB can request this when e.g. PC is 0xfffffffc */
2199 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT ", 0x%08" PRIx32 ")",
2200 address,
2201 size);
2202 return ERROR_FAIL;
2203 }
2204
2205 return target->type->read_buffer(target, address, size, buffer);
2206 }
2207
2208 static int target_read_buffer_default(struct target *target, target_addr_t address, uint32_t count, uint8_t *buffer)
2209 {
2210 uint32_t size;
2211
2212 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2213 * will have something to do with the size we leave to it. */
2214 for (size = 1; size < 4 && count >= size * 2 + (address & size); size *= 2) {
2215 if (address & size) {
2216 int retval = target_read_memory(target, address, size, 1, buffer);
2217 if (retval != ERROR_OK)
2218 return retval;
2219 address += size;
2220 count -= size;
2221 buffer += size;
2222 }
2223 }
2224
2225 /* Read the data with as large access size as possible. */
2226 for (; size > 0; size /= 2) {
2227 uint32_t aligned = count - count % size;
2228 if (aligned > 0) {
2229 int retval = target_read_memory(target, address, size, aligned / size, buffer);
2230 if (retval != ERROR_OK)
2231 return retval;
2232 address += aligned;
2233 count -= aligned;
2234 buffer += aligned;
2235 }
2236 }
2237
2238 return ERROR_OK;
2239 }
2240
2241 int target_checksum_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t* crc)
2242 {
2243 uint8_t *buffer;
2244 int retval;
2245 uint32_t i;
2246 uint32_t checksum = 0;
2247 if (!target_was_examined(target)) {
2248 LOG_ERROR("Target not examined yet");
2249 return ERROR_FAIL;
2250 }
2251
2252 retval = target->type->checksum_memory(target, address, size, &checksum);
2253 if (retval != ERROR_OK) {
2254 buffer = malloc(size);
2255 if (buffer == NULL) {
2256 LOG_ERROR("error allocating buffer for section (%" PRId32 " bytes)", size);
2257 return ERROR_COMMAND_SYNTAX_ERROR;
2258 }
2259 retval = target_read_buffer(target, address, size, buffer);
2260 if (retval != ERROR_OK) {
2261 free(buffer);
2262 return retval;
2263 }
2264
2265 /* convert to target endianness */
2266 for (i = 0; i < (size/sizeof(uint32_t)); i++) {
2267 uint32_t target_data;
2268 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
2269 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
2270 }
2271
2272 retval = image_calculate_checksum(buffer, size, &checksum);
2273 free(buffer);
2274 }
2275
2276 *crc = checksum;
2277
2278 return retval;
2279 }
2280
2281 int target_blank_check_memory(struct target *target,
2282 struct target_memory_check_block *blocks, int num_blocks,
2283 uint8_t erased_value)
2284 {
2285 if (!target_was_examined(target)) {
2286 LOG_ERROR("Target not examined yet");
2287 return ERROR_FAIL;
2288 }
2289
2290 if (target->type->blank_check_memory == NULL)
2291 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
2292
2293 return target->type->blank_check_memory(target, blocks, num_blocks, erased_value);
2294 }
2295
2296 int target_read_u64(struct target *target, target_addr_t address, uint64_t *value)
2297 {
2298 uint8_t value_buf[8];
2299 if (!target_was_examined(target)) {
2300 LOG_ERROR("Target not examined yet");
2301 return ERROR_FAIL;
2302 }
2303
2304 int retval = target_read_memory(target, address, 8, 1, value_buf);
2305
2306 if (retval == ERROR_OK) {
2307 *value = target_buffer_get_u64(target, value_buf);
2308 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64 "",
2309 address,
2310 *value);
2311 } else {
2312 *value = 0x0;
2313 LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2314 address);
2315 }
2316
2317 return retval;
2318 }
2319
2320 int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
2321 {
2322 uint8_t value_buf[4];
2323 if (!target_was_examined(target)) {
2324 LOG_ERROR("Target not examined yet");
2325 return ERROR_FAIL;
2326 }
2327
2328 int retval = target_read_memory(target, address, 4, 1, value_buf);
2329
2330 if (retval == ERROR_OK) {
2331 *value = target_buffer_get_u32(target, value_buf);
2332 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32 "",
2333 address,
2334 *value);
2335 } else {
2336 *value = 0x0;
2337 LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2338 address);
2339 }
2340
2341 return retval;
2342 }
2343
2344 int target_read_u16(struct target *target, target_addr_t address, uint16_t *value)
2345 {
2346 uint8_t value_buf[2];
2347 if (!target_was_examined(target)) {
2348 LOG_ERROR("Target not examined yet");
2349 return ERROR_FAIL;
2350 }
2351
2352 int retval = target_read_memory(target, address, 2, 1, value_buf);
2353
2354 if (retval == ERROR_OK) {
2355 *value = target_buffer_get_u16(target, value_buf);
2356 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%4.4" PRIx16,
2357 address,
2358 *value);
2359 } else {
2360 *value = 0x0;
2361 LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2362 address);
2363 }
2364
2365 return retval;
2366 }
2367
2368 int target_read_u8(struct target *target, target_addr_t address, uint8_t *value)
2369 {
2370 if (!target_was_examined(target)) {
2371 LOG_ERROR("Target not examined yet");
2372 return ERROR_FAIL;
2373 }
2374
2375 int retval = target_read_memory(target, address, 1, 1, value);
2376
2377 if (retval == ERROR_OK) {
2378 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2379 address,
2380 *value);
2381 } else {
2382 *value = 0x0;
2383 LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2384 address);
2385 }
2386
2387 return retval;
2388 }
2389
2390 int target_write_u64(struct target *target, target_addr_t address, uint64_t value)
2391 {
2392 int retval;
2393 uint8_t value_buf[8];
2394 if (!target_was_examined(target)) {
2395 LOG_ERROR("Target not examined yet");
2396 return ERROR_FAIL;
2397 }
2398
2399 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64 "",
2400 address,
2401 value);
2402
2403 target_buffer_set_u64(target, value_buf, value);
2404 retval = target_write_memory(target, address, 8, 1, value_buf);
2405 if (retval != ERROR_OK)
2406 LOG_DEBUG("failed: %i", retval);
2407
2408 return retval;
2409 }
2410
2411 int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
2412 {
2413 int retval;
2414 uint8_t value_buf[4];
2415 if (!target_was_examined(target)) {
2416 LOG_ERROR("Target not examined yet");
2417 return ERROR_FAIL;
2418 }
2419
2420 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32 "",
2421 address,
2422 value);
2423
2424 target_buffer_set_u32(target, value_buf, value);
2425 retval = target_write_memory(target, address, 4, 1, value_buf);
2426 if (retval != ERROR_OK)
2427 LOG_DEBUG("failed: %i", retval);
2428
2429 return retval;
2430 }
2431
2432 int target_write_u16(struct target *target, target_addr_t address, uint16_t value)
2433 {
2434 int retval;
2435 uint8_t value_buf[2];
2436 if (!target_was_examined(target)) {
2437 LOG_ERROR("Target not examined yet");
2438 return ERROR_FAIL;
2439 }
2440
2441 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx16,
2442 address,
2443 value);
2444
2445 target_buffer_set_u16(target, value_buf, value);
2446 retval = target_write_memory(target, address, 2, 1, value_buf);
2447 if (retval != ERROR_OK)
2448 LOG_DEBUG("failed: %i", retval);
2449
2450 return retval;
2451 }
2452
2453 int target_write_u8(struct target *target, target_addr_t address, uint8_t value)
2454 {
2455 int retval;
2456 if (!target_was_examined(target)) {
2457 LOG_ERROR("Target not examined yet");
2458 return ERROR_FAIL;
2459 }
2460
2461 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2462 address, value);
2463
2464 retval = target_write_memory(target, address, 1, 1, &value);
2465 if (retval != ERROR_OK)
2466 LOG_DEBUG("failed: %i", retval);
2467
2468 return retval;
2469 }
2470
2471 int target_write_phys_u64(struct target *target, target_addr_t address, uint64_t value)
2472 {
2473 int retval;
2474 uint8_t value_buf[8];
2475 if (!target_was_examined(target)) {
2476 LOG_ERROR("Target not examined yet");
2477 return ERROR_FAIL;
2478 }
2479
2480 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64 "",
2481 address,
2482 value);
2483
2484 target_buffer_set_u64(target, value_buf, value);
2485 retval = target_write_phys_memory(target, address, 8, 1, value_buf);
2486 if (retval != ERROR_OK)
2487 LOG_DEBUG("failed: %i", retval);
2488
2489 return retval;
2490 }
2491
2492 int target_write_phys_u32(struct target *target, target_addr_t address, uint32_t value)
2493 {
2494 int retval;
2495 uint8_t value_buf[4];
2496 if (!target_was_examined(target)) {
2497 LOG_ERROR("Target not examined yet");
2498 return ERROR_FAIL;
2499 }
2500
2501 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32 "",
2502 address,
2503 value);
2504
2505 target_buffer_set_u32(target, value_buf, value);
2506 retval = target_write_phys_memory(target, address, 4, 1, value_buf);
2507 if (retval != ERROR_OK)
2508 LOG_DEBUG("failed: %i", retval);
2509
2510 return retval;
2511 }
2512
2513 int target_write_phys_u16(struct target *target, target_addr_t address, uint16_t value)
2514 {
2515 int retval;
2516 uint8_t value_buf[2];
2517 if (!target_was_examined(target)) {
2518 LOG_ERROR("Target not examined yet");
2519 return ERROR_FAIL;
2520 }
2521
2522 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx16,
2523 address,
2524 value);
2525
2526 target_buffer_set_u16(target, value_buf, value);
2527 retval = target_write_phys_memory(target, address, 2, 1, value_buf);
2528 if (retval != ERROR_OK)
2529 LOG_DEBUG("failed: %i", retval);
2530
2531 return retval;
2532 }
2533
2534 int target_write_phys_u8(struct target *target, target_addr_t address, uint8_t value)
2535 {
2536 int retval;
2537 if (!target_was_examined(target)) {
2538 LOG_ERROR("Target not examined yet");
2539 return ERROR_FAIL;
2540 }
2541
2542 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2543 address, value);
2544
2545 retval = target_write_phys_memory(target, address, 1, 1, &value);
2546 if (retval != ERROR_OK)
2547 LOG_DEBUG("failed: %i", retval);
2548
2549 return retval;
2550 }
2551
2552 static int find_target(struct command_context *cmd_ctx, const char *name)
2553 {
2554 struct target *target = get_target(name);
2555 if (target == NULL) {
2556 LOG_ERROR("Target: %s is unknown, try one of:\n", name);
2557 return ERROR_FAIL;
2558 }
2559 if (!target->tap->enabled) {
2560 LOG_USER("Target: TAP %s is disabled, "
2561 "can't be the current target\n",
2562 target->tap->dotted_name);
2563 return ERROR_FAIL;
2564 }
2565
2566 cmd_ctx->current_target = target;
2567 if (cmd_ctx->current_target_override)
2568 cmd_ctx->current_target_override = target;
2569
2570 return ERROR_OK;
2571 }
2572
2573
2574 COMMAND_HANDLER(handle_targets_command)
2575 {
2576 int retval = ERROR_OK;
2577 if (CMD_ARGC == 1) {
2578 retval = find_target(CMD_CTX, CMD_ARGV[0]);
2579 if (retval == ERROR_OK) {
2580 /* we're done! */
2581 return retval;
2582 }
2583 }
2584
2585 struct target *target = all_targets;
2586 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
2587 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
2588 while (target) {
2589 const char *state;
2590 char marker = ' ';
2591
2592 if (target->tap->enabled)
2593 state = target_state_name(target);
2594 else
2595 state = "tap-disabled";
2596
2597 if (CMD_CTX->current_target == target)
2598 marker = '*';
2599
2600 /* keep columns lined up to match the headers above */
2601 command_print(CMD_CTX,
2602 "%2d%c %-18s %-10s %-6s %-18s %s",
2603 target->target_number,
2604 marker,
2605 target_name(target),
2606 target_type_name(target),
2607 Jim_Nvp_value2name_simple(nvp_target_endian,
2608 target->endianness)->name,
2609 target->tap->dotted_name,
2610 state);
2611 target = target->next;
2612 }
2613
2614 return retval;
2615 }
2616
2617 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2618
2619 static int powerDropout;
2620 static int srstAsserted;
2621
2622 static int runPowerRestore;
2623 static int runPowerDropout;
2624 static int runSrstAsserted;
2625 static int runSrstDeasserted;
2626
2627 static int sense_handler(void)
2628 {
2629 static int prevSrstAsserted;
2630 static int prevPowerdropout;
2631
2632 int retval = jtag_power_dropout(&powerDropout);
2633 if (retval != ERROR_OK)
2634 return retval;
2635
2636 int powerRestored;
2637 powerRestored = prevPowerdropout && !powerDropout;
2638 if (powerRestored)
2639 runPowerRestore = 1;
2640
2641 int64_t current = timeval_ms();
2642 static int64_t lastPower;
2643 bool waitMore = lastPower + 2000 > current;
2644 if (powerDropout && !waitMore) {
2645 runPowerDropout = 1;
2646 lastPower = current;
2647 }
2648
2649 retval = jtag_srst_asserted(&srstAsserted);
2650 if (retval != ERROR_OK)
2651 return retval;
2652
2653 int srstDeasserted;
2654 srstDeasserted = prevSrstAsserted && !srstAsserted;
2655
2656 static int64_t lastSrst;
2657 waitMore = lastSrst + 2000 > current;
2658 if (srstDeasserted && !waitMore) {
2659 runSrstDeasserted = 1;
2660 lastSrst = current;
2661 }
2662
2663 if (!prevSrstAsserted && srstAsserted)
2664 runSrstAsserted = 1;
2665
2666 prevSrstAsserted = srstAsserted;
2667 prevPowerdropout = powerDropout;
2668
2669 if (srstDeasserted || powerRestored) {
2670 /* Other than logging the event we can't do anything here.
2671 * Issuing a reset is a particularly bad idea as we might
2672 * be inside a reset already.
2673 */
2674 }
2675
2676 return ERROR_OK;
2677 }
2678
2679 /* process target state changes */
2680 static int handle_target(void *priv)
2681 {
2682 Jim_Interp *interp = (Jim_Interp *)priv;
2683 int retval = ERROR_OK;
2684
2685 if (!is_jtag_poll_safe()) {
2686 /* polling is disabled currently */
2687 return ERROR_OK;
2688 }
2689
2690 /* we do not want to recurse here... */
2691 static int recursive;
2692 if (!recursive) {
2693 recursive = 1;
2694 sense_handler();
2695 /* danger! running these procedures can trigger srst assertions and power dropouts.
2696 * We need to avoid an infinite loop/recursion here and we do that by
2697 * clearing the flags after running these events.
2698 */
2699 int did_something = 0;
2700 if (runSrstAsserted) {
2701 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2702 Jim_Eval(interp, "srst_asserted");
2703 did_something = 1;
2704 }
2705 if (runSrstDeasserted) {
2706 Jim_Eval(interp, "srst_deasserted");
2707 did_something = 1;
2708 }
2709 if (runPowerDropout) {
2710 LOG_INFO("Power dropout detected, running power_dropout proc.");
2711 Jim_Eval(interp, "power_dropout");
2712 did_something = 1;
2713 }
2714 if (runPowerRestore) {
2715 Jim_Eval(interp, "power_restore");
2716 did_something = 1;
2717 }
2718
2719 if (did_something) {
2720 /* clear detect flags */
2721 sense_handler();
2722 }
2723
2724 /* clear action flags */
2725
2726 runSrstAsserted = 0;
2727 runSrstDeasserted = 0;
2728 runPowerRestore = 0;
2729 runPowerDropout = 0;
2730
2731 recursive = 0;
2732 }
2733
2734 /* Poll targets for state changes unless that's globally disabled.
2735 * Skip targets that are currently disabled.
2736 */
2737 for (struct target *target = all_targets;
2738 is_jtag_poll_safe() && target;
2739 target = target->next) {
2740
2741 if (!target_was_examined(target))
2742 continue;
2743
2744 if (!target->tap->enabled)
2745 continue;
2746
2747 if (target->backoff.times > target->backoff.count) {
2748 /* do not poll this time as we failed previously */
2749 target->backoff.count++;
2750 continue;
2751 }
2752 target->backoff.count = 0;
2753
2754 /* only poll target if we've got power and srst isn't asserted */
2755 if (!powerDropout && !srstAsserted) {
2756 /* polling may fail silently until the target has been examined */
2757 retval = target_poll(target);
2758 if (retval != ERROR_OK) {
2759 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2760 if (target->backoff.times * polling_interval < 5000) {
2761 target->backoff.times *= 2;
2762 target->backoff.times++;
2763 }
2764
2765 /* Tell GDB to halt the debugger. This allows the user to
2766 * run monitor commands to handle the situation.
2767 */
2768 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2769 }
2770 if (target->backoff.times > 0) {
2771 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target));
2772 target_reset_examined(target);
2773 retval = target_examine_one(target);
2774 /* Target examination could have failed due to unstable connection,
2775 * but we set the examined flag anyway to repoll it later */
2776 if (retval != ERROR_OK) {
2777 target->examined = true;
2778 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2779 target->backoff.times * polling_interval);
2780 return retval;
2781 }
2782 }
2783
2784 /* Since we succeeded, we reset backoff count */
2785 target->backoff.times = 0;
2786 }
2787 }
2788
2789 return retval;
2790 }
2791
2792 COMMAND_HANDLER(handle_reg_command)
2793 {
2794 struct target *target;
2795 struct reg *reg = NULL;
2796 unsigned count = 0;
2797 char *value;
2798
2799 LOG_DEBUG("-");
2800
2801 target = get_current_target(CMD_CTX);
2802
2803 /* list all available registers for the current target */
2804 if (CMD_ARGC == 0) {
2805 struct reg_cache *cache = target->reg_cache;
2806
2807 count = 0;
2808 while (cache) {
2809 unsigned i;
2810
2811 command_print(CMD_CTX, "===== %s", cache->name);
2812
2813 for (i = 0, reg = cache->reg_list;
2814 i < cache->num_regs;
2815 i++, reg++, count++) {
2816 if (reg->exist == false)
2817 continue;
2818 /* only print cached values if they are valid */
2819 if (reg->valid) {
2820 value = buf_to_str(reg->value,
2821 reg->size, 16);
2822 command_print(CMD_CTX,
2823 "(%i) %s (/%" PRIu32 "): 0x%s%s",
2824 count, reg->name,
2825 reg->size, value,
2826 reg->dirty
2827 ? " (dirty)"
2828 : "");
2829 free(value);
2830 } else {
2831 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
2832 count, reg->name,
2833 reg->size) ;
2834 }
2835 }
2836 cache = cache->next;
2837 }
2838
2839 return ERROR_OK;
2840 }
2841
2842 /* access a single register by its ordinal number */
2843 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9')) {
2844 unsigned num;
2845 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
2846
2847 struct reg_cache *cache = target->reg_cache;
2848 count = 0;
2849 while (cache) {
2850 unsigned i;
2851 for (i = 0; i < cache->num_regs; i++) {
2852 if (count++ == num) {
2853 reg = &cache->reg_list[i];
2854 break;
2855 }
2856 }
2857 if (reg)
2858 break;
2859 cache = cache->next;
2860 }
2861
2862 if (!reg) {
2863 command_print(CMD_CTX, "%i is out of bounds, the current target "
2864 "has only %i registers (0 - %i)", num, count, count - 1);
2865 return ERROR_OK;
2866 }
2867 } else {
2868 /* access a single register by its name */
2869 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
2870
2871 if (!reg)
2872 goto not_found;
2873 }
2874
2875 assert(reg != NULL); /* give clang a hint that we *know* reg is != NULL here */
2876
2877 if (!reg->exist)
2878 goto not_found;
2879
2880 /* display a register */
2881 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0')
2882 && (CMD_ARGV[1][0] <= '9')))) {
2883 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
2884 reg->valid = 0;
2885
2886 if (reg->valid == 0)
2887 reg->type->get(reg);
2888 value = buf_to_str(reg->value, reg->size, 16);
2889 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2890 free(value);
2891 return ERROR_OK;
2892 }
2893
2894 /* set register value */
2895 if (CMD_ARGC == 2) {
2896 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
2897 if (buf == NULL)
2898 return ERROR_FAIL;
2899 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
2900
2901 reg->type->set(reg, buf);
2902
2903 value = buf_to_str(reg->value, reg->size, 16);
2904 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2905 free(value);
2906
2907 free(buf);
2908
2909 return ERROR_OK;
2910 }
2911
2912 return ERROR_COMMAND_SYNTAX_ERROR;
2913
2914 not_found:
2915 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
2916 return ERROR_OK;
2917 }
2918
2919 COMMAND_HANDLER(handle_poll_command)
2920 {
2921 int retval = ERROR_OK;
2922 struct target *target = get_current_target(CMD_CTX);
2923
2924 if (CMD_ARGC == 0) {
2925 command_print(CMD_CTX, "background polling: %s",
2926 jtag_poll_get_enabled() ? "on" : "off");
2927 command_print(CMD_CTX, "TAP: %s (%s)",
2928 target->tap->dotted_name,
2929 target->tap->enabled ? "enabled" : "disabled");
2930 if (!target->tap->enabled)
2931 return ERROR_OK;
2932 retval = target_poll(target);
2933 if (retval != ERROR_OK)
2934 return retval;
2935 retval = target_arch_state(target);
2936 if (retval != ERROR_OK)
2937 return retval;
2938 } else if (CMD_ARGC == 1) {
2939 bool enable;
2940 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
2941 jtag_poll_set_enabled(enable);
2942 } else
2943 return ERROR_COMMAND_SYNTAX_ERROR;
2944
2945 return retval;
2946 }
2947
2948 COMMAND_HANDLER(handle_wait_halt_command)
2949 {
2950 if (CMD_ARGC > 1)
2951 return ERROR_COMMAND_SYNTAX_ERROR;
2952
2953 unsigned ms = DEFAULT_HALT_TIMEOUT;
2954 if (1 == CMD_ARGC) {
2955 int retval = parse_uint(CMD_ARGV[0], &ms);
2956 if (ERROR_OK != retval)
2957 return ERROR_COMMAND_SYNTAX_ERROR;
2958 }
2959
2960 struct target *target = get_current_target(CMD_CTX);
2961 return target_wait_state(target, TARGET_HALTED, ms);
2962 }
2963
2964 /* wait for target state to change. The trick here is to have a low
2965 * latency for short waits and not to suck up all the CPU time
2966 * on longer waits.
2967 *
2968 * After 500ms, keep_alive() is invoked
2969 */
2970 int target_wait_state(struct target *target, enum target_state state, int ms)
2971 {
2972 int retval;
2973 int64_t then = 0, cur;
2974 bool once = true;
2975
2976 for (;;) {
2977 retval = target_poll(target);
2978 if (retval != ERROR_OK)
2979 return retval;
2980 if (target->state == state)
2981 break;
2982 cur = timeval_ms();
2983 if (once) {
2984 once = false;
2985 then = timeval_ms();
2986 LOG_DEBUG("waiting for target %s...",
2987 Jim_Nvp_value2name_simple(nvp_target_state, state)->name);
2988 }
2989
2990 if (cur-then > 500)
2991 keep_alive();
2992
2993 if ((cur-then) > ms) {
2994 LOG_ERROR("timed out while waiting for target %s",
2995 Jim_Nvp_value2name_simple(nvp_target_state, state)->name);
2996 return ERROR_FAIL;
2997 }
2998 }
2999
3000 return ERROR_OK;
3001 }
3002
3003 COMMAND_HANDLER(handle_halt_command)
3004 {
3005 LOG_DEBUG("-");
3006
3007 struct target *target = get_current_target(CMD_CTX);
3008
3009 target->verbose_halt_msg = true;
3010
3011 int retval = target_halt(target);
3012 if (ERROR_OK != retval)
3013 return retval;
3014
3015 if (CMD_ARGC == 1) {
3016 unsigned wait_local;
3017 retval = parse_uint(CMD_ARGV[0], &wait_local);
3018 if (ERROR_OK != retval)
3019 return ERROR_COMMAND_SYNTAX_ERROR;
3020 if (!wait_local)
3021 return ERROR_OK;
3022 }
3023
3024 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
3025 }
3026
3027 COMMAND_HANDLER(handle_soft_reset_halt_command)
3028 {
3029 struct target *target = get_current_target(CMD_CTX);
3030
3031 LOG_USER("requesting target halt and executing a soft reset");
3032
3033 target_soft_reset_halt(target);
3034
3035 return ERROR_OK;
3036 }
3037
3038 COMMAND_HANDLER(handle_reset_command)
3039 {
3040 if (CMD_ARGC > 1)
3041 return ERROR_COMMAND_SYNTAX_ERROR;
3042
3043 enum target_reset_mode reset_mode = RESET_RUN;
3044 if (CMD_ARGC == 1) {
3045 const Jim_Nvp *n;
3046 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
3047 if ((n->name == NULL) || (n->value == RESET_UNKNOWN))
3048 return ERROR_COMMAND_SYNTAX_ERROR;
3049 reset_mode = n->value;
3050 }
3051
3052 /* reset *all* targets */
3053 return target_process_reset(CMD_CTX, reset_mode);
3054 }
3055
3056
3057 COMMAND_HANDLER(handle_resume_command)
3058 {
3059 int current = 1;
3060 if (CMD_ARGC > 1)
3061 return ERROR_COMMAND_SYNTAX_ERROR;
3062
3063 struct target *target = get_current_target(CMD_CTX);
3064
3065 /* with no CMD_ARGV, resume from current pc, addr = 0,
3066 * with one arguments, addr = CMD_ARGV[0],
3067 * handle breakpoints, not debugging */
3068 target_addr_t addr = 0;
3069 if (CMD_ARGC == 1) {
3070 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
3071 current = 0;
3072 }
3073
3074 return target_resume(target, current, addr, 1, 0);
3075 }
3076
3077 COMMAND_HANDLER(handle_step_command)
3078 {
3079 if (CMD_ARGC > 1)
3080 return ERROR_COMMAND_SYNTAX_ERROR;
3081
3082 LOG_DEBUG("-");
3083
3084 /* with no CMD_ARGV, step from current pc, addr = 0,
3085 * with one argument addr = CMD_ARGV[0],
3086 * handle breakpoints, debugging */
3087 target_addr_t addr = 0;
3088 int current_pc = 1;
3089 if (CMD_ARGC == 1) {
3090 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
3091 current_pc = 0;
3092 }
3093
3094 struct target *target = get_current_target(CMD_CTX);
3095
3096 return target->type->step(target, current_pc, addr, 1);
3097 }
3098
3099 static void handle_md_output(struct command_context *cmd_ctx,
3100 struct target *target, target_addr_t address, unsigned size,
3101 unsigned count, const uint8_t *buffer)
3102 {
3103 const unsigned line_bytecnt = 32;
3104 unsigned line_modulo = line_bytecnt / size;
3105
3106 char output[line_bytecnt * 4 + 1];
3107 unsigned output_len = 0;
3108
3109 const char *value_fmt;
3110 switch (size) {
3111 case 8:
3112 value_fmt = "%16.16"PRIx64" ";
3113 break;
3114 case 4:
3115 value_fmt = "%8.8"PRIx64" ";
3116 break;
3117 case 2:
3118 value_fmt = "%4.4"PRIx64" ";
3119 break;
3120 case 1:
3121 value_fmt = "%2.2"PRIx64" ";
3122 break;
3123 default:
3124 /* "can't happen", caller checked */
3125 LOG_ERROR("invalid memory read size: %u", size);
3126 return;
3127 }
3128
3129 for (unsigned i = 0; i < count; i++) {
3130 if (i % line_modulo == 0) {
3131 output_len += snprintf(output + output_len,
3132 sizeof(output) - output_len,
3133 TARGET_ADDR_FMT ": ",
3134 (address + (i * size)));
3135 }
3136
3137 uint64_t value = 0;
3138 const uint8_t *value_ptr = buffer + i * size;
3139 switch (size) {
3140 case 8:
3141 value = target_buffer_get_u64(target, value_ptr);
3142 break;
3143 case 4:
3144 value = target_buffer_get_u32(target, value_ptr);
3145 break;
3146 case 2:
3147 value = target_buffer_get_u16(target, value_ptr);
3148 break;
3149 case 1:
3150 value = *value_ptr;
3151 }
3152 output_len += snprintf(output + output_len,
3153 sizeof(output) - output_len,
3154 value_fmt, value);
3155
3156 if ((i % line_modulo == line_modulo - 1) || (i == count - 1)) {
3157 command_print(cmd_ctx, "%s", output);
3158 output_len = 0;
3159 }
3160 }
3161 }
3162
3163 COMMAND_HANDLER(handle_md_command)
3164 {
3165 if (CMD_ARGC < 1)
3166 return ERROR_COMMAND_SYNTAX_ERROR;
3167
3168 unsigned size = 0;
3169 switch (CMD_NAME[2]) {
3170 case 'd':
3171 size = 8;
3172 break;
3173 case 'w':
3174 size = 4;
3175 break;
3176 case 'h':
3177 size = 2;
3178 break;
3179 case 'b':
3180 size = 1;
3181 break;
3182 default:
3183 return ERROR_COMMAND_SYNTAX_ERROR;
3184 }
3185
3186 bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
3187 int (*fn)(struct target *target,
3188 target_addr_t address, uint32_t size_value, uint32_t count, uint8_t *buffer);
3189 if (physical) {
3190 CMD_ARGC--;
3191 CMD_ARGV++;
3192 fn = target_read_phys_memory;
3193 } else
3194 fn = target_read_memory;
3195 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
3196 return ERROR_COMMAND_SYNTAX_ERROR;
3197
3198 target_addr_t address;
3199 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], address);
3200
3201 unsigned count = 1;
3202 if (CMD_ARGC == 2