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

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)