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

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)