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

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)