target: Add verify_image_checksum command
[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
58 /* default halt wait timeout (ms) */
59 #define DEFAULT_HALT_TIMEOUT 5000
60
61 static int target_read_buffer_default(struct target *target, uint32_t address,
62 uint32_t count, uint8_t *buffer);
63 static int target_write_buffer_default(struct target *target, uint32_t address,
64 uint32_t count, const uint8_t *buffer);
65 static int target_array2mem(Jim_Interp *interp, struct target *target,
66 int argc, Jim_Obj * const *argv);
67 static int target_mem2array(Jim_Interp *interp, struct target *target,
68 int argc, Jim_Obj * const *argv);
69 static int target_register_user_commands(struct command_context *cmd_ctx);
70 static int target_get_gdb_fileio_info_default(struct target *target,
71 struct gdb_fileio_info *fileio_info);
72 static int target_gdb_fileio_end_default(struct target *target, int retcode,
73 int fileio_errno, bool ctrl_c);
74 static int target_profiling_default(struct target *target, uint32_t *samples,
75 uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds);
76
77 /* targets */
78 extern struct target_type arm7tdmi_target;
79 extern struct target_type arm720t_target;
80 extern struct target_type arm9tdmi_target;
81 extern struct target_type arm920t_target;
82 extern struct target_type arm966e_target;
83 extern struct target_type arm946e_target;
84 extern struct target_type arm926ejs_target;
85 extern struct target_type fa526_target;
86 extern struct target_type feroceon_target;
87 extern struct target_type dragonite_target;
88 extern struct target_type xscale_target;
89 extern struct target_type cortexm_target;
90 extern struct target_type cortexa_target;
91 extern struct target_type cortexr4_target;
92 extern struct target_type arm11_target;
93 extern struct target_type ls1_sap_target;
94 extern struct target_type mips_m4k_target;
95 extern struct target_type avr_target;
96 extern struct target_type dsp563xx_target;
97 extern struct target_type dsp5680xx_target;
98 extern struct target_type testee_target;
99 extern struct target_type avr32_ap7k_target;
100 extern struct target_type hla_target;
101 extern struct target_type nds32_v2_target;
102 extern struct target_type nds32_v3_target;
103 extern struct target_type nds32_v3m_target;
104 extern struct target_type or1k_target;
105 extern struct target_type quark_x10xx_target;
106 extern struct target_type quark_d20xx_target;
107
108 static struct target_type *target_types[] = {
109 &arm7tdmi_target,
110 &arm9tdmi_target,
111 &arm920t_target,
112 &arm720t_target,
113 &arm966e_target,
114 &arm946e_target,
115 &arm926ejs_target,
116 &fa526_target,
117 &feroceon_target,
118 &dragonite_target,
119 &xscale_target,
120 &cortexm_target,
121 &cortexa_target,
122 &cortexr4_target,
123 &arm11_target,
124 &ls1_sap_target,
125 &mips_m4k_target,
126 &avr_target,
127 &dsp563xx_target,
128 &dsp5680xx_target,
129 &testee_target,
130 &avr32_ap7k_target,
131 &hla_target,
132 &nds32_v2_target,
133 &nds32_v3_target,
134 &nds32_v3m_target,
135 &or1k_target,
136 &quark_x10xx_target,
137 &quark_d20xx_target,
138 NULL,
139 };
140
141 struct target *all_targets;
142 static struct target_event_callback *target_event_callbacks;
143 static struct target_timer_callback *target_timer_callbacks;
144 LIST_HEAD(target_reset_callback_list);
145 LIST_HEAD(target_trace_callback_list);
146 static const int polling_interval = 100;
147
148 static const Jim_Nvp nvp_assert[] = {
149 { .name = "assert", NVP_ASSERT },
150 { .name = "deassert", NVP_DEASSERT },
151 { .name = "T", NVP_ASSERT },
152 { .name = "F", NVP_DEASSERT },
153 { .name = "t", NVP_ASSERT },
154 { .name = "f", NVP_DEASSERT },
155 { .name = NULL, .value = -1 }
156 };
157
158 static const Jim_Nvp nvp_error_target[] = {
159 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
160 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
161 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
162 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
163 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
164 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
165 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
166 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
167 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
168 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
169 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
170 { .value = -1, .name = NULL }
171 };
172
173 static const char *target_strerror_safe(int err)
174 {
175 const Jim_Nvp *n;
176
177 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
178 if (n->name == NULL)
179 return "unknown";
180 else
181 return n->name;
182 }
183
184 static const Jim_Nvp nvp_target_event[] = {
185
186 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
187 { .value = TARGET_EVENT_HALTED, .name = "halted" },
188 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
189 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
190 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
191
192 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
193 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
194
195 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
196 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
197 { .value = TARGET_EVENT_RESET_ASSERT, .name = "reset-assert" },
198 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
199 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
200 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
201 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
202 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
203 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
204 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
205 { .value = TARGET_EVENT_RESET_INIT, .name = "reset-init" },
206 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
207
208 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
209 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
210
211 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
212 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
213
214 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
215 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
216
217 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
218 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
219
220 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
221 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
222
223 { .value = TARGET_EVENT_TRACE_CONFIG, .name = "trace-config" },
224
225 { .name = NULL, .value = -1 }
226 };
227
228 static const Jim_Nvp nvp_target_state[] = {
229 { .name = "unknown", .value = TARGET_UNKNOWN },
230 { .name = "running", .value = TARGET_RUNNING },
231 { .name = "halted", .value = TARGET_HALTED },
232 { .name = "reset", .value = TARGET_RESET },
233 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
234 { .name = NULL, .value = -1 },
235 };
236
237 static const Jim_Nvp nvp_target_debug_reason[] = {
238 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
239 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
240 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
241 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
242 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
243 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
244 { .name = "program-exit" , .value = DBG_REASON_EXIT },
245 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
246 { .name = NULL, .value = -1 },
247 };
248
249 static const Jim_Nvp nvp_target_endian[] = {
250 { .name = "big", .value = TARGET_BIG_ENDIAN },
251 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
252 { .name = "be", .value = TARGET_BIG_ENDIAN },
253 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
254 { .name = NULL, .value = -1 },
255 };
256
257 static const Jim_Nvp nvp_reset_modes[] = {
258 { .name = "unknown", .value = RESET_UNKNOWN },
259 { .name = "run" , .value = RESET_RUN },
260 { .name = "halt" , .value = RESET_HALT },
261 { .name = "init" , .value = RESET_INIT },
262 { .name = NULL , .value = -1 },
263 };
264
265 const char *debug_reason_name(struct target *t)
266 {
267 const char *cp;
268
269 cp = Jim_Nvp_value2name_simple(nvp_target_debug_reason,
270 t->debug_reason)->name;
271 if (!cp) {
272 LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
273 cp = "(*BUG*unknown*BUG*)";
274 }
275 return cp;
276 }
277
278 const char *target_state_name(struct target *t)
279 {
280 const char *cp;
281 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
282 if (!cp) {
283 LOG_ERROR("Invalid target state: %d", (int)(t->state));
284 cp = "(*BUG*unknown*BUG*)";
285 }
286
287 if (!target_was_examined(t) && t->defer_examine)
288 cp = "examine deferred";
289
290 return cp;
291 }
292
293 const char *target_event_name(enum target_event event)
294 {
295 const char *cp;
296 cp = Jim_Nvp_value2name_simple(nvp_target_event, event)->name;
297 if (!cp) {
298 LOG_ERROR("Invalid target event: %d", (int)(event));
299 cp = "(*BUG*unknown*BUG*)";
300 }
301 return cp;
302 }
303
304 const char *target_reset_mode_name(enum target_reset_mode reset_mode)
305 {
306 const char *cp;
307 cp = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode)->name;
308 if (!cp) {
309 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode));
310 cp = "(*BUG*unknown*BUG*)";
311 }
312 return cp;
313 }
314
315 /* determine the number of the new target */
316 static int new_target_number(void)
317 {
318 struct target *t;
319 int x;
320
321 /* number is 0 based */
322 x = -1;
323 t = all_targets;
324 while (t) {
325 if (x < t->target_number)
326 x = t->target_number;
327 t = t->next;
328 }
329 return x + 1;
330 }
331
332 /* read a uint64_t from a buffer in target memory endianness */
333 uint64_t target_buffer_get_u64(struct target *target, const uint8_t *buffer)
334 {
335 if (target->endianness == TARGET_LITTLE_ENDIAN)
336 return le_to_h_u64(buffer);
337 else
338 return be_to_h_u64(buffer);
339 }
340
341 /* read a uint32_t from a buffer in target memory endianness */
342 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
343 {
344 if (target->endianness == TARGET_LITTLE_ENDIAN)
345 return le_to_h_u32(buffer);
346 else
347 return be_to_h_u32(buffer);
348 }
349
350 /* read a uint24_t from a buffer in target memory endianness */
351 uint32_t target_buffer_get_u24(struct target *target, const uint8_t *buffer)
352 {
353 if (target->endianness == TARGET_LITTLE_ENDIAN)
354 return le_to_h_u24(buffer);
355 else
356 return be_to_h_u24(buffer);
357 }
358
359 /* read a uint16_t from a buffer in target memory endianness */
360 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
361 {
362 if (target->endianness == TARGET_LITTLE_ENDIAN)
363 return le_to_h_u16(buffer);
364 else
365 return be_to_h_u16(buffer);
366 }
367
368 /* read a uint8_t from a buffer in target memory endianness */
369 static uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
370 {
371 return *buffer & 0x0ff;
372 }
373
374 /* write a uint64_t to a buffer in target memory endianness */
375 void target_buffer_set_u64(struct target *target, uint8_t *buffer, uint64_t value)
376 {
377 if (target->endianness == TARGET_LITTLE_ENDIAN)
378 h_u64_to_le(buffer, value);
379 else
380 h_u64_to_be(buffer, value);
381 }
382
383 /* write a uint32_t to a buffer in target memory endianness */
384 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
385 {
386 if (target->endianness == TARGET_LITTLE_ENDIAN)
387 h_u32_to_le(buffer, value);
388 else
389 h_u32_to_be(buffer, value);
390 }
391
392 /* write a uint24_t to a buffer in target memory endianness */
393 void target_buffer_set_u24(struct target *target, uint8_t *buffer, uint32_t value)
394 {
395 if (target->endianness == TARGET_LITTLE_ENDIAN)
396 h_u24_to_le(buffer, value);
397 else
398 h_u24_to_be(buffer, value);
399 }
400
401 /* write a uint16_t to a buffer in target memory endianness */
402 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
403 {
404 if (target->endianness == TARGET_LITTLE_ENDIAN)
405 h_u16_to_le(buffer, value);
406 else
407 h_u16_to_be(buffer, value);
408 }
409
410 /* write a uint8_t to a buffer in target memory endianness */
411 static void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
412 {
413 *buffer = value;
414 }
415
416 /* write a uint64_t array to a buffer in target memory endianness */
417 void target_buffer_get_u64_array(struct target *target, const uint8_t *buffer, uint32_t count, uint64_t *dstbuf)
418 {
419 uint32_t i;
420 for (i = 0; i < count; i++)
421 dstbuf[i] = target_buffer_get_u64(target, &buffer[i * 8]);
422 }
423
424 /* write a uint32_t array to a buffer in target memory endianness */
425 void target_buffer_get_u32_array(struct target *target, const uint8_t *buffer, uint32_t count, uint32_t *dstbuf)
426 {
427 uint32_t i;
428 for (i = 0; i < count; i++)
429 dstbuf[i] = target_buffer_get_u32(target, &buffer[i * 4]);
430 }
431
432 /* write a uint16_t array to a buffer in target memory endianness */
433 void target_buffer_get_u16_array(struct target *target, const uint8_t *buffer, uint32_t count, uint16_t *dstbuf)
434 {
435 uint32_t i;
436 for (i = 0; i < count; i++)
437 dstbuf[i] = target_buffer_get_u16(target, &buffer[i * 2]);
438 }
439
440 /* write a uint64_t array to a buffer in target memory endianness */
441 void target_buffer_set_u64_array(struct target *target, uint8_t *buffer, uint32_t count, const uint64_t *srcbuf)
442 {
443 uint32_t i;
444 for (i = 0; i < count; i++)
445 target_buffer_set_u64(target, &buffer[i * 8], srcbuf[i]);
446 }
447
448 /* write a uint32_t array to a buffer in target memory endianness */
449 void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, const uint32_t *srcbuf)
450 {
451 uint32_t i;
452 for (i = 0; i < count; i++)
453 target_buffer_set_u32(target, &buffer[i * 4], srcbuf[i]);
454 }
455
456 /* write a uint16_t array to a buffer in target memory endianness */
457 void target_buffer_set_u16_array(struct target *target, uint8_t *buffer, uint32_t count, const uint16_t *srcbuf)
458 {
459 uint32_t i;
460 for (i = 0; i < count; i++)
461 target_buffer_set_u16(target, &buffer[i * 2], srcbuf[i]);
462 }
463
464 /* return a pointer to a configured target; id is name or number */
465 struct target *get_target(const char *id)
466 {
467 struct target *target;
468
469 /* try as tcltarget name */
470 for (target = all_targets; target; target = target->next) {
471 if (target_name(target) == NULL)
472 continue;
473 if (strcmp(id, target_name(target)) == 0)
474 return target;
475 }
476
477 /* It's OK to remove this fallback sometime after August 2010 or so */
478
479 /* no match, try as number */
480 unsigned num;
481 if (parse_uint(id, &num) != ERROR_OK)
482 return NULL;
483
484 for (target = all_targets; target; target = target->next) {
485 if (target->target_number == (int)num) {
486 LOG_WARNING("use '%s' as target identifier, not '%u'",
487 target_name(target), num);
488 return target;
489 }
490 }
491
492 return NULL;
493 }
494
495 /* returns a pointer to the n-th configured target */
496 struct target *get_target_by_num(int num)
497 {
498 struct target *target = all_targets;
499
500 while (target) {
501 if (target->target_number == num)
502 return target;
503 target = target->next;
504 }
505
506 return NULL;
507 }
508
509 struct target *get_current_target(struct command_context *cmd_ctx)
510 {
511 struct target *target = get_target_by_num(cmd_ctx->current_target);
512
513 if (target == NULL) {
514 LOG_ERROR("BUG: current_target out of bounds");
515 exit(-1);
516 }
517
518 return target;
519 }
520
521 int target_poll(struct target *target)
522 {
523 int retval;
524
525 /* We can't poll until after examine */
526 if (!target_was_examined(target)) {
527 /* Fail silently lest we pollute the log */
528 return ERROR_FAIL;
529 }
530
531 retval = target->type->poll(target);
532 if (retval != ERROR_OK)
533 return retval;
534
535 if (target->halt_issued) {
536 if (target->state == TARGET_HALTED)
537 target->halt_issued = false;
538 else {
539 int64_t t = timeval_ms() - target->halt_issued_time;
540 if (t > DEFAULT_HALT_TIMEOUT) {
541 target->halt_issued = false;
542 LOG_INFO("Halt timed out, wake up GDB.");
543 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
544 }
545 }
546 }
547
548 return ERROR_OK;
549 }
550
551 int target_halt(struct target *target)
552 {
553 int retval;
554 /* We can't poll until after examine */
555 if (!target_was_examined(target)) {
556 LOG_ERROR("Target not examined yet");
557 return ERROR_FAIL;
558 }
559
560 retval = target->type->halt(target);
561 if (retval != ERROR_OK)
562 return retval;
563
564 target->halt_issued = true;
565 target->halt_issued_time = timeval_ms();
566
567 return ERROR_OK;
568 }
569
570 /**
571 * Make the target (re)start executing using its saved execution
572 * context (possibly with some modifications).
573 *
574 * @param target Which target should start executing.
575 * @param current True to use the target's saved program counter instead
576 * of the address parameter
577 * @param address Optionally used as the program counter.
578 * @param handle_breakpoints True iff breakpoints at the resumption PC
579 * should be skipped. (For example, maybe execution was stopped by
580 * such a breakpoint, in which case it would be counterprodutive to
581 * let it re-trigger.
582 * @param debug_execution False if all working areas allocated by OpenOCD
583 * should be released and/or restored to their original contents.
584 * (This would for example be true to run some downloaded "helper"
585 * algorithm code, which resides in one such working buffer and uses
586 * another for data storage.)
587 *
588 * @todo Resolve the ambiguity about what the "debug_execution" flag
589 * signifies. For example, Target implementations don't agree on how
590 * it relates to invalidation of the register cache, or to whether
591 * breakpoints and watchpoints should be enabled. (It would seem wrong
592 * to enable breakpoints when running downloaded "helper" algorithms
593 * (debug_execution true), since the breakpoints would be set to match
594 * target firmware being debugged, not the helper algorithm.... and
595 * enabling them could cause such helpers to malfunction (for example,
596 * by overwriting data with a breakpoint instruction. On the other
597 * hand the infrastructure for running such helpers might use this
598 * procedure but rely on hardware breakpoint to detect termination.)
599 */
600 int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
601 {
602 int retval;
603
604 /* We can't poll until after examine */
605 if (!target_was_examined(target)) {
606 LOG_ERROR("Target not examined yet");
607 return ERROR_FAIL;
608 }
609
610 target_call_event_callbacks(target, TARGET_EVENT_RESUME_START);
611
612 /* note that resume *must* be asynchronous. The CPU can halt before
613 * we poll. The CPU can even halt at the current PC as a result of
614 * a software breakpoint being inserted by (a bug?) the application.
615 */
616 retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution);
617 if (retval != ERROR_OK)
618 return retval;
619
620 target_call_event_callbacks(target, TARGET_EVENT_RESUME_END);
621
622 return retval;
623 }
624
625 static int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
626 {
627 char buf[100];
628 int retval;
629 Jim_Nvp *n;
630 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
631 if (n->name == NULL) {
632 LOG_ERROR("invalid reset mode");
633 return ERROR_FAIL;
634 }
635
636 struct target *target;
637 for (target = all_targets; target; target = target->next)
638 target_call_reset_callbacks(target, reset_mode);
639
640 /* disable polling during reset to make reset event scripts
641 * more predictable, i.e. dr/irscan & pathmove in events will
642 * not have JTAG operations injected into the middle of a sequence.
643 */
644 bool save_poll = jtag_poll_get_enabled();
645
646 jtag_poll_set_enabled(false);
647
648 sprintf(buf, "ocd_process_reset %s", n->name);
649 retval = Jim_Eval(cmd_ctx->interp, buf);
650
651 jtag_poll_set_enabled(save_poll);
652
653 if (retval != JIM_OK) {
654 Jim_MakeErrorMessage(cmd_ctx->interp);
655 command_print(NULL, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx->interp), NULL));
656 return ERROR_FAIL;
657 }
658
659 /* We want any events to be processed before the prompt */
660 retval = target_call_timer_callbacks_now();
661
662 for (target = all_targets; target; target = target->next) {
663 target->type->check_reset(target);
664 target->running_alg = false;
665 }
666
667 return retval;
668 }
669
670 static int identity_virt2phys(struct target *target,
671 uint32_t virtual, uint32_t *physical)
672 {
673 *physical = virtual;
674 return ERROR_OK;
675 }
676
677 static int no_mmu(struct target *target, int *enabled)
678 {
679 *enabled = 0;
680 return ERROR_OK;
681 }
682
683 static int default_examine(struct target *target)
684 {
685 target_set_examined(target);
686 return ERROR_OK;
687 }
688
689 /* no check by default */
690 static int default_check_reset(struct target *target)
691 {
692 return ERROR_OK;
693 }
694
695 int target_examine_one(struct target *target)
696 {
697 target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_START);
698
699 int retval = target->type->examine(target);
700 if (retval != ERROR_OK)
701 return retval;
702
703 target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_END);
704
705 return ERROR_OK;
706 }
707
708 static int jtag_enable_callback(enum jtag_event event, void *priv)
709 {
710 struct target *target = priv;
711
712 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
713 return ERROR_OK;
714
715 jtag_unregister_event_callback(jtag_enable_callback, target);
716
717 return target_examine_one(target);
718 }
719
720 /* Targets that correctly implement init + examine, i.e.
721 * no communication with target during init:
722 *
723 * XScale
724 */
725 int target_examine(void)
726 {
727 int retval = ERROR_OK;
728 struct target *target;
729
730 for (target = all_targets; target; target = target->next) {
731 /* defer examination, but don't skip it */
732 if (!target->tap->enabled) {
733 jtag_register_event_callback(jtag_enable_callback,
734 target);
735 continue;
736 }
737
738 if (target->defer_examine)
739 continue;
740
741 retval = target_examine_one(target);
742 if (retval != ERROR_OK)
743 return retval;
744 }
745 return retval;
746 }
747
748 const char *target_type_name(struct target *target)
749 {
750 return target->type->name;
751 }
752
753 static int target_soft_reset_halt(struct target *target)
754 {
755 if (!target_was_examined(target)) {
756 LOG_ERROR("Target not examined yet");
757 return ERROR_FAIL;
758 }
759 if (!target->type->soft_reset_halt) {
760 LOG_ERROR("Target %s does not support soft_reset_halt",
761 target_name(target));
762 return ERROR_FAIL;
763 }
764 return target->type->soft_reset_halt(target);
765 }
766
767 /**
768 * Downloads a target-specific native code algorithm to the target,
769 * and executes it. * Note that some targets may need to set up, enable,
770 * and tear down a breakpoint (hard or * soft) to detect algorithm
771 * termination, while others may support lower overhead schemes where
772 * soft breakpoints embedded in the algorithm automatically terminate the
773 * algorithm.
774 *
775 * @param target used to run the algorithm
776 * @param arch_info target-specific description of the algorithm.
777 */
778 int target_run_algorithm(struct target *target,
779 int num_mem_params, struct mem_param *mem_params,
780 int num_reg_params, struct reg_param *reg_param,
781 uint32_t entry_point, uint32_t exit_point,
782 int timeout_ms, void *arch_info)
783 {
784 int retval = ERROR_FAIL;
785
786 if (!target_was_examined(target)) {
787 LOG_ERROR("Target not examined yet");
788 goto done;
789 }
790 if (!target->type->run_algorithm) {
791 LOG_ERROR("Target type '%s' does not support %s",
792 target_type_name(target), __func__);
793 goto done;
794 }
795
796 target->running_alg = true;
797 retval = target->type->run_algorithm(target,
798 num_mem_params, mem_params,
799 num_reg_params, reg_param,
800 entry_point, exit_point, timeout_ms, arch_info);
801 target->running_alg = false;
802
803 done:
804 return retval;
805 }
806
807 /**
808 * Downloads a target-specific native code algorithm to the target,
809 * executes and leaves it running.
810 *
811 * @param target used to run the algorithm
812 * @param arch_info target-specific description of the algorithm.
813 */
814 int target_start_algorithm(struct target *target,
815 int num_mem_params, struct mem_param *mem_params,
816 int num_reg_params, struct reg_param *reg_params,
817 uint32_t entry_point, uint32_t exit_point,
818 void *arch_info)
819 {
820 int retval = ERROR_FAIL;
821
822 if (!target_was_examined(target)) {
823 LOG_ERROR("Target not examined yet");
824 goto done;
825 }
826 if (!target->type->start_algorithm) {
827 LOG_ERROR("Target type '%s' does not support %s",
828 target_type_name(target), __func__);
829 goto done;
830 }
831 if (target->running_alg) {
832 LOG_ERROR("Target is already running an algorithm");
833 goto done;
834 }
835
836 target->running_alg = true;
837 retval = target->type->start_algorithm(target,
838 num_mem_params, mem_params,
839 num_reg_params, reg_params,
840 entry_point, exit_point, arch_info);
841
842 done:
843 return retval;
844 }
845
846 /**
847 * Waits for an algorithm started with target_start_algorithm() to complete.
848 *
849 * @param target used to run the algorithm
850 * @param arch_info target-specific description of the algorithm.
851 */
852 int target_wait_algorithm(struct target *target,
853 int num_mem_params, struct mem_param *mem_params,
854 int num_reg_params, struct reg_param *reg_params,
855 uint32_t exit_point, int timeout_ms,
856 void *arch_info)
857 {
858 int retval = ERROR_FAIL;
859
860 if (!target->type->wait_algorithm) {
861 LOG_ERROR("Target type '%s' does not support %s",
862 target_type_name(target), __func__);
863 goto done;
864 }
865 if (!target->running_alg) {
866 LOG_ERROR("Target is not running an algorithm");
867 goto done;
868 }
869
870 retval = target->type->wait_algorithm(target,
871 num_mem_params, mem_params,
872 num_reg_params, reg_params,
873 exit_point, timeout_ms, arch_info);
874 if (retval != ERROR_TARGET_TIMEOUT)
875 target->running_alg = false;
876
877 done:
878 return retval;
879 }
880
881 /**
882 * Executes a target-specific native code algorithm in the target.
883 * It differs from target_run_algorithm in that the algorithm is asynchronous.
884 * Because of this it requires an compliant algorithm:
885 * see contrib/loaders/flash/stm32f1x.S for example.
886 *
887 * @param target used to run the algorithm
888 */
889
890 int target_run_flash_async_algorithm(struct target *target,
891 const uint8_t *buffer, uint32_t count, int block_size,
892 int num_mem_params, struct mem_param *mem_params,
893 int num_reg_params, struct reg_param *reg_params,
894 uint32_t buffer_start, uint32_t buffer_size,
895 uint32_t entry_point, uint32_t exit_point, void *arch_info)
896 {
897 int retval;
898 int timeout = 0;
899
900 const uint8_t *buffer_orig = buffer;
901
902 /* Set up working area. First word is write pointer, second word is read pointer,
903 * rest is fifo data area. */
904 uint32_t wp_addr = buffer_start;
905 uint32_t rp_addr = buffer_start + 4;
906 uint32_t fifo_start_addr = buffer_start + 8;
907 uint32_t fifo_end_addr = buffer_start + buffer_size;
908
909 uint32_t wp = fifo_start_addr;
910 uint32_t rp = fifo_start_addr;
911
912 /* validate block_size is 2^n */
913 assert(!block_size || !(block_size & (block_size - 1)));
914
915 retval = target_write_u32(target, wp_addr, wp);
916 if (retval != ERROR_OK)
917 return retval;
918 retval = target_write_u32(target, rp_addr, rp);
919 if (retval != ERROR_OK)
920 return retval;
921
922 /* Start up algorithm on target and let it idle while writing the first chunk */
923 retval = target_start_algorithm(target, num_mem_params, mem_params,
924 num_reg_params, reg_params,
925 entry_point,
926 exit_point,
927 arch_info);
928
929 if (retval != ERROR_OK) {
930 LOG_ERROR("error starting target flash write algorithm");
931 return retval;
932 }
933
934 while (count > 0) {
935
936 retval = target_read_u32(target, rp_addr, &rp);
937 if (retval != ERROR_OK) {
938 LOG_ERROR("failed to get read pointer");
939 break;
940 }
941
942 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32 " wp 0x%" PRIx32 " rp 0x%" PRIx32,
943 (size_t) (buffer - buffer_orig), count, wp, rp);
944
945 if (rp == 0) {
946 LOG_ERROR("flash write algorithm aborted by target");
947 retval = ERROR_FLASH_OPERATION_FAILED;
948 break;
949 }
950
951 if (((rp - fifo_start_addr) & (block_size - 1)) || rp < fifo_start_addr || rp >= fifo_end_addr) {
952 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32, rp);
953 break;
954 }
955
956 /* Count the number of bytes available in the fifo without
957 * crossing the wrap around. Make sure to not fill it completely,
958 * because that would make wp == rp and that's the empty condition. */
959 uint32_t thisrun_bytes;
960 if (rp > wp)
961 thisrun_bytes = rp - wp - block_size;
962 else if (rp > fifo_start_addr)
963 thisrun_bytes = fifo_end_addr - wp;
964 else
965 thisrun_bytes = fifo_end_addr - wp - block_size;
966
967 if (thisrun_bytes == 0) {
968 /* Throttle polling a bit if transfer is (much) faster than flash
969 * programming. The exact delay shouldn't matter as long as it's
970 * less than buffer size / flash speed. This is very unlikely to
971 * run when using high latency connections such as USB. */
972 alive_sleep(10);
973
974 /* to stop an infinite loop on some targets check and increment a timeout
975 * this issue was observed on a stellaris using the new ICDI interface */
976 if (timeout++ >= 500) {
977 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
978 return ERROR_FLASH_OPERATION_FAILED;
979 }
980 continue;
981 }
982
983 /* reset our timeout */
984 timeout = 0;
985
986 /* Limit to the amount of data we actually want to write */
987 if (thisrun_bytes > count * block_size)
988 thisrun_bytes = count * block_size;
989
990 /* Write data to fifo */
991 retval = target_write_buffer(target, wp, thisrun_bytes, buffer);
992 if (retval != ERROR_OK)
993 break;
994
995 /* Update counters and wrap write pointer */
996 buffer += thisrun_bytes;
997 count -= thisrun_bytes / block_size;
998 wp += thisrun_bytes;
999 if (wp >= fifo_end_addr)
1000 wp = fifo_start_addr;
1001
1002 /* Store updated write pointer to target */
1003 retval = target_write_u32(target, wp_addr, wp);
1004 if (retval != ERROR_OK)
1005 break;
1006 }
1007
1008 if (retval != ERROR_OK) {
1009 /* abort flash write algorithm on target */
1010 target_write_u32(target, wp_addr, 0);
1011 }
1012
1013 int retval2 = target_wait_algorithm(target, num_mem_params, mem_params,
1014 num_reg_params, reg_params,
1015 exit_point,
1016 10000,
1017 arch_info);
1018
1019 if (retval2 != ERROR_OK) {
1020 LOG_ERROR("error waiting for target flash write algorithm");
1021 retval = retval2;
1022 }
1023
1024 if (retval == ERROR_OK) {
1025 /* check if algorithm set rp = 0 after fifo writer loop finished */
1026 retval = target_read_u32(target, rp_addr, &rp);
1027 if (retval == ERROR_OK && rp == 0) {
1028 LOG_ERROR("flash write algorithm aborted by target");
1029 retval = ERROR_FLASH_OPERATION_FAILED;
1030 }
1031 }
1032
1033 return retval;
1034 }
1035
1036 int target_read_memory(struct target *target,
1037 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
1038 {
1039 if (!target_was_examined(target)) {
1040 LOG_ERROR("Target not examined yet");
1041 return ERROR_FAIL;
1042 }
1043 if (!target->type->read_memory) {
1044 LOG_ERROR("Target %s doesn't support read_memory", target_name(target));
1045 return ERROR_FAIL;
1046 }
1047 return target->type->read_memory(target, address, size, count, buffer);
1048 }
1049
1050 int target_read_phys_memory(struct target *target,
1051 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
1052 {
1053 if (!target_was_examined(target)) {
1054 LOG_ERROR("Target not examined yet");
1055 return ERROR_FAIL;
1056 }
1057 if (!target->type->read_phys_memory) {
1058 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target));
1059 return ERROR_FAIL;
1060 }
1061 return target->type->read_phys_memory(target, address, size, count, buffer);
1062 }
1063
1064 int target_write_memory(struct target *target,
1065 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
1066 {
1067 if (!target_was_examined(target)) {
1068 LOG_ERROR("Target not examined yet");
1069 return ERROR_FAIL;
1070 }
1071 if (!target->type->write_memory) {
1072 LOG_ERROR("Target %s doesn't support write_memory", target_name(target));
1073 return ERROR_FAIL;
1074 }
1075 return target->type->write_memory(target, address, size, count, buffer);
1076 }
1077
1078 int target_write_phys_memory(struct target *target,
1079 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
1080 {
1081 if (!target_was_examined(target)) {
1082 LOG_ERROR("Target not examined yet");
1083 return ERROR_FAIL;
1084 }
1085 if (!target->type->write_phys_memory) {
1086 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target));
1087 return ERROR_FAIL;
1088 }
1089 return target->type->write_phys_memory(target, address, size, count, buffer);
1090 }
1091
1092 int target_add_breakpoint(struct target *target,
1093 struct breakpoint *breakpoint)
1094 {
1095 if ((target->state != TARGET_HALTED) && (breakpoint->type != BKPT_HARD)) {
1096 LOG_WARNING("target %s is not halted", target_name(target));
1097 return ERROR_TARGET_NOT_HALTED;
1098 }
1099 return target->type->add_breakpoint(target, breakpoint);
1100 }
1101
1102 int target_add_context_breakpoint(struct target *target,
1103 struct breakpoint *breakpoint)
1104 {
1105 if (target->state != TARGET_HALTED) {
1106 LOG_WARNING("target %s is not halted", target_name(target));
1107 return ERROR_TARGET_NOT_HALTED;
1108 }
1109 return target->type->add_context_breakpoint(target, breakpoint);
1110 }
1111
1112 int target_add_hybrid_breakpoint(struct target *target,
1113 struct breakpoint *breakpoint)
1114 {
1115 if (target->state != TARGET_HALTED) {
1116 LOG_WARNING("target %s is not halted", target_name(target));
1117 return ERROR_TARGET_NOT_HALTED;
1118 }
1119 return target->type->add_hybrid_breakpoint(target, breakpoint);
1120 }
1121
1122 int target_remove_breakpoint(struct target *target,
1123 struct breakpoint *breakpoint)
1124 {
1125 return target->type->remove_breakpoint(target, breakpoint);
1126 }
1127
1128 int target_add_watchpoint(struct target *target,
1129 struct watchpoint *watchpoint)
1130 {
1131 if (target->state != TARGET_HALTED) {
1132 LOG_WARNING("target %s is not halted", target_name(target));
1133 return ERROR_TARGET_NOT_HALTED;
1134 }
1135 return target->type->add_watchpoint(target, watchpoint);
1136 }
1137 int target_remove_watchpoint(struct target *target,
1138 struct watchpoint *watchpoint)
1139 {
1140 return target->type->remove_watchpoint(target, watchpoint);
1141 }
1142 int target_hit_watchpoint(struct target *target,
1143 struct watchpoint **hit_watchpoint)
1144 {
1145 if (target->state != TARGET_HALTED) {
1146 LOG_WARNING("target %s is not halted", target->cmd_name);
1147 return ERROR_TARGET_NOT_HALTED;
1148 }
1149
1150 if (target->type->hit_watchpoint == NULL) {
1151 /* For backward compatible, if hit_watchpoint is not implemented,
1152 * return ERROR_FAIL such that gdb_server will not take the nonsense
1153 * information. */
1154 return ERROR_FAIL;
1155 }
1156
1157 return target->type->hit_watchpoint(target, hit_watchpoint);
1158 }
1159
1160 int target_get_gdb_reg_list(struct target *target,
1161 struct reg **reg_list[], int *reg_list_size,
1162 enum target_register_class reg_class)
1163 {
1164 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size, reg_class);
1165 }
1166 int target_step(struct target *target,
1167 int current, uint32_t address, int handle_breakpoints)
1168 {
1169 return target->type->step(target, current, address, handle_breakpoints);
1170 }
1171
1172 int target_get_gdb_fileio_info(struct target *target, struct gdb_fileio_info *fileio_info)
1173 {
1174 if (target->state != TARGET_HALTED) {
1175 LOG_WARNING("target %s is not halted", target->cmd_name);
1176 return ERROR_TARGET_NOT_HALTED;
1177 }
1178 return target->type->get_gdb_fileio_info(target, fileio_info);
1179 }
1180
1181 int target_gdb_fileio_end(struct target *target, int retcode, int fileio_errno, bool ctrl_c)
1182 {
1183 if (target->state != TARGET_HALTED) {
1184 LOG_WARNING("target %s is not halted", target->cmd_name);
1185 return ERROR_TARGET_NOT_HALTED;
1186 }
1187 return target->type->gdb_fileio_end(target, retcode, fileio_errno, ctrl_c);
1188 }
1189
1190 int target_profiling(struct target *target, uint32_t *samples,
1191 uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
1192 {
1193 if (target->state != TARGET_HALTED) {
1194 LOG_WARNING("target %s is not halted", target->cmd_name);
1195 return ERROR_TARGET_NOT_HALTED;
1196 }
1197 return target->type->profiling(target, samples, max_num_samples,
1198 num_samples, seconds);
1199 }
1200
1201 /**
1202 * Reset the @c examined flag for the given target.
1203 * Pure paranoia -- targets are zeroed on allocation.
1204 */
1205 static void target_reset_examined(struct target *target)
1206 {
1207 target->examined = false;
1208 }
1209
1210 static int handle_target(void *priv);
1211
1212 static int target_init_one(struct command_context *cmd_ctx,
1213 struct target *target)
1214 {
1215 target_reset_examined(target);
1216
1217 struct target_type *type = target->type;
1218 if (type->examine == NULL)
1219 type->examine = default_examine;
1220
1221 if (type->check_reset == NULL)
1222 type->check_reset = default_check_reset;
1223
1224 assert(type->init_target != NULL);
1225
1226 int retval = type->init_target(cmd_ctx, target);
1227 if (ERROR_OK != retval) {
1228 LOG_ERROR("target '%s' init failed", target_name(target));
1229 return retval;
1230 }
1231
1232 /* Sanity-check MMU support ... stub in what we must, to help
1233 * implement it in stages, but warn if we need to do so.
1234 */
1235 if (type->mmu) {
1236 if (type->virt2phys == NULL) {
1237 LOG_ERROR("type '%s' is missing virt2phys", type->name);
1238 type->virt2phys = identity_virt2phys;
1239 }
1240 } else {
1241 /* Make sure no-MMU targets all behave the same: make no
1242 * distinction between physical and virtual addresses, and
1243 * ensure that virt2phys() is always an identity mapping.
1244 */
1245 if (type->write_phys_memory || type->read_phys_memory || type->virt2phys)
1246 LOG_WARNING("type '%s' has bad MMU hooks", type->name);
1247
1248 type->mmu = no_mmu;
1249 type->write_phys_memory = type->write_memory;
1250 type->read_phys_memory = type->read_memory;
1251 type->virt2phys = identity_virt2phys;
1252 }
1253
1254 if (target->type->read_buffer == NULL)
1255 target->type->read_buffer = target_read_buffer_default;
1256
1257 if (target->type->write_buffer == NULL)
1258 target->type->write_buffer = target_write_buffer_default;
1259
1260 if (target->type->get_gdb_fileio_info == NULL)
1261 target->type->get_gdb_fileio_info = target_get_gdb_fileio_info_default;
1262
1263 if (target->type->gdb_fileio_end == NULL)
1264 target->type->gdb_fileio_end = target_gdb_fileio_end_default;
1265
1266 if (target->type->profiling == NULL)
1267 target->type->profiling = target_profiling_default;
1268
1269 return ERROR_OK;
1270 }
1271
1272 static int target_init(struct command_context *cmd_ctx)
1273 {
1274 struct target *target;
1275 int retval;
1276
1277 for (target = all_targets; target; target = target->next) {
1278 retval = target_init_one(cmd_ctx, target);
1279 if (ERROR_OK != retval)
1280 return retval;
1281 }
1282
1283 if (!all_targets)
1284 return ERROR_OK;
1285
1286 retval = target_register_user_commands(cmd_ctx);
1287 if (ERROR_OK != retval)
1288 return retval;
1289
1290 retval = target_register_timer_callback(&handle_target,
1291 polling_interval, 1, cmd_ctx->interp);
1292 if (ERROR_OK != retval)
1293 return retval;
1294
1295 return ERROR_OK;
1296 }
1297
1298 COMMAND_HANDLER(handle_target_init_command)
1299 {
1300 int retval;
1301
1302 if (CMD_ARGC != 0)
1303 return ERROR_COMMAND_SYNTAX_ERROR;
1304
1305 static bool target_initialized;
1306 if (target_initialized) {
1307 LOG_INFO("'target init' has already been called");
1308 return ERROR_OK;
1309 }
1310 target_initialized = true;
1311
1312 retval = command_run_line(CMD_CTX, "init_targets");
1313 if (ERROR_OK != retval)
1314 return retval;
1315
1316 retval = command_run_line(CMD_CTX, "init_target_events");
1317 if (ERROR_OK != retval)
1318 return retval;
1319
1320 retval = command_run_line(CMD_CTX, "init_board");
1321 if (ERROR_OK != retval)
1322 return retval;
1323
1324 LOG_DEBUG("Initializing targets...");
1325 return target_init(CMD_CTX);
1326 }
1327
1328 int target_register_event_callback(int (*callback)(struct target *target,
1329 enum target_event event, void *priv), void *priv)
1330 {
1331 struct target_event_callback **callbacks_p = &target_event_callbacks;
1332
1333 if (callback == NULL)
1334 return ERROR_COMMAND_SYNTAX_ERROR;
1335
1336 if (*callbacks_p) {
1337 while ((*callbacks_p)->next)
1338 callbacks_p = &((*callbacks_p)->next);
1339 callbacks_p = &((*callbacks_p)->next);
1340 }
1341
1342 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
1343 (*callbacks_p)->callback = callback;
1344 (*callbacks_p)->priv = priv;
1345 (*callbacks_p)->next = NULL;
1346
1347 return ERROR_OK;
1348 }
1349
1350 int target_register_reset_callback(int (*callback)(struct target *target,
1351 enum target_reset_mode reset_mode, void *priv), void *priv)
1352 {
1353 struct target_reset_callback *entry;
1354
1355 if (callback == NULL)
1356 return ERROR_COMMAND_SYNTAX_ERROR;
1357
1358 entry = malloc(sizeof(struct target_reset_callback));
1359 if (entry == NULL) {
1360 LOG_ERROR("error allocating buffer for reset callback entry");
1361 return ERROR_COMMAND_SYNTAX_ERROR;
1362 }
1363
1364 entry->callback = callback;
1365 entry->priv = priv;
1366 list_add(&entry->list, &target_reset_callback_list);
1367
1368
1369 return ERROR_OK;
1370 }
1371
1372 int target_register_trace_callback(int (*callback)(struct target *target,
1373 size_t len, uint8_t *data, void *priv), void *priv)
1374 {
1375 struct target_trace_callback *entry;
1376
1377 if (callback == NULL)
1378 return ERROR_COMMAND_SYNTAX_ERROR;
1379
1380 entry = malloc(sizeof(struct target_trace_callback));
1381 if (entry == NULL) {
1382 LOG_ERROR("error allocating buffer for trace callback entry");
1383 return ERROR_COMMAND_SYNTAX_ERROR;
1384 }
1385
1386 entry->callback = callback;
1387 entry->priv = priv;
1388 list_add(&entry->list, &target_trace_callback_list);
1389
1390
1391 return ERROR_OK;
1392 }
1393
1394 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
1395 {
1396 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
1397 struct timeval now;
1398
1399 if (callback == NULL)
1400 return ERROR_COMMAND_SYNTAX_ERROR;
1401
1402 if (*callbacks_p) {
1403 while ((*callbacks_p)->next)
1404 callbacks_p = &((*callbacks_p)->next);
1405 callbacks_p = &((*callbacks_p)->next);
1406 }
1407
1408 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
1409 (*callbacks_p)->callback = callback;
1410 (*callbacks_p)->periodic = periodic;
1411 (*callbacks_p)->time_ms = time_ms;
1412 (*callbacks_p)->removed = false;
1413
1414 gettimeofday(&now, NULL);
1415 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
1416 time_ms -= (time_ms % 1000);
1417 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
1418 if ((*callbacks_p)->when.tv_usec > 1000000) {
1419 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
1420 (*callbacks_p)->when.tv_sec += 1;
1421 }
1422
1423 (*callbacks_p)->priv = priv;
1424 (*callbacks_p)->next = NULL;
1425
1426 return ERROR_OK;
1427 }
1428
1429 int target_unregister_event_callback(int (*callback)(struct target *target,
1430 enum target_event event, void *priv), void *priv)
1431 {
1432 struct target_event_callback **p = &target_event_callbacks;
1433 struct target_event_callback *c = target_event_callbacks;
1434
1435 if (callback == NULL)
1436 return ERROR_COMMAND_SYNTAX_ERROR;
1437
1438 while (c) {
1439 struct target_event_callback *next = c->next;
1440 if ((c->callback == callback) && (c->priv == priv)) {
1441 *p = next;
1442 free(c);
1443 return ERROR_OK;
1444 } else
1445 p = &(c->next);
1446 c = next;
1447 }
1448
1449 return ERROR_OK;
1450 }
1451
1452 int target_unregister_reset_callback(int (*callback)(struct target *target,
1453 enum target_reset_mode reset_mode, void *priv), void *priv)
1454 {
1455 struct target_reset_callback *entry;
1456
1457 if (callback == NULL)
1458 return ERROR_COMMAND_SYNTAX_ERROR;
1459
1460 list_for_each_entry(entry, &target_reset_callback_list, list) {
1461 if (entry->callback == callback && entry->priv == priv) {
1462 list_del(&entry->list);
1463 free(entry);
1464 break;
1465 }
1466 }
1467
1468 return ERROR_OK;
1469 }
1470
1471 int target_unregister_trace_callback(int (*callback)(struct target *target,
1472 size_t len, uint8_t *data, void *priv), void *priv)
1473 {
1474 struct target_trace_callback *entry;
1475
1476 if (callback == NULL)
1477 return ERROR_COMMAND_SYNTAX_ERROR;
1478
1479 list_for_each_entry(entry, &target_trace_callback_list, list) {
1480 if (entry->callback == callback && entry->priv == priv) {
1481 list_del(&entry->list);
1482 free(entry);
1483 break;
1484 }
1485 }
1486
1487 return ERROR_OK;
1488 }
1489
1490 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
1491 {
1492 if (callback == NULL)
1493 return ERROR_COMMAND_SYNTAX_ERROR;
1494
1495 for (struct target_timer_callback *c = target_timer_callbacks;
1496 c; c = c->next) {
1497 if ((c->callback == callback) && (c->priv == priv)) {
1498 c->removed = true;
1499 return ERROR_OK;
1500 }
1501 }
1502
1503 return ERROR_FAIL;
1504 }
1505
1506 int target_call_event_callbacks(struct target *target, enum target_event event)
1507 {
1508 struct target_event_callback *callback = target_event_callbacks;
1509 struct target_event_callback *next_callback;
1510
1511 if (event == TARGET_EVENT_HALTED) {
1512 /* execute early halted first */
1513 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1514 }
1515
1516 LOG_DEBUG("target event %i (%s)", event,
1517 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
1518
1519 target_handle_event(target, event);
1520
1521 while (callback) {
1522 next_callback = callback->next;
1523 callback->callback(target, event, callback->priv);
1524 callback = next_callback;
1525 }
1526
1527 return ERROR_OK;
1528 }
1529
1530 int target_call_reset_callbacks(struct target *target, enum target_reset_mode reset_mode)
1531 {
1532 struct target_reset_callback *callback;
1533
1534 LOG_DEBUG("target reset %i (%s)", reset_mode,
1535 Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode)->name);
1536
1537 list_for_each_entry(callback, &target_reset_callback_list, list)
1538 callback->callback(target, reset_mode, callback->priv);
1539
1540 return ERROR_OK;
1541 }
1542
1543 int target_call_trace_callbacks(struct target *target, size_t len, uint8_t *data)
1544 {
1545 struct target_trace_callback *callback;
1546
1547 list_for_each_entry(callback, &target_trace_callback_list, list)
1548 callback->callback(target, len, data, callback->priv);
1549
1550 return ERROR_OK;
1551 }
1552
1553 static int target_timer_callback_periodic_restart(
1554 struct target_timer_callback *cb, struct timeval *now)
1555 {
1556 int time_ms = cb->time_ms;
1557 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
1558 time_ms -= (time_ms % 1000);
1559 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
1560 if (cb->when.tv_usec > 1000000) {
1561 cb->when.tv_usec = cb->when.tv_usec - 1000000;
1562 cb->when.tv_sec += 1;
1563 }
1564 return ERROR_OK;
1565 }
1566
1567 static int target_call_timer_callback(struct target_timer_callback *cb,
1568 struct timeval *now)
1569 {
1570 cb->callback(cb->priv);
1571
1572 if (cb->periodic)
1573 return target_timer_callback_periodic_restart(cb, now);
1574
1575 return target_unregister_timer_callback(cb->callback, cb->priv);
1576 }
1577
1578 static int target_call_timer_callbacks_check_time(int checktime)
1579 {
1580 static bool callback_processing;
1581
1582 /* Do not allow nesting */
1583 if (callback_processing)
1584 return ERROR_OK;
1585
1586 callback_processing = true;
1587
1588 keep_alive();
1589
1590 struct timeval now;
1591 gettimeofday(&now, NULL);
1592
1593 /* Store an address of the place containing a pointer to the
1594 * next item; initially, that's a standalone "root of the
1595 * list" variable. */
1596 struct target_timer_callback **callback = &target_timer_callbacks;
1597 while (*callback) {
1598 if ((*callback)->removed) {
1599 struct target_timer_callback *p = *callback;
1600 *callback = (*callback)->next;
1601 free(p);
1602 continue;
1603 }
1604
1605 bool call_it = (*callback)->callback &&
1606 ((!checktime && (*callback)->periodic) ||
1607 now.tv_sec > (*callback)->when.tv_sec ||
1608 (now.tv_sec == (*callback)->when.tv_sec &&
1609 now.tv_usec >= (*callback)->when.tv_usec));
1610
1611 if (call_it)
1612 target_call_timer_callback(*callback, &now);
1613
1614 callback = &(*callback)->next;
1615 }
1616
1617 callback_processing = false;
1618 return ERROR_OK;
1619 }
1620
1621 int target_call_timer_callbacks(void)
1622 {
1623 return target_call_timer_callbacks_check_time(1);
1624 }
1625
1626 /* invoke periodic callbacks immediately */
1627 int target_call_timer_callbacks_now(void)
1628 {
1629 return target_call_timer_callbacks_check_time(0);
1630 }
1631
1632 /* Prints the working area layout for debug purposes */
1633 static void print_wa_layout(struct target *target)
1634 {
1635 struct working_area *c = target->working_areas;
1636
1637 while (c) {
1638 LOG_DEBUG("%c%c 0x%08"PRIx32"-0x%08"PRIx32" (%"PRIu32" bytes)",
1639 c->backup ? 'b' : ' ', c->free ? ' ' : '*',
1640 c->address, c->address + c->size - 1, c->size);
1641 c = c->next;
1642 }
1643 }
1644
1645 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1646 static void target_split_working_area(struct working_area *area, uint32_t size)
1647 {
1648 assert(area->free); /* Shouldn't split an allocated area */
1649 assert(size <= area->size); /* Caller should guarantee this */
1650
1651 /* Split only if not already the right size */
1652 if (size < area->size) {
1653 struct working_area *new_wa = malloc(sizeof(*new_wa));
1654
1655 if (new_wa == NULL)
1656 return;
1657
1658 new_wa->next = area->next;
1659 new_wa->size = area->size - size;
1660 new_wa->address = area->address + size;
1661 new_wa->backup = NULL;
1662 new_wa->user = NULL;
1663 new_wa->free = true;
1664
1665 area->next = new_wa;
1666 area->size = size;
1667
1668 /* If backup memory was allocated to this area, it has the wrong size
1669 * now so free it and it will be reallocated if/when needed */
1670 if (area->backup) {
1671 free(area->backup);
1672 area->backup = NULL;
1673 }
1674 }
1675 }
1676
1677 /* Merge all adjacent free areas into one */
1678 static void target_merge_working_areas(struct target *target)
1679 {
1680 struct working_area *c = target->working_areas;
1681
1682 while (c && c->next) {
1683 assert(c->next->address == c->address + c->size); /* This is an invariant */
1684
1685 /* Find two adjacent free areas */
1686 if (c->free && c->next->free) {
1687 /* Merge the last into the first */
1688 c->size += c->next->size;
1689
1690 /* Remove the last */
1691 struct working_area *to_be_freed = c->next;
1692 c->next = c->next->next;
1693 if (to_be_freed->backup)
1694 free(to_be_freed->backup);
1695 free(to_be_freed);
1696
1697 /* If backup memory was allocated to the remaining area, it's has
1698 * the wrong size now */
1699 if (c->backup) {
1700 free(c->backup);
1701 c->backup = NULL;
1702 }
1703 } else {
1704 c = c->next;
1705 }
1706 }
1707 }
1708
1709 int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
1710 {
1711 /* Reevaluate working area address based on MMU state*/
1712 if (target->working_areas == NULL) {
1713 int retval;
1714 int enabled;
1715
1716 retval = target->type->mmu(target, &enabled);
1717 if (retval != ERROR_OK)
1718 return retval;
1719
1720 if (!enabled) {
1721 if (target->working_area_phys_spec) {
1722 LOG_DEBUG("MMU disabled, using physical "
1723 "address for working memory 0x%08"PRIx32,
1724 target->working_area_phys);
1725 target->working_area = target->working_area_phys;
1726 } else {
1727 LOG_ERROR("No working memory available. "
1728 "Specify -work-area-phys to target.");
1729 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1730 }
1731 } else {
1732 if (target->working_area_virt_spec) {
1733 LOG_DEBUG("MMU enabled, using virtual "
1734 "address for working memory 0x%08"PRIx32,
1735 target->working_area_virt);
1736 target->working_area = target->working_area_virt;
1737 } else {
1738 LOG_ERROR("No working memory available. "
1739 "Specify -work-area-virt to target.");
1740 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1741 }
1742 }
1743
1744 /* Set up initial working area on first call */
1745 struct working_area *new_wa = malloc(sizeof(*new_wa));
1746 if (new_wa) {
1747 new_wa->next = NULL;
1748 new_wa->size = target->working_area_size & ~3UL; /* 4-byte align */
1749 new_wa->address = target->working_area;
1750 new_wa->backup = NULL;
1751 new_wa->user = NULL;
1752 new_wa->free = true;
1753 }
1754
1755 target->working_areas = new_wa;
1756 }
1757
1758 /* only allocate multiples of 4 byte */
1759 if (size % 4)
1760 size = (size + 3) & (~3UL);
1761
1762 struct working_area *c = target->working_areas;
1763
1764 /* Find the first large enough working area */
1765 while (c) {
1766 if (c->free && c->size >= size)
1767 break;
1768 c = c->next;
1769 }
1770
1771 if (c == NULL)
1772 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1773
1774 /* Split the working area into the requested size */
1775 target_split_working_area(c, size);
1776
1777 LOG_DEBUG("allocated new working area of %"PRIu32" bytes at address 0x%08"PRIx32, size, c->address);
1778
1779 if (target->backup_working_area) {
1780 if (c->backup == NULL) {
1781 c->backup = malloc(c->size);
1782 if (c->backup == NULL)
1783 return ERROR_FAIL;
1784 }
1785
1786 int retval = target_read_memory(target, c->address, 4, c->size / 4, c->backup);
1787 if (retval != ERROR_OK)
1788 return retval;
1789 }
1790
1791 /* mark as used, and return the new (reused) area */
1792 c->free = false;
1793 *area = c;
1794
1795 /* user pointer */
1796 c->user = area;
1797
1798 print_wa_layout(target);
1799
1800 return ERROR_OK;
1801 }
1802
1803 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1804 {
1805 int retval;
1806
1807 retval = target_alloc_working_area_try(target, size, area);
1808 if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
1809 LOG_WARNING("not enough working area available(requested %"PRIu32")", size);
1810 return retval;
1811
1812 }
1813
1814 static int target_restore_working_area(struct target *target, struct working_area *area)
1815 {
1816 int retval = ERROR_OK;
1817
1818 if (target->backup_working_area && area->backup != NULL) {
1819 retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup);
1820 if (retval != ERROR_OK)
1821 LOG_ERROR("failed to restore %"PRIu32" bytes of working area at address 0x%08"PRIx32,
1822 area->size, area->address);
1823 }
1824
1825 return retval;
1826 }
1827
1828 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1829 static int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1830 {
1831 int retval = ERROR_OK;
1832
1833 if (area->free)
1834 return retval;
1835
1836 if (restore) {
1837 retval = target_restore_working_area(target, area);
1838 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1839 if (retval != ERROR_OK)
1840 return retval;
1841 }
1842
1843 area->free = true;
1844
1845 LOG_DEBUG("freed %"PRIu32" bytes of working area at address 0x%08"PRIx32,
1846 area->size, area->address);
1847
1848 /* mark user pointer invalid */
1849 /* TODO: Is this really safe? It points to some previous caller's memory.
1850 * How could we know that the area pointer is still in that place and not
1851 * some other vital data? What's the purpose of this, anyway? */
1852 *area->user = NULL;
1853 area->user = NULL;
1854
1855 target_merge_working_areas(target);
1856
1857 print_wa_layout(target);
1858
1859 return retval;
1860 }
1861
1862 int target_free_working_area(struct target *target, struct working_area *area)
1863 {
1864 return target_free_working_area_restore(target, area, 1);
1865 }
1866
1867 void target_quit(void)
1868 {
1869 struct target_event_callback *pe = target_event_callbacks;
1870 while (pe) {
1871 struct target_event_callback *t = pe->next;
1872 free(pe);
1873 pe = t;
1874 }
1875 target_event_callbacks = NULL;
1876
1877 struct target_timer_callback *pt = target_timer_callbacks;
1878 while (pt) {
1879 struct target_timer_callback *t = pt->next;
1880 free(pt);
1881 pt = t;
1882 }
1883 target_timer_callbacks = NULL;
1884
1885 for (struct target *target = all_targets;
1886 target; target = target->next) {
1887 if (target->type->deinit_target)
1888 target->type->deinit_target(target);
1889 }
1890 }
1891
1892 /* free resources and restore memory, if restoring memory fails,
1893 * free up resources anyway
1894 */
1895 static void target_free_all_working_areas_restore(struct target *target, int restore)
1896 {
1897 struct working_area *c = target->working_areas;
1898
1899 LOG_DEBUG("freeing all working areas");
1900
1901 /* Loop through all areas, restoring the allocated ones and marking them as free */
1902 while (c) {
1903 if (!c->free) {
1904 if (restore)
1905 target_restore_working_area(target, c);
1906 c->free = true;
1907 *c->user = NULL; /* Same as above */
1908 c->user = NULL;
1909 }
1910 c = c->next;
1911 }
1912
1913 /* Run a merge pass to combine all areas into one */
1914 target_merge_working_areas(target);
1915
1916 print_wa_layout(target);
1917 }
1918
1919 void target_free_all_working_areas(struct target *target)
1920 {
1921 target_free_all_working_areas_restore(target, 1);
1922 }
1923
1924 /* Find the largest number of bytes that can be allocated */
1925 uint32_t target_get_working_area_avail(struct target *target)
1926 {
1927 struct working_area *c = target->working_areas;
1928 uint32_t max_size = 0;
1929
1930 if (c == NULL)
1931 return target->working_area_size;
1932
1933 while (c) {
1934 if (c->free && max_size < c->size)
1935 max_size = c->size;
1936
1937 c = c->next;
1938 }
1939
1940 return max_size;
1941 }
1942
1943 int target_arch_state(struct target *target)
1944 {
1945 int retval;
1946 if (target == NULL) {
1947 LOG_USER("No target has been configured");
1948 return ERROR_OK;
1949 }
1950
1951 LOG_USER("%s: target state: %s", target_name(target),
1952 target_state_name(target));
1953
1954 if (target->state != TARGET_HALTED)
1955 return ERROR_OK;
1956
1957 retval = target->type->arch_state(target);
1958 return retval;
1959 }
1960
1961 static int target_get_gdb_fileio_info_default(struct target *target,
1962 struct gdb_fileio_info *fileio_info)
1963 {
1964 /* If target does not support semi-hosting function, target
1965 has no need to provide .get_gdb_fileio_info callback.
1966 It just return ERROR_FAIL and gdb_server will return "Txx"
1967 as target halted every time. */
1968 return ERROR_FAIL;
1969 }
1970
1971 static int target_gdb_fileio_end_default(struct target *target,
1972 int retcode, int fileio_errno, bool ctrl_c)
1973 {
1974 return ERROR_OK;
1975 }
1976
1977 static int target_profiling_default(struct target *target, uint32_t *samples,
1978 uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
1979 {
1980 struct timeval timeout, now;
1981
1982 gettimeofday(&timeout, NULL);
1983 timeval_add_time(&timeout, seconds, 0);
1984
1985 LOG_INFO("Starting profiling. Halting and resuming the"
1986 " target as often as we can...");
1987
1988 uint32_t sample_count = 0;
1989 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
1990 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
1991
1992 int retval = ERROR_OK;
1993 for (;;) {
1994 target_poll(target);
1995 if (target->state == TARGET_HALTED) {
1996 uint32_t t = buf_get_u32(reg->value, 0, 32);
1997 samples[sample_count++] = t;
1998 /* current pc, addr = 0, do not handle breakpoints, not debugging */
1999 retval = target_resume(target, 1, 0, 0, 0);
2000 target_poll(target);
2001 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2002 } else if (target->state == TARGET_RUNNING) {
2003 /* We want to quickly sample the PC. */
2004 retval = target_halt(target);
2005 } else {
2006 LOG_INFO("Target not halted or running");
2007 retval = ERROR_OK;
2008 break;
2009 }
2010
2011 if (retval != ERROR_OK)
2012 break;
2013
2014 gettimeofday(&now, NULL);
2015 if ((sample_count >= max_num_samples) ||
2016 ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec))) {
2017 LOG_INFO("Profiling completed. %" PRIu32 " samples.", sample_count);
2018 break;
2019 }
2020 }
2021
2022 *num_samples = sample_count;
2023 return retval;
2024 }
2025
2026 /* Single aligned words are guaranteed to use 16 or 32 bit access
2027 * mode respectively, otherwise data is handled as quickly as
2028 * possible
2029 */
2030 int target_write_buffer(struct target *target, uint32_t address, uint32_t size, const uint8_t *buffer)
2031 {
2032 LOG_DEBUG("writing buffer of %" PRIi32 " byte at 0x%8.8" PRIx32,
2033 size, address);
2034
2035 if (!target_was_examined(target)) {
2036 LOG_ERROR("Target not examined yet");
2037 return ERROR_FAIL;
2038 }
2039
2040 if (size == 0)
2041 return ERROR_OK;
2042
2043 if ((address + size - 1) < address) {
2044 /* GDB can request this when e.g. PC is 0xfffffffc */
2045 LOG_ERROR("address + size wrapped (0x%08" PRIx32 ", 0x%08" PRIx32 ")",
2046 address,
2047 size);
2048 return ERROR_FAIL;
2049 }
2050
2051 return target->type->write_buffer(target, address, size, buffer);
2052 }
2053
2054 static int target_write_buffer_default(struct target *target, uint32_t address, uint32_t count, const uint8_t *buffer)
2055 {
2056 uint32_t size;
2057
2058 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2059 * will have something to do with the size we leave to it. */
2060 for (size = 1; size < 4 && count >= size * 2 + (address & size); size *= 2) {
2061 if (address & size) {
2062 int retval = target_write_memory(target, address, size, 1, buffer);
2063 if (retval != ERROR_OK)
2064 return retval;
2065 address += size;
2066 count -= size;
2067 buffer += size;
2068 }
2069 }
2070
2071 /* Write the data with as large access size as possible. */
2072 for (; size > 0; size /= 2) {
2073 uint32_t aligned = count - count % size;
2074 if (aligned > 0) {
2075 int retval = target_write_memory(target, address, size, aligned / size, buffer);
2076 if (retval != ERROR_OK)
2077 return retval;
2078 address += aligned;
2079 count -= aligned;
2080 buffer += aligned;
2081 }
2082 }
2083
2084 return ERROR_OK;
2085 }
2086
2087 /* Single aligned words are guaranteed to use 16 or 32 bit access
2088 * mode respectively, otherwise data is handled as quickly as
2089 * possible
2090 */
2091 int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
2092 {
2093 LOG_DEBUG("reading buffer of %" PRIi32 " byte at 0x%8.8" PRIx32,
2094 size, address);
2095
2096 if (!target_was_examined(target)) {
2097 LOG_ERROR("Target not examined yet");
2098 return ERROR_FAIL;
2099 }
2100
2101 if (size == 0)
2102 return ERROR_OK;
2103
2104 if ((address + size - 1) < address) {
2105 /* GDB can request this when e.g. PC is 0xfffffffc */
2106 LOG_ERROR("address + size wrapped (0x%08" PRIx32 ", 0x%08" PRIx32 ")",
2107 address,
2108 size);
2109 return ERROR_FAIL;
2110 }
2111
2112 return target->type->read_buffer(target, address, size, buffer);
2113 }
2114
2115 static int target_read_buffer_default(struct target *target, uint32_t address, uint32_t count, uint8_t *buffer)
2116 {
2117 uint32_t size;
2118
2119 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2120 * will have something to do with the size we leave to it. */
2121 for (size = 1; size < 4 && count >= size * 2 + (address & size); size *= 2) {
2122 if (address & size) {
2123 int retval = target_read_memory(target, address, size, 1, buffer);
2124 if (retval != ERROR_OK)
2125 return retval;
2126 address += size;
2127 count -= size;
2128 buffer += size;
2129 }
2130 }
2131
2132 /* Read the data with as large access size as possible. */
2133 for (; size > 0; size /= 2) {
2134 uint32_t aligned = count - count % size;
2135 if (aligned > 0) {
2136 int retval = target_read_memory(target, address, size, aligned / size, buffer);
2137 if (retval != ERROR_OK)
2138 return retval;
2139 address += aligned;
2140 count -= aligned;
2141 buffer += aligned;
2142 }
2143 }
2144
2145 return ERROR_OK;
2146 }
2147
2148 int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
2149 {
2150 uint8_t *buffer;
2151 int retval;
2152 uint32_t i;
2153 uint32_t checksum = 0;
2154 if (!target_was_examined(target)) {
2155 LOG_ERROR("Target not examined yet");
2156 return ERROR_FAIL;
2157 }
2158
2159 retval = target->type->checksum_memory(target, address, size, &checksum);
2160 if (retval != ERROR_OK) {
2161 buffer = malloc(size);
2162 if (buffer == NULL) {
2163 LOG_ERROR("error allocating buffer for section (%" PRId32 " bytes)", size);
2164 return ERROR_COMMAND_SYNTAX_ERROR;
2165 }
2166 retval = target_read_buffer(target, address, size, buffer);
2167 if (retval != ERROR_OK) {
2168 free(buffer);
2169 return retval;
2170 }
2171
2172 /* convert to target endianness */
2173 for (i = 0; i < (size/sizeof(uint32_t)); i++) {
2174 uint32_t target_data;
2175 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
2176 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
2177 }
2178
2179 retval = image_calculate_checksum(buffer, size, &checksum);
2180 free(buffer);
2181 }
2182
2183 *crc = checksum;
2184
2185 return retval;
2186 }
2187
2188 int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank,
2189 uint8_t erased_value)
2190 {
2191 int retval;
2192 if (!target_was_examined(target)) {
2193 LOG_ERROR("Target not examined yet");
2194 return ERROR_FAIL;
2195 }
2196
2197 if (target->type->blank_check_memory == 0)
2198 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
2199
2200 retval = target->type->blank_check_memory(target, address, size, blank, erased_value);
2201
2202 return retval;
2203 }
2204
2205 int target_read_u64(struct target *target, uint64_t address, uint64_t *value)
2206 {
2207 uint8_t value_buf[8];
2208 if (!target_was_examined(target)) {
2209 LOG_ERROR("Target not examined yet");
2210 return ERROR_FAIL;
2211 }
2212
2213 int retval = target_read_memory(target, address, 8, 1, value_buf);
2214
2215 if (retval == ERROR_OK) {
2216 *value = target_buffer_get_u64(target, value_buf);
2217 LOG_DEBUG("address: 0x%" PRIx64 ", value: 0x%16.16" PRIx64 "",
2218 address,
2219 *value);
2220 } else {
2221 *value = 0x0;
2222 LOG_DEBUG("address: 0x%" PRIx64 " failed",
2223 address);
2224 }
2225
2226 return retval;
2227 }
2228
2229 int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
2230 {
2231 uint8_t value_buf[4];
2232 if (!target_was_examined(target)) {
2233 LOG_ERROR("Target not examined yet");
2234 return ERROR_FAIL;
2235 }
2236
2237 int retval = target_read_memory(target, address, 4, 1, value_buf);
2238
2239 if (retval == ERROR_OK) {
2240 *value = target_buffer_get_u32(target, value_buf);
2241 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
2242 address,
2243 *value);
2244 } else {
2245 *value = 0x0;
2246 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
2247 address);
2248 }
2249
2250 return retval;
2251 }
2252
2253 int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
2254 {
2255 uint8_t value_buf[2];
2256 if (!target_was_examined(target)) {
2257 LOG_ERROR("Target not examined yet");
2258 return ERROR_FAIL;
2259 }
2260
2261 int retval = target_read_memory(target, address, 2, 1, value_buf);
2262
2263 if (retval == ERROR_OK) {
2264 *value = target_buffer_get_u16(target, value_buf);
2265 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4" PRIx16,
2266 address,
2267 *value);
2268 } else {
2269 *value = 0x0;
2270 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
2271 address);
2272 }
2273
2274 return retval;
2275 }
2276
2277 int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
2278 {
2279 if (!target_was_examined(target)) {
2280 LOG_ERROR("Target not examined yet");
2281 return ERROR_FAIL;
2282 }
2283
2284 int retval = target_read_memory(target, address, 1, 1, value);
2285
2286 if (retval == ERROR_OK) {
2287 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2" PRIx8,
2288 address,
2289 *value);
2290 } else {
2291 *value = 0x0;
2292 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
2293 address);
2294 }
2295
2296 return retval;
2297 }
2298
2299 int target_write_u64(struct target *target, uint64_t address, uint64_t value)
2300 {
2301 int retval;
2302 uint8_t value_buf[8];
2303 if (!target_was_examined(target)) {
2304 LOG_ERROR("Target not examined yet");
2305 return ERROR_FAIL;
2306 }
2307
2308 LOG_DEBUG("address: 0x%" PRIx64 ", value: 0x%16.16" PRIx64 "",
2309 address,
2310 value);
2311
2312 target_buffer_set_u64(target, value_buf, value);
2313 retval = target_write_memory(target, address, 8, 1, value_buf);
2314 if (retval != ERROR_OK)
2315 LOG_DEBUG("failed: %i", retval);
2316
2317 return retval;
2318 }
2319
2320 int target_write_u32(struct target *target, uint32_t address, uint32_t value)
2321 {
2322 int retval;
2323 uint8_t value_buf[4];
2324 if (!target_was_examined(target)) {
2325 LOG_ERROR("Target not examined yet");
2326 return ERROR_FAIL;
2327 }
2328
2329 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
2330 address,
2331 value);
2332
2333 target_buffer_set_u32(target, value_buf, value);
2334 retval = target_write_memory(target, address, 4, 1, value_buf);
2335 if (retval != ERROR_OK)
2336 LOG_DEBUG("failed: %i", retval);
2337
2338 return retval;
2339 }
2340
2341 int target_write_u16(struct target *target, uint32_t address, uint16_t value)
2342 {
2343 int retval;
2344 uint8_t value_buf[2];
2345 if (!target_was_examined(target)) {
2346 LOG_ERROR("Target not examined yet");
2347 return ERROR_FAIL;
2348 }
2349
2350 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx16,
2351 address,
2352 value);
2353
2354 target_buffer_set_u16(target, value_buf, value);
2355 retval = target_write_memory(target, address, 2, 1, value_buf);
2356 if (retval != ERROR_OK)
2357 LOG_DEBUG("failed: %i", retval);
2358
2359 return retval;
2360 }
2361
2362 int target_write_u8(struct target *target, uint32_t address, uint8_t value)
2363 {
2364 int retval;
2365 if (!target_was_examined(target)) {
2366 LOG_ERROR("Target not examined yet");
2367 return ERROR_FAIL;
2368 }
2369
2370 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2" PRIx8,
2371 address, value);
2372
2373 retval = target_write_memory(target, address, 1, 1, &value);
2374 if (retval != ERROR_OK)
2375 LOG_DEBUG("failed: %i", retval);
2376
2377 return retval;
2378 }
2379
2380 static int find_target(struct command_context *cmd_ctx, const char *name)
2381 {
2382 struct target *target = get_target(name);
2383 if (target == NULL) {
2384 LOG_ERROR("Target: %s is unknown, try one of:\n", name);
2385 return ERROR_FAIL;
2386 }
2387 if (!target->tap->enabled) {
2388 LOG_USER("Target: TAP %s is disabled, "
2389 "can't be the current target\n",
2390 target->tap->dotted_name);
2391 return ERROR_FAIL;
2392 }
2393
2394 cmd_ctx->current_target = target->target_number;
2395 return ERROR_OK;
2396 }
2397
2398
2399 COMMAND_HANDLER(handle_targets_command)
2400 {
2401 int retval = ERROR_OK;
2402 if (CMD_ARGC == 1) {
2403 retval = find_target(CMD_CTX, CMD_ARGV[0]);
2404 if (retval == ERROR_OK) {
2405 /* we're done! */
2406 return retval;
2407 }
2408 }
2409
2410 struct target *target = all_targets;
2411 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
2412 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
2413 while (target) {
2414 const char *state;
2415 char marker = ' ';
2416
2417 if (target->tap->enabled)
2418 state = target_state_name(target);
2419 else
2420 state = "tap-disabled";
2421
2422 if (CMD_CTX->current_target == target->target_number)
2423 marker = '*';
2424
2425 /* keep columns lined up to match the headers above */
2426 command_print(CMD_CTX,
2427 "%2d%c %-18s %-10s %-6s %-18s %s",
2428 target->target_number,
2429 marker,
2430 target_name(target),
2431 target_type_name(target),
2432 Jim_Nvp_value2name_simple(nvp_target_endian,
2433 target->endianness)->name,
2434 target->tap->dotted_name,
2435 state);
2436 target = target->next;
2437 }
2438
2439 return retval;
2440 }
2441
2442 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2443
2444 static int powerDropout;
2445 static int srstAsserted;
2446
2447 static int runPowerRestore;
2448 static int runPowerDropout;
2449 static int runSrstAsserted;
2450 static int runSrstDeasserted;
2451
2452 static int sense_handler(void)
2453 {
2454 static int prevSrstAsserted;
2455 static int prevPowerdropout;
2456
2457 int retval = jtag_power_dropout(&powerDropout);
2458 if (retval != ERROR_OK)
2459 return retval;
2460
2461 int powerRestored;
2462 powerRestored = prevPowerdropout && !powerDropout;
2463 if (powerRestored)
2464 runPowerRestore = 1;
2465
2466 int64_t current = timeval_ms();
2467 static int64_t lastPower;
2468 bool waitMore = lastPower + 2000 > current;
2469 if (powerDropout && !waitMore) {
2470 runPowerDropout = 1;
2471 lastPower = current;
2472 }
2473
2474 retval = jtag_srst_asserted(&srstAsserted);
2475 if (retval != ERROR_OK)
2476 return retval;
2477
2478 int srstDeasserted;
2479 srstDeasserted = prevSrstAsserted && !srstAsserted;
2480
2481 static int64_t lastSrst;
2482 waitMore = lastSrst + 2000 > current;
2483 if (srstDeasserted && !waitMore) {
2484 runSrstDeasserted = 1;
2485 lastSrst = current;
2486 }
2487
2488 if (!prevSrstAsserted && srstAsserted)
2489 runSrstAsserted = 1;
2490
2491 prevSrstAsserted = srstAsserted;
2492 prevPowerdropout = powerDropout;
2493
2494 if (srstDeasserted || powerRestored) {
2495 /* Other than logging the event we can't do anything here.
2496 * Issuing a reset is a particularly bad idea as we might
2497 * be inside a reset already.
2498 */
2499 }
2500
2501 return ERROR_OK;
2502 }
2503
2504 /* process target state changes */
2505 static int handle_target(void *priv)
2506 {
2507 Jim_Interp *interp = (Jim_Interp *)priv;
2508 int retval = ERROR_OK;
2509
2510 if (!is_jtag_poll_safe()) {
2511 /* polling is disabled currently */
2512 return ERROR_OK;
2513 }
2514
2515 /* we do not want to recurse here... */
2516 static int recursive;
2517 if (!recursive) {
2518 recursive = 1;
2519 sense_handler();
2520 /* danger! running these procedures can trigger srst assertions and power dropouts.
2521 * We need to avoid an infinite loop/recursion here and we do that by
2522 * clearing the flags after running these events.
2523 */
2524 int did_something = 0;
2525 if (runSrstAsserted) {
2526 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2527 Jim_Eval(interp, "srst_asserted");
2528 did_something = 1;
2529 }
2530 if (runSrstDeasserted) {
2531 Jim_Eval(interp, "srst_deasserted");
2532 did_something = 1;
2533 }
2534 if (runPowerDropout) {
2535 LOG_INFO("Power dropout detected, running power_dropout proc.");
2536 Jim_Eval(interp, "power_dropout");
2537 did_something = 1;
2538 }
2539 if (runPowerRestore) {
2540 Jim_Eval(interp, "power_restore");
2541 did_something = 1;
2542 }
2543
2544 if (did_something) {
2545 /* clear detect flags */
2546 sense_handler();
2547 }
2548
2549 /* clear action flags */
2550
2551 runSrstAsserted = 0;
2552 runSrstDeasserted = 0;
2553 runPowerRestore = 0;
2554 runPowerDropout = 0;
2555
2556 recursive = 0;
2557 }
2558
2559 /* Poll targets for state changes unless that's globally disabled.
2560 * Skip targets that are currently disabled.
2561 */
2562 for (struct target *target = all_targets;
2563 is_jtag_poll_safe() && target;
2564 target = target->next) {
2565
2566 if (!target_was_examined(target))
2567 continue;
2568
2569 if (!target->tap->enabled)
2570 continue;
2571
2572 if (target->backoff.times > target->backoff.count) {
2573 /* do not poll this time as we failed previously */
2574 target->backoff.count++;
2575 continue;
2576 }
2577 target->backoff.count = 0;
2578
2579 /* only poll target if we've got power and srst isn't asserted */
2580 if (!powerDropout && !srstAsserted) {
2581 /* polling may fail silently until the target has been examined */
2582 retval = target_poll(target);
2583 if (retval != ERROR_OK) {
2584 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2585 if (target->backoff.times * polling_interval < 5000) {
2586 target->backoff.times *= 2;
2587 target->backoff.times++;
2588 }
2589
2590 /* Tell GDB to halt the debugger. This allows the user to
2591 * run monitor commands to handle the situation.
2592 */
2593 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2594 }
2595 if (target->backoff.times > 0) {
2596 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target));
2597 target_reset_examined(target);
2598 retval = target_examine_one(target);
2599 /* Target examination could have failed due to unstable connection,
2600 * but we set the examined flag anyway to repoll it later */
2601 if (retval != ERROR_OK) {
2602 target->examined = true;
2603 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2604 target->backoff.times * polling_interval);
2605 return retval;
2606 }
2607 }
2608
2609 /* Since we succeeded, we reset backoff count */
2610 target->backoff.times = 0;
2611 }
2612 }
2613
2614 return retval;
2615 }
2616
2617 COMMAND_HANDLER(handle_reg_command)
2618 {
2619 struct target *target;
2620 struct reg *reg = NULL;
2621 unsigned count = 0;
2622 char *value;
2623
2624 LOG_DEBUG("-");
2625
2626 target = get_current_target(CMD_CTX);
2627
2628 /* list all available registers for the current target */
2629 if (CMD_ARGC == 0) {
2630 struct reg_cache *cache = target->reg_cache;
2631
2632 count = 0;
2633 while (cache) {
2634 unsigned i;
2635
2636 command_print(CMD_CTX, "===== %s", cache->name);
2637
2638 for (i = 0, reg = cache->reg_list;
2639 i < cache->num_regs;
2640 i++, reg++, count++) {
2641 /* only print cached values if they are valid */
2642 if (reg->valid) {
2643 value = buf_to_str(reg->value,
2644 reg->size, 16);
2645 command_print(CMD_CTX,
2646 "(%i) %s (/%" PRIu32 "): 0x%s%s",
2647 count, reg->name,
2648 reg->size, value,
2649 reg->dirty
2650 ? " (dirty)"
2651 : "");
2652 free(value);
2653 } else {
2654 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
2655 count, reg->name,
2656 reg->size) ;
2657 }
2658 }
2659 cache = cache->next;
2660 }
2661
2662 return ERROR_OK;
2663 }
2664
2665 /* access a single register by its ordinal number */
2666 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9')) {
2667 unsigned num;
2668 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
2669
2670 struct reg_cache *cache = target->reg_cache;
2671 count = 0;
2672 while (cache) {
2673 unsigned i;
2674 for (i = 0; i < cache->num_regs; i++) {
2675 if (count++ == num) {
2676 reg = &cache->reg_list[i];
2677 break;
2678 }
2679 }
2680 if (reg)
2681 break;
2682 cache = cache->next;
2683 }
2684
2685 if (!reg) {
2686 command_print(CMD_CTX, "%i is out of bounds, the current target "
2687 "has only %i registers (0 - %i)", num, count, count - 1);
2688 return ERROR_OK;
2689 }
2690 } else {
2691 /* access a single register by its name */
2692 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
2693
2694 if (!reg) {
2695 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
2696 return ERROR_OK;
2697 }
2698 }
2699
2700 assert(reg != NULL); /* give clang a hint that we *know* reg is != NULL here */
2701
2702 /* display a register */
2703 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0')
2704 && (CMD_ARGV[1][0] <= '9')))) {
2705 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
2706 reg->valid = 0;
2707
2708 if (reg->valid == 0)
2709 reg->type->get(reg);
2710 value = buf_to_str(reg->value, reg->size, 16);
2711 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2712 free(value);
2713 return ERROR_OK;
2714 }
2715
2716 /* set register value */
2717 if (CMD_ARGC == 2) {
2718 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
2719 if (buf == NULL)
2720 return ERROR_FAIL;
2721 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
2722
2723 reg->type->set(reg, buf);
2724
2725 value = buf_to_str(reg->value, reg->size, 16);
2726 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2727 free(value);
2728
2729 free(buf);
2730
2731 return ERROR_OK;
2732 }
2733
2734 return ERROR_COMMAND_SYNTAX_ERROR;
2735 }
2736
2737 COMMAND_HANDLER(handle_poll_command)
2738 {
2739 int retval = ERROR_OK;
2740 struct target *target = get_current_target(CMD_CTX);
2741
2742 if (CMD_ARGC == 0) {
2743 command_print(CMD_CTX, "background polling: %s",
2744 jtag_poll_get_enabled() ? "on" : "off");
2745 command_print(CMD_CTX, "TAP: %s (%s)",
2746 target->tap->dotted_name,
2747 target->tap->enabled ? "enabled" : "disabled");
2748 if (!target->tap->enabled)
2749 return ERROR_OK;
2750 retval = target_poll(target);
2751 if (retval != ERROR_OK)
2752 return retval;
2753 retval = target_arch_state(target);
2754 if (retval != ERROR_OK)
2755 return retval;
2756 } else if (CMD_ARGC == 1) {
2757 bool enable;
2758 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
2759 jtag_poll_set_enabled(enable);
2760 } else
2761 return ERROR_COMMAND_SYNTAX_ERROR;
2762
2763 return retval;
2764 }
2765
2766 COMMAND_HANDLER(handle_wait_halt_command)
2767 {
2768 if (CMD_ARGC > 1)
2769 return ERROR_COMMAND_SYNTAX_ERROR;
2770
2771 unsigned ms = DEFAULT_HALT_TIMEOUT;
2772 if (1 == CMD_ARGC) {
2773 int retval = parse_uint(CMD_ARGV[0], &ms);
2774 if (ERROR_OK != retval)
2775 return ERROR_COMMAND_SYNTAX_ERROR;
2776 }
2777
2778 struct target *target = get_current_target(CMD_CTX);
2779 return target_wait_state(target, TARGET_HALTED, ms);
2780 }
2781
2782 /* wait for target state to change. The trick here is to have a low
2783 * latency for short waits and not to suck up all the CPU time
2784 * on longer waits.
2785 *
2786 * After 500ms, keep_alive() is invoked
2787 */
2788 int target_wait_state(struct target *target, enum target_state state, int ms)
2789 {
2790 int retval;
2791 int64_t then = 0, cur;
2792 bool once = true;
2793
2794 for (;;) {
2795 retval = target_poll(target);
2796 if (retval != ERROR_OK)
2797 return retval;
2798 if (target->state == state)
2799 break;
2800 cur = timeval_ms();
2801 if (once) {
2802 once = false;
2803 then = timeval_ms();
2804 LOG_DEBUG("waiting for target %s...",
2805 Jim_Nvp_value2name_simple(nvp_target_state, state)->name);
2806 }
2807
2808 if (cur-then > 500)
2809 keep_alive();
2810
2811 if ((cur-then) > ms) {
2812 LOG_ERROR("timed out while waiting for target %s",
2813 Jim_Nvp_value2name_simple(nvp_target_state, state)->name);
2814 return ERROR_FAIL;
2815 }
2816 }
2817
2818 return ERROR_OK;
2819 }
2820
2821 COMMAND_HANDLER(handle_halt_command)
2822 {
2823 LOG_DEBUG("-");
2824
2825 struct target *target = get_current_target(CMD_CTX);
2826 int retval = target_halt(target);
2827 if (ERROR_OK != retval)
2828 return retval;
2829
2830 if (CMD_ARGC == 1) {
2831 unsigned wait_local;
2832 retval = parse_uint(CMD_ARGV[0], &wait_local);
2833 if (ERROR_OK != retval)
2834 return ERROR_COMMAND_SYNTAX_ERROR;
2835 if (!wait_local)
2836 return ERROR_OK;
2837 }
2838
2839 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2840 }
2841
2842 COMMAND_HANDLER(handle_soft_reset_halt_command)
2843 {
2844 struct target *target = get_current_target(CMD_CTX);
2845
2846 LOG_USER("requesting target halt and executing a soft reset");
2847
2848 target_soft_reset_halt(target);
2849
2850 return ERROR_OK;
2851 }
2852
2853 COMMAND_HANDLER(handle_reset_command)
2854 {
2855 if (CMD_ARGC > 1)
2856 return ERROR_COMMAND_SYNTAX_ERROR;
2857
2858 enum target_reset_mode reset_mode = RESET_RUN;
2859 if (CMD_ARGC == 1) {
2860 const Jim_Nvp *n;
2861 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
2862 if ((n->name == NULL) || (n->value == RESET_UNKNOWN))
2863 return ERROR_COMMAND_SYNTAX_ERROR;
2864 reset_mode = n->value;
2865 }
2866
2867 /* reset *all* targets */
2868 return target_process_reset(CMD_CTX, reset_mode);
2869 }
2870
2871
2872 COMMAND_HANDLER(handle_resume_command)
2873 {
2874 int current = 1;
2875 if (CMD_ARGC > 1)
2876 return ERROR_COMMAND_SYNTAX_ERROR;
2877
2878 struct target *target = get_current_target(CMD_CTX);
2879
2880 /* with no CMD_ARGV, resume from current pc, addr = 0,
2881 * with one arguments, addr = CMD_ARGV[0],
2882 * handle breakpoints, not debugging */
2883 uint32_t addr = 0;
2884 if (CMD_ARGC == 1) {
2885 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2886 current = 0;
2887 }
2888
2889 return target_resume(target, current, addr, 1, 0);
2890 }
2891
2892 COMMAND_HANDLER(handle_step_command)
2893 {
2894 if (CMD_ARGC > 1)
2895 return ERROR_COMMAND_SYNTAX_ERROR;
2896
2897 LOG_DEBUG("-");
2898
2899 /* with no CMD_ARGV, step from current pc, addr = 0,
2900 * with one argument addr = CMD_ARGV[0],
2901 * handle breakpoints, debugging */
2902 uint32_t addr = 0;
2903 int current_pc = 1;
2904 if (CMD_ARGC == 1) {
2905 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2906 current_pc = 0;
2907 }
2908
2909 struct target *target = get_current_target(CMD_CTX);
2910
2911 return target->type->step(target, current_pc, addr, 1);
2912 }
2913
2914 static void handle_md_output(struct command_context *cmd_ctx,
2915 struct target *target, uint32_t address, unsigned size,
2916 unsigned count, const uint8_t *buffer)
2917 {
2918 const unsigned line_bytecnt = 32;
2919 unsigned line_modulo = line_bytecnt / size;
2920
2921 char output[line_bytecnt * 4 + 1];
2922 unsigned output_len = 0;
2923
2924 const char *value_fmt;
2925 switch (size) {
2926 case 4:
2927 value_fmt = "%8.8x ";
2928 break;
2929 case 2:
2930 value_fmt = "%4.4x ";
2931 break;
2932 case 1:
2933 value_fmt = "%2.2x ";
2934 break;
2935 default:
2936 /* "can't happen", caller checked */
2937 LOG_ERROR("invalid memory read size: %u", size);
2938 return;
2939 }
2940
2941 for (unsigned i = 0; i < count; i++) {
2942 if (i % line_modulo == 0) {
2943 output_len += snprintf(output + output_len,
2944 sizeof(output) - output_len,
2945 "0x%8.8x: ",
2946 (unsigned)(address + (i*size)));
2947 }
2948
2949 uint32_t value = 0;
2950 const uint8_t *value_ptr = buffer + i * size;
2951 switch (size) {
2952 case 4:
2953 value = target_buffer_get_u32(target, value_ptr);
2954 break;
2955 case 2:
2956 value = target_buffer_get_u16(target, value_ptr);
2957 break;
2958 case 1:
2959 value = *value_ptr;
2960 }
2961 output_len += snprintf(output + output_len,
2962 sizeof(output) - output_len,
2963 value_fmt, value);
2964
2965 if ((i % line_modulo == line_modulo - 1) || (i == count - 1)) {
2966 command_print(cmd_ctx, "%s", output);
2967 output_len = 0;
2968 }
2969 }
2970 }
2971
2972 COMMAND_HANDLER(handle_md_command)
2973 {
2974 if (CMD_ARGC < 1)
2975 return ERROR_COMMAND_SYNTAX_ERROR;
2976
2977 unsigned size = 0;
2978 switch (CMD_NAME[2]) {
2979 case 'w':
2980 size = 4;
2981 break;
2982 case 'h':
2983 size = 2;
2984 break;
2985 case 'b':
2986 size = 1;
2987 break;
2988 default:
2989 return ERROR_COMMAND_SYNTAX_ERROR;
2990 }
2991
2992 bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
2993 int (*fn)(struct target *target,
2994 uint32_t address, uint32_t size_value, uint32_t count, uint8_t *buffer);
2995 if (physical) {
2996 CMD_ARGC--;
2997 CMD_ARGV++;
2998 fn = target_read_phys_memory;
2999 } else
3000 fn = target_read_memory;
3001 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
3002 return ERROR_COMMAND_SYNTAX_ERROR;
3003
3004 uint32_t address;
3005 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
3006
3007 unsigned count = 1;
3008 if (CMD_ARGC == 2)
3009 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
3010
3011 uint8_t *buffer = calloc(count, size);
3012
3013 struct target *target = get_current_target(CMD_CTX);
3014 int retval = fn(target, address, size, count, buffer);
3015 if (ERROR_OK == retval)
3016 handle_md_output(CMD_CTX, target, address, size, count, buffer);
3017
3018 free(buffer);
3019
3020 return retval;
3021 }
3022
3023 typedef int (*target_write_fn)(struct target *target,
3024 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer);
3025
3026 static int target_fill_mem(struct target *target,
3027 uint32_t address,
3028 target_write_fn fn,
3029 unsigned data_size,
3030 /* value */
3031 uint32_t b,
3032 /* count */
3033 unsigned c)
3034 {
3035 /* We have to write in reasonably large chunks to be able
3036 * to fill large memory areas with any sane speed */
3037 const unsigned chunk_size = 16384;
3038 uint8_t *target_buf = malloc(chunk_size * data_size);
3039 if (target_buf == NULL) {
3040 LOG_ERROR("Out of memory");
3041 return ERROR_FAIL;
3042 }
3043
3044 for (unsigned i = 0; i < chunk_size; i++) {
3045 switch (data_size) {
3046 case 4:
3047 target_buffer_set_u32(target, target_buf + i * data_size, b);
3048 break;
3049 case 2:
3050 target_buffer_set_u16(target, target_buf + i * data_size, b);
3051 break;
3052 case 1:
3053 target_buffer_set_u8(target, target_buf + i * data_size, b);
3054 break;
3055 default:
3056 exit(-1);
3057 }
3058 }
3059
3060 int retval = ERROR_OK;
3061
3062 for (unsigned x = 0; x < c; x += chunk_size) {
3063 unsigned current;
3064 current = c - x;
3065 if (current > chunk_size)
3066 current = chunk_size;
3067 retval = fn(target, address + x * data_size, data_size, current, target_buf);
3068 if (retval != ERROR_OK)
3069 break;
3070 /* avoid GDB timeouts */
3071 keep_alive();
3072 }
3073 free(target_buf);
3074
3075 return retval;
3076 }
3077
3078
3079 COMMAND_HANDLER(handle_mw_command)
3080 {
3081 if (CMD_ARGC < 2)
3082 return ERROR_COMMAND_SYNTAX_ERROR;
3083 bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
3084 target_write_fn fn;
3085 if (physical) {
3086 CMD_ARGC--;
3087 CMD_ARGV++;
3088 fn = target_write_phys_memory;
3089 } else
3090 fn = target_write_memory;
3091 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
3092 return ERROR_COMMAND_SYNTAX_ERROR;
3093
3094 uint32_t address;
3095 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
3096
3097 uint32_t value;
3098 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
3099
3100 unsigned count = 1;
3101 if (CMD_ARGC == 3)
3102 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
3103
3104 struct target *target = get_current_target(CMD_CTX);
3105 unsigned wordsize;
3106 switch (CMD_NAME[2]) {
3107 case 'w':
3108 wordsize = 4;
3109 break;
3110 case 'h':
3111 wordsize = 2;
3112 break;
3113 case 'b':
3114 wordsize = 1;
3115 break;
3116 default:
3117 return ERROR_COMMAND_SYNTAX_ERROR;
3118 }
3119
3120 return target_fill_mem(target, address, fn, wordsize, value, count);
3121 }
3122
3123 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
3124 uint32_t *min_address, uint32_t *max_address)
3125 {
3126 if (CMD_ARGC < 1 || CMD_ARGC > 5)
3127 return ERROR_COMMAND_SYNTAX_ERROR;
3128
3129 /* a base address isn't always necessary,
3130 * default to 0x0 (i.e. don't relocate) */
3131 if (CMD_ARGC >= 2) {
3132 uint32_t addr;
3133 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
3134 image->base_address = addr;
3135 image->base_address_set = 1;
3136 } else
3137 image->base_address_set = 0;
3138
3139 image->start_address_set = 0;
3140
3141 if (CMD_ARGC >= 4)
3142 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], *min_address);
3143 if (CMD_ARGC == 5) {
3144 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], *max_address);
3145 /* use size (given) to find max (required) */
3146 *max_address += *min_address;
3147 }
3148
3149 if (*min_address > *max_address)
3150 return ERROR_COMMAND_SYNTAX_ERROR;
3151
3152 return ERROR_OK;
3153 }
3154
3155 COMMAND_HANDLER(handle_load_image_command)
3156 {
3157 uint8_t *buffer;
3158 size_t buf_cnt;
3159 uint32_t image_size;
3160 uint32_t min_address = 0;
3161 uint32_t max_address = 0xffffffff;
3162 int i;
3163 struct image image;
3164
3165 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
3166 &image, &min_address, &max_address);
3167 if (ERROR_OK != retval)
3168 return retval;
3169
3170 struct target *target = get_current_target(CMD_CTX);
3171
3172 struct duration bench;
3173 duration_start(&bench);
3174
3175 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
3176 return ERROR_FAIL;
3177
3178 image_size = 0x0;
3179 retval = ERROR_OK;
3180 for (i = 0; i < image.num_sections; i++) {
3181 buffer = malloc(image.sections[i].size);
3182 if (buffer == NULL) {
3183 command_print(CMD_CTX,
3184 "error allocating buffer for section (%d bytes)",
3185 (int)(image.sections[i].size));
3186 retval = ERROR_FAIL;
3187 break;
3188 }
3189
3190 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
3191 if (retval != ERROR_OK) {
3192 free(buffer);
3193 break;
3194 }
3195
3196 uint32_t offset = 0;
3197 uint32_t length = buf_cnt;
3198
3199 /* DANGER!!! beware of unsigned comparision here!!! */
3200
3201 if ((image.sections[i].base_address + buf_cnt >= min_address) &&
3202 (image.sections[i].base_address < max_address)) {
3203
3204 if (image.sections[i].base_address < min_address) {
3205 /* clip addresses below */
3206 offset += min_address-image.sections[i].base_address;
3207 length -= offset;
3208 }
3209
3210 if (image.sections[i].base_address + buf_cnt > max_address)
3211 length -= (image.sections[i].base_address + buf_cnt)-max_address;
3212
3213 retval = target_write_buffer(target,
3214 image.sections[i].base_address + offset, length, buffer + offset);
3215 if (retval != ERROR_OK) {
3216 free(buffer);
3217 break;
3218 }
3219 image_size += length;
3220 command_print(CMD_CTX, "%u bytes written at address 0x%8.8" PRIx32 "",
3221 (unsigned int)length,
3222 image.sections[i].base_address + offset);
3223 }
3224
3225 free(buffer);
3226 }
3227
3228 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
3229 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
3230 "in %fs (%0.3f KiB/s)", image_size,
3231 duration_elapsed(&bench), duration_kbps(&bench, image_size));
3232 }
3233
3234 image_close(&image);
3235
3236 return retval;
3237
3238 }
3239
3240 COMMAND_HANDLER(handle_dump_image_command)
3241 {
3242 struct fileio *fileio;
3243 uint8_t *buffer;
3244 int retval, retvaltemp;
3245 uint32_t address, size;
3246 struct duration bench;
3247 struct target *target = get_current_target(CMD_CTX);
3248
3249 if (CMD_ARGC != 3)
3250 return ERROR_COMMAND_SYNTAX_ERROR;
3251
3252 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], address);
3253 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], size);
3254
3255 uint32_t buf_size = (size > 4096) ? 4096 : size;
3256 buffer = malloc(buf_size);
3257 if (!buffer)
3258 return ERROR_FAIL;
3259
3260 retval = fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY);
3261 if (retval != ERROR_OK) {
3262 free(buffer);
3263 return retval;
3264 }
3265
3266 duration_start(&bench);
3267
3268 while (size > 0) {
3269 size_t size_written;
3270 uint32_t this_run_size = (size > buf_size) ? buf_size : size;
3271 retval = target_read_buffer(target, address, this_run_size, buffer);
3272 if (retval != ERROR_OK)
3273 break;
3274
3275 retval = fileio_write(fileio, this_run_size, buffer, &size_written);
3276 if (retval != ERROR_OK)
3277 break;
3278
3279 size -= this_run_size;
3280 address += this_run_size;
3281 }
3282
3283 free(buffer);
3284
3285 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
3286 size_t filesize;
3287 retval = fileio_size(fileio, &filesize);
3288 if (retval != ERROR_OK)
3289 return retval;
3290 command_print(CMD_CTX,
3291 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize,
3292 duration_elapsed(&bench), duration_kbps(&bench, filesize));
3293 }
3294
3295 retvaltemp = fileio_close(fileio);
3296 if (retvaltemp != ERROR_OK)
3297 return retvaltemp;
3298
3299 return retval;
3300 }
3301
3302 enum verify_mode {
3303 IMAGE_TEST = 0,
3304 IMAGE_VERIFY = 1,
3305 IMAGE_CHECKSUM_ONLY = 2
3306 };
3307
3308 static COMMAND_HELPER(handle_verify_image_command_internal, enum verify_mode verify)
3309 {
3310 uint8_t *buffer;
3311 size_t buf_cnt;
3312 uint32_t image_size;
3313 int i;
3314 int retval;
3315 uint32_t checksum = 0;
3316 uint32_t mem_checksum = 0;
3317
3318 struct image image;
3319
3320 struct target *target = get_current_target(CMD_CTX);
3321
3322 if (CMD_ARGC < 1)
3323 return ERROR_COMMAND_SYNTAX_ERROR;
3324
3325 if (!target) {
3326 LOG_ERROR("no target selected");
3327 return ERROR_FAIL;
3328 }
3329
3330 struct duration bench;
3331 duration_start(&bench);
3332
3333 if (CMD_ARGC >= 2) {
3334 uint32_t addr;
3335 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
3336 image.base_address = addr;
3337 image.base_address_set = 1;
3338 } else {
3339 image.base_address_set = 0;
3340 image.base_address = 0x0;
3341 }
3342
3343 image.start_address_set = 0;
3344
3345 retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL);
3346 if (retval != ERROR_OK)
3347 return retval;
3348
3349 image_size = 0x0;
3350 int diffs = 0;
3351 retval = ERROR_OK;
3352 for (i = 0; i < image.num_sections; i++) {
3353 buffer = malloc(image.sections[i].size);
3354 if (buffer == NULL) {
3355 command_print(CMD_CTX,
3356 "error allocating buffer for section (%d bytes)",
3357 (int)(image.sections[i].size));
3358 break;
3359 }
3360 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
3361 if (retval != ERROR_OK) {
3362 free(buffer);
3363 break;
3364 }
3365
3366 if (verify >= IMAGE_VERIFY) {
3367 /* calculate checksum of image */
3368 retval = image_calculate_checksum(buffer, buf_cnt, &checksum);
3369 if (retval != ERROR_OK) {
3370 free(buffer);
3371 break;
3372 }
3373
3374 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
3375 if (retval != ERROR_OK) {
3376 free(buffer);
3377 break;
3378 }
3379 if ((checksum != mem_checksum) && (verify == IMAGE_CHECKSUM_ONLY)) {
3380 LOG_ERROR("checksum mismatch");
3381 free(buffer);
3382 retval = ERROR_FAIL;
3383 goto done;
3384 }
3385 if (checksum != mem_checksum) {
3386 /* failed crc checksum, fall back to a binary compare */
3387 uint8_t *data;
3388
3389 if (diffs == 0)
3390 LOG_ERROR("checksum mismatch - attempting binary compare");
3391
3392 data = malloc(buf_cnt);
3393
3394 /* Can we use 32bit word accesses? */
3395 int size = 1;
3396 int count = buf_cnt;
3397 if ((count % 4) == 0) {
3398 size *= 4;
3399 count /= 4;
3400 }
3401 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
3402 if (retval == ERROR_OK) {
3403 uint32_t t;
3404 for (t = 0; t < buf_cnt; t++) {
3405 if (data[t] != buffer[t]) {
3406 command_print(CMD_CTX,
3407 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3408 diffs,
3409 (unsigned)(t + image.sections[i].base_address),
3410 data[t],
3411 buffer[t]);
3412 if (diffs++ >= 127) {
3413 command_print(CMD_CTX, "More than 128 errors, the rest are not printed.");
3414 free(data);
3415 free(buffer);
3416 goto done;
3417 }
3418 }
3419 keep_alive();
3420 }
3421 }
3422 free(data);
3423 }
3424 } else {
3425 command_print(CMD_CTX, "address 0x%08" PRIx32 " length 0x%08zx",
3426 image.sections[i].base_address,
3427 buf_cnt);
3428 }
3429
3430 free(buffer);
3431 image_size += buf_cnt;
3432 }
3433 if (diffs > 0)
3434 command_print(CMD_CTX, "No more differences found.");
3435 done:
3436 if (diffs > 0)
3437 retval = ERROR_FAIL;
3438 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
3439 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
3440 "in %fs (%0.3f KiB/s)", image_size,
3441 duration_elapsed(&bench), duration_kbps(&bench, image_size));
3442 }
3443
3444 image_close(&image);
3445
3446 return retval;
3447 }
3448
3449 COMMAND_HANDLER(handle_verify_image_checksum_command)
3450 {
3451 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_CHECKSUM_ONLY);
3452 }
3453
3454 COMMAND_HANDLER(handle_verify_image_command)
3455 {
3456 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_VERIFY);
3457 }
3458
3459 COMMAND_HANDLER(handle_test_image_command)
3460 {
3461 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_TEST);
3462 }
3463
3464 static int handle_bp_command_list(struct command_context *cmd_ctx)
3465 {
3466 struct target *target = get_current_target(cmd_ctx);
3467 struct breakpoint *breakpoint = target->breakpoints;
3468 while (breakpoint) {
3469 if (breakpoint->type == BKPT_SOFT) {
3470 char *buf = buf_to_str(breakpoint->orig_instr,
3471 breakpoint->length, 16);
3472 command_print(cmd_ctx, "IVA breakpoint: 0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
3473 breakpoint->address,
3474 breakpoint->length,
3475 breakpoint->set, buf);
3476 free(buf);
3477 } else {
3478 if ((breakpoint->address == 0) && (breakpoint->asid != 0))
3479 command_print(cmd_ctx, "Context breakpoint: 0x%8.8" PRIx32 ", 0x%x, %i",
3480 breakpoint->asid,
3481 breakpoint->length, breakpoint->set);
3482 else if ((breakpoint->address != 0) && (breakpoint->asid != 0)) {
3483 command_print(cmd_ctx, "Hybrid breakpoint(IVA): 0x%8.8" PRIx32 ", 0x%x, %i",
3484 breakpoint->address,
3485 breakpoint->length, breakpoint->set);
3486 command_print(cmd_ctx, "\t|--->linked with ContextID: 0x%8.8" PRIx32,
3487 breakpoint->asid);
3488 } else
3489 command_print(cmd_ctx, "Breakpoint(IVA): 0x%8.8" PRIx32 ", 0x%x, %i",
3490 breakpoint->address,
3491 breakpoint->length, breakpoint->set);
3492 }
3493
3494 breakpoint = breakpoint->next;
3495 }
3496 return ERROR_OK;
3497 }
3498
3499 static int handle_bp_command_set(struct command_context *cmd_ctx,
3500 uint32_t addr, uint32_t asid, uint32_t length, int hw)
3501 {
3502 struct target *target = get_current_target(cmd_ctx);
3503 int retval;
3504
3505 if (asid == 0) {
3506 retval = breakpoint_add(target, addr, length, hw);
3507 if (ERROR_OK == retval)
3508 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
3509 else {
3510 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3511 return retval;
3512 }
3513 } else if (addr == 0) {
3514 if (target->type->add_context_breakpoint == NULL) {
3515 LOG_WARNING("Context breakpoint not available");
3516 return ERROR_OK;
3517 }
3518 retval = context_breakpoint_add(target, asid, length, hw);
3519 if (ERROR_OK == retval)
3520 command_print(cmd_ctx, "Context breakpoint set at 0x%8.8" PRIx32 "", asid);
3521 else {
3522 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3523 return retval;
3524 }
3525 } else {
3526 if (target->type->add_hybrid_breakpoint == NULL) {
3527 LOG_WARNING("Hybrid breakpoint not available");
3528 return ERROR_OK;
3529 }
3530 retval = hybrid_breakpoint_add(target, addr, asid, length, hw);
3531 if (ERROR_OK == retval)
3532 command_print(cmd_ctx, "Hybrid breakpoint set at 0x%8.8" PRIx32 "", asid);
3533 else {
3534 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3535 return retval;
3536 }
3537 }
3538 return ERROR_OK;
3539 }
3540
3541 COMMAND_HANDLER(handle_bp_command)
3542 {
3543 uint32_t addr;
3544 uint32_t asid;
3545 uint32_t length;
3546 int hw = BKPT_SOFT;
3547
3548 switch (CMD_ARGC) {
3549 case 0:
3550 return handle_bp_command_list(CMD_CTX);
3551
3552 case 2:
3553 asid = 0;
3554 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3555 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3556 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3557
3558 case 3:
3559 if (strcmp(CMD_ARGV[2], "hw") == 0) {
3560 hw = BKPT_HARD;
3561 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3562
3563 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3564
3565 asid = 0;
3566 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3567 } else if (strcmp(CMD_ARGV[2], "hw_ctx") == 0) {
3568 hw = BKPT_HARD;
3569 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], asid);
3570 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3571 addr = 0;
3572 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3573 }
3574
3575 case 4:
3576 hw = BKPT_HARD;
3577 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3578 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], asid);
3579 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], length);
3580 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3581
3582 default:
3583 return ERROR_COMMAND_SYNTAX_ERROR;
3584 }
3585 }
3586
3587 COMMAND_HANDLER(handle_rbp_command)
3588 {
3589 if (CMD_ARGC != 1)
3590 return ERROR_COMMAND_SYNTAX_ERROR;
3591
3592 uint32_t addr;
3593 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3594
3595 struct target *target = get_current_target(CMD_CTX);
3596 breakpoint_remove(target, addr);
3597
3598 return ERROR_OK;
3599 }
3600
3601 COMMAND_HANDLER(handle_wp_command)
3602 {
3603 struct target *target = get_current_target(CMD_CTX);
3604
3605 if (CMD_ARGC == 0) {
3606 struct watchpoint *watchpoint = target->watchpoints;
3607
3608 while (watchpoint) {
3609 command_print(CMD_CTX, "address: 0x%8.8" PRIx32
3610 ", len: 0x%8.8" PRIx32
3611 ", r/w/a: %i, value: 0x%8.8" PRIx32
3612 ", mask: 0x%8.8" PRIx32,
3613 watchpoint->address,
3614 watchpoint->length,
3615 (int)watchpoint->rw,
3616 watchpoint->value,
3617 watchpoint->mask);
3618 watchpoint = watchpoint->next;
3619 }
3620 return ERROR_OK;
3621 }
3622
3623 enum watchpoint_rw type = WPT_ACCESS;
3624 uint32_t addr = 0;
3625 uint32_t length = 0;
3626 uint32_t data_value = 0x0;
3627 uint32_t data_mask = 0xffffffff;
3628
3629 switch (CMD_ARGC) {
3630 case 5:
3631 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
3632 /* fall through */
3633 case 4:
3634 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
3635 /* fall through */
3636 case 3:
3637 switch (CMD_ARGV[2][0]) {
3638 case 'r':
3639 type = WPT_READ;
3640 break;
3641 case 'w':
3642 type = WPT_WRITE;
3643 break;
3644 case 'a':
3645 type = WPT_ACCESS;
3646 break;
3647 default:
3648 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
3649 return ERROR_COMMAND_SYNTAX_ERROR;
3650 }
3651 /* fall through */
3652 case 2:
3653 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3654 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3655 break;
3656
3657 default:
3658 return ERROR_COMMAND_SYNTAX_ERROR;
3659 }
3660
3661 int retval = watchpoint_add(target, addr, length, type,
3662 data_value, data_mask);
3663 if (ERROR_OK != retval)
3664 LOG_ERROR("Failure setting watchpoints");
3665
3666 return retval;
3667 }
3668
3669 COMMAND_HANDLER(handle_rwp_command)
3670 {
3671 if (CMD_ARGC != 1)
3672 return ERROR_COMMAND_SYNTAX_ERROR;
3673
3674 uint32_t addr;
3675 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3676
3677 struct target *target = get_current_target(CMD_CTX);
3678 watchpoint_remove(target, addr);
3679
3680 return ERROR_OK;
3681 }
3682
3683 /**
3684 * Translate a virtual address to a physical address.
3685 *
3686 * The low-level target implementation must have logged a detailed error
3687 * which is forwarded to telnet/GDB session.
3688 */
3689 COMMAND_HANDLER(handle_virt2phys_command)
3690 {
3691 if (CMD_ARGC != 1)
3692 return ERROR_COMMAND_SYNTAX_ERROR;
3693
3694 uint32_t va;
3695 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], va);
3696 uint32_t pa;
3697
3698 struct target *target = get_current_target(CMD_CTX);
3699 int retval = target->type->virt2phys(target, va, &pa);
3700 if (retval == ERROR_OK)
3701 command_print(CMD_CTX, "Physical address 0x%08" PRIx32 "", pa);
3702
3703 return retval;
3704 }
3705
3706 static void writeData(FILE *f, const void *data, size_t len)
3707 {
3708 size_t written = fwrite(data, 1, len, f);
3709 if (written != len)
3710 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
3711 }
3712
3713 static void writeLong(FILE *f, int l, struct target *target)
3714 {
3715 uint8_t val[4];
3716
3717 target_buffer_set_u32(target, val, l);
3718 writeData(f, val, 4);
3719 }
3720
3721 static void writeString(FILE *f, char *s)
3722 {
3723 writeData(f, s, strlen(s));
3724 }
3725
3726 typedef unsigned char UNIT[2]; /* unit of profiling */
3727
3728 /* Dump a gmon.out histogram file. */
3729 static void write_gmon(uint32_t *samples, uint32_t sampleNum, const char *filename, bool with_range,
3730 uint32_t start_address, uint32_t end_address, struct target *target)
3731 {
3732 uint32_t i;
3733 FILE *f = fopen(filename, "w");
3734 if (f == NULL)
3735 return;
3736 writeString(f, "gmon");
3737 writeLong(f, 0x00000001, target); /* Version */
3738 writeLong(f, 0, target); /* padding */
3739 writeLong(f, 0, target); /* padding */
3740 writeLong(f, 0, target); /* padding */
3741
3742 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
3743 writeData(f, &zero, 1);
3744
3745 /* figure out bucket size */
3746 uint32_t min;
3747 uint32_t max;
3748 if (with_range) {
3749 min = start_address;
3750 max = end_address;
3751 } else {
3752 min = samples[0];
3753 max = samples[0];
3754 for (i = 0; i < sampleNum; i++) {
3755 if (min > samples[i])
3756 min = samples[i];
3757 if (max < samples[i])
3758 max = samples[i];
3759 }
3760
3761 /* max should be (largest sample + 1)
3762 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
3763 max++;
3764 }
3765
3766 int addressSpace = max - min;
3767 assert(addressSpace >= 2);
3768
3769 /* FIXME: What is the reasonable number of buckets?
3770 * The profiling result will be more accurate if there are enough buckets. */
3771 static const uint32_t maxBuckets = 128 * 1024; /* maximum buckets. */
3772 uint32_t numBuckets = addressSpace / sizeof(UNIT);
3773 if (numBuckets > maxBuckets)
3774 numBuckets = maxBuckets;
3775 int *buckets = malloc(sizeof(int) * numBuckets);
3776 if (buckets == NULL) {
3777 fclose(f);
3778 return;
3779 }
3780 memset(buckets, 0, sizeof(int) * numBuckets);
3781 for (i = 0; i < sampleNum; i++) {
3782 uint32_t address = samples[i];
3783
3784 if ((address < min) || (max <= address))
3785 continue;
3786
3787 long long a = address - min;
3788 long long b = numBuckets;
3789 long long c = addressSpace;
3790 int index_t = (a * b) / c; /* danger!!!! int32 overflows */
3791 buckets[index_t]++;
3792 }
3793
3794 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3795 writeLong(f, min, target); /* low_pc */
3796 writeLong(f, max, target); /* high_pc */
3797 writeLong(f, numBuckets, target); /* # of buckets */
3798 writeLong(f, 100, target); /* KLUDGE! We lie, ca. 100Hz best case. */
3799 writeString(f, "seconds");
3800 for (i = 0; i < (15-strlen("seconds")); i++)
3801 writeData(f, &zero, 1);
3802 writeString(f, "s");
3803
3804 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3805
3806 char *data = malloc(2 * numBuckets);
3807 if (data != NULL) {
3808 for (i = 0; i < numBuckets; i++) {
3809 int val;
3810 val = buckets[i];
3811 if (val > 65535)
3812 val = 65535;
3813 data[i * 2] = val&0xff;
3814 data[i * 2 + 1] = (val >> 8) & 0xff;
3815 }
3816 free(buckets);
3817 writeData(f, data, numBuckets * 2);
3818 free(data);
3819 } else
3820 free(buckets);
3821
3822 fclose(f);
3823 }
3824
3825 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3826 * which will be used as a random sampling of PC */
3827 COMMAND_HANDLER(handle_profile_command)
3828 {
3829 struct target *target = get_current_target(CMD_CTX);
3830
3831 if ((CMD_ARGC != 2) && (CMD_ARGC != 4))
3832 return ERROR_COMMAND_SYNTAX_ERROR;
3833
3834 const uint32_t MAX_PROFILE_SAMPLE_NUM = 10000;
3835 uint32_t offset;
3836 uint32_t num_of_samples;
3837 int retval = ERROR_OK;
3838
3839 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], offset);
3840
3841 uint32_t *samples = malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM);
3842 if (samples == NULL) {
3843 LOG_ERROR("No memory to store samples.");
3844 return ERROR_FAIL;
3845 }
3846
3847 /**
3848 * Some cores let us sample the PC without the
3849 * annoying halt/resume step; for example, ARMv7 PCSR.
3850 * Provide a way to use that more efficient mechanism.
3851 */
3852 retval = target_profiling(target, samples, MAX_PROFILE_SAMPLE_NUM,
3853 &num_of_samples, offset);
3854 if (retval != ERROR_OK) {
3855 free(samples);
3856 return retval;
3857 }
3858
3859 assert(num_of_samples <= MAX_PROFILE_SAMPLE_NUM);
3860
3861 retval = target_poll(target);
3862 if (retval != ERROR_OK) {
3863 free(samples);
3864 return retval;
3865 }
3866 if (target->state == TARGET_RUNNING) {
3867 retval = target_halt(target);
3868 if (retval != ERROR_OK) {
3869 free(samples);
3870 return retval;
3871 }
3872 }
3873
3874 retval = target_poll(target);
3875 if (retval != ERROR_OK) {
3876 free(samples);
3877 return retval;
3878 }
3879
3880 uint32_t start_address = 0;
3881 uint32_t end_address = 0;
3882 bool with_range = false;
3883 if (CMD_ARGC == 4) {
3884 with_range = true;
3885 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], start_address);
3886 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], end_address);
3887 }
3888
3889 write_gmon(samples, num_of_samples, CMD_ARGV[1],
3890 with_range, start_address, end_address, target);
3891 command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
3892
3893 free(samples);
3894 return retval;
3895 }
3896
3897 static int new_int_array_element(Jim_Interp *interp, const char *varname, int idx, uint32_t val)
3898 {
3899 char *namebuf;
3900 Jim_Obj *nameObjPtr, *valObjPtr;
3901 int result;
3902
3903 namebuf = alloc_printf("%s(%d)", varname, idx);
3904 if (!namebuf)
3905 return JIM_ERR;
3906
3907 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3908 valObjPtr = Jim_NewIntObj(interp, val);
3909 if (!nameObjPtr || !valObjPtr) {
3910 free(namebuf);
3911 return JIM_ERR;
3912 }
3913
3914 Jim_IncrRefCount(nameObjPtr);
3915 Jim_IncrRefCount(valObjPtr);
3916 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3917 Jim_DecrRefCount(interp, nameObjPtr);
3918 Jim_DecrRefCount(interp, valObjPtr);
3919 free(namebuf);
3920 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3921 return result;
3922 }
3923
3924 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3925 {
3926 struct command_context *context;
3927 struct target *target;
3928
3929 context = current_command_context(interp);
3930 assert(context != NULL);
3931
3932 target = get_current_target(context);
3933 if (target == NULL) {
3934 LOG_ERROR("mem2array: no current target");
3935 return JIM_ERR;
3936 }
3937
3938 return target_mem2array(interp, target, argc - 1, argv + 1);
3939 }
3940
3941 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3942 {
3943 long l;
3944 uint32_t width;
3945 int len;
3946 uint32_t addr;
3947 uint32_t count;
3948 uint32_t v;
3949 const char *varname;
3950 const char *phys;
3951 bool is_phys;
3952 int n, e, retval;
3953 uint32_t i;
3954
3955 /* argv[1] = name of array to receive the data
3956 * argv[2] = desired width
3957 * argv[3] = memory address
3958 * argv[4] = count of times to read
3959 */
3960 if (argc < 4 || argc > 5) {
3961 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems [phys]");
3962 return JIM_ERR;
3963 }
3964 varname = Jim_GetString(argv[0], &len);
3965 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3966
3967 e = Jim_GetLong(interp, argv[1], &l);
3968 width = l;
3969 if (e != JIM_OK)
3970 return e;
3971
3972 e = Jim_GetLong(interp, argv[2], &l);
3973 addr = l;
3974 if (e != JIM_OK)
3975 return e;
3976 e = Jim_GetLong(interp, argv[3], &l);
3977 len = l;
3978 if (e != JIM_OK)
3979 return e;
3980 is_phys = false;
3981 if (argc > 4) {
3982 phys = Jim_GetString(argv[4], &n);
3983 if (!strncmp(phys, "phys", n))
3984 is_phys = true;
3985 else
3986 return JIM_ERR;
3987 }
3988 switch (width) {
3989 case 8:
3990 width = 1;
3991 break;
3992 case 16:
3993 width = 2;
3994 break;
3995 case 32:
3996 width = 4;
3997 break;
3998 default:
3999 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4000 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
4001 return JIM_ERR;
4002 }
4003 if (len == 0) {
4004 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4005 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
4006 return JIM_ERR;
4007 }
4008 if ((addr + (len * width)) < addr) {
4009 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4010 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
4011 return JIM_ERR;
4012 }
4013 /* absurd transfer size? */
4014 if (len > 65536) {
4015 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4016 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
4017 return JIM_ERR;
4018 }
4019
4020 if ((width == 1) ||
4021 ((width == 2) && ((addr & 1) == 0)) ||
4022 ((width == 4) && ((addr & 3) == 0))) {
4023 /* all is well */
4024 } else {
4025 char buf[100];
4026 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4027 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
4028 addr,
4029 width);
4030 Jim_AppendStrings(interp, Jim_GetResult(interp), buf, NULL);
4031 return JIM_ERR;
4032 }
4033
4034 /* Transfer loop */
4035
4036 /* index counter */
4037 n = 0;
4038
4039 size_t buffersize = 4096;
4040 uint8_t *buffer = malloc(buffersize);
4041 if (buffer == NULL)
4042 return JIM_ERR;
4043
4044 /* assume ok */
4045 e = JIM_OK;
4046 while (len) {
4047 /* Slurp... in buffer size chunks */
4048
4049 count = len; /* in objects.. */
4050 if (count > (buffersize / width))
4051 count = (buffersize / width);
4052
4053 if (is_phys)
4054 retval = target_read_phys_memory(target, addr, width, count, buffer);
4055 else
4056 retval = target_read_memory(target, addr, width, count, buffer);
4057 if (retval != ERROR_OK) {
4058 /* BOO !*/
4059 LOG_ERROR("mem2array: Read @ 0x%08" PRIx32 ", w=%" PRId32 ", cnt=%" PRId32 ", failed",
4060 addr,
4061 width,
4062 count);
4063 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4064 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
4065 e = JIM_ERR;
4066 break;
4067 } else {
4068 v = 0; /* shut up gcc */
4069 for (i = 0; i < count ; i++, n++) {
4070 switch (width) {
4071 case 4:
4072 v = target_buffer_get_u32(target, &buffer[i*width]);
4073 break;
4074 case 2:
4075 v = target_buffer_get_u16(target, &buffer[i*width]);
4076 break;
4077 case 1:
4078 v = buffer[i] & 0x0ff;
4079 break;
4080 }
4081 new_int_array_element(interp, varname, n, v);
4082 }
4083 len -= count;
4084 addr += count * width;
4085 }
4086 }
4087
4088 free(buffer);
4089
4090 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4091
4092 return e;
4093 }
4094
4095 static int get_int_array_element(Jim_Interp *interp, const char *varname, int idx, uint32_t *val)
4096 {
4097 char *namebuf;
4098 Jim_Obj *nameObjPtr, *valObjPtr;
4099 int result;
4100 long l;
4101
4102 namebuf = alloc_printf("%s(%d)", varname, idx);
4103 if (!namebuf)
4104 return JIM_ERR;
4105
4106 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
4107 if (!nameObjPtr) {
4108 free(namebuf);
4109 return JIM_ERR;
4110 }
4111
4112 Jim_IncrRefCount(nameObjPtr);
4113 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
4114 Jim_DecrRefCount(interp, nameObjPtr);
4115 free(namebuf);
4116 if (valObjPtr == NULL)
4117 return JIM_ERR;
4118
4119 result = Jim_GetLong(interp, valObjPtr, &l);
4120 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
4121 *val = l;
4122 return result;
4123 }
4124
4125 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4126 {
4127 struct command_context *context;
4128 struct target *target;
4129
4130 context = current_command_context(interp);
4131 assert(context != NULL);
4132
4133 target = get_current_target(context);
4134 if (target == NULL) {
4135 LOG_ERROR("array2mem: no current target");
4136 return JIM_ERR;
4137 }
4138
4139 return target_array2mem(interp, target, argc-1, argv + 1);
4140 }
4141
4142 static int target_array2mem(Jim_Interp *interp, struct target *target,
4143 int argc, Jim_Obj *const *argv)
4144 {
4145 long l;
4146 uint32_t width;
4147 int len;
4148 uint32_t addr;
4149 uint32_t count;
4150 uint32_t v;
4151 const char *varname;
4152 const char *phys;
4153 bool is_phys;
4154 int n, e, retval;
4155 uint32_t i;
4156
4157 /* argv[1] = name of array to get the data
4158 * argv[2] = desired width
4159 * argv[3] = memory address
4160 * argv[4] = count to write
4161 */
4162 if (argc < 4 || argc > 5) {
4163 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems [phys]");
4164 return JIM_ERR;
4165 }
4166 varname = Jim_GetString(argv[0], &len);
4167 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4168
4169 e = Jim_GetLong(interp, argv[1], &l);
4170 width = l;
4171 if (e != JIM_OK)
4172 return e;
4173
4174 e = Jim_GetLong(interp, argv[2], &l);
4175 addr = l;
4176 if (e != JIM_OK)
4177 return e;
4178 e = Jim_GetLong(interp, argv[3], &l);
4179 len = l;
4180 if (e != JIM_OK)
4181 return e;
4182 is_phys = false;
4183 if (argc > 4) {
4184 phys = Jim_GetString(argv[4], &n);
4185 if (!strncmp(phys, "phys", n))
4186 is_phys = true;
4187 else
4188 return JIM_ERR;
4189 }
4190 switch (width) {
4191 case 8:
4192 width = 1;
4193 break;
4194 case 16:
4195 width = 2;
4196 break;
4197 case 32:
4198 width = 4;
4199 break;
4200 default:
4201 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4202 Jim_AppendStrings(interp, Jim_GetResult(interp),
4203 "Invalid width param, must be 8/16/32", NULL);
4204 return JIM_ERR;
4205 }
4206 if (len == 0) {
4207 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4208 Jim_AppendStrings(interp, Jim_GetResult(interp),
4209 "array2mem: zero width read?", NULL);
4210 return JIM_ERR;
4211 }
4212 if ((addr + (len * width)) < addr) {
4213 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4214 Jim_AppendStrings(interp, Jim_GetResult(interp),
4215 "array2mem: addr + len - wraps to zero?", NULL);
4216 return JIM_ERR;
4217 }
4218 /* absurd transfer size? */
4219 if (len > 65536) {
4220 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4221 Jim_AppendStrings(interp, Jim_GetResult(interp),
4222 "array2mem: absurd > 64K item request", NULL);
4223 return JIM_ERR;
4224 }
4225
4226 if ((width == 1) ||
4227 ((width == 2) && ((addr & 1) == 0)) ||
4228 ((width == 4) && ((addr & 3) == 0))) {
4229 /* all is well */
4230 } else {
4231 char buf[100];
4232 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4233 sprintf(buf, "array2mem address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
4234 addr,
4235 width);
4236 Jim_AppendStrings(interp, Jim_GetResult(interp), buf, NULL);
4237 return JIM_ERR;
4238 }
4239
4240 /* Transfer loop */
4241
4242 /* index counter */
4243 n = 0;
4244 /* assume ok */
4245 e = JIM_OK;
4246
4247 size_t buffersize = 4096;
4248 uint8_t *buffer = malloc(buffersize);
4249 if (buffer == NULL)
4250 return JIM_ERR;
4251
4252 while (len) {
4253 /* Slurp... in buffer size chunks */
4254
4255 count = len; /* in objects.. */
4256 if (count > (buffersize / width))
4257 count = (buffersize / width);
4258
4259 v = 0; /* shut up gcc */
4260 for (i = 0; i < count; i++, n++) {
4261 get_int_array_element(interp, varname, n, &v);
4262 switch (width) {
4263 case 4:
4264 target_buffer_set_u32(target, &buffer[i * width], v);
4265 break;
4266 case 2:
4267 target_buffer_set_u16(target, &buffer[i * width], v);
4268 break;
4269 case 1:
4270 buffer[i] = v & 0x0ff;
4271 break;
4272 }
4273 }
4274 len -= count;
4275
4276 if (is_phys)
4277 retval = target_write_phys_memory(target, addr, width, count, buffer);
4278 else
4279 retval = target_write_memory(target, addr, width, count, buffer);
4280 if (retval != ERROR_OK) {
4281 /* BOO !*/
4282 LOG_ERROR("array2mem: Write @ 0x%08" PRIx32 ", w=%" PRId32 ", cnt=%" PRId32 ", failed",
4283 addr,
4284 width,
4285 count);
4286 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4287 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
4288 e = JIM_ERR;
4289 break;
4290 }
4291 addr += count * width;
4292 }
4293
4294 free(buffer);
4295
4296 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4297
4298 return e;
4299 }
4300
4301 /* FIX? should we propagate errors here rather than printing them
4302 * and continuing?
4303 */
4304 void target_handle_event(struct target *target, enum target_event e)
4305 {
4306 struct target_event_action *teap;
4307
4308 for (teap = target->event_action; teap != NULL; teap = teap->next) {
4309 if (teap->event == e) {
4310 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
4311 target->target_number,
4312 target_name(target),
4313 target_type_name(target),
4314 e,
4315 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
4316 Jim_GetString(teap->body, NULL));
4317 if (Jim_EvalObj(teap->interp, teap->body) != JIM_OK) {
4318 Jim_MakeErrorMessage(teap->interp);
4319 command_print(NULL, "%s\n", Jim_GetString(Jim_GetResult(teap->interp), NULL));
4320 }
4321 }
4322 }
4323 }
4324
4325 /**
4326 * Returns true only if the target has a handler for the specified event.
4327 */
4328 bool target_has_event_action(struct target *target, enum target_event event)
4329 {
4330 struct target_event_action *teap;
4331
4332 for (teap = target->event_action; teap != NULL; teap = teap->next) {
4333 if (teap->event == event)
4334 return true;
4335 }
4336 return false;
4337 }
4338
4339 enum target_cfg_param {
4340 TCFG_TYPE,
4341 TCFG_EVENT,
4342 TCFG_WORK_AREA_VIRT,
4343 TCFG_WORK_AREA_PHYS,
4344 TCFG_WORK_AREA_SIZE,
4345 TCFG_WORK_AREA_BACKUP,
4346 TCFG_ENDIAN,
4347 TCFG_COREID,
4348 TCFG_CHAIN_POSITION,
4349 TCFG_DBGBASE,
4350 TCFG_RTOS,
4351 TCFG_DEFER_EXAMINE,
4352 };
4353
4354 static Jim_Nvp nvp_config_opts[] = {
4355 { .name = "-type", .value = TCFG_TYPE },
4356 { .name = "-event", .value = TCFG_EVENT },
4357 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
4358 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
4359 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
4360 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
4361 { .name = "-endian" , .value = TCFG_ENDIAN },
4362 { .name = "-coreid", .value = TCFG_COREID },
4363 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
4364 { .name = "-dbgbase", .value = TCFG_DBGBASE },
4365 { .name = "-rtos", .value = TCFG_RTOS },
4366 { .name = "-defer-examine", .value = TCFG_DEFER_EXAMINE },
4367 { .name = NULL, .value = -1 }
4368 };
4369
4370 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
4371 {
4372 Jim_Nvp *n;
4373 Jim_Obj *o;
4374 jim_wide w;
4375 int e;
4376
4377 /* parse config or cget options ... */
4378 while (goi->argc > 0) {
4379 Jim_SetEmptyResult(goi->interp);
4380 /* Jim_GetOpt_Debug(goi); */
4381
4382 if (target->type->target_jim_configure) {
4383 /* target defines a configure function */
4384 /* target gets first dibs on parameters */
4385 e = (*(target->type->target_jim_configure))(target, goi);
4386 if (e == JIM_OK) {
4387 /* more? */
4388 continue;
4389 }
4390 if (e == JIM_ERR) {
4391 /* An error */
4392 return e;
4393 }
4394 /* otherwise we 'continue' below */
4395 }
4396 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
4397 if (e != JIM_OK) {
4398 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
4399 return e;
4400 }
4401 switch (n->value) {
4402 case TCFG_TYPE:
4403 /* not setable */
4404 if (goi->isconfigure) {
4405 Jim_SetResultFormatted(goi->interp,
4406 "not settable: %s", n->name);
4407 return JIM_ERR;
4408 } else {
4409 no_params:
4410 if (goi->argc != 0) {
4411 Jim_WrongNumArgs(goi->interp,
4412 goi->argc, goi->argv,
4413 "NO PARAMS");
4414 return JIM_ERR;
4415 }
4416 }
4417 Jim_SetResultString(goi->interp,
4418 target_type_name(target), -1);
4419 /* loop for more */
4420 break;
4421 case TCFG_EVENT:
4422 if (goi->argc == 0) {
4423 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
4424 return JIM_ERR;
4425 }
4426
4427 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
4428 if (e != JIM_OK) {
4429 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
4430 return e;
4431 }
4432
4433 if (goi->isconfigure) {
4434 if (goi->argc != 1) {
4435 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
4436 return JIM_ERR;
4437 }
4438 } else {
4439 if (goi->argc != 0) {
4440 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
4441 return JIM_ERR;
4442 }
4443 }
4444
4445 {
4446 struct target_event_action *teap;
4447
4448 teap = target->event_action;
4449 /* replace existing? */
4450 while (teap) {
4451 if (teap->event == (enum target_event)n->value)
4452 break;
4453 teap = teap->next;
4454 }
4455
4456 if (goi->isconfigure) {
4457 bool replace = true;
4458 if (teap == NULL) {
4459 /* create new */
4460 teap = calloc(1, sizeof(*teap));
4461 replace = false;
4462 }
4463 teap->event = n->value;
4464 teap->interp = goi->interp;
4465 Jim_GetOpt_Obj(goi, &o);
4466 if (teap->body)
4467 Jim_DecrRefCount(teap->interp, teap->body);
4468 teap->body = Jim_DuplicateObj(goi->interp, o);
4469 /*
4470 * FIXME:
4471 * Tcl/TK - "tk events" have a nice feature.
4472 * See the "BIND" command.
4473 * We should support that here.
4474 * You can specify %X and %Y in the event code.
4475 * The idea is: %T - target name.
4476 * The idea is: %N - target number
4477 * The idea is: %E - event name.
4478 */
4479 Jim_IncrRefCount(teap->body);
4480
4481 if (!replace) {
4482 /* add to head of event list */
4483 teap->next = target->event_action;
4484 target->event_action = teap;
4485 }
4486 Jim_SetEmptyResult(goi->interp);
4487 } else {
4488 /* get */
4489 if (teap == NULL)
4490 Jim_SetEmptyResult(goi->interp);
4491 else
4492 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
4493 }
4494 }
4495 /* loop for more */
4496 break;
4497
4498 case TCFG_WORK_AREA_VIRT:
4499 if (goi->isconfigure) {
4500 target_free_all_working_areas(target);
4501 e = Jim_GetOpt_Wide(goi, &w);
4502 if (e != JIM_OK)
4503 return e;
4504 target->working_area_virt = w;
4505 target->working_area_virt_spec = true;
4506 } else {
4507 if (goi->argc != 0)
4508 goto no_params;
4509 }
4510 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
4511 /* loop for more */
4512 break;
4513
4514 case TCFG_WORK_AREA_PHYS:
4515 if (goi->isconfigure) {
4516 target_free_all_working_areas(target);
4517 e = Jim_GetOpt_Wide(goi, &w);
4518 if (e != JIM_OK)
4519 return e;
4520 target->working_area_phys = w;
4521 target->working_area_phys_spec = true;
4522 } else {
4523 if (goi->argc != 0)
4524 goto no_params;
4525 }
4526 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
4527 /* loop for more */
4528 break;
4529
4530 case TCFG_WORK_AREA_SIZE:
4531 if (goi->isconfigure) {
4532 target_free_all_working_areas(target);
4533 e = Jim_GetOpt_Wide(goi, &w);
4534 if (e != JIM_OK)
4535 return e;
4536 target->working_area_size = w;
4537 } else {
4538 if (goi->argc != 0)
4539 goto no_params;
4540 }
4541 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
4542 /* loop for more */
4543 break;
4544
4545 case TCFG_WORK_AREA_BACKUP:
4546 if (goi->isconfigure) {
4547 target_free_all_working_areas(target);
4548 e = Jim_GetOpt_Wide(goi, &w);
4549 if (e != JIM_OK)
4550 return e;
4551 /* make this exactly 1 or 0 */
4552 target->backup_working_area = (!!w);
4553 } else {
4554 if (goi->argc != 0)
4555 goto no_params;
4556 }
4557 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
4558 /* loop for more e*/
4559 break;
4560
4561
4562 case TCFG_ENDIAN:
4563 if (goi->isconfigure) {
4564 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
4565 if (e != JIM_OK) {
4566 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
4567 return e;
4568 }
4569 target->endianness = n->value;
4570 } else {
4571 if (goi->argc != 0)
4572 goto no_params;
4573 }
4574 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
4575 if (n->name == NULL) {
4576 target->endianness = TARGET_LITTLE_ENDIAN;
4577 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
4578 }
4579 Jim_SetResultString(goi->interp, n->name, -1);
4580 /* loop for more */
4581 break;
4582
4583 case TCFG_COREID:
4584 if (goi->isconfigure) {
4585 e = Jim_GetOpt_Wide(goi, &w);
4586 if (e != JIM_OK)
4587 return e;
4588 target->coreid = (int32_t)w;
4589 } else {
4590 if (goi->argc != 0)
4591 goto no_params;
4592 }
4593 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
4594 /* loop for more */
4595 break;
4596
4597 case TCFG_CHAIN_POSITION:
4598 if (goi->isconfigure) {
4599 Jim_Obj *o_t;
4600 struct jtag_tap *tap;
4601 target_free_all_working_areas(target);
4602 e = Jim_GetOpt_Obj(goi, &o_t);
4603 if (e != JIM_OK)
4604 return e;
4605 tap = jtag_tap_by_jim_obj(goi->interp, o_t);
4606 if (tap == NULL)
4607 return JIM_ERR;
4608 /* make this exactly 1 or 0 */
4609 target->tap = tap;
4610 } else {
4611 if (goi->argc != 0)
4612 goto no_params;
4613 }
4614 Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
4615 /* loop for more e*/
4616 break;
4617 case TCFG_DBGBASE:
4618 if (goi->isconfigure) {
4619 e = Jim_GetOpt_Wide(goi, &w);
4620 if (e != JIM_OK)
4621 return e;
4622 target->dbgbase = (uint32_t)w;
4623 target->dbgbase_set = true;
4624 } else {
4625 if (goi->argc != 0)
4626 goto no_params;
4627 }
4628 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->dbgbase));
4629 /* loop for more */
4630 break;
4631
4632 case TCFG_RTOS:
4633 /* RTOS */
4634 {
4635 int result = rtos_create(goi, target);
4636 if (result != JIM_OK)
4637 return result;
4638 }
4639 /* loop for more */
4640 break;
4641
4642 case TCFG_DEFER_EXAMINE:
4643 /* DEFER_EXAMINE */
4644 target->defer_examine = true;
4645 /* loop for more */
4646 break;
4647
4648 }
4649 } /* while (goi->argc) */
4650
4651
4652 /* done - we return */
4653 return JIM_OK;
4654 }
4655
4656 static int jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
4657 {
4658 Jim_GetOptInfo goi;
4659
4660 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4661 goi.isconfigure = !strcmp(Jim_GetString(argv[0], NULL), "configure");
4662 if (goi.argc < 1) {
4663 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
4664 "missing: -option ...");
4665 return JIM_ERR;
4666 }
4667 struct target *target = Jim_CmdPrivData(goi.interp);
4668 return target_configure(&goi, target);
4669 }
4670
4671 static int jim_target_mw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4672 {
4673 const char *cmd_name = Jim_GetString(argv[0], NULL);
4674
4675 Jim_GetOptInfo goi;
4676 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4677
4678 if (goi.argc < 2 || goi.argc > 4) {
4679 Jim_SetResultFormatted(goi.interp,
4680 "usage: %s [phys] <address> <data> [<count>]", cmd_name);
4681 return JIM_ERR;
4682 }
4683
4684 target_write_fn fn;
4685 fn = target_write_memory;
4686
4687 int e;
4688 if (strcmp(Jim_GetString(argv[1], NULL), "phys") == 0) {
4689 /* consume it */
4690 struct Jim_Obj *obj;
4691 e = Jim_GetOpt_Obj(&goi, &obj);
4692 if (e != JIM_OK)
4693 return e;
4694
4695 fn = target_write_phys_memory;
4696 }
4697
4698 jim_wide a;
4699 e = Jim_GetOpt_Wide(&goi, &a);
4700 if (e != JIM_OK)
4701 return e;
4702
4703 jim_wide b;
4704 e = Jim_GetOpt_Wide(&goi, &b);
4705 if (e != JIM_OK)
4706 return e;
4707
4708 jim_wide c = 1;
4709 if (goi.argc == 1) {
4710 e = Jim_GetOpt_Wide(&goi, &c);
4711 if (e != JIM_OK)
4712 return e;
4713 }
4714
4715 /* all args must be consumed */
4716 if (goi.argc != 0)
4717 return JIM_ERR;
4718
4719 struct target *target = Jim_CmdPrivData(goi.interp);
4720 unsigned data_size;
4721 if (strcasecmp(cmd_name, "mww") == 0)
4722 data_size = 4;
4723 else if (strcasecmp(cmd_name, "mwh") == 0)
4724 data_size = 2;
4725 else if (strcasecmp(cmd_name, "mwb") == 0)
4726 data_size = 1;
4727 else {
4728 LOG_ERROR("command '%s' unknown: ", cmd_name);
4729 return JIM_ERR;
4730 }
4731
4732 return (target_fill_mem(target, a, fn, data_size, b, c) == ERROR_OK) ? JIM_OK : JIM_ERR;
4733 }
4734
4735 /**
4736 * @brief Reads an array of words/halfwords/bytes from target memory starting at specified address.
4737 *
4738 * Usage: mdw [phys] <address> [<count>] - for 32 bit reads
4739 * mdh [phys] <address> [<count>] - for 16 bit reads
4740 * mdb [phys] <address> [<count>] - for 8 bit reads
4741 *
4742 * Count defaults to 1.
4743 *
4744 * Calls target_read_memory or target_read_phys_memory depending on
4745 * the presence of the "phys" argument
4746 * Reads the target memory in blocks of max. 32 bytes, and returns an array of ints formatted
4747 * to int representation in base16.
4748 * Also outputs read data in a human readable form using command_print
4749 *
4750 * @param phys if present target_read_phys_memory will be used instead of target_read_memory
4751 * @param address address where to start the read. May be specified in decimal or hex using the standard "0x" prefix
4752 * @param count optional count parameter to read an array of values. If not specified, defaults to 1.
4753 * @returns: JIM_ERR on error or JIM_OK on success and sets the result string to an array of ascii formatted numbers
4754 * on success, with [<count>] number of elements.
4755 *
4756 * In case of little endian target:
4757 * Example1: "mdw 0x00000000" returns "10123456"
4758 * Exmaple2: "mdh 0x00000000 1" returns "3456"
4759 * Example3: "mdb 0x00000000" returns "56"
4760 * Example4: "mdh 0x00000000 2" returns "3456 1012"
4761 * Example5: "mdb 0x00000000 3" returns "56 34 12"
4762 **/
4763 static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4764 {
4765 const char *cmd_name = Jim_GetString(argv[0], NULL);
4766
4767 Jim_GetOptInfo goi;
4768 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4769
4770 if ((goi.argc < 1) || (goi.argc > 3)) {
4771 Jim_SetResultFormatted(goi.interp,
4772 "usage: %s [phys] <address> [<count>]", cmd_name);
4773 return JIM_ERR;
4774 }
4775
4776 int (*fn)(struct target *target,
4777 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
4778 fn = target_read_memory;
4779
4780 int e;
4781 if (strcmp(Jim_GetString(argv[1], NULL), "phys") == 0) {
4782 /* consume it */
4783 struct Jim_Obj *obj;
4784 e = Jim_GetOpt_Obj(&goi, &obj);
4785 if (e != JIM_OK)
4786 return e;
4787
4788 fn = target_read_phys_memory;
4789 }
4790
4791 /* Read address parameter */
4792 jim_wide addr;
4793 e = Jim_GetOpt_Wide(&goi, &addr);
4794 if (e != JIM_OK)
4795 return JIM_ERR;
4796
4797 /* If next parameter exists, read it out as the count parameter, if not, set it to 1 (default) */
4798 jim_wide count;
4799 if (goi.argc == 1) {
4800 e = Jim_GetOpt_Wide(&goi, &count);
4801 if (e != JIM_OK)
4802 return JIM_ERR;
4803 } else
4804 count = 1;
4805
4806 /* all args must be consumed */
4807 if (goi.argc != 0)
4808 return JIM_ERR;
4809
4810 jim_wide dwidth = 1; /* shut up gcc */
4811 if (strcasecmp(cmd_name, "mdw") == 0)
4812 dwidth = 4;
4813 else if (strcasecmp(cmd_name, "mdh") == 0)
4814 dwidth = 2;
4815 else if (strcasecmp(cmd_name, "mdb") == 0)
4816 dwidth = 1;
4817 else {
4818 LOG_ERROR("command '%s' unknown: ", cmd_name);
4819 return JIM_ERR;
4820 }
4821
4822 /* convert count to "bytes" */
4823 int bytes = count * dwidth;
4824
4825 struct target *target = Jim_CmdPrivData(goi.interp);
4826 uint8_t target_buf[32];
4827 jim_wide x, y, z;
4828 while (bytes > 0) {
4829 y = (bytes < 16) ? bytes : 16; /* y = min(bytes, 16); */
4830
4831 /* Try to read out next block */
4832 e = fn(target, addr, dwidth, y / dwidth, target_buf);
4833
4834 if (e != ERROR_OK) {
4835 Jim_SetResultFormatted(interp, "error reading target @ 0x%08lx", (long)addr);
4836 return JIM_ERR;
4837 }
4838
4839 command_print_sameline(NULL, "0x%08x ", (int)(addr));
4840 switch (dwidth) {
4841 case 4:
4842 for (x = 0; x < 16 && x < y; x += 4) {
4843 z = target_buffer_get_u32(target, &(target_buf[x]));
4844 command_print_sameline(NULL, "%08x ", (int)(z));
4845 }
4846 for (; (x < 16) ; x += 4)
4847 command_print_sameline(NULL, " ");
4848 break;
4849 case 2:
4850 for (x = 0; x < 16 && x < y; x += 2) {
4851 z = target_buffer_get_u16(target, &(target_buf[x]));
4852 command_print_sameline(NULL, "%04x ", (int)(z));
4853 }
4854 for (; (x < 16) ; x += 2)
4855 command_print_sameline(NULL, " ");
4856 break;
4857 case 1:
4858 default:
4859 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4860 z = target_buffer_get_u8(target, &(target_buf[x]));
4861 command_print_sameline(NULL, "%02x ", (int)(z));
4862 }
4863 for (; (x < 16) ; x += 1)
4864 command_print_sameline(NULL, " ");
4865 break;
4866 }
4867 /* ascii-ify the bytes */
4868 for (x = 0 ; x < y ; x++) {
4869 if ((target_buf[x] >= 0x20) &&
4870 (target_buf[x] <= 0x7e)) {
4871 /* good */
4872 } else {
4873 /* smack it */
4874 target_buf[x] = '.';
4875 }
4876 }
4877 /* space pad */
4878 while (x < 16) {
4879 target_buf[x] = ' ';
4880 x++;
4881 }
4882 /* terminate */
4883 target_buf[16] = 0;
4884 /* print - with a newline */
4885 command_print_sameline(NULL, "%s\n", target_buf);
4886 /* NEXT... */
4887 bytes -= 16;
4888 addr += 16;
4889 }
4890 return JIM_OK;
4891 }
4892
4893 static int jim_target_mem2array(Jim_Interp *interp,
4894 int argc, Jim_Obj *const *argv)
4895 {
4896 struct target *target = Jim_CmdPrivData(interp);
4897 return target_mem2array(interp, target, argc - 1, argv + 1);
4898 }
4899
4900 static int jim_target_array2mem(Jim_Interp *interp,
4901 int argc, Jim_Obj *const *argv)
4902 {
4903 struct target *target = Jim_CmdPrivData(interp);
4904 return target_array2mem(interp, target, argc - 1, argv + 1);
4905 }
4906
4907 static int jim_target_tap_disabled(Jim_Interp *interp)
4908 {
4909 Jim_SetResultFormatted(interp, "[TAP is disabled]");
4910 return JIM_ERR;
4911 }
4912
4913 static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4914 {
4915 bool allow_defer = false;
4916
4917 Jim_GetOptInfo goi;
4918 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4919 if (goi.argc > 1) {
4920 const char *cmd_name = Jim_GetString(argv[0], NULL);
4921 Jim_SetResultFormatted(goi.interp,
4922 "usage: %s ['allow-defer']", cmd_name);
4923 return JIM_ERR;
4924 }
4925 if (goi.argc > 0 &&
4926 strcmp(Jim_GetString(argv[1], NULL), "allow-defer") == 0) {
4927 /* consume it */
4928 struct Jim_Obj *obj;
4929 int e = Jim_GetOpt_Obj(&goi, &obj);
4930 if (e != JIM_OK)
4931 return e;
4932 allow_defer = true;
4933 }
4934
4935 struct target *target = Jim_CmdPrivData(interp);
4936 if (!target->tap->enabled)
4937 return jim_target_tap_disabled(interp);
4938
4939 if (allow_defer && target->defer_examine) {
4940 LOG_INFO("Deferring arp_examine of %s", target_name(target));
4941 LOG_INFO("Use arp_examine command to examine it manually!");
4942 return JIM_OK;
4943 }
4944
4945 int e = target->type->examine(target);
4946 if (e != ERROR_OK)
4947 return JIM_ERR;
4948 return JIM_OK;
4949 }
4950
4951 static int jim_target_was_examined(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
4952 {
4953 struct target *target = Jim_CmdPrivData(interp);
4954
4955 Jim_SetResultBool(interp, target_was_examined(target));
4956 return JIM_OK;
4957 }
4958
4959 static int jim_target_examine_deferred(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
4960 {
4961 struct target *target = Jim_CmdPrivData(interp);
4962
4963 Jim_SetResultBool(interp, target->defer_examine);
4964 return JIM_OK;
4965 }
4966
4967 static int jim_target_halt_gdb(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4968 {
4969 if (argc != 1) {
4970 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4971 return JIM_ERR;
4972 }
4973 struct target *target = Jim_CmdPrivData(interp);
4974
4975 if (target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT) != ERROR_OK)
4976 return JIM_ERR;
4977
4978 return JIM_OK;
4979 }
4980
4981 static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4982 {
4983 if (argc != 1) {
4984 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4985 return JIM_ERR;
4986 }
4987 struct target *target = Jim_CmdPrivData(interp);
4988 if (!target->tap->enabled)
4989 return jim_target_tap_disabled(interp);
4990
4991 int e;
4992 if (!(target_was_examined(target)))
4993 e = ERROR_TARGET_NOT_EXAMINED;
4994 else
4995 e = target->type->poll(target);
4996 if (e != ERROR_OK)
4997 return JIM_ERR;
4998 return JIM_OK;
4999 }
5000
5001 static int jim_target_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5002 {
5003 Jim_GetOptInfo goi;
5004 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5005
5006 if (goi.argc != 2) {
5007 Jim_WrongNumArgs(interp, 0, argv,
5008 "([tT]|[fF]|assert|deassert) BOOL");
5009 return JIM_ERR;
5010 }
5011
5012 Jim_Nvp *n;
5013 int e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
5014 if (e != JIM_OK) {
5015 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
5016 return e;
5017 }
5018 /* the halt or not param */
5019 jim_wide a;
5020 e = Jim_GetOpt_Wide(&goi, &a);
5021 if (e != JIM_OK)
5022 return e;
5023
5024 struct target *target = Jim_CmdPrivData(goi.interp);
5025 if (!target->tap->enabled)
5026 return jim_target_tap_disabled(interp);
5027
5028 if (!target->type->assert_reset || !target->type->deassert_reset) {
5029 Jim_SetResultFormatted(interp,
5030 "No target-specific reset for %s",
5031 target_name(target));
5032 return JIM_ERR;
5033 }
5034
5035 if (target->defer_examine)
5036 target_reset_examined(target);
5037
5038 /* determine if we should halt or not. */
5039 target->reset_halt = !!a;
5040 /* When this happens - all workareas are invalid. */
5041 target_free_all_working_areas_restore(target, 0);
5042
5043 /* do the assert */
5044 if (n->value == NVP_ASSERT)
5045 e = target->type->assert_reset(target);
5046 else
5047 e = target->type->deassert_reset(target);
5048 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
5049 }
5050
5051 static int jim_target_halt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5052 {
5053 if (argc != 1) {
5054 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
5055 return JIM_ERR;
5056 }
5057 struct target *target = Jim_CmdPrivData(interp);
5058 if (!target->tap->enabled)
5059 return jim_target_tap_disabled(interp);
5060 int e = target->type->halt(target);
5061 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
5062 }
5063
5064 static int jim_target_wait_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5065 {
5066 Jim_GetOptInfo goi;
5067 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5068
5069 /* params: <name> statename timeoutmsecs */
5070 if (goi.argc != 2) {
5071 const char *cmd_name = Jim_GetString(argv[0], NULL);
5072 Jim_SetResultFormatted(goi.interp,
5073 "%s <state_name> <timeout_in_msec>", cmd_name);
5074 return JIM_ERR;
5075 }
5076
5077 Jim_Nvp *n;
5078 int e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
5079 if (e != JIM_OK) {
5080 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state, 1);
5081 return e;
5082 }
5083 jim_wide a;
5084 e = Jim_GetOpt_Wide(&goi, &a);
5085 if (e != JIM_OK)
5086 return e;
5087 struct target *target = Jim_CmdPrivData(interp);
5088 if (!target->tap->enabled)
5089 return jim_target_tap_disabled(interp);
5090
5091 e = target_wait_state(target, n->value, a);
5092 if (e != ERROR_OK) {
5093 Jim_Obj *eObj = Jim_NewIntObj(interp, e);
5094 Jim_SetResultFormatted(goi.interp,
5095 "target: %s wait %s fails (%#s) %s",
5096 target_name(target), n->name,
5097 eObj, target_strerror_safe(e));
5098 Jim_FreeNewObj(interp, eObj);
5099 return JIM_ERR;
5100 }
5101 return JIM_OK;
5102 }
5103 /* List for human, Events defined for this target.
5104 * scripts/programs should use 'name cget -event NAME'
5105 */
5106 static int jim_target_event_list(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5107 {
5108 struct command_context *cmd_ctx = current_command_context(interp);
5109 assert(cmd_ctx != NULL);
5110
5111 struct target *target = Jim_CmdPrivData(interp);
5112 struct target_event_action *teap = target->event_action;
5113 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
5114 target->target_number,
5115 target_name(target));
5116 command_print(cmd_ctx, "%-25s | Body", "Event");
5117 command_print(cmd_ctx, "------------------------- | "
5118 "----------------------------------------");
5119 while (teap) {
5120 Jim_Nvp *opt = Jim_Nvp_value2name_simple(nvp_target_event, teap->event);
5121 command_print(cmd_ctx, "%-25s | %s",
5122 opt->name, Jim_GetString(teap->body, NULL));
5123 teap = teap->next;
5124 }
5125 command_print(cmd_ctx, "***END***");
5126 return JIM_OK;
5127 }
5128 static int jim_target_current_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5129 {
5130 if (argc != 1) {
5131 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
5132 return JIM_ERR;
5133 }
5134 struct target *target = Jim_CmdPrivData(interp);
5135 Jim_SetResultString(interp, target_state_name(target), -1);
5136 return JIM_OK;
5137 }
5138 static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5139 {
5140 Jim_GetOptInfo goi;
5141 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5142 if (goi.argc != 1) {
5143 const char *cmd_name = Jim_GetString(argv[0], NULL);
5144 Jim_SetResultFormatted(goi.interp, "%s <eventname>", cmd_name);
5145 return JIM_ERR;
5146 }
5147 Jim_Nvp *n;
5148 int e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
5149 if (e != JIM_OK) {
5150 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
5151 return e;
5152 }
5153 struct target *target = Jim_CmdPrivData(interp);
5154 target_handle_event(target, n->value);
5155 return JIM_OK;
5156 }
5157
5158 static const struct command_registration target_instance_command_handlers[] = {
5159 {
5160 .name = "configure",
5161 .mode = COMMAND_CONFIG,
5162 .jim_handler = jim_target_configure,
5163 .help = "configure a new target for use",
5164 .usage = "[target_attribute ...]",
5165 },
5166 {
5167 .name = "cget",
5168 .mode = COMMAND_ANY,
5169 .jim_handler = jim_target_configure,
5170 .help = "returns the specified target attribute",
5171 .usage = "target_attribute",
5172 },
5173 {
5174 .name = "mww",
5175 .mode = COMMAND_EXEC,
5176 .jim_handler = jim_target_mw,
5177 .help = "Write 32-bit word(s) to target memory",
5178 .usage = "address data [count]",
5179 },
5180 {
5181 .name = "mwh",
5182 .mode = COMMAND_EXEC,
5183 .jim_handler = jim_target_mw,
5184 .help = "Write 16-bit half-word(s) to target memory",
5185 .usage = "address data [count]",
5186 },
5187 {
5188 .name = "mwb",
5189 .mode = COMMAND_EXEC,
5190 .jim_handler = jim_target_mw,
5191 .help = "Write byte(s) to target memory",
5192 .usage = "address data [count]",
5193 },
5194 {
5195 .name = "mdw",
5196 .mode = COMMAND_EXEC,
5197 .jim_handler = jim_target_md,
5198 .help = "Display target memory as 32-bit words",
5199 .usage = "address [count]",
5200 },
5201 {
5202 .name = "mdh",
5203 .mode = COMMAND_EXEC,
5204 .jim_handler = jim_target_md,
5205 .help = "Display target memory as 16-bit half-words",
5206 .usage = "address [count]",
5207 },
5208 {
5209 .name = "mdb",
5210 .mode = COMMAND_EXEC,
5211 .jim_handler = jim_target_md,
5212 .help = "Display target memory as 8-bit bytes",
5213 .usage = "address [count]",
5214 },
5215 {
5216 .name = "array2mem",
5217 .mode = COMMAND_EXEC,
5218 .jim_handler = jim_target_array2mem,
5219 .help = "Writes Tcl array of 8/16/32 bit numbers "
5220 "to target memory",
5221 .usage = "arrayname bitwidth address count",
5222 },
5223 {
5224 .name = "mem2array",
5225 .mode = COMMAND_EXEC,
5226 .jim_handler = jim_target_mem2array,
5227 .help = "Loads Tcl array of 8/16/32 bit numbers "
5228 "from target memory",
5229 .usage = "arrayname bitwidth address count",
5230 },
5231 {
5232 .name = "eventlist",
5233 .mode = COMMAND_EXEC,
5234 .jim_handler = jim_target_event_list,
5235 .help = "displays a table of events defined for this target",
5236 },
5237 {
5238 .name = "curstate",
5239 .mode = COMMAND_EXEC,
5240 .jim_handler = jim_target_current_state,
5241 .help = "displays the current state of this target",
5242 },
5243 {
5244 .name = "arp_examine",
5245 .mode = COMMAND_EXEC,
5246 .jim_handler = jim_target_examine,
5247 .help = "used internally for reset processing",
5248 .usage = "arp_examine ['allow-defer']",
5249 },
5250 {
5251 .name = "was_examined",
5252 .mode = COMMAND_EXEC,
5253 .jim_handler = jim_target_was_examined,
5254 .help = "used internally for reset processing",
5255 .usage = "was_examined",
5256 },
5257 {
5258 .name = "examine_deferred",
5259 .mode = COMMAND_EXEC,
5260 .jim_handler = jim_target_examine_deferred,
5261 .help = "used internally for reset processing",
5262 .usage = "examine_deferred",
5263 },
5264 {
5265 .name = "arp_halt_gdb",
5266 .mode = COMMAND_EXEC,
5267 .jim_handler = jim_target_halt_gdb,
5268 .help = "used internally for reset processing to halt GDB",
5269 },
5270 {
5271 .name = "arp_poll",
5272 .mode = COMMAND_EXEC,
5273 .jim_handler = jim_target_poll,
5274 .help = "used internally for reset processing",
5275 },
5276 {
5277 .name = "arp_reset",
5278 .mode = COMMAND_EXEC,
5279 .jim_handler = jim_target_reset,
5280 .help = "used internally for reset processing",
5281 },
5282 {
5283 .name = "arp_halt",
5284 .mode = COMMAND_EXEC,
5285 .jim_handler = jim_target_halt,
5286 .help = "used internally for reset processing",
5287 },
5288 {
5289 .name = "arp_waitstate",
5290 .mode = COMMAND_EXEC,
5291 .jim_handler = jim_target_wait_state,
5292 .help = "used internally for reset processing",
5293 },
5294 {
5295 .name = "invoke-event",
5296 .mode = COMMAND_EXEC,
5297 .jim_handler = jim_target_invoke_event,
5298 .help = "invoke handler for specified event",
5299 .usage = "event_name",
5300 },
5301 COMMAND_REGISTRATION_DONE
5302 };
5303
5304 static int target_create(Jim_GetOptInfo *goi)
5305 {
5306 Jim_Obj *new_cmd;
5307 Jim_Cmd *cmd;
5308 const char *cp;
5309 int e;
5310 int x;
5311 struct target *target;
5312 struct command_context *cmd_ctx;
5313
5314 cmd_ctx = current_command_context(goi->interp);
5315 assert(cmd_ctx != NULL);
5316
5317 if (goi->argc < 3) {
5318 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
5319 return JIM_ERR;
5320 }
5321
5322 /* COMMAND */
5323 Jim_GetOpt_Obj(goi, &new_cmd);
5324 /* does this command exist? */
5325 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
5326 if (cmd) {
5327 cp = Jim_GetString(new_cmd, NULL);
5328 Jim_SetResultFormatted(goi->interp, "Command/target: %s Exists", cp);
5329 return JIM_ERR;
5330 }
5331
5332 /* TYPE */
5333 e = Jim_GetOpt_String(goi, &cp, NULL);
5334 if (e != JIM_OK)
5335 return e;
5336 struct transport *tr = get_current_transport();
5337 if (tr->override_target) {
5338 e = tr->override_target(&cp);
5339 if (e != ERROR_OK) {
5340 LOG_ERROR("The selected transport doesn't support this target");
5341 return JIM_ERR;
5342 }
5343 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
5344 }
5345 /* now does target type exist */
5346 for (x = 0 ; target_types[x] ; x++) {
5347 if (0 == strcmp(cp, target_types[x]->name)) {
5348 /* found */
5349 break;
5350 }
5351
5352 /* check for deprecated name */
5353 if (target_types[x]->deprecated_name) {
5354 if (0 == strcmp(cp, target_types[x]->deprecated_name)) {
5355 /* found */
5356 LOG_WARNING("target name is deprecated use: \'%s\'", target_types[x]->name);
5357 break;
5358 }
5359 }
5360 }
5361 if (target_types[x] == NULL) {
5362 Jim_SetResultFormatted(goi->interp, "Unknown target type %s, try one of ", cp);
5363 for (x = 0 ; target_types[x] ; x++) {
5364 if (target_types[x + 1]) {
5365 Jim_AppendStrings(goi->interp,
5366 Jim_GetResult(goi->interp),
5367 target_types[x]->name,
5368 ", ", NULL);
5369 } else {
5370 Jim_AppendStrings(goi->interp,
5371 Jim_GetResult(goi->interp),
5372 " or ",
5373 target_types[x]->name, NULL);
5374 }
5375 }
5376 return JIM_ERR;
5377 }
5378
5379 /* Create it */
5380 target = calloc(1, sizeof(struct target));
5381 /* set target number */
5382 target->target_number = new_target_number();
5383 cmd_ctx->current_target = target->target_number;
5384
5385 /* allocate memory for each unique target type */
5386 target->type = calloc(1, sizeof(struct target_type));
5387
5388 memcpy(target->type, target_types[x], sizeof(struct target_type));
5389
5390 /* will be set by "-endian" */
5391 target->endianness = TARGET_ENDIAN_UNKNOWN;
5392
5393 /* default to first core, override with -coreid */
5394 target->coreid = 0;
5395
5396 target->working_area = 0x0;
5397 target->working_area_size = 0x0;
5398 target->working_areas = NULL;
5399 target->backup_working_area = 0;
5400
5401 target->state = TARGET_UNKNOWN;
5402 target->debug_reason = DBG_REASON_UNDEFINED;
5403 target->reg_cache = NULL;
5404 target->breakpoints = NULL;
5405 target->watchpoints = NULL;
5406 target->next = NULL;
5407 target->arch_info = NULL;
5408
5409 target->display = 1;
5410
5411 target->halt_issued = false;
5412
5413 /* initialize trace information */
5414 target->trace_info = malloc(sizeof(struct trace));
5415 target->trace_info->num_trace_points = 0;
5416 target->trace_info->trace_points_size = 0;
5417 target->trace_info->trace_points = NULL;
5418 target->trace_info->trace_history_size = 0;
5419 target->trace_info->trace_history = NULL;
5420 target->trace_info->trace_history_pos = 0;
5421 target->trace_info->trace_history_overflowed = 0;
5422
5423 target->dbgmsg = NULL;
5424 target->dbg_msg_enabled = 0;
5425
5426 target->endianness = TARGET_ENDIAN_UNKNOWN;
5427
5428 target->rtos = NULL;
5429 target->rtos_auto_detect = false;
5430
5431 /* Do the rest as "configure" options */
5432 goi->isconfigure = 1;
5433 e = target_configure(goi, target);
5434
5435 if (target->tap == NULL) {
5436 Jim_SetResultString(goi->interp, "-chain-position required when creating target", -1);
5437 e = JIM_ERR;
5438 }
5439
5440 if (e != JIM_OK) {
5441 free(target->type);
5442 free(target);
5443 return e;
5444 }
5445
5446 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
5447 /* default endian to little if not specified */
5448 target->endianness = TARGET_LITTLE_ENDIAN;
5449 }
5450
5451 cp = Jim_GetString(new_cmd, NULL);
5452 target->cmd_name = strdup(cp);
5453
5454 /* create the target specific commands */
5455 if (target->type->commands) {
5456 e = register_commands(cmd_ctx, NULL, target->type->commands);
5457 if (ERROR_OK != e)
5458 LOG_ERROR("unable to register '%s' commands", cp);
5459 }
5460 if (target->type->target_create)
5461 (*(target->type->target_create))(target, goi->interp);
5462
5463 /* append to end of list */
5464 {
5465 struct target **tpp;
5466 tpp = &(all_targets);
5467 while (*tpp)
5468 tpp = &((*tpp)->next);
5469 *tpp = target;
5470 }
5471
5472 /* now - create the new target name command */
5473 const struct command_registration target_subcommands[] = {
5474 {
5475 .chain = target_instance_command_handlers,
5476 },
5477 {
5478 .chain = target->type->commands,
5479 },
5480 COMMAND_REGISTRATION_DONE
5481 };
5482 const struct command_registration target_commands[] = {
5483 {
5484 .name = cp,
5485 .mode = COMMAND_ANY,
5486 .help = "target command group",
5487 .usage = "",
5488 .chain = target_subcommands,
5489 },
5490 COMMAND_REGISTRATION_DONE
5491 };
5492 e = register_commands(cmd_ctx, NULL, target_commands);
5493 if (ERROR_OK != e)
5494 return JIM_ERR;
5495
5496 struct command *c = command_find_in_context(cmd_ctx, cp);
5497 assert(c);
5498 command_set_handler_data(c, target);
5499
5500 return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
5501 }
5502
5503 static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5504 {
5505 if (argc != 1) {
5506 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5507 return JIM_ERR;
5508 }
5509 struct command_context *cmd_ctx = current_command_context(interp);
5510 assert(cmd_ctx != NULL);
5511
5512 Jim_SetResultString(interp, target_name(get_current_target(cmd_ctx)), -1);
5513 return JIM_OK;
5514 }
5515
5516 static int jim_target_types(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5517 {
5518 if (argc != 1) {
5519 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5520 return JIM_ERR;
5521 }
5522 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
5523 for (unsigned x = 0; NULL != target_types[x]; x++) {
5524 Jim_ListAppendElement(interp, Jim_GetResult(interp),
5525 Jim_NewStringObj(interp, target_types[x]->name, -1));
5526 }
5527 return JIM_OK;
5528 }
5529
5530 static int jim_target_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5531 {
5532 if (argc != 1) {
5533 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5534 return JIM_ERR;
5535 }
5536 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
5537 struct target *target = all_targets;
5538 while (target) {
5539 Jim_ListAppendElement(interp, Jim_GetResult(interp),
5540 Jim_NewStringObj(interp, target_name(target), -1));
5541 target = target->next;
5542 }
5543 return JIM_OK;
5544 }
5545
5546 static int jim_target_smp(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5547 {
5548 int i;
5549 const char *targetname;
5550 int retval, len;
5551 struct target *target = (struct target *) NULL;
5552 struct target_list *head, *curr, *new;
5553 curr = (struct target_list *) NULL;
5554 head = (struct target_list *) NULL;
5555
5556 retval = 0;
5557 LOG_DEBUG("%d", argc);
5558 /* argv[1] = target to associate in smp
5559 * argv[2] = target to assoicate in smp
5560 * argv[3] ...
5561 */
5562
5563 for (i = 1; i < argc; i++) {
5564
5565 targetname = Jim_GetString(argv[i], &len);
5566 target = get_target(targetname);
5567 LOG_DEBUG("%s ", targetname);
5568 if (target) {
5569 new = malloc(sizeof(struct target_list));
5570 new->target = target;
5571 new->next = (struct target_list *)NULL;
5572 if (head == (struct target_list *)NULL) {
5573 head = new;
5574 curr = head;
5575 } else {
5576 curr->next = new;
5577 curr = new;
5578 }
5579 }
5580 }
5581 /* now parse the list of cpu and put the target in smp mode*/
5582 curr = head;
5583
5584 while (curr != (struct target_list *)NULL) {
5585 target = curr->target;
5586 target->smp = 1;
5587 target->head = head;
5588 curr = curr->next;
5589 }
5590
5591 if (target && target->rtos)
5592 retval = rtos_smp_init(head->target);
5593
5594 return retval;
5595 }
5596
5597
5598 static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5599 {
5600 Jim_GetOptInfo goi;
5601 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5602 if (goi.argc < 3) {
5603 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
5604 "<name> <target_type> [<target_options> ...]");
5605 return JIM_ERR;
5606 }
5607 return target_create(&goi);
5608 }
5609
5610 static const struct command_registration target_subcommand_handlers[] = {
5611 {
5612 .name = "init",
5613 .mode = COMMAND_CONFIG,
5614 .handler = handle_target_init_command,
5615 .help = "initialize targets",
5616 },
5617 {
5618 .name = "create",
5619 /* REVISIT this should be COMMAND_CONFIG ... */
5620 .mode = COMMAND_ANY,
5621 .jim_handler = jim_target_create,
5622 .usage = "name type '-chain-position' name [options ...]",
5623 .help = "Creates and selects a new target",
5624 },
5625 {
5626 .name = "current",
5627 .mode = COMMAND_ANY,
5628 .jim_handler = jim_target_current,
5629 .help = "Returns the currently selected target",
5630 },
5631 {
5632 .name = "types",
5633 .mode = COMMAND_ANY,
5634 .jim_handler = jim_target_types,
5635 .help = "Returns the available target types as "
5636 "a list of strings",
5637 },
5638 {
5639 .name = "names",
5640 .mode = COMMAND_ANY,
5641 .jim_handler = jim_target_names,
5642 .help = "Returns the names of all targets as a list of strings",
5643 },
5644 {
5645 .name = "smp",
5646 .mode = COMMAND_ANY,
5647 .jim_handler = jim_target_smp,
5648 .usage = "targetname1 targetname2 ...",
5649 .help = "gather several target in a smp list"
5650 },
5651
5652 COMMAND_REGISTRATION_DONE
5653 };
5654
5655 struct FastLoad {
5656 uint32_t address;
5657 uint8_t *data;
5658 int length;
5659
5660 };
5661
5662 static int fastload_num;
5663 static struct FastLoad *fastload;
5664
5665 static void free_fastload(void)
5666 {
5667 if (fastload != NULL) {
5668 int i;
5669 for (i = 0; i < fastload_num; i++) {
5670 if (fastload[i].data)
5671 free(fastload[i].data);
5672 }
5673 free(fastload);
5674 fastload = NULL;
5675 }
5676 }
5677
5678 COMMAND_HANDLER(handle_fast_load_image_command)
5679 {
5680 uint8_t *buffer;
5681 size_t buf_cnt;
5682 uint32_t image_size;
5683 uint32_t min_address = 0;
5684 uint32_t max_address = 0xffffffff;
5685 int i;
5686
5687 struct image image;
5688
5689 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
5690 &image, &min_address, &max_address);
5691 if (ERROR_OK != retval)
5692 return retval;
5693
5694 struct duration bench;
5695 duration_start(&bench);
5696
5697 retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL);
5698 if (retval != ERROR_OK)
5699 return retval;
5700
5701 image_size = 0x0;
5702 retval = ERROR_OK;
5703 fastload_num = image.num_sections;
5704 fastload = malloc(sizeof(struct FastLoad)*image.num_sections);
5705 if (fastload == NULL) {
5706 command_print(CMD_CTX, "out of memory");
5707 image_close(&image);
5708 return ERROR_FAIL;
5709 }
5710 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
5711 for (i = 0; i < image.num_sections; i++) {
5712 buffer = malloc(image.sections[i].size);
5713 if (buffer == NULL) {
5714 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
5715 (int)(image.sections[i].size));
5716 retval = ERROR_FAIL;
5717 break;
5718 }
5719
5720 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
5721 if (retval != ERROR_OK) {
5722 free(buffer);
5723 break;
5724 }
5725
5726 uint32_t offset = 0;
5727 uint32_t length = buf_cnt;
5728
5729 /* DANGER!!! beware of unsigned comparision here!!! */
5730
5731 if ((image.sections[i].base_address + buf_cnt >= min_address) &&
5732 (image.sections[i].base_address < max_address)) {
5733 if (image.sections[i].base_address < min_address) {
5734 /* clip addresses below */
5735 offset += min_address-image.sections[i].base_address;
5736 length -= offset;
5737 }
5738
5739 if (image.sections[i].base_address + buf_cnt > max_address)
5740 length -= (image.sections[i].base_address + buf_cnt)-max_address;
5741
5742 fastload[i].address = image.sections[i].base_address + offset;
5743 fastload[i].data = malloc(length);
5744 if (fastload[i].data == NULL) {
5745 free(buffer);
5746 command_print(CMD_CTX, "error allocating buffer for section (%" PRIu32 " bytes)",
5747 length);
5748 retval = ERROR_FAIL;
5749 break;
5750 }
5751 memcpy(fastload[i].data, buffer + offset, length);
5752 fastload[i].length = length;
5753
5754 image_size += length;
5755 command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
5756 (unsigned int)length,
5757 ((unsigned int)(image.sections[i].base_address + offset)));
5758 }
5759
5760 free(buffer);
5761 }
5762
5763 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
5764 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
5765 "in %fs (%0.3f KiB/s)", image_size,
5766 duration_elapsed(&bench), duration_kbps(&bench, image_size));
5767
5768 command_print(CMD_CTX,
5769 "WARNING: image has not been loaded to target!"
5770 "You can issue a 'fast_load' to finish loading.");
5771 }
5772
5773 image_close(&image);
5774
5775 if (retval != ERROR_OK)
5776 free_fastload();
5777
5778 return retval;
5779 }
5780
5781 COMMAND_HANDLER(handle_fast_load_command)
5782 {
5783 if (CMD_ARGC > 0)
5784 return ERROR_COMMAND_SYNTAX_ERROR;
5785 if (fastload == NULL) {
5786 LOG_ERROR("No image in memory");
5787 return ERROR_FAIL;
5788 }
5789 int i;
5790 int64_t ms = timeval_ms();
5791 int size = 0;
5792 int retval = ERROR_OK;
5793 for (i = 0; i < fastload_num; i++) {
5794 struct target *target = get_current_target(CMD_CTX);
5795 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
5796 (unsigned int)(fastload[i].address),
5797 (unsigned int)(fastload[i].length));
5798 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
5799 if (retval != ERROR_OK)
5800 break;
5801 size += fastload[i].length;
5802 }
5803 if (retval == ERROR_OK) {
5804 int64_t after = timeval_ms();
5805 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
5806 }
5807 return retval;
5808 }
5809
5810 static const struct command_registration target_command_handlers[] = {
5811 {
5812 .name = "targets",
5813 .handler = handle_targets_command,
5814 .mode = COMMAND_ANY,
5815 .help = "change current default target (one parameter) "
5816 "or prints table of all targets (no parameters)",
5817 .usage = "[target]",
5818 },
5819 {
5820 .name = "target",
5821 .mode = COMMAND_CONFIG,
5822 .help = "configure target",
5823
5824 .chain = target_subcommand_handlers,
5825 },
5826 COMMAND_REGISTRATION_DONE
5827 };
5828
5829 int target_register_commands(struct command_context *cmd_ctx)
5830 {
5831 return register_commands(cmd_ctx, NULL, target_command_handlers);
5832 }
5833
5834 static bool target_reset_nag = true;
5835
5836 bool get_target_reset_nag(void)
5837 {
5838 return target_reset_nag;
5839 }
5840
5841 COMMAND_HANDLER(handle_target_reset_nag)
5842 {
5843 return CALL_COMMAND_HANDLER(handle_command_parse_bool,
5844 &target_reset_nag, "Nag after each reset about options to improve "
5845 "performance");
5846 }
5847
5848 COMMAND_HANDLER(handle_ps_command)
5849 {
5850 struct target *target = get_current_target(CMD_CTX);
5851 char *display;
5852 if (target->state != TARGET_HALTED) {
5853 LOG_INFO("target not halted !!");
5854 return ERROR_OK;
5855 }
5856
5857 if ((target->rtos) && (target->rtos->type)
5858 && (target->rtos->type->ps_command)) {
5859 display = target->rtos->type->ps_command(target);
5860 command_print(CMD_CTX, "%s", display);
5861 free(display);
5862 return ERROR_OK;
5863 } else {
5864 LOG_INFO("failed");
5865 return ERROR_TARGET_FAILURE;
5866 }
5867 }
5868
5869 static void binprint(struct command_context *cmd_ctx, const char *text, const uint8_t *buf, int size)
5870 {
5871 if (text != NULL)
5872 command_print_sameline(cmd_ctx, "%s", text);
5873 for (int i = 0; i < size; i++)
5874 command_print_sameline(cmd_ctx, " %02x", buf[i]);
5875 command_print(cmd_ctx, " ");
5876 }
5877
5878 COMMAND_HANDLER(handle_test_mem_access_command)
5879 {
5880 struct target *target = get_current_target(CMD_CTX);
5881 uint32_t test_size;
5882 int retval = ERROR_OK;
5883
5884 if (target->state != TARGET_HALTED) {
5885 LOG_INFO("target not halted !!");
5886 return ERROR_FAIL;
5887 }
5888
5889 if (CMD_ARGC != 1)
5890 return ERROR_COMMAND_SYNTAX_ERROR;
5891
5892 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], test_size);
5893
5894 /* Test reads */
5895 size_t num_bytes = test_size + 4;
5896
5897 struct working_area *wa = NULL;
5898 retval = target_alloc_working_area(target, num_bytes, &wa);
5899 if (retval != ERROR_OK) {
5900 LOG_ERROR("Not enough working area");
5901 return ERROR_FAIL;
5902 }
5903
5904 uint8_t *test_pattern = malloc(num_bytes);
5905
5906 for (size_t i = 0; i < num_bytes; i++)
5907 test_pattern[i] = rand();
5908
5909 retval = target_write_memory(target, wa->address, 1, num_bytes, test_pattern);
5910 if (retval != ERROR_OK) {
5911 LOG_ERROR("Test pattern write failed");
5912 goto out;
5913 }
5914
5915 for (int host_offset = 0; host_offset <= 1; host_offset++) {
5916 for (int size = 1; size <= 4; size *= 2) {
5917 for (int offset = 0; offset < 4; offset++) {
5918 uint32_t count = test_size / size;
5919 size_t host_bufsiz = (count + 2) * size + host_offset;
5920 uint8_t *read_ref = malloc(host_bufsiz);
5921 uint8_t *read_buf = malloc(host_bufsiz);
5922
5923 for (size_t i = 0; i < host_bufsiz; i++) {
5924 read_ref[i] = rand();
5925 read_buf[i] = read_ref[i];
5926 }
5927 command_print_sameline(CMD_CTX,
5928 "Test read %" PRIu32 " x %d @ %d to %saligned buffer: ", count,
5929 size, offset, host_offset ? "un" : "");
5930
5931 struct duration bench;
5932 duration_start(&bench);
5933
5934 retval = target_read_memory(target, wa->address + offset, size, count,
5935 read_buf + size + host_offset);
5936
5937 duration_measure(&bench);
5938
5939 if (retval == ERROR_TARGET_UNALIGNED_ACCESS) {
5940 command_print(CMD_CTX, "Unsupported alignment");
5941 goto next;
5942 } else if (retval != ERROR_OK) {
5943 command_print(CMD_CTX, "Memory read failed");
5944 goto next;
5945 }
5946
5947 /* replay on host */
5948 memcpy(read_ref + size + host_offset, test_pattern + offset, count * size);
5949
5950 /* check result */
5951 int result = memcmp(read_ref, read_buf, host_bufsiz);
5952 if (result == 0) {
5953 command_print(CMD_CTX, "Pass in %fs (%0.3f KiB/s)",
5954 duration_elapsed(&bench),
5955 duration_kbps(&bench, count * size));
5956 } else {
5957 command_print(CMD_CTX, "Compare failed");
5958 binprint(CMD_CTX, "ref:", read_ref, host_bufsiz);
5959 binprint(CMD_CTX, "buf:", read_buf, host_bufsiz);
5960 }
5961 next:
5962 free(read_ref);
5963 free(read_buf);
5964 }
5965 }
5966 }
5967
5968 out:
5969 free(test_pattern);
5970
5971 if (wa != NULL)
5972 target_free_working_area(target, wa);
5973
5974 /* Test writes */
5975 num_bytes = test_size + 4 + 4 + 4;
5976
5977 retval = target_alloc_working_area(target, num_bytes, &wa);
5978 if (retval != ERROR_OK) {
5979 LOG_ERROR("Not enough working area");
5980 return ERROR_FAIL;
5981 }
5982
5983 test_pattern = malloc(num_bytes);
5984
5985 for (size_t i = 0; i < num_bytes; i++)
5986 test_pattern[i] = rand();
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 * size + host_offset;
5993 uint8_t *read_ref = malloc(num_bytes);
5994 uint8_t *read_buf = malloc(num_bytes);
5995 uint8_t *write_buf = malloc(host_bufsiz);
5996
5997 for (size_t i = 0; i < host_bufsiz; i++)
5998 write_buf[i] = rand();
5999 command_print_sameline(CMD_CTX,
6000 "Test write %" PRIu32 " x %d @ %d from %saligned buffer: ", count,
6001 size, offset, host_offset ? "un" : "");
6002
6003 retval = target_write_memory(target, wa->address, 1, num_bytes, test_pattern);
6004 if (retval != ERROR_OK) {
6005 command_print(CMD_CTX, "Test pattern write failed");
6006 goto nextw;
6007 }
6008
6009 /* replay on host */
6010 memcpy(read_ref, test_pattern, num_bytes);
6011 memcpy(read_ref + size + offset, write_buf + host_offset, count * size);
6012
6013 struct duration bench;
6014 duration_start(&bench);
6015
6016 retval = target_write_memory(target, wa->address + size + offset, size, count,
6017 write_buf + host_offset);
6018
6019 duration_measure(&bench);
6020
6021 if (retval == ERROR_TARGET_UNALIGNED_ACCESS) {
6022 command_print(CMD_CTX, "Unsupported alignment");
6023 goto nextw;
6024 } else if (retval != ERROR_OK) {
6025 command_print(CMD_CTX, "Memory write failed");
6026 goto nextw;
6027 }
6028
6029 /* read back */
6030 retval = target_read_memory(target, wa->address, 1, num_bytes, read_buf);
6031 if (retval != ERROR_OK) {
6032 command_print(CMD_CTX, "Test pattern write failed");
6033 goto nextw;
6034 }
6035
6036 /* check result */
6037 int result = memcmp(read_ref, read_buf, num_bytes);
6038 if (result == 0) {
6039 command_print(CMD_CTX, "Pass in %fs (%0.3f KiB/s)",
6040 duration_elapsed(&bench),
6041 duration_kbps(&bench, count * size));
6042 } else {
6043 command_print(CMD_CTX, "Compare failed");
6044 binprint(CMD_CTX, "ref:", read_ref, num_bytes);
6045 binprint(CMD_CTX, "buf:", read_buf, num_bytes);
6046 }
6047 nextw:
6048 free(read_ref);
6049 free(read_buf);
6050 }
6051 }
6052 }
6053
6054 free(test_pattern);
6055
6056 if (wa != NULL)
6057 target_free_working_area(target, wa);
6058 return retval;
6059 }
6060
6061 static const struct command_registration target_exec_command_handlers[] = {
6062 {
6063 .name = "fast_load_image",
6064 .handler = handle_fast_load_image_command,
6065 .mode = COMMAND_ANY,
6066 .help = "Load image into server memory for later use by "
6067 "fast_load; primarily for profiling",
6068 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
6069 "[min_address [max_length]]",
6070 },
6071 {
6072 .name = "fast_load",
6073 .handler = handle_fast_load_command,
6074 .mode = COMMAND_EXEC,
6075 .help = "loads active fast load image to current target "
6076 "- mainly for profiling purposes",
6077 .usage = "",
6078 },
6079 {
6080 .name = "profile",
6081 .handler = handle_profile_command,
6082 .mode = COMMAND_EXEC,
6083 .usage = "seconds filename [start end]",
6084 .help = "profiling samples the CPU PC",
6085 },
6086 /** @todo don't register virt2phys() unless target supports it */
6087 {
6088 .name = "virt2phys",
6089 .handler = handle_virt2phys_command,
6090 .mode = COMMAND_ANY,
6091 .help = "translate a virtual address into a physical address",
6092 .usage = "virtual_address",
6093 },
6094 {
6095 .name = "reg",
6096 .handler = handle_reg_command,
6097 .mode = COMMAND_EXEC,
6098 .help = "display (reread from target with \"force\") or set a register; "
6099 "with no arguments, displays all registers and their values",
6100 .usage = "[(register_number|register_name) [(value|'force')]]",
6101 },
6102 {
6103 .name = "poll",
6104 .handler = handle_poll_command,
6105 .mode = COMMAND_EXEC,
6106 .help = "poll target state; or reconfigure background polling",
6107 .usage = "['on'|'off']",
6108 },
6109 {
6110 .name = "wait_halt",
6111 .handler = handle_wait_halt_command,
6112 .mode = COMMAND_EXEC,
6113 .help = "wait up to the specified number of milliseconds "
6114 "(default 5000) for a previously requested halt",
6115 .usage = "[milliseconds]",
6116 },
6117 {
6118 .name = "halt",
6119 .handler = handle_halt_command,
6120 .mode = COMMAND_EXEC,
6121 .help = "request target to halt, then wait up to the specified"
6122 "number of milliseconds (default 5000) for it to complete",
6123 .usage = "[milliseconds]",
6124 },
6125 {
6126 .name = "resume",
6127 .handler = handle_resume_command,
6128 .mode = COMMAND_EXEC,
6129 .help = "resume target execution from current PC or address",
6130 .usage = "[address]",
6131 },
6132 {
6133 .name = "reset",
6134 .handler = handle_reset_command,
6135 .mode = COMMAND_EXEC,
6136 .usage = "[run|halt|init]",
6137 .help = "Reset all targets into the specified mode."
6138 "Default reset mode is run, if not given.",
6139 },
6140 {
6141 .name = "soft_reset_halt",
6142 .handler = handle_soft_reset_halt_command,
6143 .mode = COMMAND_EXEC,
6144 .usage = "",
6145 .help = "halt the target and do a soft reset",
6146 },
6147 {
6148 .name = "step",
6149 .handler = handle_step_command,
6150 .mode = COMMAND_EXEC,
6151 .help = "step one instruction from current PC or address",
6152 .usage = "[address]",
6153 },
6154 {
6155 .name = "mdw",
6156 .handler = handle_md_command,
6157 .mode = COMMAND_EXEC,
6158 .help = "display memory words",
6159 .usage = "['phys'] address [count]",
6160 },
6161 {
6162 .name = "mdh",
6163 .handler = handle_md_command,
6164 .mode = COMMAND_EXEC,
6165 .help = "display memory half-words",
6166 .usage = "['phys'] address [count]",
6167 },
6168 {
6169 .name = "mdb",
6170 .handler = handle_md_command,
6171 .mode = COMMAND_EXEC,
6172 .help = "display memory bytes",
6173 .usage = "['phys'] address [count]",
6174 },
6175 {
6176 .name = "mww",
6177 .handler = handle_mw_command,
6178 .mode = COMMAND_EXEC,
6179 .help = "write memory word",
6180 .usage = "['phys'] address value [count]",
6181 },
6182 {
6183 .name = "mwh",
6184 .handler = handle_mw_command,
6185 .mode = COMMAND_EXEC,
6186 .help = "write memory half-word",
6187 .usage = "['phys'] address value [count]",
6188 },
6189 {
6190 .name = "mwb",
6191 .handler = handle_mw_command,
6192 .mode = COMMAND_EXEC,
6193 .help = "write memory byte",
6194 .usage = "['phys'] address value [count]",
6195 },
6196 {
6197 .name = "bp",
6198 .handler = handle_bp_command,
6199 .mode = COMMAND_EXEC,
6200 .help = "list or set hardware or software breakpoint",
6201 .usage = "<address> [<asid>]<length> ['hw'|'hw_ctx']",
6202 },
6203 {
6204 .name = "rbp",
6205 .handler = handle_rbp_command,
6206 .mode = COMMAND_EXEC,
6207 .help = "remove breakpoint",
6208 .usage = "address",
6209 },
6210 {
6211 .name = "wp",
6212 .handler = handle_wp_command,
6213 .mode = COMMAND_EXEC,
6214 .help = "list (no params) or create watchpoints",
6215 .usage = "[address length [('r'|'w'|'a') value [mask]]]",
6216 },
6217 {
6218 .name = "rwp",
6219 .handler = handle_rwp_command,
6220 .mode = COMMAND_EXEC,
6221 .help = "remove watchpoint",
6222 .usage = "address",
6223 },
6224 {
6225 .name = "load_image",
6226 .handler = handle_load_image_command,
6227 .mode = COMMAND_EXEC,
6228 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
6229 "[min_address] [max_length]",
6230 },
6231 {
6232 .name = "dump_image",
6233 .handler = handle_dump_image_command,
6234 .mode = COMMAND_EXEC,
6235 .usage = "filename address size",
6236 },
6237 {
6238 .name = "verify_image_checksum",
6239 .handler = handle_verify_image_checksum_command,
6240 .mode = COMMAND_EXEC,
6241 .usage = "filename [offset [type]]",
6242 },
6243 {
6244 .name = "verify_image",
6245 .handler = handle_verify_image_command,
6246 .mode = COMMAND_EXEC,
6247 .usage = "filename [offset [type]]",
6248 },
6249 {
6250 .name = "test_image",
6251 .handler = handle_test_image_command,
6252 .mode = COMMAND_EXEC,
6253 .usage = "filename [offset [type]]",
6254 },
6255 {
6256 .name = "mem2array",
6257 .mode = COMMAND_EXEC,
6258 .jim_handler = jim_mem2array,
6259 .help = "read 8/16/32 bit memory and return as a TCL array "
6260 "for script processing",
6261 .usage = "arrayname bitwidth address count",
6262 },
6263 {
6264 .name = "array2mem",
6265 .mode = COMMAND_EXEC,
6266 .jim_handler = jim_array2mem,
6267 .help = "convert a TCL array to memory locations "
6268 "and write the 8/16/32 bit values",
6269 .usage = "arrayname bitwidth address count",
6270 },
6271 {
6272 .name = "reset_nag",
6273 .handler = handle_target_reset_nag,
6274 .mode = COMMAND_ANY,
6275 .help = "Nag after each reset about options that could have been "
6276 "enabled to improve performance. ",
6277 .usage = "['enable'|'disable']",
6278 },
6279 {
6280 .name = "ps",
6281 .handler = handle_ps_command,
6282 .mode = COMMAND_EXEC,
6283 .help = "list all tasks ",
6284 .usage = " ",
6285 },
6286 {
6287 .name = "test_mem_access",
6288 .handler = handle_test_mem_access_command,
6289 .mode = COMMAND_EXEC,
6290 .help = "Test the target's memory access functions",
6291 .usage = "size",
6292 },
6293
6294 COMMAND_REGISTRATION_DONE
6295 };
6296 static int target_register_user_commands(struct command_context *cmd_ctx)
6297 {
6298 int retval = ERROR_OK;
6299 retval = target_request_register_commands(cmd_ctx);
6300 if (retval != ERROR_OK)
6301 return retval;
6302
6303 retval = trace_register_commands(cmd_ctx);
6304 if (retval != ERROR_OK)
6305 return retval;
6306
6307
6308 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);
6309 }

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)