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

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)