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

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)