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

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)