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

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)