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

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)