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

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)