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

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)