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

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)