70130d9127d68fe59c7f3267066d410ea5501a22
[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 static 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 static 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 static 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 static 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 static 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 /* "can't happen", caller checked */
2158 LOG_ERROR("invalid memory read size: %u", size);
2159 return;
2160 }
2161
2162 for (unsigned i = 0; i < count; i++)
2163 {
2164 if (i % line_modulo == 0)
2165 {
2166 output_len += snprintf(output + output_len,
2167 sizeof(output) - output_len,
2168 "0x%8.8x: ",
2169 (unsigned)(address + (i*size)));
2170 }
2171
2172 uint32_t value = 0;
2173 const uint8_t *value_ptr = buffer + i * size;
2174 switch (size) {
2175 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2176 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2177 case 1: value = *value_ptr;
2178 }
2179 output_len += snprintf(output + output_len,
2180 sizeof(output) - output_len,
2181 value_fmt, value);
2182
2183 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2184 {
2185 command_print(cmd_ctx, "%s", output);
2186 output_len = 0;
2187 }
2188 }
2189 }
2190
2191 COMMAND_HANDLER(handle_md_command)
2192 {
2193 if (CMD_ARGC < 1)
2194 return ERROR_COMMAND_SYNTAX_ERROR;
2195
2196 unsigned size = 0;
2197 switch (CMD_NAME[2]) {
2198 case 'w': size = 4; break;
2199 case 'h': size = 2; break;
2200 case 'b': size = 1; break;
2201 default: return ERROR_COMMAND_SYNTAX_ERROR;
2202 }
2203
2204 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2205 int (*fn)(struct target *target,
2206 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2207 if (physical)
2208 {
2209 CMD_ARGC--;
2210 CMD_ARGV++;
2211 fn=target_read_phys_memory;
2212 } else
2213 {
2214 fn=target_read_memory;
2215 }
2216 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
2217 {
2218 return ERROR_COMMAND_SYNTAX_ERROR;
2219 }
2220
2221 uint32_t address;
2222 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2223
2224 unsigned count = 1;
2225 if (CMD_ARGC == 2)
2226 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
2227
2228 uint8_t *buffer = calloc(count, size);
2229
2230 struct target *target = get_current_target(CMD_CTX);
2231 int retval = fn(target, address, size, count, buffer);
2232 if (ERROR_OK == retval)
2233 handle_md_output(CMD_CTX, target, address, size, count, buffer);
2234
2235 free(buffer);
2236
2237 return retval;
2238 }
2239
2240 COMMAND_HANDLER(handle_mw_command)
2241 {
2242 if (CMD_ARGC < 2)
2243 {
2244 return ERROR_COMMAND_SYNTAX_ERROR;
2245 }
2246 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2247 int (*fn)(struct target *target,
2248 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2249 if (physical)
2250 {
2251 CMD_ARGC--;
2252 CMD_ARGV++;
2253 fn=target_write_phys_memory;
2254 } else
2255 {
2256 fn=target_write_memory;
2257 }
2258 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
2259 return ERROR_COMMAND_SYNTAX_ERROR;
2260
2261 uint32_t address;
2262 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2263
2264 uint32_t value;
2265 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2266
2267 unsigned count = 1;
2268 if (CMD_ARGC == 3)
2269 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
2270
2271 struct target *target = get_current_target(CMD_CTX);
2272 unsigned wordsize;
2273 uint8_t value_buf[4];
2274 switch (CMD_NAME[2])
2275 {
2276 case 'w':
2277 wordsize = 4;
2278 target_buffer_set_u32(target, value_buf, value);
2279 break;
2280 case 'h':
2281 wordsize = 2;
2282 target_buffer_set_u16(target, value_buf, value);
2283 break;
2284 case 'b':
2285 wordsize = 1;
2286 value_buf[0] = value;
2287 break;
2288 default:
2289 return ERROR_COMMAND_SYNTAX_ERROR;
2290 }
2291 for (unsigned i = 0; i < count; i++)
2292 {
2293 int retval = fn(target,
2294 address + i * wordsize, wordsize, 1, value_buf);
2295 if (ERROR_OK != retval)
2296 return retval;
2297 keep_alive();
2298 }
2299
2300 return ERROR_OK;
2301
2302 }
2303
2304 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
2305 uint32_t *min_address, uint32_t *max_address)
2306 {
2307 if (CMD_ARGC < 1 || CMD_ARGC > 5)
2308 return ERROR_COMMAND_SYNTAX_ERROR;
2309
2310 /* a base address isn't always necessary,
2311 * default to 0x0 (i.e. don't relocate) */
2312 if (CMD_ARGC >= 2)
2313 {
2314 uint32_t addr;
2315 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2316 image->base_address = addr;
2317 image->base_address_set = 1;
2318 }
2319 else
2320 image->base_address_set = 0;
2321
2322 image->start_address_set = 0;
2323
2324 if (CMD_ARGC >= 4)
2325 {
2326 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], *min_address);
2327 }
2328 if (CMD_ARGC == 5)
2329 {
2330 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], *max_address);
2331 // use size (given) to find max (required)
2332 *max_address += *min_address;
2333 }
2334
2335 if (*min_address > *max_address)
2336 return ERROR_COMMAND_SYNTAX_ERROR;
2337
2338 return ERROR_OK;
2339 }
2340
2341 COMMAND_HANDLER(handle_load_image_command)
2342 {
2343 uint8_t *buffer;
2344 size_t buf_cnt;
2345 uint32_t image_size;
2346 uint32_t min_address = 0;
2347 uint32_t max_address = 0xffffffff;
2348 int i;
2349 struct image image;
2350
2351 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
2352 &image, &min_address, &max_address);
2353 if (ERROR_OK != retval)
2354 return retval;
2355
2356 struct target *target = get_current_target(CMD_CTX);
2357
2358 struct duration bench;
2359 duration_start(&bench);
2360
2361 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
2362 {
2363 return ERROR_OK;
2364 }
2365
2366 image_size = 0x0;
2367 retval = ERROR_OK;
2368 for (i = 0; i < image.num_sections; i++)
2369 {
2370 buffer = malloc(image.sections[i].size);
2371 if (buffer == NULL)
2372 {
2373 command_print(CMD_CTX,
2374 "error allocating buffer for section (%d bytes)",
2375 (int)(image.sections[i].size));
2376 break;
2377 }
2378
2379 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2380 {
2381 free(buffer);
2382 break;
2383 }
2384
2385 uint32_t offset = 0;
2386 uint32_t length = buf_cnt;
2387
2388 /* DANGER!!! beware of unsigned comparision here!!! */
2389
2390 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2391 (image.sections[i].base_address < max_address))
2392 {
2393 if (image.sections[i].base_address < min_address)
2394 {
2395 /* clip addresses below */
2396 offset += min_address-image.sections[i].base_address;
2397 length -= offset;
2398 }
2399
2400 if (image.sections[i].base_address + buf_cnt > max_address)
2401 {
2402 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2403 }
2404
2405 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2406 {
2407 free(buffer);
2408 break;
2409 }
2410 image_size += length;
2411 command_print(CMD_CTX, "%u bytes written at address 0x%8.8" PRIx32 "",
2412 (unsigned int)length,
2413 image.sections[i].base_address + offset);
2414 }
2415
2416 free(buffer);
2417 }
2418
2419 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2420 {
2421 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
2422 "in %fs (%0.3f kb/s)", image_size,
2423 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2424 }
2425
2426 image_close(&image);
2427
2428 return retval;
2429
2430 }
2431
2432 COMMAND_HANDLER(handle_dump_image_command)
2433 {
2434 struct fileio fileio;
2435
2436 uint8_t buffer[560];
2437 int retvaltemp;
2438
2439
2440 struct target *target = get_current_target(CMD_CTX);
2441
2442 if (CMD_ARGC != 3)
2443 {
2444 command_print(CMD_CTX, "usage: dump_image <filename> <address> <size>");
2445 return ERROR_OK;
2446 }
2447
2448 uint32_t address;
2449 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], address);
2450 uint32_t size;
2451 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], size);
2452
2453 if (fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2454 {
2455 return ERROR_OK;
2456 }
2457
2458 struct duration bench;
2459 duration_start(&bench);
2460
2461 int retval = ERROR_OK;
2462 while (size > 0)
2463 {
2464 size_t size_written;
2465 uint32_t this_run_size = (size > 560) ? 560 : size;
2466 retval = target_read_buffer(target, address, this_run_size, buffer);
2467 if (retval != ERROR_OK)
2468 {
2469 break;
2470 }
2471
2472 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2473 if (retval != ERROR_OK)
2474 {
2475 break;
2476 }
2477
2478 size -= this_run_size;
2479 address += this_run_size;
2480 }
2481
2482 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2483 return retvaltemp;
2484
2485 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2486 {
2487 command_print(CMD_CTX,
2488 "dumped %zu bytes in %fs (%0.3f kb/s)", fileio.size,
2489 duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
2490 }
2491
2492 return retval;
2493 }
2494
2495 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
2496 {
2497 uint8_t *buffer;
2498 size_t buf_cnt;
2499 uint32_t image_size;
2500 int i;
2501 int retval;
2502 uint32_t checksum = 0;
2503 uint32_t mem_checksum = 0;
2504
2505 struct image image;
2506
2507 struct target *target = get_current_target(CMD_CTX);
2508
2509 if (CMD_ARGC < 1)
2510 {
2511 return ERROR_COMMAND_SYNTAX_ERROR;
2512 }
2513
2514 if (!target)
2515 {
2516 LOG_ERROR("no target selected");
2517 return ERROR_FAIL;
2518 }
2519
2520 struct duration bench;
2521 duration_start(&bench);
2522
2523 if (CMD_ARGC >= 2)
2524 {
2525 uint32_t addr;
2526 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2527 image.base_address = addr;
2528 image.base_address_set = 1;
2529 }
2530 else
2531 {
2532 image.base_address_set = 0;
2533 image.base_address = 0x0;
2534 }
2535
2536 image.start_address_set = 0;
2537
2538 if ((retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL)) != ERROR_OK)
2539 {
2540 return retval;
2541 }
2542
2543 image_size = 0x0;
2544 retval = ERROR_OK;
2545 for (i = 0; i < image.num_sections; i++)
2546 {
2547 buffer = malloc(image.sections[i].size);
2548 if (buffer == NULL)
2549 {
2550 command_print(CMD_CTX,
2551 "error allocating buffer for section (%d bytes)",
2552 (int)(image.sections[i].size));
2553 break;
2554 }
2555 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2556 {
2557 free(buffer);
2558 break;
2559 }
2560
2561 if (verify)
2562 {
2563 /* calculate checksum of image */
2564 image_calculate_checksum(buffer, buf_cnt, &checksum);
2565
2566 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2567 if (retval != ERROR_OK)
2568 {
2569 free(buffer);
2570 break;
2571 }
2572
2573 if (checksum != mem_checksum)
2574 {
2575 /* failed crc checksum, fall back to a binary compare */
2576 uint8_t *data;
2577
2578 command_print(CMD_CTX, "checksum mismatch - attempting binary compare");
2579
2580 data = (uint8_t*)malloc(buf_cnt);
2581
2582 /* Can we use 32bit word accesses? */
2583 int size = 1;
2584 int count = buf_cnt;
2585 if ((count % 4) == 0)
2586 {
2587 size *= 4;
2588 count /= 4;
2589 }
2590 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2591 if (retval == ERROR_OK)
2592 {
2593 uint32_t t;
2594 for (t = 0; t < buf_cnt; t++)
2595 {
2596 if (data[t] != buffer[t])
2597 {
2598 command_print(CMD_CTX,
2599 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2600 (unsigned)(t + image.sections[i].base_address),
2601 data[t],
2602 buffer[t]);
2603 free(data);
2604 free(buffer);
2605 retval = ERROR_FAIL;
2606 goto done;
2607 }
2608 if ((t%16384) == 0)
2609 {
2610 keep_alive();
2611 }
2612 }
2613 }
2614
2615 free(data);
2616 }
2617 } else
2618 {
2619 command_print(CMD_CTX, "address 0x%08" PRIx32 " length 0x%08zx",
2620 image.sections[i].base_address,
2621 buf_cnt);
2622 }
2623
2624 free(buffer);
2625 image_size += buf_cnt;
2626 }
2627 done:
2628 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2629 {
2630 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
2631 "in %fs (%0.3f kb/s)", image_size,
2632 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2633 }
2634
2635 image_close(&image);
2636
2637 return retval;
2638 }
2639
2640 COMMAND_HANDLER(handle_verify_image_command)
2641 {
2642 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 1);
2643 }
2644
2645 COMMAND_HANDLER(handle_test_image_command)
2646 {
2647 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 0);
2648 }
2649
2650 static int handle_bp_command_list(struct command_context *cmd_ctx)
2651 {
2652 struct target *target = get_current_target(cmd_ctx);
2653 struct breakpoint *breakpoint = target->breakpoints;
2654 while (breakpoint)
2655 {
2656 if (breakpoint->type == BKPT_SOFT)
2657 {
2658 char* buf = buf_to_str(breakpoint->orig_instr,
2659 breakpoint->length, 16);
2660 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2661 breakpoint->address,
2662 breakpoint->length,
2663 breakpoint->set, buf);
2664 free(buf);
2665 }
2666 else
2667 {
2668 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2669 breakpoint->address,
2670 breakpoint->length, breakpoint->set);
2671 }
2672
2673 breakpoint = breakpoint->next;
2674 }
2675 return ERROR_OK;
2676 }
2677
2678 static int handle_bp_command_set(struct command_context *cmd_ctx,
2679 uint32_t addr, uint32_t length, int hw)
2680 {
2681 struct target *target = get_current_target(cmd_ctx);
2682 int retval = breakpoint_add(target, addr, length, hw);
2683 if (ERROR_OK == retval)
2684 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2685 else
2686 LOG_ERROR("Failure setting breakpoint");
2687 return retval;
2688 }
2689
2690 COMMAND_HANDLER(handle_bp_command)
2691 {
2692 if (CMD_ARGC == 0)
2693 return handle_bp_command_list(CMD_CTX);
2694
2695 if (CMD_ARGC < 2 || CMD_ARGC > 3)
2696 {
2697 command_print(CMD_CTX, "usage: bp <address> <length> ['hw']");
2698 return ERROR_COMMAND_SYNTAX_ERROR;
2699 }
2700
2701 uint32_t addr;
2702 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2703 uint32_t length;
2704 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2705
2706 int hw = BKPT_SOFT;
2707 if (CMD_ARGC == 3)
2708 {
2709 if (strcmp(CMD_ARGV[2], "hw") == 0)
2710 hw = BKPT_HARD;
2711 else
2712 return ERROR_COMMAND_SYNTAX_ERROR;
2713 }
2714
2715 return handle_bp_command_set(CMD_CTX, addr, length, hw);
2716 }
2717
2718 COMMAND_HANDLER(handle_rbp_command)
2719 {
2720 if (CMD_ARGC != 1)
2721 return ERROR_COMMAND_SYNTAX_ERROR;
2722
2723 uint32_t addr;
2724 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2725
2726 struct target *target = get_current_target(CMD_CTX);
2727 breakpoint_remove(target, addr);
2728
2729 return ERROR_OK;
2730 }
2731
2732 COMMAND_HANDLER(handle_wp_command)
2733 {
2734 struct target *target = get_current_target(CMD_CTX);
2735
2736 if (CMD_ARGC == 0)
2737 {
2738 struct watchpoint *watchpoint = target->watchpoints;
2739
2740 while (watchpoint)
2741 {
2742 command_print(CMD_CTX, "address: 0x%8.8" PRIx32
2743 ", len: 0x%8.8" PRIx32
2744 ", r/w/a: %i, value: 0x%8.8" PRIx32
2745 ", mask: 0x%8.8" PRIx32,
2746 watchpoint->address,
2747 watchpoint->length,
2748 (int)watchpoint->rw,
2749 watchpoint->value,
2750 watchpoint->mask);
2751 watchpoint = watchpoint->next;
2752 }
2753 return ERROR_OK;
2754 }
2755
2756 enum watchpoint_rw type = WPT_ACCESS;
2757 uint32_t addr = 0;
2758 uint32_t length = 0;
2759 uint32_t data_value = 0x0;
2760 uint32_t data_mask = 0xffffffff;
2761
2762 switch (CMD_ARGC)
2763 {
2764 case 5:
2765 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
2766 // fall through
2767 case 4:
2768 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
2769 // fall through
2770 case 3:
2771 switch (CMD_ARGV[2][0])
2772 {
2773 case 'r':
2774 type = WPT_READ;
2775 break;
2776 case 'w':
2777 type = WPT_WRITE;
2778 break;
2779 case 'a':
2780 type = WPT_ACCESS;
2781 break;
2782 default:
2783 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
2784 return ERROR_COMMAND_SYNTAX_ERROR;
2785 }
2786 // fall through
2787 case 2:
2788 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2789 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2790 break;
2791
2792 default:
2793 command_print(CMD_CTX, "usage: wp [address length "
2794 "[(r|w|a) [value [mask]]]]");
2795 return ERROR_COMMAND_SYNTAX_ERROR;
2796 }
2797
2798 int retval = watchpoint_add(target, addr, length, type,
2799 data_value, data_mask);
2800 if (ERROR_OK != retval)
2801 LOG_ERROR("Failure setting watchpoints");
2802
2803 return retval;
2804 }
2805
2806 COMMAND_HANDLER(handle_rwp_command)
2807 {
2808 if (CMD_ARGC != 1)
2809 return ERROR_COMMAND_SYNTAX_ERROR;
2810
2811 uint32_t addr;
2812 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2813
2814 struct target *target = get_current_target(CMD_CTX);
2815 watchpoint_remove(target, addr);
2816
2817 return ERROR_OK;
2818 }
2819
2820
2821 /**
2822 * Translate a virtual address to a physical address.
2823 *
2824 * The low-level target implementation must have logged a detailed error
2825 * which is forwarded to telnet/GDB session.
2826 */
2827 COMMAND_HANDLER(handle_virt2phys_command)
2828 {
2829 if (CMD_ARGC != 1)
2830 return ERROR_COMMAND_SYNTAX_ERROR;
2831
2832 uint32_t va;
2833 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], va);
2834 uint32_t pa;
2835
2836 struct target *target = get_current_target(CMD_CTX);
2837 int retval = target->type->virt2phys(target, va, &pa);
2838 if (retval == ERROR_OK)
2839 command_print(CMD_CTX, "Physical address 0x%08" PRIx32 "", pa);
2840
2841 return retval;
2842 }
2843
2844 static void writeData(FILE *f, const void *data, size_t len)
2845 {
2846 size_t written = fwrite(data, 1, len, f);
2847 if (written != len)
2848 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
2849 }
2850
2851 static void writeLong(FILE *f, int l)
2852 {
2853 int i;
2854 for (i = 0; i < 4; i++)
2855 {
2856 char c = (l >> (i*8))&0xff;
2857 writeData(f, &c, 1);
2858 }
2859
2860 }
2861
2862 static void writeString(FILE *f, char *s)
2863 {
2864 writeData(f, s, strlen(s));
2865 }
2866
2867 /* Dump a gmon.out histogram file. */
2868 static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filename)
2869 {
2870 uint32_t i;
2871 FILE *f = fopen(filename, "w");
2872 if (f == NULL)
2873 return;
2874 writeString(f, "gmon");
2875 writeLong(f, 0x00000001); /* Version */
2876 writeLong(f, 0); /* padding */
2877 writeLong(f, 0); /* padding */
2878 writeLong(f, 0); /* padding */
2879
2880 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
2881 writeData(f, &zero, 1);
2882
2883 /* figure out bucket size */
2884 uint32_t min = samples[0];
2885 uint32_t max = samples[0];
2886 for (i = 0; i < sampleNum; i++)
2887 {
2888 if (min > samples[i])
2889 {
2890 min = samples[i];
2891 }
2892 if (max < samples[i])
2893 {
2894 max = samples[i];
2895 }
2896 }
2897
2898 int addressSpace = (max-min + 1);
2899
2900 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
2901 uint32_t length = addressSpace;
2902 if (length > maxBuckets)
2903 {
2904 length = maxBuckets;
2905 }
2906 int *buckets = malloc(sizeof(int)*length);
2907 if (buckets == NULL)
2908 {
2909 fclose(f);
2910 return;
2911 }
2912 memset(buckets, 0, sizeof(int)*length);
2913 for (i = 0; i < sampleNum;i++)
2914 {
2915 uint32_t address = samples[i];
2916 long long a = address-min;
2917 long long b = length-1;
2918 long long c = addressSpace-1;
2919 int index = (a*b)/c; /* danger!!!! int32 overflows */
2920 buckets[index]++;
2921 }
2922
2923 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2924 writeLong(f, min); /* low_pc */
2925 writeLong(f, max); /* high_pc */
2926 writeLong(f, length); /* # of samples */
2927 writeLong(f, 64000000); /* 64MHz */
2928 writeString(f, "seconds");
2929 for (i = 0; i < (15-strlen("seconds")); i++)
2930 writeData(f, &zero, 1);
2931 writeString(f, "s");
2932
2933 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2934
2935 char *data = malloc(2*length);
2936 if (data != NULL)
2937 {
2938 for (i = 0; i < length;i++)
2939 {
2940 int val;
2941 val = buckets[i];
2942 if (val > 65535)
2943 {
2944 val = 65535;
2945 }
2946 data[i*2]=val&0xff;
2947 data[i*2 + 1]=(val >> 8)&0xff;
2948 }
2949 free(buckets);
2950 writeData(f, data, length * 2);
2951 free(data);
2952 } else
2953 {
2954 free(buckets);
2955 }
2956
2957 fclose(f);
2958 }
2959
2960 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2961 COMMAND_HANDLER(handle_profile_command)
2962 {
2963 struct target *target = get_current_target(CMD_CTX);
2964 struct timeval timeout, now;
2965
2966 gettimeofday(&timeout, NULL);
2967 if (CMD_ARGC != 2)
2968 {
2969 return ERROR_COMMAND_SYNTAX_ERROR;
2970 }
2971 unsigned offset;
2972 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], offset);
2973
2974 timeval_add_time(&timeout, offset, 0);
2975
2976 command_print(CMD_CTX, "Starting profiling. Halting and resuming the target as often as we can...");
2977
2978 static const int maxSample = 10000;
2979 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
2980 if (samples == NULL)
2981 return ERROR_OK;
2982
2983 int numSamples = 0;
2984 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2985 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
2986
2987 for (;;)
2988 {
2989 int retval;
2990 target_poll(target);
2991 if (target->state == TARGET_HALTED)
2992 {
2993 uint32_t t=*((uint32_t *)reg->value);
2994 samples[numSamples++]=t;
2995 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2996 target_poll(target);
2997 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2998 } else if (target->state == TARGET_RUNNING)
2999 {
3000 /* We want to quickly sample the PC. */
3001 if ((retval = target_halt(target)) != ERROR_OK)
3002 {
3003 free(samples);
3004 return retval;
3005 }
3006 } else
3007 {
3008 command_print(CMD_CTX, "Target not halted or running");
3009 retval = ERROR_OK;
3010 break;
3011 }
3012 if (retval != ERROR_OK)
3013 {
3014 break;
3015 }
3016
3017 gettimeofday(&now, NULL);
3018 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3019 {
3020 command_print(CMD_CTX, "Profiling completed. %d samples.", numSamples);
3021 if ((retval = target_poll(target)) != ERROR_OK)
3022 {
3023 free(samples);
3024 return retval;
3025 }
3026 if (target->state == TARGET_HALTED)
3027 {
3028 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3029 }
3030 if ((retval = target_poll(target)) != ERROR_OK)
3031 {
3032 free(samples);
3033 return retval;
3034 }
3035 writeGmon(samples, numSamples, CMD_ARGV[1]);
3036 command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
3037 break;
3038 }
3039 }
3040 free(samples);
3041
3042 return ERROR_OK;
3043 }
3044
3045 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3046 {
3047 char *namebuf;
3048 Jim_Obj *nameObjPtr, *valObjPtr;
3049 int result;
3050
3051 namebuf = alloc_printf("%s(%d)", varname, idx);
3052 if (!namebuf)
3053 return JIM_ERR;
3054
3055 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3056 valObjPtr = Jim_NewIntObj(interp, val);
3057 if (!nameObjPtr || !valObjPtr)
3058 {
3059 free(namebuf);
3060 return JIM_ERR;
3061 }
3062
3063 Jim_IncrRefCount(nameObjPtr);
3064 Jim_IncrRefCount(valObjPtr);
3065 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3066 Jim_DecrRefCount(interp, nameObjPtr);
3067 Jim_DecrRefCount(interp, valObjPtr);
3068 free(namebuf);
3069 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3070 return result;
3071 }
3072
3073 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3074 {
3075 struct command_context *context;
3076 struct target *target;
3077
3078 context = Jim_GetAssocData(interp, "context");
3079 if (context == NULL)
3080 {
3081 LOG_ERROR("mem2array: no command context");
3082 return JIM_ERR;
3083 }
3084 target = get_current_target(context);
3085 if (target == NULL)
3086 {
3087 LOG_ERROR("mem2array: no current target");
3088 return JIM_ERR;
3089 }
3090
3091 return target_mem2array(interp, target, argc-1, argv + 1);
3092 }
3093
3094 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3095 {
3096 long l;
3097 uint32_t width;
3098 int len;
3099 uint32_t addr;
3100 uint32_t count;
3101 uint32_t v;
3102 const char *varname;
3103 int n, e, retval;
3104 uint32_t i;
3105
3106 /* argv[1] = name of array to receive the data
3107 * argv[2] = desired width
3108 * argv[3] = memory address
3109 * argv[4] = count of times to read
3110 */
3111 if (argc != 4) {
3112 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3113 return JIM_ERR;
3114 }
3115 varname = Jim_GetString(argv[0], &len);
3116 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3117
3118 e = Jim_GetLong(interp, argv[1], &l);
3119 width = l;
3120 if (e != JIM_OK) {
3121 return e;
3122 }
3123
3124 e = Jim_GetLong(interp, argv[2], &l);
3125 addr = l;
3126 if (e != JIM_OK) {
3127 return e;
3128 }
3129 e = Jim_GetLong(interp, argv[3], &l);
3130 len = l;
3131 if (e != JIM_OK) {
3132 return e;
3133 }
3134 switch (width) {
3135 case 8:
3136 width = 1;
3137 break;
3138 case 16:
3139 width = 2;
3140 break;
3141 case 32:
3142 width = 4;
3143 break;
3144 default:
3145 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3146 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3147 return JIM_ERR;
3148 }
3149 if (len == 0) {
3150 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3151 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3152 return JIM_ERR;
3153 }
3154 if ((addr + (len * width)) < addr) {
3155 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3156 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3157 return JIM_ERR;
3158 }
3159 /* absurd transfer size? */
3160 if (len > 65536) {
3161 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3162 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3163 return JIM_ERR;
3164 }
3165
3166 if ((width == 1) ||
3167 ((width == 2) && ((addr & 1) == 0)) ||
3168 ((width == 4) && ((addr & 3) == 0))) {
3169 /* all is well */
3170 } else {
3171 char buf[100];
3172 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3173 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3174 addr,
3175 width);
3176 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3177 return JIM_ERR;
3178 }
3179
3180 /* Transfer loop */
3181
3182 /* index counter */
3183 n = 0;
3184
3185 size_t buffersize = 4096;
3186 uint8_t *buffer = malloc(buffersize);
3187 if (buffer == NULL)
3188 return JIM_ERR;
3189
3190 /* assume ok */
3191 e = JIM_OK;
3192 while (len) {
3193 /* Slurp... in buffer size chunks */
3194
3195 count = len; /* in objects.. */
3196 if (count > (buffersize/width)) {
3197 count = (buffersize/width);
3198 }
3199
3200 retval = target_read_memory(target, addr, width, count, buffer);
3201 if (retval != ERROR_OK) {
3202 /* BOO !*/
3203 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3204 (unsigned int)addr,
3205 (int)width,
3206 (int)count);
3207 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3208 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3209 e = JIM_ERR;
3210 len = 0;
3211 } else {
3212 v = 0; /* shut up gcc */
3213 for (i = 0 ;i < count ;i++, n++) {
3214 switch (width) {
3215 case 4:
3216 v = target_buffer_get_u32(target, &buffer[i*width]);
3217 break;
3218 case 2:
3219 v = target_buffer_get_u16(target, &buffer[i*width]);
3220 break;
3221 case 1:
3222 v = buffer[i] & 0x0ff;
3223 break;
3224 }
3225 new_int_array_element(interp, varname, n, v);
3226 }
3227 len -= count;
3228 }
3229 }
3230
3231 free(buffer);
3232
3233 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3234
3235 return JIM_OK;
3236 }
3237
3238 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3239 {
3240 char *namebuf;
3241 Jim_Obj *nameObjPtr, *valObjPtr;
3242 int result;
3243 long l;
3244
3245 namebuf = alloc_printf("%s(%d)", varname, idx);
3246 if (!namebuf)
3247 return JIM_ERR;
3248
3249 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3250 if (!nameObjPtr)
3251 {
3252 free(namebuf);
3253 return JIM_ERR;
3254 }
3255
3256 Jim_IncrRefCount(nameObjPtr);
3257 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3258 Jim_DecrRefCount(interp, nameObjPtr);
3259 free(namebuf);
3260 if (valObjPtr == NULL)
3261 return JIM_ERR;
3262
3263 result = Jim_GetLong(interp, valObjPtr, &l);
3264 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3265 *val = l;
3266 return result;
3267 }
3268
3269 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3270 {
3271 struct command_context *context;
3272 struct target *target;
3273
3274 context = Jim_GetAssocData(interp, "context");
3275 if (context == NULL) {
3276 LOG_ERROR("array2mem: no command context");
3277 return JIM_ERR;
3278 }
3279 target = get_current_target(context);
3280 if (target == NULL) {
3281 LOG_ERROR("array2mem: no current target");
3282 return JIM_ERR;
3283 }
3284
3285 return target_array2mem(interp,target, argc-1, argv + 1);
3286 }
3287 static int target_array2mem(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3288 {
3289 long l;
3290 uint32_t width;
3291 int len;
3292 uint32_t addr;
3293 uint32_t count;
3294 uint32_t v;
3295 const char *varname;
3296 int n, e, retval;
3297 uint32_t i;
3298
3299 /* argv[1] = name of array to get the data
3300 * argv[2] = desired width
3301 * argv[3] = memory address
3302 * argv[4] = count to write
3303 */
3304 if (argc != 4) {
3305 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems");
3306 return JIM_ERR;
3307 }
3308 varname = Jim_GetString(argv[0], &len);
3309 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3310
3311 e = Jim_GetLong(interp, argv[1], &l);
3312 width = l;
3313 if (e != JIM_OK) {
3314 return e;
3315 }
3316
3317 e = Jim_GetLong(interp, argv[2], &l);
3318 addr = l;
3319 if (e != JIM_OK) {
3320 return e;
3321 }
3322 e = Jim_GetLong(interp, argv[3], &l);
3323 len = l;
3324 if (e != JIM_OK) {
3325 return e;
3326 }
3327 switch (width) {
3328 case 8:
3329 width = 1;
3330 break;
3331 case 16:
3332 width = 2;
3333 break;
3334 case 32:
3335 width = 4;
3336 break;
3337 default:
3338 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3339 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3340 return JIM_ERR;
3341 }
3342 if (len == 0) {
3343 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3344 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3345 return JIM_ERR;
3346 }
3347 if ((addr + (len * width)) < addr) {
3348 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3349 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3350 return JIM_ERR;
3351 }
3352 /* absurd transfer size? */
3353 if (len > 65536) {
3354 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3355 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3356 return JIM_ERR;
3357 }
3358
3359 if ((width == 1) ||
3360 ((width == 2) && ((addr & 1) == 0)) ||
3361 ((width == 4) && ((addr & 3) == 0))) {
3362 /* all is well */
3363 } else {
3364 char buf[100];
3365 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3366 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3367 (unsigned int)addr,
3368 (int)width);
3369 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3370 return JIM_ERR;
3371 }
3372
3373 /* Transfer loop */
3374
3375 /* index counter */
3376 n = 0;
3377 /* assume ok */
3378 e = JIM_OK;
3379
3380 size_t buffersize = 4096;
3381 uint8_t *buffer = malloc(buffersize);
3382 if (buffer == NULL)
3383 return JIM_ERR;
3384
3385 while (len) {
3386 /* Slurp... in buffer size chunks */
3387
3388 count = len; /* in objects.. */
3389 if (count > (buffersize/width)) {
3390 count = (buffersize/width);
3391 }
3392
3393 v = 0; /* shut up gcc */
3394 for (i = 0 ;i < count ;i++, n++) {
3395 get_int_array_element(interp, varname, n, &v);
3396 switch (width) {
3397 case 4:
3398 target_buffer_set_u32(target, &buffer[i*width], v);
3399 break;
3400 case 2:
3401 target_buffer_set_u16(target, &buffer[i*width], v);
3402 break;
3403 case 1:
3404 buffer[i] = v & 0x0ff;
3405 break;
3406 }
3407 }
3408 len -= count;
3409
3410 retval = target_write_memory(target, addr, width, count, buffer);
3411 if (retval != ERROR_OK) {
3412 /* BOO !*/
3413 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3414 (unsigned int)addr,
3415 (int)width,
3416 (int)count);
3417 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3418 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3419 e = JIM_ERR;
3420 len = 0;
3421 }
3422 }
3423
3424 free(buffer);
3425
3426 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3427
3428 return JIM_OK;
3429 }
3430
3431 void target_all_handle_event(enum target_event e)
3432 {
3433 struct target *target;
3434
3435 LOG_DEBUG("**all*targets: event: %d, %s",
3436 (int)e,
3437 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3438
3439 target = all_targets;
3440 while (target) {
3441 target_handle_event(target, e);
3442 target = target->next;
3443 }
3444 }
3445
3446
3447 /* FIX? should we propagate errors here rather than printing them
3448 * and continuing?
3449 */
3450 void target_handle_event(struct target *target, enum target_event e)
3451 {
3452 struct target_event_action *teap;
3453
3454 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3455 if (teap->event == e) {
3456 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3457 target->target_number,
3458 target_name(target),
3459 target_type_name(target),
3460 e,
3461 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3462 Jim_GetString(teap->body, NULL));
3463 if (Jim_EvalObj(teap->interp, teap->body) != JIM_OK)
3464 {
3465 Jim_PrintErrorMessage(teap->interp);
3466 }
3467 }
3468 }
3469 }
3470
3471 /**
3472 * Returns true only if the target has a handler for the specified event.
3473 */
3474 bool target_has_event_action(struct target *target, enum target_event event)
3475 {
3476 struct target_event_action *teap;
3477
3478 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3479 if (teap->event == event)
3480 return true;
3481 }
3482 return false;
3483 }
3484
3485 enum target_cfg_param {
3486 TCFG_TYPE,
3487 TCFG_EVENT,
3488 TCFG_WORK_AREA_VIRT,
3489 TCFG_WORK_AREA_PHYS,
3490 TCFG_WORK_AREA_SIZE,
3491 TCFG_WORK_AREA_BACKUP,
3492 TCFG_ENDIAN,
3493 TCFG_VARIANT,
3494 TCFG_CHAIN_POSITION,
3495 };
3496
3497 static Jim_Nvp nvp_config_opts[] = {
3498 { .name = "-type", .value = TCFG_TYPE },
3499 { .name = "-event", .value = TCFG_EVENT },
3500 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3501 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3502 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3503 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3504 { .name = "-endian" , .value = TCFG_ENDIAN },
3505 { .name = "-variant", .value = TCFG_VARIANT },
3506 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3507
3508 { .name = NULL, .value = -1 }
3509 };
3510
3511 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
3512 {
3513 Jim_Nvp *n;
3514 Jim_Obj *o;
3515 jim_wide w;
3516 char *cp;
3517 int e;
3518
3519 /* parse config or cget options ... */
3520 while (goi->argc > 0) {
3521 Jim_SetEmptyResult(goi->interp);
3522 /* Jim_GetOpt_Debug(goi); */
3523
3524 if (target->type->target_jim_configure) {
3525 /* target defines a configure function */
3526 /* target gets first dibs on parameters */
3527 e = (*(target->type->target_jim_configure))(target, goi);
3528 if (e == JIM_OK) {
3529 /* more? */
3530 continue;
3531 }
3532 if (e == JIM_ERR) {
3533 /* An error */
3534 return e;
3535 }
3536 /* otherwise we 'continue' below */
3537 }
3538 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3539 if (e != JIM_OK) {
3540 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3541 return e;
3542 }
3543 switch (n->value) {
3544 case TCFG_TYPE:
3545 /* not setable */
3546 if (goi->isconfigure) {
3547 Jim_SetResult_sprintf(goi->interp,
3548 "not settable: %s", n->name);
3549 return JIM_ERR;
3550 } else {
3551 no_params:
3552 if (goi->argc != 0) {
3553 Jim_WrongNumArgs(goi->interp,
3554 goi->argc, goi->argv,
3555 "NO PARAMS");
3556 return JIM_ERR;
3557 }
3558 }
3559 Jim_SetResultString(goi->interp,
3560 target_type_name(target), -1);
3561 /* loop for more */
3562 break;
3563 case TCFG_EVENT:
3564 if (goi->argc == 0) {
3565 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3566 return JIM_ERR;
3567 }
3568
3569 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3570 if (e != JIM_OK) {
3571 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3572 return e;
3573 }
3574
3575 if (goi->isconfigure) {
3576 if (goi->argc != 1) {
3577 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3578 return JIM_ERR;
3579 }
3580 } else {
3581 if (goi->argc != 0) {
3582 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3583 return JIM_ERR;
3584 }
3585 }
3586
3587 {
3588 struct target_event_action *teap;
3589
3590 teap = target->event_action;
3591 /* replace existing? */
3592 while (teap) {
3593 if (teap->event == (enum target_event)n->value) {
3594 break;
3595 }
3596 teap = teap->next;
3597 }
3598
3599 if (goi->isconfigure) {
3600 bool replace = true;
3601 if (teap == NULL) {
3602 /* create new */
3603 teap = calloc(1, sizeof(*teap));
3604 replace = false;
3605 }
3606 teap->event = n->value;
3607 teap->interp = goi->interp;
3608 Jim_GetOpt_Obj(goi, &o);
3609 if (teap->body) {
3610 Jim_DecrRefCount(teap->interp, teap->body);
3611 }
3612 teap->body = Jim_DuplicateObj(goi->interp, o);
3613 /*
3614 * FIXME:
3615 * Tcl/TK - "tk events" have a nice feature.
3616 * See the "BIND" command.
3617 * We should support that here.
3618 * You can specify %X and %Y in the event code.
3619 * The idea is: %T - target name.
3620 * The idea is: %N - target number
3621 * The idea is: %E - event name.
3622 */
3623 Jim_IncrRefCount(teap->body);
3624
3625 if (!replace)
3626 {
3627 /* add to head of event list */
3628 teap->next = target->event_action;
3629 target->event_action = teap;
3630 }
3631 Jim_SetEmptyResult(goi->interp);
3632 } else {
3633 /* get */
3634 if (teap == NULL) {
3635 Jim_SetEmptyResult(goi->interp);
3636 } else {
3637 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3638 }
3639 }
3640 }
3641 /* loop for more */
3642 break;
3643
3644 case TCFG_WORK_AREA_VIRT:
3645 if (goi->isconfigure) {
3646 target_free_all_working_areas(target);
3647 e = Jim_GetOpt_Wide(goi, &w);
3648 if (e != JIM_OK) {
3649 return e;
3650 }
3651 target->working_area_virt = w;
3652 target->working_area_virt_spec = true;
3653 } else {
3654 if (goi->argc != 0) {
3655 goto no_params;
3656 }
3657 }
3658 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3659 /* loop for more */
3660 break;
3661
3662 case TCFG_WORK_AREA_PHYS:
3663 if (goi->isconfigure) {
3664 target_free_all_working_areas(target);
3665 e = Jim_GetOpt_Wide(goi, &w);
3666 if (e != JIM_OK) {
3667 return e;
3668 }
3669 target->working_area_phys = w;
3670 target->working_area_phys_spec = true;
3671 } else {
3672 if (goi->argc != 0) {
3673 goto no_params;
3674 }
3675 }
3676 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3677 /* loop for more */
3678 break;
3679
3680 case TCFG_WORK_AREA_SIZE:
3681 if (goi->isconfigure) {
3682 target_free_all_working_areas(target);
3683 e = Jim_GetOpt_Wide(goi, &w);
3684 if (e != JIM_OK) {
3685 return e;
3686 }
3687 target->working_area_size = w;
3688 } else {
3689 if (goi->argc != 0) {
3690 goto no_params;
3691 }
3692 }
3693 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3694 /* loop for more */
3695 break;
3696
3697 case TCFG_WORK_AREA_BACKUP:
3698 if (goi->isconfigure) {
3699 target_free_all_working_areas(target);
3700 e = Jim_GetOpt_Wide(goi, &w);
3701 if (e != JIM_OK) {
3702 return e;
3703 }
3704 /* make this exactly 1 or 0 */
3705 target->backup_working_area = (!!w);
3706 } else {
3707 if (goi->argc != 0) {
3708 goto no_params;
3709 }
3710 }
3711 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3712 /* loop for more e*/
3713 break;
3714
3715 case TCFG_ENDIAN:
3716 if (goi->isconfigure) {
3717 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3718 if (e != JIM_OK) {
3719 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3720 return e;
3721 }
3722 target->endianness = n->value;
3723 } else {
3724 if (goi->argc != 0) {
3725 goto no_params;
3726 }
3727 }
3728 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3729 if (n->name == NULL) {
3730 target->endianness = TARGET_LITTLE_ENDIAN;
3731 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3732 }
3733 Jim_SetResultString(goi->interp, n->name, -1);
3734 /* loop for more */
3735 break;
3736
3737 case TCFG_VARIANT:
3738 if (goi->isconfigure) {
3739 if (goi->argc < 1) {
3740 Jim_SetResult_sprintf(goi->interp,
3741 "%s ?STRING?",
3742 n->name);
3743 return JIM_ERR;
3744 }
3745 if (target->variant) {
3746 free((void *)(target->variant));
3747 }
3748 e = Jim_GetOpt_String(goi, &cp, NULL);
3749 target->variant = strdup(cp);
3750 } else {
3751 if (goi->argc != 0) {
3752 goto no_params;
3753 }
3754 }
3755 Jim_SetResultString(goi->interp, target->variant,-1);
3756 /* loop for more */
3757 break;
3758 case TCFG_CHAIN_POSITION:
3759 if (goi->isconfigure) {
3760 Jim_Obj *o;
3761 struct jtag_tap *tap;
3762 target_free_all_working_areas(target);
3763 e = Jim_GetOpt_Obj(goi, &o);
3764 if (e != JIM_OK) {
3765 return e;
3766 }
3767 tap = jtag_tap_by_jim_obj(goi->interp, o);
3768 if (tap == NULL) {
3769 return JIM_ERR;
3770 }
3771 /* make this exactly 1 or 0 */
3772 target->tap = tap;
3773 } else {
3774 if (goi->argc != 0) {
3775 goto no_params;
3776 }
3777 }
3778 Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
3779 /* loop for more e*/
3780 break;
3781 }
3782 } /* while (goi->argc) */
3783
3784
3785 /* done - we return */
3786 return JIM_OK;
3787 }
3788
3789 static int jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3790 {
3791 Jim_GetOptInfo goi;
3792 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3793 goi.isconfigure = strcmp(Jim_GetString(argv[0], NULL), "configure") == 0;
3794 int need_args = 1 + goi.isconfigure;
3795 if (goi.argc < need_args)
3796 {
3797 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
3798 goi.isconfigure
3799 ? "missing: -option VALUE ..."
3800 : "missing: -option ...");
3801 return JIM_ERR;
3802 }
3803 struct target *target = Jim_CmdPrivData(goi.interp);
3804 return target_configure(&goi, target);
3805 }
3806
3807 static int jim_target_mw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3808 {
3809 const char *cmd_name = Jim_GetString(argv[0], NULL);
3810
3811 Jim_GetOptInfo goi;
3812 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3813
3814 if (goi.argc != 2 && goi.argc != 3)
3815 {
3816 Jim_SetResult_sprintf(goi.interp,
3817 "usage: %s <address> <data> [<count>]", cmd_name);
3818 return JIM_ERR;
3819 }
3820
3821 jim_wide a;
3822 int e = Jim_GetOpt_Wide(&goi, &a);
3823 if (e != JIM_OK)
3824 return e;
3825
3826 jim_wide b;
3827 e = Jim_GetOpt_Wide(&goi, &b);
3828 if (e != JIM_OK)
3829 return e;
3830
3831 jim_wide c = 1;
3832 if (goi.argc == 3)
3833 {
3834 e = Jim_GetOpt_Wide(&goi, &c);
3835 if (e != JIM_OK)
3836 return e;
3837 }
3838
3839 struct target *target = Jim_CmdPrivData(goi.interp);
3840 uint8_t target_buf[32];
3841 if (strcasecmp(cmd_name, "mww") == 0) {
3842 target_buffer_set_u32(target, target_buf, b);
3843 b = 4;
3844 }
3845 else if (strcasecmp(cmd_name, "mwh") == 0) {
3846 target_buffer_set_u16(target, target_buf, b);
3847 b = 2;
3848 }
3849 else if (strcasecmp(cmd_name, "mwb") == 0) {
3850 target_buffer_set_u8(target, target_buf, b);
3851 b = 1;
3852 } else {
3853 LOG_ERROR("command '%s' unknown: ", cmd_name);
3854 return JIM_ERR;
3855 }
3856
3857 for (jim_wide x = 0; x < c; x++)
3858 {
3859 e = target_write_memory(target, a, b, 1, target_buf);
3860 if (e != ERROR_OK)
3861 {
3862 Jim_SetResult_sprintf(interp,
3863 "Error writing @ 0x%08x: %d\n", (int)(a), e);
3864 return JIM_ERR;
3865 }
3866 /* b = width */
3867 a = a + b;
3868 }
3869 return JIM_OK;
3870 }
3871
3872 static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3873 {
3874 const char *cmd_name = Jim_GetString(argv[0], NULL);
3875
3876 Jim_GetOptInfo goi;
3877 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3878
3879 if ((goi.argc == 2) || (goi.argc == 3))
3880 {
3881 Jim_SetResult_sprintf(goi.interp,
3882 "usage: %s <address> [<count>]", cmd_name);
3883 return JIM_ERR;
3884 }
3885
3886 jim_wide a;
3887 int e = Jim_GetOpt_Wide(&goi, &a);
3888 if (e != JIM_OK) {
3889 return JIM_ERR;
3890 }
3891 jim_wide c;
3892 if (goi.argc) {
3893 e = Jim_GetOpt_Wide(&goi, &c);
3894 if (e != JIM_OK) {
3895 return JIM_ERR;
3896 }
3897 } else {
3898 c = 1;
3899 }
3900 jim_wide b = 1; /* shut up gcc */
3901 if (strcasecmp(cmd_name, "mdw") == 0)
3902 b = 4;
3903 else if (strcasecmp(cmd_name, "mdh") == 0)
3904 b = 2;
3905 else if (strcasecmp(cmd_name, "mdb") == 0)
3906 b = 1;
3907 else {
3908 LOG_ERROR("command '%s' unknown: ", cmd_name);
3909 return JIM_ERR;
3910 }
3911
3912 /* convert count to "bytes" */
3913 c = c * b;
3914
3915 struct target *target = Jim_CmdPrivData(goi.interp);
3916 uint8_t target_buf[32];
3917 jim_wide x, y, z;
3918 while (c > 0) {
3919 y = c;
3920 if (y > 16) {
3921 y = 16;
3922 }
3923 e = target_read_memory(target, a, b, y / b, target_buf);
3924 if (e != ERROR_OK) {
3925 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
3926 return JIM_ERR;
3927 }
3928
3929 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
3930 switch (b) {
3931 case 4:
3932 for (x = 0; x < 16 && x < y; x += 4)
3933 {
3934 z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
3935 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
3936 }
3937 for (; (x < 16) ; x += 4) {
3938 Jim_fprintf(interp, interp->cookie_stdout, " ");
3939 }
3940 break;
3941 case 2:
3942 for (x = 0; x < 16 && x < y; x += 2)
3943 {
3944 z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
3945 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
3946 }
3947 for (; (x < 16) ; x += 2) {
3948 Jim_fprintf(interp, interp->cookie_stdout, " ");
3949 }
3950 break;
3951 case 1:
3952 default:
3953 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
3954 z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
3955 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
3956 }
3957 for (; (x < 16) ; x += 1) {
3958 Jim_fprintf(interp, interp->cookie_stdout, " ");
3959 }
3960 break;
3961 }
3962 /* ascii-ify the bytes */
3963 for (x = 0 ; x < y ; x++) {
3964 if ((target_buf[x] >= 0x20) &&
3965 (target_buf[x] <= 0x7e)) {
3966 /* good */
3967 } else {
3968 /* smack it */
3969 target_buf[x] = '.';
3970 }
3971 }
3972 /* space pad */
3973 while (x < 16) {
3974 target_buf[x] = ' ';
3975 x++;
3976 }
3977 /* terminate */
3978 target_buf[16] = 0;
3979 /* print - with a newline */
3980 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
3981 /* NEXT... */
3982 c -= 16;
3983 a += 16;
3984 }
3985 return JIM_OK;
3986 }
3987
3988 static int jim_target_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3989 {
3990 struct target *target = Jim_CmdPrivData(interp);
3991 return target_mem2array(interp, target, argc - 1, argv + 1);
3992 }
3993
3994 static int jim_target_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3995 {
3996 struct target *target = Jim_CmdPrivData(interp);
3997 return target_array2mem(interp, target, argc - 1, argv + 1);
3998 }
3999
4000 static int jim_target_tap_disabled(Jim_Interp *interp)
4001 {
4002 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4003 return JIM_ERR;
4004 }
4005
4006 static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4007 {
4008 if (argc != 1)
4009 {
4010 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4011 return JIM_ERR;
4012 }
4013 struct target *target = Jim_CmdPrivData(interp);
4014 if (!target->tap->enabled)
4015 return jim_target_tap_disabled(interp);
4016
4017 int e = target->type->examine(target);
4018 if (e != ERROR_OK)
4019 {
4020 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
4021 return JIM_ERR;
4022 }
4023 return JIM_OK;
4024 }
4025
4026 static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4027 {
4028 if (argc != 1)
4029 {
4030 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4031 return JIM_ERR;
4032 }
4033 struct target *target = Jim_CmdPrivData(interp);
4034 if (!target->tap->enabled)
4035 return jim_target_tap_disabled(interp);
4036
4037 int e;
4038 if (!(target_was_examined(target))) {
4039 e = ERROR_TARGET_NOT_EXAMINED;
4040 } else {
4041 e = target->type->poll(target);
4042 }
4043 if (e != ERROR_OK)
4044 {
4045 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4046 return JIM_ERR;
4047 }
4048 return JIM_OK;
4049 }
4050
4051 static int jim_target_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4052 {
4053 Jim_GetOptInfo goi;
4054 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4055
4056 if (goi.argc != 2)
4057 {
4058 Jim_WrongNumArgs(interp, 0, argv,
4059 "([tT]|[fF]|assert|deassert) BOOL");
4060 return JIM_ERR;
4061 }
4062
4063 Jim_Nvp *n;
4064 int e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4065 if (e != JIM_OK)
4066 {
4067 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4068 return e;
4069 }
4070 /* the halt or not param */
4071 jim_wide a;
4072 e = Jim_GetOpt_Wide(&goi, &a);
4073 if (e != JIM_OK)
4074 return e;
4075
4076 struct target *target = Jim_CmdPrivData(goi.interp);
4077 if (!target->tap->enabled)
4078 return jim_target_tap_disabled(interp);
4079 if (!target->type->assert_reset || !target->type->deassert_reset)
4080 {
4081 Jim_SetResult_sprintf(interp,
4082 "No target-specific reset for %s",
4083 target_name(target));
4084 return JIM_ERR;
4085 }
4086 /* determine if we should halt or not. */
4087 target->reset_halt = !!a;
4088 /* When this happens - all workareas are invalid. */
4089 target_free_all_working_areas_restore(target, 0);
4090
4091 /* do the assert */
4092 if (n->value == NVP_ASSERT) {
4093 e = target->type->assert_reset(target);
4094 } else {
4095 e = target->type->deassert_reset(target);
4096 }
4097 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4098 }
4099
4100 static int jim_target_halt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4101 {
4102 if (argc != 1) {
4103 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4104 return JIM_ERR;
4105 }
4106 struct target *target = Jim_CmdPrivData(interp);
4107 if (!target->tap->enabled)
4108 return jim_target_tap_disabled(interp);
4109 int e = target->type->halt(target);
4110 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4111 }
4112
4113 static int jim_target_wait_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4114 {
4115 Jim_GetOptInfo goi;
4116 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4117
4118 /* params: <name> statename timeoutmsecs */
4119 if (goi.argc != 2)
4120 {
4121 const char *cmd_name = Jim_GetString(argv[0], NULL);
4122 Jim_SetResult_sprintf(goi.interp,
4123 "%s <state_name> <timeout_in_msec>", cmd_name);
4124 return JIM_ERR;
4125 }
4126
4127 Jim_Nvp *n;
4128 int e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4129 if (e != JIM_OK) {
4130 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4131 return e;
4132 }
4133 jim_wide a;
4134 e = Jim_GetOpt_Wide(&goi, &a);
4135 if (e != JIM_OK) {
4136 return e;
4137 }
4138 struct target *target = Jim_CmdPrivData(interp);
4139 if (!target->tap->enabled)
4140 return jim_target_tap_disabled(interp);
4141
4142 e = target_wait_state(target, n->value, a);
4143 if (e != ERROR_OK)
4144 {
4145 Jim_SetResult_sprintf(goi.interp,
4146 "target: %s wait %s fails (%d) %s",
4147 target_name(target), n->name,
4148 e, target_strerror_safe(e));
4149 return JIM_ERR;
4150 }
4151 return JIM_OK;
4152 }
4153 /* List for human, Events defined for this target.
4154 * scripts/programs should use 'name cget -event NAME'
4155 */
4156 static int jim_target_event_list(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4157 {
4158 struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
4159 struct target *target = Jim_CmdPrivData(interp);
4160 struct target_event_action *teap = target->event_action;
4161 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4162 target->target_number,
4163 target_name(target));
4164 command_print(cmd_ctx, "%-25s | Body", "Event");
4165 command_print(cmd_ctx, "------------------------- | "
4166 "----------------------------------------");
4167 while (teap)
4168 {
4169 Jim_Nvp *opt = Jim_Nvp_value2name_simple(nvp_target_event, teap->event);
4170 command_print(cmd_ctx, "%-25s | %s",
4171 opt->name, Jim_GetString(teap->body, NULL));
4172 teap = teap->next;
4173 }
4174 command_print(cmd_ctx, "***END***");
4175 return JIM_OK;
4176 }
4177 static int jim_target_current_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4178 {
4179 if (argc != 1)
4180 {
4181 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4182 return JIM_ERR;
4183 }
4184 struct target *target = Jim_CmdPrivData(interp);
4185 Jim_SetResultString(interp, target_state_name(target), -1);
4186 return JIM_OK;
4187 }
4188 static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4189 {
4190 Jim_GetOptInfo goi;
4191 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4192 if (goi.argc != 1)
4193 {
4194 const char *cmd_name = Jim_GetString(argv[0], NULL);
4195 Jim_SetResult_sprintf(goi.interp, "%s <eventname>", cmd_name);
4196 return JIM_ERR;
4197 }
4198 Jim_Nvp *n;
4199 int e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4200 if (e != JIM_OK)
4201 {
4202 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4203 return e;
4204 }
4205 struct target *target = Jim_CmdPrivData(interp);
4206 target_handle_event(target, n->value);
4207 return JIM_OK;
4208 }
4209
4210 static const struct command_registration target_instance_command_handlers[] = {
4211 {
4212 .name = "configure",
4213 .mode = COMMAND_CONFIG,
4214 .jim_handler = &jim_target_configure,
4215 .usage = "[<target_options> ...]",
4216 .help = "configure a new target for use",
4217 },
4218 {
4219 .name = "cget",
4220 .mode = COMMAND_ANY,
4221 .jim_handler = &jim_target_configure,
4222 .usage = "<target_type> [<target_options> ...]",
4223 .help = "configure a new target for use",
4224 },
4225 {
4226 .name = "mww",
4227 .mode = COMMAND_EXEC,
4228 .jim_handler = &jim_target_mw,
4229 .usage = "<address> <data> [<count>]",
4230 .help = "Write 32-bit word(s) to target memory",
4231 },
4232 {
4233 .name = "mwh",
4234 .mode = COMMAND_EXEC,
4235 .jim_handler = &jim_target_mw,
4236 .usage = "<address> <data> [<count>]",
4237 .help = "Write 16-bit half-word(s) to target memory",
4238 },
4239 {
4240 .name = "mwb",
4241 .mode = COMMAND_EXEC,
4242 .jim_handler = &jim_target_mw,
4243 .usage = "<address> <data> [<count>]",
4244 .help = "Write byte(s) to target memory",
4245 },
4246 {
4247 .name = "mdw",
4248 .mode = COMMAND_EXEC,
4249 .jim_handler = &jim_target_md,
4250 .usage = "<address> [<count>]",
4251 .help = "Display target memory as 32-bit words",
4252 },
4253 {
4254 .name = "mdh",
4255 .mode = COMMAND_EXEC,
4256 .jim_handler = &jim_target_md,
4257 .usage = "<address> [<count>]",
4258 .help = "Display target memory as 16-bit half-words",
4259 },
4260 {
4261 .name = "mdb",
4262 .mode = COMMAND_EXEC,
4263 .jim_handler = &jim_target_md,
4264 .usage = "<address> [<count>]",
4265 .help = "Display target memory as 8-bit bytes",
4266 },
4267 {
4268 .name = "array2mem",
4269 .mode = COMMAND_EXEC,
4270 .jim_handler = &jim_target_array2mem,
4271 },
4272 {
4273 .name = "mem2array",
4274 .mode = COMMAND_EXEC,
4275 .jim_handler = &jim_target_mem2array,
4276 },
4277 {
4278 .name = "eventlist",
4279 .mode = COMMAND_EXEC,
4280 .jim_handler = &jim_target_event_list,
4281 },
4282 {
4283 .name = "curstate",
4284 .mode = COMMAND_EXEC,
4285 .jim_handler = &jim_target_current_state,
4286 },
4287 {
4288 .name = "arp_examine",
4289 .mode = COMMAND_EXEC,
4290 .jim_handler = &jim_target_examine,
4291 },
4292 {
4293 .name = "arp_poll",
4294 .mode = COMMAND_EXEC,
4295 .jim_handler = &jim_target_poll,
4296 },
4297 {
4298 .name = "arp_reset",
4299 .mode = COMMAND_EXEC,
4300 .jim_handler = &jim_target_reset,
4301 },
4302 {
4303 .name = "arp_halt",
4304 .mode = COMMAND_EXEC,
4305 .jim_handler = &jim_target_halt,
4306 },
4307 {
4308 .name = "arp_waitstate",
4309 .mode = COMMAND_EXEC,
4310 .jim_handler = &jim_target_wait_state,
4311 },
4312 {
4313 .name = "invoke-event",
4314 .mode = COMMAND_EXEC,
4315 .jim_handler = &jim_target_invoke_event,
4316 },
4317 COMMAND_REGISTRATION_DONE
4318 };
4319
4320 static int target_create(Jim_GetOptInfo *goi)
4321 {
4322 Jim_Obj *new_cmd;
4323 Jim_Cmd *cmd;
4324 const char *cp;
4325 char *cp2;
4326 int e;
4327 int x;
4328 struct target *target;
4329 struct command_context *cmd_ctx;
4330
4331 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4332 if (goi->argc < 3) {
4333 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4334 return JIM_ERR;
4335 }
4336
4337 /* COMMAND */
4338 Jim_GetOpt_Obj(goi, &new_cmd);
4339 /* does this command exist? */
4340 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4341 if (cmd) {
4342 cp = Jim_GetString(new_cmd, NULL);
4343 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4344 return JIM_ERR;
4345 }
4346
4347 /* TYPE */
4348 e = Jim_GetOpt_String(goi, &cp2, NULL);
4349 cp = cp2;
4350 /* now does target type exist */
4351 for (x = 0 ; target_types[x] ; x++) {
4352 if (0 == strcmp(cp, target_types[x]->name)) {
4353 /* found */
4354 break;
4355 }
4356 }
4357 if (target_types[x] == NULL) {
4358 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4359 for (x = 0 ; target_types[x] ; x++) {
4360 if (target_types[x + 1]) {
4361 Jim_AppendStrings(goi->interp,
4362 Jim_GetResult(goi->interp),
4363 target_types[x]->name,
4364 ", ", NULL);
4365 } else {
4366 Jim_AppendStrings(goi->interp,
4367 Jim_GetResult(goi->interp),
4368 " or ",
4369 target_types[x]->name,NULL);
4370 }
4371 }
4372 return JIM_ERR;
4373 }
4374
4375 /* Create it */
4376 target = calloc(1,sizeof(struct target));
4377 /* set target number */
4378 target->target_number = new_target_number();
4379
4380 /* allocate memory for each unique target type */
4381 target->type = (struct target_type*)calloc(1,sizeof(struct target_type));
4382
4383 memcpy(target->type, target_types[x], sizeof(struct target_type));
4384
4385 /* will be set by "-endian" */
4386 target->endianness = TARGET_ENDIAN_UNKNOWN;
4387
4388 target->working_area = 0x0;
4389 target->working_area_size = 0x0;
4390 target->working_areas = NULL;
4391 target->backup_working_area = 0;
4392
4393 target->state = TARGET_UNKNOWN;
4394 target->debug_reason = DBG_REASON_UNDEFINED;
4395 target->reg_cache = NULL;
4396 target->breakpoints = NULL;
4397 target->watchpoints = NULL;
4398 target->next = NULL;
4399 target->arch_info = NULL;
4400
4401 target->display = 1;
4402
4403 target->halt_issued = false;
4404
4405 /* initialize trace information */
4406 target->trace_info = malloc(sizeof(struct trace));
4407 target->trace_info->num_trace_points = 0;
4408 target->trace_info->trace_points_size = 0;
4409 target->trace_info->trace_points = NULL;
4410 target->trace_info->trace_history_size = 0;
4411 target->trace_info->trace_history = NULL;
4412 target->trace_info->trace_history_pos = 0;
4413 target->trace_info->trace_history_overflowed = 0;
4414
4415 target->dbgmsg = NULL;
4416 target->dbg_msg_enabled = 0;
4417
4418 target->endianness = TARGET_ENDIAN_UNKNOWN;
4419
4420 /* Do the rest as "configure" options */
4421 goi->isconfigure = 1;
4422 e = target_configure(goi, target);
4423
4424 if (target->tap == NULL)
4425 {
4426 Jim_SetResultString(goi->interp, "-chain-position required when creating target", -1);
4427 e = JIM_ERR;
4428 }
4429
4430 if (e != JIM_OK) {
4431 free(target->type);
4432 free(target);
4433 return e;
4434 }
4435
4436 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4437 /* default endian to little if not specified */
4438 target->endianness = TARGET_LITTLE_ENDIAN;
4439 }
4440
4441 /* incase variant is not set */
4442 if (!target->variant)
4443 target->variant = strdup("");
4444
4445 cp = Jim_GetString(new_cmd, NULL);
4446 target->cmd_name = strdup(cp);
4447
4448 /* create the target specific commands */
4449 if (target->type->commands) {
4450 e = register_commands(cmd_ctx, NULL, target->type->commands);
4451 if (ERROR_OK != e)
4452 LOG_ERROR("unable to register '%s' commands", cp);
4453 }
4454 if (target->type->target_create) {
4455 (*(target->type->target_create))(target, goi->interp);
4456 }
4457
4458 /* append to end of list */
4459 {
4460 struct target **tpp;
4461 tpp = &(all_targets);
4462 while (*tpp) {
4463 tpp = &((*tpp)->next);
4464 }
4465 *tpp = target;
4466 }
4467
4468 /* now - create the new target name command */
4469 const const struct command_registration target_subcommands[] = {
4470 {
4471 .chain = target_instance_command_handlers,
4472 },
4473 {
4474 .chain = target->type->commands,
4475 },
4476 COMMAND_REGISTRATION_DONE
4477 };
4478 const const struct command_registration target_commands[] = {
4479 {
4480 .name = cp,
4481 .mode = COMMAND_ANY,
4482 .help = "target command group",
4483 .chain = target_subcommands,
4484 },
4485 COMMAND_REGISTRATION_DONE
4486 };
4487 e = register_commands(cmd_ctx, NULL, target_commands);
4488 if (ERROR_OK != e)
4489 return JIM_ERR;
4490
4491 struct command *c = command_find_in_context(cmd_ctx, cp);
4492 assert(c);
4493 command_set_handler_data(c, target);
4494
4495 return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
4496 }
4497
4498 static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4499 {
4500 if (argc != 1)
4501 {
4502 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4503 return JIM_ERR;
4504 }
4505 struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
4506 Jim_SetResultString(interp, get_current_target(cmd_ctx)->cmd_name, -1);
4507 return JIM_OK;
4508 }
4509
4510 static int jim_target_types(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4511 {
4512 if (argc != 1)
4513 {
4514 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4515 return JIM_ERR;
4516 }
4517 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
4518 for (unsigned x = 0; NULL != target_types[x]; x++)
4519 {
4520 Jim_ListAppendElement(interp, Jim_GetResult(interp),
4521 Jim_NewStringObj(interp, target_types[x]->name, -1));
4522 }
4523 return JIM_OK;
4524 }
4525
4526 static int jim_target_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4527 {
4528 if (argc != 1)
4529 {
4530 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4531 return JIM_ERR;
4532 }
4533 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
4534 struct target *target = all_targets;
4535 while (target)
4536 {
4537 Jim_ListAppendElement(interp, Jim_GetResult(interp),
4538 Jim_NewStringObj(interp, target_name(target), -1));
4539 target = target->next;
4540 }
4541 return JIM_OK;
4542 }
4543
4544 static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4545 {
4546 Jim_GetOptInfo goi;
4547 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4548 if (goi.argc < 3)
4549 {
4550 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
4551 "<name> <target_type> [<target_options> ...]");
4552 return JIM_ERR;
4553 }
4554 return target_create(&goi);
4555 }
4556
4557 static int jim_target_number(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4558 {
4559 Jim_GetOptInfo goi;
4560 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4561
4562 /* It's OK to remove this mechanism sometime after August 2010 or so */
4563 LOG_WARNING("don't use numbers as target identifiers; use names");
4564 if (goi.argc != 1)
4565 {
4566 Jim_SetResult_sprintf(goi.interp, "usage: target number <number>");
4567 return JIM_ERR;
4568 }
4569 jim_wide w;
4570 int e = Jim_GetOpt_Wide(&goi, &w);
4571 if (e != JIM_OK)
4572 return JIM_ERR;
4573
4574 struct target *target;
4575 for (target = all_targets; NULL != target; target = target->next)
4576 {
4577 if (target->target_number != w)
4578 continue;
4579
4580 Jim_SetResultString(goi.interp, target_name(target), -1);
4581 return JIM_OK;
4582 }
4583 Jim_SetResult_sprintf(goi.interp,
4584 "Target: number %d does not exist", (int)(w));
4585 return JIM_ERR;
4586 }
4587
4588 static int jim_target_count(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4589 {
4590 if (argc != 1)
4591 {
4592 Jim_WrongNumArgs(interp, 1, argv, "<no parameters>");
4593 return JIM_ERR;
4594 }
4595 unsigned count = 0;
4596 struct target *target = all_targets;
4597 while (NULL != target)
4598 {
4599 target = target->next;
4600 count++;
4601 }
4602 Jim_SetResult(interp, Jim_NewIntObj(interp, count));
4603 return JIM_OK;
4604 }
4605
4606 static const struct command_registration target_subcommand_handlers[] = {
4607 {
4608 .name = "init",
4609 .mode = COMMAND_CONFIG,
4610 .handler = &handle_target_init_command,
4611 .help = "initialize targets",
4612 },
4613 {
4614 .name = "create",
4615 .mode = COMMAND_ANY,
4616 .jim_handler = &jim_target_create,
4617 .usage = "<name> <type> ...",
4618 .help = "Returns the currently selected target",
4619 },
4620 {
4621 .name = "current",
4622 .mode = COMMAND_ANY,
4623 .jim_handler = &jim_target_current,
4624 .help = "Returns the currently selected target",
4625 },
4626 {
4627 .name = "types",
4628 .mode = COMMAND_ANY,
4629 .jim_handler = &jim_target_types,
4630 .help = "Returns the available target types as a list of strings",
4631 },
4632 {
4633 .name = "names",
4634 .mode = COMMAND_ANY,
4635 .jim_handler = &jim_target_names,
4636 .help = "Returns the names of all targets as a list of strings",
4637 },
4638 {
4639 .name = "number",
4640 .mode = COMMAND_ANY,
4641 .jim_handler = &jim_target_number,
4642 .usage = "<number>",
4643 .help = "Returns the name of target <n>",
4644 },
4645 {
4646 .name = "count",
4647 .mode = COMMAND_ANY,
4648 .jim_handler = &jim_target_count,
4649 .help = "Returns the number of targets as an integer",
4650 },
4651 COMMAND_REGISTRATION_DONE
4652 };
4653
4654
4655 struct FastLoad
4656 {
4657 uint32_t address;
4658 uint8_t *data;
4659 int length;
4660
4661 };
4662
4663 static int fastload_num;
4664 static struct FastLoad *fastload;
4665
4666 static void free_fastload(void)
4667 {
4668 if (fastload != NULL)
4669 {
4670 int i;
4671 for (i = 0; i < fastload_num; i++)
4672 {
4673 if (fastload[i].data)
4674 free(fastload[i].data);
4675 }
4676 free(fastload);
4677 fastload = NULL;
4678 }
4679 }
4680
4681
4682
4683
4684 COMMAND_HANDLER(handle_fast_load_image_command)
4685 {
4686 uint8_t *buffer;
4687 size_t buf_cnt;
4688 uint32_t image_size;
4689 uint32_t min_address = 0;
4690 uint32_t max_address = 0xffffffff;
4691 int i;
4692
4693 struct image image;
4694
4695 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
4696 &image, &min_address, &max_address);
4697 if (ERROR_OK != retval)
4698 return retval;
4699
4700 struct duration bench;
4701 duration_start(&bench);
4702
4703 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
4704 {
4705 return ERROR_OK;
4706 }
4707
4708 image_size = 0x0;
4709 retval = ERROR_OK;
4710 fastload_num = image.num_sections;
4711 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4712 if (fastload == NULL)
4713 {
4714 image_close(&image);
4715 return ERROR_FAIL;
4716 }
4717 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4718 for (i = 0; i < image.num_sections; i++)
4719 {
4720 buffer = malloc(image.sections[i].size);
4721 if (buffer == NULL)
4722 {
4723 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
4724 (int)(image.sections[i].size));
4725 break;
4726 }
4727
4728 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4729 {
4730 free(buffer);
4731 break;
4732 }
4733
4734 uint32_t offset = 0;
4735 uint32_t length = buf_cnt;
4736
4737
4738 /* DANGER!!! beware of unsigned comparision here!!! */
4739
4740 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4741 (image.sections[i].base_address < max_address))
4742 {
4743 if (image.sections[i].base_address < min_address)
4744 {
4745 /* clip addresses below */
4746 offset += min_address-image.sections[i].base_address;
4747 length -= offset;
4748 }
4749
4750 if (image.sections[i].base_address + buf_cnt > max_address)
4751 {
4752 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4753 }
4754
4755 fastload[i].address = image.sections[i].base_address + offset;
4756 fastload[i].data = malloc(length);
4757 if (fastload[i].data == NULL)
4758 {
4759 free(buffer);
4760 break;
4761 }
4762 memcpy(fastload[i].data, buffer + offset, length);
4763 fastload[i].length = length;
4764
4765 image_size += length;
4766 command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
4767 (unsigned int)length,
4768 ((unsigned int)(image.sections[i].base_address + offset)));
4769 }
4770
4771 free(buffer);
4772 }
4773
4774 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
4775 {
4776 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
4777 "in %fs (%0.3f kb/s)", image_size,
4778 duration_elapsed(&bench), duration_kbps(&bench, image_size));
4779
4780 command_print(CMD_CTX,
4781 "WARNING: image has not been loaded to target!"
4782 "You can issue a 'fast_load' to finish loading.");
4783 }
4784
4785 image_close(&image);
4786
4787 if (retval != ERROR_OK)
4788 {
4789 free_fastload();
4790 }
4791
4792 return retval;
4793 }
4794
4795 COMMAND_HANDLER(handle_fast_load_command)
4796 {
4797 if (CMD_ARGC > 0)
4798 return ERROR_COMMAND_SYNTAX_ERROR;
4799 if (fastload == NULL)
4800 {
4801 LOG_ERROR("No image in memory");
4802 return ERROR_FAIL;
4803 }
4804 int i;
4805 int ms = timeval_ms();
4806 int size = 0;
4807 int retval = ERROR_OK;
4808 for (i = 0; i < fastload_num;i++)
4809 {
4810 struct target *target = get_current_target(CMD_CTX);
4811 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
4812 (unsigned int)(fastload[i].address),
4813 (unsigned int)(fastload[i].length));
4814 if (retval == ERROR_OK)
4815 {
4816 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4817 }
4818 size += fastload[i].length;
4819 }
4820 int after = timeval_ms();
4821 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
4822 return retval;
4823 }
4824
4825 static const struct command_registration target_command_handlers[] = {
4826 {
4827 .name = "targets",
4828 .handler = &handle_targets_command,
4829 .mode = COMMAND_ANY,
4830 .help = "change current command line target (one parameter) "
4831 "or list targets (no parameters)",
4832 .usage = "[<new_current_target>]",
4833 },
4834 {
4835 .name = "target",
4836 .mode = COMMAND_CONFIG,
4837 .help = "configure target",
4838
4839 .chain = target_subcommand_handlers,
4840 },
4841 COMMAND_REGISTRATION_DONE
4842 };
4843
4844 int target_register_commands(struct command_context *cmd_ctx)
4845 {
4846 return register_commands(cmd_ctx, NULL, target_command_handlers);
4847 }
4848
4849 static const struct command_registration target_exec_command_handlers[] = {
4850 {
4851 .name = "fast_load_image",
4852 .handler = &handle_fast_load_image_command,
4853 .mode = COMMAND_ANY,
4854 .help = "Load image into memory, mainly for profiling purposes",
4855 .usage = "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
4856 "[min_address] [max_length]",
4857 },
4858 {
4859 .name = "fast_load",
4860 .handler = &handle_fast_load_command,
4861 .mode = COMMAND_ANY,
4862 .help = "loads active fast load image to current target "
4863 "- mainly for profiling purposes",
4864 },
4865 {
4866 .name = "profile",
4867 .handler = &handle_profile_command,
4868 .mode = COMMAND_EXEC,
4869 .help = "profiling samples the CPU PC",
4870 },
4871 /** @todo don't register virt2phys() unless target supports it */
4872 {
4873 .name = "virt2phys",
4874 .handler = &handle_virt2phys_command,
4875 .mode = COMMAND_ANY,
4876 .help = "translate a virtual address into a physical address",
4877 },
4878
4879 {
4880 .name = "reg",
4881 .handler = &handle_reg_command,
4882 .mode = COMMAND_EXEC,
4883 .help = "display or set a register",
4884 },
4885
4886 {
4887 .name = "poll",
4888 .handler = &handle_poll_command,
4889 .mode = COMMAND_EXEC,
4890 .help = "poll target state",
4891 },
4892 {
4893 .name = "wait_halt",
4894 .handler = &handle_wait_halt_command,
4895 .mode = COMMAND_EXEC,
4896 .help = "wait for target halt",
4897 .usage = "[time (s)]",
4898 },
4899 {
4900 .name = "halt",
4901 .handler = &handle_halt_command,
4902 .mode = COMMAND_EXEC,
4903 .help = "halt target",
4904 },
4905 {
4906 .name = "resume",
4907 .handler = &handle_resume_command,
4908 .mode = COMMAND_EXEC,
4909 .help = "resume target",
4910 .usage = "[<address>]",
4911 },
4912 {
4913 .name = "reset",
4914 .handler = &handle_reset_command,
4915 .mode = COMMAND_EXEC,
4916 .usage = "[run|halt|init]",
4917 .help = "Reset all targets into the specified mode."
4918 "Default reset mode is run, if not given.",
4919 },
4920 {
4921 .name = "soft_reset_halt",
4922 .handler = &handle_soft_reset_halt_command,
4923 .mode = COMMAND_EXEC,
4924 .help = "halt the target and do a soft reset",
4925 },
4926 {
4927
4928 .name = "step",
4929 .handler = &handle_step_command,
4930 .mode = COMMAND_EXEC,
4931 .help = "step one instruction from current PC or [addr]",
4932 .usage = "[<address>]",
4933 },
4934 {
4935
4936 .name = "mdw",
4937 .handler = &handle_md_command,
4938 .mode = COMMAND_EXEC,
4939 .help = "display memory words",
4940 .usage = "[phys] <addr> [count]",
4941 },
4942 {
4943 .name = "mdh",
4944 .handler = &handle_md_command,
4945 .mode = COMMAND_EXEC,
4946 .help = "display memory half-words",
4947 .usage = "[phys] <addr> [count]",
4948 },
4949 {
4950 .name = "mdb",
4951 .handler = &handle_md_command,
4952 .mode = COMMAND_EXEC,
4953 .help = "display memory bytes",
4954 .usage = "[phys] <addr> [count]",
4955 },
4956 {
4957
4958 .name = "mww",
4959 .handler = &handle_mw_command,
4960 .mode = COMMAND_EXEC,
4961 .help = "write memory word",
4962 .usage = "[phys] <addr> <value> [count]",
4963 },
4964 {
4965 .name = "mwh",
4966 .handler = &handle_mw_command,
4967 .mode = COMMAND_EXEC,
4968 .help = "write memory half-word",
4969 .usage = "[phys] <addr> <value> [count]",
4970 },
4971 {
4972 .name = "mwb",
4973 .handler = &handle_mw_command,
4974 .mode = COMMAND_EXEC,
4975 .help = "write memory byte",
4976 .usage = "[phys] <addr> <value> [count]",
4977 },
4978 {
4979
4980 .name = "bp",
4981 .handler = &handle_bp_command,
4982 .mode = COMMAND_EXEC,
4983 .help = "list or set breakpoint",
4984 .usage = "[<address> <length> [hw]]",
4985 },
4986 {
4987 .name = "rbp",
4988 .handler = &handle_rbp_command,
4989 .mode = COMMAND_EXEC,
4990 .help = "remove breakpoint",
4991 .usage = "<address>",
4992 },
4993 {
4994
4995 .name = "wp",
4996 .handler = &handle_wp_command,
4997 .mode = COMMAND_EXEC,
4998 .help = "list or set watchpoint",
4999 .usage = "[<address> <length> <r/w/a> [value] [mask]]",
5000 },
5001 {
5002 .name = "rwp",
5003 .handler = &handle_rwp_command,
5004 .mode = COMMAND_EXEC,
5005 .help = "remove watchpoint",
5006 .usage = "<address>",
5007
5008 },
5009 {
5010 .name = "load_image",
5011 .handler = &handle_load_image_command,
5012 .mode = COMMAND_EXEC,
5013 .usage = "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
5014 "[min_address] [max_length]",
5015 },
5016 {
5017 .name = "dump_image",
5018 .handler = &handle_dump_image_command,
5019 .mode = COMMAND_EXEC,
5020 .usage = "<file> <address> <size>",
5021 },
5022 {
5023 .name = "verify_image",
5024 .handler = &handle_verify_image_command,
5025 .mode = COMMAND_EXEC,
5026 .usage = "<file> [offset] [type]",
5027 },
5028 {
5029 .name = "test_image",
5030 .handler = &handle_test_image_command,
5031 .mode = COMMAND_EXEC,
5032 .usage = "<file> [offset] [type]",
5033 },
5034 {
5035 .name = "ocd_mem2array",
5036 .mode = COMMAND_EXEC,
5037 .jim_handler = &jim_mem2array,
5038 .help = "read memory and return as a TCL array "
5039 "for script processing",
5040 .usage = "<arrayname> <width=32|16|8> <address> <count>",
5041 },
5042 {
5043 .name = "ocd_array2mem",
5044 .mode = COMMAND_EXEC,
5045 .jim_handler = &jim_array2mem,
5046 .help = "convert a TCL array to memory locations "
5047 "and write the values",
5048 .usage = "<arrayname> <width=32|16|8> <address> <count>",
5049 },
5050 COMMAND_REGISTRATION_DONE
5051 };
5052 int target_register_user_commands(struct command_context *cmd_ctx)
5053 {
5054 int retval = ERROR_OK;
5055 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
5056 return retval;
5057
5058 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
5059 return retval;
5060
5061
5062 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);
5063 }

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)