watchpoint_add() cleanup
[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 "
2992 "[(r|w|a) [value [mask]]]]");
2993 return ERROR_COMMAND_SYNTAX_ERROR;
2994 }
2995
2996 retval = watchpoint_add(target, addr, length, type,
2997 data_value, data_mask);
2998 if (ERROR_OK != retval)
2999 LOG_ERROR("Failure setting watchpoints");
3000
3001 return retval;
3002 }
3003
3004 static int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
3005 {
3006 if (argc != 1)
3007 return ERROR_COMMAND_SYNTAX_ERROR;
3008
3009 uint32_t addr;
3010 int retval = parse_u32(args[0], &addr);
3011 if (ERROR_OK != retval)
3012 return retval;
3013
3014 target_t *target = get_current_target(cmd_ctx);
3015 watchpoint_remove(target, addr);
3016
3017 return ERROR_OK;
3018 }
3019
3020
3021 /**
3022 * Translate a virtual address to a physical address.
3023 *
3024 * The low-level target implementation must have logged a detailed error
3025 * which is forwarded to telnet/GDB session.
3026 */
3027 static int handle_virt2phys_command(command_context_t *cmd_ctx,
3028 char *cmd, char **args, int argc)
3029 {
3030 if (argc != 1)
3031 return ERROR_COMMAND_SYNTAX_ERROR;
3032
3033 uint32_t va;
3034 int retval = parse_u32(args[0], &va);
3035 if (ERROR_OK != retval)
3036 return retval;
3037 uint32_t pa;
3038
3039 target_t *target = get_current_target(cmd_ctx);
3040 retval = target->type->virt2phys(target, va, &pa);
3041 if (retval == ERROR_OK)
3042 command_print(cmd_ctx, "Physical address 0x%08" PRIx32 "", pa);
3043
3044 return retval;
3045 }
3046
3047 static void writeData(FILE *f, const void *data, size_t len)
3048 {
3049 size_t written = fwrite(data, 1, len, f);
3050 if (written != len)
3051 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
3052 }
3053
3054 static void writeLong(FILE *f, int l)
3055 {
3056 int i;
3057 for (i = 0; i < 4; i++)
3058 {
3059 char c = (l >> (i*8))&0xff;
3060 writeData(f, &c, 1);
3061 }
3062
3063 }
3064
3065 static void writeString(FILE *f, char *s)
3066 {
3067 writeData(f, s, strlen(s));
3068 }
3069
3070 /* Dump a gmon.out histogram file. */
3071 static void writeGmon(uint32_t *samples, uint32_t sampleNum, char *filename)
3072 {
3073 uint32_t i;
3074 FILE *f = fopen(filename, "w");
3075 if (f == NULL)
3076 return;
3077 writeString(f, "gmon");
3078 writeLong(f, 0x00000001); /* Version */
3079 writeLong(f, 0); /* padding */
3080 writeLong(f, 0); /* padding */
3081 writeLong(f, 0); /* padding */
3082
3083 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
3084 writeData(f, &zero, 1);
3085
3086 /* figure out bucket size */
3087 uint32_t min = samples[0];
3088 uint32_t max = samples[0];
3089 for (i = 0; i < sampleNum; i++)
3090 {
3091 if (min > samples[i])
3092 {
3093 min = samples[i];
3094 }
3095 if (max < samples[i])
3096 {
3097 max = samples[i];
3098 }
3099 }
3100
3101 int addressSpace = (max-min + 1);
3102
3103 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
3104 uint32_t length = addressSpace;
3105 if (length > maxBuckets)
3106 {
3107 length = maxBuckets;
3108 }
3109 int *buckets = malloc(sizeof(int)*length);
3110 if (buckets == NULL)
3111 {
3112 fclose(f);
3113 return;
3114 }
3115 memset(buckets, 0, sizeof(int)*length);
3116 for (i = 0; i < sampleNum;i++)
3117 {
3118 uint32_t address = samples[i];
3119 long long a = address-min;
3120 long long b = length-1;
3121 long long c = addressSpace-1;
3122 int index = (a*b)/c; /* danger!!!! int32 overflows */
3123 buckets[index]++;
3124 }
3125
3126 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3127 writeLong(f, min); /* low_pc */
3128 writeLong(f, max); /* high_pc */
3129 writeLong(f, length); /* # of samples */
3130 writeLong(f, 64000000); /* 64MHz */
3131 writeString(f, "seconds");
3132 for (i = 0; i < (15-strlen("seconds")); i++)
3133 writeData(f, &zero, 1);
3134 writeString(f, "s");
3135
3136 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3137
3138 char *data = malloc(2*length);
3139 if (data != NULL)
3140 {
3141 for (i = 0; i < length;i++)
3142 {
3143 int val;
3144 val = buckets[i];
3145 if (val > 65535)
3146 {
3147 val = 65535;
3148 }
3149 data[i*2]=val&0xff;
3150 data[i*2 + 1]=(val >> 8)&0xff;
3151 }
3152 free(buckets);
3153 writeData(f, data, length * 2);
3154 free(data);
3155 } else
3156 {
3157 free(buckets);
3158 }
3159
3160 fclose(f);
3161 }
3162
3163 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
3164 static int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
3165 {
3166 target_t *target = get_current_target(cmd_ctx);
3167 struct timeval timeout, now;
3168
3169 gettimeofday(&timeout, NULL);
3170 if (argc != 2)
3171 {
3172 return ERROR_COMMAND_SYNTAX_ERROR;
3173 }
3174 unsigned offset;
3175 int retval = parse_uint(args[0], &offset);
3176 if (ERROR_OK != retval)
3177 return retval;
3178
3179 timeval_add_time(&timeout, offset, 0);
3180
3181 command_print(cmd_ctx, "Starting profiling. Halting and resuming the target as often as we can...");
3182
3183 static const int maxSample = 10000;
3184 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3185 if (samples == NULL)
3186 return ERROR_OK;
3187
3188 int numSamples = 0;
3189 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3190 reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1);
3191
3192 for (;;)
3193 {
3194 target_poll(target);
3195 if (target->state == TARGET_HALTED)
3196 {
3197 uint32_t t=*((uint32_t *)reg->value);
3198 samples[numSamples++]=t;
3199 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3200 target_poll(target);
3201 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3202 } else if (target->state == TARGET_RUNNING)
3203 {
3204 /* We want to quickly sample the PC. */
3205 if ((retval = target_halt(target)) != ERROR_OK)
3206 {
3207 free(samples);
3208 return retval;
3209 }
3210 } else
3211 {
3212 command_print(cmd_ctx, "Target not halted or running");
3213 retval = ERROR_OK;
3214 break;
3215 }
3216 if (retval != ERROR_OK)
3217 {
3218 break;
3219 }
3220
3221 gettimeofday(&now, NULL);
3222 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3223 {
3224 command_print(cmd_ctx, "Profiling completed. %d samples.", numSamples);
3225 if ((retval = target_poll(target)) != ERROR_OK)
3226 {
3227 free(samples);
3228 return retval;
3229 }
3230 if (target->state == TARGET_HALTED)
3231 {
3232 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3233 }
3234 if ((retval = target_poll(target)) != ERROR_OK)
3235 {
3236 free(samples);
3237 return retval;
3238 }
3239 writeGmon(samples, numSamples, args[1]);
3240 command_print(cmd_ctx, "Wrote %s", args[1]);
3241 break;
3242 }
3243 }
3244 free(samples);
3245
3246 return ERROR_OK;
3247 }
3248
3249 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3250 {
3251 char *namebuf;
3252 Jim_Obj *nameObjPtr, *valObjPtr;
3253 int result;
3254
3255 namebuf = alloc_printf("%s(%d)", varname, idx);
3256 if (!namebuf)
3257 return JIM_ERR;
3258
3259 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3260 valObjPtr = Jim_NewIntObj(interp, val);
3261 if (!nameObjPtr || !valObjPtr)
3262 {
3263 free(namebuf);
3264 return JIM_ERR;
3265 }
3266
3267 Jim_IncrRefCount(nameObjPtr);
3268 Jim_IncrRefCount(valObjPtr);
3269 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3270 Jim_DecrRefCount(interp, nameObjPtr);
3271 Jim_DecrRefCount(interp, valObjPtr);
3272 free(namebuf);
3273 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3274 return result;
3275 }
3276
3277 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3278 {
3279 command_context_t *context;
3280 target_t *target;
3281
3282 context = Jim_GetAssocData(interp, "context");
3283 if (context == NULL)
3284 {
3285 LOG_ERROR("mem2array: no command context");
3286 return JIM_ERR;
3287 }
3288 target = get_current_target(context);
3289 if (target == NULL)
3290 {
3291 LOG_ERROR("mem2array: no current target");
3292 return JIM_ERR;
3293 }
3294
3295 return target_mem2array(interp, target, argc-1, argv + 1);
3296 }
3297
3298 static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
3299 {
3300 long l;
3301 uint32_t width;
3302 int len;
3303 uint32_t addr;
3304 uint32_t count;
3305 uint32_t v;
3306 const char *varname;
3307 uint8_t buffer[4096];
3308 int n, e, retval;
3309 uint32_t i;
3310
3311 /* argv[1] = name of array to receive the data
3312 * argv[2] = desired width
3313 * argv[3] = memory address
3314 * argv[4] = count of times to read
3315 */
3316 if (argc != 4) {
3317 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3318 return JIM_ERR;
3319 }
3320 varname = Jim_GetString(argv[0], &len);
3321 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3322
3323 e = Jim_GetLong(interp, argv[1], &l);
3324 width = l;
3325 if (e != JIM_OK) {
3326 return e;
3327 }
3328
3329 e = Jim_GetLong(interp, argv[2], &l);
3330 addr = l;
3331 if (e != JIM_OK) {
3332 return e;
3333 }
3334 e = Jim_GetLong(interp, argv[3], &l);
3335 len = l;
3336 if (e != JIM_OK) {
3337 return e;
3338 }
3339 switch (width) {
3340 case 8:
3341 width = 1;
3342 break;
3343 case 16:
3344 width = 2;
3345 break;
3346 case 32:
3347 width = 4;
3348 break;
3349 default:
3350 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3351 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3352 return JIM_ERR;
3353 }
3354 if (len == 0) {
3355 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3356 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3357 return JIM_ERR;
3358 }
3359 if ((addr + (len * width)) < addr) {
3360 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3361 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3362 return JIM_ERR;
3363 }
3364 /* absurd transfer size? */
3365 if (len > 65536) {
3366 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3367 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3368 return JIM_ERR;
3369 }
3370
3371 if ((width == 1) ||
3372 ((width == 2) && ((addr & 1) == 0)) ||
3373 ((width == 4) && ((addr & 3) == 0))) {
3374 /* all is well */
3375 } else {
3376 char buf[100];
3377 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3378 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3379 addr,
3380 width);
3381 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3382 return JIM_ERR;
3383 }
3384
3385 /* Transfer loop */
3386
3387 /* index counter */
3388 n = 0;
3389 /* assume ok */
3390 e = JIM_OK;
3391 while (len) {
3392 /* Slurp... in buffer size chunks */
3393
3394 count = len; /* in objects.. */
3395 if (count > (sizeof(buffer)/width)) {
3396 count = (sizeof(buffer)/width);
3397 }
3398
3399 retval = target_read_memory(target, addr, width, count, buffer);
3400 if (retval != ERROR_OK) {
3401 /* BOO !*/
3402 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3403 (unsigned int)addr,
3404 (int)width,
3405 (int)count);
3406 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3407 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3408 e = JIM_ERR;
3409 len = 0;
3410 } else {
3411 v = 0; /* shut up gcc */
3412 for (i = 0 ;i < count ;i++, n++) {
3413 switch (width) {
3414 case 4:
3415 v = target_buffer_get_u32(target, &buffer[i*width]);
3416 break;
3417 case 2:
3418 v = target_buffer_get_u16(target, &buffer[i*width]);
3419 break;
3420 case 1:
3421 v = buffer[i] & 0x0ff;
3422 break;
3423 }
3424 new_int_array_element(interp, varname, n, v);
3425 }
3426 len -= count;
3427 }
3428 }
3429
3430 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3431
3432 return JIM_OK;
3433 }
3434
3435 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3436 {
3437 char *namebuf;
3438 Jim_Obj *nameObjPtr, *valObjPtr;
3439 int result;
3440 long l;
3441
3442 namebuf = alloc_printf("%s(%d)", varname, idx);
3443 if (!namebuf)
3444 return JIM_ERR;
3445
3446 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3447 if (!nameObjPtr)
3448 {
3449 free(namebuf);
3450 return JIM_ERR;
3451 }
3452
3453 Jim_IncrRefCount(nameObjPtr);
3454 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3455 Jim_DecrRefCount(interp, nameObjPtr);
3456 free(namebuf);
3457 if (valObjPtr == NULL)
3458 return JIM_ERR;
3459
3460 result = Jim_GetLong(interp, valObjPtr, &l);
3461 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3462 *val = l;
3463 return result;
3464 }
3465
3466 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3467 {
3468 command_context_t *context;
3469 target_t *target;
3470
3471 context = Jim_GetAssocData(interp, "context");
3472 if (context == NULL) {
3473 LOG_ERROR("array2mem: no command context");
3474 return JIM_ERR;
3475 }
3476 target = get_current_target(context);
3477 if (target == NULL) {
3478 LOG_ERROR("array2mem: no current target");
3479 return JIM_ERR;
3480 }
3481
3482 return target_array2mem(interp,target, argc-1, argv + 1);
3483 }
3484 static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
3485 {
3486 long l;
3487 uint32_t width;
3488 int len;
3489 uint32_t addr;
3490 uint32_t count;
3491 uint32_t v;
3492 const char *varname;
3493 uint8_t buffer[4096];
3494 int n, e, retval;
3495 uint32_t i;
3496
3497 /* argv[1] = name of array to get the data
3498 * argv[2] = desired width
3499 * argv[3] = memory address
3500 * argv[4] = count to write
3501 */
3502 if (argc != 4) {
3503 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3504 return JIM_ERR;
3505 }
3506 varname = Jim_GetString(argv[0], &len);
3507 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3508
3509 e = Jim_GetLong(interp, argv[1], &l);
3510 width = l;
3511 if (e != JIM_OK) {
3512 return e;
3513 }
3514
3515 e = Jim_GetLong(interp, argv[2], &l);
3516 addr = l;
3517 if (e != JIM_OK) {
3518 return e;
3519 }
3520 e = Jim_GetLong(interp, argv[3], &l);
3521 len = l;
3522 if (e != JIM_OK) {
3523 return e;
3524 }
3525 switch (width) {
3526 case 8:
3527 width = 1;
3528 break;
3529 case 16:
3530 width = 2;
3531 break;
3532 case 32:
3533 width = 4;
3534 break;
3535 default:
3536 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3537 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3538 return JIM_ERR;
3539 }
3540 if (len == 0) {
3541 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3542 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3543 return JIM_ERR;
3544 }
3545 if ((addr + (len * width)) < addr) {
3546 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3547 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3548 return JIM_ERR;
3549 }
3550 /* absurd transfer size? */
3551 if (len > 65536) {
3552 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3553 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3554 return JIM_ERR;
3555 }
3556
3557 if ((width == 1) ||
3558 ((width == 2) && ((addr & 1) == 0)) ||
3559 ((width == 4) && ((addr & 3) == 0))) {
3560 /* all is well */
3561 } else {
3562 char buf[100];
3563 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3564 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3565 (unsigned int)addr,
3566 (int)width);
3567 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3568 return JIM_ERR;
3569 }
3570
3571 /* Transfer loop */
3572
3573 /* index counter */
3574 n = 0;
3575 /* assume ok */
3576 e = JIM_OK;
3577 while (len) {
3578 /* Slurp... in buffer size chunks */
3579
3580 count = len; /* in objects.. */
3581 if (count > (sizeof(buffer)/width)) {
3582 count = (sizeof(buffer)/width);
3583 }
3584
3585 v = 0; /* shut up gcc */
3586 for (i = 0 ;i < count ;i++, n++) {
3587 get_int_array_element(interp, varname, n, &v);
3588 switch (width) {
3589 case 4:
3590 target_buffer_set_u32(target, &buffer[i*width], v);
3591 break;
3592 case 2:
3593 target_buffer_set_u16(target, &buffer[i*width], v);
3594 break;
3595 case 1:
3596 buffer[i] = v & 0x0ff;
3597 break;
3598 }
3599 }
3600 len -= count;
3601
3602 retval = target_write_memory(target, addr, width, count, buffer);
3603 if (retval != ERROR_OK) {
3604 /* BOO !*/
3605 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3606 (unsigned int)addr,
3607 (int)width,
3608 (int)count);
3609 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3610 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3611 e = JIM_ERR;
3612 len = 0;
3613 }
3614 }
3615
3616 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3617
3618 return JIM_OK;
3619 }
3620
3621 void target_all_handle_event(enum target_event e)
3622 {
3623 target_t *target;
3624
3625 LOG_DEBUG("**all*targets: event: %d, %s",
3626 (int)e,
3627 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3628
3629 target = all_targets;
3630 while (target) {
3631 target_handle_event(target, e);
3632 target = target->next;
3633 }
3634 }
3635
3636
3637 /* FIX? should we propagate errors here rather than printing them
3638 * and continuing?
3639 */
3640 void target_handle_event(target_t *target, enum target_event e)
3641 {
3642 target_event_action_t *teap;
3643
3644 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3645 if (teap->event == e) {
3646 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3647 target->target_number,
3648 target->cmd_name,
3649 target_get_name(target),
3650 e,
3651 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3652 Jim_GetString(teap->body, NULL));
3653 if (Jim_EvalObj(interp, teap->body) != JIM_OK)
3654 {
3655 Jim_PrintErrorMessage(interp);
3656 }
3657 }
3658 }
3659 }
3660
3661 enum target_cfg_param {
3662 TCFG_TYPE,
3663 TCFG_EVENT,
3664 TCFG_WORK_AREA_VIRT,
3665 TCFG_WORK_AREA_PHYS,
3666 TCFG_WORK_AREA_SIZE,
3667 TCFG_WORK_AREA_BACKUP,
3668 TCFG_ENDIAN,
3669 TCFG_VARIANT,
3670 TCFG_CHAIN_POSITION,
3671 };
3672
3673 static Jim_Nvp nvp_config_opts[] = {
3674 { .name = "-type", .value = TCFG_TYPE },
3675 { .name = "-event", .value = TCFG_EVENT },
3676 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3677 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3678 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3679 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3680 { .name = "-endian" , .value = TCFG_ENDIAN },
3681 { .name = "-variant", .value = TCFG_VARIANT },
3682 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3683
3684 { .name = NULL, .value = -1 }
3685 };
3686
3687 static int target_configure(Jim_GetOptInfo *goi, target_t *target)
3688 {
3689 Jim_Nvp *n;
3690 Jim_Obj *o;
3691 jim_wide w;
3692 char *cp;
3693 int e;
3694
3695 /* parse config or cget options ... */
3696 while (goi->argc > 0) {
3697 Jim_SetEmptyResult(goi->interp);
3698 /* Jim_GetOpt_Debug(goi); */
3699
3700 if (target->type->target_jim_configure) {
3701 /* target defines a configure function */
3702 /* target gets first dibs on parameters */
3703 e = (*(target->type->target_jim_configure))(target, goi);
3704 if (e == JIM_OK) {
3705 /* more? */
3706 continue;
3707 }
3708 if (e == JIM_ERR) {
3709 /* An error */
3710 return e;
3711 }
3712 /* otherwise we 'continue' below */
3713 }
3714 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3715 if (e != JIM_OK) {
3716 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3717 return e;
3718 }
3719 switch (n->value) {
3720 case TCFG_TYPE:
3721 /* not setable */
3722 if (goi->isconfigure) {
3723 Jim_SetResult_sprintf(goi->interp, "not setable: %s", n->name);
3724 return JIM_ERR;
3725 } else {
3726 no_params:
3727 if (goi->argc != 0) {
3728 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "NO PARAMS");
3729 return JIM_ERR;
3730 }
3731 }
3732 Jim_SetResultString(goi->interp, target_get_name(target), -1);
3733 /* loop for more */
3734 break;
3735 case TCFG_EVENT:
3736 if (goi->argc == 0) {
3737 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3738 return JIM_ERR;
3739 }
3740
3741 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3742 if (e != JIM_OK) {
3743 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3744 return e;
3745 }
3746
3747 if (goi->isconfigure) {
3748 if (goi->argc != 1) {
3749 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3750 return JIM_ERR;
3751 }
3752 } else {
3753 if (goi->argc != 0) {
3754 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3755 return JIM_ERR;
3756 }
3757 }
3758
3759 {
3760 target_event_action_t *teap;
3761
3762 teap = target->event_action;
3763 /* replace existing? */
3764 while (teap) {
3765 if (teap->event == (enum target_event)n->value) {
3766 break;
3767 }
3768 teap = teap->next;
3769 }
3770
3771 if (goi->isconfigure) {
3772 bool replace = true;
3773 if (teap == NULL) {
3774 /* create new */
3775 teap = calloc(1, sizeof(*teap));
3776 replace = false;
3777 }
3778 teap->event = n->value;
3779 Jim_GetOpt_Obj(goi, &o);
3780 if (teap->body) {
3781 Jim_DecrRefCount(interp, teap->body);
3782 }
3783 teap->body = Jim_DuplicateObj(goi->interp, o);
3784 /*
3785 * FIXME:
3786 * Tcl/TK - "tk events" have a nice feature.
3787 * See the "BIND" command.
3788 * We should support that here.
3789 * You can specify %X and %Y in the event code.
3790 * The idea is: %T - target name.
3791 * The idea is: %N - target number
3792 * The idea is: %E - event name.
3793 */
3794 Jim_IncrRefCount(teap->body);
3795
3796 if (!replace)
3797 {
3798 /* add to head of event list */
3799 teap->next = target->event_action;
3800 target->event_action = teap;
3801 }
3802 Jim_SetEmptyResult(goi->interp);
3803 } else {
3804 /* get */
3805 if (teap == NULL) {
3806 Jim_SetEmptyResult(goi->interp);
3807 } else {
3808 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3809 }
3810 }
3811 }
3812 /* loop for more */
3813 break;
3814
3815 case TCFG_WORK_AREA_VIRT:
3816 if (goi->isconfigure) {
3817 target_free_all_working_areas(target);
3818 e = Jim_GetOpt_Wide(goi, &w);
3819 if (e != JIM_OK) {
3820 return e;
3821 }
3822 target->working_area_virt = w;
3823 target->working_area_virt_spec = true;
3824 } else {
3825 if (goi->argc != 0) {
3826 goto no_params;
3827 }
3828 }
3829 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3830 /* loop for more */
3831 break;
3832
3833 case TCFG_WORK_AREA_PHYS:
3834 if (goi->isconfigure) {
3835 target_free_all_working_areas(target);
3836 e = Jim_GetOpt_Wide(goi, &w);
3837 if (e != JIM_OK) {
3838 return e;
3839 }
3840 target->working_area_phys = w;
3841 target->working_area_phys_spec = true;
3842 } else {
3843 if (goi->argc != 0) {
3844 goto no_params;
3845 }
3846 }
3847 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3848 /* loop for more */
3849 break;
3850
3851 case TCFG_WORK_AREA_SIZE:
3852 if (goi->isconfigure) {
3853 target_free_all_working_areas(target);
3854 e = Jim_GetOpt_Wide(goi, &w);
3855 if (e != JIM_OK) {
3856 return e;
3857 }
3858 target->working_area_size = w;
3859 } else {
3860 if (goi->argc != 0) {
3861 goto no_params;
3862 }
3863 }
3864 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3865 /* loop for more */
3866 break;
3867
3868 case TCFG_WORK_AREA_BACKUP:
3869 if (goi->isconfigure) {
3870 target_free_all_working_areas(target);
3871 e = Jim_GetOpt_Wide(goi, &w);
3872 if (e != JIM_OK) {
3873 return e;
3874 }
3875 /* make this exactly 1 or 0 */
3876 target->backup_working_area = (!!w);
3877 } else {
3878 if (goi->argc != 0) {
3879 goto no_params;
3880 }
3881 }
3882 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3883 /* loop for more e*/
3884 break;
3885
3886 case TCFG_ENDIAN:
3887 if (goi->isconfigure) {
3888 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3889 if (e != JIM_OK) {
3890 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3891 return e;
3892 }
3893 target->endianness = n->value;
3894 } else {
3895 if (goi->argc != 0) {
3896 goto no_params;
3897 }
3898 }
3899 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3900 if (n->name == NULL) {
3901 target->endianness = TARGET_LITTLE_ENDIAN;
3902 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3903 }
3904 Jim_SetResultString(goi->interp, n->name, -1);
3905 /* loop for more */
3906 break;
3907
3908 case TCFG_VARIANT:
3909 if (goi->isconfigure) {
3910 if (goi->argc < 1) {
3911 Jim_SetResult_sprintf(goi->interp,
3912 "%s ?STRING?",
3913 n->name);
3914 return JIM_ERR;
3915 }
3916 if (target->variant) {
3917 free((void *)(target->variant));
3918 }
3919 e = Jim_GetOpt_String(goi, &cp, NULL);
3920 target->variant = strdup(cp);
3921 } else {
3922 if (goi->argc != 0) {
3923 goto no_params;
3924 }
3925 }
3926 Jim_SetResultString(goi->interp, target->variant,-1);
3927 /* loop for more */
3928 break;
3929 case TCFG_CHAIN_POSITION:
3930 if (goi->isconfigure) {
3931 Jim_Obj *o;
3932 jtag_tap_t *tap;
3933 target_free_all_working_areas(target);
3934 e = Jim_GetOpt_Obj(goi, &o);
3935 if (e != JIM_OK) {
3936 return e;
3937 }
3938 tap = jtag_tap_by_jim_obj(goi->interp, o);
3939 if (tap == NULL) {
3940 return JIM_ERR;
3941 }
3942 /* make this exactly 1 or 0 */
3943 target->tap = tap;
3944 } else {
3945 if (goi->argc != 0) {
3946 goto no_params;
3947 }
3948 }
3949 Jim_SetResultString(interp, target->tap->dotted_name, -1);
3950 /* loop for more e*/
3951 break;
3952 }
3953 } /* while (goi->argc) */
3954
3955
3956 /* done - we return */
3957 return JIM_OK;
3958 }
3959
3960 /** this is the 'tcl' handler for the target specific command */
3961 static int tcl_target_func(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3962 {
3963 Jim_GetOptInfo goi;
3964 jim_wide a,b,c;
3965 int x,y,z;
3966 uint8_t target_buf[32];
3967 Jim_Nvp *n;
3968 target_t *target;
3969 struct command_context_s *cmd_ctx;
3970 int e;
3971
3972 enum {
3973 TS_CMD_CONFIGURE,
3974 TS_CMD_CGET,
3975
3976 TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
3977 TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
3978 TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
3979 TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
3980 TS_CMD_EXAMINE,
3981 TS_CMD_POLL,
3982 TS_CMD_RESET,
3983 TS_CMD_HALT,
3984 TS_CMD_WAITSTATE,
3985 TS_CMD_EVENTLIST,
3986 TS_CMD_CURSTATE,
3987 TS_CMD_INVOKE_EVENT,
3988 };
3989
3990 static const Jim_Nvp target_options[] = {
3991 { .name = "configure", .value = TS_CMD_CONFIGURE },
3992 { .name = "cget", .value = TS_CMD_CGET },
3993 { .name = "mww", .value = TS_CMD_MWW },
3994 { .name = "mwh", .value = TS_CMD_MWH },
3995 { .name = "mwb", .value = TS_CMD_MWB },
3996 { .name = "mdw", .value = TS_CMD_MDW },
3997 { .name = "mdh", .value = TS_CMD_MDH },
3998 { .name = "mdb", .value = TS_CMD_MDB },
3999 { .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
4000 { .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
4001 { .name = "eventlist", .value = TS_CMD_EVENTLIST },
4002 { .name = "curstate", .value = TS_CMD_CURSTATE },
4003
4004 { .name = "arp_examine", .value = TS_CMD_EXAMINE },
4005 { .name = "arp_poll", .value = TS_CMD_POLL },
4006 { .name = "arp_reset", .value = TS_CMD_RESET },
4007 { .name = "arp_halt", .value = TS_CMD_HALT },
4008 { .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
4009 { .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
4010
4011 { .name = NULL, .value = -1 },
4012 };
4013
4014 /* go past the "command" */
4015 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
4016
4017 target = Jim_CmdPrivData(goi.interp);
4018 cmd_ctx = Jim_GetAssocData(goi.interp, "context");
4019
4020 /* commands here are in an NVP table */
4021 e = Jim_GetOpt_Nvp(&goi, target_options, &n);
4022 if (e != JIM_OK) {
4023 Jim_GetOpt_NvpUnknown(&goi, target_options, 0);
4024 return e;
4025 }
4026 /* Assume blank result */
4027 Jim_SetEmptyResult(goi.interp);
4028
4029 switch (n->value) {
4030 case TS_CMD_CONFIGURE:
4031 if (goi.argc < 2) {
4032 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
4033 return JIM_ERR;
4034 }
4035 goi.isconfigure = 1;
4036 return target_configure(&goi, target);
4037 case TS_CMD_CGET:
4038 // some things take params
4039 if (goi.argc < 1) {
4040 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "missing: ?-option?");
4041 return JIM_ERR;
4042 }
4043 goi.isconfigure = 0;
4044 return target_configure(&goi, target);
4045 break;
4046 case TS_CMD_MWW:
4047 case TS_CMD_MWH:
4048 case TS_CMD_MWB:
4049 /* argv[0] = cmd
4050 * argv[1] = address
4051 * argv[2] = data
4052 * argv[3] = optional count.
4053 */
4054
4055 if ((goi.argc == 2) || (goi.argc == 3)) {
4056 /* all is well */
4057 } else {
4058 mwx_error:
4059 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR DATA [COUNT]", n->name);
4060 return JIM_ERR;
4061 }
4062
4063 e = Jim_GetOpt_Wide(&goi, &a);
4064 if (e != JIM_OK) {
4065 goto mwx_error;
4066 }
4067
4068 e = Jim_GetOpt_Wide(&goi, &b);
4069 if (e != JIM_OK) {
4070 goto mwx_error;
4071 }
4072 if (goi.argc == 3) {
4073 e = Jim_GetOpt_Wide(&goi, &c);
4074 if (e != JIM_OK) {
4075 goto mwx_error;
4076 }
4077 } else {
4078 c = 1;
4079 }
4080
4081 switch (n->value) {
4082 case TS_CMD_MWW:
4083 target_buffer_set_u32(target, target_buf, b);
4084 b = 4;
4085 break;
4086 case TS_CMD_MWH:
4087 target_buffer_set_u16(target, target_buf, b);
4088 b = 2;
4089 break;
4090 case TS_CMD_MWB:
4091 target_buffer_set_u8(target, target_buf, b);
4092 b = 1;
4093 break;
4094 }
4095 for (x = 0 ; x < c ; x++) {
4096 e = target_write_memory(target, a, b, 1, target_buf);
4097 if (e != ERROR_OK) {
4098 Jim_SetResult_sprintf(interp, "Error writing @ 0x%08x: %d\n", (int)(a), e);
4099 return JIM_ERR;
4100 }
4101 /* b = width */
4102 a = a + b;
4103 }
4104 return JIM_OK;
4105 break;
4106
4107 /* display */
4108 case TS_CMD_MDW:
4109 case TS_CMD_MDH:
4110 case TS_CMD_MDB:
4111 /* argv[0] = command
4112 * argv[1] = address
4113 * argv[2] = optional count
4114 */
4115 if ((goi.argc == 2) || (goi.argc == 3)) {
4116 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR [COUNT]", n->name);
4117 return JIM_ERR;
4118 }
4119 e = Jim_GetOpt_Wide(&goi, &a);
4120 if (e != JIM_OK) {
4121 return JIM_ERR;
4122 }
4123 if (goi.argc) {
4124 e = Jim_GetOpt_Wide(&goi, &c);
4125 if (e != JIM_OK) {
4126 return JIM_ERR;
4127 }
4128 } else {
4129 c = 1;
4130 }
4131 b = 1; /* shut up gcc */
4132 switch (n->value) {
4133 case TS_CMD_MDW:
4134 b = 4;
4135 break;
4136 case TS_CMD_MDH:
4137 b = 2;
4138 break;
4139 case TS_CMD_MDB:
4140 b = 1;
4141 break;
4142 }
4143
4144 /* convert to "bytes" */
4145 c = c * b;
4146 /* count is now in 'BYTES' */
4147 while (c > 0) {
4148 y = c;
4149 if (y > 16) {
4150 y = 16;
4151 }
4152 e = target_read_memory(target, a, b, y / b, target_buf);
4153 if (e != ERROR_OK) {
4154 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
4155 return JIM_ERR;
4156 }
4157
4158 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
4159 switch (b) {
4160 case 4:
4161 for (x = 0 ; (x < 16) && (x < y) ; x += 4) {
4162 z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
4163 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
4164 }
4165 for (; (x < 16) ; x += 4) {
4166 Jim_fprintf(interp, interp->cookie_stdout, " ");
4167 }
4168 break;
4169 case 2:
4170 for (x = 0 ; (x < 16) && (x < y) ; x += 2) {
4171 z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
4172 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
4173 }
4174 for (; (x < 16) ; x += 2) {
4175 Jim_fprintf(interp, interp->cookie_stdout, " ");
4176 }
4177 break;
4178 case 1:
4179 default:
4180 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4181 z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
4182 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
4183 }
4184 for (; (x < 16) ; x += 1) {
4185 Jim_fprintf(interp, interp->cookie_stdout, " ");
4186 }
4187 break;
4188 }
4189 /* ascii-ify the bytes */
4190 for (x = 0 ; x < y ; x++) {
4191 if ((target_buf[x] >= 0x20) &&
4192 (target_buf[x] <= 0x7e)) {
4193 /* good */
4194 } else {
4195 /* smack it */
4196 target_buf[x] = '.';
4197 }
4198 }
4199 /* space pad */
4200 while (x < 16) {
4201 target_buf[x] = ' ';
4202 x++;
4203 }
4204 /* terminate */
4205 target_buf[16] = 0;
4206 /* print - with a newline */
4207 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
4208 /* NEXT... */
4209 c -= 16;
4210 a += 16;
4211 }
4212 return JIM_OK;
4213 case TS_CMD_MEM2ARRAY:
4214 return target_mem2array(goi.interp, target, goi.argc, goi.argv);
4215 break;
4216 case TS_CMD_ARRAY2MEM:
4217 return target_array2mem(goi.interp, target, goi.argc, goi.argv);
4218 break;
4219 case TS_CMD_EXAMINE:
4220 if (goi.argc) {
4221 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4222 return JIM_ERR;
4223 }
4224 if (!target->tap->enabled)
4225 goto err_tap_disabled;
4226 e = target->type->examine(target);
4227 if (e != ERROR_OK) {
4228 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
4229 return JIM_ERR;
4230 }
4231 return JIM_OK;
4232 case TS_CMD_POLL:
4233 if (goi.argc) {
4234 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4235 return JIM_ERR;
4236 }
4237 if (!target->tap->enabled)
4238 goto err_tap_disabled;
4239 if (!(target_was_examined(target))) {
4240 e = ERROR_TARGET_NOT_EXAMINED;
4241 } else {
4242 e = target->type->poll(target);
4243 }
4244 if (e != ERROR_OK) {
4245 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4246 return JIM_ERR;
4247 } else {
4248 return JIM_OK;
4249 }
4250 break;
4251 case TS_CMD_RESET:
4252 if (goi.argc != 2) {
4253 Jim_WrongNumArgs(interp, 2, argv,
4254 "([tT]|[fF]|assert|deassert) BOOL");
4255 return JIM_ERR;
4256 }
4257 e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4258 if (e != JIM_OK) {
4259 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4260 return e;
4261 }
4262 /* the halt or not param */
4263 e = Jim_GetOpt_Wide(&goi, &a);
4264 if (e != JIM_OK) {
4265 return e;
4266 }
4267 if (!target->tap->enabled)
4268 goto err_tap_disabled;
4269 if (!target->type->assert_reset
4270 || !target->type->deassert_reset) {
4271 Jim_SetResult_sprintf(interp,
4272 "No target-specific reset for %s",
4273 target->cmd_name);
4274 return JIM_ERR;
4275 }
4276 /* determine if we should halt or not. */
4277 target->reset_halt = !!a;
4278 /* When this happens - all workareas are invalid. */
4279 target_free_all_working_areas_restore(target, 0);
4280
4281 /* do the assert */
4282 if (n->value == NVP_ASSERT) {
4283 e = target->type->assert_reset(target);
4284 } else {
4285 e = target->type->deassert_reset(target);
4286 }
4287 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4288 case TS_CMD_HALT:
4289 if (goi.argc) {
4290 Jim_WrongNumArgs(goi.interp, 0, argv, "halt [no parameters]");
4291 return JIM_ERR;
4292 }
4293 if (!target->tap->enabled)
4294 goto err_tap_disabled;
4295 e = target->type->halt(target);
4296 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4297 case TS_CMD_WAITSTATE:
4298 /* params: <name> statename timeoutmsecs */
4299 if (goi.argc != 2) {
4300 Jim_SetResult_sprintf(goi.interp, "%s STATENAME TIMEOUTMSECS", n->name);
4301 return JIM_ERR;
4302 }
4303 e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4304 if (e != JIM_OK) {
4305 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4306 return e;
4307 }
4308 e = Jim_GetOpt_Wide(&goi, &a);
4309 if (e != JIM_OK) {
4310 return e;
4311 }
4312 if (!target->tap->enabled)
4313 goto err_tap_disabled;
4314 e = target_wait_state(target, n->value, a);
4315 if (e != ERROR_OK) {
4316 Jim_SetResult_sprintf(goi.interp,
4317 "target: %s wait %s fails (%d) %s",
4318 target->cmd_name,
4319 n->name,
4320 e, target_strerror_safe(e));
4321 return JIM_ERR;
4322 } else {
4323 return JIM_OK;
4324 }
4325 case TS_CMD_EVENTLIST:
4326 /* List for human, Events defined for this target.
4327 * scripts/programs should use 'name cget -event NAME'
4328 */
4329 {
4330 target_event_action_t *teap;
4331 teap = target->event_action;
4332 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4333 target->target_number,
4334 target->cmd_name);
4335 command_print(cmd_ctx, "%-25s | Body", "Event");
4336 command_print(cmd_ctx, "------------------------- | ----------------------------------------");
4337 while (teap) {
4338 command_print(cmd_ctx,
4339 "%-25s | %s",
4340 Jim_Nvp_value2name_simple(nvp_target_event, teap->event)->name,
4341 Jim_GetString(teap->body, NULL));
4342 teap = teap->next;
4343 }
4344 command_print(cmd_ctx, "***END***");
4345 return JIM_OK;
4346 }
4347 case TS_CMD_CURSTATE:
4348 if (goi.argc != 0) {
4349 Jim_WrongNumArgs(goi.interp, 0, argv, "[no parameters]");
4350 return JIM_ERR;
4351 }
4352 Jim_SetResultString(goi.interp,
4353 target_state_name( target ),
4354 -1);
4355 return JIM_OK;
4356 case TS_CMD_INVOKE_EVENT:
4357 if (goi.argc != 1) {
4358 Jim_SetResult_sprintf(goi.interp, "%s ?EVENTNAME?",n->name);
4359 return JIM_ERR;
4360 }
4361 e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4362 if (e != JIM_OK) {
4363 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4364 return e;
4365 }
4366 target_handle_event(target, n->value);
4367 return JIM_OK;
4368 }
4369 return JIM_ERR;
4370
4371 err_tap_disabled:
4372 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4373 return JIM_ERR;
4374 }
4375
4376 static int target_create(Jim_GetOptInfo *goi)
4377 {
4378 Jim_Obj *new_cmd;
4379 Jim_Cmd *cmd;
4380 const char *cp;
4381 char *cp2;
4382 int e;
4383 int x;
4384 target_t *target;
4385 struct command_context_s *cmd_ctx;
4386
4387 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4388 if (goi->argc < 3) {
4389 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4390 return JIM_ERR;
4391 }
4392
4393 /* COMMAND */
4394 Jim_GetOpt_Obj(goi, &new_cmd);
4395 /* does this command exist? */
4396 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4397 if (cmd) {
4398 cp = Jim_GetString(new_cmd, NULL);
4399 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4400 return JIM_ERR;
4401 }
4402
4403 /* TYPE */
4404 e = Jim_GetOpt_String(goi, &cp2, NULL);
4405 cp = cp2;
4406 /* now does target type exist */
4407 for (x = 0 ; target_types[x] ; x++) {
4408 if (0 == strcmp(cp, target_types[x]->name)) {
4409 /* found */
4410 break;
4411 }
4412 }
4413 if (target_types[x] == NULL) {
4414 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4415 for (x = 0 ; target_types[x] ; x++) {
4416 if (target_types[x + 1]) {
4417 Jim_AppendStrings(goi->interp,
4418 Jim_GetResult(goi->interp),
4419 target_types[x]->name,
4420 ", ", NULL);
4421 } else {
4422 Jim_AppendStrings(goi->interp,
4423 Jim_GetResult(goi->interp),
4424 " or ",
4425 target_types[x]->name,NULL);
4426 }
4427 }
4428 return JIM_ERR;
4429 }
4430
4431 /* Create it */
4432 target = calloc(1,sizeof(target_t));
4433 /* set target number */
4434 target->target_number = new_target_number();
4435
4436 /* allocate memory for each unique target type */
4437 target->type = (target_type_t*)calloc(1,sizeof(target_type_t));
4438
4439 memcpy(target->type, target_types[x], sizeof(target_type_t));
4440
4441 /* will be set by "-endian" */
4442 target->endianness = TARGET_ENDIAN_UNKNOWN;
4443
4444 target->working_area = 0x0;
4445 target->working_area_size = 0x0;
4446 target->working_areas = NULL;
4447 target->backup_working_area = 0;
4448
4449 target->state = TARGET_UNKNOWN;
4450 target->debug_reason = DBG_REASON_UNDEFINED;
4451 target->reg_cache = NULL;
4452 target->breakpoints = NULL;
4453 target->watchpoints = NULL;
4454 target->next = NULL;
4455 target->arch_info = NULL;
4456
4457 target->display = 1;
4458
4459 target->halt_issued = false;
4460
4461 /* initialize trace information */
4462 target->trace_info = malloc(sizeof(trace_t));
4463 target->trace_info->num_trace_points = 0;
4464 target->trace_info->trace_points_size = 0;
4465 target->trace_info->trace_points = NULL;
4466 target->trace_info->trace_history_size = 0;
4467 target->trace_info->trace_history = NULL;
4468 target->trace_info->trace_history_pos = 0;
4469 target->trace_info->trace_history_overflowed = 0;
4470
4471 target->dbgmsg = NULL;
4472 target->dbg_msg_enabled = 0;
4473
4474 target->endianness = TARGET_ENDIAN_UNKNOWN;
4475
4476 /* Do the rest as "configure" options */
4477 goi->isconfigure = 1;
4478 e = target_configure(goi, target);
4479
4480 if (target->tap == NULL)
4481 {
4482 Jim_SetResultString(interp, "-chain-position required when creating target", -1);
4483 e = JIM_ERR;
4484 }
4485
4486 if (e != JIM_OK) {
4487 free(target->type);
4488 free(target);
4489 return e;
4490 }
4491
4492 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4493 /* default endian to little if not specified */
4494 target->endianness = TARGET_LITTLE_ENDIAN;
4495 }
4496
4497 /* incase variant is not set */
4498 if (!target->variant)
4499 target->variant = strdup("");
4500
4501 /* create the target specific commands */
4502 if (target->type->register_commands) {
4503 (*(target->type->register_commands))(cmd_ctx);
4504 }
4505 if (target->type->target_create) {
4506 (*(target->type->target_create))(target, goi->interp);
4507 }
4508
4509 /* append to end of list */
4510 {
4511 target_t **tpp;
4512 tpp = &(all_targets);
4513 while (*tpp) {
4514 tpp = &((*tpp)->next);
4515 }
4516 *tpp = target;
4517 }
4518
4519 cp = Jim_GetString(new_cmd, NULL);
4520 target->cmd_name = strdup(cp);
4521
4522 /* now - create the new target name command */
4523 e = Jim_CreateCommand(goi->interp,
4524 /* name */
4525 cp,
4526 tcl_target_func, /* C function */
4527 target, /* private data */
4528 NULL); /* no del proc */
4529
4530 return e;
4531 }
4532
4533 static int jim_target(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4534 {
4535 int x,r,e;
4536 jim_wide w;
4537 struct command_context_s *cmd_ctx;
4538 target_t *target;
4539 Jim_GetOptInfo goi;
4540 enum tcmd {
4541 /* TG = target generic */
4542 TG_CMD_CREATE,
4543 TG_CMD_TYPES,
4544 TG_CMD_NAMES,
4545 TG_CMD_CURRENT,
4546 TG_CMD_NUMBER,
4547 TG_CMD_COUNT,
4548 };
4549 const char *target_cmds[] = {
4550 "create", "types", "names", "current", "number",
4551 "count",
4552 NULL /* terminate */
4553 };
4554
4555 LOG_DEBUG("Target command params:");
4556 LOG_DEBUG("%s", Jim_Debug_ArgvString(interp, argc, argv));
4557
4558 cmd_ctx = Jim_GetAssocData(interp, "context");
4559
4560 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
4561
4562 if (goi.argc == 0) {
4563 Jim_WrongNumArgs(interp, 1, argv, "missing: command ...");
4564 return JIM_ERR;
4565 }
4566
4567 /* Jim_GetOpt_Debug(&goi); */
4568 r = Jim_GetOpt_Enum(&goi, target_cmds, &x);
4569 if (r != JIM_OK) {
4570 return r;
4571 }
4572
4573 switch (x) {
4574 default:
4575 Jim_Panic(goi.interp,"Why am I here?");
4576 return JIM_ERR;
4577 case TG_CMD_CURRENT:
4578 if (goi.argc != 0) {
4579 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4580 return JIM_ERR;
4581 }
4582 Jim_SetResultString(goi.interp, get_current_target(cmd_ctx)->cmd_name, -1);
4583 return JIM_OK;
4584 case TG_CMD_TYPES:
4585 if (goi.argc != 0) {
4586 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4587 return JIM_ERR;
4588 }
4589 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4590 for (x = 0 ; target_types[x] ; x++) {
4591 Jim_ListAppendElement(goi.interp,
4592 Jim_GetResult(goi.interp),
4593 Jim_NewStringObj(goi.interp, target_types[x]->name, -1));
4594 }
4595 return JIM_OK;
4596 case TG_CMD_NAMES:
4597 if (goi.argc != 0) {
4598 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4599 return JIM_ERR;
4600 }
4601 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4602 target = all_targets;
4603 while (target) {
4604 Jim_ListAppendElement(goi.interp,
4605 Jim_GetResult(goi.interp),
4606 Jim_NewStringObj(goi.interp, target->cmd_name, -1));
4607 target = target->next;
4608 }
4609 return JIM_OK;
4610 case TG_CMD_CREATE:
4611 if (goi.argc < 3) {
4612 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "?name ... config options ...");
4613 return JIM_ERR;
4614 }
4615 return target_create(&goi);
4616 break;
4617 case TG_CMD_NUMBER:
4618 /* It's OK to remove this mechanism sometime after August 2010 or so */
4619 LOG_WARNING("don't use numbers as target identifiers; use names");
4620 if (goi.argc != 1) {
4621 Jim_SetResult_sprintf(goi.interp, "expected: target number ?NUMBER?");
4622 return JIM_ERR;
4623 }
4624 e = Jim_GetOpt_Wide(&goi, &w);
4625 if (e != JIM_OK) {
4626 return JIM_ERR;
4627 }
4628 for (x = 0, target = all_targets; target; target = target->next, x++) {
4629 if (target->target_number == w)
4630 break;
4631 }
4632 if (target == NULL) {
4633 Jim_SetResult_sprintf(goi.interp,
4634 "Target: number %d does not exist", (int)(w));
4635 return JIM_ERR;
4636 }
4637 Jim_SetResultString(goi.interp, target->cmd_name, -1);
4638 return JIM_OK;
4639 case TG_CMD_COUNT:
4640 if (goi.argc != 0) {
4641 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "<no parameters>");
4642 return JIM_ERR;
4643 }
4644 for (x = 0, target = all_targets; target; target = target->next, x++)
4645 continue;
4646 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, x));
4647 return JIM_OK;
4648 }
4649
4650 return JIM_ERR;
4651 }
4652
4653
4654 struct FastLoad
4655 {
4656 uint32_t address;
4657 uint8_t *data;
4658 int length;
4659
4660 };
4661
4662 static int fastload_num;
4663 static struct FastLoad *fastload;
4664
4665 static void free_fastload(void)
4666 {
4667 if (fastload != NULL)
4668 {
4669 int i;
4670 for (i = 0; i < fastload_num; i++)
4671 {
4672 if (fastload[i].data)
4673 free(fastload[i].data);
4674 }
4675 free(fastload);
4676 fastload = NULL;
4677 }
4678 }
4679
4680
4681
4682
4683 static int handle_fast_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4684 {
4685 uint8_t *buffer;
4686 uint32_t buf_cnt;
4687 uint32_t image_size;
4688 uint32_t min_address = 0;
4689 uint32_t max_address = 0xffffffff;
4690 int i;
4691
4692 image_t image;
4693
4694 duration_t duration;
4695 char *duration_text;
4696
4697 int retval = parse_load_image_command_args(args, argc,
4698 &image, &min_address, &max_address);
4699 if (ERROR_OK != retval)
4700 return retval;
4701
4702 duration_start_measure(&duration);
4703
4704 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
4705 {
4706 return ERROR_OK;
4707 }
4708
4709 image_size = 0x0;
4710 retval = ERROR_OK;
4711 fastload_num = image.num_sections;
4712 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4713 if (fastload == NULL)
4714 {
4715 image_close(&image);
4716 return ERROR_FAIL;
4717 }
4718 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4719 for (i = 0; i < image.num_sections; i++)
4720 {
4721 buffer = malloc(image.sections[i].size);
4722 if (buffer == NULL)
4723 {
4724 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)",
4725 (int)(image.sections[i].size));
4726 break;
4727 }
4728
4729 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4730 {
4731 free(buffer);
4732 break;
4733 }
4734
4735 uint32_t offset = 0;
4736 uint32_t length = buf_cnt;
4737
4738
4739 /* DANGER!!! beware of unsigned comparision here!!! */
4740
4741 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4742 (image.sections[i].base_address < max_address))
4743 {
4744 if (image.sections[i].base_address < min_address)
4745 {
4746 /* clip addresses below */
4747 offset += min_address-image.sections[i].base_address;
4748 length -= offset;
4749 }
4750
4751 if (image.sections[i].base_address + buf_cnt > max_address)
4752 {
4753 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4754 }
4755
4756 fastload[i].address = image.sections[i].base_address + offset;
4757 fastload[i].data = malloc(length);
4758 if (fastload[i].data == NULL)
4759 {
4760 free(buffer);
4761 break;
4762 }
4763 memcpy(fastload[i].data, buffer + offset, length);
4764 fastload[i].length = length;
4765
4766 image_size += length;
4767 command_print(cmd_ctx, "%u bytes written at address 0x%8.8x",
4768 (unsigned int)length,
4769 ((unsigned int)(image.sections[i].base_address + offset)));
4770 }
4771
4772 free(buffer);
4773 }
4774
4775 duration_stop_measure(&duration, &duration_text);
4776 if (retval == ERROR_OK)
4777 {
4778 command_print(cmd_ctx, "Loaded %u bytes in %s", (unsigned int)image_size, duration_text);
4779 command_print(cmd_ctx, "NB!!! image has not been loaded to target, issue a subsequent 'fast_load' to do so.");
4780 }
4781 free(duration_text);
4782
4783 image_close(&image);
4784
4785 if (retval != ERROR_OK)
4786 {
4787 free_fastload();
4788 }
4789
4790 return retval;
4791 }
4792
4793 static int handle_fast_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4794 {
4795 if (argc > 0)
4796 return ERROR_COMMAND_SYNTAX_ERROR;
4797 if (fastload == NULL)
4798 {
4799 LOG_ERROR("No image in memory");
4800 return ERROR_FAIL;
4801 }
4802 int i;
4803 int ms = timeval_ms();
4804 int size = 0;
4805 int retval = ERROR_OK;
4806 for (i = 0; i < fastload_num;i++)
4807 {
4808 target_t *target = get_current_target(cmd_ctx);
4809 command_print(cmd_ctx, "Write to 0x%08x, length 0x%08x",
4810 (unsigned int)(fastload[i].address),
4811 (unsigned int)(fastload[i].length));
4812 if (retval == ERROR_OK)
4813 {
4814 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4815 }
4816 size += fastload[i].length;
4817 }
4818 int after = timeval_ms();
4819 command_print(cmd_ctx, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
4820 return retval;
4821 }
4822
4823 static int jim_mcrmrc(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4824 {
4825 command_context_t *context;
4826 target_t *target;
4827 int retval;
4828
4829 context = Jim_GetAssocData(interp, "context");
4830 if (context == NULL) {
4831 LOG_ERROR("array2mem: no command context");
4832 return JIM_ERR;
4833 }
4834 target = get_current_target(context);
4835 if (target == NULL) {
4836 LOG_ERROR("array2mem: no current target");
4837 return JIM_ERR;
4838 }
4839
4840 if ((argc < 6) || (argc > 7))
4841 {
4842 return JIM_ERR;
4843 }
4844
4845 int cpnum;
4846 uint32_t op1;
4847 uint32_t op2;
4848 uint32_t CRn;
4849 uint32_t CRm;
4850 uint32_t value;
4851
4852 int e;
4853 long l;
4854 e = Jim_GetLong(interp, argv[1], &l);
4855 if (e != JIM_OK) {
4856 return e;
4857 }
4858 cpnum = l;
4859
4860 e = Jim_GetLong(interp, argv[2], &l);
4861 if (e != JIM_OK) {
4862 return e;
4863 }
4864 op1 = l;
4865
4866 e = Jim_GetLong(interp, argv[3], &l);
4867 if (e != JIM_OK) {
4868 return e;
4869 }
4870 op2 = l;
4871
4872 e = Jim_GetLong(interp, argv[4], &l);
4873 if (e != JIM_OK) {
4874 return e;
4875 }
4876 CRn = l;
4877
4878 e = Jim_GetLong(interp, argv[5], &l);
4879 if (e != JIM_OK) {
4880 return e;
4881 }
4882 CRm = l;
4883
4884 value = 0;
4885
4886 if (argc == 7)
4887 {
4888 e = Jim_GetLong(interp, argv[6], &l);
4889 if (e != JIM_OK) {
4890 return e;
4891 }
4892 value = l;
4893
4894 retval = target_mcr(target, cpnum, op1, op2, CRn, CRm, value);
4895 if (retval != ERROR_OK)
4896 return JIM_ERR;
4897 } else
4898 {
4899 retval = target_mrc(target, cpnum, op1, op2, CRn, CRm, &value);
4900 if (retval != ERROR_OK)
4901 return JIM_ERR;
4902
4903 Jim_SetResult(interp, Jim_NewIntObj(interp, value));
4904 }
4905
4906 return JIM_OK;
4907 }

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)