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

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)