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