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

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)