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

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)