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

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)