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

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)