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

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)