jim-nvp: Make Jim_GetOpt_String const-correct
[openocd.git] / src / target / target.c
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007-2010 √ėyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
10 * *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
13 * *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
16 * *
17 * Copyright (C) 2011 by Broadcom Corporation *
18 * Evan Hunter - ehunter@broadcom.com *
19 * *
20 * Copyright (C) ST-Ericsson SA 2011 *
21 * michel.jaouen@stericsson.com : smp minimum support *
22 * *
23 * Copyright (C) 2011 Andreas Fritiofson *
24 * andreas.fritiofson@gmail.com *
25 * *
26 * This program is free software; you can redistribute it and/or modify *
27 * it under the terms of the GNU General Public License as published by *
28 * the Free Software Foundation; either version 2 of the License, or *
29 * (at your option) any later version. *
30 * *
31 * This program is distributed in the hope that it will be useful, *
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
34 * GNU General Public License for more details. *
35 * *
36 * You should have received a copy of the GNU General Public License *
37 * along with this program; if not, write to the *
38 * Free Software Foundation, Inc., *
39 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
40 ***************************************************************************/
41
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45
46 #include <helper/time_support.h>
47 #include <jtag/jtag.h>
48 #include <flash/nor/core.h>
49
50 #include "target.h"
51 #include "target_type.h"
52 #include "target_request.h"
53 #include "breakpoints.h"
54 #include "register.h"
55 #include "trace.h"
56 #include "image.h"
57 #include "rtos/rtos.h"
58 #include "transport/transport.h"
59
60 /* default halt wait timeout (ms) */
61 #define DEFAULT_HALT_TIMEOUT 5000
62
63 static int target_read_buffer_default(struct target *target, uint32_t address,
64 uint32_t count, uint8_t *buffer);
65 static int target_write_buffer_default(struct target *target, uint32_t address,
66 uint32_t count, const uint8_t *buffer);
67 static int target_array2mem(Jim_Interp *interp, struct target *target,
68 int argc, Jim_Obj * const *argv);
69 static int target_mem2array(Jim_Interp *interp, struct target *target,
70 int argc, Jim_Obj * const *argv);
71 static int target_register_user_commands(struct command_context *cmd_ctx);
72 static int target_get_gdb_fileio_info_default(struct target *target,
73 struct gdb_fileio_info *fileio_info);
74 static int target_gdb_fileio_end_default(struct target *target, int retcode,
75 int fileio_errno, bool ctrl_c);
76 static int target_profiling_default(struct target *target, uint32_t *samples,
77 uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds);
78
79 /* targets */
80 extern struct target_type arm7tdmi_target;
81 extern struct target_type arm720t_target;
82 extern struct target_type arm9tdmi_target;
83 extern struct target_type arm920t_target;
84 extern struct target_type arm966e_target;
85 extern struct target_type arm946e_target;
86 extern struct target_type arm926ejs_target;
87 extern struct target_type fa526_target;
88 extern struct target_type feroceon_target;
89 extern struct target_type dragonite_target;
90 extern struct target_type xscale_target;
91 extern struct target_type cortexm_target;
92 extern struct target_type cortexa_target;
93 extern struct target_type cortexr4_target;
94 extern struct target_type arm11_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
109 static struct target_type *target_types[] = {
110 &arm7tdmi_target,
111 &arm9tdmi_target,
112 &arm920t_target,
113 &arm720t_target,
114 &arm966e_target,
115 &arm946e_target,
116 &arm926ejs_target,
117 &fa526_target,
118 &feroceon_target,
119 &dragonite_target,
120 &xscale_target,
121 &cortexm_target,
122 &cortexa_target,
123 &cortexr4_target,
124 &arm11_target,
125 &mips_m4k_target,
126 &avr_target,
127 &dsp563xx_target,
128 &dsp5680xx_target,
129 &testee_target,
130 &avr32_ap7k_target,
131 &hla_target,
132 &nds32_v2_target,
133 &nds32_v3_target,
134 &nds32_v3m_target,
135 &or1k_target,
136 &quark_x10xx_target,
137 &quark_d20xx_target,
138 NULL,
139 };
140
141 struct target *all_targets;
142 static struct target_event_callback *target_event_callbacks;
143 static struct target_timer_callback *target_timer_callbacks;
144 LIST_HEAD(target_reset_callback_list);
145 LIST_HEAD(target_trace_callback_list);
146 static const int polling_interval = 100;
147
148 static const Jim_Nvp nvp_assert[] = {
149 { .name = "assert", NVP_ASSERT },
150 { .name = "deassert", NVP_DEASSERT },
151 { .name = "T", NVP_ASSERT },
152 { .name = "F", NVP_DEASSERT },
153 { .name = "t", NVP_ASSERT },
154 { .name = "f", NVP_DEASSERT },
155 { .name = NULL, .value = -1 }
156 };
157
158 static const Jim_Nvp nvp_error_target[] = {
159 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
160 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
161 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
162 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
163 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
164 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
165 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
166 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
167 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
168 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
169 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
170 { .value = -1, .name = NULL }
171 };
172
173 static const char *target_strerror_safe(int err)
174 {
175 const Jim_Nvp *n;
176
177 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
178 if (n->name == NULL)
179 return "unknown";
180 else
181 return n->name;
182 }
183
184 static const Jim_Nvp nvp_target_event[] = {
185
186 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
187 { .value = TARGET_EVENT_HALTED, .name = "halted" },
188 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
189 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
190 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
191
192 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
193 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
194
195 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
196 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
197 { .value = TARGET_EVENT_RESET_ASSERT, .name = "reset-assert" },
198 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
199 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
200 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
201 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
202 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
203 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
204 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
205 { .value = TARGET_EVENT_RESET_INIT, .name = "reset-init" },
206 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
207
208 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
209 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
210
211 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
212 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
213
214 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
215 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
216
217 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
218 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
219
220 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
221 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
222
223 { .value = TARGET_EVENT_TRACE_CONFIG, .name = "trace-config" },
224
225 { .name = NULL, .value = -1 }
226 };
227
228 static const Jim_Nvp nvp_target_state[] = {
229 { .name = "unknown", .value = TARGET_UNKNOWN },
230 { .name = "running", .value = TARGET_RUNNING },
231 { .name = "halted", .value = TARGET_HALTED },
232 { .name = "reset", .value = TARGET_RESET },
233 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
234 { .name = NULL, .value = -1 },
235 };
236
237 static const Jim_Nvp nvp_target_debug_reason[] = {
238 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
239 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
240 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
241 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
242 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
243 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
244 { .name = "program-exit" , .value = DBG_REASON_EXIT },
245 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
246 { .name = NULL, .value = -1 },
247 };
248
249 static const Jim_Nvp nvp_target_endian[] = {
250 { .name = "big", .value = TARGET_BIG_ENDIAN },
251 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
252 { .name = "be", .value = TARGET_BIG_ENDIAN },
253 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
254 { .name = NULL, .value = -1 },
255 };
256
257 static const Jim_Nvp nvp_reset_modes[] = {
258 { .name = "unknown", .value = RESET_UNKNOWN },
259 { .name = "run" , .value = RESET_RUN },
260 { .name = "halt" , .value = RESET_HALT },
261 { .name = "init" , .value = RESET_INIT },
262 { .name = NULL , .value = -1 },
263 };
264
265 const char *debug_reason_name(struct target *t)
266 {
267 const char *cp;
268
269 cp = Jim_Nvp_value2name_simple(nvp_target_debug_reason,
270 t->debug_reason)->name;
271 if (!cp) {
272 LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
273 cp = "(*BUG*unknown*BUG*)";
274 }
275 return cp;
276 }
277
278 const char *target_state_name(struct target *t)
279 {
280 const char *cp;
281 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
282 if (!cp) {
283 LOG_ERROR("Invalid target state: %d", (int)(t->state));
284 cp = "(*BUG*unknown*BUG*)";
285 }
286 return cp;
287 }
288
289 const char *target_event_name(enum target_event event)
290 {
291 const char *cp;
292 cp = Jim_Nvp_value2name_simple(nvp_target_event, event)->name;
293 if (!cp) {
294 LOG_ERROR("Invalid target event: %d", (int)(event));
295 cp = "(*BUG*unknown*BUG*)";
296 }
297 return cp;
298 }
299
300 const char *target_reset_mode_name(enum target_reset_mode reset_mode)
301 {
302 const char *cp;
303 cp = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode)->name;
304 if (!cp) {
305 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode));
306 cp = "(*BUG*unknown*BUG*)";
307 }
308 return cp;
309 }
310
311 /* determine the number of the new target */
312 static int new_target_number(void)
313 {
314 struct target *t;
315 int x;
316
317 /* number is 0 based */
318 x = -1;
319 t = all_targets;
320 while (t) {
321 if (x < t->target_number)
322 x = t->target_number;
323 t = t->next;
324 }
325 return x + 1;
326 }
327
328 /* read a uint64_t from a buffer in target memory endianness */
329 uint64_t target_buffer_get_u64(struct target *target, const uint8_t *buffer)
330 {
331 if (target->endianness == TARGET_LITTLE_ENDIAN)
332 return le_to_h_u64(buffer);
333 else
334 return be_to_h_u64(buffer);
335 }
336
337 /* read a uint32_t from a buffer in target memory endianness */
338 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
339 {
340 if (target->endianness == TARGET_LITTLE_ENDIAN)
341 return le_to_h_u32(buffer);
342 else
343 return be_to_h_u32(buffer);
344 }
345
346 /* read a uint24_t from a buffer in target memory endianness */
347 uint32_t target_buffer_get_u24(struct target *target, const uint8_t *buffer)
348 {
349 if (target->endianness == TARGET_LITTLE_ENDIAN)
350 return le_to_h_u24(buffer);
351 else
352 return be_to_h_u24(buffer);
353 }
354
355 /* read a uint16_t from a buffer in target memory endianness */
356 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
357 {
358 if (target->endianness == TARGET_LITTLE_ENDIAN)
359 return le_to_h_u16(buffer);
360 else
361 return be_to_h_u16(buffer);
362 }
363
364 /* read a uint8_t from a buffer in target memory endianness */
365 static uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
366 {
367 return *buffer & 0x0ff;
368 }
369
370 /* write a uint64_t to a buffer in target memory endianness */
371 void target_buffer_set_u64(struct target *target, uint8_t *buffer, uint64_t value)
372 {
373 if (target->endianness == TARGET_LITTLE_ENDIAN)
374 h_u64_to_le(buffer, value);
375 else
376 h_u64_to_be(buffer, value);
377 }
378
379 /* write a uint32_t to a buffer in target memory endianness */
380 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
381 {
382 if (target->endianness == TARGET_LITTLE_ENDIAN)
383 h_u32_to_le(buffer, value);
384 else
385 h_u32_to_be(buffer, value);
386 }
387
388 /* write a uint24_t to a buffer in target memory endianness */
389 void target_buffer_set_u24(struct target *target, uint8_t *buffer, uint32_t value)
390 {
391 if (target->endianness == TARGET_LITTLE_ENDIAN)
392 h_u24_to_le(buffer, value);
393 else
394 h_u24_to_be(buffer, value);
395 }
396
397 /* write a uint16_t to a buffer in target memory endianness */
398 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
399 {
400 if (target->endianness == TARGET_LITTLE_ENDIAN)
401 h_u16_to_le(buffer, value);
402 else
403 h_u16_to_be(buffer, value);
404 }
405
406 /* write a uint8_t to a buffer in target memory endianness */
407 static void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
408 {
409 *buffer = value;
410 }
411
412 /* write a uint64_t array to a buffer in target memory endianness */
413 void target_buffer_get_u64_array(struct target *target, const uint8_t *buffer, uint32_t count, uint64_t *dstbuf)
414 {
415 uint32_t i;
416 for (i = 0; i < count; i++)
417 dstbuf[i] = target_buffer_get_u64(target, &buffer[i * 8]);
418 }
419
420 /* write a uint32_t array to a buffer in target memory endianness */
421 void target_buffer_get_u32_array(struct target *target, const uint8_t *buffer, uint32_t count, uint32_t *dstbuf)
422 {
423 uint32_t i;
424 for (i = 0; i < count; i++)
425 dstbuf[i] = target_buffer_get_u32(target, &buffer[i * 4]);
426 }
427
428 /* write a uint16_t array to a buffer in target memory endianness */
429 void target_buffer_get_u16_array(struct target *target, const uint8_t *buffer, uint32_t count, uint16_t *dstbuf)
430 {
431 uint32_t i;
432 for (i = 0; i < count; i++)
433 dstbuf[i] = target_buffer_get_u16(target, &buffer[i * 2]);
434 }
435
436 /* write a uint64_t array to a buffer in target memory endianness */
437 void target_buffer_set_u64_array(struct target *target, uint8_t *buffer, uint32_t count, const uint64_t *srcbuf)
438 {
439 uint32_t i;
440 for (i = 0; i < count; i++)
441 target_buffer_set_u64(target, &buffer[i * 8], srcbuf[i]);
442 }
443
444 /* write a uint32_t array to a buffer in target memory endianness */
445 void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, const uint32_t *srcbuf)
446 {
447 uint32_t i;
448 for (i = 0; i < count; i++)
449 target_buffer_set_u32(target, &buffer[i * 4], srcbuf[i]);
450 }
451
452 /* write a uint16_t array to a buffer in target memory endianness */
453 void target_buffer_set_u16_array(struct target *target, uint8_t *buffer, uint32_t count, const uint16_t *srcbuf)
454 {
455 uint32_t i;
456 for (i = 0; i < count; i++)
457 target_buffer_set_u16(target, &buffer[i * 2], srcbuf[i]);
458 }
459
460 /* return a pointer to a configured target; id is name or number */
461 struct target *get_target(const char *id)
462 {
463 struct target *target;
464
465 /* try as tcltarget name */
466 for (target = all_targets; target; target = target->next) {
467 if (target_name(target) == NULL)
468 continue;
469 if (strcmp(id, target_name(target)) == 0)
470 return target;
471 }
472
473 /* It's OK to remove this fallback sometime after August 2010 or so */
474
475 /* no match, try as number */
476 unsigned num;
477 if (parse_uint(id, &num) != ERROR_OK)
478 return NULL;
479
480 for (target = all_targets; target; target = target->next) {
481 if (target->target_number == (int)num) {
482 LOG_WARNING("use '%s' as target identifier, not '%u'",
483 target_name(target), num);
484 return target;
485 }
486 }
487
488 return NULL;
489 }
490
491 /* returns a pointer to the n-th configured target */
492 struct target *get_target_by_num(int num)
493 {
494 struct target *target = all_targets;
495
496 while (target) {
497 if (target->target_number == num)
498 return target;
499 target = target->next;
500 }
501
502 return NULL;
503 }
504
505 struct target *get_current_target(struct command_context *cmd_ctx)
506 {
507 struct target *target = get_target_by_num(cmd_ctx->current_target);
508
509 if (target == NULL) {
510 LOG_ERROR("BUG: current_target out of bounds");
511 exit(-1);
512 }
513
514 return target;
515 }
516
517 int target_poll(struct target *target)
518 {
519 int retval;
520
521 /* We can't poll until after examine */
522 if (!target_was_examined(target)) {
523 /* Fail silently lest we pollute the log */
524 return ERROR_FAIL;
525 }
526
527 retval = target->type->poll(target);
528 if (retval != ERROR_OK)
529 return retval;
530
531 if (target->halt_issued) {
532 if (target->state == TARGET_HALTED)
533 target->halt_issued = false;
534 else {
535 long long t = timeval_ms() - target->halt_issued_time;
536 if (t > DEFAULT_HALT_TIMEOUT) {
537 target->halt_issued = false;
538 LOG_INFO("Halt timed out, wake up GDB.");
539 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
540 }
541 }
542 }
543
544 return ERROR_OK;
545 }
546
547 int target_halt(struct target *target)
548 {
549 int retval;
550 /* We can't poll until after examine */
551 if (!target_was_examined(target)) {
552 LOG_ERROR("Target not examined yet");
553 return ERROR_FAIL;
554 }
555
556 retval = target->type->halt(target);
557 if (retval != ERROR_OK)
558 return retval;
559
560 target->halt_issued = true;
561 target->halt_issued_time = timeval_ms();
562
563 return ERROR_OK;
564 }
565
566 /**
567 * Make the target (re)start executing using its saved execution
568 * context (possibly with some modifications).
569 *
570 * @param target Which target should start executing.
571 * @param current True to use the target's saved program counter instead
572 * of the address parameter
573 * @param address Optionally used as the program counter.
574 * @param handle_breakpoints True iff breakpoints at the resumption PC
575 * should be skipped. (For example, maybe execution was stopped by
576 * such a breakpoint, in which case it would be counterprodutive to
577 * let it re-trigger.
578 * @param debug_execution False if all working areas allocated by OpenOCD
579 * should be released and/or restored to their original contents.
580 * (This would for example be true to run some downloaded "helper"
581 * algorithm code, which resides in one such working buffer and uses
582 * another for data storage.)
583 *
584 * @todo Resolve the ambiguity about what the "debug_execution" flag
585 * signifies. For example, Target implementations don't agree on how
586 * it relates to invalidation of the register cache, or to whether
587 * breakpoints and watchpoints should be enabled. (It would seem wrong
588 * to enable breakpoints when running downloaded "helper" algorithms
589 * (debug_execution true), since the breakpoints would be set to match
590 * target firmware being debugged, not the helper algorithm.... and
591 * enabling them could cause such helpers to malfunction (for example,
592 * by overwriting data with a breakpoint instruction. On the other
593 * hand the infrastructure for running such helpers might use this
594 * procedure but rely on hardware breakpoint to detect termination.)
595 */
596 int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
597 {
598 int retval;
599
600 /* We can't poll until after examine */
601 if (!target_was_examined(target)) {
602 LOG_ERROR("Target not examined yet");
603 return ERROR_FAIL;
604 }
605
606 target_call_event_callbacks(target, TARGET_EVENT_RESUME_START);
607
608 /* note that resume *must* be asynchronous. The CPU can halt before
609 * we poll. The CPU can even halt at the current PC as a result of
610 * a software breakpoint being inserted by (a bug?) the application.
611 */
612 retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution);
613 if (retval != ERROR_OK)
614 return retval;
615
616 target_call_event_callbacks(target, TARGET_EVENT_RESUME_END);
617
618 return retval;
619 }
620
621 static int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
622 {
623 char buf[100];
624 int retval;
625 Jim_Nvp *n;
626 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
627 if (n->name == NULL) {
628 LOG_ERROR("invalid reset mode");
629 return ERROR_FAIL;
630 }
631
632 struct target *target;
633 for (target = all_targets; target; target = target->next)
634 target_call_reset_callbacks(target, reset_mode);
635
636 /* disable polling during reset to make reset event scripts
637 * more predictable, i.e. dr/irscan & pathmove in events will
638 * not have JTAG operations injected into the middle of a sequence.
639 */
640 bool save_poll = jtag_poll_get_enabled();
641
642 jtag_poll_set_enabled(false);
643
644 sprintf(buf, "ocd_process_reset %s", n->name);
645 retval = Jim_Eval(cmd_ctx->interp, buf);
646
647 jtag_poll_set_enabled(save_poll);
648
649 if (retval != JIM_OK) {
650 Jim_MakeErrorMessage(cmd_ctx->interp);
651 command_print(NULL, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx->interp), NULL));
652 return ERROR_FAIL;
653 }
654
655 /* We want any events to be processed before the prompt */
656 retval = target_call_timer_callbacks_now();
657
658 for (target = all_targets; target; target = target->next) {
659 target->type->check_reset(target);
660 target->running_alg = false;
661 }
662
663 return retval;
664 }
665
666 static int identity_virt2phys(struct target *target,
667 uint32_t virtual, uint32_t *physical)
668 {
669 *physical = virtual;
670 return ERROR_OK;
671 }
672
673 static int no_mmu(struct target *target, int *enabled)
674 {
675 *enabled = 0;
676 return ERROR_OK;
677 }
678
679 static int default_examine(struct target *target)
680 {
681 target_set_examined(target);
682 return ERROR_OK;
683 }
684
685 /* no check by default */
686 static int default_check_reset(struct target *target)
687 {
688 return ERROR_OK;
689 }
690
691 int target_examine_one(struct target *target)
692 {
693 target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_START);
694
695 int retval = target->type->examine(target);
696 if (retval != ERROR_OK)
697 return retval;
698
699 target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_END);
700
701 return ERROR_OK;
702 }
703
704 static int jtag_enable_callback(enum jtag_event event, void *priv)
705 {
706 struct target *target = priv;
707
708 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
709 return ERROR_OK;
710
711 jtag_unregister_event_callback(jtag_enable_callback, target);
712
713 return target_examine_one(target);
714 }
715
716 /* Targets that correctly implement init + examine, i.e.
717 * no communication with target during init:
718 *
719 * XScale
720 */
721 int target_examine(void)
722 {
723 int retval = ERROR_OK;
724 struct target *target;
725
726 for (target = all_targets; target; target = target->next) {
727 /* defer examination, but don't skip it */
728 if (!target->tap->enabled) {
729 jtag_register_event_callback(jtag_enable_callback,
730 target);
731 continue;
732 }
733
734 retval = target_examine_one(target);
735 if (retval != ERROR_OK)
736 return retval;
737 }
738 return retval;
739 }
740
741 const char *target_type_name(struct target *target)
742 {
743 return target->type->name;
744 }
745
746 static int target_soft_reset_halt(struct target *target)
747 {
748 if (!target_was_examined(target)) {
749 LOG_ERROR("Target not examined yet");
750 return ERROR_FAIL;
751 }
752 if (!target->type->soft_reset_halt) {
753 LOG_ERROR("Target %s does not support soft_reset_halt",
754 target_name(target));
755 return ERROR_FAIL;
756 }
757 return target->type->soft_reset_halt(target);
758 }
759
760 /**
761 * Downloads a target-specific native code algorithm to the target,
762 * and executes it. * Note that some targets may need to set up, enable,
763 * and tear down a breakpoint (hard or * soft) to detect algorithm
764 * termination, while others may support lower overhead schemes where
765 * soft breakpoints embedded in the algorithm automatically terminate the
766 * algorithm.
767 *
768 * @param target used to run the algorithm
769 * @param arch_info target-specific description of the algorithm.
770 */
771 int target_run_algorithm(struct target *target,
772 int num_mem_params, struct mem_param *mem_params,
773 int num_reg_params, struct reg_param *reg_param,
774 uint32_t entry_point, uint32_t exit_point,
775 int timeout_ms, void *arch_info)
776 {
777 int retval = ERROR_FAIL;
778
779 if (!target_was_examined(target)) {
780 LOG_ERROR("Target not examined yet");
781 goto done;
782 }
783 if (!target->type->run_algorithm) {
784 LOG_ERROR("Target type '%s' does not support %s",
785 target_type_name(target), __func__);
786 goto done;
787 }
788
789 target->running_alg = true;
790 retval = target->type->run_algorithm(target,
791 num_mem_params, mem_params,
792 num_reg_params, reg_param,
793 entry_point, exit_point, timeout_ms, arch_info);
794 target->running_alg = false;
795
796 done:
797 return retval;
798 }
799
800 /**
801 * Downloads a target-specific native code algorithm to the target,
802 * executes and leaves it running.
803 *
804 * @param target used to run the algorithm
805 * @param arch_info target-specific description of the algorithm.
806 */
807 int target_start_algorithm(struct target *target,
808 int num_mem_params, struct mem_param *mem_params,
809 int num_reg_params, struct reg_param *reg_params,
810 uint32_t entry_point, uint32_t exit_point,
811 void *arch_info)
812 {
813 int retval = ERROR_FAIL;
814
815 if (!target_was_examined(target)) {
816 LOG_ERROR("Target not examined yet");
817 goto done;
818 }
819 if (!target->type->start_algorithm) {
820 LOG_ERROR("Target type '%s' does not support %s",
821 target_type_name(target), __func__);
822 goto done;
823 }
824 if (target->running_alg) {
825 LOG_ERROR("Target is already running an algorithm");
826 goto done;
827 }
828
829 target->running_alg = true;
830 retval = target->type->start_algorithm(target,
831 num_mem_params, mem_params,
832 num_reg_params, reg_params,
833 entry_point, exit_point, arch_info);
834
835 done:
836 return retval;
837 }
838
839 /**
840 * Waits for an algorithm started with target_start_algorithm() to complete.
841 *
842 * @param target used to run the algorithm
843 * @param arch_info target-specific description of the algorithm.
844 */
845 int target_wait_algorithm(struct target *target,
846 int num_mem_params, struct mem_param *mem_params,
847 int num_reg_params, struct reg_param *reg_params,
848 uint32_t exit_point, int timeout_ms,
849 void *arch_info)
850 {
851 int retval = ERROR_FAIL;
852
853 if (!target->type->wait_algorithm) {
854 LOG_ERROR("Target type '%s' does not support %s",
855 target_type_name(target), __func__);
856 goto done;
857 }
858 if (!target->running_alg) {
859 LOG_ERROR("Target is not running an algorithm");
860 goto done;
861 }
862
863 retval = target->type->wait_algorithm(target,
864 num_mem_params, mem_params,
865 num_reg_params, reg_params,
866 exit_point, timeout_ms, arch_info);
867 if (retval != ERROR_TARGET_TIMEOUT)
868 target->running_alg = false;
869
870 done:
871 return retval;
872 }
873
874 /**
875 * Executes a target-specific native code algorithm in the target.
876 * It differs from target_run_algorithm in that the algorithm is asynchronous.
877 * Because of this it requires an compliant algorithm:
878 * see contrib/loaders/flash/stm32f1x.S for example.
879 *
880 * @param target used to run the algorithm
881 */
882
883 int target_run_flash_async_algorithm(struct target *target,
884 const uint8_t *buffer, uint32_t count, int block_size,
885 int num_mem_params, struct mem_param *mem_params,
886 int num_reg_params, struct reg_param *reg_params,
887 uint32_t buffer_start, uint32_t buffer_size,
888 uint32_t entry_point, uint32_t exit_point, void *arch_info)
889 {
890 int retval;
891 int timeout = 0;
892
893 const uint8_t *buffer_orig = buffer;
894
895 /* Set up working area. First word is write pointer, second word is read pointer,
896 * rest is fifo data area. */
897 uint32_t wp_addr = buffer_start;
898 uint32_t rp_addr = buffer_start + 4;
899 uint32_t fifo_start_addr = buffer_start + 8;
900 uint32_t fifo_end_addr = buffer_start + buffer_size;
901
902 uint32_t wp = fifo_start_addr;
903 uint32_t rp = fifo_start_addr;
904
905 /* validate block_size is 2^n */
906 assert(!block_size || !(block_size & (block_size - 1)));
907
908 retval = target_write_u32(target, wp_addr, wp);
909 if (retval != ERROR_OK)
910 return retval;
911 retval = target_write_u32(target, rp_addr, rp);
912 if (retval != ERROR_OK)
913 return retval;
914
915 /* Start up algorithm on target and let it idle while writing the first chunk */
916 retval = target_start_algorithm(target, num_mem_params, mem_params,
917 num_reg_params, reg_params,
918 entry_point,
919 exit_point,
920 arch_info);
921
922 if (retval != ERROR_OK) {
923 LOG_ERROR("error starting target flash write algorithm");
924 return retval;
925 }
926
927 while (count > 0) {
928
929 retval = target_read_u32(target, rp_addr, &rp);
930 if (retval != ERROR_OK) {
931 LOG_ERROR("failed to get read pointer");
932 break;
933 }
934
935 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32 " wp 0x%" PRIx32 " rp 0x%" PRIx32,
936 (size_t) (buffer - buffer_orig), count, wp, rp);
937
938 if (rp == 0) {
939 LOG_ERROR("flash write algorithm aborted by target");
940 retval = ERROR_FLASH_OPERATION_FAILED;
941 break;
942 }
943
944 if (((rp - fifo_start_addr) & (block_size - 1)) || rp < fifo_start_addr || rp >= fifo_end_addr) {
945 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32, rp);
946 break;
947 }
948
949 /* Count the number of bytes available in the fifo without
950 * crossing the wrap around. Make sure to not fill it completely,
951 * because that would make wp == rp and that's the empty condition. */
952 uint32_t thisrun_bytes;
953 if (rp > wp)
954 thisrun_bytes = rp - wp - block_size;
955 else if (rp > fifo_start_addr)
956 thisrun_bytes = fifo_end_addr - wp;
957 else
958 thisrun_bytes = fifo_end_addr - wp - block_size;
959
960 if (thisrun_bytes == 0) {
961 /* Throttle polling a bit if transfer is (much) faster than flash
962 * programming. The exact delay shouldn't matter as long as it's
963 * less than buffer size / flash speed. This is very unlikely to
964 * run when using high latency connections such as USB. */
965 alive_sleep(10);
966
967 /* to stop an infinite loop on some targets check and increment a timeout
968 * this issue was observed on a stellaris using the new ICDI interface */
969 if (timeout++ >= 500) {
970 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
971 return ERROR_FLASH_OPERATION_FAILED;
972 }
973 continue;
974 }
975
976 /* reset our timeout */
977 timeout = 0;
978
979 /* Limit to the amount of data we actually want to write */
980 if (thisrun_bytes > count * block_size)
981 thisrun_bytes = count * block_size;
982
983 /* Write data to fifo */
984 retval = target_write_buffer(target, wp, thisrun_bytes, buffer);
985 if (retval != ERROR_OK)
986 break;
987
988 /* Update counters and wrap write pointer */
989 buffer += thisrun_bytes;
990 count -= thisrun_bytes / block_size;
991 wp += thisrun_bytes;
992 if (wp >= fifo_end_addr)
993 wp = fifo_start_addr;
994
995 /* Store updated write pointer to target */
996 retval = target_write_u32(target, wp_addr, wp);
997 if (retval != ERROR_OK)
998 break;
999 }
1000
1001 if (retval != ERROR_OK) {
1002 /* abort flash write algorithm on target */
1003 target_write_u32(target, wp_addr, 0);
1004 }
1005
1006 int retval2 = target_wait_algorithm(target, num_mem_params, mem_params,
1007 num_reg_params, reg_params,
1008 exit_point,
1009 10000,
1010 arch_info);
1011
1012 if (retval2 != ERROR_OK) {
1013 LOG_ERROR("error waiting for target flash write algorithm");
1014 retval = retval2;
1015 }
1016
1017 return retval;
1018 }
1019
1020 int target_read_memory(struct target *target,
1021 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
1022 {
1023 if (!target_was_examined(target)) {
1024 LOG_ERROR("Target not examined yet");
1025 return ERROR_FAIL;
1026 }
1027 if (!target->type->read_memory) {
1028 LOG_ERROR("Target %s doesn't support read_memory", target_name(target));
1029 return ERROR_FAIL;
1030 }
1031 return target->type->read_memory(target, address, size, count, buffer);
1032 }
1033
1034 int target_read_phys_memory(struct target *target,
1035 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
1036 {
1037 if (!target_was_examined(target)) {
1038 LOG_ERROR("Target not examined yet");
1039 return ERROR_FAIL;
1040 }
1041 if (!target->type->read_phys_memory) {
1042 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target));
1043 return ERROR_FAIL;
1044 }
1045 return target->type->read_phys_memory(target, address, size, count, buffer);
1046 }
1047
1048 int target_write_memory(struct target *target,
1049 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
1050 {
1051 if (!target_was_examined(target)) {
1052 LOG_ERROR("Target not examined yet");
1053 return ERROR_FAIL;
1054 }
1055 if (!target->type->write_memory) {
1056 LOG_ERROR("Target %s doesn't support write_memory", target_name(target));
1057 return ERROR_FAIL;
1058 }
1059 return target->type->write_memory(target, address, size, count, buffer);
1060 }
1061
1062 int target_write_phys_memory(struct target *target,
1063 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
1064 {
1065 if (!target_was_examined(target)) {
1066 LOG_ERROR("Target not examined yet");
1067 return ERROR_FAIL;
1068 }
1069 if (!target->type->write_phys_memory) {
1070 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target));
1071 return ERROR_FAIL;
1072 }
1073 return target->type->write_phys_memory(target, address, size, count, buffer);
1074 }
1075
1076 int target_add_breakpoint(struct target *target,
1077 struct breakpoint *breakpoint)
1078 {
1079 if ((target->state != TARGET_HALTED) && (breakpoint->type != BKPT_HARD)) {
1080 LOG_WARNING("target %s is not halted", target_name(target));
1081 return ERROR_TARGET_NOT_HALTED;
1082 }
1083 return target->type->add_breakpoint(target, breakpoint);
1084 }
1085
1086 int target_add_context_breakpoint(struct target *target,
1087 struct breakpoint *breakpoint)
1088 {
1089 if (target->state != TARGET_HALTED) {
1090 LOG_WARNING("target %s is not halted", target_name(target));
1091 return ERROR_TARGET_NOT_HALTED;
1092 }
1093 return target->type->add_context_breakpoint(target, breakpoint);
1094 }
1095
1096 int target_add_hybrid_breakpoint(struct target *target,
1097 struct breakpoint *breakpoint)
1098 {
1099 if (target->state != TARGET_HALTED) {
1100 LOG_WARNING("target %s is not halted", target_name(target));
1101 return ERROR_TARGET_NOT_HALTED;
1102 }
1103 return target->type->add_hybrid_breakpoint(target, breakpoint);
1104 }
1105
1106 int target_remove_breakpoint(struct target *target,
1107 struct breakpoint *breakpoint)
1108 {
1109 return target->type->remove_breakpoint(target, breakpoint);
1110 }
1111
1112 int target_add_watchpoint(struct target *target,
1113 struct watchpoint *watchpoint)
1114 {
1115 if (target->state != TARGET_HALTED) {
1116 LOG_WARNING("target %s is not halted", target_name(target));
1117 return ERROR_TARGET_NOT_HALTED;
1118 }
1119 return target->type->add_watchpoint(target, watchpoint);
1120 }
1121 int target_remove_watchpoint(struct target *target,
1122 struct watchpoint *watchpoint)
1123 {
1124 return target->type->remove_watchpoint(target, watchpoint);
1125 }
1126 int target_hit_watchpoint(struct target *target,
1127 struct watchpoint **hit_watchpoint)
1128 {
1129 if (target->state != TARGET_HALTED) {
1130 LOG_WARNING("target %s is not halted", target->cmd_name);
1131 return ERROR_TARGET_NOT_HALTED;
1132 }
1133
1134 if (target->type->hit_watchpoint == NULL) {
1135 /* For backward compatible, if hit_watchpoint is not implemented,
1136 * return ERROR_FAIL such that gdb_server will not take the nonsense
1137 * information. */
1138 return ERROR_FAIL;
1139 }
1140
1141 return target->type->hit_watchpoint(target, hit_watchpoint);
1142 }
1143
1144 int target_get_gdb_reg_list(struct target *target,
1145 struct reg **reg_list[], int *reg_list_size,
1146 enum target_register_class reg_class)
1147 {
1148 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size, reg_class);
1149 }
1150 int target_step(struct target *target,
1151 int current, uint32_t address, int handle_breakpoints)
1152 {
1153 return target->type->step(target, current, address, handle_breakpoints);
1154 }
1155
1156 int target_get_gdb_fileio_info(struct target *target, struct gdb_fileio_info *fileio_info)
1157 {
1158 if (target->state != TARGET_HALTED) {
1159 LOG_WARNING("target %s is not halted", target->cmd_name);
1160 return ERROR_TARGET_NOT_HALTED;
1161 }
1162 return target->type->get_gdb_fileio_info(target, fileio_info);
1163 }
1164
1165 int target_gdb_fileio_end(struct target *target, int retcode, int fileio_errno, bool ctrl_c)
1166 {
1167 if (target->state != TARGET_HALTED) {
1168 LOG_WARNING("target %s is not halted", target->cmd_name);
1169 return ERROR_TARGET_NOT_HALTED;
1170 }
1171 return target->type->gdb_fileio_end(target, retcode, fileio_errno, ctrl_c);
1172 }
1173
1174 int target_profiling(struct target *target, uint32_t *samples,
1175 uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
1176 {
1177 if (target->state != TARGET_HALTED) {
1178 LOG_WARNING("target %s is not halted", target->cmd_name);
1179 return ERROR_TARGET_NOT_HALTED;
1180 }
1181 return target->type->profiling(target, samples, max_num_samples,
1182 num_samples, seconds);
1183 }
1184
1185 /**
1186 * Reset the @c examined flag for the given target.
1187 * Pure paranoia -- targets are zeroed on allocation.
1188 */
1189 static void target_reset_examined(struct target *target)
1190 {
1191 target->examined = false;
1192 }
1193
1194 static int handle_target(void *priv);
1195
1196 static int target_init_one(struct command_context *cmd_ctx,
1197 struct target *target)
1198 {
1199 target_reset_examined(target);
1200
1201 struct target_type *type = target->type;
1202 if (type->examine == NULL)
1203 type->examine = default_examine;
1204
1205 if (type->check_reset == NULL)
1206 type->check_reset = default_check_reset;
1207
1208 assert(type->init_target != NULL);
1209
1210 int retval = type->init_target(cmd_ctx, target);
1211 if (ERROR_OK != retval) {
1212 LOG_ERROR("target '%s' init failed", target_name(target));
1213 return retval;
1214 }
1215
1216 /* Sanity-check MMU support ... stub in what we must, to help
1217 * implement it in stages, but warn if we need to do so.
1218 */
1219 if (type->mmu) {
1220 if (type->virt2phys == NULL) {
1221 LOG_ERROR("type '%s' is missing virt2phys", type->name);
1222 type->virt2phys = identity_virt2phys;
1223 }
1224 } else {
1225 /* Make sure no-MMU targets all behave the same: make no
1226 * distinction between physical and virtual addresses, and
1227 * ensure that virt2phys() is always an identity mapping.
1228 */
1229 if (type->write_phys_memory || type->read_phys_memory || type->virt2phys)
1230 LOG_WARNING("type '%s' has bad MMU hooks", type->name);
1231
1232 type->mmu = no_mmu;
1233 type->write_phys_memory = type->write_memory;
1234 type->read_phys_memory = type->read_memory;
1235 type->virt2phys = identity_virt2phys;
1236 }
1237
1238 if (target->type->read_buffer == NULL)
1239 target->type->read_buffer = target_read_buffer_default;
1240
1241 if (target->type->write_buffer == NULL)
1242 target->type->write_buffer = target_write_buffer_default;
1243
1244 if (target->type->get_gdb_fileio_info == NULL)
1245 target->type->get_gdb_fileio_info = target_get_gdb_fileio_info_default;
1246
1247 if (target->type->gdb_fileio_end == NULL)
1248 target->type->gdb_fileio_end = target_gdb_fileio_end_default;
1249
1250 if (target->type->profiling == NULL)
1251 target->type->profiling = target_profiling_default;
1252
1253 return ERROR_OK;
1254 }
1255
1256 static int target_init(struct command_context *cmd_ctx)
1257 {
1258 struct target *target;
1259 int retval;
1260
1261 for (target = all_targets; target; target = target->next) {
1262 retval = target_init_one(cmd_ctx, target);
1263 if (ERROR_OK != retval)
1264 return retval;
1265 }
1266
1267 if (!all_targets)
1268 return ERROR_OK;
1269
1270 retval = target_register_user_commands(cmd_ctx);
1271 if (ERROR_OK != retval)
1272 return retval;
1273
1274 retval = target_register_timer_callback(&handle_target,
1275 polling_interval, 1, cmd_ctx->interp);
1276 if (ERROR_OK != retval)
1277 return retval;
1278
1279 return ERROR_OK;
1280 }
1281
1282 COMMAND_HANDLER(handle_target_init_command)
1283 {
1284 int retval;
1285
1286 if (CMD_ARGC != 0)
1287 return ERROR_COMMAND_SYNTAX_ERROR;
1288
1289 static bool target_initialized;
1290 if (target_initialized) {
1291 LOG_INFO("'target init' has already been called");
1292 return ERROR_OK;
1293 }
1294 target_initialized = true;
1295
1296 retval = command_run_line(CMD_CTX, "init_targets");
1297 if (ERROR_OK != retval)
1298 return retval;
1299
1300 retval = command_run_line(CMD_CTX, "init_target_events");
1301 if (ERROR_OK != retval)
1302 return retval;
1303
1304 retval = command_run_line(CMD_CTX, "init_board");
1305 if (ERROR_OK != retval)
1306 return retval;
1307
1308 LOG_DEBUG("Initializing targets...");
1309 return target_init(CMD_CTX);
1310 }
1311
1312 int target_register_event_callback(int (*callback)(struct target *target,
1313 enum target_event event, void *priv), void *priv)
1314 {
1315 struct target_event_callback **callbacks_p = &target_event_callbacks;
1316
1317 if (callback == NULL)
1318 return ERROR_COMMAND_SYNTAX_ERROR;
1319
1320 if (*callbacks_p) {
1321 while ((*callbacks_p)->next)
1322 callbacks_p = &((*callbacks_p)->next);
1323 callbacks_p = &((*callbacks_p)->next);
1324 }
1325
1326 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
1327 (*callbacks_p)->callback = callback;
1328 (*callbacks_p)->priv = priv;
1329 (*callbacks_p)->next = NULL;
1330
1331 return ERROR_OK;
1332 }
1333
1334 int target_register_reset_callback(int (*callback)(struct target *target,
1335 enum target_reset_mode reset_mode, void *priv), void *priv)
1336 {
1337 struct target_reset_callback *entry;
1338
1339 if (callback == NULL)
1340 return ERROR_COMMAND_SYNTAX_ERROR;
1341
1342 entry = malloc(sizeof(struct target_reset_callback));
1343 if (entry == NULL) {
1344 LOG_ERROR("error allocating buffer for reset callback entry");
1345 return ERROR_COMMAND_SYNTAX_ERROR;
1346 }
1347
1348 entry->callback = callback;
1349 entry->priv = priv;
1350 list_add(&entry->list, &target_reset_callback_list);
1351
1352
1353 return ERROR_OK;
1354 }
1355
1356 int target_register_trace_callback(int (*callback)(struct target *target,
1357 size_t len, uint8_t *data, void *priv), void *priv)
1358 {
1359 struct target_trace_callback *entry;
1360
1361 if (callback == NULL)
1362 return ERROR_COMMAND_SYNTAX_ERROR;
1363
1364 entry = malloc(sizeof(struct target_trace_callback));
1365 if (entry == NULL) {
1366 LOG_ERROR("error allocating buffer for trace callback entry");
1367 return ERROR_COMMAND_SYNTAX_ERROR;
1368 }
1369
1370 entry->callback = callback;
1371 entry->priv = priv;
1372 list_add(&entry->list, &target_trace_callback_list);
1373
1374
1375 return ERROR_OK;
1376 }
1377
1378 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
1379 {
1380 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
1381 struct timeval now;
1382
1383 if (callback == NULL)
1384 return ERROR_COMMAND_SYNTAX_ERROR;
1385
1386 if (*callbacks_p) {
1387 while ((*callbacks_p)->next)
1388 callbacks_p = &((*callbacks_p)->next);
1389 callbacks_p = &((*callbacks_p)->next);
1390 }
1391
1392 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
1393 (*callbacks_p)->callback = callback;
1394 (*callbacks_p)->periodic = periodic;
1395 (*callbacks_p)->time_ms = time_ms;
1396 (*callbacks_p)->removed = false;
1397
1398 gettimeofday(&now, NULL);
1399 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
1400 time_ms -= (time_ms % 1000);
1401 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
1402 if ((*callbacks_p)->when.tv_usec > 1000000) {
1403 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
1404 (*callbacks_p)->when.tv_sec += 1;
1405 }
1406
1407 (*callbacks_p)->priv = priv;
1408 (*callbacks_p)->next = NULL;
1409
1410 return ERROR_OK;
1411 }
1412
1413 int target_unregister_event_callback(int (*callback)(struct target *target,
1414 enum target_event event, void *priv), void *priv)
1415 {
1416 struct target_event_callback **p = &target_event_callbacks;
1417 struct target_event_callback *c = target_event_callbacks;
1418
1419 if (callback == NULL)
1420 return ERROR_COMMAND_SYNTAX_ERROR;
1421
1422 while (c) {
1423 struct target_event_callback *next = c->next;
1424 if ((c->callback == callback) && (c->priv == priv)) {
1425 *p = next;
1426 free(c);
1427 return ERROR_OK;
1428 } else
1429 p = &(c->next);
1430 c = next;
1431 }
1432
1433 return ERROR_OK;
1434 }
1435
1436 int target_unregister_reset_callback(int (*callback)(struct target *target,
1437 enum target_reset_mode reset_mode, void *priv), void *priv)
1438 {
1439 struct target_reset_callback *entry;
1440
1441 if (callback == NULL)
1442 return ERROR_COMMAND_SYNTAX_ERROR;
1443
1444 list_for_each_entry(entry, &target_reset_callback_list, list) {
1445 if (entry->callback == callback && entry->priv == priv) {
1446 list_del(&entry->list);
1447 free(entry);
1448 break;
1449 }
1450 }
1451
1452 return ERROR_OK;
1453 }
1454
1455 int target_unregister_trace_callback(int (*callback)(struct target *target,
1456 size_t len, uint8_t *data, void *priv), void *priv)
1457 {
1458 struct target_trace_callback *entry;
1459
1460 if (callback == NULL)
1461 return ERROR_COMMAND_SYNTAX_ERROR;
1462
1463 list_for_each_entry(entry, &target_trace_callback_list, list) {
1464 if (entry->callback == callback && entry->priv == priv) {
1465 list_del(&entry->list);
1466 free(entry);
1467 break;
1468 }
1469 }
1470
1471 return ERROR_OK;
1472 }
1473
1474 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
1475 {
1476 if (callback == NULL)
1477 return ERROR_COMMAND_SYNTAX_ERROR;
1478
1479 for (struct target_timer_callback *c = target_timer_callbacks;
1480 c; c = c->next) {
1481 if ((c->callback == callback) && (c->priv == priv)) {
1482 c->removed = true;
1483 return ERROR_OK;
1484 }
1485 }
1486
1487 return ERROR_FAIL;
1488 }
1489
1490 int target_call_event_callbacks(struct target *target, enum target_event event)
1491 {
1492 struct target_event_callback *callback = target_event_callbacks;
1493 struct target_event_callback *next_callback;
1494
1495 if (event == TARGET_EVENT_HALTED) {
1496 /* execute early halted first */
1497 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1498 }
1499
1500 LOG_DEBUG("target event %i (%s)", event,
1501 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
1502
1503 target_handle_event(target, event);
1504
1505 while (callback) {
1506 next_callback = callback->next;
1507 callback->callback(target, event, callback->priv);
1508 callback = next_callback;
1509 }
1510
1511 return ERROR_OK;
1512 }
1513
1514 int target_call_reset_callbacks(struct target *target, enum target_reset_mode reset_mode)
1515 {
1516 struct target_reset_callback *callback;
1517
1518 LOG_DEBUG("target reset %i (%s)", reset_mode,
1519 Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode)->name);
1520
1521 list_for_each_entry(callback, &target_reset_callback_list, list)
1522 callback->callback(target, reset_mode, callback->priv);
1523
1524 return ERROR_OK;
1525 }
1526
1527 int target_call_trace_callbacks(struct target *target, size_t len, uint8_t *data)
1528 {
1529 struct target_trace_callback *callback;
1530
1531 list_for_each_entry(callback, &target_trace_callback_list, list)
1532 callback->callback(target, len, data, callback->priv);
1533
1534 return ERROR_OK;
1535 }
1536
1537 static int target_timer_callback_periodic_restart(
1538 struct target_timer_callback *cb, struct timeval *now)
1539 {
1540 int time_ms = cb->time_ms;
1541 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
1542 time_ms -= (time_ms % 1000);
1543 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
1544 if (cb->when.tv_usec > 1000000) {
1545 cb->when.tv_usec = cb->when.tv_usec - 1000000;
1546 cb->when.tv_sec += 1;
1547 }
1548 return ERROR_OK;
1549 }
1550
1551 static int target_call_timer_callback(struct target_timer_callback *cb,
1552 struct timeval *now)
1553 {
1554 cb->callback(cb->priv);
1555
1556 if (cb->periodic)
1557 return target_timer_callback_periodic_restart(cb, now);
1558
1559 return target_unregister_timer_callback(cb->callback, cb->priv);
1560 }
1561
1562 static int target_call_timer_callbacks_check_time(int checktime)
1563 {
1564 static bool callback_processing;
1565
1566 /* Do not allow nesting */
1567 if (callback_processing)
1568 return ERROR_OK;
1569
1570 callback_processing = true;
1571
1572 keep_alive();
1573
1574 struct timeval now;
1575 gettimeofday(&now, NULL);
1576
1577 /* Store an address of the place containing a pointer to the
1578 * next item; initially, that's a standalone "root of the
1579 * list" variable. */
1580 struct target_timer_callback **callback = &target_timer_callbacks;
1581 while (*callback) {
1582 if ((*callback)->removed) {
1583 struct target_timer_callback *p = *callback;
1584 *callback = (*callback)->next;
1585 free(p);
1586 continue;
1587 }
1588
1589 bool call_it = (*callback)->callback &&
1590 ((!checktime && (*callback)->periodic) ||
1591 now.tv_sec > (*callback)->when.tv_sec ||
1592 (now.tv_sec == (*callback)->when.tv_sec &&
1593 now.tv_usec >= (*callback)->when.tv_usec));
1594
1595 if (call_it)
1596 target_call_timer_callback(*callback, &now);
1597
1598 callback = &(*callback)->next;
1599 }
1600
1601 callback_processing = false;
1602 return ERROR_OK;
1603 }
1604
1605 int target_call_timer_callbacks(void)
1606 {
1607 return target_call_timer_callbacks_check_time(1);
1608 }
1609
1610 /* invoke periodic callbacks immediately */
1611 int target_call_timer_callbacks_now(void)
1612 {
1613 return target_call_timer_callbacks_check_time(0);
1614 }
1615
1616 /* Prints the working area layout for debug purposes */
1617 static void print_wa_layout(struct target *target)
1618 {
1619 struct working_area *c = target->working_areas;
1620
1621 while (c) {
1622 LOG_DEBUG("%c%c 0x%08"PRIx32"-0x%08"PRIx32" (%"PRIu32" bytes)",
1623 c->backup ? 'b' : ' ', c->free ? ' ' : '*',
1624 c->address, c->address + c->size - 1, c->size);
1625 c = c->next;
1626 }
1627 }
1628
1629 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1630 static void target_split_working_area(struct working_area *area, uint32_t size)
1631 {
1632 assert(area->free); /* Shouldn't split an allocated area */
1633 assert(size <= area->size); /* Caller should guarantee this */
1634
1635 /* Split only if not already the right size */
1636 if (size < area->size) {
1637 struct working_area *new_wa = malloc(sizeof(*new_wa));
1638
1639 if (new_wa == NULL)
1640 return;
1641
1642 new_wa->next = area->next;
1643 new_wa->size = area->size - size;
1644 new_wa->address = area->address + size;
1645 new_wa->backup = NULL;
1646 new_wa->user = NULL;
1647 new_wa->free = true;
1648
1649 area->next = new_wa;
1650 area->size = size;
1651
1652 /* If backup memory was allocated to this area, it has the wrong size
1653 * now so free it and it will be reallocated if/when needed */
1654 if (area->backup) {
1655 free(area->backup);
1656 area->backup = NULL;
1657 }
1658 }
1659 }
1660
1661 /* Merge all adjacent free areas into one */
1662 static void target_merge_working_areas(struct target *target)
1663 {
1664 struct working_area *c = target->working_areas;
1665
1666 while (c && c->next) {
1667 assert(c->next->address == c->address + c->size); /* This is an invariant */
1668
1669 /* Find two adjacent free areas */
1670 if (c->free && c->next->free) {
1671 /* Merge the last into the first */
1672 c->size += c->next->size;
1673
1674 /* Remove the last */
1675 struct working_area *to_be_freed = c->next;
1676 c->next = c->next->next;
1677 if (to_be_freed->backup)
1678 free(to_be_freed->backup);
1679 free(to_be_freed);
1680
1681 /* If backup memory was allocated to the remaining area, it's has
1682 * the wrong size now */
1683 if (c->backup) {
1684 free(c->backup);
1685 c->backup = NULL;
1686 }
1687 } else {
1688 c = c->next;
1689 }
1690 }
1691 }
1692
1693 int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
1694 {
1695 /* Reevaluate working area address based on MMU state*/
1696 if (target->working_areas == NULL) {
1697 int retval;
1698 int enabled;
1699
1700 retval = target->type->mmu(target, &enabled);
1701 if (retval != ERROR_OK)
1702 return retval;
1703
1704 if (!enabled) {
1705 if (target->working_area_phys_spec) {
1706 LOG_DEBUG("MMU disabled, using physical "
1707 "address for working memory 0x%08"PRIx32,
1708 target->working_area_phys);
1709 target->working_area = target->working_area_phys;
1710 } else {
1711 LOG_ERROR("No working memory available. "
1712 "Specify -work-area-phys to target.");
1713 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1714 }
1715 } else {
1716 if (target->working_area_virt_spec) {
1717 LOG_DEBUG("MMU enabled, using virtual "
1718 "address for working memory 0x%08"PRIx32,
1719 target->working_area_virt);
1720 target->working_area = target->working_area_virt;
1721 } else {
1722 LOG_ERROR("No working memory available. "
1723 "Specify -work-area-virt to target.");
1724 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1725 }
1726 }
1727
1728 /* Set up initial working area on first call */
1729 struct working_area *new_wa = malloc(sizeof(*new_wa));
1730 if (new_wa) {
1731 new_wa->next = NULL;
1732 new_wa->size = target->working_area_size & ~3UL; /* 4-byte align */
1733 new_wa->address = target->working_area;
1734 new_wa->backup = NULL;
1735 new_wa->user = NULL;
1736 new_wa->free = true;
1737 }
1738
1739 target->working_areas = new_wa;
1740 }
1741
1742 /* only allocate multiples of 4 byte */
1743 if (size % 4)
1744 size = (size + 3) & (~3UL);
1745
1746 struct working_area *c = target->working_areas;
1747
1748 /* Find the first large enough working area */
1749 while (c) {
1750 if (c->free && c->size >= size)
1751 break;
1752 c = c->next;
1753 }
1754
1755 if (c == NULL)
1756 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1757
1758 /* Split the working area into the requested size */
1759 target_split_working_area(c, size);
1760
1761 LOG_DEBUG("allocated new working area of %"PRIu32" bytes at address 0x%08"PRIx32, size, c->address);
1762
1763 if (target->backup_working_area) {
1764 if (c->backup == NULL) {
1765 c->backup = malloc(c->size);
1766 if (c->backup == NULL)
1767 return ERROR_FAIL;
1768 }
1769
1770 int retval = target_read_memory(target, c->address, 4, c->size / 4, c->backup);
1771 if (retval != ERROR_OK)
1772 return retval;
1773 }
1774
1775 /* mark as used, and return the new (reused) area */
1776 c->free = false;
1777 *area = c;
1778
1779 /* user pointer */
1780 c->user = area;
1781
1782 print_wa_layout(target);
1783
1784 return ERROR_OK;
1785 }
1786
1787 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1788 {
1789 int retval;
1790
1791 retval = target_alloc_working_area_try(target, size, area);
1792 if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
1793 LOG_WARNING("not enough working area available(requested %"PRIu32")", size);
1794 return retval;
1795
1796 }
1797
1798 static int target_restore_working_area(struct target *target, struct working_area *area)
1799 {
1800 int retval = ERROR_OK;
1801
1802 if (target->backup_working_area && area->backup != NULL) {
1803 retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup);
1804 if (retval != ERROR_OK)
1805 LOG_ERROR("failed to restore %"PRIu32" bytes of working area at address 0x%08"PRIx32,
1806 area->size, area->address);
1807 }
1808
1809 return retval;
1810 }
1811
1812 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1813 static int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1814 {
1815 int retval = ERROR_OK;
1816
1817 if (area->free)
1818 return retval;
1819
1820 if (restore) {
1821 retval = target_restore_working_area(target, area);
1822 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1823 if (retval != ERROR_OK)
1824 return retval;
1825 }
1826
1827 area->free = true;
1828
1829 LOG_DEBUG("freed %"PRIu32" bytes of working area at address 0x%08"PRIx32,
1830 area->size, area->address);
1831
1832 /* mark user pointer invalid */
1833 /* TODO: Is this really safe? It points to some previous caller's memory.
1834 * How could we know that the area pointer is still in that place and not
1835 * some other vital data? What's the purpose of this, anyway? */
1836 *area->user = NULL;
1837 area->user = NULL;
1838
1839 target_merge_working_areas(target);
1840
1841 print_wa_layout(target);
1842
1843 return retval;
1844 }
1845
1846 int target_free_working_area(struct target *target, struct working_area *area)
1847 {
1848 return target_free_working_area_restore(target, area, 1);
1849 }
1850
1851 void target_quit(void)
1852 {
1853 struct target_event_callback *pe = target_event_callbacks;
1854 while (pe) {
1855 struct target_event_callback *t = pe->next;
1856 free(pe);
1857 pe = t;
1858 }
1859 target_event_callbacks = NULL;
1860
1861 struct target_timer_callback *pt = target_timer_callbacks;
1862 while (pt) {
1863 struct target_timer_callback *t = pt->next;
1864 free(pt);
1865 pt = t;
1866 }
1867 target_timer_callbacks = NULL;
1868
1869 for (struct target *target = all_targets;
1870 target; target = target->next) {
1871 if (target->type->deinit_target)
1872 target->type->deinit_target(target);
1873 }
1874 }
1875
1876 /* free resources and restore memory, if restoring memory fails,
1877 * free up resources anyway
1878 */
1879 static void target_free_all_working_areas_restore(struct target *target, int restore)
1880 {
1881 struct working_area *c = target->working_areas;
1882
1883 LOG_DEBUG("freeing all working areas");
1884
1885 /* Loop through all areas, restoring the allocated ones and marking them as free */
1886 while (c) {
1887 if (!c->free) {
1888 if (restore)
1889 target_restore_working_area(target, c);
1890 c->free = true;
1891 *c->user = NULL; /* Same as above */
1892 c->user = NULL;
1893 }
1894 c = c->next;
1895 }
1896
1897 /* Run a merge pass to combine all areas into one */
1898 target_merge_working_areas(target);
1899
1900 print_wa_layout(target);
1901 }
1902
1903 void target_free_all_working_areas(struct target *target)
1904 {
1905 target_free_all_working_areas_restore(target, 1);
1906 }
1907
1908 /* Find the largest number of bytes that can be allocated */
1909 uint32_t target_get_working_area_avail(struct target *target)
1910 {
1911 struct working_area *c = target->working_areas;
1912 uint32_t max_size = 0;
1913
1914 if (c == NULL)
1915 return target->working_area_size;
1916
1917 while (c) {
1918 if (c->free && max_size < c->size)
1919 max_size = c->size;
1920
1921 c = c->next;
1922 }
1923
1924 return max_size;
1925 }
1926
1927 int target_arch_state(struct target *target)
1928 {
1929 int retval;
1930 if (target == NULL) {
1931 LOG_USER("No target has been configured");
1932 return ERROR_OK;
1933 }
1934
1935 LOG_USER("%s: target state: %s", target_name(target),
1936 target_state_name(target));
1937
1938 if (target->state != TARGET_HALTED)
1939 return ERROR_OK;
1940
1941 retval = target->type->arch_state(target);
1942 return retval;
1943 }
1944
1945 static int target_get_gdb_fileio_info_default(struct target *target,
1946 struct gdb_fileio_info *fileio_info)
1947 {
1948 /* If target does not support semi-hosting function, target
1949 has no need to provide .get_gdb_fileio_info callback.
1950 It just return ERROR_FAIL and gdb_server will return "Txx"
1951 as target halted every time. */
1952 return ERROR_FAIL;
1953 }
1954
1955 static int target_gdb_fileio_end_default(struct target *target,
1956 int retcode, int fileio_errno, bool ctrl_c)
1957 {
1958 return ERROR_OK;
1959 }
1960
1961 static int target_profiling_default(struct target *target, uint32_t *samples,
1962 uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
1963 {
1964 struct timeval timeout, now;
1965
1966 gettimeofday(&timeout, NULL);
1967 timeval_add_time(&timeout, seconds, 0);
1968
1969 LOG_INFO("Starting profiling. Halting and resuming the"
1970 " target as often as we can...");
1971
1972 uint32_t sample_count = 0;
1973 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
1974 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
1975
1976 int retval = ERROR_OK;
1977 for (;;) {
1978 target_poll(target);
1979 if (target->state == TARGET_HALTED) {
1980 uint32_t t = buf_get_u32(reg->value, 0, 32);
1981 samples[sample_count++] = t;
1982 /* current pc, addr = 0, do not handle breakpoints, not debugging */
1983 retval = target_resume(target, 1, 0, 0, 0);
1984 target_poll(target);
1985 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
1986 } else if (target->state == TARGET_RUNNING) {
1987 /* We want to quickly sample the PC. */
1988 retval = target_halt(target);
1989 } else {
1990 LOG_INFO("Target not halted or running");
1991 retval = ERROR_OK;
1992 break;
1993 }
1994
1995 if (retval != ERROR_OK)
1996 break;
1997
1998 gettimeofday(&now, NULL);
1999 if ((sample_count >= max_num_samples) ||
2000 ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec))) {
2001 LOG_INFO("Profiling completed. %" PRIu32 " samples.", sample_count);
2002 break;
2003 }
2004 }
2005
2006 *num_samples = sample_count;
2007 return retval;
2008 }
2009
2010 /* Single aligned words are guaranteed to use 16 or 32 bit access
2011 * mode respectively, otherwise data is handled as quickly as
2012 * possible
2013 */
2014 int target_write_buffer(struct target *target, uint32_t address, uint32_t size, const uint8_t *buffer)
2015 {
2016 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
2017 (int)size, (unsigned)address);
2018
2019 if (!target_was_examined(target)) {
2020 LOG_ERROR("Target not examined yet");
2021 return ERROR_FAIL;
2022 }
2023
2024 if (size == 0)
2025 return ERROR_OK;
2026
2027 if ((address + size - 1) < address) {
2028 /* GDB can request this when e.g. PC is 0xfffffffc*/
2029 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
2030 (unsigned)address,
2031 (unsigned)size);
2032 return ERROR_FAIL;
2033 }
2034
2035 return target->type->write_buffer(target, address, size, buffer);
2036 }
2037
2038 static int target_write_buffer_default(struct target *target, uint32_t address, uint32_t count, const uint8_t *buffer)
2039 {
2040 uint32_t size;
2041
2042 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2043 * will have something to do with the size we leave to it. */
2044 for (size = 1; size < 4 && count >= size * 2 + (address & size); size *= 2) {
2045 if (address & size) {
2046 int retval = target_write_memory(target, address, size, 1, buffer);
2047 if (retval != ERROR_OK)
2048 return retval;
2049 address += size;
2050 count -= size;
2051 buffer += size;
2052 }
2053 }
2054
2055 /* Write the data with as large access size as possible. */
2056 for (; size > 0; size /= 2) {
2057 uint32_t aligned = count - count % size;
2058 if (aligned > 0) {
2059 int retval = target_write_memory(target, address, size, aligned / size, buffer);
2060 if (retval != ERROR_OK)
2061 return retval;
2062 address += aligned;
2063 count -= aligned;
2064 buffer += aligned;
2065 }
2066 }
2067
2068 return ERROR_OK;
2069 }
2070
2071 /* Single aligned words are guaranteed to use 16 or 32 bit access
2072 * mode respectively, otherwise data is handled as quickly as
2073 * possible
2074 */
2075 int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
2076 {
2077 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
2078 (int)size, (unsigned)address);
2079
2080 if (!target_was_examined(target)) {
2081 LOG_ERROR("Target not examined yet");
2082 return ERROR_FAIL;
2083 }
2084
2085 if (size == 0)
2086 return ERROR_OK;
2087
2088 if ((address + size - 1) < address) {
2089 /* GDB can request this when e.g. PC is 0xfffffffc*/
2090 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
2091 address,
2092 size);
2093 return ERROR_FAIL;
2094 }
2095
2096 return target->type->read_buffer(target, address, size, buffer);
2097 }
2098
2099 static int target_read_buffer_default(struct target *target, uint32_t address, uint32_t count, uint8_t *buffer)
2100 {
2101 uint32_t size;
2102
2103 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2104 * will have something to do with the size we leave to it. */
2105 for (size = 1; size < 4 && count >= size * 2 + (address & size); size *= 2) {
2106 if (address & size) {
2107 int retval = target_read_memory(target, address, size, 1, buffer);
2108 if (retval != ERROR_OK)
2109 return retval;
2110 address += size;
2111 count -= size;
2112 buffer += size;
2113 }
2114 }
2115
2116 /* Read the data with as large access size as possible. */
2117 for (; size > 0; size /= 2) {
2118 uint32_t aligned = count - count % size;
2119 if (aligned > 0) {
2120 int retval = target_read_memory(target, address, size, aligned / size, buffer);
2121 if (retval != ERROR_OK)
2122 return retval;
2123 address += aligned;
2124 count -= aligned;
2125 buffer += aligned;
2126 }
2127 }
2128
2129 return ERROR_OK;
2130 }
2131
2132 int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
2133 {
2134 uint8_t *buffer;
2135 int retval;
2136 uint32_t i;
2137 uint32_t checksum = 0;
2138 if (!target_was_examined(target)) {
2139 LOG_ERROR("Target not examined yet");
2140 return ERROR_FAIL;
2141 }
2142
2143 retval = target->type->checksum_memory(target, address, size, &checksum);
2144 if (retval != ERROR_OK) {
2145 buffer = malloc(size);
2146 if (buffer == NULL) {
2147 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
2148 return ERROR_COMMAND_SYNTAX_ERROR;
2149 }
2150 retval = target_read_buffer(target, address, size, buffer);
2151 if (retval != ERROR_OK) {
2152 free(buffer);
2153 return retval;
2154 }
2155
2156 /* convert to target endianness */
2157 for (i = 0; i < (size/sizeof(uint32_t)); i++) {
2158 uint32_t target_data;
2159 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
2160 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
2161 }
2162
2163 retval = image_calculate_checksum(buffer, size, &checksum);
2164 free(buffer);
2165 }
2166
2167 *crc = checksum;
2168
2169 return retval;
2170 }
2171
2172 int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank)
2173 {
2174 int retval;
2175 if (!target_was_examined(target)) {
2176 LOG_ERROR("Target not examined yet");
2177 return ERROR_FAIL;
2178 }
2179
2180 if (target->type->blank_check_memory == 0)
2181 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
2182
2183 retval = target->type->blank_check_memory(target, address, size, blank);
2184
2185 return retval;
2186 }
2187
2188 int target_read_u64(struct target *target, uint64_t address, uint64_t *value)
2189 {
2190 uint8_t value_buf[8];
2191 if (!target_was_examined(target)) {
2192 LOG_ERROR("Target not examined yet");
2193 return ERROR_FAIL;
2194 }
2195
2196 int retval = target_read_memory(target, address, 8, 1, value_buf);
2197
2198 if (retval == ERROR_OK) {
2199 *value = target_buffer_get_u64(target, value_buf);
2200 LOG_DEBUG("address: 0x%" PRIx64 ", value: 0x%16.16" PRIx64 "",
2201 address,
2202 *value);
2203 } else {
2204 *value = 0x0;
2205 LOG_DEBUG("address: 0x%" PRIx64 " failed",
2206 address);
2207 }
2208
2209 return retval;
2210 }
2211
2212 int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
2213 {
2214 uint8_t value_buf[4];
2215 if (!target_was_examined(target)) {
2216 LOG_ERROR("Target not examined yet");
2217 return ERROR_FAIL;
2218 }
2219
2220 int retval = target_read_memory(target, address, 4, 1, value_buf);
2221
2222 if (retval == ERROR_OK) {
2223 *value = target_buffer_get_u32(target, value_buf);
2224 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
2225 address,
2226 *value);
2227 } else {
2228 *value = 0x0;
2229 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
2230 address);
2231 }
2232
2233 return retval;
2234 }
2235
2236 int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
2237 {
2238 uint8_t value_buf[2];
2239 if (!target_was_examined(target)) {
2240 LOG_ERROR("Target not examined yet");
2241 return ERROR_FAIL;
2242 }
2243
2244 int retval = target_read_memory(target, address, 2, 1, value_buf);
2245
2246 if (retval == ERROR_OK) {
2247 *value = target_buffer_get_u16(target, value_buf);
2248 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
2249 address,
2250 *value);
2251 } else {
2252 *value = 0x0;
2253 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
2254 address);
2255 }
2256
2257 return retval;
2258 }
2259
2260 int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
2261 {
2262 if (!target_was_examined(target)) {
2263 LOG_ERROR("Target not examined yet");
2264 return ERROR_FAIL;
2265 }
2266
2267 int retval = target_read_memory(target, address, 1, 1, value);
2268
2269 if (retval == ERROR_OK) {
2270 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
2271 address,
2272 *value);
2273 } else {
2274 *value = 0x0;
2275 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
2276 address);
2277 }
2278
2279 return retval;
2280 }
2281
2282 int target_write_u64(struct target *target, uint64_t address, uint64_t value)
2283 {
2284 int retval;
2285 uint8_t value_buf[8];
2286 if (!target_was_examined(target)) {
2287 LOG_ERROR("Target not examined yet");
2288 return ERROR_FAIL;
2289 }
2290
2291 LOG_DEBUG("address: 0x%" PRIx64 ", value: 0x%16.16" PRIx64 "",
2292 address,
2293 value);
2294
2295 target_buffer_set_u64(target, value_buf, value);
2296 retval = target_write_memory(target, address, 8, 1, value_buf);
2297 if (retval != ERROR_OK)
2298 LOG_DEBUG("failed: %i", retval);
2299
2300 return retval;
2301 }
2302
2303 int target_write_u32(struct target *target, uint32_t address, uint32_t value)
2304 {
2305 int retval;
2306 uint8_t value_buf[4];
2307 if (!target_was_examined(target)) {
2308 LOG_ERROR("Target not examined yet");
2309 return ERROR_FAIL;
2310 }
2311
2312 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
2313 address,
2314 value);
2315
2316 target_buffer_set_u32(target, value_buf, value);
2317 retval = target_write_memory(target, address, 4, 1, value_buf);
2318 if (retval != ERROR_OK)
2319 LOG_DEBUG("failed: %i", retval);
2320
2321 return retval;
2322 }
2323
2324 int target_write_u16(struct target *target, uint32_t address, uint16_t value)
2325 {
2326 int retval;
2327 uint8_t value_buf[2];
2328 if (!target_was_examined(target)) {
2329 LOG_ERROR("Target not examined yet");
2330 return ERROR_FAIL;
2331 }
2332
2333 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
2334 address,
2335 value);
2336
2337 target_buffer_set_u16(target, value_buf, value);
2338 retval = target_write_memory(target, address, 2, 1, value_buf);
2339 if (retval != ERROR_OK)
2340 LOG_DEBUG("failed: %i", retval);
2341
2342 return retval;
2343 }
2344
2345 int target_write_u8(struct target *target, uint32_t address, uint8_t value)
2346 {
2347 int retval;
2348 if (!target_was_examined(target)) {
2349 LOG_ERROR("Target not examined yet");
2350 return ERROR_FAIL;
2351 }
2352
2353 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
2354 address, value);
2355
2356 retval = target_write_memory(target, address, 1, 1, &value);
2357 if (retval != ERROR_OK)
2358 LOG_DEBUG("failed: %i", retval);
2359
2360 return retval;
2361 }
2362
2363 static int find_target(struct command_context *cmd_ctx, const char *name)
2364 {
2365 struct target *target = get_target(name);
2366 if (target == NULL) {
2367 LOG_ERROR("Target: %s is unknown, try one of:\n", name);
2368 return ERROR_FAIL;
2369 }
2370 if (!target->tap->enabled) {
2371 LOG_USER("Target: TAP %s is disabled, "
2372 "can't be the current target\n",
2373 target->tap->dotted_name);
2374 return ERROR_FAIL;
2375 }
2376
2377 cmd_ctx->current_target = target->target_number;
2378 return ERROR_OK;
2379 }
2380
2381
2382 COMMAND_HANDLER(handle_targets_command)
2383 {
2384 int retval = ERROR_OK;
2385 if (CMD_ARGC == 1) {
2386 retval = find_target(CMD_CTX, CMD_ARGV[0]);
2387 if (retval == ERROR_OK) {
2388 /* we're done! */
2389 return retval;
2390 }
2391 }
2392
2393 struct target *target = all_targets;
2394 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
2395 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
2396 while (target) {
2397 const char *state;
2398 char marker = ' ';
2399
2400 if (target->tap->enabled)
2401 state = target_state_name(target);
2402 else
2403 state = "tap-disabled";
2404
2405 if (CMD_CTX->current_target == target->target_number)
2406 marker = '*';
2407
2408 /* keep columns lined up to match the headers above */
2409 command_print(CMD_CTX,
2410 "%2d%c %-18s %-10s %-6s %-18s %s",
2411 target->target_number,
2412 marker,
2413 target_name(target),
2414 target_type_name(target),
2415 Jim_Nvp_value2name_simple(nvp_target_endian,
2416 target->endianness)->name,
2417 target->tap->dotted_name,
2418 state);
2419 target = target->next;
2420 }
2421
2422 return retval;
2423 }
2424
2425 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2426
2427 static int powerDropout;
2428 static int srstAsserted;
2429
2430 static int runPowerRestore;
2431 static int runPowerDropout;
2432 static int runSrstAsserted;
2433 static int runSrstDeasserted;
2434
2435 static int sense_handler(void)
2436 {
2437 static int prevSrstAsserted;
2438 static int prevPowerdropout;
2439
2440 int retval = jtag_power_dropout(&powerDropout);
2441 if (retval != ERROR_OK)
2442 return retval;
2443
2444 int powerRestored;
2445 powerRestored = prevPowerdropout && !powerDropout;
2446 if (powerRestored)
2447 runPowerRestore = 1;
2448
2449 long long current = timeval_ms();
2450 static long long lastPower;
2451 int waitMore = lastPower + 2000 > current;
2452 if (powerDropout && !waitMore) {
2453 runPowerDropout = 1;
2454 lastPower = current;
2455 }
2456
2457 retval = jtag_srst_asserted(&srstAsserted);
2458 if (retval != ERROR_OK)
2459 return retval;
2460
2461 int srstDeasserted;
2462 srstDeasserted = prevSrstAsserted && !srstAsserted;
2463
2464 static long long lastSrst;
2465 waitMore = lastSrst + 2000 > current;
2466 if (srstDeasserted && !waitMore) {
2467 runSrstDeasserted = 1;
2468 lastSrst = current;
2469 }
2470
2471 if (!prevSrstAsserted && srstAsserted)
2472 runSrstAsserted = 1;
2473
2474 prevSrstAsserted = srstAsserted;
2475 prevPowerdropout = powerDropout;
2476
2477 if (srstDeasserted || powerRestored) {
2478 /* Other than logging the event we can't do anything here.
2479 * Issuing a reset is a particularly bad idea as we might
2480 * be inside a reset already.
2481 */
2482 }
2483
2484 return ERROR_OK;
2485 }
2486
2487 /* process target state changes */
2488 static int handle_target(void *priv)
2489 {
2490 Jim_Interp *interp = (Jim_Interp *)priv;
2491 int retval = ERROR_OK;
2492
2493 if (!is_jtag_poll_safe()) {
2494 /* polling is disabled currently */
2495 return ERROR_OK;
2496 }
2497
2498 /* we do not want to recurse here... */
2499 static int recursive;
2500 if (!recursive) {
2501 recursive = 1;
2502 sense_handler();
2503 /* danger! running these procedures can trigger srst assertions and power dropouts.
2504 * We need to avoid an infinite loop/recursion here and we do that by
2505 * clearing the flags after running these events.
2506 */
2507 int did_something = 0;
2508 if (runSrstAsserted) {
2509 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2510 Jim_Eval(interp, "srst_asserted");
2511 did_something = 1;
2512 }
2513 if (runSrstDeasserted) {
2514 Jim_Eval(interp, "srst_deasserted");
2515 did_something = 1;
2516 }
2517 if (runPowerDropout) {
2518 LOG_INFO("Power dropout detected, running power_dropout proc.");
2519 Jim_Eval(interp, "power_dropout");
2520 did_something = 1;
2521 }
2522 if (runPowerRestore) {
2523 Jim_Eval(interp, "power_restore");
2524 did_something = 1;
2525 }
2526
2527 if (did_something) {
2528 /* clear detect flags */
2529 sense_handler();
2530 }
2531
2532 /* clear action flags */
2533
2534 runSrstAsserted = 0;
2535 runSrstDeasserted = 0;
2536 runPowerRestore = 0;
2537 runPowerDropout = 0;
2538
2539 recursive = 0;
2540 }
2541
2542 /* Poll targets for state changes unless that's globally disabled.
2543 * Skip targets that are currently disabled.
2544 */
2545 for (struct target *target = all_targets;
2546 is_jtag_poll_safe() && target;
2547 target = target->next) {
2548
2549 if (!target_was_examined(target))
2550 continue;
2551
2552 if (!target->tap->enabled)
2553 continue;
2554
2555 if (target->backoff.times > target->backoff.count) {
2556 /* do not poll this time as we failed previously */
2557 target->backoff.count++;
2558 continue;
2559 }
2560 target->backoff.count = 0;
2561
2562 /* only poll target if we've got power and srst isn't asserted */
2563 if (!powerDropout && !srstAsserted) {
2564 /* polling may fail silently until the target has been examined */
2565 retval = target_poll(target);
2566 if (retval != ERROR_OK) {
2567 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2568 if (target->backoff.times * polling_interval < 5000) {
2569 target->backoff.times *= 2;
2570 target->backoff.times++;
2571 }
2572
2573 /* Tell GDB to halt the debugger. This allows the user to
2574 * run monitor commands to handle the situation.
2575 */
2576 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2577 }
2578 if (target->backoff.times > 0) {
2579 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target));
2580 target_reset_examined(target);
2581 retval = target_examine_one(target);
2582 /* Target examination could have failed due to unstable connection,
2583 * but we set the examined flag anyway to repoll it later */
2584 if (retval != ERROR_OK) {
2585 target->examined = true;
2586 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2587 target->backoff.times * polling_interval);
2588 return retval;
2589 }
2590 }
2591
2592 /* Since we succeeded, we reset backoff count */
2593 target->backoff.times = 0;
2594 }
2595 }
2596
2597 return retval;
2598 }
2599
2600 COMMAND_HANDLER(handle_reg_command)
2601 {
2602 struct target *target;
2603 struct reg *reg = NULL;
2604 unsigned count = 0;
2605 char *value;
2606
2607 LOG_DEBUG("-");
2608
2609 target = get_current_target(CMD_CTX);
2610
2611 /* list all available registers for the current target */
2612 if (CMD_ARGC == 0) {
2613 struct reg_cache *cache = target->reg_cache;
2614
2615 count = 0;
2616 while (cache) {
2617 unsigned i;
2618
2619 command_print(CMD_CTX, "===== %s", cache->name);
2620
2621 for (i = 0, reg = cache->reg_list;
2622 i < cache->num_regs;
2623 i++, reg++, count++) {
2624 /* only print cached values if they are valid */
2625 if (reg->valid) {
2626 value = buf_to_str(reg->value,
2627 reg->size, 16);
2628 command_print(CMD_CTX,
2629 "(%i) %s (/%" PRIu32 "): 0x%s%s",
2630 count, reg->name,
2631 reg->size, value,
2632 reg->dirty
2633 ? " (dirty)"
2634 : "");
2635 free(value);
2636 } else {
2637 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
2638 count, reg->name,
2639 reg->size) ;
2640 }
2641 }
2642 cache = cache->next;
2643 }
2644
2645 return ERROR_OK;
2646 }
2647
2648 /* access a single register by its ordinal number */
2649 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9')) {
2650 unsigned num;
2651 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
2652
2653 struct reg_cache *cache = target->reg_cache;
2654 count = 0;
2655 while (cache) {
2656 unsigned i;
2657 for (i = 0; i < cache->num_regs; i++) {
2658 if (count++ == num) {
2659 reg = &cache->reg_list[i];
2660 break;
2661 }
2662 }
2663 if (reg)
2664 break;
2665 cache = cache->next;
2666 }
2667
2668 if (!reg) {
2669 command_print(CMD_CTX, "%i is out of bounds, the current target "
2670 "has only %i registers (0 - %i)", num, count, count - 1);
2671 return ERROR_OK;
2672 }
2673 } else {
2674 /* access a single register by its name */
2675 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
2676
2677 if (!reg) {
2678 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
2679 return ERROR_OK;
2680 }
2681 }
2682
2683 assert(reg != NULL); /* give clang a hint that we *know* reg is != NULL here */
2684
2685 /* display a register */
2686 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0')
2687 && (CMD_ARGV[1][0] <= '9')))) {
2688 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
2689 reg->valid = 0;
2690
2691 if (reg->valid == 0)
2692 reg->type->get(reg);
2693 value = buf_to_str(reg->value, reg->size, 16);
2694 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2695 free(value);
2696 return ERROR_OK;
2697 }
2698
2699 /* set register value */
2700 if (CMD_ARGC == 2) {
2701 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
2702 if (buf == NULL)
2703 return ERROR_FAIL;
2704 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
2705
2706 reg->type->set(reg, buf);
2707
2708 value = buf_to_str(reg->value, reg->size, 16);
2709 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2710 free(value);
2711
2712 free(buf);
2713
2714 return ERROR_OK;
2715 }
2716
2717 return ERROR_COMMAND_SYNTAX_ERROR;
2718 }
2719
2720 COMMAND_HANDLER(handle_poll_command)
2721 {
2722 int retval = ERROR_OK;
2723 struct target *target = get_current_target(CMD_CTX);
2724
2725 if (CMD_ARGC == 0) {
2726 command_print(CMD_CTX, "background polling: %s",
2727 jtag_poll_get_enabled() ? "on" : "off");
2728 command_print(CMD_CTX, "TAP: %s (%s)",
2729 target->tap->dotted_name,
2730 target->tap->enabled ? "enabled" : "disabled");
2731 if (!target->tap->enabled)
2732 return ERROR_OK;
2733 retval = target_poll(target);
2734 if (retval != ERROR_OK)
2735 return retval;
2736 retval = target_arch_state(target);
2737 if (retval != ERROR_OK)
2738 return retval;
2739 } else if (CMD_ARGC == 1) {
2740 bool enable;
2741 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
2742 jtag_poll_set_enabled(enable);
2743 } else
2744 return ERROR_COMMAND_SYNTAX_ERROR;
2745
2746 return retval;
2747 }
2748
2749 COMMAND_HANDLER(handle_wait_halt_command)
2750 {
2751 if (CMD_ARGC > 1)
2752 return ERROR_COMMAND_SYNTAX_ERROR;
2753
2754 unsigned ms = DEFAULT_HALT_TIMEOUT;
2755 if (1 == CMD_ARGC) {
2756 int retval = parse_uint(CMD_ARGV[0], &ms);
2757 if (ERROR_OK != retval)
2758 return ERROR_COMMAND_SYNTAX_ERROR;
2759 }
2760
2761 struct target *target = get_current_target(CMD_CTX);
2762 return target_wait_state(target, TARGET_HALTED, ms);
2763 }
2764
2765 /* wait for target state to change. The trick here is to have a low
2766 * latency for short waits and not to suck up all the CPU time
2767 * on longer waits.
2768 *
2769 * After 500ms, keep_alive() is invoked
2770 */
2771 int target_wait_state(struct target *target, enum target_state state, int ms)
2772 {
2773 int retval;
2774 long long then = 0, cur;
2775 int once = 1;
2776
2777 for (;;) {
2778 retval = target_poll(target);
2779 if (retval != ERROR_OK)
2780 return retval;
2781 if (target->state == state)
2782 break;
2783 cur = timeval_ms();
2784 if (once) {
2785 once = 0;
2786 then = timeval_ms();
2787 LOG_DEBUG("waiting for target %s...",
2788 Jim_Nvp_value2name_simple(nvp_target_state, state)->name);
2789 }
2790
2791 if (cur-then > 500)
2792 keep_alive();
2793
2794 if ((cur-then) > ms) {
2795 LOG_ERROR("timed out while waiting for target %s",
2796 Jim_Nvp_value2name_simple(nvp_target_state, state)->name);
2797 return ERROR_FAIL;
2798 }
2799 }
2800
2801 return ERROR_OK;
2802 }
2803
2804 COMMAND_HANDLER(handle_halt_command)
2805 {
2806 LOG_DEBUG("-");
2807
2808 struct target *target = get_current_target(CMD_CTX);
2809 int retval = target_halt(target);
2810 if (ERROR_OK != retval)
2811 return retval;
2812
2813 if (CMD_ARGC == 1) {
2814 unsigned wait_local;
2815 retval = parse_uint(CMD_ARGV[0], &wait_local);
2816 if (ERROR_OK != retval)
2817 return ERROR_COMMAND_SYNTAX_ERROR;
2818 if (!wait_local)
2819 return ERROR_OK;
2820 }
2821
2822 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2823 }
2824
2825 COMMAND_HANDLER(handle_soft_reset_halt_command)
2826 {
2827 struct target *target = get_current_target(CMD_CTX);
2828
2829 LOG_USER("requesting target halt and executing a soft reset");
2830
2831 target_soft_reset_halt(target);
2832
2833 return ERROR_OK;
2834 }
2835
2836 COMMAND_HANDLER(handle_reset_command)
2837 {
2838 if (CMD_ARGC > 1)
2839 return ERROR_COMMAND_SYNTAX_ERROR;
2840
2841 enum target_reset_mode reset_mode = RESET_RUN;
2842 if (CMD_ARGC == 1) {
2843 const Jim_Nvp *n;
2844 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
2845 if ((n->name == NULL) || (n->value == RESET_UNKNOWN))
2846 return ERROR_COMMAND_SYNTAX_ERROR;
2847 reset_mode = n->value;
2848 }
2849
2850 /* reset *all* targets */
2851 return target_process_reset(CMD_CTX, reset_mode);
2852 }
2853
2854
2855 COMMAND_HANDLER(handle_resume_command)
2856 {
2857 int current = 1;
2858 if (CMD_ARGC > 1)
2859 return ERROR_COMMAND_SYNTAX_ERROR;
2860
2861 struct target *target = get_current_target(CMD_CTX);
2862
2863 /* with no CMD_ARGV, resume from current pc, addr = 0,
2864 * with one arguments, addr = CMD_ARGV[0],
2865 * handle breakpoints, not debugging */
2866 uint32_t addr = 0;
2867 if (CMD_ARGC == 1) {
2868 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2869 current = 0;
2870 }
2871
2872 return target_resume(target, current, addr, 1, 0);
2873 }
2874
2875 COMMAND_HANDLER(handle_step_command)
2876 {
2877 if (CMD_ARGC > 1)
2878 return ERROR_COMMAND_SYNTAX_ERROR;
2879
2880 LOG_DEBUG("-");
2881
2882 /* with no CMD_ARGV, step from current pc, addr = 0,
2883 * with one argument addr = CMD_ARGV[0],
2884 * handle breakpoints, debugging */
2885 uint32_t addr = 0;
2886 int current_pc = 1;
2887 if (CMD_ARGC == 1) {
2888 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2889 current_pc = 0;
2890 }
2891
2892 struct target *target = get_current_target(CMD_CTX);
2893
2894 return target->type->step(target, current_pc, addr, 1);
2895 }
2896
2897 static void handle_md_output(struct command_context *cmd_ctx,
2898 struct target *target, uint32_t address, unsigned size,
2899 unsigned count, const uint8_t *buffer)
2900 {
2901 const unsigned line_bytecnt = 32;
2902 unsigned line_modulo = line_bytecnt / size;
2903
2904 char output[line_bytecnt * 4 + 1];
2905 unsigned output_len = 0;
2906
2907 const char *value_fmt;
2908 switch (size) {
2909 case 4:
2910 value_fmt = "%8.8x ";
2911 break;
2912 case 2:
2913 value_fmt = "%4.4x ";
2914 break;
2915 case 1:
2916 value_fmt = "%2.2x ";
2917 break;
2918 default:
2919 /* "can't happen", caller checked */
2920 LOG_ERROR("invalid memory read size: %u", size);
2921 return;
2922 }
2923
2924 for (unsigned i = 0; i < count; i++) {
2925 if (i % line_modulo == 0) {
2926 output_len += snprintf(output + output_len,
2927 sizeof(output) - output_len,
2928 "0x%8.8x: ",
2929 (unsigned)(address + (i*size)));
2930 }
2931
2932 uint32_t value = 0;
2933 const uint8_t *value_ptr = buffer + i * size;
2934 switch (size) {
2935 case 4:
2936 value = target_buffer_get_u32(target, value_ptr);
2937 break;
2938 case 2:
2939 value = target_buffer_get_u16(target, value_ptr);
2940 break;
2941 case 1:
2942 value = *value_ptr;
2943 }
2944 output_len += snprintf(output + output_len,
2945 sizeof(output) - output_len,
2946 value_fmt, value);
2947
2948 if ((i % line_modulo == line_modulo - 1) || (i == count - 1)) {
2949 command_print(cmd_ctx, "%s", output);
2950 output_len = 0;
2951 }
2952 }
2953 }
2954
2955 COMMAND_HANDLER(handle_md_command)
2956 {
2957 if (CMD_ARGC < 1)
2958 return ERROR_COMMAND_SYNTAX_ERROR;
2959
2960 unsigned size = 0;
2961 switch (CMD_NAME[2]) {
2962 case 'w':
2963 size = 4;
2964 break;
2965 case 'h':
2966 size = 2;
2967 break;
2968 case 'b':
2969 size = 1;
2970 break;
2971 default:
2972 return ERROR_COMMAND_SYNTAX_ERROR;
2973 }
2974
2975 bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
2976 int (*fn)(struct target *target,
2977 uint32_t address, uint32_t size_value, uint32_t count, uint8_t *buffer);
2978 if (physical) {
2979 CMD_ARGC--;
2980 CMD_ARGV++;
2981 fn = target_read_phys_memory;
2982 } else
2983 fn = target_read_memory;
2984 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
2985 return ERROR_COMMAND_SYNTAX_ERROR;
2986
2987 uint32_t address;
2988 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2989
2990 unsigned count = 1;
2991 if (CMD_ARGC == 2)
2992 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
2993
2994 uint8_t *buffer = calloc(count, size);
2995
2996 struct target *target = get_current_target(CMD_CTX);
2997 int retval = fn(target, address, size, count, buffer);
2998 if (ERROR_OK == retval)
2999 handle_md_output(CMD_CTX, target, address, size, count, buffer);
3000
3001 free(buffer);
3002
3003 return retval;
3004 }
3005
3006 typedef int (*target_write_fn)(struct target *target,
3007 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer);
3008
3009 static int target_fill_mem(struct target *target,
3010 uint32_t address,
3011 target_write_fn fn,
3012 unsigned data_size,
3013 /* value */
3014 uint32_t b,
3015 /* count */
3016 unsigned c)
3017 {
3018 /* We have to write in reasonably large chunks to be able
3019 * to fill large memory areas with any sane speed */
3020 const unsigned chunk_size = 16384;
3021 uint8_t *target_buf = malloc(chunk_size * data_size);
3022 if (target_buf == NULL) {
3023 LOG_ERROR("Out of memory");
3024 return ERROR_FAIL;
3025 }
3026
3027 for (unsigned i = 0; i < chunk_size; i++) {
3028 switch (data_size) {
3029 case 4:
3030 target_buffer_set_u32(target, target_buf + i * data_size, b);
3031 break;
3032 case 2:
3033 target_buffer_set_u16(target, target_buf + i * data_size, b);
3034 break;
3035 case 1:
3036 target_buffer_set_u8(target, target_buf + i * data_size, b);
3037 break;
3038 default:
3039 exit(-1);
3040 }
3041 }
3042
3043 int retval = ERROR_OK;
3044
3045 for (unsigned x = 0; x < c; x += chunk_size) {
3046 unsigned current;
3047 current = c - x;
3048 if (current > chunk_size)
3049 current = chunk_size;
3050 retval = fn(target, address + x * data_size, data_size, current, target_buf);
3051 if (retval != ERROR_OK)
3052 break;
3053 /* avoid GDB timeouts */
3054 keep_alive();
3055 }
3056 free(target_buf);
3057
3058 return retval;
3059 }
3060
3061
3062 COMMAND_HANDLER(handle_mw_command)
3063 {
3064 if (CMD_ARGC < 2)
3065 return ERROR_COMMAND_SYNTAX_ERROR;
3066 bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
3067 target_write_fn fn;
3068 if (physical) {
3069 CMD_ARGC--;
3070 CMD_ARGV++;
3071 fn = target_write_phys_memory;
3072 } else
3073 fn = target_write_memory;
3074 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
3075 return ERROR_COMMAND_SYNTAX_ERROR;
3076
3077 uint32_t address;
3078 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
3079
3080 uint32_t value;
3081 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
3082
3083 unsigned count = 1;
3084 if (CMD_ARGC == 3)
3085 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
3086
3087 struct target *target = get_current_target(CMD_CTX);
3088 unsigned wordsize;
3089 switch (CMD_NAME[2]) {
3090 case 'w':
3091 wordsize = 4;
3092 break;
3093 case 'h':
3094 wordsize = 2;
3095 break;
3096 case 'b':
3097 wordsize = 1;
3098 break;
3099 default:
3100 return ERROR_COMMAND_SYNTAX_ERROR;
3101 }
3102
3103 return target_fill_mem(target, address, fn, wordsize, value, count);
3104 }
3105
3106 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
3107 uint32_t *min_address, uint32_t *max_address)
3108 {
3109 if (CMD_ARGC < 1 || CMD_ARGC > 5)
3110 return ERROR_COMMAND_SYNTAX_ERROR;
3111
3112 /* a base address isn't always necessary,
3113 * default to 0x0 (i.e. don't relocate) */
3114 if (CMD_ARGC >= 2) {
3115 uint32_t addr;
3116 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
3117 image->base_address = addr;
3118 image->base_address_set = 1;
3119 } else
3120 image->base_address_set = 0;
3121
3122 image->start_address_set = 0;
3123
3124 if (CMD_ARGC >= 4)
3125 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], *min_address);
3126 if (CMD_ARGC == 5) {
3127 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], *max_address);
3128 /* use size (given) to find max (required) */
3129 *max_address += *min_address;
3130 }
3131
3132 if (*min_address > *max_address)
3133 return ERROR_COMMAND_SYNTAX_ERROR;
3134
3135 return ERROR_OK;
3136 }
3137
3138 COMMAND_HANDLER(handle_load_image_command)
3139 {
3140 uint8_t *buffer;
3141 size_t buf_cnt;
3142 uint32_t image_size;
3143 uint32_t min_address = 0;
3144 uint32_t max_address = 0xffffffff;
3145 int i;
3146 struct image image;
3147
3148 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
3149 &image, &min_address, &max_address);
3150 if (ERROR_OK != retval)
3151 return retval;
3152
3153 struct target *target = get_current_target(CMD_CTX);
3154
3155 struct duration bench;
3156 duration_start(&bench);
3157
3158 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
3159 return ERROR_OK;
3160
3161 image_size = 0x0;
3162 retval = ERROR_OK;
3163 for (i = 0; i < image.num_sections; i++) {
3164 buffer = malloc(image.sections[i].size);
3165 if (buffer == NULL) {
3166 command_print(CMD_CTX,
3167 "error allocating buffer for section (%d bytes)",
3168 (int)(image.sections[i].size));
3169 break;
3170 }
3171
3172 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
3173 if (retval != ERROR_OK) {
3174 free(buffer);
3175 break;
3176 }
3177
3178 uint32_t offset = 0;
3179 uint32_t length = buf_cnt;