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

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)