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