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

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)