target: avoid using interp global variable
[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 extern struct target_type testee_target;
69
70 struct target_type *target_types[] =
71 {
72 &arm7tdmi_target,
73 &arm9tdmi_target,
74 &arm920t_target,
75 &arm720t_target,
76 &arm966e_target,
77 &arm926ejs_target,
78 &fa526_target,
79 &feroceon_target,
80 &dragonite_target,
81 &xscale_target,
82 &cortexm3_target,
83 &cortexa8_target,
84 &arm11_target,
85 &mips_m4k_target,
86 &avr_target,
87 &testee_target,
88 NULL,
89 };
90
91 struct target *all_targets = NULL;
92 struct target_event_callback *target_event_callbacks = NULL;
93 struct target_timer_callback *target_timer_callbacks = NULL;
94
95 const Jim_Nvp nvp_assert[] = {
96 { .name = "assert", NVP_ASSERT },
97 { .name = "deassert", NVP_DEASSERT },
98 { .name = "T", NVP_ASSERT },
99 { .name = "F", NVP_DEASSERT },
100 { .name = "t", NVP_ASSERT },
101 { .name = "f", NVP_DEASSERT },
102 { .name = NULL, .value = -1 }
103 };
104
105 const Jim_Nvp nvp_error_target[] = {
106 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
107 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
108 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
109 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
110 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
111 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
112 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
113 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
114 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
115 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
116 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
117 { .value = -1, .name = NULL }
118 };
119
120 const char *target_strerror_safe(int err)
121 {
122 const Jim_Nvp *n;
123
124 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
125 if (n->name == NULL) {
126 return "unknown";
127 } else {
128 return n->name;
129 }
130 }
131
132 static const Jim_Nvp nvp_target_event[] = {
133 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
134 { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
135
136 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
137 { .value = TARGET_EVENT_HALTED, .name = "halted" },
138 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
139 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
140 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
141
142 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
143 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
144
145 /* historical name */
146
147 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
148
149 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
150 { .value = TARGET_EVENT_RESET_ASSERT, .name = "reset-assert" },
151 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
152 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
153 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
154 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
155 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
156 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
157 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
158 { .value = TARGET_EVENT_RESET_INIT, .name = "reset-init" },
159 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
160
161 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
162 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
163
164 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
165 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
166
167 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
168 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
169
170 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
171 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
172
173 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
174 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
175
176 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
177 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
178 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
179
180 { .name = NULL, .value = -1 }
181 };
182
183 const Jim_Nvp nvp_target_state[] = {
184 { .name = "unknown", .value = TARGET_UNKNOWN },
185 { .name = "running", .value = TARGET_RUNNING },
186 { .name = "halted", .value = TARGET_HALTED },
187 { .name = "reset", .value = TARGET_RESET },
188 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
189 { .name = NULL, .value = -1 },
190 };
191
192 const Jim_Nvp nvp_target_debug_reason [] = {
193 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
194 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
195 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
196 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
197 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
198 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
199 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
200 { .name = NULL, .value = -1 },
201 };
202
203 const Jim_Nvp nvp_target_endian[] = {
204 { .name = "big", .value = TARGET_BIG_ENDIAN },
205 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
206 { .name = "be", .value = TARGET_BIG_ENDIAN },
207 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
208 { .name = NULL, .value = -1 },
209 };
210
211 const Jim_Nvp nvp_reset_modes[] = {
212 { .name = "unknown", .value = RESET_UNKNOWN },
213 { .name = "run" , .value = RESET_RUN },
214 { .name = "halt" , .value = RESET_HALT },
215 { .name = "init" , .value = RESET_INIT },
216 { .name = NULL , .value = -1 },
217 };
218
219 const char *
220 target_state_name( struct target *t )
221 {
222 const char *cp;
223 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
224 if( !cp ){
225 LOG_ERROR("Invalid target state: %d", (int)(t->state));
226 cp = "(*BUG*unknown*BUG*)";
227 }
228 return cp;
229 }
230
231 /* determine the number of the new target */
232 static int new_target_number(void)
233 {
234 struct target *t;
235 int x;
236
237 /* number is 0 based */
238 x = -1;
239 t = all_targets;
240 while (t) {
241 if (x < t->target_number) {
242 x = t->target_number;
243 }
244 t = t->next;
245 }
246 return x + 1;
247 }
248
249 /* read a uint32_t from a buffer in target memory endianness */
250 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
251 {
252 if (target->endianness == TARGET_LITTLE_ENDIAN)
253 return le_to_h_u32(buffer);
254 else
255 return be_to_h_u32(buffer);
256 }
257
258 /* read a uint16_t from a buffer in target memory endianness */
259 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
260 {
261 if (target->endianness == TARGET_LITTLE_ENDIAN)
262 return le_to_h_u16(buffer);
263 else
264 return be_to_h_u16(buffer);
265 }
266
267 /* read a uint8_t from a buffer in target memory endianness */
268 uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
269 {
270 return *buffer & 0x0ff;
271 }
272
273 /* write a uint32_t to a buffer in target memory endianness */
274 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
275 {
276 if (target->endianness == TARGET_LITTLE_ENDIAN)
277 h_u32_to_le(buffer, value);
278 else
279 h_u32_to_be(buffer, value);
280 }
281
282 /* write a uint16_t to a buffer in target memory endianness */
283 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
284 {
285 if (target->endianness == TARGET_LITTLE_ENDIAN)
286 h_u16_to_le(buffer, value);
287 else
288 h_u16_to_be(buffer, value);
289 }
290
291 /* write a uint8_t to a buffer in target memory endianness */
292 void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
293 {
294 *buffer = value;
295 }
296
297 /* return a pointer to a configured target; id is name or number */
298 struct target *get_target(const char *id)
299 {
300 struct target *target;
301
302 /* try as tcltarget name */
303 for (target = all_targets; target; target = target->next) {
304 if (target->cmd_name == NULL)
305 continue;
306 if (strcmp(id, target->cmd_name) == 0)
307 return target;
308 }
309
310 /* It's OK to remove this fallback sometime after August 2010 or so */
311
312 /* no match, try as number */
313 unsigned num;
314 if (parse_uint(id, &num) != ERROR_OK)
315 return NULL;
316
317 for (target = all_targets; target; target = target->next) {
318 if (target->target_number == (int)num) {
319 LOG_WARNING("use '%s' as target identifier, not '%u'",
320 target->cmd_name, num);
321 return target;
322 }
323 }
324
325 return NULL;
326 }
327
328 /* returns a pointer to the n-th configured target */
329 static struct target *get_target_by_num(int num)
330 {
331 struct target *target = all_targets;
332
333 while (target) {
334 if (target->target_number == num) {
335 return target;
336 }
337 target = target->next;
338 }
339
340 return NULL;
341 }
342
343 struct target* get_current_target(struct command_context *cmd_ctx)
344 {
345 struct target *target = get_target_by_num(cmd_ctx->current_target);
346
347 if (target == NULL)
348 {
349 LOG_ERROR("BUG: current_target out of bounds");
350 exit(-1);
351 }
352
353 return target;
354 }
355
356 int target_poll(struct target *target)
357 {
358 int retval;
359
360 /* We can't poll until after examine */
361 if (!target_was_examined(target))
362 {
363 /* Fail silently lest we pollute the log */
364 return ERROR_FAIL;
365 }
366
367 retval = target->type->poll(target);
368 if (retval != ERROR_OK)
369 return retval;
370
371 if (target->halt_issued)
372 {
373 if (target->state == TARGET_HALTED)
374 {
375 target->halt_issued = false;
376 } else
377 {
378 long long t = timeval_ms() - target->halt_issued_time;
379 if (t>1000)
380 {
381 target->halt_issued = false;
382 LOG_INFO("Halt timed out, wake up GDB.");
383 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
384 }
385 }
386 }
387
388 return ERROR_OK;
389 }
390
391 int target_halt(struct target *target)
392 {
393 int retval;
394 /* We can't poll until after examine */
395 if (!target_was_examined(target))
396 {
397 LOG_ERROR("Target not examined yet");
398 return ERROR_FAIL;
399 }
400
401 retval = target->type->halt(target);
402 if (retval != ERROR_OK)
403 return retval;
404
405 target->halt_issued = true;
406 target->halt_issued_time = timeval_ms();
407
408 return ERROR_OK;
409 }
410
411 int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
412 {
413 int retval;
414
415 /* We can't poll until after examine */
416 if (!target_was_examined(target))
417 {
418 LOG_ERROR("Target not examined yet");
419 return ERROR_FAIL;
420 }
421
422 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
423 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
424 * the application.
425 */
426 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
427 return retval;
428
429 return retval;
430 }
431
432 int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
433 {
434 char buf[100];
435 int retval;
436 Jim_Nvp *n;
437 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
438 if (n->name == NULL) {
439 LOG_ERROR("invalid reset mode");
440 return ERROR_FAIL;
441 }
442
443 /* disable polling during reset to make reset event scripts
444 * more predictable, i.e. dr/irscan & pathmove in events will
445 * not have JTAG operations injected into the middle of a sequence.
446 */
447 bool save_poll = jtag_poll_get_enabled();
448
449 jtag_poll_set_enabled(false);
450
451 sprintf(buf, "ocd_process_reset %s", n->name);
452 retval = Jim_Eval(interp, buf);
453
454 jtag_poll_set_enabled(save_poll);
455
456 if (retval != JIM_OK) {
457 Jim_PrintErrorMessage(interp);
458 return ERROR_FAIL;
459 }
460
461 /* We want any events to be processed before the prompt */
462 retval = target_call_timer_callbacks_now();
463
464 return retval;
465 }
466
467 static int identity_virt2phys(struct target *target,
468 uint32_t virtual, uint32_t *physical)
469 {
470 *physical = virtual;
471 return ERROR_OK;
472 }
473
474 static int no_mmu(struct target *target, int *enabled)
475 {
476 *enabled = 0;
477 return ERROR_OK;
478 }
479
480 static int default_examine(struct target *target)
481 {
482 target_set_examined(target);
483 return ERROR_OK;
484 }
485
486 int target_examine_one(struct target *target)
487 {
488 return target->type->examine(target);
489 }
490
491 static int jtag_enable_callback(enum jtag_event event, void *priv)
492 {
493 struct target *target = priv;
494
495 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
496 return ERROR_OK;
497
498 jtag_unregister_event_callback(jtag_enable_callback, target);
499 return target_examine_one(target);
500 }
501
502
503 /* Targets that correctly implement init + examine, i.e.
504 * no communication with target during init:
505 *
506 * XScale
507 */
508 int target_examine(void)
509 {
510 int retval = ERROR_OK;
511 struct target *target;
512
513 for (target = all_targets; target; target = target->next)
514 {
515 /* defer examination, but don't skip it */
516 if (!target->tap->enabled) {
517 jtag_register_event_callback(jtag_enable_callback,
518 target);
519 continue;
520 }
521 if ((retval = target_examine_one(target)) != ERROR_OK)
522 return retval;
523 }
524 return retval;
525 }
526 const char *target_type_name(struct target *target)
527 {
528 return target->type->name;
529 }
530
531 static int target_write_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
532 {
533 if (!target_was_examined(target))
534 {
535 LOG_ERROR("Target not examined yet");
536 return ERROR_FAIL;
537 }
538 return target->type->write_memory_imp(target, address, size, count, buffer);
539 }
540
541 static int target_read_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
542 {
543 if (!target_was_examined(target))
544 {
545 LOG_ERROR("Target not examined yet");
546 return ERROR_FAIL;
547 }
548 return target->type->read_memory_imp(target, address, size, count, buffer);
549 }
550
551 static int target_soft_reset_halt_imp(struct target *target)
552 {
553 if (!target_was_examined(target))
554 {
555 LOG_ERROR("Target not examined yet");
556 return ERROR_FAIL;
557 }
558 if (!target->type->soft_reset_halt_imp) {
559 LOG_ERROR("Target %s does not support soft_reset_halt",
560 target_name(target));
561 return ERROR_FAIL;
562 }
563 return target->type->soft_reset_halt_imp(target);
564 }
565
566 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)
567 {
568 if (!target_was_examined(target))
569 {
570 LOG_ERROR("Target not examined yet");
571 return ERROR_FAIL;
572 }
573 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);
574 }
575
576 int target_read_memory(struct target *target,
577 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
578 {
579 return target->type->read_memory(target, address, size, count, buffer);
580 }
581
582 int target_read_phys_memory(struct target *target,
583 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
584 {
585 return target->type->read_phys_memory(target, address, size, count, buffer);
586 }
587
588 int target_write_memory(struct target *target,
589 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
590 {
591 return target->type->write_memory(target, address, size, count, buffer);
592 }
593
594 int target_write_phys_memory(struct target *target,
595 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
596 {
597 return target->type->write_phys_memory(target, address, size, count, buffer);
598 }
599
600 int target_bulk_write_memory(struct target *target,
601 uint32_t address, uint32_t count, uint8_t *buffer)
602 {
603 return target->type->bulk_write_memory(target, address, count, buffer);
604 }
605
606 int target_add_breakpoint(struct target *target,
607 struct breakpoint *breakpoint)
608 {
609 if (target->state != TARGET_HALTED) {
610 LOG_WARNING("target %s is not halted", target->cmd_name);
611 return ERROR_TARGET_NOT_HALTED;
612 }
613 return target->type->add_breakpoint(target, breakpoint);
614 }
615 int target_remove_breakpoint(struct target *target,
616 struct breakpoint *breakpoint)
617 {
618 return target->type->remove_breakpoint(target, breakpoint);
619 }
620
621 int target_add_watchpoint(struct target *target,
622 struct watchpoint *watchpoint)
623 {
624 if (target->state != TARGET_HALTED) {
625 LOG_WARNING("target %s is not halted", target->cmd_name);
626 return ERROR_TARGET_NOT_HALTED;
627 }
628 return target->type->add_watchpoint(target, watchpoint);
629 }
630 int target_remove_watchpoint(struct target *target,
631 struct watchpoint *watchpoint)
632 {
633 return target->type->remove_watchpoint(target, watchpoint);
634 }
635
636 int target_get_gdb_reg_list(struct target *target,
637 struct reg **reg_list[], int *reg_list_size)
638 {
639 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
640 }
641 int target_step(struct target *target,
642 int current, uint32_t address, int handle_breakpoints)
643 {
644 return target->type->step(target, current, address, handle_breakpoints);
645 }
646
647
648 int target_run_algorithm(struct target *target,
649 int num_mem_params, struct mem_param *mem_params,
650 int num_reg_params, struct reg_param *reg_param,
651 uint32_t entry_point, uint32_t exit_point,
652 int timeout_ms, void *arch_info)
653 {
654 return target->type->run_algorithm(target,
655 num_mem_params, mem_params, num_reg_params, reg_param,
656 entry_point, exit_point, timeout_ms, arch_info);
657 }
658
659 /**
660 * Reset the @c examined flag for the given target.
661 * Pure paranoia -- targets are zeroed on allocation.
662 */
663 static void target_reset_examined(struct target *target)
664 {
665 target->examined = false;
666 }
667
668
669
670 static int default_mrc(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t *value)
671 {
672 LOG_ERROR("Not implemented: %s", __func__);
673 return ERROR_FAIL;
674 }
675
676 static int default_mcr(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t value)
677 {
678 LOG_ERROR("Not implemented: %s", __func__);
679 return ERROR_FAIL;
680 }
681
682 static int arm_cp_check(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm)
683 {
684 /* basic check */
685 if (!target_was_examined(target))
686 {
687 LOG_ERROR("Target not examined yet");
688 return ERROR_FAIL;
689 }
690
691 if ((cpnum <0) || (cpnum > 15))
692 {
693 LOG_ERROR("Illegal co-processor %d", cpnum);
694 return ERROR_FAIL;
695 }
696
697 if (op1 > 7)
698 {
699 LOG_ERROR("Illegal op1");
700 return ERROR_FAIL;
701 }
702
703 if (op2 > 7)
704 {
705 LOG_ERROR("Illegal op2");
706 return ERROR_FAIL;
707 }
708
709 if (CRn > 15)
710 {
711 LOG_ERROR("Illegal CRn");
712 return ERROR_FAIL;
713 }
714
715 if (CRm > 15)
716 {
717 LOG_ERROR("Illegal CRm");
718 return ERROR_FAIL;
719 }
720
721 return ERROR_OK;
722 }
723
724 int target_mrc(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->mrc(target, cpnum, op1, op2, CRn, CRm, value);
733 }
734
735 int target_mcr(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t value)
736 {
737 int retval;
738
739 retval = arm_cp_check(target, cpnum, op1, op2, CRn, CRm);
740 if (retval != ERROR_OK)
741 return retval;
742
743 return target->type->mcr(target, cpnum, op1, op2, CRn, CRm, value);
744 }
745
746 static int
747 err_read_phys_memory(struct target *target, uint32_t address,
748 uint32_t size, uint32_t count, uint8_t *buffer)
749 {
750 LOG_ERROR("Not implemented: %s", __func__);
751 return ERROR_FAIL;
752 }
753
754 static int
755 err_write_phys_memory(struct target *target, uint32_t address,
756 uint32_t size, uint32_t count, uint8_t *buffer)
757 {
758 LOG_ERROR("Not implemented: %s", __func__);
759 return ERROR_FAIL;
760 }
761
762 int target_init(struct command_context *cmd_ctx)
763 {
764 struct target *target;
765 int retval;
766
767 for (target = all_targets; target; target = target->next) {
768 struct target_type *type = target->type;
769
770 target_reset_examined(target);
771 if (target->type->examine == NULL)
772 {
773 target->type->examine = default_examine;
774 }
775
776 if ((retval = target->type->init_target(cmd_ctx, target)) != ERROR_OK)
777 {
778 LOG_ERROR("target '%s' init failed", target_name(target));
779 return retval;
780 }
781
782 /**
783 * @todo MCR/MRC are ARM-specific; don't require them in
784 * all targets, or for ARMs without coprocessors.
785 */
786 if (target->type->mcr == NULL)
787 {
788 target->type->mcr = default_mcr;
789 } else
790 {
791 const struct command_registration mcr_cmd = {
792 .name = "mcr",
793 .mode = COMMAND_EXEC,
794 .jim_handler = &jim_mcrmrc,
795 .help = "write coprocessor",
796 .usage = "<cpnum> <op1> <op2> <CRn> <CRm> <value>",
797 };
798 register_command(cmd_ctx, NULL, &mcr_cmd);
799 }
800
801 if (target->type->mrc == NULL)
802 {
803 target->type->mrc = default_mrc;
804 } else
805 {
806 const struct command_registration mrc_cmd = {
807 .name = "mrc",
808 .jim_handler = &jim_mcrmrc,
809 .help = "read coprocessor",
810 .usage = "<cpnum> <op1> <op2> <CRn> <CRm>",
811 };
812 register_command(cmd_ctx, NULL, &mrc_cmd);
813 }
814
815
816 /**
817 * @todo get rid of those *memory_imp() methods, now that all
818 * callers are using target_*_memory() accessors ... and make
819 * sure the "physical" paths handle the same issues.
820 */
821
822 /* a non-invasive way(in terms of patches) to add some code that
823 * runs before the type->write/read_memory implementation
824 */
825 target->type->write_memory_imp = target->type->write_memory;
826 target->type->write_memory = target_write_memory_imp;
827 target->type->read_memory_imp = target->type->read_memory;
828 target->type->read_memory = target_read_memory_imp;
829 target->type->soft_reset_halt_imp = target->type->soft_reset_halt;
830 target->type->soft_reset_halt = target_soft_reset_halt_imp;
831 target->type->run_algorithm_imp = target->type->run_algorithm;
832 target->type->run_algorithm = target_run_algorithm_imp;
833
834 /* Sanity-check MMU support ... stub in what we must, to help
835 * implement it in stages, but warn if we need to do so.
836 */
837 if (type->mmu) {
838 if (type->write_phys_memory == NULL) {
839 LOG_ERROR("type '%s' is missing %s",
840 type->name,
841 "write_phys_memory");
842 type->write_phys_memory = err_write_phys_memory;
843 }
844 if (type->read_phys_memory == NULL) {
845 LOG_ERROR("type '%s' is missing %s",
846 type->name,
847 "read_phys_memory");
848 type->read_phys_memory = err_read_phys_memory;
849 }
850 if (type->virt2phys == NULL) {
851 LOG_ERROR("type '%s' is missing %s",
852 type->name,
853 "virt2phys");
854 type->virt2phys = identity_virt2phys;
855 }
856
857 /* Make sure no-MMU targets all behave the same: make no
858 * distinction between physical and virtual addresses, and
859 * ensure that virt2phys() is always an identity mapping.
860 */
861 } else {
862 if (type->write_phys_memory
863 || type->read_phys_memory
864 || type->virt2phys)
865 LOG_WARNING("type '%s' has broken MMU hooks",
866 type->name);
867
868 type->mmu = no_mmu;
869 type->write_phys_memory = type->write_memory;
870 type->read_phys_memory = type->read_memory;
871 type->virt2phys = identity_virt2phys;
872 }
873 }
874
875 if (all_targets)
876 {
877 if ((retval = target_register_user_commands(cmd_ctx)) != ERROR_OK)
878 return retval;
879 if ((retval = target_register_timer_callback(handle_target, 100, 1, NULL)) != ERROR_OK)
880 return retval;
881 }
882
883 return ERROR_OK;
884 }
885
886 int target_register_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
887 {
888 struct target_event_callback **callbacks_p = &target_event_callbacks;
889
890 if (callback == NULL)
891 {
892 return ERROR_INVALID_ARGUMENTS;
893 }
894
895 if (*callbacks_p)
896 {
897 while ((*callbacks_p)->next)
898 callbacks_p = &((*callbacks_p)->next);
899 callbacks_p = &((*callbacks_p)->next);
900 }
901
902 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
903 (*callbacks_p)->callback = callback;
904 (*callbacks_p)->priv = priv;
905 (*callbacks_p)->next = NULL;
906
907 return ERROR_OK;
908 }
909
910 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
911 {
912 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
913 struct timeval now;
914
915 if (callback == NULL)
916 {
917 return ERROR_INVALID_ARGUMENTS;
918 }
919
920 if (*callbacks_p)
921 {
922 while ((*callbacks_p)->next)
923 callbacks_p = &((*callbacks_p)->next);
924 callbacks_p = &((*callbacks_p)->next);
925 }
926
927 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
928 (*callbacks_p)->callback = callback;
929 (*callbacks_p)->periodic = periodic;
930 (*callbacks_p)->time_ms = time_ms;
931
932 gettimeofday(&now, NULL);
933 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
934 time_ms -= (time_ms % 1000);
935 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
936 if ((*callbacks_p)->when.tv_usec > 1000000)
937 {
938 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
939 (*callbacks_p)->when.tv_sec += 1;
940 }
941
942 (*callbacks_p)->priv = priv;
943 (*callbacks_p)->next = NULL;
944
945 return ERROR_OK;
946 }
947
948 int target_unregister_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
949 {
950 struct target_event_callback **p = &target_event_callbacks;
951 struct target_event_callback *c = target_event_callbacks;
952
953 if (callback == NULL)
954 {
955 return ERROR_INVALID_ARGUMENTS;
956 }
957
958 while (c)
959 {
960 struct target_event_callback *next = c->next;
961 if ((c->callback == callback) && (c->priv == priv))
962 {
963 *p = next;
964 free(c);
965 return ERROR_OK;
966 }
967 else
968 p = &(c->next);
969 c = next;
970 }
971
972 return ERROR_OK;
973 }
974
975 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
976 {
977 struct target_timer_callback **p = &target_timer_callbacks;
978 struct target_timer_callback *c = target_timer_callbacks;
979
980 if (callback == NULL)
981 {
982 return ERROR_INVALID_ARGUMENTS;
983 }
984
985 while (c)
986 {
987 struct target_timer_callback *next = c->next;
988 if ((c->callback == callback) && (c->priv == priv))
989 {
990 *p = next;
991 free(c);
992 return ERROR_OK;
993 }
994 else
995 p = &(c->next);
996 c = next;
997 }
998
999 return ERROR_OK;
1000 }
1001
1002 int target_call_event_callbacks(struct target *target, enum target_event event)
1003 {
1004 struct target_event_callback *callback = target_event_callbacks;
1005 struct target_event_callback *next_callback;
1006
1007 if (event == TARGET_EVENT_HALTED)
1008 {
1009 /* execute early halted first */
1010 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1011 }
1012
1013 LOG_DEBUG("target event %i (%s)",
1014 event,
1015 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
1016
1017 target_handle_event(target, event);
1018
1019 while (callback)
1020 {
1021 next_callback = callback->next;
1022 callback->callback(target, event, callback->priv);
1023 callback = next_callback;
1024 }
1025
1026 return ERROR_OK;
1027 }
1028
1029 static int target_timer_callback_periodic_restart(
1030 struct target_timer_callback *cb, struct timeval *now)
1031 {
1032 int time_ms = cb->time_ms;
1033 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
1034 time_ms -= (time_ms % 1000);
1035 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
1036 if (cb->when.tv_usec > 1000000)
1037 {
1038 cb->when.tv_usec = cb->when.tv_usec - 1000000;
1039 cb->when.tv_sec += 1;
1040 }
1041 return ERROR_OK;
1042 }
1043
1044 static int target_call_timer_callback(struct target_timer_callback *cb,
1045 struct timeval *now)
1046 {
1047 cb->callback(cb->priv);
1048
1049 if (cb->periodic)
1050 return target_timer_callback_periodic_restart(cb, now);
1051
1052 return target_unregister_timer_callback(cb->callback, cb->priv);
1053 }
1054
1055 static int target_call_timer_callbacks_check_time(int checktime)
1056 {
1057 keep_alive();
1058
1059 struct timeval now;
1060 gettimeofday(&now, NULL);
1061
1062 struct target_timer_callback *callback = target_timer_callbacks;
1063 while (callback)
1064 {
1065 // cleaning up may unregister and free this callback
1066 struct target_timer_callback *next_callback = callback->next;
1067
1068 bool call_it = callback->callback &&
1069 ((!checktime && callback->periodic) ||
1070 now.tv_sec > callback->when.tv_sec ||
1071 (now.tv_sec == callback->when.tv_sec &&
1072 now.tv_usec >= callback->when.tv_usec));
1073
1074 if (call_it)
1075 {
1076 int retval = target_call_timer_callback(callback, &now);
1077 if (retval != ERROR_OK)
1078 return retval;
1079 }
1080
1081 callback = next_callback;
1082 }
1083
1084 return ERROR_OK;
1085 }
1086
1087 int target_call_timer_callbacks(void)
1088 {
1089 return target_call_timer_callbacks_check_time(1);
1090 }
1091
1092 /* invoke periodic callbacks immediately */
1093 int target_call_timer_callbacks_now(void)
1094 {
1095 return target_call_timer_callbacks_check_time(0);
1096 }
1097
1098 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1099 {
1100 struct working_area *c = target->working_areas;
1101 struct working_area *new_wa = NULL;
1102
1103 /* Reevaluate working area address based on MMU state*/
1104 if (target->working_areas == NULL)
1105 {
1106 int retval;
1107 int enabled;
1108
1109 retval = target->type->mmu(target, &enabled);
1110 if (retval != ERROR_OK)
1111 {
1112 return retval;
1113 }
1114
1115 if (!enabled) {
1116 if (target->working_area_phys_spec) {
1117 LOG_DEBUG("MMU disabled, using physical "
1118 "address for working memory 0x%08x",
1119 (unsigned)target->working_area_phys);
1120 target->working_area = target->working_area_phys;
1121 } else {
1122 LOG_ERROR("No working memory available. "
1123 "Specify -work-area-phys to target.");
1124 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1125 }
1126 } else {
1127 if (target->working_area_virt_spec) {
1128 LOG_DEBUG("MMU enabled, using virtual "
1129 "address for working memory 0x%08x",
1130 (unsigned)target->working_area_virt);
1131 target->working_area = target->working_area_virt;
1132 } else {
1133 LOG_ERROR("No working memory available. "
1134 "Specify -work-area-virt to target.");
1135 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1136 }
1137 }
1138 }
1139
1140 /* only allocate multiples of 4 byte */
1141 if (size % 4)
1142 {
1143 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
1144 size = (size + 3) & (~3);
1145 }
1146
1147 /* see if there's already a matching working area */
1148 while (c)
1149 {
1150 if ((c->free) && (c->size == size))
1151 {
1152 new_wa = c;
1153 break;
1154 }
1155 c = c->next;
1156 }
1157
1158 /* if not, allocate a new one */
1159 if (!new_wa)
1160 {
1161 struct working_area **p = &target->working_areas;
1162 uint32_t first_free = target->working_area;
1163 uint32_t free_size = target->working_area_size;
1164
1165 c = target->working_areas;
1166 while (c)
1167 {
1168 first_free += c->size;
1169 free_size -= c->size;
1170 p = &c->next;
1171 c = c->next;
1172 }
1173
1174 if (free_size < size)
1175 {
1176 LOG_WARNING("not enough working area available(requested %u, free %u)",
1177 (unsigned)(size), (unsigned)(free_size));
1178 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1179 }
1180
1181 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free);
1182
1183 new_wa = malloc(sizeof(struct working_area));
1184 new_wa->next = NULL;
1185 new_wa->size = size;
1186 new_wa->address = first_free;
1187
1188 if (target->backup_working_area)
1189 {
1190 int retval;
1191 new_wa->backup = malloc(new_wa->size);
1192 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1193 {
1194 free(new_wa->backup);
1195 free(new_wa);
1196 return retval;
1197 }
1198 }
1199 else
1200 {
1201 new_wa->backup = NULL;
1202 }
1203
1204 /* put new entry in list */
1205 *p = new_wa;
1206 }
1207
1208 /* mark as used, and return the new (reused) area */
1209 new_wa->free = 0;
1210 *area = new_wa;
1211
1212 /* user pointer */
1213 new_wa->user = area;
1214
1215 return ERROR_OK;
1216 }
1217
1218 int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1219 {
1220 if (area->free)
1221 return ERROR_OK;
1222
1223 if (restore && target->backup_working_area)
1224 {
1225 int retval;
1226 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1227 return retval;
1228 }
1229
1230 area->free = 1;
1231
1232 /* mark user pointer invalid */
1233 *area->user = NULL;
1234 area->user = NULL;
1235
1236 return ERROR_OK;
1237 }
1238
1239 int target_free_working_area(struct target *target, struct working_area *area)
1240 {
1241 return target_free_working_area_restore(target, area, 1);
1242 }
1243
1244 /* free resources and restore memory, if restoring memory fails,
1245 * free up resources anyway
1246 */
1247 void target_free_all_working_areas_restore(struct target *target, int restore)
1248 {
1249 struct working_area *c = target->working_areas;
1250
1251 while (c)
1252 {
1253 struct working_area *next = c->next;
1254 target_free_working_area_restore(target, c, restore);
1255
1256 if (c->backup)
1257 free(c->backup);
1258
1259 free(c);
1260
1261 c = next;
1262 }
1263
1264 target->working_areas = NULL;
1265 }
1266
1267 void target_free_all_working_areas(struct target *target)
1268 {
1269 target_free_all_working_areas_restore(target, 1);
1270 }
1271
1272 int target_arch_state(struct target *target)
1273 {
1274 int retval;
1275 if (target == NULL)
1276 {
1277 LOG_USER("No target has been configured");
1278 return ERROR_OK;
1279 }
1280
1281 LOG_USER("target state: %s", target_state_name( target ));
1282
1283 if (target->state != TARGET_HALTED)
1284 return ERROR_OK;
1285
1286 retval = target->type->arch_state(target);
1287 return retval;
1288 }
1289
1290 /* Single aligned words are guaranteed to use 16 or 32 bit access
1291 * mode respectively, otherwise data is handled as quickly as
1292 * possible
1293 */
1294 int target_write_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1295 {
1296 int retval;
1297 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1298 (int)size, (unsigned)address);
1299
1300 if (!target_was_examined(target))
1301 {
1302 LOG_ERROR("Target not examined yet");
1303 return ERROR_FAIL;
1304 }
1305
1306 if (size == 0) {
1307 return ERROR_OK;
1308 }
1309
1310 if ((address + size - 1) < address)
1311 {
1312 /* GDB can request this when e.g. PC is 0xfffffffc*/
1313 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1314 (unsigned)address,
1315 (unsigned)size);
1316 return ERROR_FAIL;
1317 }
1318
1319 if (((address % 2) == 0) && (size == 2))
1320 {
1321 return target_write_memory(target, address, 2, 1, buffer);
1322 }
1323
1324 /* handle unaligned head bytes */
1325 if (address % 4)
1326 {
1327 uint32_t unaligned = 4 - (address % 4);
1328
1329 if (unaligned > size)
1330 unaligned = size;
1331
1332 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1333 return retval;
1334
1335 buffer += unaligned;
1336 address += unaligned;
1337 size -= unaligned;
1338 }
1339
1340 /* handle aligned words */
1341 if (size >= 4)
1342 {
1343 int aligned = size - (size % 4);
1344
1345 /* use bulk writes above a certain limit. This may have to be changed */
1346 if (aligned > 128)
1347 {
1348 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1349 return retval;
1350 }
1351 else
1352 {
1353 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1354 return retval;
1355 }
1356
1357 buffer += aligned;
1358 address += aligned;
1359 size -= aligned;
1360 }
1361
1362 /* handle tail writes of less than 4 bytes */
1363 if (size > 0)
1364 {
1365 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1366 return retval;
1367 }
1368
1369 return ERROR_OK;
1370 }
1371
1372 /* Single aligned words are guaranteed to use 16 or 32 bit access
1373 * mode respectively, otherwise data is handled as quickly as
1374 * possible
1375 */
1376 int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1377 {
1378 int retval;
1379 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1380 (int)size, (unsigned)address);
1381
1382 if (!target_was_examined(target))
1383 {
1384 LOG_ERROR("Target not examined yet");
1385 return ERROR_FAIL;
1386 }
1387
1388 if (size == 0) {
1389 return ERROR_OK;
1390 }
1391
1392 if ((address + size - 1) < address)
1393 {
1394 /* GDB can request this when e.g. PC is 0xfffffffc*/
1395 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1396 address,
1397 size);
1398 return ERROR_FAIL;
1399 }
1400
1401 if (((address % 2) == 0) && (size == 2))
1402 {
1403 return target_read_memory(target, address, 2, 1, buffer);
1404 }
1405
1406 /* handle unaligned head bytes */
1407 if (address % 4)
1408 {
1409 uint32_t unaligned = 4 - (address % 4);
1410
1411 if (unaligned > size)
1412 unaligned = size;
1413
1414 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1415 return retval;
1416
1417 buffer += unaligned;
1418 address += unaligned;
1419 size -= unaligned;
1420 }
1421
1422 /* handle aligned words */
1423 if (size >= 4)
1424 {
1425 int aligned = size - (size % 4);
1426
1427 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1428 return retval;
1429
1430 buffer += aligned;
1431 address += aligned;
1432 size -= aligned;
1433 }
1434
1435 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1436 if(size >=2)
1437 {
1438 int aligned = size - (size%2);
1439 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1440 if (retval != ERROR_OK)
1441 return retval;
1442
1443 buffer += aligned;
1444 address += aligned;
1445 size -= aligned;
1446 }
1447 /* handle tail writes of less than 4 bytes */
1448 if (size > 0)
1449 {
1450 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1451 return retval;
1452 }
1453
1454 return ERROR_OK;
1455 }
1456
1457 int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
1458 {
1459 uint8_t *buffer;
1460 int retval;
1461 uint32_t i;
1462 uint32_t checksum = 0;
1463 if (!target_was_examined(target))
1464 {
1465 LOG_ERROR("Target not examined yet");
1466 return ERROR_FAIL;
1467 }
1468
1469 if ((retval = target->type->checksum_memory(target, address,
1470 size, &checksum)) != ERROR_OK)
1471 {
1472 buffer = malloc(size);
1473 if (buffer == NULL)
1474 {
1475 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1476 return ERROR_INVALID_ARGUMENTS;
1477 }
1478 retval = target_read_buffer(target, address, size, buffer);
1479 if (retval != ERROR_OK)
1480 {
1481 free(buffer);
1482 return retval;
1483 }
1484
1485 /* convert to target endianess */
1486 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1487 {
1488 uint32_t target_data;
1489 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1490 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1491 }
1492
1493 retval = image_calculate_checksum(buffer, size, &checksum);
1494 free(buffer);
1495 }
1496
1497 *crc = checksum;
1498
1499 return retval;
1500 }
1501
1502 int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank)
1503 {
1504 int retval;
1505 if (!target_was_examined(target))
1506 {
1507 LOG_ERROR("Target not examined yet");
1508 return ERROR_FAIL;
1509 }
1510
1511 if (target->type->blank_check_memory == 0)
1512 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1513
1514 retval = target->type->blank_check_memory(target, address, size, blank);
1515
1516 return retval;
1517 }
1518
1519 int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
1520 {
1521 uint8_t value_buf[4];
1522 if (!target_was_examined(target))
1523 {
1524 LOG_ERROR("Target not examined yet");
1525 return ERROR_FAIL;
1526 }
1527
1528 int retval = target_read_memory(target, address, 4, 1, value_buf);
1529
1530 if (retval == ERROR_OK)
1531 {
1532 *value = target_buffer_get_u32(target, value_buf);
1533 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1534 address,
1535 *value);
1536 }
1537 else
1538 {
1539 *value = 0x0;
1540 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1541 address);
1542 }
1543
1544 return retval;
1545 }
1546
1547 int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
1548 {
1549 uint8_t value_buf[2];
1550 if (!target_was_examined(target))
1551 {
1552 LOG_ERROR("Target not examined yet");
1553 return ERROR_FAIL;
1554 }
1555
1556 int retval = target_read_memory(target, address, 2, 1, value_buf);
1557
1558 if (retval == ERROR_OK)
1559 {
1560 *value = target_buffer_get_u16(target, value_buf);
1561 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1562 address,
1563 *value);
1564 }
1565 else
1566 {
1567 *value = 0x0;
1568 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1569 address);
1570 }
1571
1572 return retval;
1573 }
1574
1575 int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
1576 {
1577 int retval = target_read_memory(target, address, 1, 1, value);
1578 if (!target_was_examined(target))
1579 {
1580 LOG_ERROR("Target not examined yet");
1581 return ERROR_FAIL;
1582 }
1583
1584 if (retval == ERROR_OK)
1585 {
1586 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1587 address,
1588 *value);
1589 }
1590 else
1591 {
1592 *value = 0x0;
1593 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1594 address);
1595 }
1596
1597 return retval;
1598 }
1599
1600 int target_write_u32(struct target *target, uint32_t address, uint32_t value)
1601 {
1602 int retval;
1603 uint8_t value_buf[4];
1604 if (!target_was_examined(target))
1605 {
1606 LOG_ERROR("Target not examined yet");
1607 return ERROR_FAIL;
1608 }
1609
1610 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1611 address,
1612 value);
1613
1614 target_buffer_set_u32(target, value_buf, value);
1615 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1616 {
1617 LOG_DEBUG("failed: %i", retval);
1618 }
1619
1620 return retval;
1621 }
1622
1623 int target_write_u16(struct target *target, uint32_t address, uint16_t value)
1624 {
1625 int retval;
1626 uint8_t value_buf[2];
1627 if (!target_was_examined(target))
1628 {
1629 LOG_ERROR("Target not examined yet");
1630 return ERROR_FAIL;
1631 }
1632
1633 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1634 address,
1635 value);
1636
1637 target_buffer_set_u16(target, value_buf, value);
1638 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1639 {
1640 LOG_DEBUG("failed: %i", retval);
1641 }
1642
1643 return retval;
1644 }
1645
1646 int target_write_u8(struct target *target, uint32_t address, uint8_t value)
1647 {
1648 int retval;
1649 if (!target_was_examined(target))
1650 {
1651 LOG_ERROR("Target not examined yet");
1652 return ERROR_FAIL;
1653 }
1654
1655 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1656 address, value);
1657
1658 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1659 {
1660 LOG_DEBUG("failed: %i", retval);
1661 }
1662
1663 return retval;
1664 }
1665
1666 COMMAND_HANDLER(handle_targets_command)
1667 {
1668 struct target *target = all_targets;
1669
1670 if (CMD_ARGC == 1)
1671 {
1672 target = get_target(CMD_ARGV[0]);
1673 if (target == NULL) {
1674 command_print(CMD_CTX,"Target: %s is unknown, try one of:\n", CMD_ARGV[0]);
1675 goto DumpTargets;
1676 }
1677 if (!target->tap->enabled) {
1678 command_print(CMD_CTX,"Target: TAP %s is disabled, "
1679 "can't be the current target\n",
1680 target->tap->dotted_name);
1681 return ERROR_FAIL;
1682 }
1683
1684 CMD_CTX->current_target = target->target_number;
1685 return ERROR_OK;
1686 }
1687 DumpTargets:
1688
1689 target = all_targets;
1690 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
1691 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
1692 while (target)
1693 {
1694 const char *state;
1695 char marker = ' ';
1696
1697 if (target->tap->enabled)
1698 state = target_state_name( target );
1699 else
1700 state = "tap-disabled";
1701
1702 if (CMD_CTX->current_target == target->target_number)
1703 marker = '*';
1704
1705 /* keep columns lined up to match the headers above */
1706 command_print(CMD_CTX, "%2d%c %-18s %-10s %-6s %-18s %s",
1707 target->target_number,
1708 marker,
1709 target_name(target),
1710 target_type_name(target),
1711 Jim_Nvp_value2name_simple(nvp_target_endian,
1712 target->endianness)->name,
1713 target->tap->dotted_name,
1714 state);
1715 target = target->next;
1716 }
1717
1718 return ERROR_OK;
1719 }
1720
1721 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1722
1723 static int powerDropout;
1724 static int srstAsserted;
1725
1726 static int runPowerRestore;
1727 static int runPowerDropout;
1728 static int runSrstAsserted;
1729 static int runSrstDeasserted;
1730
1731 static int sense_handler(void)
1732 {
1733 static int prevSrstAsserted = 0;
1734 static int prevPowerdropout = 0;
1735
1736 int retval;
1737 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1738 return retval;
1739
1740 int powerRestored;
1741 powerRestored = prevPowerdropout && !powerDropout;
1742 if (powerRestored)
1743 {
1744 runPowerRestore = 1;
1745 }
1746
1747 long long current = timeval_ms();
1748 static long long lastPower = 0;
1749 int waitMore = lastPower + 2000 > current;
1750 if (powerDropout && !waitMore)
1751 {
1752 runPowerDropout = 1;
1753 lastPower = current;
1754 }
1755
1756 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1757 return retval;
1758
1759 int srstDeasserted;
1760 srstDeasserted = prevSrstAsserted && !srstAsserted;
1761
1762 static long long lastSrst = 0;
1763 waitMore = lastSrst + 2000 > current;
1764 if (srstDeasserted && !waitMore)
1765 {
1766 runSrstDeasserted = 1;
1767 lastSrst = current;
1768 }
1769
1770 if (!prevSrstAsserted && srstAsserted)
1771 {
1772 runSrstAsserted = 1;
1773 }
1774
1775 prevSrstAsserted = srstAsserted;
1776 prevPowerdropout = powerDropout;
1777
1778 if (srstDeasserted || powerRestored)
1779 {
1780 /* Other than logging the event we can't do anything here.
1781 * Issuing a reset is a particularly bad idea as we might
1782 * be inside a reset already.
1783 */
1784 }
1785
1786 return ERROR_OK;
1787 }
1788
1789 static void target_call_event_callbacks_all(enum target_event e) {
1790 struct target *target;
1791 target = all_targets;
1792 while (target) {
1793 target_call_event_callbacks(target, e);
1794 target = target->next;
1795 }
1796 }
1797
1798 /* process target state changes */
1799 int handle_target(void *priv)
1800 {
1801 int retval = ERROR_OK;
1802
1803 /* we do not want to recurse here... */
1804 static int recursive = 0;
1805 if (! recursive)
1806 {
1807 recursive = 1;
1808 sense_handler();
1809 /* danger! running these procedures can trigger srst assertions and power dropouts.
1810 * We need to avoid an infinite loop/recursion here and we do that by
1811 * clearing the flags after running these events.
1812 */
1813 int did_something = 0;
1814 if (runSrstAsserted)
1815 {
1816 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1817 Jim_Eval(interp, "srst_asserted");
1818 did_something = 1;
1819 }
1820 if (runSrstDeasserted)
1821 {
1822 Jim_Eval(interp, "srst_deasserted");
1823 did_something = 1;
1824 }
1825 if (runPowerDropout)
1826 {
1827 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1828 Jim_Eval(interp, "power_dropout");
1829 did_something = 1;
1830 }
1831 if (runPowerRestore)
1832 {
1833 Jim_Eval(interp, "power_restore");
1834 did_something = 1;
1835 }
1836
1837 if (did_something)
1838 {
1839 /* clear detect flags */
1840 sense_handler();
1841 }
1842
1843 /* clear action flags */
1844
1845 runSrstAsserted = 0;
1846 runSrstDeasserted = 0;
1847 runPowerRestore = 0;
1848 runPowerDropout = 0;
1849
1850 recursive = 0;
1851 }
1852
1853 /* Poll targets for state changes unless that's globally disabled.
1854 * Skip targets that are currently disabled.
1855 */
1856 for (struct target *target = all_targets;
1857 is_jtag_poll_safe() && target;
1858 target = target->next)
1859 {
1860 if (!target->tap->enabled)
1861 continue;
1862
1863 /* only poll target if we've got power and srst isn't asserted */
1864 if (!powerDropout && !srstAsserted)
1865 {
1866 /* polling may fail silently until the target has been examined */
1867 if ((retval = target_poll(target)) != ERROR_OK)
1868 {
1869 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1870 return retval;
1871 }
1872 }
1873 }
1874
1875 return retval;
1876 }
1877
1878 COMMAND_HANDLER(handle_reg_command)
1879 {
1880 struct target *target;
1881 struct reg *reg = NULL;
1882 unsigned count = 0;
1883 char *value;
1884
1885 LOG_DEBUG("-");
1886
1887 target = get_current_target(CMD_CTX);
1888
1889 /* list all available registers for the current target */
1890 if (CMD_ARGC == 0)
1891 {
1892 struct reg_cache *cache = target->reg_cache;
1893
1894 count = 0;
1895 while (cache)
1896 {
1897 unsigned i;
1898
1899 command_print(CMD_CTX, "===== %s", cache->name);
1900
1901 for (i = 0, reg = cache->reg_list;
1902 i < cache->num_regs;
1903 i++, reg++, count++)
1904 {
1905 /* only print cached values if they are valid */
1906 if (reg->valid) {
1907 value = buf_to_str(reg->value,
1908 reg->size, 16);
1909 command_print(CMD_CTX,
1910 "(%i) %s (/%" PRIu32 "): 0x%s%s",
1911 count, reg->name,
1912 reg->size, value,
1913 reg->dirty
1914 ? " (dirty)"
1915 : "");
1916 free(value);
1917 } else {
1918 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
1919 count, reg->name,
1920 reg->size) ;
1921 }
1922 }
1923 cache = cache->next;
1924 }
1925
1926 return ERROR_OK;
1927 }
1928
1929 /* access a single register by its ordinal number */
1930 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9'))
1931 {
1932 unsigned num;
1933 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
1934
1935 struct reg_cache *cache = target->reg_cache;
1936 count = 0;
1937 while (cache)
1938 {
1939 unsigned i;
1940 for (i = 0; i < cache->num_regs; i++)
1941 {
1942 if (count++ == num)
1943 {
1944 reg = &cache->reg_list[i];
1945 break;
1946 }
1947 }
1948 if (reg)
1949 break;
1950 cache = cache->next;
1951 }
1952
1953 if (!reg)
1954 {
1955 command_print(CMD_CTX, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1956 return ERROR_OK;
1957 }
1958 } else /* access a single register by its name */
1959 {
1960 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
1961
1962 if (!reg)
1963 {
1964 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
1965 return ERROR_OK;
1966 }
1967 }
1968
1969 /* display a register */
1970 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0') && (CMD_ARGV[1][0] <= '9'))))
1971 {
1972 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
1973 reg->valid = 0;
1974
1975 if (reg->valid == 0)
1976 {
1977 reg->type->get(reg);
1978 }
1979 value = buf_to_str(reg->value, reg->size, 16);
1980 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1981 free(value);
1982 return ERROR_OK;
1983 }
1984
1985 /* set register value */
1986 if (CMD_ARGC == 2)
1987 {
1988 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
1989 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
1990
1991 reg->type->set(reg, buf);
1992
1993 value = buf_to_str(reg->value, reg->size, 16);
1994 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1995 free(value);
1996
1997 free(buf);
1998
1999 return ERROR_OK;
2000 }
2001
2002 command_print(CMD_CTX, "usage: reg <#|name> [value]");
2003
2004 return ERROR_OK;
2005 }
2006
2007 COMMAND_HANDLER(handle_poll_command)
2008 {
2009 int retval = ERROR_OK;
2010 struct target *target = get_current_target(CMD_CTX);
2011
2012 if (CMD_ARGC == 0)
2013 {
2014 command_print(CMD_CTX, "background polling: %s",
2015 jtag_poll_get_enabled() ? "on" : "off");
2016 command_print(CMD_CTX, "TAP: %s (%s)",
2017 target->tap->dotted_name,
2018 target->tap->enabled ? "enabled" : "disabled");
2019 if (!target->tap->enabled)
2020 return ERROR_OK;
2021 if ((retval = target_poll(target)) != ERROR_OK)
2022 return retval;
2023 if ((retval = target_arch_state(target)) != ERROR_OK)
2024 return retval;
2025 }
2026 else if (CMD_ARGC == 1)
2027 {
2028 bool enable;
2029 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
2030 jtag_poll_set_enabled(enable);
2031 }
2032 else
2033 {
2034 return ERROR_COMMAND_SYNTAX_ERROR;
2035 }
2036
2037 return retval;
2038 }
2039
2040 COMMAND_HANDLER(handle_wait_halt_command)
2041 {
2042 if (CMD_ARGC > 1)
2043 return ERROR_COMMAND_SYNTAX_ERROR;
2044
2045 unsigned ms = 5000;
2046 if (1 == CMD_ARGC)
2047 {
2048 int retval = parse_uint(CMD_ARGV[0], &ms);
2049 if (ERROR_OK != retval)
2050 {
2051 command_print(CMD_CTX, "usage: %s [seconds]", CMD_NAME);
2052 return ERROR_COMMAND_SYNTAX_ERROR;
2053 }
2054 // convert seconds (given) to milliseconds (needed)
2055 ms *= 1000;
2056 }
2057
2058 struct target *target = get_current_target(CMD_CTX);
2059 return target_wait_state(target, TARGET_HALTED, ms);
2060 }
2061
2062 /* wait for target state to change. The trick here is to have a low
2063 * latency for short waits and not to suck up all the CPU time
2064 * on longer waits.
2065 *
2066 * After 500ms, keep_alive() is invoked
2067 */
2068 int target_wait_state(struct target *target, enum target_state state, int ms)
2069 {
2070 int retval;
2071 long long then = 0, cur;
2072 int once = 1;
2073
2074 for (;;)
2075 {
2076 if ((retval = target_poll(target)) != ERROR_OK)
2077 return retval;
2078 if (target->state == state)
2079 {
2080 break;
2081 }
2082 cur = timeval_ms();
2083 if (once)
2084 {
2085 once = 0;
2086 then = timeval_ms();
2087 LOG_DEBUG("waiting for target %s...",
2088 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2089 }
2090
2091 if (cur-then > 500)
2092 {
2093 keep_alive();
2094 }
2095
2096 if ((cur-then) > ms)
2097 {
2098 LOG_ERROR("timed out while waiting for target %s",
2099 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2100 return ERROR_FAIL;
2101 }
2102 }
2103
2104 return ERROR_OK;
2105 }
2106
2107 COMMAND_HANDLER(handle_halt_command)
2108 {
2109 LOG_DEBUG("-");
2110
2111 struct target *target = get_current_target(CMD_CTX);
2112 int retval = target_halt(target);
2113 if (ERROR_OK != retval)
2114 return retval;
2115
2116 if (CMD_ARGC == 1)
2117 {
2118 unsigned wait;
2119 retval = parse_uint(CMD_ARGV[0], &wait);
2120 if (ERROR_OK != retval)
2121 return ERROR_COMMAND_SYNTAX_ERROR;
2122 if (!wait)
2123 return ERROR_OK;
2124 }
2125
2126 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2127 }
2128
2129 COMMAND_HANDLER(handle_soft_reset_halt_command)
2130 {
2131 struct target *target = get_current_target(CMD_CTX);
2132
2133 LOG_USER("requesting target halt and executing a soft reset");
2134
2135 target->type->soft_reset_halt(target);
2136
2137 return ERROR_OK;
2138 }
2139
2140 COMMAND_HANDLER(handle_reset_command)
2141 {
2142 if (CMD_ARGC > 1)
2143 return ERROR_COMMAND_SYNTAX_ERROR;
2144
2145 enum target_reset_mode reset_mode = RESET_RUN;
2146 if (CMD_ARGC == 1)
2147 {
2148 const Jim_Nvp *n;
2149 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
2150 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2151 return ERROR_COMMAND_SYNTAX_ERROR;
2152 }
2153 reset_mode = n->value;
2154 }
2155
2156 /* reset *all* targets */
2157 return target_process_reset(CMD_CTX, reset_mode);
2158 }
2159
2160
2161 COMMAND_HANDLER(handle_resume_command)
2162 {
2163 int current = 1;
2164 if (CMD_ARGC > 1)
2165 return ERROR_COMMAND_SYNTAX_ERROR;
2166
2167 struct target *target = get_current_target(CMD_CTX);
2168 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2169
2170 /* with no CMD_ARGV, resume from current pc, addr = 0,
2171 * with one arguments, addr = CMD_ARGV[0],
2172 * handle breakpoints, not debugging */
2173 uint32_t addr = 0;
2174 if (CMD_ARGC == 1)
2175 {
2176 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2177 current = 0;
2178 }
2179
2180 return target_resume(target, current, addr, 1, 0);
2181 }
2182
2183 COMMAND_HANDLER(handle_step_command)
2184 {
2185 if (CMD_ARGC > 1)
2186 return ERROR_COMMAND_SYNTAX_ERROR;
2187
2188 LOG_DEBUG("-");
2189
2190 /* with no CMD_ARGV, step from current pc, addr = 0,
2191 * with one argument addr = CMD_ARGV[0],
2192 * handle breakpoints, debugging */
2193 uint32_t addr = 0;
2194 int current_pc = 1;
2195 if (CMD_ARGC == 1)
2196 {
2197 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2198 current_pc = 0;
2199 }
2200
2201 struct target *target = get_current_target(CMD_CTX);
2202
2203 return target->type->step(target, current_pc, addr, 1);
2204 }
2205
2206 static void handle_md_output(struct command_context *cmd_ctx,
2207 struct target *target, uint32_t address, unsigned size,
2208 unsigned count, const uint8_t *buffer)
2209 {
2210 const unsigned line_bytecnt = 32;
2211 unsigned line_modulo = line_bytecnt / size;
2212
2213 char output[line_bytecnt * 4 + 1];
2214 unsigned output_len = 0;
2215
2216 const char *value_fmt;
2217 switch (size) {
2218 case 4: value_fmt = "%8.8x "; break;
2219 case 2: value_fmt = "%4.2x "; break;
2220 case 1: value_fmt = "%2.2x "; break;
2221 default:
2222 LOG_ERROR("invalid memory read size: %u", size);
2223 exit(-1);
2224 }
2225
2226 for (unsigned i = 0; i < count; i++)
2227 {
2228 if (i % line_modulo == 0)
2229 {
2230 output_len += snprintf(output + output_len,
2231 sizeof(output) - output_len,
2232 "0x%8.8x: ",
2233 (unsigned)(address + (i*size)));
2234 }
2235
2236 uint32_t value = 0;
2237 const uint8_t *value_ptr = buffer + i * size;
2238 switch (size) {
2239 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2240 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2241 case 1: value = *value_ptr;
2242 }
2243 output_len += snprintf(output + output_len,
2244 sizeof(output) - output_len,
2245 value_fmt, value);
2246
2247 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2248 {
2249 command_print(cmd_ctx, "%s", output);
2250 output_len = 0;
2251 }
2252 }
2253 }
2254
2255 COMMAND_HANDLER(handle_md_command)
2256 {
2257 if (CMD_ARGC < 1)
2258 return ERROR_COMMAND_SYNTAX_ERROR;
2259
2260 unsigned size = 0;
2261 switch (CMD_NAME[2]) {
2262 case 'w': size = 4; break;
2263 case 'h': size = 2; break;
2264 case 'b': size = 1; break;
2265 default: return ERROR_COMMAND_SYNTAX_ERROR;
2266 }
2267
2268 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2269 int (*fn)(struct target *target,
2270 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2271 if (physical)
2272 {
2273 CMD_ARGC--;
2274 CMD_ARGV++;
2275 fn=target_read_phys_memory;
2276 } else
2277 {
2278 fn=target_read_memory;
2279 }
2280 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
2281 {
2282 return ERROR_COMMAND_SYNTAX_ERROR;
2283 }
2284
2285 uint32_t address;
2286 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2287
2288 unsigned count = 1;
2289 if (CMD_ARGC == 2)
2290 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
2291
2292 uint8_t *buffer = calloc(count, size);
2293
2294 struct target *target = get_current_target(CMD_CTX);
2295 int retval = fn(target, address, size, count, buffer);
2296 if (ERROR_OK == retval)
2297 handle_md_output(CMD_CTX, target, address, size, count, buffer);
2298
2299 free(buffer);
2300
2301 return retval;
2302 }
2303
2304 COMMAND_HANDLER(handle_mw_command)
2305 {
2306 if (CMD_ARGC < 2)
2307 {
2308 return ERROR_COMMAND_SYNTAX_ERROR;
2309 }
2310 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2311 int (*fn)(struct target *target,
2312 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2313 if (physical)
2314 {
2315 CMD_ARGC--;
2316 CMD_ARGV++;
2317 fn=target_write_phys_memory;
2318 } else
2319 {
2320 fn=target_write_memory;
2321 }
2322 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
2323 return ERROR_COMMAND_SYNTAX_ERROR;
2324
2325 uint32_t address;
2326 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2327
2328 uint32_t value;
2329 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2330
2331 unsigned count = 1;
2332 if (CMD_ARGC == 3)
2333 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
2334
2335 struct target *target = get_current_target(CMD_CTX);
2336 unsigned wordsize;
2337 uint8_t value_buf[4];
2338 switch (CMD_NAME[2])
2339 {
2340 case 'w':
2341 wordsize = 4;
2342 target_buffer_set_u32(target, value_buf, value);
2343 break;
2344 case 'h':
2345 wordsize = 2;
2346 target_buffer_set_u16(target, value_buf, value);
2347 break;
2348 case 'b':
2349 wordsize = 1;
2350 value_buf[0] = value;
2351 break;
2352 default:
2353 return ERROR_COMMAND_SYNTAX_ERROR;
2354 }
2355 for (unsigned i = 0; i < count; i++)
2356 {
2357 int retval = fn(target,
2358 address + i * wordsize, wordsize, 1, value_buf);
2359 if (ERROR_OK != retval)
2360 return retval;
2361 keep_alive();
2362 }
2363
2364 return ERROR_OK;
2365
2366 }
2367
2368 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
2369 uint32_t *min_address, uint32_t *max_address)
2370 {
2371 if (CMD_ARGC < 1 || CMD_ARGC > 5)
2372 return ERROR_COMMAND_SYNTAX_ERROR;
2373
2374 /* a base address isn't always necessary,
2375 * default to 0x0 (i.e. don't relocate) */
2376 if (CMD_ARGC >= 2)
2377 {
2378 uint32_t addr;
2379 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2380 image->base_address = addr;
2381 image->base_address_set = 1;
2382 }
2383 else
2384 image->base_address_set = 0;
2385
2386 image->start_address_set = 0;
2387
2388 if (CMD_ARGC >= 4)
2389 {
2390 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], *min_address);
2391 }
2392 if (CMD_ARGC == 5)
2393 {
2394 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], *max_address);
2395 // use size (given) to find max (required)
2396 *max_address += *min_address;
2397 }
2398
2399 if (*min_address > *max_address)
2400 return ERROR_COMMAND_SYNTAX_ERROR;
2401
2402 return ERROR_OK;
2403 }
2404
2405 COMMAND_HANDLER(handle_load_image_command)
2406 {
2407 uint8_t *buffer;
2408 size_t buf_cnt;
2409 uint32_t image_size;
2410 uint32_t min_address = 0;
2411 uint32_t max_address = 0xffffffff;
2412 int i;
2413 struct image image;
2414
2415 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
2416 &image, &min_address, &max_address);
2417 if (ERROR_OK != retval)
2418 return retval;
2419
2420 struct target *target = get_current_target(CMD_CTX);
2421
2422 struct duration bench;
2423 duration_start(&bench);
2424
2425 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
2426 {
2427 return ERROR_OK;
2428 }
2429
2430 image_size = 0x0;
2431 retval = ERROR_OK;
2432 for (i = 0; i < image.num_sections; i++)
2433 {
2434 buffer = malloc(image.sections[i].size);
2435 if (buffer == NULL)
2436 {
2437 command_print(CMD_CTX,
2438 "error allocating buffer for section (%d bytes)",
2439 (int)(image.sections[i].size));
2440 break;
2441 }
2442
2443 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2444 {
2445 free(buffer);
2446 break;
2447 }
2448
2449 uint32_t offset = 0;
2450 uint32_t length = buf_cnt;
2451
2452 /* DANGER!!! beware of unsigned comparision here!!! */
2453
2454 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2455 (image.sections[i].base_address < max_address))
2456 {
2457 if (image.sections[i].base_address < min_address)
2458 {
2459 /* clip addresses below */
2460 offset += min_address-image.sections[i].base_address;
2461 length -= offset;
2462 }
2463
2464 if (image.sections[i].base_address + buf_cnt > max_address)
2465 {
2466 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2467 }
2468
2469 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2470 {
2471 free(buffer);
2472 break;
2473 }
2474 image_size += length;
2475 command_print(CMD_CTX, "%u bytes written at address 0x%8.8" PRIx32 "",
2476 (unsigned int)length,
2477 image.sections[i].base_address + offset);
2478 }
2479
2480 free(buffer);
2481 }
2482
2483 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2484 {
2485 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
2486 "in %fs (%0.3f kb/s)", image_size,
2487 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2488 }
2489
2490 image_close(&image);
2491
2492 return retval;
2493
2494 }
2495
2496 COMMAND_HANDLER(handle_dump_image_command)
2497 {
2498 struct fileio fileio;
2499
2500 uint8_t buffer[560];
2501 int retvaltemp;
2502
2503
2504 struct target *target = get_current_target(CMD_CTX);
2505
2506 if (CMD_ARGC != 3)
2507 {
2508 command_print(CMD_CTX, "usage: dump_image <filename> <address> <size>");
2509 return ERROR_OK;
2510 }
2511
2512 uint32_t address;
2513 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], address);
2514 uint32_t size;
2515 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], size);
2516
2517 if (fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2518 {
2519 return ERROR_OK;
2520 }
2521
2522 struct duration bench;
2523 duration_start(&bench);
2524
2525 int retval = ERROR_OK;
2526 while (size > 0)
2527 {
2528 size_t size_written;
2529 uint32_t this_run_size = (size > 560) ? 560 : size;
2530 retval = target_read_buffer(target, address, this_run_size, buffer);
2531 if (retval != ERROR_OK)
2532 {
2533 break;
2534 }
2535
2536 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2537 if (retval != ERROR_OK)
2538 {
2539 break;
2540 }
2541
2542 size -= this_run_size;
2543 address += this_run_size;
2544 }
2545
2546 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2547 return retvaltemp;
2548
2549 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2550 {
2551 command_print(CMD_CTX,
2552 "dumped %zu bytes in %fs (%0.3f kb/s)", fileio.size,
2553 duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
2554 }
2555
2556 return retval;
2557 }
2558
2559 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
2560 {
2561 uint8_t *buffer;
2562 size_t buf_cnt;
2563 uint32_t image_size;
2564 int i;
2565 int retval;
2566 uint32_t checksum = 0;
2567 uint32_t mem_checksum = 0;
2568
2569 struct image image;
2570
2571 struct target *target = get_current_target(CMD_CTX);
2572
2573 if (CMD_ARGC < 1)
2574 {
2575 return ERROR_COMMAND_SYNTAX_ERROR;
2576 }
2577
2578 if (!target)
2579 {
2580 LOG_ERROR("no target selected");
2581 return ERROR_FAIL;
2582 }
2583
2584 struct duration bench;
2585 duration_start(&bench);
2586
2587 if (CMD_ARGC >= 2)
2588 {
2589 uint32_t addr;
2590 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2591 image.base_address = addr;
2592 image.base_address_set = 1;
2593 }
2594 else
2595 {
2596 image.base_address_set = 0;
2597 image.base_address = 0x0;
2598 }
2599
2600 image.start_address_set = 0;
2601
2602 if ((retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL)) != ERROR_OK)
2603 {
2604 return retval;
2605 }
2606
2607 image_size = 0x0;
2608 retval = ERROR_OK;
2609 for (i = 0; i < image.num_sections; i++)
2610 {
2611 buffer = malloc(image.sections[i].size);
2612 if (buffer == NULL)
2613 {
2614 command_print(CMD_CTX,
2615 "error allocating buffer for section (%d bytes)",
2616 (int)(image.sections[i].size));
2617 break;
2618 }
2619 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2620 {
2621 free(buffer);
2622 break;
2623 }
2624
2625 if (verify)
2626 {
2627 /* calculate checksum of image */
2628 image_calculate_checksum(buffer, buf_cnt, &checksum);
2629
2630 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2631 if (retval != ERROR_OK)
2632 {
2633 free(buffer);
2634 break;
2635 }
2636
2637 if (checksum != mem_checksum)
2638 {
2639 /* failed crc checksum, fall back to a binary compare */
2640 uint8_t *data;
2641
2642 command_print(CMD_CTX, "checksum mismatch - attempting binary compare");
2643
2644 data = (uint8_t*)malloc(buf_cnt);
2645
2646 /* Can we use 32bit word accesses? */
2647 int size = 1;
2648 int count = buf_cnt;
2649 if ((count % 4) == 0)
2650 {
2651 size *= 4;
2652 count /= 4;
2653 }
2654 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2655 if (retval == ERROR_OK)
2656 {
2657 uint32_t t;
2658 for (t = 0; t < buf_cnt; t++)
2659 {
2660 if (data[t] != buffer[t])
2661 {
2662 command_print(CMD_CTX,
2663 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2664 (unsigned)(t + image.sections[i].base_address),
2665 data[t],
2666 buffer[t]);
2667 free(data);
2668 free(buffer);
2669 retval = ERROR_FAIL;
2670 goto done;
2671 }
2672 if ((t%16384) == 0)
2673 {
2674 keep_alive();
2675 }
2676 }
2677 }
2678
2679 free(data);
2680 }
2681 } else
2682 {
2683 command_print(CMD_CTX, "address 0x%08" PRIx32 " length 0x%08zx",
2684 image.sections[i].base_address,
2685 buf_cnt);
2686 }
2687
2688 free(buffer);
2689 image_size += buf_cnt;
2690 }
2691 done:
2692 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2693 {
2694 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
2695 "in %fs (%0.3f kb/s)", image_size,
2696 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2697 }
2698
2699 image_close(&image);
2700
2701 return retval;
2702 }
2703
2704 COMMAND_HANDLER(handle_verify_image_command)
2705 {
2706 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 1);
2707 }
2708
2709 COMMAND_HANDLER(handle_test_image_command)
2710 {
2711 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 0);
2712 }
2713
2714 static int handle_bp_command_list(struct command_context *cmd_ctx)
2715 {
2716 struct target *target = get_current_target(cmd_ctx);
2717 struct breakpoint *breakpoint = target->breakpoints;
2718 while (breakpoint)
2719 {
2720 if (breakpoint->type == BKPT_SOFT)
2721 {
2722 char* buf = buf_to_str(breakpoint->orig_instr,
2723 breakpoint->length, 16);
2724 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2725 breakpoint->address,
2726 breakpoint->length,
2727 breakpoint->set, buf);
2728 free(buf);
2729 }
2730 else
2731 {
2732 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2733 breakpoint->address,
2734 breakpoint->length, breakpoint->set);
2735 }
2736
2737 breakpoint = breakpoint->next;
2738 }
2739 return ERROR_OK;
2740 }
2741
2742 static int handle_bp_command_set(struct command_context *cmd_ctx,
2743 uint32_t addr, uint32_t length, int hw)
2744 {
2745 struct target *target = get_current_target(cmd_ctx);
2746 int retval = breakpoint_add(target, addr, length, hw);
2747 if (ERROR_OK == retval)
2748 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2749 else
2750 LOG_ERROR("Failure setting breakpoint");
2751 return retval;
2752 }
2753
2754 COMMAND_HANDLER(handle_bp_command)
2755 {
2756 if (CMD_ARGC == 0)
2757 return handle_bp_command_list(CMD_CTX);
2758
2759 if (CMD_ARGC < 2 || CMD_ARGC > 3)
2760 {
2761 command_print(CMD_CTX, "usage: bp <address> <length> ['hw']");
2762 return ERROR_COMMAND_SYNTAX_ERROR;
2763 }
2764
2765 uint32_t addr;
2766 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2767 uint32_t length;
2768 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2769
2770 int hw = BKPT_SOFT;
2771 if (CMD_ARGC == 3)
2772 {
2773 if (strcmp(CMD_ARGV[2], "hw") == 0)
2774 hw = BKPT_HARD;
2775 else
2776 return ERROR_COMMAND_SYNTAX_ERROR;
2777 }
2778
2779 return handle_bp_command_set(CMD_CTX, addr, length, hw);
2780 }
2781
2782 COMMAND_HANDLER(handle_rbp_command)
2783 {
2784 if (CMD_ARGC != 1)
2785 return ERROR_COMMAND_SYNTAX_ERROR;
2786
2787 uint32_t addr;
2788 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2789
2790 struct target *target = get_current_target(CMD_CTX);
2791 breakpoint_remove(target, addr);
2792
2793 return ERROR_OK;
2794 }
2795
2796 COMMAND_HANDLER(handle_wp_command)
2797 {
2798 struct target *target = get_current_target(CMD_CTX);
2799
2800 if (CMD_ARGC == 0)
2801 {
2802 struct watchpoint *watchpoint = target->watchpoints;
2803
2804 while (watchpoint)
2805 {
2806 command_print(CMD_CTX, "address: 0x%8.8" PRIx32
2807 ", len: 0x%8.8" PRIx32
2808 ", r/w/a: %i, value: 0x%8.8" PRIx32
2809 ", mask: 0x%8.8" PRIx32,
2810 watchpoint->address,
2811 watchpoint->length,
2812 (int)watchpoint->rw,
2813 watchpoint->value,
2814 watchpoint->mask);
2815 watchpoint = watchpoint->next;
2816 }
2817 return ERROR_OK;
2818 }
2819
2820 enum watchpoint_rw type = WPT_ACCESS;
2821 uint32_t addr = 0;
2822 uint32_t length = 0;
2823 uint32_t data_value = 0x0;
2824 uint32_t data_mask = 0xffffffff;
2825
2826 switch (CMD_ARGC)
2827 {
2828 case 5:
2829 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
2830 // fall through
2831 case 4:
2832 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
2833 // fall through
2834 case 3:
2835 switch (CMD_ARGV[2][0])
2836 {
2837 case 'r':
2838 type = WPT_READ;
2839 break;
2840 case 'w':
2841 type = WPT_WRITE;
2842 break;
2843 case 'a':
2844 type = WPT_ACCESS;
2845 break;
2846 default:
2847 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
2848 return ERROR_COMMAND_SYNTAX_ERROR;
2849 }
2850 // fall through
2851 case 2:
2852 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2853 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2854 break;
2855
2856 default:
2857 command_print(CMD_CTX, "usage: wp [address length "
2858 "[(r|w|a) [value [mask]]]]");
2859 return ERROR_COMMAND_SYNTAX_ERROR;
2860 }
2861
2862 int retval = watchpoint_add(target, addr, length, type,
2863 data_value, data_mask);
2864 if (ERROR_OK != retval)
2865 LOG_ERROR("Failure setting watchpoints");
2866
2867 return retval;
2868 }
2869
2870 COMMAND_HANDLER(handle_rwp_command)
2871 {
2872 if (CMD_ARGC != 1)
2873 return ERROR_COMMAND_SYNTAX_ERROR;
2874
2875 uint32_t addr;
2876 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2877
2878 struct target *target = get_current_target(CMD_CTX);
2879 watchpoint_remove(target, addr);
2880
2881 return ERROR_OK;
2882 }
2883
2884
2885 /**
2886 * Translate a virtual address to a physical address.
2887 *
2888 * The low-level target implementation must have logged a detailed error
2889 * which is forwarded to telnet/GDB session.
2890 */
2891 COMMAND_HANDLER(handle_virt2phys_command)
2892 {
2893 if (CMD_ARGC != 1)
2894 return ERROR_COMMAND_SYNTAX_ERROR;
2895
2896 uint32_t va;
2897 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], va);
2898 uint32_t pa;
2899
2900 struct target *target = get_current_target(CMD_CTX);
2901 int retval = target->type->virt2phys(target, va, &pa);
2902 if (retval == ERROR_OK)
2903 command_print(CMD_CTX, "Physical address 0x%08" PRIx32 "", pa);
2904
2905 return retval;
2906 }
2907
2908 static void writeData(FILE *f, const void *data, size_t len)
2909 {
2910 size_t written = fwrite(data, 1, len, f);
2911 if (written != len)
2912 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
2913 }
2914
2915 static void writeLong(FILE *f, int l)
2916 {
2917 int i;
2918 for (i = 0; i < 4; i++)
2919 {
2920 char c = (l >> (i*8))&0xff;
2921 writeData(f, &c, 1);
2922 }
2923
2924 }
2925
2926 static void writeString(FILE *f, char *s)
2927 {
2928 writeData(f, s, strlen(s));
2929 }
2930
2931 /* Dump a gmon.out histogram file. */
2932 static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filename)
2933 {
2934 uint32_t i;
2935 FILE *f = fopen(filename, "w");
2936 if (f == NULL)
2937 return;
2938 writeString(f, "gmon");
2939 writeLong(f, 0x00000001); /* Version */
2940 writeLong(f, 0); /* padding */
2941 writeLong(f, 0); /* padding */
2942 writeLong(f, 0); /* padding */
2943
2944 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
2945 writeData(f, &zero, 1);
2946
2947 /* figure out bucket size */
2948 uint32_t min = samples[0];
2949 uint32_t max = samples[0];
2950 for (i = 0; i < sampleNum; i++)
2951 {
2952 if (min > samples[i])
2953 {
2954 min = samples[i];
2955 }
2956 if (max < samples[i])
2957 {
2958 max = samples[i];
2959 }
2960 }
2961
2962 int addressSpace = (max-min + 1);
2963
2964 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
2965 uint32_t length = addressSpace;
2966 if (length > maxBuckets)
2967 {
2968 length = maxBuckets;
2969 }
2970 int *buckets = malloc(sizeof(int)*length);
2971 if (buckets == NULL)
2972 {
2973 fclose(f);
2974 return;
2975 }
2976 memset(buckets, 0, sizeof(int)*length);
2977 for (i = 0; i < sampleNum;i++)
2978 {
2979 uint32_t address = samples[i];
2980 long long a = address-min;
2981 long long b = length-1;
2982 long long c = addressSpace-1;
2983 int index = (a*b)/c; /* danger!!!! int32 overflows */
2984 buckets[index]++;
2985 }
2986
2987 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2988 writeLong(f, min); /* low_pc */
2989 writeLong(f, max); /* high_pc */
2990 writeLong(f, length); /* # of samples */
2991 writeLong(f, 64000000); /* 64MHz */
2992 writeString(f, "seconds");
2993 for (i = 0; i < (15-strlen("seconds")); i++)
2994 writeData(f, &zero, 1);
2995 writeString(f, "s");
2996
2997 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2998
2999 char *data = malloc(2*length);
3000 if (data != NULL)
3001 {
3002 for (i = 0; i < length;i++)
3003 {
3004 int val;
3005 val = buckets[i];
3006 if (val > 65535)
3007 {
3008 val = 65535;
3009 }
3010 data[i*2]=val&0xff;
3011 data[i*2 + 1]=(val >> 8)&0xff;
3012 }
3013 free(buckets);
3014 writeData(f, data, length * 2);
3015 free(data);
3016 } else
3017 {
3018 free(buckets);
3019 }
3020
3021 fclose(f);
3022 }
3023
3024 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
3025 COMMAND_HANDLER(handle_profile_command)
3026 {
3027 struct target *target = get_current_target(CMD_CTX);
3028 struct timeval timeout, now;
3029
3030 gettimeofday(&timeout, NULL);
3031 if (CMD_ARGC != 2)
3032 {
3033 return ERROR_COMMAND_SYNTAX_ERROR;
3034 }
3035 unsigned offset;
3036 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], offset);
3037
3038 timeval_add_time(&timeout, offset, 0);
3039
3040 command_print(CMD_CTX, "Starting profiling. Halting and resuming the target as often as we can...");
3041
3042 static const int maxSample = 10000;
3043 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3044 if (samples == NULL)
3045 return ERROR_OK;
3046
3047 int numSamples = 0;
3048 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3049 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
3050
3051 for (;;)
3052 {
3053 int retval;
3054 target_poll(target);
3055 if (target->state == TARGET_HALTED)
3056 {
3057 uint32_t t=*((uint32_t *)reg->value);
3058 samples[numSamples++]=t;
3059 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3060 target_poll(target);
3061 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3062 } else if (target->state == TARGET_RUNNING)
3063 {
3064 /* We want to quickly sample the PC. */
3065 if ((retval = target_halt(target)) != ERROR_OK)
3066 {
3067 free(samples);
3068 return retval;
3069 }
3070 } else
3071 {
3072 command_print(CMD_CTX, "Target not halted or running");
3073 retval = ERROR_OK;
3074 break;
3075 }
3076 if (retval != ERROR_OK)
3077 {
3078 break;
3079 }
3080
3081 gettimeofday(&now, NULL);
3082 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3083 {
3084 command_print(CMD_CTX, "Profiling completed. %d samples.", numSamples);
3085 if ((retval = target_poll(target)) != ERROR_OK)
3086 {
3087 free(samples);
3088 return retval;
3089 }
3090 if (target->state == TARGET_HALTED)
3091 {
3092 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3093 }
3094 if ((retval = target_poll(target)) != ERROR_OK)
3095 {
3096 free(samples);
3097 return retval;
3098 }
3099 writeGmon(samples, numSamples, CMD_ARGV[1]);
3100 command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
3101 break;
3102 }
3103 }
3104 free(samples);
3105
3106 return ERROR_OK;
3107 }
3108
3109 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3110 {
3111 char *namebuf;
3112 Jim_Obj *nameObjPtr, *valObjPtr;
3113 int result;
3114
3115 namebuf = alloc_printf("%s(%d)", varname, idx);
3116 if (!namebuf)
3117 return JIM_ERR;
3118
3119 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3120 valObjPtr = Jim_NewIntObj(interp, val);
3121 if (!nameObjPtr || !valObjPtr)
3122 {
3123 free(namebuf);
3124 return JIM_ERR;
3125 }
3126
3127 Jim_IncrRefCount(nameObjPtr);
3128 Jim_IncrRefCount(valObjPtr);
3129 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3130 Jim_DecrRefCount(interp, nameObjPtr);
3131 Jim_DecrRefCount(interp, valObjPtr);
3132 free(namebuf);
3133 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3134 return result;
3135 }
3136
3137 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3138 {
3139 struct command_context *context;
3140 struct target *target;
3141
3142 context = Jim_GetAssocData(interp, "context");
3143 if (context == NULL)
3144 {
3145 LOG_ERROR("mem2array: no command context");
3146 return JIM_ERR;
3147 }
3148 target = get_current_target(context);
3149 if (target == NULL)
3150 {
3151 LOG_ERROR("mem2array: no current target");
3152 return JIM_ERR;
3153 }
3154
3155 return target_mem2array(interp, target, argc-1, argv + 1);
3156 }
3157
3158 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3159 {
3160 long l;
3161 uint32_t width;
3162 int len;
3163 uint32_t addr;
3164 uint32_t count;
3165 uint32_t v;
3166 const char *varname;
3167 int n, e, retval;
3168 uint32_t i;
3169
3170 /* argv[1] = name of array to receive the data
3171 * argv[2] = desired width
3172 * argv[3] = memory address
3173 * argv[4] = count of times to read
3174 */
3175 if (argc != 4) {
3176 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3177 return JIM_ERR;
3178 }
3179 varname = Jim_GetString(argv[0], &len);
3180 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3181
3182 e = Jim_GetLong(interp, argv[1], &l);
3183 width = l;
3184 if (e != JIM_OK) {
3185 return e;
3186 }
3187
3188 e = Jim_GetLong(interp, argv[2], &l);
3189 addr = l;
3190 if (e != JIM_OK) {
3191 return e;
3192 }
3193 e = Jim_GetLong(interp, argv[3], &l);
3194 len = l;
3195 if (e != JIM_OK) {
3196 return e;
3197 }
3198 switch (width) {
3199 case 8:
3200 width = 1;
3201 break;
3202 case 16:
3203 width = 2;
3204 break;
3205 case 32:
3206 width = 4;
3207 break;
3208 default:
3209 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3210 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3211 return JIM_ERR;
3212 }
3213 if (len == 0) {
3214 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3215 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3216 return JIM_ERR;
3217 }
3218 if ((addr + (len * width)) < addr) {
3219 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3220 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3221 return JIM_ERR;
3222 }
3223 /* absurd transfer size? */
3224 if (len > 65536) {
3225 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3226 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3227 return JIM_ERR;
3228 }
3229
3230 if ((width == 1) ||
3231 ((width == 2) && ((addr & 1) == 0)) ||
3232 ((width == 4) && ((addr & 3) == 0))) {
3233 /* all is well */
3234 } else {
3235 char buf[100];
3236 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3237 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3238 addr,
3239 width);
3240 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3241 return JIM_ERR;
3242 }
3243
3244 /* Transfer loop */
3245
3246 /* index counter */
3247 n = 0;
3248
3249 size_t buffersize = 4096;
3250 uint8_t *buffer = malloc(buffersize);
3251 if (buffer == NULL)
3252 return JIM_ERR;
3253
3254 /* assume ok */
3255 e = JIM_OK;
3256 while (len) {
3257 /* Slurp... in buffer size chunks */
3258
3259 count = len; /* in objects.. */
3260 if (count > (buffersize/width)) {
3261 count = (buffersize/width);
3262 }
3263
3264 retval = target_read_memory(target, addr, width, count, buffer);
3265 if (retval != ERROR_OK) {
3266 /* BOO !*/
3267 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3268 (unsigned int)addr,
3269 (int)width,
3270 (int)count);
3271 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3272 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3273 e = JIM_ERR;
3274 len = 0;
3275 } else {
3276 v = 0; /* shut up gcc */
3277 for (i = 0 ;i < count ;i++, n++) {
3278 switch (width) {
3279 case 4:
3280 v = target_buffer_get_u32(target, &buffer[i*width]);
3281 break;
3282 case 2:
3283 v = target_buffer_get_u16(target, &buffer[i*width]);
3284 break;
3285 case 1:
3286 v = buffer[i] & 0x0ff;
3287 break;
3288 }
3289 new_int_array_element(interp, varname, n, v);
3290 }
3291 len -= count;
3292 }
3293 }
3294
3295 free(buffer);
3296
3297 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3298
3299 return JIM_OK;
3300 }
3301
3302 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3303 {
3304 char *namebuf;
3305 Jim_Obj *nameObjPtr, *valObjPtr;
3306 int result;
3307 long l;
3308
3309 namebuf = alloc_printf("%s(%d)", varname, idx);
3310 if (!namebuf)
3311 return JIM_ERR;
3312
3313 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3314 if (!nameObjPtr)
3315 {
3316 free(namebuf);
3317 return JIM_ERR;
3318 }
3319
3320 Jim_IncrRefCount(nameObjPtr);
3321 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3322 Jim_DecrRefCount(interp, nameObjPtr);
3323 free(namebuf);
3324 if (valObjPtr == NULL)
3325 return JIM_ERR;
3326
3327 result = Jim_GetLong(interp, valObjPtr, &l);
3328 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3329 *val = l;
3330 return result;
3331 }
3332
3333 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3334 {
3335 struct command_context *context;
3336 struct target *target;
3337
3338 context = Jim_GetAssocData(interp, "context");
3339 if (context == NULL) {
3340 LOG_ERROR("array2mem: no command context");
3341 return JIM_ERR;
3342 }
3343 target = get_current_target(context);
3344 if (target == NULL) {
3345 LOG_ERROR("array2mem: no current target");
3346 return JIM_ERR;
3347 }
3348
3349 return target_array2mem(interp,target, argc-1, argv + 1);
3350 }
3351 static int target_array2mem(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3352 {
3353 long l;
3354 uint32_t width;
3355 int len;
3356 uint32_t addr;
3357 uint32_t count;
3358 uint32_t v;
3359 const char *varname;
3360 int n, e, retval;
3361 uint32_t i;
3362
3363 /* argv[1] = name of array to get the data
3364 * argv[2] = desired width
3365 * argv[3] = memory address
3366 * argv[4] = count to write
3367 */
3368 if (argc != 4) {
3369 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems");
3370 return JIM_ERR;
3371 }
3372 varname = Jim_GetString(argv[0], &len);
3373 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3374
3375 e = Jim_GetLong(interp, argv[1], &l);
3376 width = l;
3377 if (e != JIM_OK) {
3378 return e;
3379 }
3380
3381 e = Jim_GetLong(interp, argv[2], &l);
3382 addr = l;
3383 if (e != JIM_OK) {
3384 return e;
3385 }
3386 e = Jim_GetLong(interp, argv[3], &l);
3387 len = l;
3388 if (e != JIM_OK) {
3389 return e;
3390 }
3391 switch (width) {
3392 case 8:
3393 width = 1;
3394 break;
3395 case 16:
3396 width = 2;
3397 break;
3398 case 32:
3399 width = 4;
3400 break;
3401 default:
3402 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3403 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3404 return JIM_ERR;
3405 }
3406 if (len == 0) {
3407 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3408 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3409 return JIM_ERR;
3410 }
3411 if ((addr + (len * width)) < addr) {
3412 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3413 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3414 return JIM_ERR;
3415 }
3416 /* absurd transfer size? */
3417 if (len > 65536) {
3418 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3419 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3420 return JIM_ERR;
3421 }
3422
3423 if ((width == 1) ||
3424 ((width == 2) && ((addr & 1) == 0)) ||
3425 ((width == 4) && ((addr & 3) == 0))) {
3426 /* all is well */
3427 } else {
3428 char buf[100];
3429 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3430 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3431 (unsigned int)addr,
3432 (int)width);
3433 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3434 return JIM_ERR;
3435 }
3436
3437 /* Transfer loop */
3438
3439 /* index counter */
3440 n = 0;
3441 /* assume ok */
3442 e = JIM_OK;
3443
3444 size_t buffersize = 4096;
3445 uint8_t *buffer = malloc(buffersize);
3446 if (buffer == NULL)
3447 return JIM_ERR;
3448
3449 while (len) {
3450 /* Slurp... in buffer size chunks */
3451
3452 count = len; /* in objects.. */
3453 if (count > (buffersize/width)) {
3454 count = (buffersize/width);
3455 }
3456
3457 v = 0; /* shut up gcc */
3458 for (i = 0 ;i < count ;i++, n++) {
3459 get_int_array_element(interp, varname, n, &v);
3460 switch (width) {
3461 case 4:
3462 target_buffer_set_u32(target, &buffer[i*width], v);
3463 break;
3464 case 2:
3465 target_buffer_set_u16(target, &buffer[i*width], v);
3466 break;
3467 case 1:
3468 buffer[i] = v & 0x0ff;
3469 break;
3470 }
3471 }
3472 len -= count;
3473
3474 retval = target_write_memory(target, addr, width, count, buffer);
3475 if (retval != ERROR_OK) {
3476 /* BOO !*/
3477 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3478 (unsigned int)addr,
3479 (int)width,
3480 (int)count);
3481 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3482 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3483 e = JIM_ERR;
3484 len = 0;
3485 }
3486 }
3487
3488 free(buffer);
3489
3490 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3491
3492 return JIM_OK;
3493 }
3494
3495 void target_all_handle_event(enum target_event e)
3496 {
3497 struct target *target;
3498
3499 LOG_DEBUG("**all*targets: event: %d, %s",
3500 (int)e,
3501 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3502
3503 target = all_targets;
3504 while (target) {
3505 target_handle_event(target, e);
3506 target = target->next;
3507 }
3508 }
3509
3510
3511 /* FIX? should we propagate errors here rather than printing them
3512 * and continuing?
3513 */
3514 void target_handle_event(struct target *target, enum target_event e)
3515 {
3516 struct target_event_action *teap;
3517
3518 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3519 if (teap->event == e) {
3520 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3521 target->target_number,
3522 target_name(target),
3523 target_type_name(target),
3524 e,
3525 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3526 Jim_GetString(teap->body, NULL));
3527 if (Jim_EvalObj(teap->interp, teap->body) != JIM_OK)
3528 {
3529 Jim_PrintErrorMessage(teap->interp);
3530 }
3531 }
3532 }
3533 }
3534
3535 /**
3536 * Returns true only if the target has a handler for the specified event.
3537 */
3538 bool target_has_event_action(struct target *target, enum target_event event)
3539 {
3540 struct target_event_action *teap;
3541
3542 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3543 if (teap->event == event)
3544 return true;
3545 }
3546 return false;
3547 }
3548
3549 enum target_cfg_param {
3550 TCFG_TYPE,
3551 TCFG_EVENT,
3552 TCFG_WORK_AREA_VIRT,
3553 TCFG_WORK_AREA_PHYS,
3554 TCFG_WORK_AREA_SIZE,
3555 TCFG_WORK_AREA_BACKUP,
3556 TCFG_ENDIAN,
3557 TCFG_VARIANT,
3558 TCFG_CHAIN_POSITION,
3559 };
3560
3561 static Jim_Nvp nvp_config_opts[] = {
3562 { .name = "-type", .value = TCFG_TYPE },
3563 { .name = "-event", .value = TCFG_EVENT },
3564 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3565 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3566 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3567 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3568 { .name = "-endian" , .value = TCFG_ENDIAN },
3569 { .name = "-variant", .value = TCFG_VARIANT },
3570 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3571
3572 { .name = NULL, .value = -1 }
3573 };
3574
3575 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
3576 {
3577 Jim_Nvp *n;
3578 Jim_Obj *o;
3579 jim_wide w;
3580 char *cp;
3581 int e;
3582
3583 /* parse config or cget options ... */
3584 while (goi->argc > 0) {
3585 Jim_SetEmptyResult(goi->interp);
3586 /* Jim_GetOpt_Debug(goi); */
3587
3588 if (target->type->target_jim_configure) {
3589 /* target defines a configure function */
3590 /* target gets first dibs on parameters */
3591 e = (*(target->type->target_jim_configure))(target, goi);
3592 if (e == JIM_OK) {
3593 /* more? */
3594 continue;
3595 }
3596 if (e == JIM_ERR) {
3597 /* An error */
3598 return e;
3599 }
3600 /* otherwise we 'continue' below */
3601 }
3602 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3603 if (e != JIM_OK) {
3604 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3605 return e;
3606 }
3607 switch (n->value) {
3608 case TCFG_TYPE:
3609 /* not setable */
3610 if (goi->isconfigure) {
3611 Jim_SetResult_sprintf(goi->interp,
3612 "not settable: %s", n->name);
3613 return JIM_ERR;
3614 } else {
3615 no_params:
3616 if (goi->argc != 0) {
3617 Jim_WrongNumArgs(goi->interp,
3618 goi->argc, goi->argv,
3619 "NO PARAMS");
3620 return JIM_ERR;
3621 }
3622 }
3623 Jim_SetResultString(goi->interp,
3624 target_type_name(target), -1);
3625 /* loop for more */
3626 break;
3627 case TCFG_EVENT:
3628 if (goi->argc == 0) {
3629 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3630 return JIM_ERR;
3631 }
3632
3633 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3634 if (e != JIM_OK) {
3635 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3636 return e;
3637 }
3638
3639 if (goi->isconfigure) {
3640 if (goi->argc != 1) {
3641 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3642 return JIM_ERR;
3643 }
3644 } else {
3645 if (goi->argc != 0) {
3646 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3647 return JIM_ERR;
3648 }
3649 }
3650
3651 {
3652 struct target_event_action *teap;
3653
3654 teap = target->event_action;
3655 /* replace existing? */
3656 while (teap) {
3657 if (teap->event == (enum target_event)n->value) {
3658 break;
3659 }
3660 teap = teap->next;
3661 }
3662
3663 if (goi->isconfigure) {
3664 bool replace = true;
3665 if (teap == NULL) {
3666 /* create new */
3667 teap = calloc(1, sizeof(*teap));
3668 replace = false;
3669 }
3670 teap->event = n->value;
3671 teap->interp = goi->interp;
3672 Jim_GetOpt_Obj(goi, &o);
3673 if (teap->body) {
3674 Jim_DecrRefCount(teap->interp, teap->body);
3675 }
3676 teap->body = Jim_DuplicateObj(goi->interp, o);
3677 /*
3678 * FIXME:
3679 * Tcl/TK - "tk events" have a nice feature.
3680 * See the "BIND" command.
3681 * We should support that here.
3682 * You can specify %X and %Y in the event code.
3683 * The idea is: %T - target name.
3684 * The idea is: %N - target number
3685 * The idea is: %E - event name.
3686 */
3687 Jim_IncrRefCount(teap->body);
3688
3689 if (!replace)
3690 {
3691 /* add to head of event list */
3692 teap->next = target->event_action;
3693 target->event_action = teap;
3694 }
3695 Jim_SetEmptyResult(goi->interp);
3696 } else {
3697 /* get */
3698 if (teap == NULL) {
3699 Jim_SetEmptyResult(goi->interp);
3700 } else {
3701 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3702 }
3703 }
3704 }
3705 /* loop for more */
3706 break;
3707
3708 case TCFG_WORK_AREA_VIRT:
3709 if (goi->isconfigure) {
3710 target_free_all_working_areas(target);
3711 e = Jim_GetOpt_Wide(goi, &w);
3712 if (e != JIM_OK) {
3713 return e;
3714 }
3715 target->working_area_virt = w;
3716 target->working_area_virt_spec = true;
3717 } else {
3718 if (goi->argc != 0) {
3719 goto no_params;
3720 }
3721 }
3722 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3723 /* loop for more */
3724 break;
3725
3726 case TCFG_WORK_AREA_PHYS:
3727 if (goi->isconfigure) {
3728 target_free_all_working_areas(target);
3729 e = Jim_GetOpt_Wide(goi, &w);
3730 if (e != JIM_OK) {
3731 return e;
3732 }
3733 target->working_area_phys = w;
3734 target->working_area_phys_spec = true;
3735 } else {
3736 if (goi->argc != 0) {
3737 goto no_params;
3738 }
3739 }
3740 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3741 /* loop for more */
3742 break;
3743
3744 case TCFG_WORK_AREA_SIZE:
3745 if (goi->isconfigure) {
3746 target_free_all_working_areas(target);
3747 e = Jim_GetOpt_Wide(goi, &w);
3748 if (e != JIM_OK) {
3749 return e;
3750 }
3751 target->working_area_size = w;
3752 } else {
3753 if (goi->argc != 0) {
3754 goto no_params;
3755 }
3756 }
3757 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3758 /* loop for more */
3759 break;
3760
3761 case TCFG_WORK_AREA_BACKUP:
3762 if (goi->isconfigure) {
3763 target_free_all_working_areas(target);
3764 e = Jim_GetOpt_Wide(goi, &w);
3765 if (e != JIM_OK) {
3766 return e;
3767 }
3768 /* make this exactly 1 or 0 */
3769 target->backup_working_area = (!!w);
3770 } else {
3771 if (goi->argc != 0) {
3772 goto no_params;
3773 }
3774 }
3775 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3776 /* loop for more e*/
3777 break;
3778
3779 case TCFG_ENDIAN:
3780 if (goi->isconfigure) {
3781 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3782 if (e != JIM_OK) {
3783 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3784 return e;
3785 }
3786 target->endianness = n->value;
3787 } else {
3788 if (goi->argc != 0) {
3789 goto no_params;
3790 }
3791 }
3792 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3793 if (n->name == NULL) {
3794 target->endianness = TARGET_LITTLE_ENDIAN;
3795 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3796 }
3797 Jim_SetResultString(goi->interp, n->name, -1);
3798 /* loop for more */
3799 break;
3800
3801 case TCFG_VARIANT:
3802 if (goi->isconfigure) {
3803 if (goi->argc < 1) {
3804 Jim_SetResult_sprintf(goi->interp,
3805 "%s ?STRING?",
3806 n->name);
3807 return JIM_ERR;
3808 }
3809 if (target->variant) {
3810 free((void *)(target->variant));
3811 }
3812 e = Jim_GetOpt_String(goi, &cp, NULL);
3813 target->variant = strdup(cp);
3814 } else {
3815 if (goi->argc != 0) {
3816 goto no_params;
3817 }
3818 }
3819 Jim_SetResultString(goi->interp, target->variant,-1);
3820 /* loop for more */
3821 break;
3822 case TCFG_CHAIN_POSITION:
3823 if (goi->isconfigure) {
3824 Jim_Obj *o;
3825 struct jtag_tap *tap;
3826 target_free_all_working_areas(target);
3827 e = Jim_GetOpt_Obj(goi, &o);
3828 if (e != JIM_OK) {
3829 return e;
3830 }
3831 tap = jtag_tap_by_jim_obj(goi->interp, o);
3832 if (tap == NULL) {
3833 return JIM_ERR;
3834 }
3835 /* make this exactly 1 or 0 */
3836 target->tap = tap;
3837 } else {
3838 if (goi->argc != 0) {
3839 goto no_params;
3840 }
3841 }
3842 Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
3843 /* loop for more e*/
3844 break;
3845 }
3846 } /* while (goi->argc) */
3847
3848
3849 /* done - we return */
3850 return JIM_OK;
3851 }
3852
3853 static int jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3854 {
3855 Jim_GetOptInfo goi;
3856 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3857 goi.isconfigure = strcmp(Jim_GetString(argv[0], NULL), "configure") == 0;
3858 int need_args = 1 + goi.isconfigure;
3859 if (goi.argc < need_args)
3860 {
3861 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
3862 goi.isconfigure
3863 ? "missing: -option VALUE ..."
3864 : "missing: -option ...");
3865 return JIM_ERR;
3866 }
3867 struct target *target = Jim_CmdPrivData(goi.interp);
3868 return target_configure(&goi, target);
3869 }
3870
3871 static int jim_target_mw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3872 {
3873 const char *cmd_name = Jim_GetString(argv[0], NULL);
3874
3875 Jim_GetOptInfo goi;
3876 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3877
3878 if (goi.argc != 2 && goi.argc != 3)
3879 {
3880 Jim_SetResult_sprintf(goi.interp,
3881 "usage: %s <address> <data> [<count>]", cmd_name);
3882 return JIM_ERR;
3883 }
3884
3885 jim_wide a;
3886 int e = Jim_GetOpt_Wide(&goi, &a);
3887 if (e != JIM_OK)
3888 return e;
3889
3890 jim_wide b;
3891 e = Jim_GetOpt_Wide(&goi, &b);
3892 if (e != JIM_OK)
3893 return e;
3894
3895 jim_wide c = 1;
3896 if (goi.argc == 3)
3897 {
3898 e = Jim_GetOpt_Wide(&goi, &c);
3899 if (e != JIM_OK)
3900 return e;
3901 }
3902
3903 struct target *target = Jim_CmdPrivData(goi.interp);
3904 uint8_t target_buf[32];
3905 if (strcasecmp(cmd_name, "mww") == 0) {
3906 target_buffer_set_u32(target, target_buf, b);
3907 b = 4;
3908 }
3909 else if (strcasecmp(cmd_name, "mwh") == 0) {
3910 target_buffer_set_u16(target, target_buf, b);
3911 b = 2;
3912 }
3913 else if (strcasecmp(cmd_name, "mwb") == 0) {
3914 target_buffer_set_u8(target, target_buf, b);
3915 b = 1;
3916 } else {
3917 LOG_ERROR("command '%s' unknown: ", cmd_name);
3918 return JIM_ERR;
3919 }
3920
3921 for (jim_wide x = 0; x < c; x++)
3922 {
3923 e = target_write_memory(target, a, b, 1, target_buf);
3924 if (e != ERROR_OK)
3925 {
3926 Jim_SetResult_sprintf(interp,
3927 "Error writing @ 0x%08x: %d\n", (int)(a), e);
3928 return JIM_ERR;
3929 }
3930 /* b = width */
3931 a = a + b;
3932 }
3933 return JIM_OK;
3934 }
3935
3936 static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3937 {
3938 const char *cmd_name = Jim_GetString(argv[0], NULL);
3939
3940 Jim_GetOptInfo goi;
3941 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3942
3943 if ((goi.argc == 2) || (goi.argc == 3))
3944 {
3945 Jim_SetResult_sprintf(goi.interp,
3946 "usage: %s <address> [<count>]", cmd_name);
3947 return JIM_ERR;
3948 }
3949
3950 jim_wide a;
3951 int e = Jim_GetOpt_Wide(&goi, &a);
3952 if (e != JIM_OK) {
3953 return JIM_ERR;
3954 }
3955 jim_wide c;
3956 if (goi.argc) {
3957 e = Jim_GetOpt_Wide(&goi, &c);
3958 if (e != JIM_OK) {
3959 return JIM_ERR;
3960 }
3961 } else {
3962 c = 1;
3963 }
3964 jim_wide b = 1; /* shut up gcc */
3965 if (strcasecmp(cmd_name, "mdw") == 0)
3966 b = 4;
3967 else if (strcasecmp(cmd_name, "mdh") == 0)
3968 b = 2;
3969 else if (strcasecmp(cmd_name, "mdb") == 0)
3970 b = 1;
3971 else {
3972 LOG_ERROR("command '%s' unknown: ", cmd_name);
3973 return JIM_ERR;
3974 }
3975
3976 /* convert count to "bytes" */
3977 c = c * b;
3978
3979 struct target *target = Jim_CmdPrivData(goi.interp);
3980 uint8_t target_buf[32];
3981 jim_wide x, y, z;
3982 while (c > 0) {
3983 y = c;
3984 if (y > 16) {
3985 y = 16;
3986 }
3987 e = target_read_memory(target, a, b, y / b, target_buf);
3988 if (e != ERROR_OK) {
3989 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
3990 return JIM_ERR;
3991 }
3992
3993 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
3994 switch (b) {
3995 case 4:
3996 for (x = 0; x < 16 && x < y; x += 4)
3997 {
3998 z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
3999 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
4000 }
4001 for (; (x < 16) ; x += 4) {
4002 Jim_fprintf(interp, interp->cookie_stdout, " ");
4003 }
4004 break;
4005 case 2:
4006 for (x = 0; x < 16 && x < y; x += 2)
4007 {
4008 z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
4009 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
4010 }
4011 for (; (x < 16) ; x += 2) {
4012 Jim_fprintf(interp, interp->cookie_stdout, " ");
4013 }
4014 break;
4015 case 1:
4016 default:
4017 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4018 z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
4019 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
4020 }
4021 for (; (x < 16) ; x += 1) {
4022 Jim_fprintf(interp, interp->cookie_stdout, " ");
4023 }
4024 break;
4025 }
4026 /* ascii-ify the bytes */
4027 for (x = 0 ; x < y ; x++) {
4028 if ((target_buf[x] >= 0x20) &&
4029 (target_buf[x] <= 0x7e)) {
4030 /* good */
4031 } else {
4032 /* smack it */
4033 target_buf[x] = '.';
4034 }
4035 }
4036 /* space pad */
4037 while (x < 16) {
4038 target_buf[x] = ' ';
4039 x++;
4040 }
4041 /* terminate */
4042 target_buf[16] = 0;
4043 /* print - with a newline */
4044 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
4045 /* NEXT... */
4046 c -= 16;
4047 a += 16;
4048 }
4049 return JIM_OK;
4050 }
4051
4052 static int jim_target_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4053 {
4054 struct target *target = Jim_CmdPrivData(interp);
4055 return target_mem2array(interp, target, argc - 1, argv + 1);
4056 }
4057
4058 static int jim_target_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4059 {
4060 struct target *target = Jim_CmdPrivData(interp);
4061 return target_array2mem(interp, target, argc - 1, argv + 1);
4062 }
4063
4064 static int jim_target_tap_disabled(Jim_Interp *interp)
4065 {
4066 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4067 return JIM_ERR;
4068 }
4069
4070 static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4071 {
4072 if (argc != 1)
4073 {
4074 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4075 return JIM_ERR;
4076 }
4077 struct target *target = Jim_CmdPrivData(interp);
4078 if (!target->tap->enabled)
4079 return jim_target_tap_disabled(interp);
4080
4081 int e = target->type->examine(target);
4082 if (e != ERROR_OK)
4083 {
4084 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
4085 return JIM_ERR;
4086 }
4087 return JIM_OK;
4088 }
4089
4090 static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4091 {
4092 if (argc != 1)
4093 {
4094 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4095 return JIM_ERR;
4096 }
4097 struct target *target = Jim_CmdPrivData(interp);
4098 if (!target->tap->enabled)
4099 return jim_target_tap_disabled(interp);
4100
4101 int e;
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 {
4109 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4110 return JIM_ERR;
4111 }
4112 return JIM_OK;
4113 }
4114
4115 static int jim_target_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4116 {
4117 Jim_GetOptInfo goi;
4118 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4119
4120 if (goi.argc != 2)
4121 {
4122 Jim_WrongNumArgs(interp, 0, argv,
4123 "([tT]|[fF]|assert|deassert) BOOL");
4124 return JIM_ERR;
4125 }
4126
4127 Jim_Nvp *n;
4128 int e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4129 if (e != JIM_OK)
4130 {
4131 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4132 return e;
4133 }
4134 /* the halt or not param */
4135 jim_wide a;
4136 e = Jim_GetOpt_Wide(&goi, &a);
4137 if (e != JIM_OK)
4138 return e;
4139
4140 struct target *target = Jim_CmdPrivData(goi.interp);
4141 if (!target->tap->enabled)
4142 return jim_target_tap_disabled(interp);
4143 if (!target->type->assert_reset || !target->type->deassert_reset)
4144 {
4145 Jim_SetResult_sprintf(interp,
4146 "No target-specific reset for %s",
4147 target_name(target));
4148 return JIM_ERR;
4149 }
4150 /* determine if we should halt or not. */
4151 target->reset_halt = !!a;
4152 /* When this happens - all workareas are invalid. */
4153 target_free_all_working_areas_restore(target, 0);
4154
4155 /* do the assert */
4156 if (n->value == NVP_ASSERT) {
4157 e = target->type->assert_reset(target);
4158 } else {
4159 e = target->type->deassert_reset(target);
4160 }
4161 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4162 }
4163
4164 static int jim_target_halt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4165 {
4166 if (argc != 1) {
4167 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4168 return JIM_ERR;
4169 }
4170 struct target *target = Jim_CmdPrivData(interp);
4171 if (!target->tap->enabled)
4172 return jim_target_tap_disabled(interp);
4173 int e = target->type->halt(target);
4174 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4175 }
4176
4177 static int jim_target_wait_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4178 {
4179 Jim_GetOptInfo goi;
4180 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4181
4182 /* params: <name> statename timeoutmsecs */
4183 if (goi.argc != 2)
4184 {
4185 const char *cmd_name = Jim_GetString(argv[0], NULL);
4186 Jim_SetResult_sprintf(goi.interp,
4187 "%s <state_name> <timeout_in_msec>", cmd_name);
4188 return JIM_ERR;
4189 }
4190
4191 Jim_Nvp *n;
4192 int e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4193 if (e != JIM_OK) {
4194 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4195 return e;
4196 }
4197 jim_wide a;
4198 e = Jim_GetOpt_Wide(&goi, &a);
4199 if (e != JIM_OK) {
4200 return e;
4201 }
4202 struct target *target = Jim_CmdPrivData(interp);
4203 if (!target->tap->enabled)
4204 return jim_target_tap_disabled(interp);
4205
4206 e = target_wait_state(target, n->value, a);
4207 if (e != ERROR_OK)
4208 {
4209 Jim_SetResult_sprintf(goi.interp,
4210 "target: %s wait %s fails (%d) %s",
4211 target_name(target), n->name,
4212 e, target_strerror_safe(e));
4213 return JIM_ERR;
4214 }
4215 return JIM_OK;
4216 }
4217 /* List for human, Events defined for this target.
4218 * scripts/programs should use 'name cget -event NAME'
4219 */
4220 static int jim_target_event_list(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4221 {
4222 struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
4223 struct target *target = Jim_CmdPrivData(interp);
4224 struct target_event_action *teap = target->event_action;
4225 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4226 target->target_number,
4227 target_name(target));
4228 command_print(cmd_ctx, "%-25s | Body", "Event");
4229 command_print(cmd_ctx, "------------------------- | "
4230 "----------------------------------------");
4231 while (teap)
4232 {
4233 Jim_Nvp *opt = Jim_Nvp_value2name_simple(nvp_target_event, teap->event);
4234 command_print(cmd_ctx, "%-25s | %s",
4235 opt->name, Jim_GetString(teap->body, NULL));
4236 teap = teap->next;
4237 }
4238 command_print(cmd_ctx, "***END***");
4239 return JIM_OK;
4240 }
4241 static int jim_target_current_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4242 {
4243 if (argc != 1)
4244 {
4245 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4246 return JIM_ERR;
4247 }
4248 struct target *target = Jim_CmdPrivData(interp);
4249 Jim_SetResultString(interp, target_state_name(target), -1);
4250 return JIM_OK;
4251 }
4252 static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4253 {
4254 Jim_GetOptInfo goi;
4255 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4256 if (goi.argc != 1)
4257 {
4258 const char *cmd_name = Jim_GetString(argv[0], NULL);
4259 Jim_SetResult_sprintf(goi.interp, "%s <eventname>", cmd_name);
4260 return JIM_ERR;
4261 }
4262 Jim_Nvp *n;
4263 int e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4264 if (e != JIM_OK)
4265 {
4266 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4267 return e;
4268 }
4269 struct target *target = Jim_CmdPrivData(interp);
4270 target_handle_event(target, n->value);
4271 return JIM_OK;
4272 }
4273
4274 static const struct command_registration target_instance_command_handlers[] = {
4275 {
4276 .name = "configure",
4277 .mode = COMMAND_CONFIG,
4278 .jim_handler = &jim_target_configure,
4279 .usage = "[<target_options> ...]",
4280 .help = "configure a new target for use",
4281 },
4282 {
4283 .name = "cget",
4284 .mode = COMMAND_ANY,
4285 .jim_handler = &jim_target_configure,
4286 .usage = "<target_type> [<target_options> ...]",
4287 .help = "configure a new target for use",
4288 },
4289 {
4290 .name = "mww",
4291 .mode = COMMAND_EXEC,
4292 .jim_handler = &jim_target_mw,
4293 .usage = "<address> <data> [<count>]",
4294 .help = "Write 32-bit word(s) to target memory",
4295 },
4296 {
4297 .name = "mwh",
4298 .mode = COMMAND_EXEC,
4299 .jim_handler = &jim_target_mw,
4300 .usage = "<address> <data> [<count>]",
4301 .help = "Write 16-bit half-word(s) to target memory",
4302 },
4303 {
4304 .name = "mwb",
4305 .mode = COMMAND_EXEC,
4306 .jim_handler = &jim_target_mw,
4307 .usage = "<address> <data> [<count>]",
4308 .help = "Write byte(s) to target memory",
4309 },
4310 {
4311 .name = "mdw",
4312 .mode = COMMAND_EXEC,
4313 .jim_handler = &jim_target_md,
4314 .usage = "<address> [<count>]",
4315 .help = "Display target memory as 32-bit words",
4316 },
4317 {
4318 .name = "mdh",
4319 .mode = COMMAND_EXEC,
4320 .jim_handler = &jim_target_md,
4321 .usage = "<address> [<count>]",
4322 .help = "Display target memory as 16-bit half-words",
4323 },
4324 {
4325 .name = "mdb",
4326 .mode = COMMAND_EXEC,
4327 .jim_handler = &jim_target_md,
4328 .usage = "<address> [<count>]",
4329 .help = "Display target memory as 8-bit bytes",
4330 },
4331 {
4332 .name = "array2mem",
4333 .mode = COMMAND_EXEC,
4334 .jim_handler = &jim_target_array2mem,
4335 },
4336 {
4337 .name = "mem2array",
4338 .mode = COMMAND_EXEC,
4339 .jim_handler = &jim_target_mem2array,
4340 },
4341 {
4342 .name = "eventlist",
4343 .mode = COMMAND_EXEC,
4344 .jim_handler = &jim_target_event_list,
4345 },
4346 {
4347 .name = "curstate",
4348 .mode = COMMAND_EXEC,
4349 .jim_handler = &jim_target_current_state,
4350 },
4351 {
4352 .name = "arp_examine",
4353 .mode = COMMAND_EXEC,
4354 .jim_handler = &jim_target_examine,
4355 },
4356 {
4357 .name = "arp_poll",
4358 .mode = COMMAND_EXEC,
4359 .jim_handler = &jim_target_poll,
4360 },
4361 {
4362 .name = "arp_reset",
4363 .mode = COMMAND_EXEC,
4364 .jim_handler = &jim_target_reset,
4365 },
4366 {
4367 .name = "arp_halt",
4368 .mode = COMMAND_EXEC,
4369 .jim_handler = &jim_target_halt,
4370 },
4371 {
4372 .name = "arp_waitstate",
4373 .mode = COMMAND_EXEC,
4374 .jim_handler = &jim_target_wait_state,
4375 },
4376 {
4377 .name = "invoke-event",
4378 .mode = COMMAND_EXEC,
4379 .jim_handler = &jim_target_invoke_event,
4380 },
4381 COMMAND_REGISTRATION_DONE
4382 };
4383
4384 static int target_create(Jim_GetOptInfo *goi)
4385 {
4386 Jim_Obj *new_cmd;
4387 Jim_Cmd *cmd;
4388 const char *cp;
4389 char *cp2;
4390 int e;
4391 int x;
4392 struct target *target;
4393 struct command_context *cmd_ctx;
4394
4395 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4396 if (goi->argc < 3) {
4397 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4398 return JIM_ERR;
4399 }
4400
4401 /* COMMAND */
4402 Jim_GetOpt_Obj(goi, &new_cmd);
4403 /* does this command exist? */
4404 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4405 if (cmd) {
4406 cp = Jim_GetString(new_cmd, NULL);
4407 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4408 return JIM_ERR;
4409 }
4410
4411 /* TYPE */
4412 e = Jim_GetOpt_String(goi, &cp2, NULL);
4413 cp = cp2;
4414 /* now does target type exist */
4415 for (x = 0 ; target_types[x] ; x++) {
4416 if (0 == strcmp(cp, target_types[x]->name)) {
4417 /* found */
4418 break;
4419 }
4420 }
4421 if (target_types[x] == NULL) {
4422 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4423 for (x = 0 ; target_types[x] ; x++) {
4424 if (target_types[x + 1]) {
4425 Jim_AppendStrings(goi->interp,
4426 Jim_GetResult(goi->interp),
4427 target_types[x]->name,
4428 ", ", NULL);
4429 } else {
4430 Jim_AppendStrings(goi->interp,
4431 Jim_GetResult(goi->interp),
4432 " or ",
4433 target_types[x]->name,NULL);
4434 }
4435 }
4436 return JIM_ERR;
4437 }
4438
4439 /* Create it */
4440 target = calloc(1,sizeof(struct target));
4441 /* set target number */
4442 target->target_number = new_target_number();
4443
4444 /* allocate memory for each unique target type */
4445 target->type = (struct target_type*)calloc(1,sizeof(struct target_type));
4446
4447 memcpy(target->type, target_types[x], sizeof(struct target_type));
4448
4449 /* will be set by "-endian" */
4450 target->endianness = TARGET_ENDIAN_UNKNOWN;
4451
4452 target->working_area = 0x0;
4453 target->working_area_size = 0x0;
4454 target->working_areas = NULL;
4455 target->backup_working_area = 0;
4456
4457 target->state = TARGET_UNKNOWN;
4458 target->debug_reason = DBG_REASON_UNDEFINED;
4459 target->reg_cache = NULL;
4460 target->breakpoints = NULL;
4461 target->watchpoints = NULL;
4462 target->next = NULL;
4463 target->arch_info = NULL;
4464
4465 target->display = 1;
4466
4467 target->halt_issued = false;
4468
4469 /* initialize trace information */
4470 target->trace_info = malloc(sizeof(struct trace));
4471 target->trace_info->num_trace_points = 0;
4472 target->trace_info->trace_points_size = 0;
4473 target->trace_info->trace_points = NULL;
4474 target->trace_info->trace_history_size = 0;
4475 target->trace_info->trace_history = NULL;
4476 target->trace_info->trace_history_pos = 0;
4477 target->trace_info->trace_history_overflowed = 0;
4478
4479 target->dbgmsg = NULL;
4480 target->dbg_msg_enabled = 0;
4481
4482 target->endianness = TARGET_ENDIAN_UNKNOWN;
4483
4484 /* Do the rest as "configure" options */
4485 goi->isconfigure = 1;
4486 e = target_configure(goi, target);
4487
4488 if (target->tap == NULL)
4489 {
4490 Jim_SetResultString(goi->interp, "-chain-position required when creating target", -1);
4491 e = JIM_ERR;
4492 }
4493
4494 if (e != JIM_OK) {
4495 free(target->type);
4496 free(target);
4497 return e;
4498 }
4499
4500 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4501 /* default endian to little if not specified */
4502 target->endianness = TARGET_LITTLE_ENDIAN;
4503 }
4504
4505 /* incase variant is not set */
4506 if (!target->variant)
4507 target->variant = strdup("");
4508
4509 cp = Jim_GetString(new_cmd, NULL);
4510 target->cmd_name = strdup(cp);
4511
4512 /* create the target specific commands */
4513 if (target->type->commands) {
4514 e = register_commands(cmd_ctx, NULL, target->type->commands);
4515 if (ERROR_OK != e)
4516 LOG_ERROR("unable to register '%s' commands", cp);
4517 }
4518 if (target->type->target_create) {
4519 (*(target->type->target_create))(target, goi->interp);
4520 }
4521
4522 /* append to end of list */
4523 {
4524 struct target **tpp;
4525 tpp = &(all_targets);
4526 while (*tpp) {
4527 tpp = &((*tpp)->next);
4528 }
4529 *tpp = target;
4530 }
4531
4532 /* now - create the new target name command */
4533 const const struct command_registration target_subcommands[] = {
4534 {
4535 .chain = target_instance_command_handlers,
4536 },
4537 {
4538 .chain = target->type->commands,
4539 },
4540 COMMAND_REGISTRATION_DONE
4541 };
4542 const const struct command_registration target_commands[] = {
4543 {
4544 .name = cp,
4545 .mode = COMMAND_ANY,
4546 .help = "target command group",
4547 .chain = target_subcommands,
4548 },
4549 COMMAND_REGISTRATION_DONE
4550 };
4551 e = register_commands(cmd_ctx, NULL, target_commands);
4552 if (ERROR_OK != e)
4553 return JIM_ERR;
4554
4555 struct command *c = command_find_in_context(cmd_ctx, cp);
4556 assert(c);
4557 command_set_handler_data(c, target);
4558
4559 return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
4560 }
4561
4562 static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4563 {
4564 if (argc != 1)
4565 {
4566 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4567 return JIM_ERR;
4568 }
4569 struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
4570 Jim_SetResultString(interp, get_current_target(cmd_ctx)->cmd_name, -1);
4571 return JIM_OK;
4572 }
4573
4574 static int jim_target_types(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4575 {
4576 if (argc != 1)
4577 {
4578 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4579 return JIM_ERR;
4580 }
4581 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
4582 for (unsigned x = 0; NULL != target_types[x]; x++)
4583 {
4584 Jim_ListAppendElement(interp, Jim_GetResult(interp),
4585 Jim_NewStringObj(interp, target_types[x]->name, -1));
4586 }
4587 return JIM_OK;
4588 }
4589
4590 static int jim_target_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4591 {
4592 if (argc != 1)
4593 {
4594 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4595 return JIM_ERR;
4596 }
4597 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
4598 struct target *target = all_targets;
4599 while (target)
4600 {
4601 Jim_ListAppendElement(interp, Jim_GetResult(interp),
4602 Jim_NewStringObj(interp, target_name(target), -1));
4603 target = target->next;
4604 }
4605 return JIM_OK;
4606 }
4607
4608 static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4609 {
4610 Jim_GetOptInfo goi;
4611 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4612 if (goi.argc < 3)
4613 {
4614 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
4615 "<name> <target_type> [<target_options> ...]");
4616 return JIM_ERR;
4617 }
4618 return target_create(&goi);
4619 }
4620
4621 static int jim_target_number(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4622 {
4623 Jim_GetOptInfo goi;
4624 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4625
4626 /* It's OK to remove this mechanism sometime after August 2010 or so */
4627 LOG_WARNING("don't use numbers as target identifiers; use names");
4628 if (goi.argc != 1)
4629 {
4630 Jim_SetResult_sprintf(goi.interp, "usage: target number <number>");
4631 return JIM_ERR;
4632 }
4633 jim_wide w;
4634 int e = Jim_GetOpt_Wide(&goi, &w);
4635 if (e != JIM_OK)
4636 return JIM_ERR;
4637
4638 struct target *target;
4639 for (target = all_targets; NULL != target; target = target->next)
4640 {
4641 if (target->target_number != w)
4642 continue;
4643
4644 Jim_SetResultString(goi.interp, target_name(target), -1);
4645 return JIM_OK;
4646 }
4647 Jim_SetResult_sprintf(goi.interp,
4648 "Target: number %d does not exist", (int)(w));
4649 return JIM_ERR;
4650 }
4651
4652 static int jim_target_count(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4653 {
4654 if (argc != 1)
4655 {
4656 Jim_WrongNumArgs(interp, 1, argv, "<no parameters>");
4657 return JIM_ERR;
4658 }
4659 unsigned count = 0;
4660 struct target *target = all_targets;
4661 while (NULL != target)
4662 {
4663 target = target->next;
4664 count++;
4665 }
4666 Jim_SetResult(interp, Jim_NewIntObj(interp, count));
4667 return JIM_OK;
4668 }
4669
4670 static const struct command_registration target_subcommand_handlers[] = {
4671 {
4672 .name = "create",
4673 .mode = COMMAND_ANY,
4674 .jim_handler = &jim_target_create,
4675 .usage = "<name> <type> ...",
4676 .help = "Returns the currently selected target",
4677 },
4678 {
4679 .name = "current",
4680 .mode = COMMAND_ANY,
4681 .jim_handler = &jim_target_current,
4682 .help = "Returns the currently selected target",
4683 },
4684 {
4685 .name = "types",
4686 .mode = COMMAND_ANY,
4687 .jim_handler = &jim_target_types,
4688 .help = "Returns the available target types as a list of strings",
4689 },
4690 {
4691 .name = "names",
4692 .mode = COMMAND_ANY,
4693 .jim_handler = &jim_target_names,
4694 .help = "Returns the names of all targets as a list of strings",
4695 },
4696 {
4697 .name = "number",
4698 .mode = COMMAND_ANY,
4699 .jim_handler = &jim_target_number,
4700 .usage = "<number>",
4701 .help = "Returns the name of target <n>",
4702 },
4703 {
4704 .name = "count",
4705 .mode = COMMAND_ANY,
4706 .jim_handler = &jim_target_count,
4707 .help = "Returns the number of targets as an integer",
4708 },
4709 COMMAND_REGISTRATION_DONE
4710 };
4711
4712
4713 struct FastLoad
4714 {
4715 uint32_t address;
4716 uint8_t *data;
4717 int length;
4718
4719 };
4720
4721 static int fastload_num;
4722 static struct FastLoad *fastload;
4723
4724 static void free_fastload(void)
4725 {
4726 if (fastload != NULL)
4727 {
4728 int i;
4729 for (i = 0; i < fastload_num; i++)
4730 {
4731 if (fastload[i].data)
4732 free(fastload[i].data);
4733 }
4734 free(fastload);
4735 fastload = NULL;
4736 }
4737 }
4738
4739
4740
4741
4742 COMMAND_HANDLER(handle_fast_load_image_command)
4743 {
4744 uint8_t *buffer;
4745 size_t buf_cnt;
4746 uint32_t image_size;
4747 uint32_t min_address = 0;
4748 uint32_t max_address = 0xffffffff;
4749 int i;
4750
4751 struct image image;
4752
4753 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
4754 &image, &min_address, &max_address);
4755 if (ERROR_OK != retval)
4756 return retval;
4757
4758 struct duration bench;
4759 duration_start(&bench);
4760
4761 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
4762 {
4763 return ERROR_OK;
4764 }
4765
4766 image_size = 0x0;
4767 retval = ERROR_OK;
4768 fastload_num = image.num_sections;
4769 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4770 if (fastload == NULL)
4771 {
4772 image_close(&image);
4773 return ERROR_FAIL;
4774 }
4775 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4776 for (i = 0; i < image.num_sections; i++)
4777 {
4778 buffer = malloc(image.sections[i].size);
4779 if (buffer == NULL)
4780 {
4781 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
4782 (int)(image.sections[i].size));
4783 break;
4784 }
4785
4786 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4787 {
4788 free(buffer);
4789 break;
4790 }
4791
4792 uint32_t offset = 0;
4793 uint32_t length = buf_cnt;
4794
4795
4796 /* DANGER!!! beware of unsigned comparision here!!! */
4797
4798 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4799 (image.sections[i].base_address < max_address))
4800 {
4801 if (image.sections[i].base_address < min_address)
4802 {
4803 /* clip addresses below */
4804 offset += min_address-image.sections[i].base_address;
4805 length -= offset;
4806 }
4807
4808 if (image.sections[i].base_address + buf_cnt > max_address)
4809 {
4810 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4811 }
4812
4813 fastload[i].address = image.sections[i].base_address + offset;
4814 fastload[i].data = malloc(length);
4815 if (fastload[i].data == NULL)
4816 {
4817 free(buffer);
4818 break;
4819 }
4820 memcpy(fastload[i].data, buffer + offset, length);
4821 fastload[i].length = length;
4822
4823 image_size += length;
4824 command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
4825 (unsigned int)length,
4826 ((unsigned int)(image.sections[i].base_address + offset)));
4827 }
4828
4829 free(buffer);
4830 }
4831
4832 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
4833 {
4834 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
4835 "in %fs (%0.3f kb/s)", image_size,
4836 duration_elapsed(&bench), duration_kbps(&bench, image_size));
4837
4838 command_print(CMD_CTX,
4839 "WARNING: image has not been loaded to target!"
4840 "You can issue a 'fast_load' to finish loading.");
4841 }
4842
4843 image_close(&image);
4844
4845 if (retval != ERROR_OK)
4846 {
4847 free_fastload();
4848 }
4849
4850 return retval;
4851 }
4852
4853 COMMAND_HANDLER(handle_fast_load_command)
4854 {
4855 if (CMD_ARGC > 0)
4856 return ERROR_COMMAND_SYNTAX_ERROR;
4857 if (fastload == NULL)
4858 {
4859 LOG_ERROR("No image in memory");
4860 return ERROR_FAIL;
4861 }
4862 int i;
4863 int ms = timeval_ms();
4864 int size = 0;
4865 int retval = ERROR_OK;
4866 for (i = 0; i < fastload_num;i++)
4867 {
4868 struct target *target = get_current_target(CMD_CTX);
4869 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
4870 (unsigned int)(fastload[i].address),
4871 (unsigned int)(fastload[i].length));
4872 if (retval == ERROR_OK)
4873 {
4874 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4875 }
4876 size += fastload[i].length;
4877 }
4878 int after = timeval_ms();
4879 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
4880 return retval;
4881 }
4882
4883 static int jim_mcrmrc(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4884 {
4885 struct command_context *context;
4886 struct target *target;
4887 int retval;
4888
4889 context = Jim_GetAssocData(interp, "context");
4890 if (context == NULL) {
4891 LOG_ERROR("array2mem: no command context");
4892 return JIM_ERR;
4893 }
4894 target = get_current_target(context);
4895 if (target == NULL) {
4896 LOG_ERROR("array2mem: no current target");
4897 return JIM_ERR;
4898 }
4899
4900 if ((argc < 6) || (argc > 7))
4901 {
4902 return JIM_ERR;
4903 }
4904
4905 int cpnum;
4906 uint32_t op1;
4907 uint32_t op2;
4908 uint32_t CRn;
4909 uint32_t CRm;
4910 uint32_t value;
4911
4912 int e;
4913 long l;
4914 e = Jim_GetLong(interp, argv[1], &l);
4915 if (e != JIM_OK) {
4916 return e;
4917 }
4918 cpnum = l;
4919
4920 e = Jim_GetLong(interp, argv[2], &l);
4921 if (e != JIM_OK) {
4922 return e;
4923 }
4924 op1 = l;
4925
4926 e = Jim_GetLong(interp, argv[3], &l);
4927 if (e != JIM_OK) {
4928 return e;
4929 }
4930 CRn = l;
4931
4932 e = Jim_GetLong(interp, argv[4], &l);
4933 if (e != JIM_OK) {
4934 return e;
4935 }
4936 CRm = l;
4937
4938 e = Jim_GetLong(interp, argv[5], &l);
4939 if (e != JIM_OK) {
4940 return e;
4941 }
4942 op2 = l;
4943
4944 value = 0;
4945
4946 if (argc == 7)
4947 {
4948 e = Jim_GetLong(interp, argv[6], &l);
4949 if (e != JIM_OK) {
4950 return e;
4951 }
4952 value = l;
4953
4954 retval = target_mcr(target, cpnum, op1, op2, CRn, CRm, value);
4955 if (retval != ERROR_OK)
4956 return JIM_ERR;
4957 } else
4958 {
4959 retval = target_mrc(target, cpnum, op1, op2, CRn, CRm, &value);
4960 if (retval != ERROR_OK)
4961 return JIM_ERR;
4962
4963 Jim_SetResult(interp, Jim_NewIntObj(interp, value));
4964 }
4965
4966 return JIM_OK;
4967 }
4968
4969 static const struct command_registration target_command_handlers[] = {
4970 {
4971 .name = "targets",
4972 .handler = &handle_targets_command,
4973 .mode = COMMAND_ANY,
4974 .help = "change current command line target (one parameter) "
4975 "or list targets (no parameters)",
4976 .usage = "[<new_current_target>]",
4977 },
4978 {
4979 .name = "target",
4980 .mode = COMMAND_CONFIG,
4981 .help = "configure target",
4982
4983 .chain = target_subcommand_handlers,
4984 },
4985 COMMAND_REGISTRATION_DONE
4986 };
4987
4988 int target_register_commands(struct command_context *cmd_ctx)
4989 {
4990 return register_commands(cmd_ctx, NULL, target_command_handlers);
4991 }
4992
4993 static const struct command_registration target_exec_command_handlers[] = {
4994 {
4995 .name = "fast_load_image",
4996 .handler = &handle_fast_load_image_command,
4997 .mode = COMMAND_ANY,
4998 .help = "Load image into memory, mainly for profiling purposes",
4999 .usage = "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
5000 "[min_address] [max_length]",
5001 },
5002 {
5003 .name = "fast_load",
5004 .handler = &handle_fast_load_command,
5005 .mode = COMMAND_ANY,
5006 .help = "loads active fast load image to current target "
5007 "- mainly for profiling purposes",
5008 },
5009 {
5010 .name = "profile",
5011 .handler = &handle_profile_command,
5012 .mode = COMMAND_EXEC,
5013 .help = "profiling samples the CPU PC",
5014 },
5015 /** @todo don't register virt2phys() unless target supports it */
5016 {
5017 .name = "virt2phys",
5018 .handler = &handle_virt2phys_command,
5019 .mode = COMMAND_ANY,
5020 .help = "translate a virtual address into a physical address",
5021 },
5022
5023 {
5024 .name = "reg",
5025 .handler = &handle_reg_command,
5026 .mode = COMMAND_EXEC,
5027 .help = "display or set a register",
5028 },
5029
5030 {
5031 .name = "poll",
5032 .handler = &handle_poll_command,
5033 .mode = COMMAND_EXEC,
5034 .help = "poll target state",
5035 },
5036 {
5037 .name = "wait_halt",
5038 .handler = &handle_wait_halt_command,
5039 .mode = COMMAND_EXEC,
5040 .help = "wait for target halt",
5041 .usage = "[time (s)]",
5042 },
5043 {
5044 .name = "halt",
5045 .handler = &handle_halt_command,
5046 .mode = COMMAND_EXEC,
5047 .help = "halt target",
5048 },
5049 {
5050 .name = "resume",
5051 .handler = &handle_resume_command,
5052 .mode = COMMAND_EXEC,
5053 .help = "resume target",
5054 .usage = "[<address>]",
5055 },
5056 {
5057 .name = "reset",
5058 .handler = &handle_reset_command,
5059 .mode = COMMAND_EXEC,
5060 .usage = "[run|halt|init]",
5061 .help = "Reset all targets into the specified mode."
5062 "Default reset mode is run, if not given.",
5063 },
5064 {
5065 .name = "soft_reset_halt",
5066 .handler = &handle_soft_reset_halt_command,
5067 .mode = COMMAND_EXEC,
5068 .help = "halt the target and do a soft reset",
5069 },
5070 {
5071
5072 .name = "step",
5073 .handler = &handle_step_command,
5074 .mode = COMMAND_EXEC,
5075 .help = "step one instruction from current PC or [addr]",
5076 .usage = "[<address>]",
5077 },
5078 {
5079
5080 .name = "mdw",
5081 .handler = &handle_md_command,
5082 .mode = COMMAND_EXEC,
5083 .help = "display memory words",
5084 .usage = "[phys] <addr> [count]",
5085 },
5086 {
5087 .name = "mdh",
5088 .handler = &handle_md_command,
5089 .mode = COMMAND_EXEC,
5090 .help = "display memory half-words",
5091 .usage = "[phys] <addr> [count]",
5092 },
5093 {
5094 .name = "mdb",
5095 .handler = &handle_md_command,
5096 .mode = COMMAND_EXEC,
5097 .help = "display memory bytes",
5098 .usage = "[phys] <addr> [count]",
5099 },
5100 {
5101
5102 .name = "mww",
5103 .handler = &handle_mw_command,
5104 .mode = COMMAND_EXEC,
5105 .help = "write memory word",
5106 .usage = "[phys] <addr> <value> [count]",
5107 },
5108 {
5109 .name = "mwh",
5110 .handler = &handle_mw_command,
5111 .mode = COMMAND_EXEC,
5112 .help = "write memory half-word",
5113 .usage = "[phys] <addr> <value> [count]",
5114 },
5115 {
5116 .name = "mwb",
5117 .handler = &handle_mw_command,
5118 .mode = COMMAND_EXEC,
5119 .help = "write memory byte",
5120 .usage = "[phys] <addr> <value> [count]",
5121 },
5122 {
5123
5124 .name = "bp",
5125 .handler = &handle_bp_command,
5126 .mode = COMMAND_EXEC,
5127 .help = "list or set breakpoint",
5128 .usage = "[<address> <length> [hw]]",
5129 },
5130 {
5131 .name = "rbp",
5132 .handler = &handle_rbp_command,
5133 .mode = COMMAND_EXEC,
5134 .help = "remove breakpoint",
5135 .usage = "<address>",
5136 },
5137 {
5138
5139 .name = "wp",
5140 .handler = &handle_wp_command,
5141 .mode = COMMAND_EXEC,
5142 .help = "list or set watchpoint",
5143 .usage = "[<address> <length> <r/w/a> [value] [mask]]",
5144 },
5145 {
5146 .name = "rwp",
5147 .handler = &handle_rwp_command,
5148 .mode = COMMAND_EXEC,
5149 .help = "remove watchpoint",
5150 .usage = "<address>",
5151
5152 },
5153 {
5154 .name = "load_image",
5155 .handler = &handle_load_image_command,
5156 .mode = COMMAND_EXEC,
5157 .usage = "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
5158 "[min_address] [max_length]",
5159 },
5160 {
5161 .name = "dump_image",
5162 .handler = &handle_dump_image_command,
5163 .mode = COMMAND_EXEC,
5164 .usage = "<file> <address> <size>",
5165 },
5166 {
5167 .name = "verify_image",
5168 .handler = &handle_verify_image_command,
5169 .mode = COMMAND_EXEC,
5170 .usage = "<file> [offset] [type]",
5171 },
5172 {
5173 .name = "test_image",
5174 .handler = &handle_test_image_command,
5175 .mode = COMMAND_EXEC,
5176 .usage = "<file> [offset] [type]",
5177 },
5178 {
5179 .name = "ocd_mem2array",
5180 .mode = COMMAND_EXEC,
5181 .jim_handler = &jim_mem2array,
5182 .help = "read memory and return as a TCL array "
5183 "for script processing",
5184 .usage = "<arrayname> <width=32|16|8> <address> <count>",
5185 },
5186 {
5187 .name = "ocd_array2mem",
5188 .mode = COMMAND_EXEC,
5189 .jim_handler = &jim_array2mem,
5190 .help = "convert a TCL array to memory locations "
5191 "and write the values",
5192 .usage = "<arrayname> <width=32|16|8> <address> <count>",
5193 },
5194 COMMAND_REGISTRATION_DONE
5195 };
5196 int target_register_user_commands(struct command_context *cmd_ctx)
5197 {
5198 int retval = ERROR_OK;
5199 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
5200 return retval;
5201
5202 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
5203 return retval;
5204
5205
5206 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);
5207 }

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)