TARGET: review unused symbols
[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(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 LOG_WARNING("not enough working area available(requested %u, free %u)",
1193 (unsigned)(size), (unsigned)(free_size));
1194 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1195 }
1196
1197 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free);
1198
1199 new_wa = malloc(sizeof(struct working_area));
1200 new_wa->next = NULL;
1201 new_wa->size = size;
1202 new_wa->address = first_free;
1203
1204 if (target->backup_working_area)
1205 {
1206 int retval;
1207 new_wa->backup = malloc(new_wa->size);
1208 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1209 {
1210 free(new_wa->backup);
1211 free(new_wa);
1212 return retval;
1213 }
1214 }
1215 else
1216 {
1217 new_wa->backup = NULL;
1218 }
1219
1220 /* put new entry in list */
1221 *p = new_wa;
1222 }
1223
1224 /* mark as used, and return the new (reused) area */
1225 new_wa->free = 0;
1226 *area = new_wa;
1227
1228 /* user pointer */
1229 new_wa->user = area;
1230
1231 return ERROR_OK;
1232 }
1233
1234 static int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1235 {
1236 if (area->free)
1237 return ERROR_OK;
1238
1239 if (restore && target->backup_working_area)
1240 {
1241 int retval;
1242 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1243 return retval;
1244 }
1245
1246 area->free = 1;
1247
1248 /* mark user pointer invalid */
1249 *area->user = NULL;
1250 area->user = NULL;
1251
1252 return ERROR_OK;
1253 }
1254
1255 int target_free_working_area(struct target *target, struct working_area *area)
1256 {
1257 return target_free_working_area_restore(target, area, 1);
1258 }
1259
1260 /* free resources and restore memory, if restoring memory fails,
1261 * free up resources anyway
1262 */
1263 static void target_free_all_working_areas_restore(struct target *target, int restore)
1264 {
1265 struct working_area *c = target->working_areas;
1266
1267 while (c)
1268 {
1269 struct working_area *next = c->next;
1270 target_free_working_area_restore(target, c, restore);
1271
1272 if (c->backup)
1273 free(c->backup);
1274
1275 free(c);
1276
1277 c = next;
1278 }
1279
1280 target->working_areas = NULL;
1281 }
1282
1283 void target_free_all_working_areas(struct target *target)
1284 {
1285 target_free_all_working_areas_restore(target, 1);
1286 }
1287
1288 int target_arch_state(struct target *target)
1289 {
1290 int retval;
1291 if (target == NULL)
1292 {
1293 LOG_USER("No target has been configured");
1294 return ERROR_OK;
1295 }
1296
1297 LOG_USER("target state: %s", target_state_name( target ));
1298
1299 if (target->state != TARGET_HALTED)
1300 return ERROR_OK;
1301
1302 retval = target->type->arch_state(target);
1303 return retval;
1304 }
1305
1306 /* Single aligned words are guaranteed to use 16 or 32 bit access
1307 * mode respectively, otherwise data is handled as quickly as
1308 * possible
1309 */
1310 int target_write_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1311 {
1312 int retval;
1313 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1314 (int)size, (unsigned)address);
1315
1316 if (!target_was_examined(target))
1317 {
1318 LOG_ERROR("Target not examined yet");
1319 return ERROR_FAIL;
1320 }
1321
1322 if (size == 0) {
1323 return ERROR_OK;
1324 }
1325
1326 if ((address + size - 1) < address)
1327 {
1328 /* GDB can request this when e.g. PC is 0xfffffffc*/
1329 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1330 (unsigned)address,
1331 (unsigned)size);
1332 return ERROR_FAIL;
1333 }
1334
1335 if (((address % 2) == 0) && (size == 2))
1336 {
1337 return target_write_memory(target, address, 2, 1, buffer);
1338 }
1339
1340 /* handle unaligned head bytes */
1341 if (address % 4)
1342 {
1343 uint32_t unaligned = 4 - (address % 4);
1344
1345 if (unaligned > size)
1346 unaligned = size;
1347
1348 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1349 return retval;
1350
1351 buffer += unaligned;
1352 address += unaligned;
1353 size -= unaligned;
1354 }
1355
1356 /* handle aligned words */
1357 if (size >= 4)
1358 {
1359 int aligned = size - (size % 4);
1360
1361 /* use bulk writes above a certain limit. This may have to be changed */
1362 if (aligned > 128)
1363 {
1364 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1365 return retval;
1366 }
1367 else
1368 {
1369 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1370 return retval;
1371 }
1372
1373 buffer += aligned;
1374 address += aligned;
1375 size -= aligned;
1376 }
1377
1378 /* handle tail writes of less than 4 bytes */
1379 if (size > 0)
1380 {
1381 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1382 return retval;
1383 }
1384
1385 return ERROR_OK;
1386 }
1387
1388 /* Single aligned words are guaranteed to use 16 or 32 bit access
1389 * mode respectively, otherwise data is handled as quickly as
1390 * possible
1391 */
1392 int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1393 {
1394 int retval;
1395 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1396 (int)size, (unsigned)address);
1397
1398 if (!target_was_examined(target))
1399 {
1400 LOG_ERROR("Target not examined yet");
1401 return ERROR_FAIL;
1402 }
1403
1404 if (size == 0) {
1405 return ERROR_OK;
1406 }
1407
1408 if ((address + size - 1) < address)
1409 {
1410 /* GDB can request this when e.g. PC is 0xfffffffc*/
1411 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1412 address,
1413 size);
1414 return ERROR_FAIL;
1415 }
1416
1417 if (((address % 2) == 0) && (size == 2))
1418 {
1419 return target_read_memory(target, address, 2, 1, buffer);
1420 }
1421
1422 /* handle unaligned head bytes */
1423 if (address % 4)
1424 {
1425 uint32_t unaligned = 4 - (address % 4);
1426
1427 if (unaligned > size)
1428 unaligned = size;
1429
1430 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1431 return retval;
1432
1433 buffer += unaligned;
1434 address += unaligned;
1435 size -= unaligned;
1436 }
1437
1438 /* handle aligned words */
1439 if (size >= 4)
1440 {
1441 int aligned = size - (size % 4);
1442
1443 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1444 return retval;
1445
1446 buffer += aligned;
1447 address += aligned;
1448 size -= aligned;
1449 }
1450
1451 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1452 if(size >=2)
1453 {
1454 int aligned = size - (size%2);
1455 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1456 if (retval != ERROR_OK)
1457 return retval;
1458
1459 buffer += aligned;
1460 address += aligned;
1461 size -= aligned;
1462 }
1463 /* handle tail writes of less than 4 bytes */
1464 if (size > 0)
1465 {
1466 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1467 return retval;
1468 }
1469
1470 return ERROR_OK;
1471 }
1472
1473 int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
1474 {
1475 uint8_t *buffer;
1476 int retval;
1477 uint32_t i;
1478 uint32_t checksum = 0;
1479 if (!target_was_examined(target))
1480 {
1481 LOG_ERROR("Target not examined yet");
1482 return ERROR_FAIL;
1483 }
1484
1485 if ((retval = target->type->checksum_memory(target, address,
1486 size, &checksum)) != ERROR_OK)
1487 {
1488 buffer = malloc(size);
1489 if (buffer == NULL)
1490 {
1491 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1492 return ERROR_INVALID_ARGUMENTS;
1493 }
1494 retval = target_read_buffer(target, address, size, buffer);
1495 if (retval != ERROR_OK)
1496 {
1497 free(buffer);
1498 return retval;
1499 }
1500
1501 /* convert to target endianess */
1502 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1503 {
1504 uint32_t target_data;
1505 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1506 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1507 }
1508
1509 retval = image_calculate_checksum(buffer, size, &checksum);
1510 free(buffer);
1511 }
1512
1513 *crc = checksum;
1514
1515 return retval;
1516 }
1517
1518 int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank)
1519 {
1520 int retval;
1521 if (!target_was_examined(target))
1522 {
1523 LOG_ERROR("Target not examined yet");
1524 return ERROR_FAIL;
1525 }
1526
1527 if (target->type->blank_check_memory == 0)
1528 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1529
1530 retval = target->type->blank_check_memory(target, address, size, blank);
1531
1532 return retval;
1533 }
1534
1535 int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
1536 {
1537 uint8_t value_buf[4];
1538 if (!target_was_examined(target))
1539 {
1540 LOG_ERROR("Target not examined yet");
1541 return ERROR_FAIL;
1542 }
1543
1544 int retval = target_read_memory(target, address, 4, 1, value_buf);
1545
1546 if (retval == ERROR_OK)
1547 {
1548 *value = target_buffer_get_u32(target, value_buf);
1549 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1550 address,
1551 *value);
1552 }
1553 else
1554 {
1555 *value = 0x0;
1556 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1557 address);
1558 }
1559
1560 return retval;
1561 }
1562
1563 int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
1564 {
1565 uint8_t value_buf[2];
1566 if (!target_was_examined(target))
1567 {
1568 LOG_ERROR("Target not examined yet");
1569 return ERROR_FAIL;
1570 }
1571
1572 int retval = target_read_memory(target, address, 2, 1, value_buf);
1573
1574 if (retval == ERROR_OK)
1575 {
1576 *value = target_buffer_get_u16(target, value_buf);
1577 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1578 address,
1579 *value);
1580 }
1581 else
1582 {
1583 *value = 0x0;
1584 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1585 address);
1586 }
1587
1588 return retval;
1589 }
1590
1591 int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
1592 {
1593 int retval = target_read_memory(target, address, 1, 1, value);
1594 if (!target_was_examined(target))
1595 {
1596 LOG_ERROR("Target not examined yet");
1597 return ERROR_FAIL;
1598 }
1599
1600 if (retval == ERROR_OK)
1601 {
1602 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1603 address,
1604 *value);
1605 }
1606 else
1607 {
1608 *value = 0x0;
1609 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1610 address);
1611 }
1612
1613 return retval;
1614 }
1615
1616 int target_write_u32(struct target *target, uint32_t address, uint32_t value)
1617 {
1618 int retval;
1619 uint8_t value_buf[4];
1620 if (!target_was_examined(target))
1621 {
1622 LOG_ERROR("Target not examined yet");
1623 return ERROR_FAIL;
1624 }
1625
1626 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1627 address,
1628 value);
1629
1630 target_buffer_set_u32(target, value_buf, value);
1631 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1632 {
1633 LOG_DEBUG("failed: %i", retval);
1634 }
1635
1636 return retval;
1637 }
1638
1639 int target_write_u16(struct target *target, uint32_t address, uint16_t value)
1640 {
1641 int retval;
1642 uint8_t value_buf[2];
1643 if (!target_was_examined(target))
1644 {
1645 LOG_ERROR("Target not examined yet");
1646 return ERROR_FAIL;
1647 }
1648
1649 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1650 address,
1651 value);
1652
1653 target_buffer_set_u16(target, value_buf, value);
1654 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1655 {
1656 LOG_DEBUG("failed: %i", retval);
1657 }
1658
1659 return retval;
1660 }
1661
1662 int target_write_u8(struct target *target, uint32_t address, uint8_t value)
1663 {
1664 int retval;
1665 if (!target_was_examined(target))
1666 {
1667 LOG_ERROR("Target not examined yet");
1668 return ERROR_FAIL;
1669 }
1670
1671 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1672 address, value);
1673
1674 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1675 {
1676 LOG_DEBUG("failed: %i", retval);
1677 }
1678
1679 return retval;
1680 }
1681
1682 COMMAND_HANDLER(handle_targets_command)
1683 {
1684 struct target *target = all_targets;
1685
1686 if (CMD_ARGC == 1)
1687 {
1688 target = get_target(CMD_ARGV[0]);
1689 if (target == NULL) {
1690 command_print(CMD_CTX,"Target: %s is unknown, try one of:\n", CMD_ARGV[0]);
1691 goto DumpTargets;
1692 }
1693 if (!target->tap->enabled) {
1694 command_print(CMD_CTX,"Target: TAP %s is disabled, "
1695 "can't be the current target\n",
1696 target->tap->dotted_name);
1697 return ERROR_FAIL;
1698 }
1699
1700 CMD_CTX->current_target = target->target_number;
1701 return ERROR_OK;
1702 }
1703 DumpTargets:
1704
1705 target = all_targets;
1706 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
1707 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
1708 while (target)
1709 {
1710 const char *state;
1711 char marker = ' ';
1712
1713 if (target->tap->enabled)
1714 state = target_state_name( target );
1715 else
1716 state = "tap-disabled";
1717
1718 if (CMD_CTX->current_target == target->target_number)
1719 marker = '*';
1720
1721 /* keep columns lined up to match the headers above */
1722 command_print(CMD_CTX, "%2d%c %-18s %-10s %-6s %-18s %s",
1723 target->target_number,
1724 marker,
1725 target_name(target),
1726 target_type_name(target),
1727 Jim_Nvp_value2name_simple(nvp_target_endian,
1728 target->endianness)->name,
1729 target->tap->dotted_name,
1730 state);
1731 target = target->next;
1732 }
1733
1734 return ERROR_OK;
1735 }
1736
1737 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1738
1739 static int powerDropout;
1740 static int srstAsserted;
1741
1742 static int runPowerRestore;
1743 static int runPowerDropout;
1744 static int runSrstAsserted;
1745 static int runSrstDeasserted;
1746
1747 static int sense_handler(void)
1748 {
1749 static int prevSrstAsserted = 0;
1750 static int prevPowerdropout = 0;
1751
1752 int retval;
1753 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1754 return retval;
1755
1756 int powerRestored;
1757 powerRestored = prevPowerdropout && !powerDropout;
1758 if (powerRestored)
1759 {
1760 runPowerRestore = 1;
1761 }
1762
1763 long long current = timeval_ms();
1764 static long long lastPower = 0;
1765 int waitMore = lastPower + 2000 > current;
1766 if (powerDropout && !waitMore)
1767 {
1768 runPowerDropout = 1;
1769 lastPower = current;
1770 }
1771
1772 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1773 return retval;
1774
1775 int srstDeasserted;
1776 srstDeasserted = prevSrstAsserted && !srstAsserted;
1777
1778 static long long lastSrst = 0;
1779 waitMore = lastSrst + 2000 > current;
1780 if (srstDeasserted && !waitMore)
1781 {
1782 runSrstDeasserted = 1;
1783 lastSrst = current;
1784 }
1785
1786 if (!prevSrstAsserted && srstAsserted)
1787 {
1788 runSrstAsserted = 1;
1789 }
1790
1791 prevSrstAsserted = srstAsserted;
1792 prevPowerdropout = powerDropout;
1793
1794 if (srstDeasserted || powerRestored)
1795 {
1796 /* Other than logging the event we can't do anything here.
1797 * Issuing a reset is a particularly bad idea as we might
1798 * be inside a reset already.
1799 */
1800 }
1801
1802 return ERROR_OK;
1803 }
1804
1805 /* process target state changes */
1806 static int handle_target(void *priv)
1807 {
1808 Jim_Interp *interp = (Jim_Interp *)priv;
1809 int retval = ERROR_OK;
1810
1811 if (!is_jtag_poll_safe())
1812 {
1813 /* polling is disabled currently */
1814 return ERROR_OK;
1815 }
1816
1817 /* we do not want to recurse here... */
1818 static int recursive = 0;
1819 if (! recursive)
1820 {
1821 recursive = 1;
1822 sense_handler();
1823 /* danger! running these procedures can trigger srst assertions and power dropouts.
1824 * We need to avoid an infinite loop/recursion here and we do that by
1825 * clearing the flags after running these events.
1826 */
1827 int did_something = 0;
1828 if (runSrstAsserted)
1829 {
1830 LOG_INFO("srst asserted detected, running srst_asserted proc.");
1831 Jim_Eval(interp, "srst_asserted");
1832 did_something = 1;
1833 }
1834 if (runSrstDeasserted)
1835 {
1836 Jim_Eval(interp, "srst_deasserted");
1837 did_something = 1;
1838 }
1839 if (runPowerDropout)
1840 {
1841 LOG_INFO("Power dropout detected, running power_dropout proc.");
1842 Jim_Eval(interp, "power_dropout");
1843 did_something = 1;
1844 }
1845 if (runPowerRestore)
1846 {
1847 Jim_Eval(interp, "power_restore");
1848 did_something = 1;
1849 }
1850
1851 if (did_something)
1852 {
1853 /* clear detect flags */
1854 sense_handler();
1855 }
1856
1857 /* clear action flags */
1858
1859 runSrstAsserted = 0;
1860 runSrstDeasserted = 0;
1861 runPowerRestore = 0;
1862 runPowerDropout = 0;
1863
1864 recursive = 0;
1865 }
1866
1867 /* Poll targets for state changes unless that's globally disabled.
1868 * Skip targets that are currently disabled.
1869 */
1870 for (struct target *target = all_targets;
1871 is_jtag_poll_safe() && target;
1872 target = target->next)
1873 {
1874 if (!target->tap->enabled)
1875 continue;
1876
1877 /* only poll target if we've got power and srst isn't asserted */
1878 if (!powerDropout && !srstAsserted)
1879 {
1880 /* polling may fail silently until the target has been examined */
1881 if ((retval = target_poll(target)) != ERROR_OK)
1882 {
1883 /* FIX!!!!! If we add a LOG_INFO() here to output a line in GDB
1884 * *why* we are aborting GDB, then we'll spam telnet when the
1885 * poll is failing persistently.
1886 *
1887 * If we could implement an event that detected the
1888 * target going from non-pollable to pollable, we could issue
1889 * an error only upon the transition.
1890 */
1891 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1892 return retval;
1893 }
1894 }
1895 }
1896
1897 return retval;
1898 }
1899
1900 COMMAND_HANDLER(handle_reg_command)
1901 {
1902 struct target *target;
1903 struct reg *reg = NULL;
1904 unsigned count = 0;
1905 char *value;
1906
1907 LOG_DEBUG("-");
1908
1909 target = get_current_target(CMD_CTX);
1910
1911 /* list all available registers for the current target */
1912 if (CMD_ARGC == 0)
1913 {
1914 struct reg_cache *cache = target->reg_cache;
1915
1916 count = 0;
1917 while (cache)
1918 {
1919 unsigned i;
1920
1921 command_print(CMD_CTX, "===== %s", cache->name);
1922
1923 for (i = 0, reg = cache->reg_list;
1924 i < cache->num_regs;
1925 i++, reg++, count++)
1926 {
1927 /* only print cached values if they are valid */
1928 if (reg->valid) {
1929 value = buf_to_str(reg->value,
1930 reg->size, 16);
1931 command_print(CMD_CTX,
1932 "(%i) %s (/%" PRIu32 "): 0x%s%s",
1933 count, reg->name,
1934 reg->size, value,
1935 reg->dirty
1936 ? " (dirty)"
1937 : "");
1938 free(value);
1939 } else {
1940 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
1941 count, reg->name,
1942 reg->size) ;
1943 }
1944 }
1945 cache = cache->next;
1946 }
1947
1948 return ERROR_OK;
1949 }
1950
1951 /* access a single register by its ordinal number */
1952 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9'))
1953 {
1954 unsigned num;
1955 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
1956
1957 struct reg_cache *cache = target->reg_cache;
1958 count = 0;
1959 while (cache)
1960 {
1961 unsigned i;
1962 for (i = 0; i < cache->num_regs; i++)
1963 {
1964 if (count++ == num)
1965 {
1966 reg = &cache->reg_list[i];
1967 break;
1968 }
1969 }
1970 if (reg)
1971 break;
1972 cache = cache->next;
1973 }
1974
1975 if (!reg)
1976 {
1977 command_print(CMD_CTX, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1978 return ERROR_OK;
1979 }
1980 } else /* access a single register by its name */
1981 {
1982 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
1983
1984 if (!reg)
1985 {
1986 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
1987 return ERROR_OK;
1988 }
1989 }
1990
1991 /* display a register */
1992 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0') && (CMD_ARGV[1][0] <= '9'))))
1993 {
1994 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
1995 reg->valid = 0;
1996
1997 if (reg->valid == 0)
1998 {
1999 reg->type->get(reg);
2000 }
2001 value = buf_to_str(reg->value, reg->size, 16);
2002 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2003 free(value);
2004 return ERROR_OK;
2005 }
2006
2007 /* set register value */
2008 if (CMD_ARGC == 2)
2009 {
2010 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
2011 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
2012
2013 reg->type->set(reg, buf);
2014
2015 value = buf_to_str(reg->value, reg->size, 16);
2016 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2017 free(value);
2018
2019 free(buf);
2020
2021 return ERROR_OK;
2022 }
2023
2024 command_print(CMD_CTX, "usage: reg <#|name> [value]");
2025
2026 return ERROR_OK;
2027 }
2028
2029 COMMAND_HANDLER(handle_poll_command)
2030 {
2031 int retval = ERROR_OK;
2032 struct target *target = get_current_target(CMD_CTX);
2033
2034 if (CMD_ARGC == 0)
2035 {
2036 command_print(CMD_CTX, "background polling: %s",
2037 jtag_poll_get_enabled() ? "on" : "off");
2038 command_print(CMD_CTX, "TAP: %s (%s)",
2039 target->tap->dotted_name,
2040 target->tap->enabled ? "enabled" : "disabled");
2041 if (!target->tap->enabled)
2042 return ERROR_OK;
2043 if ((retval = target_poll(target)) != ERROR_OK)
2044 return retval;
2045 if ((retval = target_arch_state(target)) != ERROR_OK)
2046 return retval;
2047 }
2048 else if (CMD_ARGC == 1)
2049 {
2050 bool enable;
2051 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
2052 jtag_poll_set_enabled(enable);
2053 }
2054 else
2055 {
2056 return ERROR_COMMAND_SYNTAX_ERROR;
2057 }
2058
2059 return retval;
2060 }
2061
2062 COMMAND_HANDLER(handle_wait_halt_command)
2063 {
2064 if (CMD_ARGC > 1)
2065 return ERROR_COMMAND_SYNTAX_ERROR;
2066
2067 unsigned ms = 5000;
2068 if (1 == CMD_ARGC)
2069 {
2070 int retval = parse_uint(CMD_ARGV[0], &ms);
2071 if (ERROR_OK != retval)
2072 {
2073 command_print(CMD_CTX, "usage: %s [seconds]", CMD_NAME);
2074 return ERROR_COMMAND_SYNTAX_ERROR;
2075 }
2076 // convert seconds (given) to milliseconds (needed)
2077 ms *= 1000;
2078 }
2079
2080 struct target *target = get_current_target(CMD_CTX);
2081 return target_wait_state(target, TARGET_HALTED, ms);
2082 }
2083
2084 /* wait for target state to change. The trick here is to have a low
2085 * latency for short waits and not to suck up all the CPU time
2086 * on longer waits.
2087 *
2088 * After 500ms, keep_alive() is invoked
2089 */
2090 int target_wait_state(struct target *target, enum target_state state, int ms)
2091 {
2092 int retval;
2093 long long then = 0, cur;
2094 int once = 1;
2095
2096 for (;;)
2097 {
2098 if ((retval = target_poll(target)) != ERROR_OK)
2099 return retval;
2100 if (target->state == state)
2101 {
2102 break;
2103 }
2104 cur = timeval_ms();
2105 if (once)
2106 {
2107 once = 0;
2108 then = timeval_ms();
2109 LOG_DEBUG("waiting for target %s...",
2110 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2111 }
2112
2113 if (cur-then > 500)
2114 {
2115 keep_alive();
2116 }
2117
2118 if ((cur-then) > ms)
2119 {
2120 LOG_ERROR("timed out while waiting for target %s",
2121 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2122 return ERROR_FAIL;
2123 }
2124 }
2125
2126 return ERROR_OK;
2127 }
2128
2129 COMMAND_HANDLER(handle_halt_command)
2130 {
2131 LOG_DEBUG("-");
2132
2133 struct target *target = get_current_target(CMD_CTX);
2134 int retval = target_halt(target);
2135 if (ERROR_OK != retval)
2136 return retval;
2137
2138 if (CMD_ARGC == 1)
2139 {
2140 unsigned wait;
2141 retval = parse_uint(CMD_ARGV[0], &wait);
2142 if (ERROR_OK != retval)
2143 return ERROR_COMMAND_SYNTAX_ERROR;
2144 if (!wait)
2145 return ERROR_OK;
2146 }
2147
2148 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2149 }
2150
2151 COMMAND_HANDLER(handle_soft_reset_halt_command)
2152 {
2153 struct target *target = get_current_target(CMD_CTX);
2154
2155 LOG_USER("requesting target halt and executing a soft reset");
2156
2157 target->type->soft_reset_halt(target);
2158
2159 return ERROR_OK;
2160 }
2161
2162 COMMAND_HANDLER(handle_reset_command)
2163 {
2164 if (CMD_ARGC > 1)
2165 return ERROR_COMMAND_SYNTAX_ERROR;
2166
2167 enum target_reset_mode reset_mode = RESET_RUN;
2168 if (CMD_ARGC == 1)
2169 {
2170 const Jim_Nvp *n;
2171 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
2172 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2173 return ERROR_COMMAND_SYNTAX_ERROR;
2174 }
2175 reset_mode = n->value;
2176 }
2177
2178 /* reset *all* targets */
2179 return target_process_reset(CMD_CTX, reset_mode);
2180 }
2181
2182
2183 COMMAND_HANDLER(handle_resume_command)
2184 {
2185 int current = 1;
2186 if (CMD_ARGC > 1)
2187 return ERROR_COMMAND_SYNTAX_ERROR;
2188
2189 struct target *target = get_current_target(CMD_CTX);
2190 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2191
2192 /* with no CMD_ARGV, resume from current pc, addr = 0,
2193 * with one arguments, addr = CMD_ARGV[0],
2194 * handle breakpoints, not debugging */
2195 uint32_t addr = 0;
2196 if (CMD_ARGC == 1)
2197 {
2198 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2199 current = 0;
2200 }
2201
2202 return target_resume(target, current, addr, 1, 0);
2203 }
2204
2205 COMMAND_HANDLER(handle_step_command)
2206 {
2207 if (CMD_ARGC > 1)
2208 return ERROR_COMMAND_SYNTAX_ERROR;
2209
2210 LOG_DEBUG("-");
2211
2212 /* with no CMD_ARGV, step from current pc, addr = 0,
2213 * with one argument addr = CMD_ARGV[0],
2214 * handle breakpoints, debugging */
2215 uint32_t addr = 0;
2216 int current_pc = 1;
2217 if (CMD_ARGC == 1)
2218 {
2219 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2220 current_pc = 0;
2221 }
2222
2223 struct target *target = get_current_target(CMD_CTX);
2224
2225 return target->type->step(target, current_pc, addr, 1);
2226 }
2227
2228 static void handle_md_output(struct command_context *cmd_ctx,
2229 struct target *target, uint32_t address, unsigned size,
2230 unsigned count, const uint8_t *buffer)
2231 {
2232 const unsigned line_bytecnt = 32;
2233 unsigned line_modulo = line_bytecnt / size;
2234
2235 char output[line_bytecnt * 4 + 1];
2236 unsigned output_len = 0;
2237
2238 const char *value_fmt;
2239 switch (size) {
2240 case 4: value_fmt = "%8.8x "; break;
2241 case 2: value_fmt = "%4.4x "; break;
2242 case 1: value_fmt = "%2.2x "; break;
2243 default:
2244 /* "can't happen", caller checked */
2245 LOG_ERROR("invalid memory read size: %u", size);
2246 return;
2247 }
2248
2249 for (unsigned i = 0; i < count; i++)
2250 {
2251 if (i % line_modulo == 0)
2252 {
2253 output_len += snprintf(output + output_len,
2254 sizeof(output) - output_len,
2255 "0x%8.8x: ",
2256 (unsigned)(address + (i*size)));
2257 }
2258
2259 uint32_t value = 0;
2260 const uint8_t *value_ptr = buffer + i * size;
2261 switch (size) {
2262 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2263 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2264 case 1: value = *value_ptr;
2265 }
2266 output_len += snprintf(output + output_len,
2267 sizeof(output) - output_len,
2268 value_fmt, value);
2269
2270 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2271 {
2272 command_print(cmd_ctx, "%s", output);
2273 output_len = 0;
2274 }
2275 }
2276 }
2277
2278 COMMAND_HANDLER(handle_md_command)
2279 {
2280 if (CMD_ARGC < 1)
2281 return ERROR_COMMAND_SYNTAX_ERROR;
2282
2283 unsigned size = 0;
2284 switch (CMD_NAME[2]) {
2285 case 'w': size = 4; break;
2286 case 'h': size = 2; break;
2287 case 'b': size = 1; break;
2288 default: return ERROR_COMMAND_SYNTAX_ERROR;
2289 }
2290
2291 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2292 int (*fn)(struct target *target,
2293 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2294 if (physical)
2295 {
2296 CMD_ARGC--;
2297 CMD_ARGV++;
2298 fn=target_read_phys_memory;
2299 } else
2300 {
2301 fn=target_read_memory;
2302 }
2303 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
2304 {
2305 return ERROR_COMMAND_SYNTAX_ERROR;
2306 }
2307
2308 uint32_t address;
2309 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2310
2311 unsigned count = 1;
2312 if (CMD_ARGC == 2)
2313 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
2314
2315 uint8_t *buffer = calloc(count, size);
2316
2317 struct target *target = get_current_target(CMD_CTX);
2318 int retval = fn(target, address, size, count, buffer);
2319 if (ERROR_OK == retval)
2320 handle_md_output(CMD_CTX, target, address, size, count, buffer);
2321
2322 free(buffer);
2323
2324 return retval;
2325 }
2326
2327 typedef int (*target_write_fn)(struct target *target,
2328 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2329
2330 static int target_write_memory_fast(struct target *target,
2331 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
2332 {
2333 return target_write_buffer(target, address, size * count, buffer);
2334 }
2335
2336 static int target_fill_mem(struct target *target,
2337 uint32_t address,
2338 target_write_fn fn,
2339 unsigned data_size,
2340 /* value */
2341 uint32_t b,
2342 /* count */
2343 unsigned c)
2344 {
2345 /* We have to write in reasonably large chunks to be able
2346 * to fill large memory areas with any sane speed */
2347 const unsigned chunk_size = 16384;
2348 uint8_t *target_buf = malloc(chunk_size * data_size);
2349 if (target_buf == NULL)
2350 {
2351 LOG_ERROR("Out of memory");
2352 return ERROR_FAIL;
2353 }
2354
2355 for (unsigned i = 0; i < chunk_size; i ++)
2356 {
2357 switch (data_size)
2358 {
2359 case 4:
2360 target_buffer_set_u32(target, target_buf + i*data_size, b);
2361 break;
2362 case 2:
2363 target_buffer_set_u16(target, target_buf + i*data_size, b);
2364 break;
2365 case 1:
2366 target_buffer_set_u8(target, target_buf + i*data_size, b);
2367 break;
2368 default:
2369 exit(-1);
2370 }
2371 }
2372
2373 int retval = ERROR_OK;
2374
2375 for (unsigned x = 0; x < c; x += chunk_size)
2376 {
2377 unsigned current;
2378 current = c - x;
2379 if (current > chunk_size)
2380 {
2381 current = chunk_size;
2382 }
2383 int retval = fn(target, address + x * data_size, data_size, current, target_buf);
2384 if (retval != ERROR_OK)
2385 {
2386 break;
2387 }
2388 /* avoid GDB timeouts */
2389 keep_alive();
2390 }
2391 free(target_buf);
2392
2393 return retval;
2394 }
2395
2396
2397 COMMAND_HANDLER(handle_mw_command)
2398 {
2399 if (CMD_ARGC < 2)
2400 {
2401 return ERROR_COMMAND_SYNTAX_ERROR;
2402 }
2403 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2404 target_write_fn fn;
2405 if (physical)
2406 {
2407 CMD_ARGC--;
2408 CMD_ARGV++;
2409 fn=target_write_phys_memory;
2410 } else
2411 {
2412 fn = target_write_memory_fast;
2413 }
2414 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
2415 return ERROR_COMMAND_SYNTAX_ERROR;
2416
2417 uint32_t address;
2418 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2419
2420 uint32_t value;
2421 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2422
2423 unsigned count = 1;
2424 if (CMD_ARGC == 3)
2425 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
2426
2427 struct target *target = get_current_target(CMD_CTX);
2428 unsigned wordsize;
2429 switch (CMD_NAME[2])
2430 {
2431 case 'w':
2432 wordsize = 4;
2433 break;
2434 case 'h':
2435 wordsize = 2;
2436 break;
2437 case 'b':
2438 wordsize = 1;
2439 break;
2440 default:
2441 return ERROR_COMMAND_SYNTAX_ERROR;
2442 }
2443
2444 return target_fill_mem(target, address, fn, wordsize, value, count);
2445 }
2446
2447 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
2448 uint32_t *min_address, uint32_t *max_address)
2449 {
2450 if (CMD_ARGC < 1 || CMD_ARGC > 5)
2451 return ERROR_COMMAND_SYNTAX_ERROR;
2452
2453 /* a base address isn't always necessary,
2454 * default to 0x0 (i.e. don't relocate) */
2455 if (CMD_ARGC >= 2)
2456 {
2457 uint32_t addr;
2458 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2459 image->base_address = addr;
2460 image->base_address_set = 1;
2461 }
2462 else
2463 image->base_address_set = 0;
2464
2465 image->start_address_set = 0;
2466
2467 if (CMD_ARGC >= 4)
2468 {
2469 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], *min_address);
2470 }
2471 if (CMD_ARGC == 5)
2472 {
2473 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], *max_address);
2474 // use size (given) to find max (required)
2475 *max_address += *min_address;
2476 }
2477
2478 if (*min_address > *max_address)
2479 return ERROR_COMMAND_SYNTAX_ERROR;
2480
2481 return ERROR_OK;
2482 }
2483
2484 COMMAND_HANDLER(handle_load_image_command)
2485 {
2486 uint8_t *buffer;
2487 size_t buf_cnt;
2488 uint32_t image_size;
2489 uint32_t min_address = 0;
2490 uint32_t max_address = 0xffffffff;
2491 int i;
2492 struct image image;
2493
2494 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
2495 &image, &min_address, &max_address);
2496 if (ERROR_OK != retval)
2497 return retval;
2498
2499 struct target *target = get_current_target(CMD_CTX);
2500
2501 struct duration bench;
2502 duration_start(&bench);
2503
2504 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
2505 {
2506 return ERROR_OK;
2507 }
2508
2509 image_size = 0x0;
2510 retval = ERROR_OK;
2511 for (i = 0; i < image.num_sections; i++)
2512 {
2513 buffer = malloc(image.sections[i].size);
2514 if (buffer == NULL)
2515 {
2516 command_print(CMD_CTX,
2517 "error allocating buffer for section (%d bytes)",
2518 (int)(image.sections[i].size));
2519 break;
2520 }
2521
2522 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2523 {
2524 free(buffer);
2525 break;
2526 }
2527
2528 uint32_t offset = 0;
2529 uint32_t length = buf_cnt;
2530
2531 /* DANGER!!! beware of unsigned comparision here!!! */
2532
2533 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2534 (image.sections[i].base_address < max_address))
2535 {
2536 if (image.sections[i].base_address < min_address)
2537 {
2538 /* clip addresses below */
2539 offset += min_address-image.sections[i].base_address;
2540 length -= offset;
2541 }
2542
2543 if (image.sections[i].base_address + buf_cnt > max_address)
2544 {
2545 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2546 }
2547
2548 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2549 {
2550 free(buffer);
2551 break;
2552 }
2553 image_size += length;
2554 command_print(CMD_CTX, "%u bytes written at address 0x%8.8" PRIx32 "",
2555 (unsigned int)length,
2556 image.sections[i].base_address + offset);
2557 }
2558
2559 free(buffer);
2560 }
2561
2562 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2563 {
2564 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
2565 "in %fs (%0.3f kb/s)", image_size,
2566 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2567 }
2568
2569 image_close(&image);
2570
2571 return retval;
2572
2573 }
2574
2575 COMMAND_HANDLER(handle_dump_image_command)
2576 {
2577 struct fileio fileio;
2578
2579 uint8_t buffer[560];
2580 int retvaltemp;
2581
2582
2583 struct target *target = get_current_target(CMD_CTX);
2584
2585 if (CMD_ARGC != 3)
2586 {
2587 command_print(CMD_CTX, "usage: dump_image <filename> <address> <size>");
2588 return ERROR_OK;
2589 }
2590
2591 uint32_t address;
2592 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], address);
2593 uint32_t size;
2594 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], size);
2595
2596 if (fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2597 {
2598 return ERROR_OK;
2599 }
2600
2601 struct duration bench;
2602 duration_start(&bench);
2603
2604 int retval = ERROR_OK;
2605 while (size > 0)
2606 {
2607 size_t size_written;
2608 uint32_t this_run_size = (size > 560) ? 560 : size;
2609 retval = target_read_buffer(target, address, this_run_size, buffer);
2610 if (retval != ERROR_OK)
2611 {
2612 break;
2613 }
2614
2615 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2616 if (retval != ERROR_OK)
2617 {
2618 break;
2619 }
2620
2621 size -= this_run_size;
2622 address += this_run_size;
2623 }
2624
2625 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2626 return retvaltemp;
2627
2628 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2629 {
2630 command_print(CMD_CTX,
2631 "dumped %ld bytes in %fs (%0.3f kb/s)", (long)fileio.size,
2632 duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
2633 }
2634
2635 return retval;
2636 }
2637
2638 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
2639 {
2640 uint8_t *buffer;
2641 size_t buf_cnt;
2642 uint32_t image_size;
2643 int i;
2644 int retval;
2645 uint32_t checksum = 0;
2646 uint32_t mem_checksum = 0;
2647
2648 struct image image;
2649
2650 struct target *target = get_current_target(CMD_CTX);
2651
2652 if (CMD_ARGC < 1)
2653 {
2654 return ERROR_COMMAND_SYNTAX_ERROR;
2655 }
2656
2657 if (!target)
2658 {
2659 LOG_ERROR("no target selected");
2660 return ERROR_FAIL;
2661 }
2662
2663 struct duration bench;
2664 duration_start(&bench);
2665
2666 if (CMD_ARGC >= 2)
2667 {
2668 uint32_t addr;
2669 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2670 image.base_address = addr;
2671 image.base_address_set = 1;
2672 }
2673 else
2674 {
2675 image.base_address_set = 0;
2676 image.base_address = 0x0;
2677 }
2678
2679 image.start_address_set = 0;
2680
2681 if ((retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL)) != ERROR_OK)
2682 {
2683 return retval;
2684 }
2685
2686 image_size = 0x0;
2687 retval = ERROR_OK;
2688 for (i = 0; i < image.num_sections; i++)
2689 {
2690 buffer = malloc(image.sections[i].size);
2691 if (buffer == NULL)
2692 {
2693 command_print(CMD_CTX,
2694 "error allocating buffer for section (%d bytes)",
2695 (int)(image.sections[i].size));
2696 break;
2697 }
2698 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2699 {
2700 free(buffer);
2701 break;
2702 }
2703
2704 if (verify)
2705 {
2706 /* calculate checksum of image */
2707 image_calculate_checksum(buffer, buf_cnt, &checksum);
2708
2709 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2710 if (retval != ERROR_OK)
2711 {
2712 free(buffer);
2713 break;
2714 }
2715
2716 if (checksum != mem_checksum)
2717 {
2718 /* failed crc checksum, fall back to a binary compare */
2719 uint8_t *data;
2720
2721 command_print(CMD_CTX, "checksum mismatch - attempting binary compare");
2722
2723 data = (uint8_t*)malloc(buf_cnt);
2724
2725 /* Can we use 32bit word accesses? */
2726 int size = 1;
2727 int count = buf_cnt;
2728 if ((count % 4) == 0)
2729 {
2730 size *= 4;
2731 count /= 4;
2732 }
2733 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2734 if (retval == ERROR_OK)
2735 {
2736 uint32_t t;
2737 for (t = 0; t < buf_cnt; t++)
2738 {
2739 if (data[t] != buffer[t])
2740 {
2741 command_print(CMD_CTX,
2742 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2743 (unsigned)(t + image.sections[i].base_address),
2744 data[t],
2745 buffer[t]);
2746 free(data);
2747 free(buffer);
2748 retval = ERROR_FAIL;
2749 goto done;
2750 }
2751 if ((t%16384) == 0)
2752 {
2753 keep_alive();
2754 }
2755 }
2756 }
2757
2758 free(data);
2759 }
2760 } else
2761 {
2762 command_print(CMD_CTX, "address 0x%08" PRIx32 " length 0x%08zx",
2763 image.sections[i].base_address,
2764 buf_cnt);
2765 }
2766
2767 free(buffer);
2768 image_size += buf_cnt;
2769 }
2770 done:
2771 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2772 {
2773 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
2774 "in %fs (%0.3f kb/s)", image_size,
2775 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2776 }
2777
2778 image_close(&image);
2779
2780 return retval;
2781 }
2782
2783 COMMAND_HANDLER(handle_verify_image_command)
2784 {
2785 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 1);
2786 }
2787
2788 COMMAND_HANDLER(handle_test_image_command)
2789 {
2790 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 0);
2791 }
2792
2793 static int handle_bp_command_list(struct command_context *cmd_ctx)
2794 {
2795 struct target *target = get_current_target(cmd_ctx);
2796 struct breakpoint *breakpoint = target->breakpoints;
2797 while (breakpoint)
2798 {
2799 if (breakpoint->type == BKPT_SOFT)
2800 {
2801 char* buf = buf_to_str(breakpoint->orig_instr,
2802 breakpoint->length, 16);
2803 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2804 breakpoint->address,
2805 breakpoint->length,
2806 breakpoint->set, buf);
2807 free(buf);
2808 }
2809 else
2810 {
2811 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2812 breakpoint->address,
2813 breakpoint->length, breakpoint->set);
2814 }
2815
2816 breakpoint = breakpoint->next;
2817 }
2818 return ERROR_OK;
2819 }
2820
2821 static int handle_bp_command_set(struct command_context *cmd_ctx,
2822 uint32_t addr, uint32_t length, int hw)
2823 {
2824 struct target *target = get_current_target(cmd_ctx);
2825 int retval = breakpoint_add(target, addr, length, hw);
2826 if (ERROR_OK == retval)
2827 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2828 else
2829 LOG_ERROR("Failure setting breakpoint");
2830 return retval;
2831 }
2832
2833 COMMAND_HANDLER(handle_bp_command)
2834 {
2835 if (CMD_ARGC == 0)
2836 return handle_bp_command_list(CMD_CTX);
2837
2838 if (CMD_ARGC < 2 || CMD_ARGC > 3)
2839 {
2840 command_print(CMD_CTX, "usage: bp <address> <length> ['hw']");
2841 return ERROR_COMMAND_SYNTAX_ERROR;
2842 }
2843
2844 uint32_t addr;
2845 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2846 uint32_t length;
2847 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2848
2849 int hw = BKPT_SOFT;
2850 if (CMD_ARGC == 3)
2851 {
2852 if (strcmp(CMD_ARGV[2], "hw") == 0)
2853 hw = BKPT_HARD;
2854 else
2855 return ERROR_COMMAND_SYNTAX_ERROR;
2856 }
2857
2858 return handle_bp_command_set(CMD_CTX, addr, length, hw);
2859 }
2860
2861 COMMAND_HANDLER(handle_rbp_command)
2862 {
2863 if (CMD_ARGC != 1)
2864 return ERROR_COMMAND_SYNTAX_ERROR;
2865
2866 uint32_t addr;
2867 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2868
2869 struct target *target = get_current_target(CMD_CTX);
2870 breakpoint_remove(target, addr);
2871
2872 return ERROR_OK;
2873 }
2874
2875 COMMAND_HANDLER(handle_wp_command)
2876 {
2877 struct target *target = get_current_target(CMD_CTX);
2878
2879 if (CMD_ARGC == 0)
2880 {
2881 struct watchpoint *watchpoint = target->watchpoints;
2882
2883 while (watchpoint)
2884 {
2885 command_print(CMD_CTX, "address: 0x%8.8" PRIx32
2886 ", len: 0x%8.8" PRIx32
2887 ", r/w/a: %i, value: 0x%8.8" PRIx32
2888 ", mask: 0x%8.8" PRIx32,
2889 watchpoint->address,
2890 watchpoint->length,
2891 (int)watchpoint->rw,
2892 watchpoint->value,
2893 watchpoint->mask);
2894 watchpoint = watchpoint->next;
2895 }
2896 return ERROR_OK;
2897 }
2898
2899 enum watchpoint_rw type = WPT_ACCESS;
2900 uint32_t addr = 0;
2901 uint32_t length = 0;
2902 uint32_t data_value = 0x0;
2903 uint32_t data_mask = 0xffffffff;
2904
2905 switch (CMD_ARGC)
2906 {
2907 case 5:
2908 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
2909 // fall through
2910 case 4:
2911 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
2912 // fall through
2913 case 3:
2914 switch (CMD_ARGV[2][0])
2915 {
2916 case 'r':
2917 type = WPT_READ;
2918 break;
2919 case 'w':
2920 type = WPT_WRITE;
2921 break;
2922 case 'a':
2923 type = WPT_ACCESS;
2924 break;
2925 default:
2926 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
2927 return ERROR_COMMAND_SYNTAX_ERROR;
2928 }
2929 // fall through
2930 case 2:
2931 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2932 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2933 break;
2934
2935 default:
2936 command_print(CMD_CTX, "usage: wp [address length "
2937 "[(r|w|a) [value [mask]]]]");
2938 return ERROR_COMMAND_SYNTAX_ERROR;
2939 }
2940
2941 int retval = watchpoint_add(target, addr, length, type,
2942 data_value, data_mask);
2943 if (ERROR_OK != retval)
2944 LOG_ERROR("Failure setting watchpoints");
2945
2946 return retval;
2947 }
2948
2949 COMMAND_HANDLER(handle_rwp_command)
2950 {
2951 if (CMD_ARGC != 1)
2952 return ERROR_COMMAND_SYNTAX_ERROR;
2953
2954 uint32_t addr;
2955 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2956
2957 struct target *target = get_current_target(CMD_CTX);
2958 watchpoint_remove(target, addr);
2959
2960 return ERROR_OK;
2961 }
2962
2963
2964 /**
2965 * Translate a virtual address to a physical address.
2966 *
2967 * The low-level target implementation must have logged a detailed error
2968 * which is forwarded to telnet/GDB session.
2969 */
2970 COMMAND_HANDLER(handle_virt2phys_command)
2971 {
2972 if (CMD_ARGC != 1)
2973 return ERROR_COMMAND_SYNTAX_ERROR;
2974
2975 uint32_t va;
2976 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], va);
2977 uint32_t pa;
2978
2979 struct target *target = get_current_target(CMD_CTX);
2980 int retval = target->type->virt2phys(target, va, &pa);
2981 if (retval == ERROR_OK)
2982 command_print(CMD_CTX, "Physical address 0x%08" PRIx32 "", pa);
2983
2984 return retval;
2985 }
2986
2987 static void writeData(FILE *f, const void *data, size_t len)
2988 {
2989 size_t written = fwrite(data, 1, len, f);
2990 if (written != len)
2991 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
2992 }
2993
2994 static void writeLong(FILE *f, int l)
2995 {
2996 int i;
2997 for (i = 0; i < 4; i++)
2998 {
2999 char c = (l >> (i*8))&0xff;
3000 writeData(f, &c, 1);
3001 }
3002
3003 }
3004
3005 static void writeString(FILE *f, char *s)
3006 {
3007 writeData(f, s, strlen(s));
3008 }
3009
3010 /* Dump a gmon.out histogram file. */
3011 static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filename)
3012 {
3013 uint32_t i;
3014 FILE *f = fopen(filename, "w");
3015 if (f == NULL)
3016 return;
3017 writeString(f, "gmon");
3018 writeLong(f, 0x00000001); /* Version */
3019 writeLong(f, 0); /* padding */
3020 writeLong(f, 0); /* padding */
3021 writeLong(f, 0); /* padding */
3022
3023 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
3024 writeData(f, &zero, 1);
3025
3026 /* figure out bucket size */
3027 uint32_t min = samples[0];
3028 uint32_t max = samples[0];
3029 for (i = 0; i < sampleNum; i++)
3030 {
3031 if (min > samples[i])
3032 {
3033 min = samples[i];
3034 }
3035 if (max < samples[i])
3036 {
3037 max = samples[i];
3038 }
3039 }
3040
3041 int addressSpace = (max-min + 1);
3042
3043 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
3044 uint32_t length = addressSpace;
3045 if (length > maxBuckets)
3046 {
3047 length = maxBuckets;
3048 }
3049 int *buckets = malloc(sizeof(int)*length);
3050 if (buckets == NULL)
3051 {
3052 fclose(f);
3053 return;
3054 }
3055 memset(buckets, 0, sizeof(int)*length);
3056 for (i = 0; i < sampleNum;i++)
3057 {
3058 uint32_t address = samples[i];
3059 long long a = address-min;
3060 long long b = length-1;
3061 long long c = addressSpace-1;
3062 int index = (a*b)/c; /* danger!!!! int32 overflows */
3063 buckets[index]++;
3064 }
3065
3066 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3067 writeLong(f, min); /* low_pc */
3068 writeLong(f, max); /* high_pc */
3069 writeLong(f, length); /* # of samples */
3070 writeLong(f, 64000000); /* 64MHz */
3071 writeString(f, "seconds");
3072 for (i = 0; i < (15-strlen("seconds")); i++)
3073 writeData(f, &zero, 1);
3074 writeString(f, "s");
3075
3076 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3077
3078 char *data = malloc(2*length);
3079 if (data != NULL)
3080 {
3081 for (i = 0; i < length;i++)
3082 {
3083 int val;
3084 val = buckets[i];
3085 if (val > 65535)
3086 {
3087 val = 65535;
3088 }
3089 data[i*2]=val&0xff;
3090 data[i*2 + 1]=(val >> 8)&0xff;
3091 }
3092 free(buckets);
3093 writeData(f, data, length * 2);
3094 free(data);
3095 } else
3096 {
3097 free(buckets);
3098 }
3099
3100 fclose(f);
3101 }
3102
3103 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3104 * which will be used as a random sampling of PC */
3105 COMMAND_HANDLER(handle_profile_command)
3106 {
3107 struct target *target = get_current_target(CMD_CTX);
3108 struct timeval timeout, now;
3109
3110 gettimeofday(&timeout, NULL);
3111 if (CMD_ARGC != 2)
3112 {
3113 return ERROR_COMMAND_SYNTAX_ERROR;
3114 }
3115 unsigned offset;
3116 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], offset);
3117
3118 timeval_add_time(&timeout, offset, 0);
3119
3120 /**
3121 * @todo: Some cores let us sample the PC without the
3122 * annoying halt/resume step; for example, ARMv7 PCSR.
3123 * Provide a way to use that more efficient mechanism.
3124 */
3125
3126 command_print(CMD_CTX, "Starting profiling. Halting and resuming the target as often as we can...");
3127
3128 static const int maxSample = 10000;
3129 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3130 if (samples == NULL)
3131 return ERROR_OK;
3132
3133 int numSamples = 0;
3134 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3135 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
3136
3137 for (;;)
3138 {
3139 int retval;
3140 target_poll(target);
3141 if (target->state == TARGET_HALTED)
3142 {
3143 uint32_t t=*((uint32_t *)reg->value);
3144 samples[numSamples++]=t;
3145 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3146 target_poll(target);
3147 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3148 } else if (target->state == TARGET_RUNNING)
3149 {
3150 /* We want to quickly sample the PC. */
3151 if ((retval = target_halt(target)) != ERROR_OK)
3152 {
3153 free(samples);
3154 return retval;
3155 }
3156 } else
3157 {
3158 command_print(CMD_CTX, "Target not halted or running");
3159 retval = ERROR_OK;
3160 break;
3161 }
3162 if (retval != ERROR_OK)
3163 {
3164 break;
3165 }
3166
3167 gettimeofday(&now, NULL);
3168 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3169 {
3170 command_print(CMD_CTX, "Profiling completed. %d samples.", numSamples);
3171 if ((retval = target_poll(target)) != ERROR_OK)
3172 {
3173 free(samples);
3174 return retval;
3175 }
3176 if (target->state == TARGET_HALTED)
3177 {
3178 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3179 }
3180 if ((retval = target_poll(target)) != ERROR_OK)
3181 {
3182 free(samples);
3183 return retval;
3184 }
3185 writeGmon(samples, numSamples, CMD_ARGV[1]);
3186 command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
3187 break;
3188 }
3189 }
3190 free(samples);
3191
3192 return ERROR_OK;
3193 }
3194
3195 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3196 {
3197 char *namebuf;
3198 Jim_Obj *nameObjPtr, *valObjPtr;
3199 int result;
3200
3201 namebuf = alloc_printf("%s(%d)", varname, idx);
3202 if (!namebuf)
3203 return JIM_ERR;
3204
3205 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3206 valObjPtr = Jim_NewIntObj(interp, val);
3207 if (!nameObjPtr || !valObjPtr)
3208 {
3209 free(namebuf);
3210 return JIM_ERR;
3211 }
3212
3213 Jim_IncrRefCount(nameObjPtr);
3214 Jim_IncrRefCount(valObjPtr);
3215 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3216 Jim_DecrRefCount(interp, nameObjPtr);
3217 Jim_DecrRefCount(interp, valObjPtr);
3218 free(namebuf);
3219 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3220 return result;
3221 }
3222
3223 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3224 {
3225 struct command_context *context;
3226 struct target *target;
3227
3228 context = Jim_GetAssocData(interp, "context");
3229 if (context == NULL)
3230 {
3231 LOG_ERROR("mem2array: no command context");
3232 return JIM_ERR;
3233 }
3234 target = get_current_target(context);
3235 if (target == NULL)
3236 {
3237 LOG_ERROR("mem2array: no current target");
3238 return JIM_ERR;
3239 }
3240
3241 return target_mem2array(interp, target, argc-1, argv + 1);
3242 }
3243
3244 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3245 {
3246 long l;
3247 uint32_t width;
3248 int len;
3249 uint32_t addr;
3250 uint32_t count;
3251 uint32_t v;
3252 const char *varname;
3253 int n, e, retval;
3254 uint32_t i;
3255
3256 /* argv[1] = name of array to receive the data
3257 * argv[2] = desired width
3258 * argv[3] = memory address
3259 * argv[4] = count of times to read
3260 */
3261 if (argc != 4) {
3262 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3263 return JIM_ERR;
3264 }
3265 varname = Jim_GetString(argv[0], &len);
3266 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3267
3268 e = Jim_GetLong(interp, argv[1], &l);
3269 width = l;
3270 if (e != JIM_OK) {
3271 return e;
3272 }
3273
3274 e = Jim_GetLong(interp, argv[2], &l);
3275 addr = l;
3276 if (e != JIM_OK) {
3277 return e;
3278 }
3279 e = Jim_GetLong(interp, argv[3], &l);
3280 len = l;
3281 if (e != JIM_OK) {
3282 return e;
3283 }
3284 switch (width) {
3285 case 8:
3286 width = 1;
3287 break;
3288 case 16:
3289 width = 2;
3290 break;
3291 case 32:
3292 width = 4;
3293 break;
3294 default:
3295 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3296 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3297 return JIM_ERR;
3298 }
3299 if (len == 0) {
3300 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3301 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3302 return JIM_ERR;
3303 }
3304 if ((addr + (len * width)) < addr) {
3305 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3306 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3307 return JIM_ERR;
3308 }
3309 /* absurd transfer size? */
3310 if (len > 65536) {
3311 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3312 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3313 return JIM_ERR;
3314 }
3315
3316 if ((width == 1) ||
3317 ((width == 2) && ((addr & 1) == 0)) ||
3318 ((width == 4) && ((addr & 3) == 0))) {
3319 /* all is well */
3320 } else {
3321 char buf[100];
3322 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3323 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3324 addr,
3325 width);
3326 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3327 return JIM_ERR;
3328 }
3329
3330 /* Transfer loop */
3331
3332 /* index counter */
3333 n = 0;
3334
3335 size_t buffersize = 4096;
3336 uint8_t *buffer = malloc(buffersize);
3337 if (buffer == NULL)
3338 return JIM_ERR;
3339
3340 /* assume ok */
3341 e = JIM_OK;
3342 while (len) {
3343 /* Slurp... in buffer size chunks */
3344
3345 count = len; /* in objects.. */
3346 if (count > (buffersize/width)) {
3347 count = (buffersize/width);
3348 }
3349
3350 retval = target_read_memory(target, addr, width, count, buffer);
3351 if (retval != ERROR_OK) {
3352 /* BOO !*/
3353 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3354 (unsigned int)addr,
3355 (int)width,
3356 (int)count);
3357 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3358 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3359 e = JIM_ERR;
3360 len = 0;
3361 } else {
3362 v = 0; /* shut up gcc */
3363 for (i = 0 ;i < count ;i++, n++) {
3364 switch (width) {
3365 case 4:
3366 v = target_buffer_get_u32(target, &buffer[i*width]);
3367 break;
3368 case 2:
3369 v = target_buffer_get_u16(target, &buffer[i*width]);
3370 break;
3371 case 1:
3372 v = buffer[i] & 0x0ff;
3373 break;
3374 }
3375 new_int_array_element(interp, varname, n, v);
3376 }
3377 len -= count;
3378 }
3379 }
3380
3381 free(buffer);
3382
3383 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3384
3385 return JIM_OK;
3386 }
3387
3388 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3389 {
3390 char *namebuf;
3391 Jim_Obj *nameObjPtr, *valObjPtr;
3392 int result;
3393 long l;
3394
3395 namebuf = alloc_printf("%s(%d)", varname, idx);
3396 if (!namebuf)
3397 return JIM_ERR;
3398
3399 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3400 if (!nameObjPtr)
3401 {
3402 free(namebuf);
3403 return JIM_ERR;
3404 }
3405
3406 Jim_IncrRefCount(nameObjPtr);
3407 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3408 Jim_DecrRefCount(interp, nameObjPtr);
3409 free(namebuf);
3410 if (valObjPtr == NULL)
3411 return JIM_ERR;
3412
3413 result = Jim_GetLong(interp, valObjPtr, &l);
3414 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3415 *val = l;
3416 return result;
3417 }
3418
3419 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3420 {
3421 struct command_context *context;
3422 struct target *target;
3423
3424 context = Jim_GetAssocData(interp, "context");
3425 if (context == NULL) {
3426 LOG_ERROR("array2mem: no command context");
3427 return JIM_ERR;
3428 }
3429 target = get_current_target(context);
3430 if (target == NULL) {
3431 LOG_ERROR("array2mem: no current target");
3432 return JIM_ERR;
3433 }
3434
3435 return target_array2mem(interp,target, argc-1, argv + 1);
3436 }
3437
3438 static int target_array2mem(Jim_Interp *interp, struct target *target,
3439 int argc, Jim_Obj *const *argv)
3440 {
3441 long l;
3442 uint32_t width;
3443 int len;
3444 uint32_t addr;
3445 uint32_t count;
3446 uint32_t v;
3447 const char *varname;
3448 int n, e, retval;
3449 uint32_t i;
3450
3451 /* argv[1] = name of array to get the data
3452 * argv[2] = desired width
3453 * argv[3] = memory address
3454 * argv[4] = count to write
3455 */
3456 if (argc != 4) {
3457 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems");
3458 return JIM_ERR;
3459 }
3460 varname = Jim_GetString(argv[0], &len);
3461 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3462
3463 e = Jim_GetLong(interp, argv[1], &l);
3464 width = l;
3465 if (e != JIM_OK) {
3466 return e;
3467 }
3468
3469 e = Jim_GetLong(interp, argv[2], &l);
3470 addr = l;
3471 if (e != JIM_OK) {
3472 return e;
3473 }
3474 e = Jim_GetLong(interp, argv[3], &l);
3475 len = l;
3476 if (e != JIM_OK) {
3477 return e;
3478 }
3479 switch (width) {
3480 case 8:
3481 width = 1;
3482 break;
3483 case 16:
3484 width = 2;
3485 break;
3486 case 32:
3487 width = 4;
3488 break;
3489 default:
3490 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3491 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3492 return JIM_ERR;
3493 }
3494 if (len == 0) {
3495 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3496 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3497 return JIM_ERR;
3498 }
3499 if ((addr + (len * width)) < addr) {
3500 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3501 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3502 return JIM_ERR;
3503 }
3504 /* absurd transfer size? */
3505 if (len > 65536) {
3506 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3507 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3508 return JIM_ERR;
3509 }
3510
3511 if ((width == 1) ||
3512 ((width == 2) && ((addr & 1) == 0)) ||
3513 ((width == 4) && ((addr & 3) == 0))) {
3514 /* all is well */
3515 } else {
3516 char buf[100];
3517 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3518 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3519 (unsigned int)addr,
3520 (int)width);
3521 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3522 return JIM_ERR;
3523 }
3524
3525 /* Transfer loop */
3526
3527 /* index counter */
3528 n = 0;
3529 /* assume ok */
3530 e = JIM_OK;
3531
3532 size_t buffersize = 4096;
3533 uint8_t *buffer = malloc(buffersize);
3534 if (buffer == NULL)
3535 return JIM_ERR;
3536
3537 while (len) {
3538 /* Slurp... in buffer size chunks */
3539
3540 count = len; /* in objects.. */
3541 if (count > (buffersize/width)) {
3542 count = (buffersize/width);
3543 }
3544
3545 v = 0; /* shut up gcc */
3546 for (i = 0 ;i < count ;i++, n++) {
3547 get_int_array_element(interp, varname, n, &v);
3548 switch (width) {
3549 case 4:
3550 target_buffer_set_u32(target, &buffer[i*width], v);
3551 break;
3552 case 2:
3553 target_buffer_set_u16(target, &buffer[i*width], v);
3554 break;
3555 case 1:
3556 buffer[i] = v & 0x0ff;
3557 break;
3558 }
3559 }
3560 len -= count;
3561
3562 retval = target_write_memory(target, addr, width, count, buffer);
3563 if (retval != ERROR_OK) {
3564 /* BOO !*/
3565 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3566 (unsigned int)addr,
3567 (int)width,
3568 (int)count);
3569 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3570 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3571 e = JIM_ERR;
3572 len = 0;
3573 }
3574 }
3575
3576 free(buffer);
3577
3578 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3579
3580 return JIM_OK;
3581 }
3582
3583 /* FIX? should we propagate errors here rather than printing them
3584 * and continuing?
3585 */
3586 void target_handle_event(struct target *target, enum target_event e)
3587 {
3588 struct target_event_action *teap;
3589
3590 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3591 if (teap->event == e) {
3592 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3593 target->target_number,
3594 target_name(target),
3595 target_type_name(target),
3596 e,
3597 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3598 Jim_GetString(teap->body, NULL));
3599 if (Jim_EvalObj(teap->interp, teap->body) != JIM_OK)
3600 {
3601 Jim_PrintErrorMessage(teap->interp);
3602 }
3603 }
3604 }
3605 }
3606
3607 /**
3608 * Returns true only if the target has a handler for the specified event.
3609 */
3610 bool target_has_event_action(struct target *target, enum target_event event)
3611 {
3612 struct target_event_action *teap;
3613
3614 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3615 if (teap->event == event)
3616 return true;
3617 }
3618 return false;
3619 }
3620
3621 enum target_cfg_param {
3622 TCFG_TYPE,
3623 TCFG_EVENT,
3624 TCFG_WORK_AREA_VIRT,
3625 TCFG_WORK_AREA_PHYS,
3626 TCFG_WORK_AREA_SIZE,
3627 TCFG_WORK_AREA_BACKUP,
3628 TCFG_ENDIAN,
3629 TCFG_VARIANT,
3630 TCFG_CHAIN_POSITION,
3631 };
3632
3633 static Jim_Nvp nvp_config_opts[] = {
3634 { .name = "-type", .value = TCFG_TYPE },
3635 { .name = "-event", .value = TCFG_EVENT },
3636 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3637 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3638 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3639 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3640 { .name = "-endian" , .value = TCFG_ENDIAN },
3641 { .name = "-variant", .value = TCFG_VARIANT },
3642 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3643
3644 { .name = NULL, .value = -1 }
3645 };
3646
3647 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
3648 {
3649 Jim_Nvp *n;
3650 Jim_Obj *o;
3651 jim_wide w;
3652 char *cp;
3653 int e;
3654
3655 /* parse config or cget options ... */
3656 while (goi->argc > 0) {
3657 Jim_SetEmptyResult(goi->interp);
3658 /* Jim_GetOpt_Debug(goi); */
3659
3660 if (target->type->target_jim_configure) {
3661 /* target defines a configure function */
3662 /* target gets first dibs on parameters */
3663 e = (*(target->type->target_jim_configure))(target, goi);
3664 if (e == JIM_OK) {
3665 /* more? */
3666 continue;
3667 }
3668 if (e == JIM_ERR) {
3669 /* An error */
3670 return e;
3671 }
3672 /* otherwise we 'continue' below */
3673 }
3674 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3675 if (e != JIM_OK) {
3676 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3677 return e;
3678 }
3679 switch (n->value) {
3680 case TCFG_TYPE:
3681 /* not setable */
3682 if (goi->isconfigure) {
3683 Jim_SetResult_sprintf(goi->interp,
3684 "not settable: %s", n->name);
3685 return JIM_ERR;
3686 } else {
3687 no_params:
3688 if (goi->argc != 0) {
3689 Jim_WrongNumArgs(goi->interp,
3690 goi->argc, goi->argv,
3691 "NO PARAMS");
3692 return JIM_ERR;
3693 }
3694 }
3695 Jim_SetResultString(goi->interp,
3696 target_type_name(target), -1);
3697 /* loop for more */
3698 break;
3699 case TCFG_EVENT:
3700 if (goi->argc == 0) {
3701 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3702 return JIM_ERR;
3703 }
3704
3705 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3706 if (e != JIM_OK) {
3707 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3708 return e;
3709 }
3710
3711 if (goi->isconfigure) {
3712 if (goi->argc != 1) {
3713 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3714 return JIM_ERR;
3715 }
3716 } else {
3717 if (goi->argc != 0) {
3718 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3719 return JIM_ERR;
3720 }
3721 }
3722
3723 {
3724 struct target_event_action *teap;
3725
3726 teap = target->event_action;
3727 /* replace existing? */
3728 while (teap) {
3729 if (teap->event == (enum target_event)n->value) {
3730 break;
3731 }
3732 teap = teap->next;
3733 }
3734
3735 if (goi->isconfigure) {
3736 bool replace = true;
3737 if (teap == NULL) {
3738 /* create new */
3739 teap = calloc(1, sizeof(*teap));
3740 replace = false;
3741 }
3742 teap->event = n->value;
3743 teap->interp = goi->interp;
3744 Jim_GetOpt_Obj(goi, &o);
3745 if (teap->body) {
3746 Jim_DecrRefCount(teap->interp, teap->body);
3747 }
3748 teap->body = Jim_DuplicateObj(goi->interp, o);
3749 /*
3750 * FIXME:
3751 * Tcl/TK - "tk events" have a nice feature.
3752 * See the "BIND" command.
3753 * We should support that here.
3754 * You can specify %X and %Y in the event code.
3755 * The idea is: %T - target name.
3756 * The idea is: %N - target number
3757 * The idea is: %E - event name.
3758 */
3759 Jim_IncrRefCount(teap->body);
3760
3761 if (!replace)
3762 {
3763 /* add to head of event list */
3764 teap->next = target->event_action;
3765 target->event_action = teap;
3766 }
3767 Jim_SetEmptyResult(goi->interp);
3768 } else {
3769 /* get */
3770 if (teap == NULL) {
3771 Jim_SetEmptyResult(goi->interp);
3772 } else {
3773 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3774 }
3775 }
3776 }
3777 /* loop for more */
3778 break;
3779
3780 case TCFG_WORK_AREA_VIRT:
3781 if (goi->isconfigure) {
3782 target_free_all_working_areas(target);
3783 e = Jim_GetOpt_Wide(goi, &w);
3784 if (e != JIM_OK) {
3785 return e;
3786 }
3787 target->working_area_virt = w;
3788 target->working_area_virt_spec = true;
3789 } else {
3790 if (goi->argc != 0) {
3791 goto no_params;
3792 }
3793 }
3794 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3795 /* loop for more */
3796 break;
3797
3798 case TCFG_WORK_AREA_PHYS:
3799 if (goi->isconfigure) {
3800 target_free_all_working_areas(target);
3801 e = Jim_GetOpt_Wide(goi, &w);
3802 if (e != JIM_OK) {
3803 return e;
3804 }
3805 target->working_area_phys = w;
3806 target->working_area_phys_spec = true;
3807 } else {
3808 if (goi->argc != 0) {
3809 goto no_params;
3810 }
3811 }
3812 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3813 /* loop for more */
3814 break;
3815
3816 case TCFG_WORK_AREA_SIZE:
3817 if (goi->isconfigure) {
3818 target_free_all_working_areas(target);
3819 e = Jim_GetOpt_Wide(goi, &w);
3820 if (e != JIM_OK) {
3821 return e;
3822 }
3823 target->working_area_size = w;
3824 } else {
3825 if (goi->argc != 0) {
3826 goto no_params;
3827 }
3828 }
3829 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3830 /* loop for more */
3831 break;
3832
3833 case TCFG_WORK_AREA_BACKUP:
3834 if (goi->isconfigure) {
3835 target_free_all_working_areas(target);
3836 e = Jim_GetOpt_Wide(goi, &w);
3837 if (e != JIM_OK) {
3838 return e;
3839 }
3840 /* make this exactly 1 or 0 */
3841 target->backup_working_area = (!!w);
3842 } else {
3843 if (goi->argc != 0) {
3844 goto no_params;
3845 }
3846 }
3847 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3848 /* loop for more e*/
3849 break;
3850
3851 case TCFG_ENDIAN:
3852 if (goi->isconfigure) {
3853 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3854 if (e != JIM_OK) {
3855 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3856 return e;
3857 }
3858 target->endianness = n->value;
3859 } else {
3860 if (goi->argc != 0) {
3861 goto no_params;
3862 }
3863 }
3864 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3865 if (n->name == NULL) {
3866 target->endianness = TARGET_LITTLE_ENDIAN;
3867 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3868 }
3869 Jim_SetResultString(goi->interp, n->name, -1);
3870 /* loop for more */
3871 break;
3872
3873 case TCFG_VARIANT:
3874 if (goi->isconfigure) {
3875 if (goi->argc < 1) {
3876 Jim_SetResult_sprintf(goi->interp,
3877 "%s ?STRING?",
3878 n->name);
3879 return JIM_ERR;
3880 }
3881 if (target->variant) {
3882 free((void *)(target->variant));
3883 }
3884 e = Jim_GetOpt_String(goi, &cp, NULL);
3885 target->variant = strdup(cp);
3886 } else {
3887 if (goi->argc != 0) {
3888 goto no_params;
3889 }
3890 }
3891 Jim_SetResultString(goi->interp, target->variant,-1);
3892 /* loop for more */
3893 break;
3894 case TCFG_CHAIN_POSITION:
3895 if (goi->isconfigure) {
3896 Jim_Obj *o;
3897 struct jtag_tap *tap;
3898 target_free_all_working_areas(target);
3899 e = Jim_GetOpt_Obj(goi, &o);
3900 if (e != JIM_OK) {
3901 return e;
3902 }
3903 tap = jtag_tap_by_jim_obj(goi->interp, o);
3904 if (tap == NULL) {
3905 return JIM_ERR;
3906 }
3907 /* make this exactly 1 or 0 */
3908 target->tap = tap;
3909 } else {
3910 if (goi->argc != 0) {
3911 goto no_params;
3912 }
3913 }
3914 Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
3915 /* loop for more e*/
3916 break;
3917 }
3918 } /* while (goi->argc) */
3919
3920
3921 /* done - we return */
3922 return JIM_OK;
3923 }
3924
3925 static int
3926 jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3927 {
3928 Jim_GetOptInfo goi;
3929
3930 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3931 goi.isconfigure = !strcmp(Jim_GetString(argv[0], NULL), "configure");
3932 int need_args = 1 + goi.isconfigure;
3933 if (goi.argc < need_args)
3934 {
3935 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
3936 goi.isconfigure
3937 ? "missing: -option VALUE ..."
3938 : "missing: -option ...");
3939 return JIM_ERR;
3940 }
3941 struct target *target = Jim_CmdPrivData(goi.interp);
3942 return target_configure(&goi, target);
3943 }
3944
3945 static int jim_target_mw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3946 {
3947 const char *cmd_name = Jim_GetString(argv[0], NULL);
3948
3949 Jim_GetOptInfo goi;
3950 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3951
3952 /* danger! goi.argc will be modified below! */
3953 argc = goi.argc;
3954
3955 if (argc != 2 && argc != 3)
3956 {
3957 Jim_SetResult_sprintf(goi.interp,
3958 "usage: %s <address> <data> [<count>]", cmd_name);
3959 return JIM_ERR;
3960 }
3961
3962
3963 jim_wide a;
3964 int e = Jim_GetOpt_Wide(&goi, &a);
3965 if (e != JIM_OK)
3966 return e;
3967
3968 jim_wide b;
3969 e = Jim_GetOpt_Wide(&goi, &b);
3970 if (e != JIM_OK)
3971 return e;
3972
3973 jim_wide c = 1;
3974 if (argc == 3)
3975 {
3976 e = Jim_GetOpt_Wide(&goi, &c);
3977 if (e != JIM_OK)
3978 return e;
3979 }
3980
3981 struct target *target = Jim_CmdPrivData(goi.interp);
3982 unsigned data_size;
3983 if (strcasecmp(cmd_name, "mww") == 0) {
3984 data_size = 4;
3985 }
3986 else if (strcasecmp(cmd_name, "mwh") == 0) {
3987 data_size = 2;
3988 }
3989 else if (strcasecmp(cmd_name, "mwb") == 0) {
3990 data_size = 1;
3991 } else {
3992 LOG_ERROR("command '%s' unknown: ", cmd_name);
3993 return JIM_ERR;
3994 }
3995
3996 return (target_fill_mem(target, a, target_write_memory_fast, data_size, b, c) == ERROR_OK) ? JIM_OK : JIM_ERR;
3997 }
3998
3999 static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4000 {
4001 const char *cmd_name = Jim_GetString(argv[0], NULL);
4002
4003 Jim_GetOptInfo goi;
4004 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4005
4006 /* danger! goi.argc will be modified below! */
4007 argc = goi.argc;
4008
4009 if ((argc != 1) && (argc != 2))
4010 {
4011 Jim_SetResult_sprintf(goi.interp,
4012 "usage: %s <address> [<count>]", cmd_name);
4013 return JIM_ERR;
4014 }
4015
4016 jim_wide a;
4017 int e = Jim_GetOpt_Wide(&goi, &a);
4018 if (e != JIM_OK) {
4019 return JIM_ERR;
4020 }
4021 jim_wide c;
4022 if (argc == 2) {
4023 e = Jim_GetOpt_Wide(&goi, &c);
4024 if (e != JIM_OK) {
4025 return JIM_ERR;
4026 }
4027 } else {
4028 c = 1;
4029 }
4030 jim_wide b = 1; /* shut up gcc */
4031 if (strcasecmp(cmd_name, "mdw") == 0)
4032 b = 4;
4033 else if (strcasecmp(cmd_name, "mdh") == 0)
4034 b = 2;
4035 else if (strcasecmp(cmd_name, "mdb") == 0)
4036 b = 1;
4037 else {
4038 LOG_ERROR("command '%s' unknown: ", cmd_name);
4039 return JIM_ERR;
4040 }
4041
4042 /* convert count to "bytes" */
4043 c = c * b;
4044
4045 struct target *target = Jim_CmdPrivData(goi.interp);
4046 uint8_t target_buf[32];
4047 jim_wide x, y, z;
4048 while (c > 0) {
4049 y = c;
4050 if (y > 16) {
4051 y = 16;
4052 }
4053 e = target_read_memory(target, a, b, y / b, target_buf);
4054 if (e != ERROR_OK) {
4055 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
4056 return JIM_ERR;
4057 }
4058
4059 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
4060 switch (b) {
4061 case 4:
4062 for (x = 0; x < 16 && x < y; x += 4)
4063 {
4064 z = target_buffer_get_u32(target, &(target_buf[ x ]));
4065 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
4066 }
4067 for (; (x < 16) ; x += 4) {
4068 Jim_fprintf(interp, interp->cookie_stdout, " ");
4069 }
4070 break;
4071 case 2:
4072 for (x = 0; x < 16 && x < y; x += 2)
4073 {
4074 z = target_buffer_get_u16(target, &(target_buf[ x ]));
4075 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
4076 }
4077 for (; (x < 16) ; x += 2) {
4078 Jim_fprintf(interp, interp->cookie_stdout, " ");
4079 }
4080 break;
4081 case 1:
4082 default:
4083 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4084 z = target_buffer_get_u8(target, &(target_buf[ x ]));
4085 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
4086 }
4087 for (; (x < 16) ; x += 1) {
4088 Jim_fprintf(interp, interp->cookie_stdout, " ");
4089 }
4090 break;
4091 }
4092 /* ascii-ify the bytes */
4093 for (x = 0 ; x < y ; x++) {
4094 if ((target_buf[x] >= 0x20) &&
4095 (target_buf[x] <= 0x7e)) {
4096 /* good */
4097 } else {
4098 /* smack it */
4099 target_buf[x] = '.';
4100 }
4101 }
4102 /* space pad */
4103 while (x < 16) {
4104 target_buf[x] = ' ';
4105 x++;
4106 }
4107 /* terminate */
4108 target_buf[16] = 0;
4109 /* print - with a newline */
4110 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
4111 /* NEXT... */
4112 c -= 16;
4113 a += 16;
4114 }
4115 return JIM_OK;
4116 }
4117
4118 static int jim_target_mem2array(Jim_Interp *interp,
4119 int argc, Jim_Obj *const *argv)
4120 {
4121 struct target *target = Jim_CmdPrivData(interp);
4122 return target_mem2array(interp, target, argc - 1, argv + 1);
4123 }
4124
4125 static int jim_target_array2mem(Jim_Interp *interp,
4126 int argc, Jim_Obj *const *argv)
4127 {
4128 struct target *target = Jim_CmdPrivData(interp);
4129 return target_array2mem(interp, target, argc - 1, argv + 1);
4130 }
4131
4132 static int jim_target_tap_disabled(Jim_Interp *interp)
4133 {
4134 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4135 return JIM_ERR;
4136 }
4137
4138 static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4139 {
4140 if (argc != 1)
4141 {
4142 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4143 return JIM_ERR;
4144 }
4145 struct target *target = Jim_CmdPrivData(interp);
4146 if (!target->tap->enabled)
4147 return jim_target_tap_disabled(interp);
4148
4149 int e = target->type->examine(target);
4150 if (e != ERROR_OK)
4151 {
4152 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
4153 return JIM_ERR;
4154 }
4155 return JIM_OK;
4156 }
4157
4158 static int jim_target_halt_gdb(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4159 {
4160 if (argc != 1)
4161 {
4162 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4163 return JIM_ERR;
4164 }
4165 struct target *target = Jim_CmdPrivData(interp);
4166
4167 if (target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT) != ERROR_OK)
4168 return JIM_ERR;
4169
4170 return JIM_OK;
4171 }
4172
4173 static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4174 {
4175 if (argc != 1)
4176 {
4177 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4178 return JIM_ERR;
4179 }
4180 struct target *target = Jim_CmdPrivData(interp);
4181 if (!target->tap->enabled)
4182 return jim_target_tap_disabled(interp);
4183
4184 int e;
4185 if (!(target_was_examined(target))) {
4186 e = ERROR_TARGET_NOT_EXAMINED;
4187 } else {
4188 e = target->type->poll(target);
4189 }
4190 if (e != ERROR_OK)
4191 {
4192 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4193 return JIM_ERR;
4194 }
4195 return JIM_OK;
4196 }
4197
4198 static int jim_target_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4199 {
4200 Jim_GetOptInfo goi;
4201 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4202
4203 if (goi.argc != 2)
4204 {
4205 Jim_WrongNumArgs(interp, 0, argv,
4206 "([tT]|[fF]|assert|deassert) BOOL");
4207 return JIM_ERR;
4208 }
4209
4210 Jim_Nvp *n;
4211 int e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4212 if (e != JIM_OK)
4213 {
4214 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4215 return e;
4216 }
4217 /* the halt or not param */
4218 jim_wide a;
4219 e = Jim_GetOpt_Wide(&goi, &a);
4220 if (e != JIM_OK)
4221 return e;
4222
4223 struct target *target = Jim_CmdPrivData(goi.interp);
4224 if (!target->tap->enabled)
4225 return jim_target_tap_disabled(interp);
4226 if (!(target_was_examined(target)))
4227 {
4228 LOG_ERROR("Target not examined yet");
4229 return ERROR_TARGET_NOT_EXAMINED;
4230 }
4231 if (!target->type->assert_reset || !target->type->deassert_reset)
4232 {
4233 Jim_SetResult_sprintf(interp,
4234 "No target-specific reset for %s",
4235 target_name(target));
4236 return JIM_ERR;
4237 }
4238 /* determine if we should halt or not. */
4239 target->reset_halt = !!a;
4240 /* When this happens - all workareas are invalid. */
4241 target_free_all_working_areas_restore(target, 0);
4242
4243 /* do the assert */
4244 if (n->value == NVP_ASSERT) {
4245 e = target->type->assert_reset(target);
4246 } else {
4247 e = target->type->deassert_reset(target);
4248 }
4249 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4250 }
4251
4252 static int jim_target_halt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4253 {
4254 if (argc != 1) {
4255 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4256 return JIM_ERR;
4257 }
4258 struct target *target = Jim_CmdPrivData(interp);
4259 if (!target->tap->enabled)
4260 return jim_target_tap_disabled(interp);
4261 int e = target->type->halt(target);
4262 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4263 }
4264
4265 static int jim_target_wait_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4266 {
4267 Jim_GetOptInfo goi;
4268 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4269
4270 /* params: <name> statename timeoutmsecs */
4271 if (goi.argc != 2)
4272 {
4273 const char *cmd_name = Jim_GetString(argv[0], NULL);
4274 Jim_SetResult_sprintf(goi.interp,
4275 "%s <state_name> <timeout_in_msec>", cmd_name);
4276 return JIM_ERR;
4277 }
4278
4279 Jim_Nvp *n;
4280 int e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4281 if (e != JIM_OK) {
4282 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4283 return e;
4284 }
4285 jim_wide a;
4286 e = Jim_GetOpt_Wide(&goi, &a);
4287 if (e != JIM_OK) {
4288 return e;
4289 }
4290 struct target *target = Jim_CmdPrivData(interp);
4291 if (!target->tap->enabled)
4292 return jim_target_tap_disabled(interp);
4293
4294 e = target_wait_state(target, n->value, a);
4295 if (e != ERROR_OK)
4296 {
4297 Jim_SetResult_sprintf(goi.interp,
4298 "target: %s wait %s fails (%d) %s",
4299 target_name(target), n->name,
4300 e, target_strerror_safe(e));
4301 return JIM_ERR;
4302 }
4303 return JIM_OK;
4304 }
4305 /* List for human, Events defined for this target.
4306 * scripts/programs should use 'name cget -event NAME'
4307 */
4308 static int jim_target_event_list(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4309 {
4310 struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
4311 struct target *target = Jim_CmdPrivData(interp);
4312 struct target_event_action *teap = target->event_action;
4313 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4314 target->target_number,
4315 target_name(target));
4316 command_print(cmd_ctx, "%-25s | Body", "Event");
4317 command_print(cmd_ctx, "------------------------- | "
4318 "----------------------------------------");
4319 while (teap)
4320 {
4321 Jim_Nvp *opt = Jim_Nvp_value2name_simple(nvp_target_event, teap->event);
4322 command_print(cmd_ctx, "%-25s | %s",
4323 opt->name, Jim_GetString(teap->body, NULL));
4324 teap = teap->next;
4325 }
4326 command_print(cmd_ctx, "***END***");
4327 return JIM_OK;
4328 }
4329 static int jim_target_current_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4330 {
4331 if (argc != 1)
4332 {
4333 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4334 return JIM_ERR;
4335 }
4336 struct target *target = Jim_CmdPrivData(interp);
4337 Jim_SetResultString(interp, target_state_name(target), -1);
4338 return JIM_OK;
4339 }
4340 static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4341 {
4342 Jim_GetOptInfo goi;
4343 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4344 if (goi.argc != 1)
4345 {
4346 const char *cmd_name = Jim_GetString(argv[0], NULL);
4347 Jim_SetResult_sprintf(goi.interp, "%s <eventname>", cmd_name);
4348 return JIM_ERR;
4349 }
4350 Jim_Nvp *n;
4351 int e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4352 if (e != JIM_OK)
4353 {
4354 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4355 return e;
4356 }
4357 struct target *target = Jim_CmdPrivData(interp);
4358 target_handle_event(target, n->value);
4359 return JIM_OK;
4360 }
4361
4362 static const struct command_registration target_instance_command_handlers[] = {
4363 {
4364 .name = "configure",
4365 .mode = COMMAND_CONFIG,
4366 .jim_handler = jim_target_configure,
4367 .help = "configure a new target for use",
4368 .usage = "[target_attribute ...]",
4369 },
4370 {
4371 .name = "cget",
4372 .mode = COMMAND_ANY,
4373 .jim_handler = jim_target_configure,
4374 .help = "returns the specified target attribute",
4375 .usage = "target_attribute",
4376 },
4377 {
4378 .name = "mww",
4379 .mode = COMMAND_EXEC,
4380 .jim_handler = jim_target_mw,
4381 .help = "Write 32-bit word(s) to target memory",
4382 .usage = "address data [count]",
4383 },
4384 {
4385 .name = "mwh",
4386 .mode = COMMAND_EXEC,
4387 .jim_handler = jim_target_mw,
4388 .help = "Write 16-bit half-word(s) to target memory",
4389 .usage = "address data [count]",
4390 },
4391 {
4392 .name = "mwb",
4393 .mode = COMMAND_EXEC,
4394 .jim_handler = jim_target_mw,
4395 .help = "Write byte(s) to target memory",
4396 .usage = "address data [count]",
4397 },
4398 {
4399 .name = "mdw",
4400 .mode = COMMAND_EXEC,
4401 .jim_handler = jim_target_md,
4402 .help = "Display target memory as 32-bit words",
4403 .usage = "address [count]",
4404 },
4405 {
4406 .name = "mdh",
4407 .mode = COMMAND_EXEC,
4408 .jim_handler = jim_target_md,
4409 .help = "Display target memory as 16-bit half-words",
4410 .usage = "address [count]",
4411 },
4412 {
4413 .name = "mdb",
4414 .mode = COMMAND_EXEC,
4415 .jim_handler = jim_target_md,
4416 .help = "Display target memory as 8-bit bytes",
4417 .usage = "address [count]",
4418 },
4419 {
4420 .name = "array2mem",
4421 .mode = COMMAND_EXEC,
4422 .jim_handler = jim_target_array2mem,
4423 .help = "Writes Tcl array of 8/16/32 bit numbers "
4424 "to target memory",
4425 .usage = "arrayname bitwidth address count",
4426 },
4427 {
4428 .name = "mem2array",
4429 .mode = COMMAND_EXEC,
4430 .jim_handler = jim_target_mem2array,
4431 .help = "Loads Tcl array of 8/16/32 bit numbers "
4432 "from target memory",
4433 .usage = "arrayname bitwidth address count",
4434 },
4435 {
4436 .name = "eventlist",
4437 .mode = COMMAND_EXEC,
4438 .jim_handler = jim_target_event_list,
4439 .help = "displays a table of events defined for this target",
4440 },
4441 {
4442 .name = "curstate",
4443 .mode = COMMAND_EXEC,
4444 .jim_handler = jim_target_current_state,
4445 .help = "displays the current state of this target",
4446 },
4447 {
4448 .name = "arp_examine",
4449 .mode = COMMAND_EXEC,
4450 .jim_handler = jim_target_examine,
4451 .help = "used internally for reset processing",
4452 },
4453 {
4454 .name = "arp_halt_gdb",
4455 .mode = COMMAND_EXEC,
4456 .jim_handler = jim_target_halt_gdb,
4457 .help = "used internally for reset processing to halt GDB",
4458 },
4459 {
4460 .name = "arp_poll",
4461 .mode = COMMAND_EXEC,
4462 .jim_handler = jim_target_poll,
4463 .help = "used internally for reset processing",
4464 },
4465 {
4466 .name = "arp_reset",
4467 .mode = COMMAND_EXEC,
4468 .jim_handler = jim_target_reset,
4469 .help = "used internally for reset processing",
4470 },
4471 {
4472 .name = "arp_halt",
4473 .mode = COMMAND_EXEC,
4474 .jim_handler = jim_target_halt,
4475 .help = "used internally for reset processing",
4476 },
4477 {
4478 .name = "arp_waitstate",
4479 .mode = COMMAND_EXEC,
4480 .jim_handler = jim_target_wait_state,
4481 .help = "used internally for reset processing",
4482 },
4483 {
4484 .name = "invoke-event",
4485 .mode = COMMAND_EXEC,
4486 .jim_handler = jim_target_invoke_event,
4487 .help = "invoke handler for specified event",
4488 .usage = "event_name",
4489 },
4490 COMMAND_REGISTRATION_DONE
4491 };
4492
4493 static int target_create(Jim_GetOptInfo *goi)
4494 {
4495 Jim_Obj *new_cmd;
4496 Jim_Cmd *cmd;
4497 const char *cp;
4498 char *cp2;
4499 int e;
4500 int x;
4501 struct target *target;
4502 struct command_context *cmd_ctx;
4503
4504 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4505 if (goi->argc < 3) {
4506 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4507 return JIM_ERR;
4508 }
4509
4510 /* COMMAND */
4511 Jim_GetOpt_Obj(goi, &new_cmd);
4512 /* does this command exist? */
4513 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4514 if (cmd) {
4515 cp = Jim_GetString(new_cmd, NULL);
4516 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4517 return JIM_ERR;
4518 }
4519
4520 /* TYPE */
4521 e = Jim_GetOpt_String(goi, &cp2, NULL);
4522 cp = cp2;
4523 /* now does target type exist */
4524 for (x = 0 ; target_types[x] ; x++) {
4525 if (0 == strcmp(cp, target_types[x]->name)) {
4526 /* found */
4527 break;
4528 }
4529 }
4530 if (target_types[x] == NULL) {
4531 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4532 for (x = 0 ; target_types[x] ; x++) {
4533 if (target_types[x + 1]) {
4534 Jim_AppendStrings(goi->interp,
4535 Jim_GetResult(goi->interp),
4536 target_types[x]->name,
4537 ", ", NULL);
4538 } else {
4539 Jim_AppendStrings(goi->interp,
4540 Jim_GetResult(goi->interp),
4541 " or ",
4542 target_types[x]->name,NULL);
4543 }
4544 }
4545 return JIM_ERR;
4546 }
4547
4548 /* Create it */
4549 target = calloc(1,sizeof(struct target));
4550 /* set target number */
4551 target->target_number = new_target_number();
4552
4553 /* allocate memory for each unique target type */
4554 target->type = (struct target_type*)calloc(1,sizeof(struct target_type));
4555
4556 memcpy(target->type, target_types[x], sizeof(struct target_type));
4557
4558 /* will be set by "-endian" */
4559 target->endianness = TARGET_ENDIAN_UNKNOWN;
4560
4561 target->working_area = 0x0;
4562 target->working_area_size = 0x0;
4563 target->working_areas = NULL;
4564 target->backup_working_area = 0;
4565
4566 target->state = TARGET_UNKNOWN;
4567 target->debug_reason = DBG_REASON_UNDEFINED;
4568 target->reg_cache = NULL;
4569 target->breakpoints = NULL;
4570 target->watchpoints = NULL;
4571 target->next = NULL;
4572 target->arch_info = NULL;
4573
4574 target->display = 1;
4575
4576 target->halt_issued = false;
4577
4578 /* initialize trace information */
4579 target->trace_info = malloc(sizeof(struct trace));
4580 target->trace_info->num_trace_points = 0;
4581 target->trace_info->trace_points_size = 0;
4582 target->trace_info->trace_points = NULL;
4583 target->trace_info->trace_history_size = 0;
4584 target->trace_info->trace_history = NULL;
4585 target->trace_info->trace_history_pos = 0;
4586 target->trace_info->trace_history_overflowed = 0;
4587
4588 target->dbgmsg = NULL;
4589 target->dbg_msg_enabled = 0;
4590
4591 target->endianness = TARGET_ENDIAN_UNKNOWN;
4592
4593 /* Do the rest as "configure" options */
4594 goi->isconfigure = 1;
4595 e = target_configure(goi, target);
4596
4597 if (target->tap == NULL)
4598 {
4599 Jim_SetResultString(goi->interp, "-chain-position required when creating target", -1);
4600 e = JIM_ERR;
4601 }
4602
4603 if (e != JIM_OK) {
4604 free(target->type);
4605 free(target);
4606 return e;
4607 }
4608
4609 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4610 /* default endian to little if not specified */
4611 target->endianness = TARGET_LITTLE_ENDIAN;
4612 }
4613
4614 /* incase variant is not set */
4615 if (!target->variant)
4616 target->variant = strdup("");
4617
4618 cp = Jim_GetString(new_cmd, NULL);
4619 target->cmd_name = strdup(cp);
4620
4621 /* create the target specific commands */
4622 if (target->type->commands) {
4623 e = register_commands(cmd_ctx, NULL, target->type->commands);
4624 if (ERROR_OK != e)
4625 LOG_ERROR("unable to register '%s' commands", cp);
4626 }
4627 if (target->type->target_create) {
4628 (*(target->type->target_create))(target, goi->interp);
4629 }
4630
4631 /* append to end of list */
4632 {
4633 struct target **tpp;
4634 tpp = &(all_targets);
4635 while (*tpp) {
4636 tpp = &((*tpp)->next);
4637 }
4638 *tpp = target;
4639 }
4640
4641 /* now - create the new target name command */
4642 const const struct command_registration target_subcommands[] = {
4643 {
4644 .chain = target_instance_command_handlers,
4645 },
4646 {
4647 .chain = target->type->commands,
4648 },
4649 COMMAND_REGISTRATION_DONE
4650 };
4651 const const struct command_registration target_commands[] = {
4652 {
4653 .name = cp,
4654 .mode = COMMAND_ANY,
4655 .help = "target command group",
4656 .chain = target_subcommands,
4657 },
4658 COMMAND_REGISTRATION_DONE
4659 };
4660 e = register_commands(cmd_ctx, NULL, target_commands);
4661 if (ERROR_OK != e)
4662 return JIM_ERR;
4663
4664 struct command *c = command_find_in_context(cmd_ctx, cp);
4665 assert(c);
4666 command_set_handler_data(c, target);
4667
4668 return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
4669 }
4670
4671 static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4672 {
4673 if (argc != 1)
4674 {
4675 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4676 return JIM_ERR;
4677 }
4678 struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
4679 Jim_SetResultString(interp, get_current_target(cmd_ctx)->cmd_name, -1);
4680 return JIM_OK;
4681 }
4682
4683 static int jim_target_types(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4684 {
4685 if (argc != 1)
4686 {
4687 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4688 return JIM_ERR;
4689 }
4690 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
4691 for (unsigned x = 0; NULL != target_types[x]; x++)
4692 {
4693 Jim_ListAppendElement(interp, Jim_GetResult(interp),
4694 Jim_NewStringObj(interp, target_types[x]->name, -1));
4695 }
4696 return JIM_OK;
4697 }
4698
4699 static int jim_target_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4700 {
4701 if (argc != 1)
4702 {
4703 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4704 return JIM_ERR;
4705 }
4706 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
4707 struct target *target = all_targets;
4708 while (target)
4709 {
4710 Jim_ListAppendElement(interp, Jim_GetResult(interp),
4711 Jim_NewStringObj(interp, target_name(target), -1));
4712 target = target->next;
4713 }
4714 return JIM_OK;
4715 }
4716
4717 static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4718 {
4719 Jim_GetOptInfo goi;
4720 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4721 if (goi.argc < 3)
4722 {
4723 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
4724 "<name> <target_type> [<target_options> ...]");
4725 return JIM_ERR;
4726 }
4727 return target_create(&goi);
4728 }
4729
4730 static int jim_target_number(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4731 {
4732 Jim_GetOptInfo goi;
4733 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4734
4735 /* It's OK to remove this mechanism sometime after August 2010 or so */
4736 LOG_WARNING("don't use numbers as target identifiers; use names");
4737 if (goi.argc != 1)
4738 {
4739 Jim_SetResult_sprintf(goi.interp, "usage: target number <number>");
4740 return JIM_ERR;
4741 }
4742 jim_wide w;
4743 int e = Jim_GetOpt_Wide(&goi, &w);
4744 if (e != JIM_OK)
4745 return JIM_ERR;
4746
4747 struct target *target;
4748 for (target = all_targets; NULL != target; target = target->next)
4749 {
4750 if (target->target_number != w)
4751 continue;
4752
4753 Jim_SetResultString(goi.interp, target_name(target), -1);
4754 return JIM_OK;
4755 }
4756 Jim_SetResult_sprintf(goi.interp,
4757 "Target: number %d does not exist", (int)(w));
4758 return JIM_ERR;
4759 }
4760
4761 static int jim_target_count(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4762 {
4763 if (argc != 1)
4764 {
4765 Jim_WrongNumArgs(interp, 1, argv, "<no parameters>");
4766 return JIM_ERR;
4767 }
4768 unsigned count = 0;
4769 struct target *target = all_targets;
4770 while (NULL != target)
4771 {
4772 target = target->next;
4773 count++;
4774 }
4775 Jim_SetResult(interp, Jim_NewIntObj(interp, count));
4776 return JIM_OK;
4777 }
4778
4779 static const struct command_registration target_subcommand_handlers[] = {
4780 {
4781 .name = "init",
4782 .mode = COMMAND_CONFIG,
4783 .handler = handle_target_init_command,
4784 .help = "initialize targets",
4785 },
4786 {
4787 .name = "create",
4788 /* REVISIT this should be COMMAND_CONFIG ... */
4789 .mode = COMMAND_ANY,
4790 .jim_handler = jim_target_create,
4791 .usage = "name type '-chain-position' name [options ...]",
4792 .help = "Creates and selects a new target",
4793 },
4794 {
4795 .name = "current",
4796 .mode = COMMAND_ANY,
4797 .jim_handler = jim_target_current,
4798 .help = "Returns the currently selected target",
4799 },
4800 {
4801 .name = "types",
4802 .mode = COMMAND_ANY,
4803 .jim_handler = jim_target_types,
4804 .help = "Returns the available target types as "
4805 "a list of strings",
4806 },
4807 {
4808 .name = "names",
4809 .mode = COMMAND_ANY,
4810 .jim_handler = jim_target_names,
4811 .help = "Returns the names of all targets as a list of strings",
4812 },
4813 {
4814 .name = "number",
4815 .mode = COMMAND_ANY,
4816 .jim_handler = jim_target_number,
4817 .usage = "number",
4818 .help = "Returns the name of the numbered target "
4819 "(DEPRECATED)",
4820 },
4821 {
4822 .name = "count",
4823 .mode = COMMAND_ANY,
4824 .jim_handler = jim_target_count,
4825 .help = "Returns the number of targets as an integer "
4826 "(DEPRECATED)",
4827 },
4828 COMMAND_REGISTRATION_DONE
4829 };
4830
4831 struct FastLoad
4832 {
4833 uint32_t address;
4834 uint8_t *data;
4835 int length;
4836
4837 };
4838
4839 static int fastload_num;
4840 static struct FastLoad *fastload;
4841
4842 static void free_fastload(void)
4843 {
4844 if (fastload != NULL)
4845 {
4846 int i;
4847 for (i = 0; i < fastload_num; i++)
4848 {
4849 if (fastload[i].data)
4850 free(fastload[i].data);
4851 }
4852 free(fastload);
4853 fastload = NULL;
4854 }
4855 }
4856
4857
4858
4859
4860 COMMAND_HANDLER(handle_fast_load_image_command)
4861 {
4862 uint8_t *buffer;
4863 size_t buf_cnt;
4864 uint32_t image_size;
4865 uint32_t min_address = 0;
4866 uint32_t max_address = 0xffffffff;
4867 int i;
4868
4869 struct image image;
4870
4871 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
4872 &image, &min_address, &max_address);
4873 if (ERROR_OK != retval)
4874 return retval;
4875
4876 struct duration bench;
4877 duration_start(&bench);
4878
4879 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
4880 {
4881 return ERROR_OK;
4882 }
4883
4884 image_size = 0x0;
4885 retval = ERROR_OK;
4886 fastload_num = image.num_sections;
4887 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4888 if (fastload == NULL)
4889 {
4890 image_close(&image);
4891 return ERROR_FAIL;
4892 }
4893 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4894 for (i = 0; i < image.num_sections; i++)
4895 {
4896 buffer = malloc(image.sections[i].size);
4897 if (buffer == NULL)
4898 {
4899 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
4900 (int)(image.sections[i].size));
4901 break;
4902 }
4903
4904 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4905 {
4906 free(buffer);
4907 break;
4908 }
4909
4910 uint32_t offset = 0;
4911 uint32_t length = buf_cnt;
4912
4913
4914 /* DANGER!!! beware of unsigned comparision here!!! */
4915
4916 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4917 (image.sections[i].base_address < max_address))
4918 {
4919 if (image.sections[i].base_address < min_address)
4920 {
4921 /* clip addresses below */
4922 offset += min_address-image.sections[i].base_address;
4923 length -= offset;
4924 }
4925
4926 if (image.sections[i].base_address + buf_cnt > max_address)
4927 {
4928 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4929 }
4930
4931 fastload[i].address = image.sections[i].base_address + offset;
4932 fastload[i].data = malloc(length);
4933 if (fastload[i].data == NULL)
4934 {
4935 free(buffer);
4936 break;
4937 }
4938 memcpy(fastload[i].data, buffer + offset, length);
4939 fastload[i].length = length;
4940
4941 image_size += length;
4942 command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
4943 (unsigned int)length,
4944 ((unsigned int)(image.sections[i].base_address + offset)));
4945 }
4946
4947 free(buffer);
4948 }
4949
4950 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
4951 {
4952 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
4953 "in %fs (%0.3f kb/s)", image_size,
4954 duration_elapsed(&bench), duration_kbps(&bench, image_size));
4955
4956 command_print(CMD_CTX,
4957 "WARNING: image has not been loaded to target!"
4958 "You can issue a 'fast_load' to finish loading.");
4959 }
4960
4961 image_close(&image);
4962
4963 if (retval != ERROR_OK)
4964 {
4965 free_fastload();
4966 }
4967
4968 return retval;
4969 }
4970
4971 COMMAND_HANDLER(handle_fast_load_command)
4972 {
4973 if (CMD_ARGC > 0)
4974 return ERROR_COMMAND_SYNTAX_ERROR;
4975 if (fastload == NULL)
4976 {
4977 LOG_ERROR("No image in memory");
4978 return ERROR_FAIL;
4979 }
4980 int i;
4981 int ms = timeval_ms();
4982 int size = 0;
4983 int retval = ERROR_OK;
4984 for (i = 0; i < fastload_num;i++)
4985 {
4986 struct target *target = get_current_target(CMD_CTX);
4987 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
4988 (unsigned int)(fastload[i].address),
4989 (unsigned int)(fastload[i].length));
4990 if (retval == ERROR_OK)
4991 {
4992 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4993 }
4994 size += fastload[i].length;
4995 }
4996 int after = timeval_ms();
4997 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
4998 return retval;
4999 }
5000
5001 static const struct command_registration target_command_handlers[] = {
5002 {
5003 .name = "targets",
5004 .handler = handle_targets_command,
5005 .mode = COMMAND_ANY,
5006 .help = "change current default target (one parameter) "
5007 "or prints table of all targets (no parameters)",
5008 .usage = "[target]",
5009 },
5010 {
5011 .name = "target",
5012 .mode = COMMAND_CONFIG,
5013 .help = "configure target",
5014
5015 .chain = target_subcommand_handlers,
5016 },
5017 COMMAND_REGISTRATION_DONE
5018 };
5019
5020 int target_register_commands(struct command_context *cmd_ctx)
5021 {
5022 return register_commands(cmd_ctx, NULL, target_command_handlers);
5023 }
5024
5025 static bool target_reset_nag = true;
5026
5027 bool get_target_reset_nag(void)
5028 {
5029 return target_reset_nag;
5030 }
5031
5032 COMMAND_HANDLER(handle_target_reset_nag)
5033 {
5034 return CALL_COMMAND_HANDLER(handle_command_parse_bool,
5035 &target_reset_nag, "Nag after each reset about options to improve "
5036 "performance");
5037 }
5038
5039 static const struct command_registration target_exec_command_handlers[] = {
5040 {
5041 .name = "fast_load_image",
5042 .handler = handle_fast_load_image_command,
5043 .mode = COMMAND_ANY,
5044 .help = "Load image into server memory for later use by "
5045 "fast_load; primarily for profiling",
5046 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
5047 "[min_address [max_length]]",
5048 },
5049 {
5050 .name = "fast_load",
5051 .handler = handle_fast_load_command,
5052 .mode = COMMAND_EXEC,
5053 .help = "loads active fast load image to current target "
5054 "- mainly for profiling purposes",
5055 },
5056 {
5057 .name = "profile",
5058 .handler = handle_profile_command,
5059 .mode = COMMAND_EXEC,
5060 .help = "profiling samples the CPU PC",
5061 },
5062 /** @todo don't register virt2phys() unless target supports it */
5063 {
5064 .name = "virt2phys",
5065 .handler = handle_virt2phys_command,
5066 .mode = COMMAND_ANY,
5067 .help = "translate a virtual address into a physical address",
5068 .usage = "virtual_address",
5069 },
5070 {
5071 .name = "reg",
5072 .handler = handle_reg_command,
5073 .mode = COMMAND_EXEC,
5074 .help = "display or set a register; with no arguments, "
5075 "displays all registers and their values",
5076 .usage = "[(register_name|register_number) [value]]",
5077 },
5078 {
5079 .name = "poll",
5080 .handler = handle_poll_command,
5081 .mode = COMMAND_EXEC,
5082 .help = "poll target state; or reconfigure background polling",
5083 .usage = "['on'|'off']",
5084 },
5085 {
5086 .name = "wait_halt",
5087 .handler = handle_wait_halt_command,
5088 .mode = COMMAND_EXEC,
5089 .help = "wait up to the specified number of milliseconds "
5090 "(default 5) for a previously requested halt",
5091 .usage = "[milliseconds]",
5092 },
5093 {
5094 .name = "halt",
5095 .handler = handle_halt_command,
5096 .mode = COMMAND_EXEC,
5097 .help = "request target to halt, then wait up to the specified"
5098 "number of milliseconds (default 5) for it to complete",
5099 .usage = "[milliseconds]",
5100 },
5101 {
5102 .name = "resume",
5103 .handler = handle_resume_command,
5104 .mode = COMMAND_EXEC,
5105 .help = "resume target execution from current PC or address",
5106 .usage = "[address]",
5107 },
5108 {
5109 .name = "reset",
5110 .handler = handle_reset_command,
5111 .mode = COMMAND_EXEC,
5112 .usage = "[run|halt|init]",
5113 .help = "Reset all targets into the specified mode."
5114 "Default reset mode is run, if not given.",
5115 },
5116 {
5117 .name = "soft_reset_halt",
5118 .handler = handle_soft_reset_halt_command,
5119 .mode = COMMAND_EXEC,
5120 .help = "halt the target and do a soft reset",
5121 },
5122 {
5123 .name = "step",
5124 .handler = handle_step_command,
5125 .mode = COMMAND_EXEC,
5126 .help = "step one instruction from current PC or address",
5127 .usage = "[address]",
5128 },
5129 {
5130 .name = "mdw",
5131 .handler = handle_md_command,
5132 .mode = COMMAND_EXEC,
5133 .help = "display memory words",
5134 .usage = "['phys'] address [count]",
5135 },
5136 {
5137 .name = "mdh",
5138 .handler = handle_md_command,
5139 .mode = COMMAND_EXEC,
5140 .help = "display memory half-words",
5141 .usage = "['phys'] address [count]",
5142 },
5143 {
5144 .name = "mdb",
5145 .handler = handle_md_command,
5146 .mode = COMMAND_EXEC,
5147 .help = "display memory bytes",
5148 .usage = "['phys'] address [count]",
5149 },
5150 {
5151 .name = "mww",
5152 .handler = handle_mw_command,
5153 .mode = COMMAND_EXEC,
5154 .help = "write memory word",
5155 .usage = "['phys'] address value [count]",
5156 },
5157 {
5158 .name = "mwh",
5159 .handler = handle_mw_command,
5160 .mode = COMMAND_EXEC,
5161 .help = "write memory half-word",
5162 .usage = "['phys'] address value [count]",
5163 },
5164 {
5165 .name = "mwb",
5166 .handler = handle_mw_command,
5167 .mode = COMMAND_EXEC,
5168 .help = "write memory byte",
5169 .usage = "['phys'] address value [count]",
5170 },
5171 {
5172 .name = "bp",
5173 .handler = handle_bp_command,
5174 .mode = COMMAND_EXEC,
5175 .help = "list or set hardware or software breakpoint",
5176 .usage = "[address length ['hw']]",
5177 },
5178 {
5179 .name = "rbp",
5180 .handler = handle_rbp_command,
5181 .mode = COMMAND_EXEC,
5182 .help = "remove breakpoint",
5183 .usage = "address",
5184 },
5185 {
5186 .name = "wp",
5187 .handler = handle_wp_command,
5188 .mode = COMMAND_EXEC,
5189 .help = "list (no params) or create watchpoints",
5190 .usage = "[address length [('r'|'w'|'a') value [mask]]]",
5191 },
5192 {
5193 .name = "rwp",
5194 .handler = handle_rwp_command,
5195 .mode = COMMAND_EXEC,
5196 .help = "remove watchpoint",
5197 .usage = "address",
5198 },
5199 {
5200 .name = "load_image",
5201 .handler = handle_load_image_command,
5202 .mode = COMMAND_EXEC,
5203 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
5204 "[min_address] [max_length]",
5205 },
5206 {
5207 .name = "dump_image",
5208 .handler = handle_dump_image_command,
5209 .mode = COMMAND_EXEC,
5210 .usage = "filename address size",
5211 },
5212 {
5213 .name = "verify_image",
5214 .handler = handle_verify_image_command,
5215 .mode = COMMAND_EXEC,
5216 .usage = "filename [offset [type]]",
5217 },
5218 {
5219 .name = "test_image",
5220 .handler = handle_test_image_command,
5221 .mode = COMMAND_EXEC,
5222 .usage = "filename [offset [type]]",
5223 },
5224 {
5225 .name = "ocd_mem2array",
5226 .mode = COMMAND_EXEC,
5227 .jim_handler = jim_mem2array,
5228 .help = "read 8/16/32 bit memory and return as a TCL array "
5229 "for script processing",
5230 .usage = "arrayname bitwidth address count",
5231 },
5232 {
5233 .name = "ocd_array2mem",
5234 .mode = COMMAND_EXEC,
5235 .jim_handler = jim_array2mem,
5236 .help = "convert a TCL array to memory locations "
5237 "and write the 8/16/32 bit values",
5238 .usage = "arrayname bitwidth address count",
5239 },
5240 {
5241 .name = "reset_nag",
5242 .handler = handle_target_reset_nag,
5243 .mode = COMMAND_ANY,
5244 .help = "Nag after each reset about options that could have been "
5245 "enabled to improve performance. ",
5246 .usage = "['enable'|'disable']",
5247 },
5248 COMMAND_REGISTRATION_DONE
5249 };
5250 static int target_register_user_commands(struct command_context *cmd_ctx)
5251 {
5252 int retval = ERROR_OK;
5253 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
5254 return retval;
5255
5256 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
5257 return retval;
5258
5259
5260 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);
5261 }

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)