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

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)