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