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

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)