target: make target re-configuration possible again
[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;
3199 case 'h':
3200 size = 2;
3201 break;
3202 case 'b':
3203 size = 1;
3204 break;
3205 default:
3206 return ERROR_COMMAND_SYNTAX_ERROR;
3207 }
3208
3209 bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
3210 int (*fn)(struct target *target,
3211 target_addr_t address, uint32_t size_value, uint32_t count, uint8_t *buffer);
3212 if (physical) {
3213 CMD_ARGC--;
3214 CMD_ARGV++;
3215 fn = target_read_phys_memory;
3216 } else
3217 fn = target_read_memory;
3218 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
3219 return ERROR_COMMAND_SYNTAX_ERROR;
3220
3221 target_addr_t address;
3222 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], address);
3223
3224 unsigned count = 1;
3225 if (CMD_ARGC == 2)
3226 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
3227
3228 uint8_t *buffer = calloc(count, size);
3229 if (buffer == NULL) {
3230 LOG_ERROR("Failed to allocate md read buffer");
3231 return ERROR_FAIL;
3232 }
3233
3234 struct target *target = get_current_target(CMD_CTX);
3235 int retval = fn(target, address, size, count, buffer);
3236 if (ERROR_OK == retval)
3237 target_handle_md_output(CMD, target, address, size, count, buffer);
3238
3239 free(buffer);
3240
3241 return retval;
3242 }
3243
3244 typedef int (*target_write_fn)(struct target *target,
3245 target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer);
3246
3247 static int target_fill_mem(struct target *target,
3248 target_addr_t address,
3249 target_write_fn fn,
3250 unsigned data_size,
3251 /* value */
3252 uint64_t b,
3253 /* count */
3254 unsigned c)
3255 {
3256 /* We have to write in reasonably large chunks to be able
3257 * to fill large memory areas with any sane speed */
3258 const unsigned chunk_size = 16384;
3259 uint8_t *target_buf = malloc(chunk_size * data_size);
3260 if (target_buf == NULL) {
3261 LOG_ERROR("Out of memory");
3262 return ERROR_FAIL;
3263 }
3264
3265 for (unsigned i = 0; i < chunk_size; i++) {
3266 switch (data_size) {
3267 case 8:
3268 target_buffer_set_u64(target, target_buf + i * data_size, b);
3269 break;
3270 case 4:
3271 target_buffer_set_u32(target, target_buf + i * data_size, b);
3272 break;
3273 case 2:
3274 target_buffer_set_u16(target, target_buf + i * data_size, b);
3275 break;
3276 case 1:
3277 target_buffer_set_u8(target, target_buf + i * data_size, b);
3278 break;
3279 default:
3280 exit(-1);
3281 }
3282 }
3283
3284 int retval = ERROR_OK;
3285
3286 for (unsigned x = 0; x < c; x += chunk_size) {
3287 unsigned current;
3288 current = c - x;
3289 if (current > chunk_size)
3290 current = chunk_size;
3291 retval = fn(target, address + x * data_size, data_size, current, target_buf);
3292 if (retval != ERROR_OK)
3293 break;
3294 /* avoid GDB timeouts */
3295 keep_alive();
3296 }
3297 free(target_buf);
3298
3299 return retval;
3300 }
3301
3302
3303 COMMAND_HANDLER(handle_mw_command)
3304 {
3305 if (CMD_ARGC < 2)
3306 return ERROR_COMMAND_SYNTAX_ERROR;
3307 bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
3308 target_write_fn fn;
3309 if (physical) {
3310 CMD_ARGC--;
3311 CMD_ARGV++;
3312 fn = target_write_phys_memory;
3313 } else
3314 fn = target_write_memory;
3315 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
3316 return ERROR_COMMAND_SYNTAX_ERROR;
3317
3318 target_addr_t address;
3319 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], address);
3320
3321 target_addr_t value;
3322 COMMAND_PARSE_ADDRESS(CMD_ARGV[1], value);
3323
3324 unsigned count = 1;
3325 if (CMD_ARGC == 3)
3326 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
3327
3328 struct target *target = get_current_target(CMD_CTX);
3329 unsigned wordsize;
3330 switch (CMD_NAME[2]) {
3331 case 'd':
3332 wordsize = 8;
3333 break;
3334 case 'w':
3335 wordsize = 4;
3336 break;
3337 case 'h':
3338 wordsize = 2;
3339 break;
3340 case 'b':
3341 wordsize = 1;
3342 break;
3343 default:
3344 return ERROR_COMMAND_SYNTAX_ERROR;
3345 }
3346
3347 return target_fill_mem(target, address, fn, wordsize, value, count);
3348 }
3349
3350 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
3351 target_addr_t *min_address, target_addr_t *max_address)
3352 {
3353 if (CMD_ARGC < 1 || CMD_ARGC > 5)
3354 return ERROR_COMMAND_SYNTAX_ERROR;
3355
3356 /* a base address isn't always necessary,
3357 * default to 0x0 (i.e. don't relocate) */
3358 if (CMD_ARGC >= 2) {
3359 target_addr_t addr;
3360 COMMAND_PARSE_ADDRESS(CMD_ARGV[1], addr);
3361 image->base_address = addr;
3362 image->base_address_set = 1;
3363 } else
3364 image->base_address_set = 0;
3365
3366 image->start_address_set = 0;
3367
3368 if (CMD_ARGC >= 4)
3369 COMMAND_PARSE_ADDRESS(CMD_ARGV[3], *min_address);
3370 if (CMD_ARGC == 5) {
3371 COMMAND_PARSE_ADDRESS(CMD_ARGV[4], *max_address);
3372 /* use size (given) to find max (required) */
3373 *max_address += *min_address;
3374 }
3375
3376 if (*min_address > *max_address)
3377 return ERROR_COMMAND_SYNTAX_ERROR;
3378
3379 return ERROR_OK;
3380 }
3381
3382 COMMAND_HANDLER(handle_load_image_command)
3383 {
3384 uint8_t *buffer;
3385 size_t buf_cnt;
3386 uint32_t image_size;
3387 target_addr_t min_address = 0;
3388 target_addr_t max_address = -1;
3389 int i;
3390 struct image image;
3391
3392 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
3393 &image, &min_address, &max_address);
3394 if (ERROR_OK != retval)
3395 return retval;
3396
3397 struct target *target = get_current_target(CMD_CTX);
3398
3399 struct duration bench;
3400 duration_start(&bench);
3401
3402 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
3403 return ERROR_FAIL;
3404
3405 image_size = 0x0;
3406 retval = ERROR_OK;
3407 for (i = 0; i < image.num_sections; i++) {
3408 buffer = malloc(image.sections[i].size);
3409 if (buffer == NULL) {
3410 command_print(CMD,
3411 "error allocating buffer for section (%d bytes)",
3412 (int)(image.sections[i].size));
3413 retval = ERROR_FAIL;
3414 break;
3415 }
3416
3417 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
3418 if (retval != ERROR_OK) {
3419 free(buffer);
3420 break;
3421 }
3422
3423 uint32_t offset = 0;
3424 uint32_t length = buf_cnt;
3425
3426 /* DANGER!!! beware of unsigned comparision here!!! */
3427
3428 if ((image.sections[i].base_address + buf_cnt >= min_address) &&
3429 (image.sections[i].base_address < max_address)) {
3430
3431 if (image.sections[i].base_address < min_address) {
3432 /* clip addresses below */
3433 offset += min_address-image.sections[i].base_address;
3434 length -= offset;
3435 }
3436
3437 if (image.sections[i].base_address + buf_cnt > max_address)
3438 length -= (image.sections[i].base_address + buf_cnt)-max_address;
3439
3440 retval = target_write_buffer(target,
3441 image.sections[i].base_address + offset, length, buffer + offset);
3442 if (retval != ERROR_OK) {
3443 free(buffer);
3444 break;
3445 }
3446 image_size += length;
3447 command_print(CMD, "%u bytes written at address " TARGET_ADDR_FMT "",
3448 (unsigned int)length,
3449 image.sections[i].base_address + offset);
3450 }
3451
3452 free(buffer);
3453 }
3454
3455 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
3456 command_print(CMD, "downloaded %" PRIu32 " bytes "
3457 "in %fs (%0.3f KiB/s)", image_size,
3458 duration_elapsed(&bench), duration_kbps(&bench, image_size));
3459 }
3460
3461 image_close(&image);
3462
3463 return retval;
3464
3465 }
3466
3467 COMMAND_HANDLER(handle_dump_image_command)
3468 {
3469 struct fileio *fileio;
3470 uint8_t *buffer;
3471 int retval, retvaltemp;
3472 target_addr_t address, size;
3473 struct duration bench;
3474 struct target *target = get_current_target(CMD_CTX);
3475
3476 if (CMD_ARGC != 3)
3477 return ERROR_COMMAND_SYNTAX_ERROR;
3478
3479 COMMAND_PARSE_ADDRESS(CMD_ARGV[1], address);
3480 COMMAND_PARSE_ADDRESS(CMD_ARGV[2], size);
3481
3482 uint32_t buf_size = (size > 4096) ? 4096 : size;
3483 buffer = malloc(buf_size);
3484 if (!buffer)
3485 return ERROR_FAIL;
3486
3487 retval = fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY);
3488 if (retval != ERROR_OK) {
3489 free(buffer);
3490 return retval;
3491 }
3492
3493 duration_start(&bench);
3494
3495 while (size > 0) {
3496 size_t size_written;
3497 uint32_t this_run_size = (size > buf_size) ? buf_size : size;
3498 retval = target_read_buffer(target, address, this_run_size, buffer);
3499 if (retval != ERROR_OK)
3500 break;
3501
3502 retval = fileio_write(fileio, this_run_size, buffer, &size_written);
3503 if (retval != ERROR_OK)
3504 break;
3505
3506 size -= this_run_size;
3507 address += this_run_size;
3508 }
3509
3510 free(buffer);
3511
3512 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
3513 size_t filesize;
3514 retval = fileio_size(fileio, &filesize);
3515 if (retval != ERROR_OK)
3516 return retval;
3517 command_print(CMD,
3518 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize,
3519 duration_elapsed(&bench), duration_kbps(&bench, filesize));
3520 }
3521
3522 retvaltemp = fileio_close(fileio);
3523 if (retvaltemp != ERROR_OK)
3524 return retvaltemp;
3525
3526 return retval;
3527 }
3528
3529 enum verify_mode {
3530 IMAGE_TEST = 0,
3531 IMAGE_VERIFY = 1,
3532 IMAGE_CHECKSUM_ONLY = 2
3533 };
3534
3535 static COMMAND_HELPER(handle_verify_image_command_internal, enum verify_mode verify)
3536 {
3537 uint8_t *buffer;
3538 size_t buf_cnt;
3539 uint32_t image_size;
3540 int i;
3541 int retval;
3542 uint32_t checksum = 0;
3543 uint32_t mem_checksum = 0;
3544
3545 struct image image;
3546
3547 struct target *target = get_current_target(CMD_CTX);
3548
3549 if (CMD_ARGC < 1)
3550 return ERROR_COMMAND_SYNTAX_ERROR;
3551
3552 if (!target) {
3553 LOG_ERROR("no target selected");
3554 return ERROR_FAIL;
3555 }
3556
3557 struct duration bench;
3558 duration_start(&bench);
3559
3560 if (CMD_ARGC >= 2) {
3561 target_addr_t addr;
3562 COMMAND_PARSE_ADDRESS(CMD_ARGV[1], addr);
3563 image.base_address = addr;
3564 image.base_address_set = 1;
3565 } else {
3566 image.base_address_set = 0;
3567 image.base_address = 0x0;
3568 }
3569
3570 image.start_address_set = 0;
3571
3572 retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL);
3573 if (retval != ERROR_OK)
3574 return retval;
3575
3576 image_size = 0x0;
3577 int diffs = 0;
3578 retval = ERROR_OK;
3579 for (i = 0; i < image.num_sections; i++) {
3580 buffer = malloc(image.sections[i].size);
3581 if (buffer == NULL) {
3582 command_print(CMD,
3583 "error allocating buffer for section (%d bytes)",
3584 (int)(image.sections[i].size));
3585 break;
3586 }
3587 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
3588 if (retval != ERROR_OK) {
3589 free(buffer);
3590 break;
3591 }
3592
3593 if (verify >= IMAGE_VERIFY) {
3594 /* calculate checksum of image */
3595 retval = image_calculate_checksum(buffer, buf_cnt, &checksum);
3596 if (retval != ERROR_OK) {
3597 free(buffer);
3598 break;
3599 }
3600
3601 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
3602 if (retval != ERROR_OK) {
3603 free(buffer);
3604 break;
3605 }
3606 if ((checksum != mem_checksum) && (verify == IMAGE_CHECKSUM_ONLY)) {
3607 LOG_ERROR("checksum mismatch");
3608 free(buffer);
3609 retval = ERROR_FAIL;
3610 goto done;
3611 }
3612 if (checksum != mem_checksum) {
3613 /* failed crc checksum, fall back to a binary compare */
3614 uint8_t *data;
3615
3616 if (diffs == 0)
3617 LOG_ERROR("checksum mismatch - attempting binary compare");
3618
3619 data = malloc(buf_cnt);
3620
3621 /* Can we use 32bit word accesses? */
3622 int size = 1;
3623 int count = buf_cnt;
3624 if ((count % 4) == 0) {
3625 size *= 4;
3626 count /= 4;
3627 }
3628 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
3629 if (retval == ERROR_OK) {
3630 uint32_t t;
3631 for (t = 0; t < buf_cnt; t++) {
3632 if (data[t] != buffer[t]) {
3633 command_print(CMD,
3634 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3635 diffs,
3636 (unsigned)(t + image.sections[i].base_address),
3637 data[t],
3638 buffer[t]);
3639 if (diffs++ >= 127) {
3640 command_print(CMD, "More than 128 errors, the rest are not printed.");
3641 free(data);
3642 free(buffer);
3643 goto done;
3644 }
3645 }
3646 keep_alive();
3647 }
3648 }
3649 free(data);
3650 }
3651 } else {
3652 command_print(CMD, "address " TARGET_ADDR_FMT " length 0x%08zx",
3653 image.sections[i].base_address,
3654 buf_cnt);
3655 }
3656
3657 free(buffer);
3658 image_size += buf_cnt;
3659 }
3660 if (diffs > 0)
3661 command_print(CMD, "No more differences found.");
3662 done:
3663 if (diffs > 0)
3664 retval = ERROR_FAIL;
3665 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
3666 command_print(CMD, "verified %" PRIu32 " bytes "
3667 "in %fs (%0.3f KiB/s)", image_size,
3668 duration_elapsed(&bench), duration_kbps(&bench, image_size));
3669 }
3670
3671 image_close(&image);
3672
3673 return retval;
3674 }
3675
3676 COMMAND_HANDLER(handle_verify_image_checksum_command)
3677 {
3678 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_CHECKSUM_ONLY);
3679 }
3680
3681 COMMAND_HANDLER(handle_verify_image_command)
3682 {
3683 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_VERIFY);
3684 }
3685
3686 COMMAND_HANDLER(handle_test_image_command)
3687 {
3688 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_TEST);
3689 }
3690
3691 static int handle_bp_command_list(struct command_invocation *cmd)
3692 {
3693 struct target *target = get_current_target(cmd->ctx);
3694 struct breakpoint *breakpoint = target->breakpoints;
3695 while (breakpoint) {
3696 if (breakpoint->type == BKPT_SOFT) {
3697 char *buf = buf_to_str(breakpoint->orig_instr,
3698 breakpoint->length, 16);
3699 command_print(cmd, "IVA breakpoint: " TARGET_ADDR_FMT ", 0x%x, %i, 0x%s",
3700 breakpoint->address,
3701 breakpoint->length,
3702 breakpoint->set, buf);
3703 free(buf);
3704 } else {
3705 if ((breakpoint->address == 0) && (breakpoint->asid != 0))
3706 command_print(cmd, "Context breakpoint: 0x%8.8" PRIx32 ", 0x%x, %i",
3707 breakpoint->asid,
3708 breakpoint->length, breakpoint->set);
3709 else if ((breakpoint->address != 0) && (breakpoint->asid != 0)) {
3710 command_print(cmd, "Hybrid breakpoint(IVA): " TARGET_ADDR_FMT ", 0x%x, %i",
3711 breakpoint->address,
3712 breakpoint->length, breakpoint->set);
3713 command_print(cmd, "\t|--->linked with ContextID: 0x%8.8" PRIx32,
3714 breakpoint->asid);
3715 } else
3716 command_print(cmd, "Breakpoint(IVA): " TARGET_ADDR_FMT ", 0x%x, %i",
3717 breakpoint->address,
3718 breakpoint->length, breakpoint->set);
3719 }
3720
3721 breakpoint = breakpoint->next;
3722 }
3723 return ERROR_OK;
3724 }
3725
3726 static int handle_bp_command_set(struct command_invocation *cmd,
3727 target_addr_t addr, uint32_t asid, uint32_t length, int hw)
3728 {
3729 struct target *target = get_current_target(cmd->ctx);
3730 int retval;
3731
3732 if (asid == 0) {
3733 retval = breakpoint_add(target, addr, length, hw);
3734 /* error is always logged in breakpoint_add(), do not print it again */
3735 if (ERROR_OK == retval)
3736 command_print(cmd, "breakpoint set at " TARGET_ADDR_FMT "", addr);
3737
3738 } else if (addr == 0) {
3739 if (target->type->add_context_breakpoint == NULL) {
3740 LOG_ERROR("Context breakpoint not available");
3741 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
3742 }
3743 retval = context_breakpoint_add(target, asid, length, hw);
3744 /* error is always logged in context_breakpoint_add(), do not print it again */
3745 if (ERROR_OK == retval)
3746 command_print(cmd, "Context breakpoint set at 0x%8.8" PRIx32 "", asid);
3747
3748 } else {
3749 if (target->type->add_hybrid_breakpoint == NULL) {
3750 LOG_ERROR("Hybrid breakpoint not available");
3751 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
3752 }
3753 retval = hybrid_breakpoint_add(target, addr, asid, length, hw);
3754 /* error is always logged in hybrid_breakpoint_add(), do not print it again */
3755 if (ERROR_OK == retval)
3756 command_print(cmd, "Hybrid breakpoint set at 0x%8.8" PRIx32 "", asid);
3757 }
3758 return retval;
3759 }
3760
3761 COMMAND_HANDLER(handle_bp_command)
3762 {
3763 target_addr_t addr;
3764 uint32_t asid;
3765 uint32_t length;
3766 int hw = BKPT_SOFT;
3767
3768 switch (CMD_ARGC) {
3769 case 0:
3770 return handle_bp_command_list(CMD);
3771
3772 case 2:
3773 asid = 0;
3774 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
3775 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3776 return handle_bp_command_set(CMD, addr, asid, length, hw);
3777
3778 case 3:
3779 if (strcmp(CMD_ARGV[2], "hw") == 0) {
3780 hw = BKPT_HARD;
3781 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
3782 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3783 asid = 0;
3784 return handle_bp_command_set(CMD, addr, asid, length, hw);
3785 } else if (strcmp(CMD_ARGV[2], "hw_ctx") == 0) {
3786 hw = BKPT_HARD;
3787 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], asid);
3788 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3789 addr = 0;
3790 return handle_bp_command_set(CMD, addr, asid, length, hw);
3791 }
3792 /* fallthrough */
3793 case 4:
3794 hw = BKPT_HARD;
3795 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
3796 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], asid);
3797 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], length);
3798 return handle_bp_command_set(CMD, addr, asid, length, hw);
3799
3800 default:
3801 return ERROR_COMMAND_SYNTAX_ERROR;
3802 }
3803 }
3804
3805 COMMAND_HANDLER(handle_rbp_command)
3806 {
3807 if (CMD_ARGC != 1)
3808 return ERROR_COMMAND_SYNTAX_ERROR;
3809
3810 target_addr_t addr;
3811 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
3812
3813 struct target *target = get_current_target(CMD_CTX);
3814 breakpoint_remove(target, addr);
3815
3816 return ERROR_OK;
3817 }
3818
3819 COMMAND_HANDLER(handle_wp_command)
3820 {
3821 struct target *target = get_current_target(CMD_CTX);
3822
3823 if (CMD_ARGC == 0) {
3824 struct watchpoint *watchpoint = target->watchpoints;
3825
3826 while (watchpoint) {
3827 command_print(CMD, "address: " TARGET_ADDR_FMT
3828 ", len: 0x%8.8" PRIx32
3829 ", r/w/a: %i, value: 0x%8.8" PRIx32
3830 ", mask: 0x%8.8" PRIx32,
3831 watchpoint->address,
3832 watchpoint->length,
3833 (int)watchpoint->rw,
3834 watchpoint->value,
3835 watchpoint->mask);
3836 watchpoint = watchpoint->next;
3837 }
3838 return ERROR_OK;
3839 }
3840
3841 enum watchpoint_rw type = WPT_ACCESS;
3842 uint32_t addr = 0;
3843 uint32_t length = 0;
3844 uint32_t data_value = 0x0;
3845 uint32_t data_mask = 0xffffffff;
3846
3847 switch (CMD_ARGC) {
3848 case 5:
3849 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
3850 /* fall through */
3851 case 4:
3852 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
3853 /* fall through */
3854 case 3:
3855 switch (CMD_ARGV[2][0]) {
3856 case 'r':
3857 type = WPT_READ;
3858 break;
3859 case 'w':
3860 type = WPT_WRITE;
3861 break;
3862 case 'a':
3863 type = WPT_ACCESS;
3864 break;
3865 default:
3866 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
3867 return ERROR_COMMAND_SYNTAX_ERROR;
3868 }
3869 /* fall through */
3870 case 2:
3871 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3872 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3873 break;
3874
3875 default:
3876 return ERROR_COMMAND_SYNTAX_ERROR;
3877 }
3878
3879 int retval = watchpoint_add(target, addr, length, type,
3880 data_value, data_mask);
3881 if (ERROR_OK != retval)
3882 LOG_ERROR("Failure setting watchpoints");
3883
3884 return retval;
3885 }
3886
3887 COMMAND_HANDLER(handle_rwp_command)
3888 {
3889 if (CMD_ARGC != 1)
3890 return ERROR_COMMAND_SYNTAX_ERROR;
3891
3892 uint32_t addr;
3893 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3894
3895 struct target *target = get_current_target(CMD_CTX);
3896 watchpoint_remove(target, addr);
3897
3898 return ERROR_OK;
3899 }
3900
3901 /**
3902 * Translate a virtual address to a physical address.
3903 *
3904 * The low-level target implementation must have logged a detailed error
3905 * which is forwarded to telnet/GDB session.
3906 */
3907 COMMAND_HANDLER(handle_virt2phys_command)
3908 {
3909 if (CMD_ARGC != 1)
3910 return ERROR_COMMAND_SYNTAX_ERROR;
3911
3912 target_addr_t va;
3913 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], va);
3914 target_addr_t pa;
3915
3916 struct target *target = get_current_target(CMD_CTX);
3917 int retval = target->type->virt2phys(target, va, &pa);
3918 if (retval == ERROR_OK)
3919 command_print(CMD, "Physical address " TARGET_ADDR_FMT "", pa);
3920
3921 return retval;
3922 }
3923
3924 static void writeData(FILE *f, const void *data, size_t len)
3925 {
3926 size_t written = fwrite(data, 1, len, f);
3927 if (written != len)
3928 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
3929 }
3930
3931 static void writeLong(FILE *f, int l, struct target *target)
3932 {
3933 uint8_t val[4];
3934
3935 target_buffer_set_u32(target, val, l);
3936 writeData(f, val, 4);
3937 }
3938
3939 static void writeString(FILE *f, char *s)
3940 {
3941 writeData(f, s, strlen(s));
3942 }
3943
3944 typedef unsigned char UNIT[2]; /* unit of profiling */
3945
3946 /* Dump a gmon.out histogram file. */
3947 static void write_gmon(uint32_t *samples, uint32_t sampleNum, const char *filename, bool with_range,
3948 uint32_t start_address, uint32_t end_address, struct target *target, uint32_t duration_ms)
3949 {
3950 uint32_t i;
3951 FILE *f = fopen(filename, "w");
3952 if (f == NULL)
3953 return;
3954 writeString(f, "gmon");
3955 writeLong(f, 0x00000001, target); /* Version */
3956 writeLong(f, 0, target); /* padding */
3957 writeLong(f, 0, target); /* padding */
3958 writeLong(f, 0, target); /* padding */
3959
3960 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
3961 writeData(f, &zero, 1);
3962
3963 /* figure out bucket size */
3964 uint32_t min;
3965 uint32_t max;
3966 if (with_range) {
3967 min = start_address;
3968 max = end_address;
3969 } else {
3970 min = samples[0];
3971 max = samples[0];
3972 for (i = 0; i < sampleNum; i++) {
3973 if (min > samples[i])
3974 min = samples[i];
3975 if (max < samples[i])
3976 max = samples[i];
3977 }
3978
3979 /* max should be (largest sample + 1)
3980 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
3981 max++;
3982 }
3983
3984 int addressSpace = max - min;
3985 assert(addressSpace >= 2);
3986
3987 /* FIXME: What is the reasonable number of buckets?
3988 * The profiling result will be more accurate if there are enough buckets. */
3989 static const uint32_t maxBuckets = 128 * 1024; /* maximum buckets. */
3990 uint32_t numBuckets = addressSpace / sizeof(UNIT);
3991 if (numBuckets > maxBuckets)
3992 numBuckets = maxBuckets;
3993 int *buckets = malloc(sizeof(int) * numBuckets);
3994 if (buckets == NULL) {
3995 fclose(f);
3996 return;
3997 }
3998 memset(buckets, 0, sizeof(int) * numBuckets);
3999 for (i = 0; i < sampleNum; i++) {
4000 uint32_t address = samples[i];
4001
4002 if ((address < min) || (max <= address))
4003 continue;
4004
4005 long long a = address - min;
4006 long long b = numBuckets;
4007 long long c = addressSpace;
4008 int index_t = (a * b) / c; /* danger!!!! int32 overflows */
4009 buckets[index_t]++;
4010 }
4011
4012 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
4013 writeLong(f, min, target); /* low_pc */
4014 writeLong(f, max, target); /* high_pc */
4015 writeLong(f, numBuckets, target); /* # of buckets */
4016 float sample_rate = sampleNum / (duration_ms / 1000.0);
4017 writeLong(f, sample_rate, target);
4018 writeString(f, "seconds");
4019 for (i = 0; i < (15-strlen("seconds")); i++)
4020 writeData(f, &zero, 1);
4021 writeString(f, "s");
4022
4023 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
4024
4025 char *data = malloc(2 * numBuckets);
4026 if (data != NULL) {
4027 for (i = 0; i < numBuckets; i++) {
4028 int val;
4029 val = buckets[i];
4030 if (val > 65535)
4031 val = 65535;
4032 data[i * 2] = val&0xff;
4033 data[i * 2 + 1] = (val >> 8) & 0xff;
4034 }
4035 free(buckets);
4036 writeData(f, data, numBuckets * 2);
4037 free(data);
4038 } else
4039 free(buckets);
4040
4041 fclose(f);
4042 }
4043
4044 /* profiling samples the CPU PC as quickly as OpenOCD is able,
4045 * which will be used as a random sampling of PC */
4046 COMMAND_HANDLER(handle_profile_command)
4047 {
4048 struct target *target = get_current_target(CMD_CTX);
4049
4050 if ((CMD_ARGC != 2) && (CMD_ARGC != 4))
4051 return ERROR_COMMAND_SYNTAX_ERROR;
4052
4053 const uint32_t MAX_PROFILE_SAMPLE_NUM = 10000;
4054 uint32_t offset;
4055 uint32_t num_of_samples;
4056 int retval = ERROR_OK;
4057
4058 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], offset);
4059
4060 uint32_t *samples = malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM);
4061 if (samples == NULL) {
4062 LOG_ERROR("No memory to store samples.");
4063 return ERROR_FAIL;
4064 }
4065
4066 uint64_t timestart_ms = timeval_ms();
4067 /**
4068 * Some cores let us sample the PC without the
4069 * annoying halt/resume step; for example, ARMv7 PCSR.
4070 * Provide a way to use that more efficient mechanism.
4071 */
4072 retval = target_profiling(target, samples, MAX_PROFILE_SAMPLE_NUM,
4073 &num_of_samples, offset);
4074 if (retval != ERROR_OK) {
4075 free(samples);
4076 return retval;
4077 }
4078 uint32_t duration_ms = timeval_ms() - timestart_ms;
4079
4080 assert(num_of_samples <= MAX_PROFILE_SAMPLE_NUM);
4081
4082 retval = target_poll(target);
4083 if (retval != ERROR_OK) {
4084 free(samples);
4085 return retval;
4086 }
4087 if (target->state == TARGET_RUNNING) {
4088 retval = target_halt(target);
4089 if (retval != ERROR_OK) {
4090 free(samples);
4091 return retval;
4092 }
4093 }
4094
4095 retval = target_poll(target);
4096 if (retval != ERROR_OK) {
4097 free(samples);
4098 return retval;
4099 }
4100
4101 uint32_t start_address = 0;
4102 uint32_t end_address = 0;
4103 bool with_range = false;
4104 if (CMD_ARGC == 4) {
4105 with_range = true;
4106 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], start_address);
4107 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], end_address);
4108 }
4109
4110 write_gmon(samples, num_of_samples, CMD_ARGV[1],
4111 with_range, start_address, end_address, target, duration_ms);
4112 command_print(CMD, "Wrote %s", CMD_ARGV[1]);
4113
4114 free(samples);
4115 return retval;
4116 }
4117
4118 static int new_int_array_element(Jim_Interp *interp, const char *varname, int idx, uint32_t val)
4119 {
4120 char *namebuf;
4121 Jim_Obj *nameObjPtr, *valObjPtr;
4122 int result;
4123
4124 namebuf = alloc_printf("%s(%d)", varname, idx);
4125 if (!namebuf)
4126 return JIM_ERR;
4127
4128 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
4129 valObjPtr = Jim_NewIntObj(interp, val);
4130 if (!nameObjPtr || !valObjPtr) {
4131 free(namebuf);
4132 return JIM_ERR;
4133 }
4134
4135 Jim_IncrRefCount(nameObjPtr);
4136 Jim_IncrRefCount(valObjPtr);
4137 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
4138 Jim_DecrRefCount(interp, nameObjPtr);
4139 Jim_DecrRefCount(interp, valObjPtr);
4140 free(namebuf);
4141 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
4142 return result;
4143 }
4144
4145 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4146 {
4147 struct command_context *context;
4148 struct target *target;
4149
4150 context = current_command_context(interp);
4151 assert(context != NULL);
4152
4153 target = get_current_target(context);
4154 if (target == NULL) {
4155 LOG_ERROR("mem2array: no current target");
4156 return JIM_ERR;
4157 }
4158
4159 return target_mem2array(interp, target, argc - 1, argv + 1);
4160 }
4161
4162 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
4163 {
4164 long l;
4165 uint32_t width;
4166 int len;
4167 uint32_t addr;
4168 uint32_t count;
4169 uint32_t v;
4170 const char *varname;
4171 const char *phys;
4172 bool is_phys;
4173 int n, e, retval;
4174 uint32_t i;
4175
4176 /* argv[1] = name of array to receive the data
4177 * argv[2] = desired width
4178 * argv[3] = memory address
4179 * argv[4] = count of times to read
4180 */
4181
4182 if (argc < 4 || argc > 5) {
4183 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems [phys]");
4184 return JIM_ERR;
4185 }
4186 varname = Jim_GetString(argv[0], &len);
4187 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4188
4189 e = Jim_GetLong(interp, argv[1], &l);
4190 width = l;
4191 if (e != JIM_OK)
4192 return e;
4193
4194 e = Jim_GetLong(interp, argv[2], &l);
4195 addr = l;
4196 if (e != JIM_OK)
4197 return e;
4198 e = Jim_GetLong(interp, argv[3], &l);
4199 len = l;
4200 if (e != JIM_OK)
4201 return e;
4202 is_phys = false;
4203 if (argc > 4) {
4204 phys = Jim_GetString(argv[4], &n);
4205 if (!strncmp(phys, "phys", n))
4206 is_phys = true;
4207 else
4208 return JIM_ERR;
4209 }
4210 switch (width) {
4211 case 8:
4212 width = 1;
4213 break;
4214 case 16:
4215 width = 2;
4216 break;
4217 case 32:
4218 width = 4;
4219 break;
4220 default:
4221 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4222 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
4223 return JIM_ERR;
4224 }
4225 if (len == 0) {
4226 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4227 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
4228 return JIM_ERR;
4229 }
4230 if ((addr + (len * width)) < addr) {
4231 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4232 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
4233 return JIM_ERR;
4234 }
4235 /* absurd transfer size? */
4236 if (len > 65536) {
4237 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4238 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
4239 return JIM_ERR;
4240 }
4241
4242 if ((width == 1) ||
4243 ((width == 2) && ((addr & 1) == 0)) ||
4244 ((width == 4) && ((addr & 3) == 0))) {
4245 /* all is well */
4246 } else {
4247 char buf[100];
4248 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4249 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
4250 addr,
4251 width);
4252 Jim_AppendStrings(interp, Jim_GetResult(interp), buf, NULL);
4253 return JIM_ERR;
4254 }
4255
4256 /* Transfer loop */
4257
4258 /* index counter */
4259 n = 0;
4260
4261 size_t buffersize = 4096;
4262 uint8_t *buffer = malloc(buffersize);
4263 if (buffer == NULL)
4264 return JIM_ERR;
4265
4266 /* assume ok */
4267 e = JIM_OK;
4268 while (len) {
4269 /* Slurp... in buffer size chunks */
4270
4271 count = len; /* in objects.. */
4272 if (count > (buffersize / width))
4273 count = (buffersize / width);
4274
4275 if (is_phys)
4276 retval = target_read_phys_memory(target, addr, width, count, buffer);
4277 else
4278 retval = target_read_memory(target, addr, width, count, buffer);
4279 if (retval != ERROR_OK) {
4280 /* BOO !*/
4281 LOG_ERROR("mem2array: Read @ 0x%08" PRIx32 ", w=%" PRId32 ", cnt=%" PRId32 ", failed",
4282 addr,
4283 width,
4284 count);
4285 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4286 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
4287 e = JIM_ERR;
4288 break;
4289 } else {
4290 v = 0; /* shut up gcc */
4291 for (i = 0; i < count ; i++, n++) {
4292 switch (width) {
4293 case 4:
4294 v = target_buffer_get_u32(target, &buffer[i*width]);
4295 break;
4296 case 2:
4297 v = target_buffer_get_u16(target, &buffer[i*width]);
4298 break;
4299 case 1:
4300 v = buffer[i] & 0x0ff;
4301 break;
4302 }
4303 new_int_array_element(interp, varname, n, v);
4304 }
4305 len -= count;
4306 addr += count * width;
4307 }
4308 }
4309
4310 free(buffer);
4311
4312 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4313
4314 return e;
4315 }
4316
4317 static int get_int_array_element(Jim_Interp *interp, const char *varname, int idx, uint32_t *val)
4318 {
4319 char *namebuf;
4320 Jim_Obj *nameObjPtr, *valObjPtr;
4321 int result;
4322 long l;
4323
4324 namebuf = alloc_printf("%s(%d)", varname, idx);
4325 if (!namebuf)
4326 return JIM_ERR;
4327
4328 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
4329 if (!nameObjPtr) {
4330 free(namebuf);
4331 return JIM_ERR;
4332 }
4333
4334 Jim_IncrRefCount(nameObjPtr);
4335 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
4336 Jim_DecrRefCount(interp, nameObjPtr);
4337 free(namebuf);
4338 if (valObjPtr == NULL)
4339 return JIM_ERR;
4340
4341 result = Jim_GetLong(interp, valObjPtr, &l);
4342 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
4343 *val = l;
4344 return result;
4345 }
4346
4347 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4348 {
4349 struct command_context *context;
4350 struct target *target;
4351
4352 context = current_command_context(interp);
4353 assert(context != NULL);
4354
4355 target = get_current_target(context);
4356 if (target == NULL) {
4357 LOG_ERROR("array2mem: no current target");
4358 return JIM_ERR;
4359 }
4360
4361 return target_array2mem(interp, target, argc-1, argv + 1);
4362 }
4363
4364 static int target_array2mem(Jim_Interp *interp, struct target *target,
4365 int argc, Jim_Obj *const *argv)
4366 {
4367 long l;
4368 uint32_t width;
4369 int len;
4370 uint32_t addr;
4371 uint32_t count;
4372 uint32_t v;
4373 const char *varname;
4374 const char *phys;
4375 bool is_phys;
4376 int n, e, retval;
4377 uint32_t i;
4378
4379 /* argv[1] = name of array to get the data
4380 * argv[2] = desired width
4381 * argv[3] = memory address
4382 * argv[4] = count to write
4383 */
4384 if (argc < 4 || argc > 5) {
4385 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems [phys]");
4386 return JIM_ERR;
4387 }
4388 varname = Jim_GetString(argv[0], &len);
4389 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4390
4391 e = Jim_GetLong(interp, argv[1], &l);
4392 width = l;
4393 if (e != JIM_OK)
4394 return e;
4395
4396 e = Jim_GetLong(interp, argv[2], &l);
4397 addr = l;
4398 if (e != JIM_OK)
4399 return e;
4400 e = Jim_GetLong(interp, argv[3], &l);
4401 len = l;
4402 if (e != JIM_OK)
4403 return e;
4404 is_phys = false;
4405 if (argc > 4) {
4406 phys = Jim_GetString(argv[4], &n);
4407 if (!strncmp(phys, "phys", n))
4408 is_phys = true;
4409 else
4410 return JIM_ERR;
4411 }
4412 switch (width) {
4413 case 8:
4414 width = 1;
4415 break;
4416 case 16:
4417 width = 2;
4418 break;
4419 case 32:
4420 width = 4;
4421 break;
4422 default:
4423 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4424 Jim_AppendStrings(interp, Jim_GetResult(interp),
4425 "Invalid width param, must be 8/16/32", NULL);
4426 return JIM_ERR;
4427 }
4428 if (len == 0) {
4429 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4430 Jim_AppendStrings(interp, Jim_GetResult(interp),
4431 "array2mem: zero width read?", NULL);
4432 return JIM_ERR;
4433 }
4434 if ((addr + (len * width)) < addr) {
4435 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4436 Jim_AppendStrings(interp, Jim_GetResult(interp),
4437 "array2mem: addr + len - wraps to zero?", NULL);
4438 return JIM_ERR;
4439 }
4440 /* absurd transfer size? */
4441 if (len > 65536) {
4442 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4443 Jim_AppendStrings(interp, Jim_GetResult(interp),
4444 "array2mem: absurd > 64K item request", NULL);
4445 return JIM_ERR;
4446 }
4447
4448 if ((width == 1) ||
4449 ((width == 2) && ((addr & 1) == 0)) ||
4450 ((width == 4) && ((addr & 3) == 0))) {
4451 /* all is well */
4452 } else {
4453 char buf[100];
4454 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4455 sprintf(buf, "array2mem address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
4456 addr,
4457 width);
4458 Jim_AppendStrings(interp, Jim_GetResult(interp), buf, NULL);
4459 return JIM_ERR;
4460 }
4461
4462 /* Transfer loop */
4463
4464 /* index counter */
4465 n = 0;
4466 /* assume ok */
4467 e = JIM_OK;
4468
4469 size_t buffersize = 4096;
4470 uint8_t *buffer = malloc(buffersize);
4471 if (buffer == NULL)
4472 return JIM_ERR;
4473
4474 while (len) {
4475 /* Slurp... in buffer size chunks */
4476
4477 count = len; /* in objects.. */
4478 if (count > (buffersize / width))
4479 count = (buffersize / width);
4480
4481 v = 0; /* shut up gcc */
4482 for (i = 0; i < count; i++, n++) {
4483 get_int_array_element(interp, varname, n, &v);
4484 switch (width) {
4485 case 4:
4486 target_buffer_set_u32(target, &buffer[i * width], v);
4487 break;
4488 case 2:
4489 target_buffer_set_u16(target, &buffer[i * width], v);
4490 break;
4491 case 1:
4492 buffer[i] = v & 0x0ff;
4493 break;
4494 }
4495 }
4496 len -= count;
4497
4498 if (is_phys)
4499 retval = target_write_phys_memory(target, addr, width, count, buffer);
4500 else
4501 retval = target_write_memory(target, addr, width, count, buffer);
4502 if (retval != ERROR_OK) {
4503 /* BOO !*/
4504 LOG_ERROR("array2mem: Write @ 0x%08" PRIx32 ", w=%" PRId32 ", cnt=%" PRId32 ", failed",
4505 addr,
4506 width,
4507 count);
4508 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4509 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
4510 e = JIM_ERR;
4511 break;
4512 }
4513 addr += count * width;
4514 }
4515
4516 free(buffer);
4517
4518 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4519
4520 return e;
4521 }
4522
4523 /* FIX? should we propagate errors here rather than printing them
4524 * and continuing?
4525 */
4526 void target_handle_event(struct target *target, enum target_event e)
4527 {
4528 struct target_event_action *teap;
4529
4530 for (teap = target->event_action; teap != NULL; teap = teap->next) {
4531 if (teap->event == e) {
4532 LOG_DEBUG("target(%d): %s (%s) event: %d (%s) action: %s",
4533 target->target_number,
4534 target_name(target),
4535 target_type_name(target),
4536 e,
4537 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
4538 Jim_GetString(teap->body, NULL));
4539
4540 /* Override current target by the target an event
4541 * is issued from (lot of scripts need it).
4542 * Return back to previous override as soon
4543 * as the handler processing is done */
4544 struct command_context *cmd_ctx = current_command_context(teap->interp);
4545 struct target *saved_target_override = cmd_ctx->current_target_override;
4546 cmd_ctx->current_target_override = target;
4547
4548 if (Jim_EvalObj(teap->interp, teap->body) != JIM_OK) {
4549 Jim_MakeErrorMessage(teap->interp);
4550 LOG_USER("Error executing event %s on target %s:\n%s",
4551 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
4552 target_name(target),
4553 Jim_GetString(Jim_GetResult(teap->interp), NULL));
4554 /* clean both error code and stacktrace before return */
4555 Jim_Eval(teap->interp, "error \"\" \"\"");
4556 }
4557
4558 cmd_ctx->current_target_override = saved_target_override;
4559 }
4560 }
4561 }
4562
4563 /**
4564 * Returns true only if the target has a handler for the specified event.
4565 */
4566 bool target_has_event_action(struct target *target, enum target_event event)
4567 {
4568 struct target_event_action *teap;
4569
4570 for (teap = target->event_action; teap != NULL; teap = teap->next) {
4571 if (teap->event == event)
4572 return true;
4573 }
4574 return false;
4575 }
4576
4577 enum target_cfg_param {
4578 TCFG_TYPE,
4579 TCFG_EVENT,
4580 TCFG_WORK_AREA_VIRT,
4581 TCFG_WORK_AREA_PHYS,
4582 TCFG_WORK_AREA_SIZE,
4583 TCFG_WORK_AREA_BACKUP,
4584 TCFG_ENDIAN,
4585 TCFG_COREID,
4586 TCFG_CHAIN_POSITION,
4587 TCFG_DBGBASE,
4588 TCFG_RTOS,
4589 TCFG_DEFER_EXAMINE,
4590 TCFG_GDB_PORT,
4591 };
4592
4593 static Jim_Nvp nvp_config_opts[] = {
4594 { .name = "-type", .value = TCFG_TYPE },
4595 { .name = "-event", .value = TCFG_EVENT },
4596 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
4597 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
4598 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
4599 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
4600 { .name = "-endian" , .value = TCFG_ENDIAN },
4601 { .name = "-coreid", .value = TCFG_COREID },
4602 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
4603 { .name = "-dbgbase", .value = TCFG_DBGBASE },
4604 { .name = "-rtos", .value = TCFG_RTOS },
4605 { .name = "-defer-examine", .value = TCFG_DEFER_EXAMINE },
4606 { .name = "-gdb-port", .value = TCFG_GDB_PORT },
4607 { .name = NULL, .value = -1 }
4608 };
4609
4610 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
4611 {
4612 Jim_Nvp *n;
4613 Jim_Obj *o;
4614 jim_wide w;
4615 int e;
4616
4617 /* parse config or cget options ... */
4618 while (goi->argc > 0) {
4619 Jim_SetEmptyResult(goi->interp);
4620 /* Jim_GetOpt_Debug(goi); */
4621
4622 if (target->type->target_jim_configure) {
4623 /* target defines a configure function */
4624 /* target gets first dibs on parameters */
4625 e = (*(target->type->target_jim_configure))(target, goi);
4626 if (e == JIM_OK) {
4627 /* more? */
4628 continue;
4629 }
4630 if (e == JIM_ERR) {
4631 /* An error */
4632 return e;
4633 }
4634 /* otherwise we 'continue' below */
4635 }
4636 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
4637 if (e != JIM_OK) {
4638 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
4639 return e;
4640 }
4641 switch (n->value) {
4642 case TCFG_TYPE:
4643 /* not setable */
4644 if (goi->isconfigure) {
4645 Jim_SetResultFormatted(goi->interp,
4646 "not settable: %s", n->name);
4647 return JIM_ERR;
4648 } else {
4649 no_params:
4650 if (goi->argc != 0) {
4651 Jim_WrongNumArgs(goi->interp,
4652 goi->argc, goi->argv,
4653 "NO PARAMS");
4654 return JIM_ERR;
4655 }
4656 }
4657 Jim_SetResultString(goi->interp,
4658 target_type_name(target), -1);
4659 /* loop for more */
4660 break;
4661 case TCFG_EVENT:
4662 if (goi->argc == 0) {
4663 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
4664 return JIM_ERR;
4665 }
4666
4667 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
4668 if (e != JIM_OK) {
4669 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
4670 return e;
4671 }
4672
4673 if (goi->isconfigure) {
4674 if (goi->argc != 1) {
4675 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
4676 return JIM_ERR;
4677 }
4678 } else {
4679 if (goi->argc != 0) {
4680 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
4681 return JIM_ERR;
4682 }
4683 }
4684
4685 {
4686 struct target_event_action *teap;
4687
4688 teap = target->event_action;
4689 /* replace existing? */
4690 while (teap) {
4691 if (teap->event == (enum target_event)n->value)
4692 break;
4693 teap = teap->next;
4694 }
4695
4696 if (goi->isconfigure) {
4697 bool replace = true;
4698 if (teap == NULL) {
4699 /* create new */
4700 teap = calloc(1, sizeof(*teap));
4701 replace = false;
4702 }
4703 teap->event = n->value;
4704 teap->interp = goi->interp;
4705 Jim_GetOpt_Obj(goi, &o);
4706 if (teap->body)
4707 Jim_DecrRefCount(teap->interp, teap->body);
4708 teap->body = Jim_DuplicateObj(goi->interp, o);
4709 /*
4710 * FIXME:
4711 * Tcl/TK - "tk events" have a nice feature.
4712 * See the "BIND" command.
4713 * We should support that here.
4714 * You can specify %X and %Y in the event code.
4715 * The idea is: %T - target name.
4716 * The idea is: %N - target number
4717 * The idea is: %E - event name.
4718 */
4719 Jim_IncrRefCount(teap->body);
4720
4721 if (!replace) {
4722 /* add to head of event list */
4723 teap->next = target->event_action;
4724 target->event_action = teap;
4725 }
4726 Jim_SetEmptyResult(goi->interp);
4727 } else {
4728 /* get */
4729 if (teap == NULL)
4730 Jim_SetEmptyResult(goi->interp);
4731 else
4732 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
4733 }
4734 }
4735 /* loop for more */
4736 break;
4737
4738 case TCFG_WORK_AREA_VIRT:
4739 if (goi->isconfigure) {
4740 target_free_all_working_areas(target);
4741 e = Jim_GetOpt_Wide(goi, &w);
4742 if (e != JIM_OK)
4743 return e;
4744 target->working_area_virt = w;
4745 target->working_area_virt_spec = true;
4746 } else {
4747 if (goi->argc != 0)
4748 goto no_params;
4749 }
4750 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
4751 /* loop for more */
4752 break;
4753
4754 case TCFG_WORK_AREA_PHYS:
4755 if (goi->isconfigure) {
4756 target_free_all_working_areas(target);
4757 e = Jim_GetOpt_Wide(goi, &w);
4758 if (e != JIM_OK)
4759 return e;
4760 target->working_area_phys = w;
4761 target->working_area_phys_spec = true;
4762 } else {
4763 if (goi->argc != 0)
4764 goto no_params;
4765 }
4766 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
4767 /* loop for more */
4768 break;
4769
4770 case TCFG_WORK_AREA_SIZE:
4771 if (goi->isconfigure) {
4772 target_free_all_working_areas(target);
4773 e = Jim_GetOpt_Wide(goi, &w);
4774 if (e != JIM_OK)
4775 return e;
4776 target->working_area_size = w;
4777 } else {
4778 if (goi->argc != 0)
4779 goto no_params;
4780 }
4781 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
4782 /* loop for more */
4783 break;
4784
4785 case TCFG_WORK_AREA_BACKUP:
4786 if (goi->isconfigure) {
4787 target_free_all_working_areas(target);
4788 e = Jim_GetOpt_Wide(goi, &w);
4789 if (e != JIM_OK)
4790 return e;
4791 /* make this exactly 1 or 0 */
4792 target->backup_working_area = (!!w);
4793 } else {
4794 if (goi->argc != 0)
4795 goto no_params;
4796 }
4797 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
4798 /* loop for more e*/
4799 break;
4800
4801
4802 case TCFG_ENDIAN:
4803 if (goi->isconfigure) {
4804 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
4805 if (e != JIM_OK) {
4806 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
4807 return e;
4808 }
4809 target->endianness = n->value;
4810 } else {
4811 if (goi->argc != 0)
4812 goto no_params;
4813 }
4814 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
4815 if (n->name == NULL) {
4816 target->endianness = TARGET_LITTLE_ENDIAN;
4817 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
4818 }
4819 Jim_SetResultString(goi->interp, n->name, -1);
4820 /* loop for more */
4821 break;
4822
4823 case TCFG_COREID:
4824 if (goi->isconfigure) {
4825 e = Jim_GetOpt_Wide(goi, &w);
4826 if (e != JIM_OK)
4827 return e;
4828 target->coreid = (int32_t)w;
4829 } else {
4830 if (goi->argc != 0)
4831 goto no_params;
4832 }
4833 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->coreid));
4834 /* loop for more */
4835 break;
4836
4837 case TCFG_CHAIN_POSITION:
4838 if (goi->isconfigure) {
4839 Jim_Obj *o_t;
4840 struct jtag_tap *tap;
4841
4842 if (target->has_dap) {
4843 Jim_SetResultString(goi->interp,
4844 "target requires -dap parameter instead of -chain-position!", -1);
4845 return JIM_ERR;
4846 }
4847
4848 target_free_all_working_areas(target);
4849 e = Jim_GetOpt_Obj(goi, &o_t);
4850 if (e != JIM_OK)
4851 return e;
4852 tap = jtag_tap_by_jim_obj(goi->interp, o_t);
4853 if (tap == NULL)
4854 return JIM_ERR;
4855 target->tap = tap;
4856 target->tap_configured = true;
4857 } else {
4858 if (goi->argc != 0)
4859 goto no_params;
4860 }
4861 Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
4862 /* loop for more e*/
4863 break;
4864 case TCFG_DBGBASE:
4865 if (goi->isconfigure) {
4866 e = Jim_GetOpt_Wide(goi, &w);
4867 if (e != JIM_OK)
4868 return e;
4869 target->dbgbase = (uint32_t)w;
4870 target->dbgbase_set = true;
4871 } else {
4872 if (goi->argc != 0)
4873 goto no_params;
4874 }
4875 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->dbgbase));
4876 /* loop for more */
4877 break;
4878 case TCFG_RTOS:
4879 /* RTOS */
4880 {
4881 int result = rtos_create(goi, target);
4882 if (result != JIM_OK)
4883 return result;
4884 }
4885 /* loop for more */
4886 break;
4887
4888 case TCFG_DEFER_EXAMINE:
4889 /* DEFER_EXAMINE */
4890 target->defer_examine = true;
4891 /* loop for more */
4892 break;
4893
4894 case TCFG_GDB_PORT:
4895 if (goi->isconfigure) {
4896 struct command_context *cmd_ctx = current_command_context(goi->interp);
4897 if (cmd_ctx->mode != COMMAND_CONFIG) {
4898 Jim_SetResultString(goi->interp, "-gdb-port must be configured before 'init'", -1);
4899 return JIM_ERR;
4900 }
4901
4902 const char *s;
4903 e = Jim_GetOpt_String(goi, &s, NULL);
4904 if (e != JIM_OK)
4905 return e;
4906 target->gdb_port_override = strdup(s);
4907 } else {
4908 if (goi->argc != 0)
4909 goto no_params;
4910 }
4911 Jim_SetResultString(goi->interp, target->gdb_port_override ? : "undefined", -1);
4912 /* loop for more */
4913 break;
4914 }
4915 } /* while (goi->argc) */
4916
4917
4918 /* done - we return */
4919 return JIM_OK;
4920 }
4921
4922 static int jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
4923 {
4924 Jim_GetOptInfo goi;
4925
4926 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4927 goi.isconfigure = !strcmp(Jim_GetString(argv[0], NULL), "configure");
4928 if (goi.argc < 1) {
4929 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
4930 "missing: -option ...");
4931 return JIM_ERR;
4932 }
4933 struct target *target = Jim_CmdPrivData(goi.interp);
4934 return target_configure(&goi, target);
4935 }
4936
4937 static int jim_target_mem2array(Jim_Interp *interp,
4938 int argc, Jim_Obj *const *argv)
4939 {
4940 struct target *target = Jim_CmdPrivData(interp);
4941 return target_mem2array(interp, target, argc - 1, argv + 1);
4942 }
4943
4944 static int jim_target_array2mem(Jim_Interp *interp,
4945 int argc, Jim_Obj *const *argv)
4946 {
4947 struct target *target = Jim_CmdPrivData(interp);
4948 return target_array2mem(interp, target, argc - 1, argv + 1);
4949 }
4950
4951 static int jim_target_tap_disabled(Jim_Interp *interp)
4952 {
4953 Jim_SetResultFormatted(interp, "[TAP is disabled]");
4954 return JIM_ERR;
4955 }
4956
4957 static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4958 {
4959 bool allow_defer = false;
4960
4961 Jim_GetOptInfo goi;
4962 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4963 if (goi.argc > 1) {
4964 const char *cmd_name = Jim_GetString(argv[0], NULL);
4965 Jim_SetResultFormatted(goi.interp,
4966 "usage: %s ['allow-defer']", cmd_name);
4967 return JIM_ERR;
4968 }
4969 if (goi.argc > 0 &&
4970 strcmp(Jim_GetString(argv[1], NULL), "allow-defer") == 0) {
4971 /* consume it */
4972 struct Jim_Obj *obj;
4973 int e = Jim_GetOpt_Obj(&goi, &obj);
4974 if (e != JIM_OK)
4975 return e;
4976 allow_defer = true;
4977 }
4978
4979 struct target *target = Jim_CmdPrivData(interp);
4980 if (!target->tap->enabled)
4981 return jim_target_tap_disabled(interp);
4982
4983 if (allow_defer && target->defer_examine) {
4984 LOG_INFO("Deferring arp_examine of %s", target_name(target));
4985 LOG_INFO("Use arp_examine command to examine it manually!");
4986 return JIM_OK;
4987 }
4988
4989 int e = target->type->examine(target);
4990 if (e != ERROR_OK)
4991 return JIM_ERR;
4992 return JIM_OK;
4993 }
4994
4995 static int jim_target_was_examined(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
4996 {
4997 struct target *target = Jim_CmdPrivData(interp);
4998
4999 Jim_SetResultBool(interp, target_was_examined(target));
5000 return JIM_OK;
5001 }
5002
5003 static int jim_target_examine_deferred(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
5004 {
5005 struct target *target = Jim_CmdPrivData(interp);
5006
5007 Jim_SetResultBool(interp, target->defer_examine);
5008 return JIM_OK;
5009 }
5010
5011 static int jim_target_halt_gdb(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5012 {
5013 if (argc != 1) {
5014 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
5015 return JIM_ERR;
5016 }
5017 struct target *target = Jim_CmdPrivData(interp);
5018
5019 if (target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT) != ERROR_OK)
5020 return JIM_ERR;
5021
5022 return JIM_OK;
5023 }
5024
5025 static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5026 {
5027 if (argc != 1) {
5028 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
5029 return JIM_ERR;
5030 }
5031 struct target *target = Jim_CmdPrivData(interp);
5032 if (!target->tap->enabled)
5033 return jim_target_tap_disabled(interp);
5034
5035 int e;
5036 if (!(target_was_examined(target)))
5037 e = ERROR_TARGET_NOT_EXAMINED;
5038 else
5039 e = target->type->poll(target);
5040 if (e != ERROR_OK)
5041 return JIM_ERR;
5042 return JIM_OK;
5043 }
5044
5045 static int jim_target_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5046 {
5047 Jim_GetOptInfo goi;
5048 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5049
5050 if (goi.argc != 2) {
5051 Jim_WrongNumArgs(interp, 0, argv,
5052 "([tT]|[fF]|assert|deassert) BOOL");
5053 return JIM_ERR;
5054 }
5055
5056 Jim_Nvp *n;
5057 int e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
5058 if (e != JIM_OK) {
5059 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
5060 return e;
5061 }
5062 /* the halt or not param */
5063 jim_wide a;
5064 e = Jim_GetOpt_Wide(&goi, &a);
5065 if (e != JIM_OK)
5066 return e;
5067
5068 struct target *target = Jim_CmdPrivData(goi.interp);
5069 if (!target->tap->enabled)
5070 return jim_target_tap_disabled(interp);
5071
5072 if (!target->type->assert_reset || !target->type->deassert_reset) {
5073 Jim_SetResultFormatted(interp,
5074 "No target-specific reset for %s",
5075 target_name(target));
5076 return JIM_ERR;
5077 }
5078
5079 if (target->defer_examine)
5080 target_reset_examined(target);
5081
5082 /* determine if we should halt or not. */
5083 target->reset_halt = !!a;
5084 /* When this happens - all workareas are invalid. */
5085 target_free_all_working_areas_restore(target, 0);
5086
5087 /* do the assert */
5088 if (n->value == NVP_ASSERT)
5089 e = target->type->assert_reset(target);
5090 else
5091 e = target->type->deassert_reset(target);
5092 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
5093 }
5094
5095 static int jim_target_halt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5096 {
5097 if (argc != 1) {
5098 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
5099 return JIM_ERR;
5100 }
5101 struct target *target = Jim_CmdPrivData(interp);
5102 if (!target->tap->enabled)
5103 return jim_target_tap_disabled(interp);
5104 int e = target->type->halt(target);
5105 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
5106 }
5107
5108 static int jim_target_wait_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5109 {
5110 Jim_GetOptInfo goi;
5111 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5112
5113 /* params: <name> statename timeoutmsecs */
5114 if (goi.argc != 2) {
5115 const char *cmd_name = Jim_GetString(argv[0], NULL);
5116 Jim_SetResultFormatted(goi.interp,
5117 "%s <state_name> <timeout_in_msec>", cmd_name);
5118 return JIM_ERR;
5119 }
5120
5121 Jim_Nvp *n;
5122 int e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
5123 if (e != JIM_OK) {
5124 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state, 1);
5125 return e;
5126 }
5127 jim_wide a;
5128 e = Jim_GetOpt_Wide(&goi, &a);
5129 if (e != JIM_OK)
5130 return e;
5131 struct target *target = Jim_CmdPrivData(interp);
5132 if (!target->tap->enabled)
5133 return jim_target_tap_disabled(interp);
5134
5135 e = target_wait_state(target, n->value, a);
5136 if (e != ERROR_OK) {
5137 Jim_Obj *eObj = Jim_NewIntObj(interp, e);
5138 Jim_SetResultFormatted(goi.interp,
5139 "target: %s wait %s fails (%#s) %s",
5140 target_name(target), n->name,
5141 eObj, target_strerror_safe(e));
5142 Jim_FreeNewObj(interp, eObj);
5143 return JIM_ERR;
5144 }
5145 return JIM_OK;
5146 }
5147 /* List for human, Events defined for this target.
5148 * scripts/programs should use 'name cget -event NAME'
5149 */
5150 COMMAND_HANDLER(handle_target_event_list)
5151 {
5152 struct target *target = get_current_target(CMD_CTX);
5153 struct target_event_action *teap = target->event_action;
5154
5155 command_print(CMD, "Event actions for target (%d) %s\n",
5156 target->target_number,
5157 target_name(target));
5158 command_print(CMD, "%-25s | Body", "Event");
5159 command_print(CMD, "------------------------- | "
5160 "----------------------------------------");
5161 while (teap) {
5162 Jim_Nvp *opt = Jim_Nvp_value2name_simple(nvp_target_event, teap->event);
5163 command_print(CMD, "%-25s | %s",
5164 opt->name, Jim_GetString(teap->body, NULL));
5165 teap = teap->next;
5166 }
5167 command_print(CMD, "***END***");
5168 return ERROR_OK;
5169 }
5170 static int jim_target_current_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5171 {
5172 if (argc != 1) {
5173 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
5174 return JIM_ERR;
5175 }
5176 struct target *target = Jim_CmdPrivData(interp);
5177 Jim_SetResultString(interp, target_state_name(target), -1);
5178 return JIM_OK;
5179 }
5180 static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5181 {
5182 Jim_GetOptInfo goi;
5183 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5184 if (goi.argc != 1) {
5185 const char *cmd_name = Jim_GetString(argv[0], NULL);
5186 Jim_SetResultFormatted(goi.interp, "%s <eventname>", cmd_name);
5187 return JIM_ERR;
5188 }
5189 Jim_Nvp *n;
5190 int e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
5191 if (e != JIM_OK) {
5192 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
5193 return e;
5194 }
5195 struct target *target = Jim_CmdPrivData(interp);
5196 target_handle_event(target, n->value);
5197 return JIM_OK;
5198 }
5199
5200 static const struct command_registration target_instance_command_handlers[] = {
5201 {
5202 .name = "configure",
5203 .mode = COMMAND_ANY,
5204 .jim_handler = jim_target_configure,
5205 .help = "configure a new target for use",
5206 .usage = "[target_attribute ...]",
5207 },
5208 {
5209 .name = "cget",
5210 .mode = COMMAND_ANY,
5211 .jim_handler = jim_target_configure,
5212 .help = "returns the specified target attribute",
5213 .usage = "target_attribute",
5214 },
5215 {
5216 .name = "mwd",
5217 .handler = handle_mw_command,
5218 .mode = COMMAND_EXEC,
5219 .help = "Write 64-bit word(s) to target memory",
5220 .usage = "address data [count]",
5221 },
5222 {
5223 .name = "mww",
5224 .handler = handle_mw_command,
5225 .mode = COMMAND_EXEC,
5226 .help = "Write 32-bit word(s) to target memory",
5227 .usage = "address data [count]",
5228 },
5229 {
5230 .name = "mwh",
5231 .handler = handle_mw_command,
5232 .mode = COMMAND_EXEC,
5233 .help = "Write 16-bit half-word(s) to target memory",
5234 .usage = "address data [count]",
5235 },
5236 {
5237 .name = "mwb",
5238 .handler = handle_mw_command,
5239 .mode = COMMAND_EXEC,
5240 .help = "Write byte(s) to target memory",
5241 .usage = "address data [count]",
5242 },
5243 {
5244 .name = "mdd",
5245 .handler = handle_md_command,
5246 .mode = COMMAND_EXEC,
5247 .help = "Display target memory as 64-bit words",
5248 .usage = "address [count]",
5249 },
5250 {
5251 .name = "mdw",
5252 .handler = handle_md_command,
5253 .mode = COMMAND_EXEC,
5254 .help = "Display target memory as 32-bit words",
5255 .usage = "address [count]",
5256 },
5257 {
5258 .name = "mdh",
5259 .handler = handle_md_command,
5260 .mode = COMMAND_EXEC,
5261 .help = "Display target memory as 16-bit half-words",
5262 .usage = "address [count]",
5263 },
5264 {
5265 .name = "mdb",
5266 .handler = handle_md_command,
5267 .mode = COMMAND_EXEC,
5268 .help = "Display target memory as 8-bit bytes",
5269 .usage = "address [count]",
5270 },
5271 {
5272 .name = "array2mem",
5273 .mode = COMMAND_EXEC,
5274 .jim_handler = jim_target_array2mem,
5275 .help = "Writes Tcl array of 8/16/32 bit numbers "
5276 "to target memory",
5277 .usage = "arrayname bitwidth address count",
5278 },
5279 {
5280 .name = "mem2array",
5281 .mode = COMMAND_EXEC,
5282 .jim_handler = jim_target_mem2array,
5283 .help = "Loads Tcl array of 8/16/32 bit numbers "
5284 "from target memory",
5285 .usage = "arrayname bitwidth address count",
5286 },
5287 {
5288 .name = "eventlist",
5289 .handler = handle_target_event_list,
5290 .mode = COMMAND_EXEC,
5291 .help = "displays a table of events defined for this target",
5292 .usage = "",
5293 },
5294 {
5295 .name = "curstate",
5296 .mode = COMMAND_EXEC,
5297 .jim_handler = jim_target_current_state,
5298 .help = "displays the current state of this target",
5299 },
5300 {
5301 .name = "arp_examine",
5302 .mode = COMMAND_EXEC,
5303 .jim_handler = jim_target_examine,
5304 .help = "used internally for reset processing",
5305 .usage = "['allow-defer']",
5306 },
5307 {
5308 .name = "was_examined",
5309 .mode = COMMAND_EXEC,
5310 .jim_handler = jim_target_was_examined,
5311 .help = "used internally for reset processing",
5312 },
5313 {
5314 .name = "examine_deferred",
5315 .mode = COMMAND_EXEC,
5316 .jim_handler = jim_target_examine_deferred,
5317 .help = "used internally for reset processing",
5318 },
5319 {
5320 .name = "arp_halt_gdb",
5321 .mode = COMMAND_EXEC,
5322 .jim_handler = jim_target_halt_gdb,
5323 .help = "used internally for reset processing to halt GDB",
5324 },
5325 {
5326 .name = "arp_poll",
5327 .mode = COMMAND_EXEC,
5328 .jim_handler = jim_target_poll,
5329 .help = "used internally for reset processing",
5330 },
5331 {
5332 .name = "arp_reset",
5333 .mode = COMMAND_EXEC,
5334 .jim_handler = jim_target_reset,
5335 .help = "used internally for reset processing",
5336 },
5337 {
5338 .name = "arp_halt",
5339 .mode = COMMAND_EXEC,
5340 .jim_handler = jim_target_halt,
5341 .help = "used internally for reset processing",
5342 },
5343 {
5344 .name = "arp_waitstate",
5345 .mode = COMMAND_EXEC,
5346 .jim_handler = jim_target_wait_state,
5347 .help = "used internally for reset processing",
5348 },
5349 {
5350 .name = "invoke-event",
5351 .mode = COMMAND_EXEC,
5352 .jim_handler = jim_target_invoke_event,
5353 .help = "invoke handler for specified event",
5354 .usage = "event_name",
5355 },
5356 COMMAND_REGISTRATION_DONE
5357 };
5358
5359 static int target_create(Jim_GetOptInfo *goi)
5360 {
5361 Jim_Obj *new_cmd;
5362 Jim_Cmd *cmd;
5363 const char *cp;
5364 int e;
5365 int x;
5366 struct target *target;
5367 struct command_context *cmd_ctx;
5368
5369 cmd_ctx = current_command_context(goi->interp);
5370 assert(cmd_ctx != NULL);
5371
5372 if (goi->argc < 3) {
5373 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
5374 return JIM_ERR;
5375 }
5376
5377 /* COMMAND */
5378 Jim_GetOpt_Obj(goi, &new_cmd);
5379 /* does this command exist? */
5380 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
5381 if (cmd) {
5382 cp = Jim_GetString(new_cmd, NULL);
5383 Jim_SetResultFormatted(goi->interp, "Command/target: %s Exists", cp);
5384 return JIM_ERR;
5385 }
5386
5387 /* TYPE */
5388 e = Jim_GetOpt_String(goi, &cp, NULL);
5389 if (e != JIM_OK)
5390 return e;
5391 struct transport *tr = get_current_transport();
5392 if (tr->override_target) {
5393 e = tr->override_target(&cp);
5394 if (e != ERROR_OK) {
5395 LOG_ERROR("The selected transport doesn't support this target");
5396 return JIM_ERR;
5397 }
5398 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
5399 }
5400 /* now does target type exist */
5401 for (x = 0 ; target_types[x] ; x++) {
5402 if (0 == strcmp(cp, target_types[x]->name)) {
5403 /* found */
5404 break;
5405 }
5406
5407 /* check for deprecated name */
5408 if (target_types[x]->deprecated_name) {
5409 if (0 == strcmp(cp, target_types[x]->deprecated_name)) {
5410 /* found */
5411 LOG_WARNING("target name is deprecated use: \'%s\'", target_types[x]->name);
5412 break;
5413 }
5414 }
5415 }
5416 if (target_types[x] == NULL) {
5417 Jim_SetResultFormatted(goi->interp, "Unknown target type %s, try one of ", cp);
5418 for (x = 0 ; target_types[x] ; x++) {
5419 if (target_types[x + 1]) {
5420 Jim_AppendStrings(goi->interp,
5421 Jim_GetResult(goi->interp),
5422 target_types[x]->name,
5423 ", ", NULL);
5424 } else {
5425 Jim_AppendStrings(goi->interp,
5426 Jim_GetResult(goi->interp),
5427 " or ",
5428 target_types[x]->name, NULL);
5429 }
5430 }
5431 return JIM_ERR;
5432 }
5433
5434 /* Create it */
5435 target = calloc(1, sizeof(struct target));
5436 /* set target number */
5437 target->target_number = new_target_number();
5438 cmd_ctx->current_target = target;
5439
5440 /* allocate memory for each unique target type */
5441 target->type = calloc(1, sizeof(struct target_type));
5442
5443 memcpy(target->type, target_types[x], sizeof(struct target_type));
5444
5445 /* will be set by "-endian" */
5446 target->endianness = TARGET_ENDIAN_UNKNOWN;
5447
5448 /* default to first core, override with -coreid */
5449 target->coreid = 0;
5450
5451 target->working_area = 0x0;
5452 target->working_area_size = 0x0;
5453 target->working_areas = NULL;
5454 target->backup_working_area = 0;
5455
5456 target->state = TARGET_UNKNOWN;
5457 target->debug_reason = DBG_REASON_UNDEFINED;
5458 target->reg_cache = NULL;
5459 target->breakpoints = NULL;
5460 target->watchpoints = NULL;
5461 target->next = NULL;
5462 target->arch_info = NULL;
5463
5464 target->verbose_halt_msg = true;
5465
5466 target->halt_issued = false;
5467
5468 /* initialize trace information */
5469 target->trace_info = calloc(1, sizeof(struct trace));
5470
5471 target->dbgmsg = NULL;
5472 target->dbg_msg_enabled = 0;
5473
5474 target->endianness = TARGET_ENDIAN_UNKNOWN;
5475
5476 target->rtos = NULL;
5477 target->rtos_auto_detect = false;
5478
5479 target->gdb_port_override = NULL;
5480
5481 /* Do the rest as "configure" options */
5482 goi->isconfigure = 1;
5483 e = target_configure(goi, target);
5484
5485 if (e == JIM_OK) {
5486 if (target->has_dap) {
5487 if (!target->dap_configured) {
5488 Jim_SetResultString(goi->interp, "-dap ?name? required when creating target", -1);
5489 e = JIM_ERR;
5490 }
5491 } else {
5492 if (!target->tap_configured) {
5493 Jim_SetResultString(goi->interp, "-chain-position ?name? required when creating target", -1);
5494 e = JIM_ERR;
5495 }
5496 }
5497 /* tap must be set after target was configured */
5498 if (target->tap == NULL)
5499 e = JIM_ERR;
5500 }
5501
5502 if (e != JIM_OK) {
5503 free(target->gdb_port_override);
5504 free(target->type);
5505 free(target);
5506 return e;
5507 }
5508
5509 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
5510 /* default endian to little if not specified */
5511 target->endianness = TARGET_LITTLE_ENDIAN;
5512 }
5513
5514 cp = Jim_GetString(new_cmd, NULL);
5515 target->cmd_name = strdup(cp);
5516
5517 if (target->type->target_create) {
5518 e = (*(target->type->target_create))(target, goi->interp);
5519 if (e != ERROR_OK) {
5520 LOG_DEBUG("target_create failed");
5521 free(target->gdb_port_override);
5522 free(target->type);
5523 free(target->cmd_name);
5524 free(target);
5525 return JIM_ERR;
5526 }
5527 }
5528
5529 /* create the target specific commands */
5530 if (target->type->commands) {
5531 e = register_commands(cmd_ctx, NULL, target->type->commands);
5532 if (ERROR_OK != e)
5533 LOG_ERROR("unable to register '%s' commands", cp);
5534 }
5535
5536 /* append to end of list */
5537 {
5538 struct target **tpp;
5539 tpp = &(all_targets);
5540 while (*tpp)
5541 tpp = &((*tpp)->next);
5542 *tpp = target;
5543 }
5544
5545 /* now - create the new target name command */
5546 const struct command_registration target_subcommands[] = {
5547 {
5548 .chain = target_instance_command_handlers,
5549 },
5550 {
5551 .chain = target->type->commands,
5552 },
5553 COMMAND_REGISTRATION_DONE
5554 };
5555 const struct command_registration target_commands[] = {
5556 {
5557 .name = cp,
5558 .mode = COMMAND_ANY,
5559 .help = "target command group",
5560 .usage = "",
5561 .chain = target_subcommands,
5562 },
5563 COMMAND_REGISTRATION_DONE
5564 };
5565 e = register_commands(cmd_ctx, NULL, target_commands);
5566 if (ERROR_OK != e)
5567 return JIM_ERR;
5568
5569 struct command *c = command_find_in_context(cmd_ctx, cp);
5570 assert(c);
5571 command_set_handler_data(c, target);
5572
5573 return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
5574 }
5575
5576 static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5577 {
5578 if (argc != 1) {
5579 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5580 return JIM_ERR;
5581 }
5582 struct command_context *cmd_ctx = current_command_context(interp);
5583 assert(cmd_ctx != NULL);
5584
5585 Jim_SetResultString(interp, target_name(get_current_target(cmd_ctx)), -1);
5586 return JIM_OK;
5587 }
5588
5589 static int jim_target_types(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5590 {
5591 if (argc != 1) {
5592 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5593 return JIM_ERR;
5594 }
5595 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
5596 for (unsigned x = 0; NULL != target_types[x]; x++) {
5597 Jim_ListAppendElement(interp, Jim_GetResult(interp),
5598 Jim_NewStringObj(interp, target_types[x]->name, -1));
5599 }
5600 return JIM_OK;
5601 }
5602
5603 static int jim_target_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5604 {
5605 if (argc != 1) {
5606 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5607 return JIM_ERR;
5608 }
5609 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
5610 struct target *target = all_targets;
5611 while (target) {
5612 Jim_ListAppendElement(interp, Jim_GetResult(interp),
5613 Jim_NewStringObj(interp, target_name(target), -1));
5614 target = target->next;
5615 }
5616 return JIM_OK;
5617 }
5618
5619 static int jim_target_smp(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5620 {
5621 int i;
5622 const char *targetname;
5623 int retval, len;
5624 struct target *target = (struct target *) NULL;
5625 struct target_list *head, *curr, *new;
5626 curr = (struct target_list *) NULL;
5627 head = (struct target_list *) NULL;
5628
5629 retval = 0;
5630 LOG_DEBUG("%d", argc);
5631 /* argv[1] = target to associate in smp
5632 * argv[2] = target to assoicate in smp
5633 * argv[3] ...
5634 */
5635
5636 for (i = 1; i < argc; i++) {
5637
5638 targetname = Jim_GetString(argv[i], &len);
5639 target = get_target(targetname);
5640 LOG_DEBUG("%s ", targetname);
5641 if (target) {
5642 new = malloc(sizeof(struct target_list));
5643 new->target = target;
5644 new->next = (struct target_list *)NULL;
5645 if (head == (struct target_list *)NULL) {
5646 head = new;
5647 curr = head;
5648 } else {
5649 curr->next = new;
5650 curr = new;
5651 }
5652 }
5653 }
5654 /* now parse the list of cpu and put the target in smp mode*/
5655 curr = head;
5656
5657 while (curr != (struct target_list *)NULL) {
5658 target = curr->target;
5659 target->smp = 1;
5660 target->head = head;
5661 curr = curr->next;
5662 }
5663
5664 if (target && target->rtos)
5665 retval = rtos_smp_init(head->target);
5666
5667 return retval;
5668 }
5669
5670
5671 static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5672 {
5673 Jim_GetOptInfo goi;
5674 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5675 if (goi.argc < 3) {
5676 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
5677 "<name> <target_type> [<target_options> ...]");
5678 return JIM_ERR;
5679 }
5680 return target_create(&goi);
5681 }
5682
5683 static const struct command_registration target_subcommand_handlers[] = {
5684 {
5685 .name = "init",
5686 .mode = COMMAND_CONFIG,
5687 .handler = handle_target_init_command,
5688 .help = "initialize targets",
5689 .usage = "",
5690 },
5691 {
5692 .name = "create",
5693 .mode = COMMAND_CONFIG,
5694 .jim_handler = jim_target_create,
5695 .usage = "name type '-chain-position' name [options ...]",
5696 .help = "Creates and selects a new target",
5697 },
5698 {
5699 .name = "current",
5700 .mode = COMMAND_ANY,
5701 .jim_handler = jim_target_current,
5702 .help = "Returns the currently selected target",
5703 },
5704 {
5705 .name = "types",
5706 .mode = COMMAND_ANY,
5707 .jim_handler = jim_target_types,
5708 .help = "Returns the available target types as "
5709 "a list of strings",
5710 },
5711 {
5712 .name = "names",
5713 .mode = COMMAND_ANY,
5714 .jim_handler = jim_target_names,
5715 .help = "Returns the names of all targets as a list of strings",
5716 },
5717 {
5718 .name = "smp",
5719 .mode = COMMAND_ANY,
5720 .jim_handler = jim_target_smp,
5721 .usage = "targetname1 targetname2 ...",
5722 .help = "gather several target in a smp list"
5723 },
5724
5725 COMMAND_REGISTRATION_DONE
5726 };
5727
5728 struct FastLoad {
5729 target_addr_t address;
5730 uint8_t *data;
5731 int length;
5732
5733 };
5734
5735 static int fastload_num;
5736 static struct FastLoad *fastload;
5737
5738 static void free_fastload(void)
5739 {
5740 if (fastload != NULL) {
5741 int i;
5742 for (i = 0; i < fastload_num; i++) {
5743 if (fastload[i].data)
5744 free(fastload[i].data);
5745 }
5746 free(fastload);
5747 fastload = NULL;
5748 }
5749 }
5750
5751 COMMAND_HANDLER(handle_fast_load_image_command)
5752 {
5753 uint8_t *buffer;
5754 size_t buf_cnt;
5755 uint32_t image_size;
5756 target_addr_t min_address = 0;
5757 target_addr_t max_address = -1;
5758 int i;
5759
5760 struct image image;
5761
5762 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
5763 &image, &min_address, &max_address);
5764 if (ERROR_OK != retval)
5765 return retval;
5766
5767 struct duration bench;
5768 duration_start(&bench);
5769
5770 retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL);
5771 if (retval != ERROR_OK)
5772 return retval;
5773
5774 image_size = 0x0;
5775 retval = ERROR_OK;
5776 fastload_num = image.num_sections;
5777 fastload = malloc(sizeof(struct FastLoad)*image.num_sections);
5778 if (fastload == NULL) {
5779 command_print(CMD, "out of memory");
5780 image_close(&image);
5781 return ERROR_FAIL;
5782 }
5783 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
5784 for (i = 0; i < image.num_sections; i++) {
5785 buffer = malloc(image.sections[i].size);
5786 if (buffer == NULL) {
5787 command_print(CMD, "error allocating buffer for section (%d bytes)",
5788 (int)(image.sections[i].size));
5789 retval = ERROR_FAIL;
5790 break;
5791 }
5792
5793 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
5794 if (retval != ERROR_OK) {
5795 free(buffer);
5796 break;
5797 }
5798
5799 uint32_t offset = 0;
5800 uint32_t length = buf_cnt;
5801
5802 /* DANGER!!! beware of unsigned comparision here!!! */
5803
5804 if ((image.sections[i].base_address + buf_cnt >= min_address) &&
5805 (image.sections[i].base_address < max_address)) {
5806 if (image.sections[i].base_address < min_address) {
5807 /* clip addresses below */
5808 offset += min_address-image.sections[i].base_address;
5809 length -= offset;
5810 }
5811
5812 if (image.sections[i].base_address + buf_cnt > max_address)
5813 length -= (image.sections[i].base_address + buf_cnt)-max_address;
5814
5815 fastload[i].address = image.sections[i].base_address + offset;
5816 fastload[i].data = malloc(length);
5817 if (fastload[i].data == NULL) {
5818 free(buffer);
5819 command_print(CMD, "error allocating buffer for section (%" PRIu32 " bytes)",
5820 length);
5821 retval = ERROR_FAIL;
5822 break;
5823 }
5824 memcpy(fastload[i].data, buffer + offset, length);
5825 fastload[i].length = length;
5826
5827 image_size += length;
5828 command_print(CMD, "%u bytes written at address 0x%8.8x",
5829 (unsigned int)length,
5830 ((unsigned int)(image.sections[i].base_address + offset)));
5831 }
5832
5833 free(buffer);
5834 }
5835
5836 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
5837 command_print(CMD, "Loaded %" PRIu32 " bytes "
5838 "in %fs (%0.3f KiB/s)", image_size,
5839 duration_elapsed(&bench), duration_kbps(&bench, image_size));
5840
5841 command_print(CMD,
5842 "WARNING: image has not been loaded to target!"
5843 "You can issue a 'fast_load' to finish loading.");
5844 }
5845
5846 image_close(&image);
5847
5848 if (retval != ERROR_OK)
5849 free_fastload();
5850
5851 return retval;
5852 }
5853
5854 COMMAND_HANDLER(handle_fast_load_command)
5855 {
5856 if (CMD_ARGC > 0)
5857 return ERROR_COMMAND_SYNTAX_ERROR;
5858 if (fastload == NULL) {
5859 LOG_ERROR("No image in memory");
5860 return ERROR_FAIL;
5861 }
5862 int i;
5863 int64_t ms = timeval_ms();
5864 int size = 0;
5865 int retval = ERROR_OK;
5866 for (i = 0; i < fastload_num; i++) {
5867 struct target *target = get_current_target(CMD_CTX);
5868 command_print(CMD, "Write to 0x%08x, length 0x%08x",
5869 (unsigned int)(fastload[i].address),
5870 (unsigned int)(fastload[i].length));
5871 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
5872 if (retval != ERROR_OK)
5873 break;
5874 size += fastload[i].length;
5875 }
5876 if (retval == ERROR_OK) {
5877 int64_t after = timeval_ms();
5878 command_print(CMD, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
5879 }
5880 return retval;
5881 }
5882
5883 static const struct command_registration target_command_handlers[] = {
5884 {
5885 .name = "targets",
5886 .handler = handle_targets_command,
5887 .mode = COMMAND_ANY,
5888 .help = "change current default target (one parameter) "
5889 "or prints table of all targets (no parameters)",
5890 .usage = "[target]",
5891 },
5892 {
5893 .name = "target",
5894 .mode = COMMAND_CONFIG,
5895 .help = "configure target",
5896 .chain = target_subcommand_handlers,
5897 .usage = "",
5898 },
5899 COMMAND_REGISTRATION_DONE
5900 };
5901
5902 int target_register_commands(struct command_context *cmd_ctx)
5903 {
5904 return register_commands(cmd_ctx, NULL, target_command_handlers);
5905 }
5906
5907 static bool target_reset_nag = true;
5908
5909 bool get_target_reset_nag(void)
5910 {
5911 return target_reset_nag;
5912 }
5913
5914 COMMAND_HANDLER(handle_target_reset_nag)
5915 {
5916 return CALL_COMMAND_HANDLER(handle_command_parse_bool,
5917 &target_reset_nag, "Nag after each reset about options to improve "
5918 "performance");
5919 }
5920
5921 COMMAND_HANDLER(handle_ps_command)
5922 {
5923 struct target *target = get_current_target(CMD_CTX);
5924 char *display;
5925 if (target->state != TARGET_HALTED) {
5926 LOG_INFO("target not halted !!");
5927 return ERROR_OK;
5928 }
5929
5930 if ((target->rtos) && (target->rtos->type)
5931 && (target->rtos->type->ps_command)) {
5932 display = target->rtos->type->ps_command(target);
5933 command_print(CMD, "%s", display);
5934 free(display);
5935 return ERROR_OK;
5936 } else {
5937 LOG_INFO("failed");
5938 return ERROR_TARGET_FAILURE;
5939 }
5940 }
5941
5942 static void binprint(struct command_invocation *cmd, const char *text, const uint8_t *buf, int size)
5943 {
5944 if (text != NULL)
5945 command_print_sameline(cmd, "%s", text);
5946 for (int i = 0; i < size; i++)
5947 command_print_sameline(cmd, " %02x", buf[i]);
5948 command_print(cmd, " ");
5949 }
5950
5951 COMMAND_HANDLER(handle_test_mem_access_command)
5952 {
5953 struct target *target = get_current_target(CMD_CTX);
5954 uint32_t test_size;
5955 int retval = ERROR_OK;
5956
5957 if (target->state != TARGET_HALTED) {
5958 LOG_INFO("target not halted !!");
5959 return ERROR_FAIL;
5960 }
5961
5962 if (CMD_ARGC != 1)
5963 return ERROR_COMMAND_SYNTAX_ERROR;
5964
5965 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], test_size);
5966
5967 /* Test reads */
5968 size_t num_bytes = test_size + 4;
5969
5970 struct working_area *wa = NULL;
5971 retval = target_alloc_working_area(target, num_bytes, &wa);
5972 if (retval != ERROR_OK) {
5973 LOG_ERROR("Not enough working area");
5974 return ERROR_FAIL;
5975 }
5976
5977 uint8_t *test_pattern = malloc(num_bytes);
5978
5979 for (size_t i = 0; i < num_bytes; i++)
5980 test_pattern[i] = rand();
5981
5982 retval = target_write_memory(target, wa->address, 1, num_bytes, test_pattern);
5983 if (retval != ERROR_OK) {
5984 LOG_ERROR("Test pattern write failed");
5985 goto out;
5986 }
5987
5988 for (int host_offset = 0; host_offset <= 1; host_offset++) {
5989 for (int size = 1; size <= 4; size *= 2) {
5990 for (int offset = 0; offset < 4; offset++) {
5991 uint32_t count = test_size / size;
5992 size_t host_bufsiz = (count + 2) * size + host_offset;
5993 uint8_t *read_ref = malloc(host_bufsiz);
5994 uint8_t *read_buf = malloc(host_bufsiz);
5995
5996 for (size_t i = 0; i < host_bufsiz; i++) {
5997 read_ref[i] = rand();
5998 read_buf[i] = read_ref[i];
5999 }
6000 command_print_sameline(CMD,
6001 "Test read %" PRIu32 " x %d @ %d to %saligned buffer: ", count,
6002 size, offset, host_offset ? "un" : "");
6003
6004 struct duration bench;
6005 duration_start(&bench);
6006
6007 retval = target_read_memory(target, wa->address + offset, size, count,
6008 read_buf + size + host_offset);
6009
6010 duration_measure(&bench);
6011
6012 if (retval == ERROR_TARGET_UNALIGNED_ACCESS) {
6013 command_print(CMD, "Unsupported alignment");
6014 goto next;
6015 } else if (retval != ERROR_OK) {
6016 command_print(CMD, "Memory read failed");
6017 goto next;
6018 }
6019
6020 /* replay on host */
6021 memcpy(read_ref + size + host_offset, test_pattern + offset, count * size);
6022
6023 /* check result */
6024 int result = memcmp(read_ref, read_buf, host_bufsiz);
6025 if (result == 0) {
6026 command_print(CMD, "Pass in %fs (%0.3f KiB/s)",
6027 duration_elapsed(&bench),
6028 duration_kbps(&bench, count * size));
6029 } else {
6030 command_print(CMD, "Compare failed");
6031 binprint(CMD, "ref:", read_ref, host_bufsiz);
6032 binprint(CMD, "buf:", read_buf, host_bufsiz);
6033 }
6034 next:
6035 free(read_ref);
6036 free(read_buf);
6037 }
6038 }
6039 }
6040
6041 out:
6042 free(test_pattern);
6043
6044 if (wa != NULL)
6045 target_free_working_area(target, wa);
6046
6047 /* Test writes */
6048 num_bytes = test_size + 4 + 4 + 4;
6049
6050 retval = target_alloc_working_area(target, num_bytes, &wa);
6051 if (retval != ERROR_OK) {
6052 LOG_ERROR("Not enough working area");
6053 return ERROR_FAIL;
6054 }
6055
6056 test_pattern = malloc(num_bytes);
6057
6058 for (size_t i = 0; i < num_bytes; i++)
6059 test_pattern[i] = rand();
6060
6061 for (int host_offset = 0; host_offset <= 1; host_offset++) {
6062 for (int size = 1; size <= 4; size *= 2) {
6063 for (int offset = 0; offset < 4; offset++) {
6064 uint32_t count = test_size / size;
6065 size_t host_bufsiz = count * size + host_offset;
6066 uint8_t *read_ref = malloc(num_bytes);
6067 uint8_t *read_buf = malloc(num_bytes);
6068 uint8_t *write_buf = malloc(host_bufsiz);
6069
6070 for (size_t i = 0; i < host_bufsiz; i++)
6071 write_buf[i] = rand();
6072 command_print_sameline(CMD,
6073 "Test write %" PRIu32 " x %d @ %d from %saligned buffer: ", count,
6074 size, offset, host_offset ? "un" : "");
6075
6076 retval = target_write_memory(target, wa->address, 1, num_bytes, test_pattern);
6077 if (retval != ERROR_OK) {
6078 command_print(CMD, "Test pattern write failed");
6079 goto nextw;
6080 }
6081
6082 /* replay on host */
6083 memcpy(read_ref, test_pattern, num_bytes);
6084 memcpy(read_ref + size + offset, write_buf + host_offset, count * size);
6085
6086 struct duration bench;
6087 duration_start(&bench);
6088
6089 retval = target_write_memory(target, wa->address + size + offset, size, count,
6090 write_buf + host_offset);
6091
6092 duration_measure(&bench);
6093
6094 if (retval == ERROR_TARGET_UNALIGNED_ACCESS) {
6095 command_print(CMD, "Unsupported alignment");
6096 goto nextw;
6097 } else if (retval != ERROR_OK) {
6098 command_print(CMD, "Memory write failed");
6099 goto nextw;
6100 }
6101
6102 /* read back */
6103 retval = target_read_memory(target, wa->address, 1, num_bytes, read_buf);
6104 if (retval != ERROR_OK) {
6105 command_print(CMD, "Test pattern write failed");
6106 goto nextw;
6107 }
6108
6109 /* check result */
6110 int result = memcmp(read_ref, read_buf, num_bytes);
6111 if (result == 0) {
6112 command_print(CMD, "Pass in %fs (%0.3f KiB/s)",
6113 duration_elapsed(&bench),
6114 duration_kbps(&bench, count * size));
6115 } else {
6116 command_print(CMD, "Compare failed");
6117 binprint(CMD, "ref:", read_ref, num_bytes);
6118 binprint(CMD, "buf:", read_buf, num_bytes);
6119 }
6120 nextw:
6121 free(read_ref);
6122 free(read_buf);
6123 }
6124 }
6125 }
6126
6127 free(test_pattern);
6128
6129 if (wa != NULL)
6130 target_free_working_area(target, wa);
6131 return retval;
6132 }
6133
6134 static const struct command_registration target_exec_command_handlers[] = {
6135 {
6136 .name = "fast_load_image",
6137 .handler = handle_fast_load_image_command,
6138 .mode = COMMAND_ANY,
6139 .help = "Load image into server memory for later use by "
6140 "fast_load; primarily for profiling",
6141 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
6142 "[min_address [max_length]]",
6143 },
6144 {
6145 .name = "fast_load",
6146 .handler = handle_fast_load_command,
6147 .mode = COMMAND_EXEC,
6148 .help = "loads active fast load image to current target "
6149 "- mainly for profiling purposes",
6150 .usage = "",
6151 },
6152 {
6153 .name = "profile",
6154 .handler = handle_profile_command,
6155 .mode = COMMAND_EXEC,
6156 .usage = "seconds filename [start end]",
6157 .help = "profiling samples the CPU PC",
6158 },
6159 /** @todo don't register virt2phys() unless target supports it */
6160 {
6161 .name = "virt2phys",
6162 .handler = handle_virt2phys_command,
6163 .mode = COMMAND_ANY,
6164 .help = "translate a virtual address into a physical address",
6165 .usage = "virtual_address",
6166 },
6167 {
6168 .name = "reg",
6169 .handler = handle_reg_command,
6170 .mode = COMMAND_EXEC,
6171 .help = "display (reread from target with \"force\") or set a register; "
6172 "with no arguments, displays all registers and their values",
6173 .usage = "[(register_number|register_name) [(value|'force')]]",
6174 },
6175 {
6176 .name = "poll",
6177 .handler = handle_poll_command,
6178 .mode = COMMAND_EXEC,
6179 .help = "poll target state; or reconfigure background polling",
6180 .usage = "['on'|'off']",
6181 },
6182 {
6183 .name = "wait_halt",
6184 .handler = handle_wait_halt_command,
6185 .mode = COMMAND_EXEC,
6186 .help = "wait up to the specified number of milliseconds "
6187 "(default 5000) for a previously requested halt",
6188 .usage = "[milliseconds]",
6189 },
6190 {
6191 .name = "halt",
6192 .handler = handle_halt_command,
6193 .mode = COMMAND_EXEC,
6194 .help = "request target to halt, then wait up to the specified"
6195 "number of milliseconds (default 5000) for it to complete",
6196 .usage = "[milliseconds]",
6197 },
6198 {
6199 .name = "resume",
6200 .handler = handle_resume_command,
6201 .mode = COMMAND_EXEC,
6202 .help = "resume target execution from current PC or address",
6203 .usage = "[address]",
6204 },
6205 {
6206 .name = "reset",
6207 .handler = handle_reset_command,
6208 .mode = COMMAND_EXEC,
6209 .usage = "[run|halt|init]",
6210 .help = "Reset all targets into the specified mode."
6211 "Default reset mode is run, if not given.",
6212 },
6213 {
6214 .name = "soft_reset_halt",
6215 .handler = handle_soft_reset_halt_command,
6216 .mode = COMMAND_EXEC,
6217 .usage = "",
6218 .help = "halt the target and do a soft reset",
6219 },
6220 {
6221 .name = "step",
6222 .handler = handle_step_command,
6223 .mode = COMMAND_EXEC,
6224 .help = "step one instruction from current PC or address",
6225 .usage = "[address]",
6226 },
6227 {
6228 .name = "mdd",
6229 .handler = handle_md_command,
6230 .mode = COMMAND_EXEC,
6231 .help = "display memory double-words",
6232 .usage = "['phys'] address [count]",
6233 },
6234 {
6235 .name = "mdw",
6236 .handler = handle_md_command,
6237 .mode = COMMAND_EXEC,
6238 .help = "display memory words",
6239 .usage = "['phys'] address [count]",
6240 },
6241 {
6242 .name = "mdh",
6243 .handler = handle_md_command,
6244 .mode = COMMAND_EXEC,
6245 .help = "display memory half-words",
6246 .usage = "['phys'] address [count]",
6247 },
6248 {
6249 .name = "mdb",
6250 .handler = handle_md_command,
6251 .mode = COMMAND_EXEC,
6252 .help = "display memory bytes",
6253 .usage = "['phys'] address [count]",
6254 },
6255 {
6256 .name = "mwd",
6257 .handler = handle_mw_command,
6258 .mode = COMMAND_EXEC,
6259 .help = "write memory double-word",
6260 .usage = "['phys'] address value [count]",
6261 },
6262 {
6263 .name = "mww",
6264 .handler = handle_mw_command,
6265 .mode = COMMAND_EXEC,
6266 .help = "write memory word",
6267 .usage = "['phys'] address value [count]",
6268 },
6269 {
6270 .name = "mwh",
6271 .handler = handle_mw_command,
6272 .mode = COMMAND_EXEC,
6273 .help = "write memory half-word",
6274 .usage = "['phys'] address value [count]",
6275 },
6276 {
6277 .name = "mwb",
6278 .handler = handle_mw_command,
6279 .mode = COMMAND_EXEC,
6280 .help = "write memory byte",
6281 .usage = "['phys'] address value [count]",
6282 },
6283 {
6284 .name = "bp",
6285 .handler = handle_bp_command,
6286 .mode = COMMAND_EXEC,
6287 .help = "list or set hardware or software breakpoint",
6288 .usage = "[<address> [<asid>] <length> ['hw'|'hw_ctx']]",
6289 },
6290 {
6291 .name = "rbp",
6292 .handler = handle_rbp_command,
6293 .mode = COMMAND_EXEC,
6294 .help = "remove breakpoint",
6295 .usage = "address",
6296 },
6297 {
6298 .name = "wp",
6299 .handler = handle_wp_command,
6300 .mode = COMMAND_EXEC,
6301 .help = "list (no params) or create watchpoints",
6302 .usage = "[address length [('r'|'w'|'a') value [mask]]]",
6303 },
6304 {
6305 .name = "rwp",
6306 .handler = handle_rwp_command,
6307 .mode = COMMAND_EXEC,
6308 .help = "remove watchpoint",
6309 .usage = "address",
6310 },
6311 {
6312 .name = "load_image",
6313 .handler = handle_load_image_command,
6314 .mode = COMMAND_EXEC,
6315 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
6316 "[min_address] [max_length]",
6317 },
6318 {
6319 .name = "dump_image",
6320 .handler = handle_dump_image_command,
6321 .mode = COMMAND_EXEC,
6322 .usage = "filename address size",
6323 },
6324 {
6325 .name = "verify_image_checksum",
6326 .handler = handle_verify_image_checksum_command,
6327 .mode = COMMAND_EXEC,
6328 .usage = "filename [offset [type]]",
6329 },
6330 {
6331 .name = "verify_image",
6332 .handler = handle_verify_image_command,
6333 .mode = COMMAND_EXEC,
6334 .usage = "filename [offset [type]]",
6335 },
6336 {
6337 .name = "test_image",
6338 .handler = handle_test_image_command,
6339 .mode = COMMAND_EXEC,
6340 .usage = "filename [offset [type]]",
6341 },
6342 {
6343 .name = "mem2array",
6344 .mode = COMMAND_EXEC,
6345 .jim_handler = jim_mem2array,
6346 .help = "read 8/16/32 bit memory and return as a TCL array "
6347 "for script processing",
6348 .usage = "arrayname bitwidth address count",
6349 },
6350 {
6351 .name = "array2mem",
6352 .mode = COMMAND_EXEC,
6353 .jim_handler = jim_array2mem,
6354 .help = "convert a TCL array to memory locations "
6355 "and write the 8/16/32 bit values",
6356 .usage = "arrayname bitwidth address count",
6357 },
6358 {
6359 .name = "reset_nag",
6360 .handler = handle_target_reset_nag,
6361 .mode = COMMAND_ANY,
6362 .help = "Nag after each reset about options that could have been "
6363 "enabled to improve performance. ",
6364 .usage = "['enable'|'disable']",
6365 },
6366 {
6367 .name = "ps",
6368 .handler = handle_ps_command,
6369 .mode = COMMAND_EXEC,
6370 .help = "list all tasks ",
6371 .usage = " ",
6372 },
6373 {
6374 .name = "test_mem_access",
6375 .handler = handle_test_mem_access_command,
6376 .mode = COMMAND_EXEC,
6377 .help = "Test the target's memory access functions",
6378 .usage = "size",
6379 },
6380
6381 COMMAND_REGISTRATION_DONE
6382 };
6383 static int target_register_user_commands(struct command_context *cmd_ctx)
6384 {
6385 int retval = ERROR_OK;
6386 retval = target_request_register_commands(cmd_ctx);
6387 if (retval != ERROR_OK)
6388 return retval;
6389
6390 retval = trace_register_commands(cmd_ctx);
6391 if (retval != ERROR_OK)
6392 return retval;
6393
6394
6395 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);
6396 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)