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) {
3203 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3204 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3205 return JIM_ERR;
3206 }
3207 if ((addr + (len * width)) < addr) {
3208 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3209 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3210 return JIM_ERR;
3211 }
3212 /* absurd transfer size? */
3213 if (len > 65536) {
3214 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3215 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3216 return JIM_ERR;
3217 }
3218
3219 if ((width == 1) ||
3220 ((width == 2) && ((addr & 1) == 0)) ||
3221 ((width == 4) && ((addr & 3) == 0))) {
3222 /* all is well */
3223 } else {
3224 char buf[100];
3225 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3226 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3227 addr,
3228 width);
3229 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3230 return JIM_ERR;
3231 }
3232
3233 /* Transfer loop */
3234
3235 /* index counter */
3236 n = 0;
3237
3238 size_t buffersize = 4096;
3239 uint8_t *buffer = malloc(buffersize);
3240 if (buffer == NULL)
3241 return JIM_ERR;
3242
3243 /* assume ok */
3244 e = JIM_OK;
3245 while (len) {
3246 /* Slurp... in buffer size chunks */
3247
3248 count = len; /* in objects.. */
3249 if (count > (buffersize/width)) {
3250 count = (buffersize/width);
3251 }
3252
3253 retval = target_read_memory(target, addr, width, count, buffer);
3254 if (retval != ERROR_OK) {
3255 /* BOO !*/
3256 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3257 (unsigned int)addr,
3258 (int)width,
3259 (int)count);
3260 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3261 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3262 e = JIM_ERR;
3263 len = 0;
3264 } else {
3265 v = 0; /* shut up gcc */
3266 for (i = 0 ;i < count ;i++, n++) {
3267 switch (width) {
3268 case 4:
3269 v = target_buffer_get_u32(target, &buffer[i*width]);
3270 break;
3271 case 2:
3272 v = target_buffer_get_u16(target, &buffer[i*width]);
3273 break;
3274 case 1:
3275 v = buffer[i] & 0x0ff;
3276 break;
3277 }
3278 new_int_array_element(interp, varname, n, v);
3279 }
3280 len -= count;
3281 }
3282 }
3283
3284 free(buffer);
3285
3286 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3287
3288 return JIM_OK;
3289 }
3290
3291 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3292 {
3293 char *namebuf;
3294 Jim_Obj *nameObjPtr, *valObjPtr;
3295 int result;
3296 long l;
3297
3298 namebuf = alloc_printf("%s(%d)", varname, idx);
3299 if (!namebuf)
3300 return JIM_ERR;
3301
3302 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3303 if (!nameObjPtr)
3304 {
3305 free(namebuf);
3306 return JIM_ERR;
3307 }
3308
3309 Jim_IncrRefCount(nameObjPtr);
3310 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3311 Jim_DecrRefCount(interp, nameObjPtr);
3312 free(namebuf);
3313 if (valObjPtr == NULL)
3314 return JIM_ERR;
3315
3316 result = Jim_GetLong(interp, valObjPtr, &l);
3317 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3318 *val = l;
3319 return result;
3320 }
3321
3322 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3323 {
3324 struct command_context *context;
3325 struct target *target;
3326
3327 context = Jim_GetAssocData(interp, "context");
3328 if (context == NULL) {
3329 LOG_ERROR("array2mem: no command context");
3330 return JIM_ERR;
3331 }
3332 target = get_current_target(context);
3333 if (target == NULL) {
3334 LOG_ERROR("array2mem: no current target");
3335 return JIM_ERR;
3336 }
3337
3338 return target_array2mem(interp,target, argc-1, argv + 1);
3339 }
3340 static int target_array2mem(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3341 {
3342 long l;
3343 uint32_t width;
3344 int len;
3345 uint32_t addr;
3346 uint32_t count;
3347 uint32_t v;
3348 const char *varname;
3349 int n, e, retval;
3350 uint32_t i;
3351
3352 /* argv[1] = name of array to get the data
3353 * argv[2] = desired width
3354 * argv[3] = memory address
3355 * argv[4] = count to write
3356 */
3357 if (argc != 4) {
3358 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3359 return JIM_ERR;
3360 }
3361 varname = Jim_GetString(argv[0], &len);
3362 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3363
3364 e = Jim_GetLong(interp, argv[1], &l);
3365 width = l;
3366 if (e != JIM_OK) {
3367 return e;
3368 }
3369
3370 e = Jim_GetLong(interp, argv[2], &l);
3371 addr = l;
3372 if (e != JIM_OK) {
3373 return e;
3374 }
3375 e = Jim_GetLong(interp, argv[3], &l);
3376 len = l;
3377 if (e != JIM_OK) {
3378 return e;
3379 }
3380 switch (width) {
3381 case 8:
3382 width = 1;
3383 break;
3384 case 16:
3385 width = 2;
3386 break;
3387 case 32:
3388 width = 4;
3389 break;
3390 default:
3391 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3392 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3393 return JIM_ERR;
3394 }
3395 if (len == 0) {
3396 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3397 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3398 return JIM_ERR;
3399 }
3400 if ((addr + (len * width)) < addr) {
3401 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3402 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3403 return JIM_ERR;
3404 }
3405 /* absurd transfer size? */
3406 if (len > 65536) {
3407 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3408 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3409 return JIM_ERR;
3410 }
3411
3412 if ((width == 1) ||
3413 ((width == 2) && ((addr & 1) == 0)) ||
3414 ((width == 4) && ((addr & 3) == 0))) {
3415 /* all is well */
3416 } else {
3417 char buf[100];
3418 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3419 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3420 (unsigned int)addr,
3421 (int)width);
3422 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3423 return JIM_ERR;
3424 }
3425
3426 /* Transfer loop */
3427
3428 /* index counter */
3429 n = 0;
3430 /* assume ok */
3431 e = JIM_OK;
3432
3433 size_t buffersize = 4096;
3434 uint8_t *buffer = malloc(buffersize);
3435 if (buffer == NULL)
3436 return JIM_ERR;
3437
3438 while (len) {
3439 /* Slurp... in buffer size chunks */
3440
3441 count = len; /* in objects.. */
3442 if (count > (buffersize/width)) {
3443 count = (buffersize/width);
3444 }
3445
3446 v = 0; /* shut up gcc */
3447 for (i = 0 ;i < count ;i++, n++) {
3448 get_int_array_element(interp, varname, n, &v);
3449 switch (width) {
3450 case 4:
3451 target_buffer_set_u32(target, &buffer[i*width], v);
3452 break;
3453 case 2:
3454 target_buffer_set_u16(target, &buffer[i*width], v);
3455 break;
3456 case 1:
3457 buffer[i] = v & 0x0ff;
3458 break;
3459 }
3460 }
3461 len -= count;
3462
3463 retval = target_write_memory(target, addr, width, count, buffer);
3464 if (retval != ERROR_OK) {
3465 /* BOO !*/
3466 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3467 (unsigned int)addr,
3468 (int)width,
3469 (int)count);
3470 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3471 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3472 e = JIM_ERR;
3473 len = 0;
3474 }
3475 }
3476
3477 free(buffer);
3478
3479 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3480
3481 return JIM_OK;
3482 }
3483
3484 void target_all_handle_event(enum target_event e)
3485 {
3486 struct target *target;
3487
3488 LOG_DEBUG("**all*targets: event: %d, %s",
3489 (int)e,
3490 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3491
3492 target = all_targets;
3493 while (target) {
3494 target_handle_event(target, e);
3495 target = target->next;
3496 }
3497 }
3498
3499
3500 /* FIX? should we propagate errors here rather than printing them
3501 * and continuing?
3502 */
3503 void target_handle_event(struct target *target, enum target_event e)
3504 {
3505 struct target_event_action *teap;
3506
3507 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3508 if (teap->event == e) {
3509 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3510 target->target_number,
3511 target->cmd_name,
3512 target_get_name(target),
3513 e,
3514 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3515 Jim_GetString(teap->body, NULL));
3516 if (Jim_EvalObj(interp, teap->body) != JIM_OK)
3517 {
3518 Jim_PrintErrorMessage(interp);
3519 }
3520 }
3521 }
3522 }
3523
3524 enum target_cfg_param {
3525 TCFG_TYPE,
3526 TCFG_EVENT,
3527 TCFG_WORK_AREA_VIRT,
3528 TCFG_WORK_AREA_PHYS,
3529 TCFG_WORK_AREA_SIZE,
3530 TCFG_WORK_AREA_BACKUP,
3531 TCFG_ENDIAN,
3532 TCFG_VARIANT,
3533 TCFG_CHAIN_POSITION,
3534 };
3535
3536 static Jim_Nvp nvp_config_opts[] = {
3537 { .name = "-type", .value = TCFG_TYPE },
3538 { .name = "-event", .value = TCFG_EVENT },
3539 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3540 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3541 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3542 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3543 { .name = "-endian" , .value = TCFG_ENDIAN },
3544 { .name = "-variant", .value = TCFG_VARIANT },
3545 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3546
3547 { .name = NULL, .value = -1 }
3548 };
3549
3550 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
3551 {
3552 Jim_Nvp *n;
3553 Jim_Obj *o;
3554 jim_wide w;
3555 char *cp;
3556 int e;
3557
3558 /* parse config or cget options ... */
3559 while (goi->argc > 0) {
3560 Jim_SetEmptyResult(goi->interp);
3561 /* Jim_GetOpt_Debug(goi); */
3562
3563 if (target->type->target_jim_configure) {
3564 /* target defines a configure function */
3565 /* target gets first dibs on parameters */
3566 e = (*(target->type->target_jim_configure))(target, goi);
3567 if (e == JIM_OK) {
3568 /* more? */
3569 continue;
3570 }
3571 if (e == JIM_ERR) {
3572 /* An error */
3573 return e;
3574 }
3575 /* otherwise we 'continue' below */
3576 }
3577 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3578 if (e != JIM_OK) {
3579 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3580 return e;
3581 }
3582 switch (n->value) {
3583 case TCFG_TYPE:
3584 /* not setable */
3585 if (goi->isconfigure) {
3586 Jim_SetResult_sprintf(goi->interp, "not setable: %s", n->name);
3587 return JIM_ERR;
3588 } else {
3589 no_params:
3590 if (goi->argc != 0) {
3591 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "NO PARAMS");
3592 return JIM_ERR;
3593 }
3594 }
3595 Jim_SetResultString(goi->interp, target_get_name(target), -1);
3596 /* loop for more */
3597 break;
3598 case TCFG_EVENT:
3599 if (goi->argc == 0) {
3600 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3601 return JIM_ERR;
3602 }
3603
3604 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3605 if (e != JIM_OK) {
3606 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3607 return e;
3608 }
3609
3610 if (goi->isconfigure) {
3611 if (goi->argc != 1) {
3612 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3613 return JIM_ERR;
3614 }
3615 } else {
3616 if (goi->argc != 0) {
3617 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3618 return JIM_ERR;
3619 }
3620 }
3621
3622 {
3623 struct target_event_action *teap;
3624
3625 teap = target->event_action;
3626 /* replace existing? */
3627 while (teap) {
3628 if (teap->event == (enum target_event)n->value) {
3629 break;
3630 }
3631 teap = teap->next;
3632 }
3633
3634 if (goi->isconfigure) {
3635 bool replace = true;
3636 if (teap == NULL) {
3637 /* create new */
3638 teap = calloc(1, sizeof(*teap));
3639 replace = false;
3640 }
3641 teap->event = n->value;
3642 Jim_GetOpt_Obj(goi, &o);
3643 if (teap->body) {
3644 Jim_DecrRefCount(interp, teap->body);
3645 }
3646 teap->body = Jim_DuplicateObj(goi->interp, o);
3647 /*
3648 * FIXME:
3649 * Tcl/TK - "tk events" have a nice feature.
3650 * See the "BIND" command.
3651 * We should support that here.
3652 * You can specify %X and %Y in the event code.
3653 * The idea is: %T - target name.
3654 * The idea is: %N - target number
3655 * The idea is: %E - event name.
3656 */
3657 Jim_IncrRefCount(teap->body);
3658
3659 if (!replace)
3660 {
3661 /* add to head of event list */
3662 teap->next = target->event_action;
3663 target->event_action = teap;
3664 }
3665 Jim_SetEmptyResult(goi->interp);
3666 } else {
3667 /* get */
3668 if (teap == NULL) {
3669 Jim_SetEmptyResult(goi->interp);
3670 } else {
3671 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3672 }
3673 }
3674 }
3675 /* loop for more */
3676 break;
3677
3678 case TCFG_WORK_AREA_VIRT:
3679 if (goi->isconfigure) {
3680 target_free_all_working_areas(target);
3681 e = Jim_GetOpt_Wide(goi, &w);
3682 if (e != JIM_OK) {
3683 return e;
3684 }
3685 target->working_area_virt = w;
3686 target->working_area_virt_spec = true;
3687 } else {
3688 if (goi->argc != 0) {
3689 goto no_params;
3690 }
3691 }
3692 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3693 /* loop for more */
3694 break;
3695
3696 case TCFG_WORK_AREA_PHYS:
3697 if (goi->isconfigure) {
3698 target_free_all_working_areas(target);
3699 e = Jim_GetOpt_Wide(goi, &w);
3700 if (e != JIM_OK) {
3701 return e;
3702 }
3703 target->working_area_phys = w;
3704 target->working_area_phys_spec = true;
3705 } else {
3706 if (goi->argc != 0) {
3707 goto no_params;
3708 }
3709 }
3710 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3711 /* loop for more */
3712 break;
3713
3714 case TCFG_WORK_AREA_SIZE:
3715 if (goi->isconfigure) {
3716 target_free_all_working_areas(target);
3717 e = Jim_GetOpt_Wide(goi, &w);
3718 if (e != JIM_OK) {
3719 return e;
3720 }
3721 target->working_area_size = w;
3722 } else {
3723 if (goi->argc != 0) {
3724 goto no_params;
3725 }
3726 }
3727 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3728 /* loop for more */
3729 break;
3730
3731 case TCFG_WORK_AREA_BACKUP:
3732 if (goi->isconfigure) {
3733 target_free_all_working_areas(target);
3734 e = Jim_GetOpt_Wide(goi, &w);
3735 if (e != JIM_OK) {
3736 return e;
3737 }
3738 /* make this exactly 1 or 0 */
3739 target->backup_working_area = (!!w);
3740 } else {
3741 if (goi->argc != 0) {
3742 goto no_params;
3743 }
3744 }
3745 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3746 /* loop for more e*/
3747 break;
3748
3749 case TCFG_ENDIAN:
3750 if (goi->isconfigure) {
3751 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3752 if (e != JIM_OK) {
3753 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3754 return e;
3755 }
3756 target->endianness = n->value;
3757 } else {
3758 if (goi->argc != 0) {
3759 goto no_params;
3760 }
3761 }
3762 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3763 if (n->name == NULL) {
3764 target->endianness = TARGET_LITTLE_ENDIAN;
3765 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3766 }
3767 Jim_SetResultString(goi->interp, n->name, -1);
3768 /* loop for more */
3769 break;
3770
3771 case TCFG_VARIANT:
3772 if (goi->isconfigure) {
3773 if (goi->argc < 1) {
3774 Jim_SetResult_sprintf(goi->interp,
3775 "%s ?STRING?",
3776 n->name);
3777 return JIM_ERR;
3778 }
3779 if (target->variant) {
3780 free((void *)(target->variant));
3781 }
3782 e = Jim_GetOpt_String(goi, &cp, NULL);
3783 target->variant = strdup(cp);
3784 } else {
3785 if (goi->argc != 0) {
3786 goto no_params;
3787 }
3788 }
3789 Jim_SetResultString(goi->interp, target->variant,-1);
3790 /* loop for more */
3791 break;
3792 case TCFG_CHAIN_POSITION:
3793 if (goi->isconfigure) {
3794 Jim_Obj *o;
3795 struct jtag_tap *tap;
3796 target_free_all_working_areas(target);
3797 e = Jim_GetOpt_Obj(goi, &o);
3798 if (e != JIM_OK) {
3799 return e;
3800 }
3801 tap = jtag_tap_by_jim_obj(goi->interp, o);
3802 if (tap == NULL) {
3803 return JIM_ERR;
3804 }
3805 /* make this exactly 1 or 0 */
3806 target->tap = tap;
3807 } else {
3808 if (goi->argc != 0) {
3809 goto no_params;
3810 }
3811 }
3812 Jim_SetResultString(interp, target->tap->dotted_name, -1);
3813 /* loop for more e*/
3814 break;
3815 }
3816 } /* while (goi->argc) */
3817
3818
3819 /* done - we return */
3820 return JIM_OK;
3821 }
3822
3823 /** this is the 'tcl' handler for the target specific command */
3824 static int tcl_target_func(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3825 {
3826 Jim_GetOptInfo goi;
3827 jim_wide a,b,c;
3828 int x,y,z;
3829 uint8_t target_buf[32];
3830 Jim_Nvp *n;
3831 struct target *target;
3832 struct command_context *cmd_ctx;
3833 int e;
3834
3835 enum {
3836 TS_CMD_CONFIGURE,
3837 TS_CMD_CGET,
3838
3839 TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
3840 TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
3841 TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
3842 TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
3843 TS_CMD_EXAMINE,
3844 TS_CMD_POLL,
3845 TS_CMD_RESET,
3846 TS_CMD_HALT,
3847 TS_CMD_WAITSTATE,
3848 TS_CMD_EVENTLIST,
3849 TS_CMD_CURSTATE,
3850 TS_CMD_INVOKE_EVENT,
3851 };
3852
3853 static const Jim_Nvp target_options[] = {
3854 { .name = "configure", .value = TS_CMD_CONFIGURE },
3855 { .name = "cget", .value = TS_CMD_CGET },
3856 { .name = "mww", .value = TS_CMD_MWW },
3857 { .name = "mwh", .value = TS_CMD_MWH },
3858 { .name = "mwb", .value = TS_CMD_MWB },
3859 { .name = "mdw", .value = TS_CMD_MDW },
3860 { .name = "mdh", .value = TS_CMD_MDH },
3861 { .name = "mdb", .value = TS_CMD_MDB },
3862 { .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
3863 { .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
3864 { .name = "eventlist", .value = TS_CMD_EVENTLIST },
3865 { .name = "curstate", .value = TS_CMD_CURSTATE },
3866
3867 { .name = "arp_examine", .value = TS_CMD_EXAMINE },
3868 { .name = "arp_poll", .value = TS_CMD_POLL },
3869 { .name = "arp_reset", .value = TS_CMD_RESET },
3870 { .name = "arp_halt", .value = TS_CMD_HALT },
3871 { .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
3872 { .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
3873
3874 { .name = NULL, .value = -1 },
3875 };
3876
3877 /* go past the "command" */
3878 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
3879
3880 target = Jim_CmdPrivData(goi.interp);
3881 cmd_ctx = Jim_GetAssocData(goi.interp, "context");
3882
3883 /* commands here are in an NVP table */
3884 e = Jim_GetOpt_Nvp(&goi, target_options, &n);
3885 if (e != JIM_OK) {
3886 Jim_GetOpt_NvpUnknown(&goi, target_options, 0);
3887 return e;
3888 }
3889 /* Assume blank result */
3890 Jim_SetEmptyResult(goi.interp);
3891
3892 switch (n->value) {
3893 case TS_CMD_CONFIGURE:
3894 if (goi.argc < 2) {
3895 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
3896 return JIM_ERR;
3897 }
3898 goi.isconfigure = 1;
3899 return target_configure(&goi, target);
3900 case TS_CMD_CGET:
3901 // some things take params
3902 if (goi.argc < 1) {
3903 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "missing: ?-option?");
3904 return JIM_ERR;
3905 }
3906 goi.isconfigure = 0;
3907 return target_configure(&goi, target);
3908 break;
3909 case TS_CMD_MWW:
3910 case TS_CMD_MWH:
3911 case TS_CMD_MWB:
3912 /* argv[0] = cmd
3913 * argv[1] = address
3914 * argv[2] = data
3915 * argv[3] = optional count.
3916 */
3917
3918 if ((goi.argc == 2) || (goi.argc == 3)) {
3919 /* all is well */
3920 } else {
3921 mwx_error:
3922 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR DATA [COUNT]", n->name);
3923 return JIM_ERR;
3924 }
3925
3926 e = Jim_GetOpt_Wide(&goi, &a);
3927 if (e != JIM_OK) {
3928 goto mwx_error;
3929 }
3930
3931 e = Jim_GetOpt_Wide(&goi, &b);
3932 if (e != JIM_OK) {
3933 goto mwx_error;
3934 }
3935 if (goi.argc == 3) {
3936 e = Jim_GetOpt_Wide(&goi, &c);
3937 if (e != JIM_OK) {
3938 goto mwx_error;
3939 }
3940 } else {
3941 c = 1;
3942 }
3943
3944 switch (n->value) {
3945 case TS_CMD_MWW:
3946 target_buffer_set_u32(target, target_buf, b);
3947 b = 4;
3948 break;
3949 case TS_CMD_MWH:
3950 target_buffer_set_u16(target, target_buf, b);
3951 b = 2;
3952 break;
3953 case TS_CMD_MWB:
3954 target_buffer_set_u8(target, target_buf, b);
3955 b = 1;
3956 break;
3957 }
3958 for (x = 0 ; x < c ; x++) {
3959 e = target_write_memory(target, a, b, 1, target_buf);
3960 if (e != ERROR_OK) {
3961 Jim_SetResult_sprintf(interp, "Error writing @ 0x%08x: %d\n", (int)(a), e);
3962 return JIM_ERR;
3963 }
3964 /* b = width */
3965 a = a + b;
3966 }
3967 return JIM_OK;
3968 break;
3969
3970 /* display */
3971 case TS_CMD_MDW:
3972 case TS_CMD_MDH:
3973 case TS_CMD_MDB:
3974 /* argv[0] = command
3975 * argv[1] = address
3976 * argv[2] = optional count
3977 */
3978 if ((goi.argc == 2) || (goi.argc == 3)) {
3979 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR [COUNT]", n->name);
3980 return JIM_ERR;
3981 }
3982 e = Jim_GetOpt_Wide(&goi, &a);
3983 if (e != JIM_OK) {
3984 return JIM_ERR;
3985 }
3986 if (goi.argc) {
3987 e = Jim_GetOpt_Wide(&goi, &c);
3988 if (e != JIM_OK) {
3989 return JIM_ERR;
3990 }
3991 } else {
3992 c = 1;
3993 }
3994 b = 1; /* shut up gcc */
3995 switch (n->value) {
3996 case TS_CMD_MDW:
3997 b = 4;
3998 break;
3999 case TS_CMD_MDH:
4000 b = 2;
4001 break;
4002 case TS_CMD_MDB:
4003 b = 1;
4004 break;
4005 }
4006
4007 /* convert to "bytes" */
4008 c = c * b;
4009 /* count is now in 'BYTES' */
4010 while (c > 0) {
4011 y = c;
4012 if (y > 16) {
4013 y = 16;
4014 }
4015 e = target_read_memory(target, a, b, y / b, target_buf);
4016 if (e != ERROR_OK) {
4017 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
4018 return JIM_ERR;
4019 }
4020
4021 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
4022 switch (b) {
4023 case 4:
4024 for (x = 0 ; (x < 16) && (x < y) ; x += 4) {
4025 z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
4026 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
4027 }
4028 for (; (x < 16) ; x += 4) {
4029 Jim_fprintf(interp, interp->cookie_stdout, " ");
4030 }
4031 break;
4032 case 2:
4033 for (x = 0 ; (x < 16) && (x < y) ; x += 2) {
4034 z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
4035 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
4036 }
4037 for (; (x < 16) ; x += 2) {
4038 Jim_fprintf(interp, interp->cookie_stdout, " ");
4039 }
4040 break;
4041 case 1:
4042 default:
4043 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4044 z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
4045 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
4046 }
4047 for (; (x < 16) ; x += 1) {
4048 Jim_fprintf(interp, interp->cookie_stdout, " ");
4049 }
4050 break;
4051 }
4052 /* ascii-ify the bytes */
4053 for (x = 0 ; x < y ; x++) {
4054 if ((target_buf[x] >= 0x20) &&
4055 (target_buf[x] <= 0x7e)) {
4056 /* good */
4057 } else {
4058 /* smack it */
4059 target_buf[x] = '.';
4060 }
4061 }
4062 /* space pad */
4063 while (x < 16) {
4064 target_buf[x] = ' ';
4065 x++;
4066 }
4067 /* terminate */
4068 target_buf[16] = 0;
4069 /* print - with a newline */
4070 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
4071 /* NEXT... */
4072 c -= 16;
4073 a += 16;
4074 }
4075 return JIM_OK;
4076 case TS_CMD_MEM2ARRAY:
4077 return target_mem2array(goi.interp, target, goi.argc, goi.argv);
4078 break;
4079 case TS_CMD_ARRAY2MEM:
4080 return target_array2mem(goi.interp, target, goi.argc, goi.argv);
4081 break;
4082 case TS_CMD_EXAMINE:
4083 if (goi.argc) {
4084 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4085 return JIM_ERR;
4086 }
4087 if (!target->tap->enabled)
4088 goto err_tap_disabled;
4089 e = target->type->examine(target);
4090 if (e != ERROR_OK) {
4091 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
4092 return JIM_ERR;
4093 }
4094 return JIM_OK;
4095 case TS_CMD_POLL:
4096 if (goi.argc) {
4097 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4098 return JIM_ERR;
4099 }
4100 if (!target->tap->enabled)
4101 goto err_tap_disabled;
4102 if (!(target_was_examined(target))) {
4103 e = ERROR_TARGET_NOT_EXAMINED;
4104 } else {
4105 e = target->type->poll(target);
4106 }
4107 if (e != ERROR_OK) {
4108 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4109 return JIM_ERR;
4110 } else {
4111 return JIM_OK;
4112 }
4113 break;
4114 case TS_CMD_RESET:
4115 if (goi.argc != 2) {
4116 Jim_WrongNumArgs(interp, 2, argv,
4117 "([tT]|[fF]|assert|deassert) BOOL");
4118 return JIM_ERR;
4119 }
4120 e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4121 if (e != JIM_OK) {
4122 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4123 return e;
4124 }
4125 /* the halt or not param */
4126 e = Jim_GetOpt_Wide(&goi, &a);
4127 if (e != JIM_OK) {
4128 return e;
4129 }
4130 if (!target->tap->enabled)
4131 goto err_tap_disabled;
4132 if (!target->type->assert_reset
4133 || !target->type->deassert_reset) {
4134 Jim_SetResult_sprintf(interp,
4135 "No target-specific reset for %s",
4136 target->cmd_name);
4137 return JIM_ERR;
4138 }
4139 /* determine if we should halt or not. */
4140 target->reset_halt = !!a;
4141 /* When this happens - all workareas are invalid. */
4142 target_free_all_working_areas_restore(target, 0);
4143
4144 /* do the assert */
4145 if (n->value == NVP_ASSERT) {
4146 e = target->type->assert_reset(target);
4147 } else {
4148 e = target->type->deassert_reset(target);
4149 }
4150 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4151 case TS_CMD_HALT:
4152 if (goi.argc) {
4153 Jim_WrongNumArgs(goi.interp, 0, argv, "halt [no parameters]");
4154 return JIM_ERR;
4155 }
4156 if (!target->tap->enabled)
4157 goto err_tap_disabled;
4158 e = target->type->halt(target);
4159 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4160 case TS_CMD_WAITSTATE:
4161 /* params: <name> statename timeoutmsecs */
4162 if (goi.argc != 2) {
4163 Jim_SetResult_sprintf(goi.interp, "%s STATENAME TIMEOUTMSECS", n->name);
4164 return JIM_ERR;
4165 }
4166 e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4167 if (e != JIM_OK) {
4168 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4169 return e;
4170 }
4171 e = Jim_GetOpt_Wide(&goi, &a);
4172 if (e != JIM_OK) {
4173 return e;
4174 }
4175 if (!target->tap->enabled)
4176 goto err_tap_disabled;
4177 e = target_wait_state(target, n->value, a);
4178 if (e != ERROR_OK) {
4179 Jim_SetResult_sprintf(goi.interp,
4180 "target: %s wait %s fails (%d) %s",
4181 target->cmd_name,
4182 n->name,
4183 e, target_strerror_safe(e));
4184 return JIM_ERR;
4185 } else {
4186 return JIM_OK;
4187 }
4188 case TS_CMD_EVENTLIST:
4189 /* List for human, Events defined for this target.
4190 * scripts/programs should use 'name cget -event NAME'
4191 */
4192 {
4193 struct target_event_action *teap;
4194 teap = target->event_action;
4195 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4196 target->target_number,
4197 target->cmd_name);
4198 command_print(cmd_ctx, "%-25s | Body", "Event");
4199 command_print(cmd_ctx, "------------------------- | ----------------------------------------");
4200 while (teap) {
4201 command_print(cmd_ctx,
4202 "%-25s | %s",
4203 Jim_Nvp_value2name_simple(nvp_target_event, teap->event)->name,
4204 Jim_GetString(teap->body, NULL));
4205 teap = teap->next;
4206 }
4207 command_print(cmd_ctx, "***END***");
4208 return JIM_OK;
4209 }
4210 case TS_CMD_CURSTATE:
4211 if (goi.argc != 0) {
4212 Jim_WrongNumArgs(goi.interp, 0, argv, "[no parameters]");
4213 return JIM_ERR;
4214 }
4215 Jim_SetResultString(goi.interp,
4216 target_state_name( target ),
4217 -1);
4218 return JIM_OK;
4219 case TS_CMD_INVOKE_EVENT:
4220 if (goi.argc != 1) {
4221 Jim_SetResult_sprintf(goi.interp, "%s ?EVENTNAME?",n->name);
4222 return JIM_ERR;
4223 }
4224 e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4225 if (e != JIM_OK) {
4226 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4227 return e;
4228 }
4229 target_handle_event(target, n->value);
4230 return JIM_OK;
4231 }
4232 return JIM_ERR;
4233
4234 err_tap_disabled:
4235 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4236 return JIM_ERR;
4237 }
4238
4239 static int target_create(Jim_GetOptInfo *goi)
4240 {
4241 Jim_Obj *new_cmd;
4242 Jim_Cmd *cmd;
4243 const char *cp;
4244 char *cp2;
4245 int e;
4246 int x;
4247 struct target *target;
4248 struct command_context *cmd_ctx;
4249
4250 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4251 if (goi->argc < 3) {
4252 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4253 return JIM_ERR;
4254 }
4255
4256 /* COMMAND */
4257 Jim_GetOpt_Obj(goi, &new_cmd);
4258 /* does this command exist? */
4259 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4260 if (cmd) {
4261 cp = Jim_GetString(new_cmd, NULL);
4262 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4263 return JIM_ERR;
4264 }
4265
4266 /* TYPE */
4267 e = Jim_GetOpt_String(goi, &cp2, NULL);
4268 cp = cp2;
4269 /* now does target type exist */
4270 for (x = 0 ; target_types[x] ; x++) {
4271 if (0 == strcmp(cp, target_types[x]->name)) {
4272 /* found */
4273 break;
4274 }
4275 }
4276 if (target_types[x] == NULL) {
4277 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4278 for (x = 0 ; target_types[x] ; x++) {
4279 if (target_types[x + 1]) {
4280 Jim_AppendStrings(goi->interp,
4281 Jim_GetResult(goi->interp),
4282 target_types[x]->name,
4283 ", ", NULL);
4284 } else {
4285 Jim_AppendStrings(goi->interp,
4286 Jim_GetResult(goi->interp),
4287 " or ",
4288 target_types[x]->name,NULL);
4289 }
4290 }
4291 return JIM_ERR;
4292 }
4293
4294 /* Create it */
4295 target = calloc(1,sizeof(struct target));
4296 /* set target number */
4297 target->target_number = new_target_number();
4298
4299 /* allocate memory for each unique target type */
4300 target->type = (struct target_type*)calloc(1,sizeof(struct target_type));
4301
4302 memcpy(target->type, target_types[x], sizeof(struct target_type));
4303
4304 /* will be set by "-endian" */
4305 target->endianness = TARGET_ENDIAN_UNKNOWN;
4306
4307 target->working_area = 0x0;
4308 target->working_area_size = 0x0;
4309 target->working_areas = NULL;
4310 target->backup_working_area = 0;
4311
4312 target->state = TARGET_UNKNOWN;
4313 target->debug_reason = DBG_REASON_UNDEFINED;
4314 target->reg_cache = NULL;
4315 target->breakpoints = NULL;
4316 target->watchpoints = NULL;
4317 target->next = NULL;
4318 target->arch_info = NULL;
4319
4320 target->display = 1;
4321
4322 target->halt_issued = false;
4323
4324 /* initialize trace information */
4325 target->trace_info = malloc(sizeof(struct trace));
4326 target->trace_info->num_trace_points = 0;
4327 target->trace_info->trace_points_size = 0;
4328 target->trace_info->trace_points = NULL;
4329 target->trace_info->trace_history_size = 0;
4330 target->trace_info->trace_history = NULL;
4331 target->trace_info->trace_history_pos = 0;
4332 target->trace_info->trace_history_overflowed = 0;
4333
4334 target->dbgmsg = NULL;
4335 target->dbg_msg_enabled = 0;
4336
4337 target->endianness = TARGET_ENDIAN_UNKNOWN;
4338
4339 /* Do the rest as "configure" options */
4340 goi->isconfigure = 1;
4341 e = target_configure(goi, target);
4342
4343 if (target->tap == NULL)
4344 {
4345 Jim_SetResultString(interp, "-chain-position required when creating target", -1);
4346 e = JIM_ERR;
4347 }
4348
4349 if (e != JIM_OK) {
4350 free(target->type);
4351 free(target);
4352 return e;
4353 }
4354
4355 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4356 /* default endian to little if not specified */
4357 target->endianness = TARGET_LITTLE_ENDIAN;
4358 }
4359
4360 /* incase variant is not set */
4361 if (!target->variant)
4362 target->variant = strdup("");
4363
4364 cp = Jim_GetString(new_cmd, NULL);
4365 target->cmd_name = strdup(cp);
4366
4367 /* create the target specific commands */
4368 if (target->type->commands) {
4369 e = register_commands(cmd_ctx, NULL, target->type->commands);
4370 if (ERROR_OK != e)
4371 LOG_ERROR("unable to register '%s' commands", cp);
4372 }
4373 if (target->type->target_create) {
4374 (*(target->type->target_create))(target, goi->interp);
4375 }
4376
4377 /* append to end of list */
4378 {
4379 struct target **tpp;
4380 tpp = &(all_targets);
4381 while (*tpp) {
4382 tpp = &((*tpp)->next);
4383 }
4384 *tpp = target;
4385 }
4386
4387 /* now - create the new target name command */
4388 const struct command_registration target_command = {
4389 .name = cp,
4390 .jim_handler = &tcl_target_func,
4391 .jim_handler_data = target,
4392 .help = "target command group",
4393 };
4394 struct command *c = register_command(cmd_ctx, NULL, &target_command);
4395 return (NULL != c) ? ERROR_OK : ERROR_FAIL;
4396 }
4397
4398 static int jim_target(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4399 {
4400 int x,r,e;
4401 jim_wide w;
4402 struct command_context *cmd_ctx;
4403 struct target *target;
4404 Jim_GetOptInfo goi;
4405 enum tcmd {
4406 /* TG = target generic */
4407 TG_CMD_CREATE,
4408 TG_CMD_TYPES,
4409 TG_CMD_NAMES,
4410 TG_CMD_CURRENT,
4411 TG_CMD_NUMBER,
4412 TG_CMD_COUNT,
4413 };
4414 const char *target_cmds[] = {
4415 "create", "types", "names", "current", "number",
4416 "count",
4417 NULL /* terminate */
4418 };
4419
4420 LOG_DEBUG("Target command params:");
4421 LOG_DEBUG("%s", Jim_Debug_ArgvString(interp, argc, argv));
4422
4423 cmd_ctx = Jim_GetAssocData(interp, "context");
4424
4425 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
4426
4427 if (goi.argc == 0) {
4428 Jim_WrongNumArgs(interp, 1, argv, "missing: command ...");
4429 return JIM_ERR;
4430 }
4431
4432 /* Jim_GetOpt_Debug(&goi); */
4433 r = Jim_GetOpt_Enum(&goi, target_cmds, &x);
4434 if (r != JIM_OK) {
4435 return r;
4436 }
4437
4438 switch (x) {
4439 default:
4440 Jim_Panic(goi.interp,"Why am I here?");
4441 return JIM_ERR;
4442 case TG_CMD_CURRENT:
4443 if (goi.argc != 0) {
4444 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4445 return JIM_ERR;
4446 }
4447 Jim_SetResultString(goi.interp, get_current_target(cmd_ctx)->cmd_name, -1);
4448 return JIM_OK;
4449 case TG_CMD_TYPES:
4450 if (goi.argc != 0) {
4451 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4452 return JIM_ERR;
4453 }
4454 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4455 for (x = 0 ; target_types[x] ; x++) {
4456 Jim_ListAppendElement(goi.interp,
4457 Jim_GetResult(goi.interp),
4458 Jim_NewStringObj(goi.interp, target_types[x]->name, -1));
4459 }
4460 return JIM_OK;
4461 case TG_CMD_NAMES:
4462 if (goi.argc != 0) {
4463 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4464 return JIM_ERR;
4465 }
4466 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4467 target = all_targets;
4468 while (target) {
4469 Jim_ListAppendElement(goi.interp,
4470 Jim_GetResult(goi.interp),
4471 Jim_NewStringObj(goi.interp, target->cmd_name, -1));
4472 target = target->next;
4473 }
4474 return JIM_OK;
4475 case TG_CMD_CREATE:
4476 if (goi.argc < 3) {
4477 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "?name ... config options ...");
4478 return JIM_ERR;
4479 }
4480 return target_create(&goi);
4481 break;
4482 case TG_CMD_NUMBER:
4483 /* It's OK to remove this mechanism sometime after August 2010 or so */
4484 LOG_WARNING("don't use numbers as target identifiers; use names");
4485 if (goi.argc != 1) {
4486 Jim_SetResult_sprintf(goi.interp, "expected: target number ?NUMBER?");
4487 return JIM_ERR;
4488 }
4489 e = Jim_GetOpt_Wide(&goi, &w);
4490 if (e != JIM_OK) {
4491 return JIM_ERR;
4492 }
4493 for (x = 0, target = all_targets; target; target = target->next, x++) {
4494 if (target->target_number == w)
4495 break;
4496 }
4497 if (target == NULL) {
4498 Jim_SetResult_sprintf(goi.interp,
4499 "Target: number %d does not exist", (int)(w));
4500 return JIM_ERR;
4501 }
4502 Jim_SetResultString(goi.interp, target->cmd_name, -1);
4503 return JIM_OK;
4504 case TG_CMD_COUNT:
4505 if (goi.argc != 0) {
4506 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "<no parameters>");
4507 return JIM_ERR;
4508 }
4509 for (x = 0, target = all_targets; target; target = target->next, x++)
4510 continue;
4511 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, x));
4512 return JIM_OK;
4513 }
4514
4515 return JIM_ERR;
4516 }
4517
4518
4519 struct FastLoad
4520 {
4521 uint32_t address;
4522 uint8_t *data;
4523 int length;
4524
4525 };
4526
4527 static int fastload_num;
4528 static struct FastLoad *fastload;
4529
4530 static void free_fastload(void)
4531 {
4532 if (fastload != NULL)
4533 {
4534 int i;
4535 for (i = 0; i < fastload_num; i++)
4536 {
4537 if (fastload[i].data)
4538 free(fastload[i].data);
4539 }
4540 free(fastload);
4541 fastload = NULL;
4542 }
4543 }
4544
4545
4546
4547
4548 COMMAND_HANDLER(handle_fast_load_image_command)
4549 {
4550 uint8_t *buffer;
4551 size_t buf_cnt;
4552 uint32_t image_size;
4553 uint32_t min_address = 0;
4554 uint32_t max_address = 0xffffffff;
4555 int i;
4556
4557 struct image image;
4558
4559 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
4560 &image, &min_address, &max_address);
4561 if (ERROR_OK != retval)
4562 return retval;
4563
4564 struct duration bench;
4565 duration_start(&bench);
4566
4567 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
4568 {
4569 return ERROR_OK;
4570 }
4571
4572 image_size = 0x0;
4573 retval = ERROR_OK;
4574 fastload_num = image.num_sections;
4575 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4576 if (fastload == NULL)
4577 {
4578 image_close(&image);
4579 return ERROR_FAIL;
4580 }
4581 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4582 for (i = 0; i < image.num_sections; i++)
4583 {
4584 buffer = malloc(image.sections[i].size);
4585 if (buffer == NULL)
4586 {
4587 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
4588 (int)(image.sections[i].size));
4589 break;
4590 }
4591
4592 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4593 {
4594 free(buffer);
4595 break;
4596 }
4597
4598 uint32_t offset = 0;
4599 uint32_t length = buf_cnt;
4600
4601
4602 /* DANGER!!! beware of unsigned comparision here!!! */
4603
4604 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4605 (image.sections[i].base_address < max_address))
4606 {
4607 if (image.sections[i].base_address < min_address)
4608 {
4609 /* clip addresses below */
4610 offset += min_address-image.sections[i].base_address;
4611 length -= offset;
4612 }
4613
4614 if (image.sections[i].base_address + buf_cnt > max_address)
4615 {
4616 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4617 }
4618
4619 fastload[i].address = image.sections[i].base_address + offset;
4620 fastload[i].data = malloc(length);
4621 if (fastload[i].data == NULL)
4622 {
4623 free(buffer);
4624 break;
4625 }
4626 memcpy(fastload[i].data, buffer + offset, length);
4627 fastload[i].length = length;
4628
4629 image_size += length;
4630 command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
4631 (unsigned int)length,
4632 ((unsigned int)(image.sections[i].base_address + offset)));
4633 }
4634
4635 free(buffer);
4636 }
4637
4638 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
4639 {
4640 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
4641 "in %fs (%0.3f kb/s)", image_size,
4642 duration_elapsed(&bench), duration_kbps(&bench, image_size));
4643
4644 command_print(CMD_CTX,
4645 "WARNING: image has not been loaded to target!"
4646 "You can issue a 'fast_load' to finish loading.");
4647 }
4648
4649 image_close(&image);
4650
4651 if (retval != ERROR_OK)
4652 {
4653 free_fastload();
4654 }
4655
4656 return retval;
4657 }
4658
4659 COMMAND_HANDLER(handle_fast_load_command)
4660 {
4661 if (CMD_ARGC > 0)
4662 return ERROR_COMMAND_SYNTAX_ERROR;
4663 if (fastload == NULL)
4664 {
4665 LOG_ERROR("No image in memory");
4666 return ERROR_FAIL;
4667 }
4668 int i;
4669 int ms = timeval_ms();
4670 int size = 0;
4671 int retval = ERROR_OK;
4672 for (i = 0; i < fastload_num;i++)
4673 {
4674 struct target *target = get_current_target(CMD_CTX);
4675 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
4676 (unsigned int)(fastload[i].address),
4677 (unsigned int)(fastload[i].length));
4678 if (retval == ERROR_OK)
4679 {
4680 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4681 }
4682 size += fastload[i].length;
4683 }
4684 int after = timeval_ms();
4685 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
4686 return retval;
4687 }
4688
4689 static int jim_mcrmrc(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4690 {
4691 struct command_context *context;
4692 struct target *target;
4693 int retval;
4694
4695 context = Jim_GetAssocData(interp, "context");
4696 if (context == NULL) {
4697 LOG_ERROR("array2mem: no command context");
4698 return JIM_ERR;
4699 }
4700 target = get_current_target(context);
4701 if (target == NULL) {
4702 LOG_ERROR("array2mem: no current target");
4703 return JIM_ERR;
4704 }
4705
4706 if ((argc < 6) || (argc > 7))
4707 {
4708 return JIM_ERR;
4709 }
4710
4711 int cpnum;
4712 uint32_t op1;
4713 uint32_t op2;
4714 uint32_t CRn;
4715 uint32_t CRm;
4716 uint32_t value;
4717
4718 int e;
4719 long l;
4720 e = Jim_GetLong(interp, argv[1], &l);
4721 if (e != JIM_OK) {
4722 return e;
4723 }
4724 cpnum = l;
4725
4726 e = Jim_GetLong(interp, argv[2], &l);
4727 if (e != JIM_OK) {
4728 return e;
4729 }
4730 op1 = l;
4731
4732 e = Jim_GetLong(interp, argv[3], &l);
4733 if (e != JIM_OK) {
4734 return e;
4735 }
4736 CRn = l;
4737
4738 e = Jim_GetLong(interp, argv[4], &l);
4739 if (e != JIM_OK) {
4740 return e;
4741 }
4742 CRm = l;
4743
4744 e = Jim_GetLong(interp, argv[5], &l);
4745 if (e != JIM_OK) {
4746 return e;
4747 }
4748 op2 = l;
4749
4750 value = 0;
4751
4752 if (argc == 7)
4753 {
4754 e = Jim_GetLong(interp, argv[6], &l);
4755 if (e != JIM_OK) {
4756 return e;
4757 }
4758 value = l;
4759
4760 retval = target_mcr(target, cpnum, op1, op2, CRn, CRm, value);
4761 if (retval != ERROR_OK)
4762 return JIM_ERR;
4763 } else
4764 {
4765 retval = target_mrc(target, cpnum, op1, op2, CRn, CRm, &value);
4766 if (retval != ERROR_OK)
4767 return JIM_ERR;
4768
4769 Jim_SetResult(interp, Jim_NewIntObj(interp, value));
4770 }
4771
4772 return JIM_OK;
4773 }
4774
4775 static const struct command_registration target_command_handlers[] = {
4776 {
4777 .name = "targets",
4778 .handler = &handle_targets_command,
4779 .mode = COMMAND_ANY,
4780 .help = "change current command line target (one parameter) "
4781 "or list targets (no parameters)",
4782 .usage = "[<new_current_target>]",
4783 },
4784 {
4785 .name = "target",
4786 .mode = COMMAND_CONFIG,
4787 .jim_handler = &jim_target,
4788 .help = "configure target",
4789 },
4790 COMMAND_REGISTRATION_DONE
4791 };
4792
4793 int target_register_commands(struct command_context *cmd_ctx)
4794 {
4795 return register_commands(cmd_ctx, NULL, target_command_handlers);
4796 }
4797
4798 static const struct command_registration target_exec_command_handlers[] = {
4799 {
4800 .name = "fast_load_image",
4801 .handler = &handle_fast_load_image_command,
4802 .mode = COMMAND_ANY,
4803 .help = "Load image into memory, mainly for profiling purposes",
4804 .usage = "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
4805 "[min_address] [max_length]",
4806 },
4807 {
4808 .name = "fast_load",
4809 .handler = &handle_fast_load_command,
4810 .mode = COMMAND_ANY,
4811 .help = "loads active fast load image to current target "
4812 "- mainly for profiling purposes",
4813 },
4814 {
4815 .name = "profile",
4816 .handler = &handle_profile_command,
4817 .mode = COMMAND_EXEC,
4818 .help = "profiling samples the CPU PC",
4819 },
4820 /** @todo don't register virt2phys() unless target supports it */
4821 {
4822 .name = "virt2phys",
4823 .handler = &handle_virt2phys_command,
4824 .mode = COMMAND_ANY,
4825 .help = "translate a virtual address into a physical address",
4826 },
4827
4828 {
4829 .name = "reg",
4830 .handler = &handle_reg_command,
4831 .mode = COMMAND_EXEC,
4832 .help = "display or set a register",
4833 },
4834
4835 {
4836 .name = "poll",
4837 .handler = &handle_poll_command,
4838 .mode = COMMAND_EXEC,
4839 .help = "poll target state",
4840 },
4841 {
4842 .name = "wait_halt",
4843 .handler = &handle_wait_halt_command,
4844 .mode = COMMAND_EXEC,
4845 .help = "wait for target halt",
4846 .usage = "[time (s)]",
4847 },
4848 {
4849 .name = "halt",
4850 .handler = &handle_halt_command,
4851 .mode = COMMAND_EXEC,
4852 .help = "halt target",
4853 },
4854 {
4855 .name = "resume",
4856 .handler = &handle_resume_command,
4857 .mode = COMMAND_EXEC,
4858 .help = "resume target",
4859 .usage = "[<address>]",
4860 },
4861 {
4862 .name = "reset",
4863 .handler = &handle_reset_command,
4864 .mode = COMMAND_EXEC,
4865 .usage = "[run|halt|init]",
4866 .help = "Reset all targets into the specified mode."
4867 "Default reset mode is run, if not given.",
4868 },
4869 {
4870 .name = "soft_reset_halt",
4871 .handler = &handle_soft_reset_halt_command,
4872 .mode = COMMAND_EXEC,
4873 .help = "halt the target and do a soft reset",
4874 },
4875 {
4876
4877 .name = "step",
4878 .handler = &handle_step_command,
4879 .mode = COMMAND_EXEC,
4880 .help = "step one instruction from current PC or [addr]",
4881 .usage = "[<address>]",
4882 },
4883 {
4884
4885 .name = "mdw",
4886 .handler = &handle_md_command,
4887 .mode = COMMAND_EXEC,
4888 .help = "display memory words",
4889 .usage = "[phys] <addr> [count]",
4890 },
4891 {
4892 .name = "mdh",
4893 .handler = &handle_md_command,
4894 .mode = COMMAND_EXEC,
4895 .help = "display memory half-words",
4896 .usage = "[phys] <addr> [count]",
4897 },
4898 {
4899 .name = "mdb",
4900 .handler = &handle_md_command,
4901 .mode = COMMAND_EXEC,
4902 .help = "display memory bytes",
4903 .usage = "[phys] <addr> [count]",
4904 },
4905 {
4906
4907 .name = "mww",
4908 .handler = &handle_mw_command,
4909 .mode = COMMAND_EXEC,
4910 .help = "write memory word",
4911 .usage = "[phys] <addr> <value> [count]",
4912 },
4913 {
4914 .name = "mwh",
4915 .handler = &handle_mw_command,
4916 .mode = COMMAND_EXEC,
4917 .help = "write memory half-word",
4918 .usage = "[phys] <addr> <value> [count]",
4919 },
4920 {
4921 .name = "mwb",
4922 .handler = &handle_mw_command,
4923 .mode = COMMAND_EXEC,
4924 .help = "write memory byte",
4925 .usage = "[phys] <addr> <value> [count]",
4926 },
4927 {
4928
4929 .name = "bp",
4930 .handler = &handle_bp_command,
4931 .mode = COMMAND_EXEC,
4932 .help = "list or set breakpoint",
4933 .usage = "[<address> <length> [hw]]",
4934 },
4935 {
4936 .name = "rbp",
4937 .handler = &handle_rbp_command,
4938 .mode = COMMAND_EXEC,
4939 .help = "remove breakpoint",
4940 .usage = "<address>",
4941 },
4942 {
4943
4944 .name = "wp",
4945 .handler = &handle_wp_command,
4946 .mode = COMMAND_EXEC,
4947 .help = "list or set watchpoint",
4948 .usage = "[<address> <length> <r/w/a> [value] [mask]]",
4949 },
4950 {
4951 .name = "rwp",
4952 .handler = &handle_rwp_command,
4953 .mode = COMMAND_EXEC,
4954 .help = "remove watchpoint",
4955 .usage = "<address>",
4956
4957 },
4958 {
4959 .name = "load_image",
4960 .handler = &handle_load_image_command,
4961 .mode = COMMAND_EXEC,
4962 .usage = "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
4963 "[min_address] [max_length]",
4964 },
4965 {
4966 .name = "dump_image",
4967 .handler = &handle_dump_image_command,
4968 .mode = COMMAND_EXEC,
4969 .usage = "<file> <address> <size>",
4970 },
4971 {
4972 .name = "verify_image",
4973 .handler = &handle_verify_image_command,
4974 .mode = COMMAND_EXEC,
4975 .usage = "<file> [offset] [type]",
4976 },
4977 {
4978 .name = "test_image",
4979 .handler = &handle_test_image_command,
4980 .mode = COMMAND_EXEC,
4981 .usage = "<file> [offset] [type]",
4982 },
4983 {
4984 .name = "ocd_mem2array",
4985 .mode = COMMAND_EXEC,
4986 .jim_handler = &jim_mem2array,
4987 .help = "read memory and return as a TCL array "
4988 "for script processing",
4989 .usage = "<arrayname> <width=32|16|8> <address> <count>",
4990 },
4991 {
4992 .name = "ocd_array2mem",
4993 .mode = COMMAND_EXEC,
4994 .jim_handler = &jim_array2mem,
4995 .help = "convert a TCL array to memory locations "
4996 "and write the values",
4997 .usage = "<arrayname> <width=32|16|8> <address> <count>",
4998 },
4999 COMMAND_REGISTRATION_DONE
5000 };
5001 int target_register_user_commands(struct command_context *cmd_ctx)
5002 {
5003 int retval = ERROR_OK;
5004 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
5005 return retval;
5006
5007 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
5008 return retval;
5009
5010
5011 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);
5012 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)