David Brownell <david-b@pacbell.net>Fix some command helptext:
[openocd.git] / src / target / target.c
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 √ė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_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
73 static int jim_target(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
74
75 static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv);
76 static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv);
77
78 /* targets */
79 extern target_type_t arm7tdmi_target;
80 extern target_type_t arm720t_target;
81 extern target_type_t arm9tdmi_target;
82 extern target_type_t arm920t_target;
83 extern target_type_t arm966e_target;
84 extern target_type_t arm926ejs_target;
85 extern target_type_t fa526_target;
86 extern target_type_t feroceon_target;
87 extern target_type_t xscale_target;
88 extern target_type_t cortexm3_target;
89 extern target_type_t cortexa8_target;
90 extern target_type_t arm11_target;
91 extern target_type_t mips_m4k_target;
92 extern target_type_t avr_target;
93
94 target_type_t *target_types[] =
95 {
96 &arm7tdmi_target,
97 &arm9tdmi_target,
98 &arm920t_target,
99 &arm720t_target,
100 &arm966e_target,
101 &arm926ejs_target,
102 &fa526_target,
103 &feroceon_target,
104 &xscale_target,
105 &cortexm3_target,
106 &cortexa8_target,
107 &arm11_target,
108 &mips_m4k_target,
109 &avr_target,
110 NULL,
111 };
112
113 target_t *all_targets = NULL;
114 target_event_callback_t *target_event_callbacks = NULL;
115 target_timer_callback_t *target_timer_callbacks = NULL;
116
117 const Jim_Nvp nvp_assert[] = {
118 { .name = "assert", NVP_ASSERT },
119 { .name = "deassert", NVP_DEASSERT },
120 { .name = "T", NVP_ASSERT },
121 { .name = "F", NVP_DEASSERT },
122 { .name = "t", NVP_ASSERT },
123 { .name = "f", NVP_DEASSERT },
124 { .name = NULL, .value = -1 }
125 };
126
127 const Jim_Nvp nvp_error_target[] = {
128 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
129 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
130 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
131 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
132 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
133 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
134 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
135 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
136 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
137 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
138 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
139 { .value = -1, .name = NULL }
140 };
141
142 const char *target_strerror_safe(int err)
143 {
144 const Jim_Nvp *n;
145
146 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
147 if (n->name == NULL) {
148 return "unknown";
149 } else {
150 return n->name;
151 }
152 }
153
154 static const Jim_Nvp nvp_target_event[] = {
155 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
156 { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
157
158 { .value = TARGET_EVENT_EARLY_HALTED, .name = "early-halted" },
159 { .value = TARGET_EVENT_HALTED, .name = "halted" },
160 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
161 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
162 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
163
164 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
165 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
166
167 /* historical name */
168
169 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
170
171 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
172 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
173 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
174 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
175 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
176 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
177 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
178 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
179 { .value = TARGET_EVENT_RESET_INIT , .name = "reset-init" },
180 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
181
182 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
183 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
184
185 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
186 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
187
188 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
189 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
190
191 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
192 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
193
194 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
195 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
196
197 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
198 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
199 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
200
201 { .name = NULL, .value = -1 }
202 };
203
204 const Jim_Nvp nvp_target_state[] = {
205 { .name = "unknown", .value = TARGET_UNKNOWN },
206 { .name = "running", .value = TARGET_RUNNING },
207 { .name = "halted", .value = TARGET_HALTED },
208 { .name = "reset", .value = TARGET_RESET },
209 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
210 { .name = NULL, .value = -1 },
211 };
212
213 const Jim_Nvp nvp_target_debug_reason [] = {
214 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
215 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
216 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
217 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
218 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
219 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
220 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
221 { .name = NULL, .value = -1 },
222 };
223
224 const Jim_Nvp nvp_target_endian[] = {
225 { .name = "big", .value = TARGET_BIG_ENDIAN },
226 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
227 { .name = "be", .value = TARGET_BIG_ENDIAN },
228 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
229 { .name = NULL, .value = -1 },
230 };
231
232 const Jim_Nvp nvp_reset_modes[] = {
233 { .name = "unknown", .value = RESET_UNKNOWN },
234 { .name = "run" , .value = RESET_RUN },
235 { .name = "halt" , .value = RESET_HALT },
236 { .name = "init" , .value = RESET_INIT },
237 { .name = NULL , .value = -1 },
238 };
239
240 const char *
241 target_state_name( target_t *t )
242 {
243 const char *cp;
244 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
245 if( !cp ){
246 LOG_ERROR("Invalid target state: %d", (int)(t->state));
247 cp = "(*BUG*unknown*BUG*)";
248 }
249 return cp;
250 }
251
252 static int max_target_number(void)
253 {
254 target_t *t;
255 int x;
256
257 x = -1;
258 t = all_targets;
259 while (t) {
260 if (x < t->target_number) {
261 x = (t->target_number) + 1;
262 }
263 t = t->next;
264 }
265 return x;
266 }
267
268 /* determine the number of the new target */
269 static int new_target_number(void)
270 {
271 target_t *t;
272 int x;
273
274 /* number is 0 based */
275 x = -1;
276 t = all_targets;
277 while (t) {
278 if (x < t->target_number) {
279 x = t->target_number;
280 }
281 t = t->next;
282 }
283 return x + 1;
284 }
285
286 static int target_continuous_poll = 1;
287
288 /* read a uint32_t from a buffer in target memory endianness */
289 uint32_t target_buffer_get_u32(target_t *target, const uint8_t *buffer)
290 {
291 if (target->endianness == TARGET_LITTLE_ENDIAN)
292 return le_to_h_u32(buffer);
293 else
294 return be_to_h_u32(buffer);
295 }
296
297 /* read a uint16_t from a buffer in target memory endianness */
298 uint16_t target_buffer_get_u16(target_t *target, const uint8_t *buffer)
299 {
300 if (target->endianness == TARGET_LITTLE_ENDIAN)
301 return le_to_h_u16(buffer);
302 else
303 return be_to_h_u16(buffer);
304 }
305
306 /* read a uint8_t from a buffer in target memory endianness */
307 uint8_t target_buffer_get_u8(target_t *target, const uint8_t *buffer)
308 {
309 return *buffer & 0x0ff;
310 }
311
312 /* write a uint32_t to a buffer in target memory endianness */
313 void target_buffer_set_u32(target_t *target, uint8_t *buffer, uint32_t value)
314 {
315 if (target->endianness == TARGET_LITTLE_ENDIAN)
316 h_u32_to_le(buffer, value);
317 else
318 h_u32_to_be(buffer, value);
319 }
320
321 /* write a uint16_t to a buffer in target memory endianness */
322 void target_buffer_set_u16(target_t *target, uint8_t *buffer, uint16_t value)
323 {
324 if (target->endianness == TARGET_LITTLE_ENDIAN)
325 h_u16_to_le(buffer, value);
326 else
327 h_u16_to_be(buffer, value);
328 }
329
330 /* write a uint8_t to a buffer in target memory endianness */
331 void target_buffer_set_u8(target_t *target, uint8_t *buffer, uint8_t value)
332 {
333 *buffer = value;
334 }
335
336 /* return a pointer to a configured target; id is name or number */
337 target_t *get_target(const char *id)
338 {
339 target_t *target;
340
341 /* try as tcltarget name */
342 for (target = all_targets; target; target = target->next) {
343 if (target->cmd_name == NULL)
344 continue;
345 if (strcmp(id, target->cmd_name) == 0)
346 return target;
347 }
348
349 /* no match, try as number */
350 unsigned num;
351 if (parse_uint(id, &num) != ERROR_OK)
352 return NULL;
353
354 for (target = all_targets; target; target = target->next) {
355 if (target->target_number == (int)num)
356 return target;
357 }
358
359 return NULL;
360 }
361
362 /* returns a pointer to the n-th configured target */
363 static target_t *get_target_by_num(int num)
364 {
365 target_t *target = all_targets;
366
367 while (target) {
368 if (target->target_number == num) {
369 return target;
370 }
371 target = target->next;
372 }
373
374 return NULL;
375 }
376
377 int get_num_by_target(target_t *query_target)
378 {
379 return query_target->target_number;
380 }
381
382 target_t* get_current_target(command_context_t *cmd_ctx)
383 {
384 target_t *target = get_target_by_num(cmd_ctx->current_target);
385
386 if (target == NULL)
387 {
388 LOG_ERROR("BUG: current_target out of bounds");
389 exit(-1);
390 }
391
392 return target;
393 }
394
395 int target_poll(struct target_s *target)
396 {
397 /* We can't poll until after examine */
398 if (!target_was_examined(target))
399 {
400 /* Fail silently lest we pollute the log */
401 return ERROR_FAIL;
402 }
403 return target->type->poll(target);
404 }
405
406 int target_halt(struct target_s *target)
407 {
408 /* We can't poll until after examine */
409 if (!target_was_examined(target))
410 {
411 LOG_ERROR("Target not examined yet");
412 return ERROR_FAIL;
413 }
414 return target->type->halt(target);
415 }
416
417 int target_resume(struct target_s *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
418 {
419 int retval;
420
421 /* We can't poll until after examine */
422 if (!target_was_examined(target))
423 {
424 LOG_ERROR("Target not examined yet");
425 return ERROR_FAIL;
426 }
427
428 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
429 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
430 * the application.
431 */
432 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
433 return retval;
434
435 return retval;
436 }
437
438 int target_process_reset(struct command_context_s *cmd_ctx, enum target_reset_mode reset_mode)
439 {
440 char buf[100];
441 int retval;
442 Jim_Nvp *n;
443 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
444 if (n->name == NULL) {
445 LOG_ERROR("invalid reset mode");
446 return ERROR_FAIL;
447 }
448
449 /* disable polling during reset to make reset event scripts
450 * more predictable, i.e. dr/irscan & pathmove in events will
451 * not have JTAG operations injected into the middle of a sequence.
452 */
453 int save_poll = target_continuous_poll;
454 target_continuous_poll = 0;
455
456 sprintf(buf, "ocd_process_reset %s", n->name);
457 retval = Jim_Eval(interp, buf);
458
459 target_continuous_poll = save_poll;
460
461 if (retval != JIM_OK) {
462 Jim_PrintErrorMessage(interp);
463 return ERROR_FAIL;
464 }
465
466 /* We want any events to be processed before the prompt */
467 retval = target_call_timer_callbacks_now();
468
469 return retval;
470 }
471
472 static int default_virt2phys(struct target_s *target, uint32_t virtual, uint32_t *physical)
473 {
474 *physical = virtual;
475 return ERROR_OK;
476 }
477
478 static int default_mmu(struct target_s *target, int *enabled)
479 {
480 *enabled = 0;
481 return ERROR_OK;
482 }
483
484 static int default_examine(struct target_s *target)
485 {
486 target_set_examined(target);
487 return ERROR_OK;
488 }
489
490 int target_examine_one(struct target_s *target)
491 {
492 return target->type->examine(target);
493 }
494
495 static int jtag_enable_callback(enum jtag_event event, void *priv)
496 {
497 target_t *target = priv;
498
499 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
500 return ERROR_OK;
501
502 jtag_unregister_event_callback(jtag_enable_callback, target);
503 return target_examine_one(target);
504 }
505
506
507 /* Targets that correctly implement init + examine, i.e.
508 * no communication with target during init:
509 *
510 * XScale
511 */
512 int target_examine(void)
513 {
514 int retval = ERROR_OK;
515 target_t *target;
516
517 for (target = all_targets; target; target = target->next)
518 {
519 /* defer examination, but don't skip it */
520 if (!target->tap->enabled) {
521 jtag_register_event_callback(jtag_enable_callback,
522 target);
523 continue;
524 }
525 if ((retval = target_examine_one(target)) != ERROR_OK)
526 return retval;
527 }
528 return retval;
529 }
530 const char *target_get_name(struct target_s *target)
531 {
532 return target->type->name;
533 }
534
535 static int target_write_memory_imp(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
536 {
537 if (!target_was_examined(target))
538 {
539 LOG_ERROR("Target not examined yet");
540 return ERROR_FAIL;
541 }
542 return target->type->write_memory_imp(target, address, size, count, buffer);
543 }
544
545 static int target_read_memory_imp(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
546 {
547 if (!target_was_examined(target))
548 {
549 LOG_ERROR("Target not examined yet");
550 return ERROR_FAIL;
551 }
552 return target->type->read_memory_imp(target, address, size, count, buffer);
553 }
554
555 static int target_soft_reset_halt_imp(struct target_s *target)
556 {
557 if (!target_was_examined(target))
558 {
559 LOG_ERROR("Target not examined yet");
560 return ERROR_FAIL;
561 }
562 return target->type->soft_reset_halt_imp(target);
563 }
564
565 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)
566 {
567 if (!target_was_examined(target))
568 {
569 LOG_ERROR("Target not examined yet");
570 return ERROR_FAIL;
571 }
572 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);
573 }
574
575 int target_read_memory(struct target_s *target,
576 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
577 {
578 return target->type->read_memory(target, address, size, count, buffer);
579 }
580
581 int target_write_memory(struct target_s *target,
582 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
583 {
584 return target->type->write_memory(target, address, size, count, buffer);
585 }
586 int target_bulk_write_memory(struct target_s *target,
587 uint32_t address, uint32_t count, uint8_t *buffer)
588 {
589 return target->type->bulk_write_memory(target, address, count, buffer);
590 }
591
592 int target_add_breakpoint(struct target_s *target,
593 struct breakpoint_s *breakpoint)
594 {
595 return target->type->add_breakpoint(target, breakpoint);
596 }
597 int target_remove_breakpoint(struct target_s *target,
598 struct breakpoint_s *breakpoint)
599 {
600 return target->type->remove_breakpoint(target, breakpoint);
601 }
602
603 int target_add_watchpoint(struct target_s *target,
604 struct watchpoint_s *watchpoint)
605 {
606 return target->type->add_watchpoint(target, watchpoint);
607 }
608 int target_remove_watchpoint(struct target_s *target,
609 struct watchpoint_s *watchpoint)
610 {
611 return target->type->remove_watchpoint(target, watchpoint);
612 }
613
614 int target_get_gdb_reg_list(struct target_s *target,
615 struct reg_s **reg_list[], int *reg_list_size)
616 {
617 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
618 }
619 int target_step(struct target_s *target,
620 int current, uint32_t address, int handle_breakpoints)
621 {
622 return target->type->step(target, current, address, handle_breakpoints);
623 }
624
625
626 int target_run_algorithm(struct target_s *target,
627 int num_mem_params, mem_param_t *mem_params,
628 int num_reg_params, reg_param_t *reg_param,
629 uint32_t entry_point, uint32_t exit_point,
630 int timeout_ms, void *arch_info)
631 {
632 return target->type->run_algorithm(target,
633 num_mem_params, mem_params, num_reg_params, reg_param,
634 entry_point, exit_point, timeout_ms, arch_info);
635 }
636
637 /// @returns @c true if the target has been examined.
638 bool target_was_examined(struct target_s *target)
639 {
640 return target->type->examined;
641 }
642 /// Sets the @c examined flag for the given target.
643 void target_set_examined(struct target_s *target)
644 {
645 target->type->examined = true;
646 }
647 // Reset the @c examined flag for the given target.
648 void target_reset_examined(struct target_s *target)
649 {
650 target->type->examined = false;
651 }
652
653
654 int target_init(struct command_context_s *cmd_ctx)
655 {
656 target_t *target = all_targets;
657 int retval;
658
659 while (target)
660 {
661 target_reset_examined(target);
662 if (target->type->examine == NULL)
663 {
664 target->type->examine = default_examine;
665 }
666
667 if ((retval = target->type->init_target(cmd_ctx, target)) != ERROR_OK)
668 {
669 LOG_ERROR("target '%s' init failed", target_get_name(target));
670 return retval;
671 }
672
673 /* Set up default functions if none are provided by target */
674 if (target->type->virt2phys == NULL)
675 {
676 target->type->virt2phys = default_virt2phys;
677 }
678 target->type->virt2phys = default_virt2phys;
679 /* a non-invasive way(in terms of patches) to add some code that
680 * runs before the type->write/read_memory implementation
681 */
682 target->type->write_memory_imp = target->type->write_memory;
683 target->type->write_memory = target_write_memory_imp;
684 target->type->read_memory_imp = target->type->read_memory;
685 target->type->read_memory = target_read_memory_imp;
686 target->type->soft_reset_halt_imp = target->type->soft_reset_halt;
687 target->type->soft_reset_halt = target_soft_reset_halt_imp;
688 target->type->run_algorithm_imp = target->type->run_algorithm;
689 target->type->run_algorithm = target_run_algorithm_imp;
690
691 if (target->type->mmu == NULL)
692 {
693 target->type->mmu = default_mmu;
694 }
695 target = target->next;
696 }
697
698 if (all_targets)
699 {
700 if ((retval = target_register_user_commands(cmd_ctx)) != ERROR_OK)
701 return retval;
702 if ((retval = target_register_timer_callback(handle_target, 100, 1, NULL)) != ERROR_OK)
703 return retval;
704 }
705
706 return ERROR_OK;
707 }
708
709 int target_register_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
710 {
711 target_event_callback_t **callbacks_p = &target_event_callbacks;
712
713 if (callback == NULL)
714 {
715 return ERROR_INVALID_ARGUMENTS;
716 }
717
718 if (*callbacks_p)
719 {
720 while ((*callbacks_p)->next)
721 callbacks_p = &((*callbacks_p)->next);
722 callbacks_p = &((*callbacks_p)->next);
723 }
724
725 (*callbacks_p) = malloc(sizeof(target_event_callback_t));
726 (*callbacks_p)->callback = callback;
727 (*callbacks_p)->priv = priv;
728 (*callbacks_p)->next = NULL;
729
730 return ERROR_OK;
731 }
732
733 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
734 {
735 target_timer_callback_t **callbacks_p = &target_timer_callbacks;
736 struct timeval now;
737
738 if (callback == NULL)
739 {
740 return ERROR_INVALID_ARGUMENTS;
741 }
742
743 if (*callbacks_p)
744 {
745 while ((*callbacks_p)->next)
746 callbacks_p = &((*callbacks_p)->next);
747 callbacks_p = &((*callbacks_p)->next);
748 }
749
750 (*callbacks_p) = malloc(sizeof(target_timer_callback_t));
751 (*callbacks_p)->callback = callback;
752 (*callbacks_p)->periodic = periodic;
753 (*callbacks_p)->time_ms = time_ms;
754
755 gettimeofday(&now, NULL);
756 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
757 time_ms -= (time_ms % 1000);
758 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
759 if ((*callbacks_p)->when.tv_usec > 1000000)
760 {
761 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
762 (*callbacks_p)->when.tv_sec += 1;
763 }
764
765 (*callbacks_p)->priv = priv;
766 (*callbacks_p)->next = NULL;
767
768 return ERROR_OK;
769 }
770
771 int target_unregister_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
772 {
773 target_event_callback_t **p = &target_event_callbacks;
774 target_event_callback_t *c = target_event_callbacks;
775
776 if (callback == NULL)
777 {
778 return ERROR_INVALID_ARGUMENTS;
779 }
780
781 while (c)
782 {
783 target_event_callback_t *next = c->next;
784 if ((c->callback == callback) && (c->priv == priv))
785 {
786 *p = next;
787 free(c);
788 return ERROR_OK;
789 }
790 else
791 p = &(c->next);
792 c = next;
793 }
794
795 return ERROR_OK;
796 }
797
798 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
799 {
800 target_timer_callback_t **p = &target_timer_callbacks;
801 target_timer_callback_t *c = target_timer_callbacks;
802
803 if (callback == NULL)
804 {
805 return ERROR_INVALID_ARGUMENTS;
806 }
807
808 while (c)
809 {
810 target_timer_callback_t *next = c->next;
811 if ((c->callback == callback) && (c->priv == priv))
812 {
813 *p = next;
814 free(c);
815 return ERROR_OK;
816 }
817 else
818 p = &(c->next);
819 c = next;
820 }
821
822 return ERROR_OK;
823 }
824
825 int target_call_event_callbacks(target_t *target, enum target_event event)
826 {
827 target_event_callback_t *callback = target_event_callbacks;
828 target_event_callback_t *next_callback;
829
830 if (event == TARGET_EVENT_HALTED)
831 {
832 /* execute early halted first */
833 target_call_event_callbacks(target, TARGET_EVENT_EARLY_HALTED);
834 }
835
836 LOG_DEBUG("target event %i (%s)",
837 event,
838 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
839
840 target_handle_event(target, event);
841
842 while (callback)
843 {
844 next_callback = callback->next;
845 callback->callback(target, event, callback->priv);
846 callback = next_callback;
847 }
848
849 return ERROR_OK;
850 }
851
852 static int target_timer_callback_periodic_restart(
853 target_timer_callback_t *cb, struct timeval *now)
854 {
855 int time_ms = cb->time_ms;
856 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
857 time_ms -= (time_ms % 1000);
858 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
859 if (cb->when.tv_usec > 1000000)
860 {
861 cb->when.tv_usec = cb->when.tv_usec - 1000000;
862 cb->when.tv_sec += 1;
863 }
864 return ERROR_OK;
865 }
866
867 static int target_call_timer_callback(target_timer_callback_t *cb,
868 struct timeval *now)
869 {
870 cb->callback(cb->priv);
871
872 if (cb->periodic)
873 return target_timer_callback_periodic_restart(cb, now);
874
875 return target_unregister_timer_callback(cb->callback, cb->priv);
876 }
877
878 static int target_call_timer_callbacks_check_time(int checktime)
879 {
880 keep_alive();
881
882 struct timeval now;
883 gettimeofday(&now, NULL);
884
885 target_timer_callback_t *callback = target_timer_callbacks;
886 while (callback)
887 {
888 // cleaning up may unregister and free this callback
889 target_timer_callback_t *next_callback = callback->next;
890
891 bool call_it = callback->callback &&
892 ((!checktime && callback->periodic) ||
893 now.tv_sec > callback->when.tv_sec ||
894 (now.tv_sec == callback->when.tv_sec &&
895 now.tv_usec >= callback->when.tv_usec));
896
897 if (call_it)
898 {
899 int retval = target_call_timer_callback(callback, &now);
900 if (retval != ERROR_OK)
901 return retval;
902 }
903
904 callback = next_callback;
905 }
906
907 return ERROR_OK;
908 }
909
910 int target_call_timer_callbacks(void)
911 {
912 return target_call_timer_callbacks_check_time(1);
913 }
914
915 /* invoke periodic callbacks immediately */
916 int target_call_timer_callbacks_now(void)
917 {
918 return target_call_timer_callbacks_check_time(0);
919 }
920
921 int target_alloc_working_area(struct target_s *target, uint32_t size, working_area_t **area)
922 {
923 working_area_t *c = target->working_areas;
924 working_area_t *new_wa = NULL;
925
926 /* Reevaluate working area address based on MMU state*/
927 if (target->working_areas == NULL)
928 {
929 int retval;
930 int enabled;
931 retval = target->type->mmu(target, &enabled);
932 if (retval != ERROR_OK)
933 {
934 return retval;
935 }
936 if (enabled)
937 {
938 target->working_area = target->working_area_virt;
939 }
940 else
941 {
942 target->working_area = target->working_area_phys;
943 }
944 }
945
946 /* only allocate multiples of 4 byte */
947 if (size % 4)
948 {
949 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
950 size = (size + 3) & (~3);
951 }
952
953 /* see if there's already a matching working area */
954 while (c)
955 {
956 if ((c->free) && (c->size == size))
957 {
958 new_wa = c;
959 break;
960 }
961 c = c->next;
962 }
963
964 /* if not, allocate a new one */
965 if (!new_wa)
966 {
967 working_area_t **p = &target->working_areas;
968 uint32_t first_free = target->working_area;
969 uint32_t free_size = target->working_area_size;
970
971 LOG_DEBUG("allocating new working area");
972
973 c = target->working_areas;
974 while (c)
975 {
976 first_free += c->size;
977 free_size -= c->size;
978 p = &c->next;
979 c = c->next;
980 }
981
982 if (free_size < size)
983 {
984 LOG_WARNING("not enough working area available(requested %u, free %u)",
985 (unsigned)(size), (unsigned)(free_size));
986 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
987 }
988
989 new_wa = malloc(sizeof(working_area_t));
990 new_wa->next = NULL;
991 new_wa->size = size;
992 new_wa->address = first_free;
993
994 if (target->backup_working_area)
995 {
996 int retval;
997 new_wa->backup = malloc(new_wa->size);
998 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
999 {
1000 free(new_wa->backup);
1001 free(new_wa);
1002 return retval;
1003 }
1004 }
1005 else
1006 {
1007 new_wa->backup = NULL;
1008 }
1009
1010 /* put new entry in list */
1011 *p = new_wa;
1012 }
1013
1014 /* mark as used, and return the new (reused) area */
1015 new_wa->free = 0;
1016 *area = new_wa;
1017
1018 /* user pointer */
1019 new_wa->user = area;
1020
1021 return ERROR_OK;
1022 }
1023
1024 int target_free_working_area_restore(struct target_s *target, working_area_t *area, int restore)
1025 {
1026 if (area->free)
1027 return ERROR_OK;
1028
1029 if (restore && target->backup_working_area)
1030 {
1031 int retval;
1032 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1033 return retval;
1034 }
1035
1036 area->free = 1;
1037
1038 /* mark user pointer invalid */
1039 *area->user = NULL;
1040 area->user = NULL;
1041
1042 return ERROR_OK;
1043 }
1044
1045 int target_free_working_area(struct target_s *target, working_area_t *area)
1046 {
1047 return target_free_working_area_restore(target, area, 1);
1048 }
1049
1050 /* free resources and restore memory, if restoring memory fails,
1051 * free up resources anyway
1052 */
1053 void target_free_all_working_areas_restore(struct target_s *target, int restore)
1054 {
1055 working_area_t *c = target->working_areas;
1056
1057 while (c)
1058 {
1059 working_area_t *next = c->next;
1060 target_free_working_area_restore(target, c, restore);
1061
1062 if (c->backup)
1063 free(c->backup);
1064
1065 free(c);
1066
1067 c = next;
1068 }
1069
1070 target->working_areas = NULL;
1071 }
1072
1073 void target_free_all_working_areas(struct target_s *target)
1074 {
1075 target_free_all_working_areas_restore(target, 1);
1076 }
1077
1078 int target_register_commands(struct command_context_s *cmd_ctx)
1079 {
1080
1081 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)");
1082
1083
1084
1085
1086 register_jim(cmd_ctx, "target", jim_target, "configure target");
1087
1088 return ERROR_OK;
1089 }
1090
1091 int target_arch_state(struct target_s *target)
1092 {
1093 int retval;
1094 if (target == NULL)
1095 {
1096 LOG_USER("No target has been configured");
1097 return ERROR_OK;
1098 }
1099
1100 LOG_USER("target state: %s", target_state_name( target ));
1101
1102 if (target->state != TARGET_HALTED)
1103 return ERROR_OK;
1104
1105 retval = target->type->arch_state(target);
1106 return retval;
1107 }
1108
1109 /* Single aligned words are guaranteed to use 16 or 32 bit access
1110 * mode respectively, otherwise data is handled as quickly as
1111 * possible
1112 */
1113 int target_write_buffer(struct target_s *target, uint32_t address, uint32_t size, uint8_t *buffer)
1114 {
1115 int retval;
1116 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1117 (int)size, (unsigned)address);
1118
1119 if (!target_was_examined(target))
1120 {
1121 LOG_ERROR("Target not examined yet");
1122 return ERROR_FAIL;
1123 }
1124
1125 if (size == 0) {
1126 return ERROR_OK;
1127 }
1128
1129 if ((address + size - 1) < address)
1130 {
1131 /* GDB can request this when e.g. PC is 0xfffffffc*/
1132 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1133 (unsigned)address,
1134 (unsigned)size);
1135 return ERROR_FAIL;
1136 }
1137
1138 if (((address % 2) == 0) && (size == 2))
1139 {
1140 return target_write_memory(target, address, 2, 1, buffer);
1141 }
1142
1143 /* handle unaligned head bytes */
1144 if (address % 4)
1145 {
1146 uint32_t unaligned = 4 - (address % 4);
1147
1148 if (unaligned > size)
1149 unaligned = size;
1150
1151 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1152 return retval;
1153
1154 buffer += unaligned;
1155 address += unaligned;
1156 size -= unaligned;
1157 }
1158
1159 /* handle aligned words */
1160 if (size >= 4)
1161 {
1162 int aligned = size - (size % 4);
1163
1164 /* use bulk writes above a certain limit. This may have to be changed */
1165 if (aligned > 128)
1166 {
1167 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1168 return retval;
1169 }
1170 else
1171 {
1172 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1173 return retval;
1174 }
1175
1176 buffer += aligned;
1177 address += aligned;
1178 size -= aligned;
1179 }
1180
1181 /* handle tail writes of less than 4 bytes */
1182 if (size > 0)
1183 {
1184 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1185 return retval;
1186 }
1187
1188 return ERROR_OK;
1189 }
1190
1191 /* Single aligned words are guaranteed to use 16 or 32 bit access
1192 * mode respectively, otherwise data is handled as quickly as
1193 * possible
1194 */
1195 int target_read_buffer(struct target_s *target, uint32_t address, uint32_t size, uint8_t *buffer)
1196 {
1197 int retval;
1198 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1199 (int)size, (unsigned)address);
1200
1201 if (!target_was_examined(target))
1202 {
1203 LOG_ERROR("Target not examined yet");
1204 return ERROR_FAIL;
1205 }
1206
1207 if (size == 0) {
1208 return ERROR_OK;
1209 }
1210
1211 if ((address + size - 1) < address)
1212 {
1213 /* GDB can request this when e.g. PC is 0xfffffffc*/
1214 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1215 address,
1216 size);
1217 return ERROR_FAIL;
1218 }
1219
1220 if (((address % 2) == 0) && (size == 2))
1221 {
1222 return target_read_memory(target, address, 2, 1, buffer);
1223 }
1224
1225 /* handle unaligned head bytes */
1226 if (address % 4)
1227 {
1228 uint32_t unaligned = 4 - (address % 4);
1229
1230 if (unaligned > size)
1231 unaligned = size;
1232
1233 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1234 return retval;
1235
1236 buffer += unaligned;
1237 address += unaligned;
1238 size -= unaligned;
1239 }
1240
1241 /* handle aligned words */
1242 if (size >= 4)
1243 {
1244 int aligned = size - (size % 4);
1245
1246 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1247 return retval;
1248
1249 buffer += aligned;
1250 address += aligned;
1251 size -= aligned;
1252 }
1253
1254 /* handle tail writes of less than 4 bytes */
1255 if (size > 0)
1256 {
1257 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1258 return retval;
1259 }
1260
1261 return ERROR_OK;
1262 }
1263
1264 int target_checksum_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t* crc)
1265 {
1266 uint8_t *buffer;
1267 int retval;
1268 uint32_t i;
1269 uint32_t checksum = 0;
1270 if (!target_was_examined(target))
1271 {
1272 LOG_ERROR("Target not examined yet");
1273 return ERROR_FAIL;
1274 }
1275
1276 if ((retval = target->type->checksum_memory(target, address,
1277 size, &checksum)) != ERROR_OK)
1278 {
1279 buffer = malloc(size);
1280 if (buffer == NULL)
1281 {
1282 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1283 return ERROR_INVALID_ARGUMENTS;
1284 }
1285 retval = target_read_buffer(target, address, size, buffer);
1286 if (retval != ERROR_OK)
1287 {
1288 free(buffer);
1289 return retval;
1290 }
1291
1292 /* convert to target endianess */
1293 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1294 {
1295 uint32_t target_data;
1296 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1297 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1298 }
1299
1300 retval = image_calculate_checksum(buffer, size, &checksum);
1301 free(buffer);
1302 }
1303
1304 *crc = checksum;
1305
1306 return retval;
1307 }
1308
1309 int target_blank_check_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t* blank)
1310 {
1311 int retval;
1312 if (!target_was_examined(target))
1313 {
1314 LOG_ERROR("Target not examined yet");
1315 return ERROR_FAIL;
1316 }
1317
1318 if (target->type->blank_check_memory == 0)
1319 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1320
1321 retval = target->type->blank_check_memory(target, address, size, blank);
1322
1323 return retval;
1324 }
1325
1326 int target_read_u32(struct target_s *target, uint32_t address, uint32_t *value)
1327 {
1328 uint8_t value_buf[4];
1329 if (!target_was_examined(target))
1330 {
1331 LOG_ERROR("Target not examined yet");
1332 return ERROR_FAIL;
1333 }
1334
1335 int retval = target_read_memory(target, address, 4, 1, value_buf);
1336
1337 if (retval == ERROR_OK)
1338 {
1339 *value = target_buffer_get_u32(target, value_buf);
1340 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1341 address,
1342 *value);
1343 }
1344 else
1345 {
1346 *value = 0x0;
1347 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1348 address);
1349 }
1350
1351 return retval;
1352 }
1353
1354 int target_read_u16(struct target_s *target, uint32_t address, uint16_t *value)
1355 {
1356 uint8_t value_buf[2];
1357 if (!target_was_examined(target))
1358 {
1359 LOG_ERROR("Target not examined yet");
1360 return ERROR_FAIL;
1361 }
1362
1363 int retval = target_read_memory(target, address, 2, 1, value_buf);
1364
1365 if (retval == ERROR_OK)
1366 {
1367 *value = target_buffer_get_u16(target, value_buf);
1368 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1369 address,
1370 *value);
1371 }
1372 else
1373 {
1374 *value = 0x0;
1375 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1376 address);
1377 }
1378
1379 return retval;
1380 }
1381
1382 int target_read_u8(struct target_s *target, uint32_t address, uint8_t *value)
1383 {
1384 int retval = target_read_memory(target, address, 1, 1, value);
1385 if (!target_was_examined(target))
1386 {
1387 LOG_ERROR("Target not examined yet");
1388 return ERROR_FAIL;
1389 }
1390
1391 if (retval == ERROR_OK)
1392 {
1393 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1394 address,
1395 *value);
1396 }
1397 else
1398 {
1399 *value = 0x0;
1400 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1401 address);
1402 }
1403
1404 return retval;
1405 }
1406
1407 int target_write_u32(struct target_s *target, uint32_t address, uint32_t value)
1408 {
1409 int retval;
1410 uint8_t value_buf[4];
1411 if (!target_was_examined(target))
1412 {
1413 LOG_ERROR("Target not examined yet");
1414 return ERROR_FAIL;
1415 }
1416
1417 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1418 address,
1419 value);
1420
1421 target_buffer_set_u32(target, value_buf, value);
1422 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1423 {
1424 LOG_DEBUG("failed: %i", retval);
1425 }
1426
1427 return retval;
1428 }
1429
1430 int target_write_u16(struct target_s *target, uint32_t address, uint16_t value)
1431 {
1432 int retval;
1433 uint8_t value_buf[2];
1434 if (!target_was_examined(target))
1435 {
1436 LOG_ERROR("Target not examined yet");
1437 return ERROR_FAIL;
1438 }
1439
1440 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1441 address,
1442 value);
1443
1444 target_buffer_set_u16(target, value_buf, value);
1445 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1446 {
1447 LOG_DEBUG("failed: %i", retval);
1448 }
1449
1450 return retval;
1451 }
1452
1453 int target_write_u8(struct target_s *target, uint32_t address, uint8_t value)
1454 {
1455 int retval;
1456 if (!target_was_examined(target))
1457 {
1458 LOG_ERROR("Target not examined yet");
1459 return ERROR_FAIL;
1460 }
1461
1462 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1463 address, value);
1464
1465 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1466 {
1467 LOG_DEBUG("failed: %i", retval);
1468 }
1469
1470 return retval;
1471 }
1472
1473 int target_register_user_commands(struct command_context_s *cmd_ctx)
1474 {
1475 int retval = ERROR_OK;
1476
1477
1478 /* script procedures */
1479 register_command(cmd_ctx, NULL, "profile", handle_profile_command, COMMAND_EXEC, "profiling samples the CPU PC");
1480 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>");
1481 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>");
1482
1483 register_command(cmd_ctx, NULL, "fast_load_image", handle_fast_load_image_command, COMMAND_ANY,
1484 "same args as load_image, image stored in memory - mainly for profiling purposes");
1485
1486 register_command(cmd_ctx, NULL, "fast_load", handle_fast_load_command, COMMAND_ANY,
1487 "loads active fast load image to current target - mainly for profiling purposes");
1488
1489
1490 register_command(cmd_ctx, NULL, "virt2phys", handle_virt2phys_command, COMMAND_ANY, "translate a virtual address into a physical address");
1491 register_command(cmd_ctx, NULL, "reg", handle_reg_command, COMMAND_EXEC, "display or set a register");
1492 register_command(cmd_ctx, NULL, "poll", handle_poll_command, COMMAND_EXEC, "poll target state");
1493 register_command(cmd_ctx, NULL, "wait_halt", handle_wait_halt_command, COMMAND_EXEC, "wait for target halt [time (s)]");
1494 register_command(cmd_ctx, NULL, "halt", handle_halt_command, COMMAND_EXEC, "halt target");
1495 register_command(cmd_ctx, NULL, "resume", handle_resume_command, COMMAND_EXEC, "resume target [addr]");
1496 register_command(cmd_ctx, NULL, "step", handle_step_command, COMMAND_EXEC, "step one instruction from current PC or [addr]");
1497 register_command(cmd_ctx, NULL, "reset", handle_reset_command, COMMAND_EXEC, "reset target [run | halt | init] - default is run");
1498 register_command(cmd_ctx, NULL, "soft_reset_halt", handle_soft_reset_halt_command, COMMAND_EXEC, "halt the target and do a soft reset");
1499
1500 register_command(cmd_ctx, NULL, "mdw", handle_md_command, COMMAND_EXEC, "display memory words <addr> [count]");
1501 register_command(cmd_ctx, NULL, "mdh", handle_md_command, COMMAND_EXEC, "display memory half-words <addr> [count]");
1502 register_command(cmd_ctx, NULL, "mdb", handle_md_command, COMMAND_EXEC, "display memory bytes <addr> [count]");
1503
1504 register_command(cmd_ctx, NULL, "mww", handle_mw_command, COMMAND_EXEC, "write memory word <addr> <value> [count]");
1505 register_command(cmd_ctx, NULL, "mwh", handle_mw_command, COMMAND_EXEC, "write memory half-word <addr> <value> [count]");
1506 register_command(cmd_ctx, NULL, "mwb", handle_mw_command, COMMAND_EXEC, "write memory byte <addr> <value> [count]");
1507
1508 register_command(cmd_ctx, NULL, "bp",
1509 handle_bp_command, COMMAND_EXEC,
1510 "list or set breakpoint [<address> <length> [hw]]");
1511 register_command(cmd_ctx, NULL, "rbp",
1512 handle_rbp_command, COMMAND_EXEC,
1513 "remove breakpoint <address>");
1514 register_command(cmd_ctx, NULL, "wp",
1515 handle_wp_command, COMMAND_EXEC,
1516 "list or set watchpoint "
1517 "[<address> <length> <r/w/a> [value] [mask]]");
1518 register_command(cmd_ctx, NULL, "rwp",
1519 handle_rwp_command, COMMAND_EXEC,
1520 "remove watchpoint <address>");
1521
1522 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]");
1523 register_command(cmd_ctx, NULL, "dump_image", handle_dump_image_command, COMMAND_EXEC, "dump_image <file> <address> <size>");
1524 register_command(cmd_ctx, NULL, "verify_image", handle_verify_image_command, COMMAND_EXEC, "verify_image <file> [offset] [type]");
1525 register_command(cmd_ctx, NULL, "test_image", handle_test_image_command, COMMAND_EXEC, "test_image <file> [offset] [type]");
1526
1527 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
1528 return retval;
1529 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
1530 return retval;
1531
1532 return retval;
1533 }
1534
1535 static int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1536 {
1537 target_t *target = all_targets;
1538
1539 if (argc == 1)
1540 {
1541 target = get_target(args[0]);
1542 if (target == NULL) {
1543 command_print(cmd_ctx,"Target: %s is unknown, try one of:\n", args[0]);
1544 goto DumpTargets;
1545 }
1546 if (!target->tap->enabled) {
1547 command_print(cmd_ctx,"Target: TAP %s is disabled, "
1548 "can't be the current target\n",
1549 target->tap->dotted_name);
1550 return ERROR_FAIL;
1551 }
1552
1553 cmd_ctx->current_target = target->target_number;
1554 return ERROR_OK;
1555 }
1556 DumpTargets:
1557
1558 target = all_targets;
1559 command_print(cmd_ctx, " TargetName Type Endian TapName State ");
1560 command_print(cmd_ctx, "-- ------------------ ---------- ------ ------------------ ------------");
1561 while (target)
1562 {
1563 const char *state;
1564 char marker = ' ';
1565
1566 if (target->tap->enabled)
1567 state = target_state_name( target );
1568 else
1569 state = "tap-disabled";
1570
1571 if (cmd_ctx->current_target == target->target_number)
1572 marker = '*';
1573
1574 /* keep columns lined up to match the headers above */
1575 command_print(cmd_ctx, "%2d%c %-18s %-10s %-6s %-18s %s",
1576 target->target_number,
1577 marker,
1578 target->cmd_name,
1579 target_get_name(target),
1580 Jim_Nvp_value2name_simple(nvp_target_endian,
1581 target->endianness)->name,
1582 target->tap->dotted_name,
1583 state);
1584 target = target->next;
1585 }
1586
1587 return ERROR_OK;
1588 }
1589
1590 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1591
1592 static int powerDropout;
1593 static int srstAsserted;
1594
1595 static int runPowerRestore;
1596 static int runPowerDropout;
1597 static int runSrstAsserted;
1598 static int runSrstDeasserted;
1599
1600 static int sense_handler(void)
1601 {
1602 static int prevSrstAsserted = 0;
1603 static int prevPowerdropout = 0;
1604
1605 int retval;
1606 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1607 return retval;
1608
1609 int powerRestored;
1610 powerRestored = prevPowerdropout && !powerDropout;
1611 if (powerRestored)
1612 {
1613 runPowerRestore = 1;
1614 }
1615
1616 long long current = timeval_ms();
1617 static long long lastPower = 0;
1618 int waitMore = lastPower + 2000 > current;
1619 if (powerDropout && !waitMore)
1620 {
1621 runPowerDropout = 1;
1622 lastPower = current;
1623 }
1624
1625 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1626 return retval;
1627
1628 int srstDeasserted;
1629 srstDeasserted = prevSrstAsserted && !srstAsserted;
1630
1631 static long long lastSrst = 0;
1632 waitMore = lastSrst + 2000 > current;
1633 if (srstDeasserted && !waitMore)
1634 {
1635 runSrstDeasserted = 1;
1636 lastSrst = current;
1637 }
1638
1639 if (!prevSrstAsserted && srstAsserted)
1640 {
1641 runSrstAsserted = 1;
1642 }
1643
1644 prevSrstAsserted = srstAsserted;
1645 prevPowerdropout = powerDropout;
1646
1647 if (srstDeasserted || powerRestored)
1648 {
1649 /* Other than logging the event we can't do anything here.
1650 * Issuing a reset is a particularly bad idea as we might
1651 * be inside a reset already.
1652 */
1653 }
1654
1655 return ERROR_OK;
1656 }
1657
1658 /* process target state changes */
1659 int handle_target(void *priv)
1660 {
1661 int retval = ERROR_OK;
1662
1663 /* we do not want to recurse here... */
1664 static int recursive = 0;
1665 if (! recursive)
1666 {
1667 recursive = 1;
1668 sense_handler();
1669 /* danger! running these procedures can trigger srst assertions and power dropouts.
1670 * We need to avoid an infinite loop/recursion here and we do that by
1671 * clearing the flags after running these events.
1672 */
1673 int did_something = 0;
1674 if (runSrstAsserted)
1675 {
1676 Jim_Eval(interp, "srst_asserted");
1677 did_something = 1;
1678 }
1679 if (runSrstDeasserted)
1680 {
1681 Jim_Eval(interp, "srst_deasserted");
1682 did_something = 1;
1683 }
1684 if (runPowerDropout)
1685 {
1686 Jim_Eval(interp, "power_dropout");
1687 did_something = 1;
1688 }
1689 if (runPowerRestore)
1690 {
1691 Jim_Eval(interp, "power_restore");
1692 did_something = 1;
1693 }
1694
1695 if (did_something)
1696 {
1697 /* clear detect flags */
1698 sense_handler();
1699 }
1700
1701 /* clear action flags */
1702
1703 runSrstAsserted = 0;
1704 runSrstDeasserted = 0;
1705 runPowerRestore = 0;
1706 runPowerDropout = 0;
1707
1708 recursive = 0;
1709 }
1710
1711 /* Poll targets for state changes unless that's globally disabled.
1712 * Skip targets that are currently disabled.
1713 */
1714 for (target_t *target = all_targets;
1715 target_continuous_poll && target;
1716 target = target->next)
1717 {
1718 if (!target->tap->enabled)
1719 continue;
1720
1721 /* only poll target if we've got power and srst isn't asserted */
1722 if (!powerDropout && !srstAsserted)
1723 {
1724 /* polling may fail silently until the target has been examined */
1725 if ((retval = target_poll(target)) != ERROR_OK)
1726 return retval;
1727 }
1728 }
1729
1730 return retval;
1731 }
1732
1733 static int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1734 {
1735 target_t *target;
1736 reg_t *reg = NULL;
1737 int count = 0;
1738 char *value;
1739
1740 LOG_DEBUG("-");
1741
1742 target = get_current_target(cmd_ctx);
1743
1744 /* list all available registers for the current target */
1745 if (argc == 0)
1746 {
1747 reg_cache_t *cache = target->reg_cache;
1748
1749 count = 0;
1750 while (cache)
1751 {
1752 int i;
1753
1754 for (i = 0, reg = cache->reg_list;
1755 i < cache->num_regs;
1756 i++, reg++, count++)
1757 {
1758 /* only print cached values if they are valid */
1759 if (reg->valid) {
1760 value = buf_to_str(reg->value,
1761 reg->size, 16);
1762 command_print(cmd_ctx,
1763 "(%i) %s (/%u): 0x%s%s",
1764 count, reg->name,
1765 reg->size, value,
1766 reg->dirty
1767 ? " (dirty)"
1768 : "");
1769 free(value);
1770 } else {
1771 command_print(cmd_ctx, "(%i) %s (/%u)",
1772 count, reg->name,
1773 reg->size) ;
1774 }
1775 }
1776 cache = cache->next;
1777 }
1778
1779 return ERROR_OK;
1780 }
1781
1782 /* access a single register by its ordinal number */
1783 if ((args[0][0] >= '0') && (args[0][0] <= '9'))
1784 {
1785 unsigned num;
1786 int retval = parse_uint(args[0], &num);
1787 if (ERROR_OK != retval)
1788 return ERROR_COMMAND_SYNTAX_ERROR;
1789
1790 reg_cache_t *cache = target->reg_cache;
1791 count = 0;
1792 while (cache)
1793 {
1794 int i;
1795 for (i = 0; i < cache->num_regs; i++)
1796 {
1797 if (count++ == (int)num)
1798 {
1799 reg = &cache->reg_list[i];
1800 break;
1801 }
1802 }
1803 if (reg)
1804 break;
1805 cache = cache->next;
1806 }
1807
1808 if (!reg)
1809 {
1810 command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1811 return ERROR_OK;
1812 }
1813 } else /* access a single register by its name */
1814 {
1815 reg = register_get_by_name(target->reg_cache, args[0], 1);
1816
1817 if (!reg)
1818 {
1819 command_print(cmd_ctx, "register %s not found in current target", args[0]);
1820 return ERROR_OK;
1821 }
1822 }
1823
1824 /* display a register */
1825 if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
1826 {
1827 if ((argc == 2) && (strcmp(args[1], "force") == 0))
1828 reg->valid = 0;
1829
1830 if (reg->valid == 0)
1831 {
1832 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1833 arch_type->get(reg);
1834 }
1835 value = buf_to_str(reg->value, reg->size, 16);
1836 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1837 free(value);
1838 return ERROR_OK;
1839 }
1840
1841 /* set register value */
1842 if (argc == 2)
1843 {
1844 uint8_t *buf = malloc(CEIL(reg->size, 8));
1845 str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
1846
1847 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1848 arch_type->set(reg, buf);
1849
1850 value = buf_to_str(reg->value, reg->size, 16);
1851 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1852 free(value);
1853
1854 free(buf);
1855
1856 return ERROR_OK;
1857 }
1858
1859 command_print(cmd_ctx, "usage: reg <#|name> [value]");
1860
1861 return ERROR_OK;
1862 }
1863
1864 static int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1865 {
1866 int retval = ERROR_OK;
1867 target_t *target = get_current_target(cmd_ctx);
1868
1869 if (argc == 0)
1870 {
1871 command_print(cmd_ctx, "background polling: %s",
1872 target_continuous_poll ? "on" : "off");
1873 command_print(cmd_ctx, "TAP: %s (%s)",
1874 target->tap->dotted_name,
1875 target->tap->enabled ? "enabled" : "disabled");
1876 if (!target->tap->enabled)
1877 return ERROR_OK;
1878 if ((retval = target_poll(target)) != ERROR_OK)
1879 return retval;
1880 if ((retval = target_arch_state(target)) != ERROR_OK)
1881 return retval;
1882
1883 }
1884 else if (argc == 1)
1885 {
1886 if (strcmp(args[0], "on") == 0)
1887 {
1888 target_continuous_poll = 1;
1889 }
1890 else if (strcmp(args[0], "off") == 0)
1891 {
1892 target_continuous_poll = 0;
1893 }
1894 else
1895 {
1896 command_print(cmd_ctx, "arg is \"on\" or \"off\"");
1897 }
1898 } else
1899 {
1900 return ERROR_COMMAND_SYNTAX_ERROR;
1901 }
1902
1903 return retval;
1904 }
1905
1906 static int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1907 {
1908 if (argc > 1)
1909 return ERROR_COMMAND_SYNTAX_ERROR;
1910
1911 unsigned ms = 5000;
1912 if (1 == argc)
1913 {
1914 int retval = parse_uint(args[0], &ms);
1915 if (ERROR_OK != retval)
1916 {
1917 command_print(cmd_ctx, "usage: %s [seconds]", cmd);
1918 return ERROR_COMMAND_SYNTAX_ERROR;
1919 }
1920 // convert seconds (given) to milliseconds (needed)
1921 ms *= 1000;
1922 }
1923
1924 target_t *target = get_current_target(cmd_ctx);
1925 return target_wait_state(target, TARGET_HALTED, ms);
1926 }
1927
1928 /* wait for target state to change. The trick here is to have a low
1929 * latency for short waits and not to suck up all the CPU time
1930 * on longer waits.
1931 *
1932 * After 500ms, keep_alive() is invoked
1933 */
1934 int target_wait_state(target_t *target, enum target_state state, int ms)
1935 {
1936 int retval;
1937 long long then = 0, cur;
1938 int once = 1;
1939
1940 for (;;)
1941 {
1942 if ((retval = target_poll(target)) != ERROR_OK)
1943 return retval;
1944 if (target->state == state)
1945 {
1946 break;
1947 }
1948 cur = timeval_ms();
1949 if (once)
1950 {
1951 once = 0;
1952 then = timeval_ms();
1953 LOG_DEBUG("waiting for target %s...",
1954 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
1955 }
1956
1957 if (cur-then > 500)
1958 {
1959 keep_alive();
1960 }
1961
1962 if ((cur-then) > ms)
1963 {
1964 LOG_ERROR("timed out while waiting for target %s",
1965 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
1966 return ERROR_FAIL;
1967 }
1968 }
1969
1970 return ERROR_OK;
1971 }
1972
1973 static int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1974 {
1975 LOG_DEBUG("-");
1976
1977 target_t *target = get_current_target(cmd_ctx);
1978 int retval = target_halt(target);
1979 if (ERROR_OK != retval)
1980 return retval;
1981
1982 if (argc == 1)
1983 {
1984 unsigned wait;
1985 retval = parse_uint(args[0], &wait);
1986 if (ERROR_OK != retval)
1987 return ERROR_COMMAND_SYNTAX_ERROR;
1988 if (!wait)
1989 return ERROR_OK;
1990 }
1991
1992 return handle_wait_halt_command(cmd_ctx, cmd, args, argc);
1993 }
1994
1995 static int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1996 {
1997 target_t *target = get_current_target(cmd_ctx);
1998
1999 LOG_USER("requesting target halt and executing a soft reset");
2000
2001 target->type->soft_reset_halt(target);
2002
2003 return ERROR_OK;
2004 }
2005
2006 static int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2007 {
2008 if (argc > 1)
2009 return ERROR_COMMAND_SYNTAX_ERROR;
2010
2011 enum target_reset_mode reset_mode = RESET_RUN;
2012 if (argc == 1)
2013 {
2014 const Jim_Nvp *n;
2015 n = Jim_Nvp_name2value_simple(nvp_reset_modes, args[0]);
2016 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2017 return ERROR_COMMAND_SYNTAX_ERROR;
2018 }
2019 reset_mode = n->value;
2020 }
2021
2022 /* reset *all* targets */
2023 return target_process_reset(cmd_ctx, reset_mode);
2024 }
2025
2026
2027 static int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2028 {
2029 int current = 1;
2030 if (argc > 1)
2031 return ERROR_COMMAND_SYNTAX_ERROR;
2032
2033 target_t *target = get_current_target(cmd_ctx);
2034 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2035
2036 /* with no args, resume from current pc, addr = 0,
2037 * with one arguments, addr = args[0],
2038 * handle breakpoints, not debugging */
2039 uint32_t addr = 0;
2040 if (argc == 1)
2041 {
2042 int retval = parse_u32(args[0], &addr);
2043 if (ERROR_OK != retval)
2044 return retval;
2045 current = 0;
2046 }
2047
2048 return target_resume(target, current, addr, 1, 0);
2049 }
2050
2051 static int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2052 {
2053 if (argc > 1)
2054 return ERROR_COMMAND_SYNTAX_ERROR;
2055
2056 LOG_DEBUG("-");
2057
2058 /* with no args, step from current pc, addr = 0,
2059 * with one argument addr = args[0],
2060 * handle breakpoints, debugging */
2061 uint32_t addr = 0;
2062 int current_pc = 1;
2063 if (argc == 1)
2064 {
2065 int retval = parse_u32(args[0], &addr);
2066 if (ERROR_OK != retval)
2067 return retval;
2068 current_pc = 0;
2069 }
2070
2071 target_t *target = get_current_target(cmd_ctx);
2072
2073 return target->type->step(target, current_pc, addr, 1);
2074 }
2075
2076 static void handle_md_output(struct command_context_s *cmd_ctx,
2077 struct target_s *target, uint32_t address, unsigned size,
2078 unsigned count, const uint8_t *buffer)
2079 {
2080 const unsigned line_bytecnt = 32;
2081 unsigned line_modulo = line_bytecnt / size;
2082
2083 char output[line_bytecnt * 4 + 1];
2084 unsigned output_len = 0;
2085
2086 const char *value_fmt;
2087 switch (size) {
2088 case 4: value_fmt = "%8.8x "; break;
2089 case 2: value_fmt = "%4.2x "; break;
2090 case 1: value_fmt = "%2.2x "; break;
2091 default:
2092 LOG_ERROR("invalid memory read size: %u", size);
2093 exit(-1);
2094 }
2095
2096 for (unsigned i = 0; i < count; i++)
2097 {
2098 if (i % line_modulo == 0)
2099 {
2100 output_len += snprintf(output + output_len,
2101 sizeof(output) - output_len,
2102 "0x%8.8x: ",
2103 (unsigned)(address + (i*size)));
2104 }
2105
2106 uint32_t value = 0;
2107 const uint8_t *value_ptr = buffer + i * size;
2108 switch (size) {
2109 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2110 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2111 case 1: value = *value_ptr;
2112 }
2113 output_len += snprintf(output + output_len,
2114 sizeof(output) - output_len,
2115 value_fmt, value);
2116
2117 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2118 {
2119 command_print(cmd_ctx, "%s", output);
2120 output_len = 0;
2121 }
2122 }
2123 }
2124
2125 static int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2126 {
2127 if (argc < 1)
2128 return ERROR_COMMAND_SYNTAX_ERROR;
2129
2130 unsigned size = 0;
2131 switch (cmd[2]) {
2132 case 'w': size = 4; break;
2133 case 'h': size = 2; break;
2134 case 'b': size = 1; break;
2135 default: return ERROR_COMMAND_SYNTAX_ERROR;
2136 }
2137
2138 uint32_t address;
2139 int retval = parse_u32(args[0], &address);
2140 if (ERROR_OK != retval)
2141 return retval;
2142
2143 unsigned count = 1;
2144 if (argc == 2)
2145 {
2146 retval = parse_uint(args[1], &count);
2147 if (ERROR_OK != retval)
2148 return retval;
2149 }
2150
2151 uint8_t *buffer = calloc(count, size);
2152
2153 target_t *target = get_current_target(cmd_ctx);
2154 retval = target_read_memory(target,
2155 address, size, count, buffer);
2156 if (ERROR_OK == retval)
2157 handle_md_output(cmd_ctx, target, address, size, count, buffer);
2158
2159 free(buffer);
2160
2161 return retval;
2162 }
2163
2164 static int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2165 {
2166 if ((argc < 2) || (argc > 3))
2167 return ERROR_COMMAND_SYNTAX_ERROR;
2168
2169 uint32_t address;
2170 int retval = parse_u32(args[0], &address);
2171 if (ERROR_OK != retval)
2172 return retval;
2173
2174 uint32_t value;
2175 retval = parse_u32(args[1], &value);
2176 if (ERROR_OK != retval)
2177 return retval;
2178
2179 unsigned count = 1;
2180 if (argc == 3)
2181 {
2182 retval = parse_uint(args[2], &count);
2183 if (ERROR_OK != retval)
2184 return retval;
2185 }
2186
2187 target_t *target = get_current_target(cmd_ctx);
2188 unsigned wordsize;
2189 uint8_t value_buf[4];
2190 switch (cmd[2])
2191 {
2192 case 'w':
2193 wordsize = 4;
2194 target_buffer_set_u32(target, value_buf, value);
2195 break;
2196 case 'h':
2197 wordsize = 2;
2198 target_buffer_set_u16(target, value_buf, value);
2199 break;
2200 case 'b':
2201 wordsize = 1;
2202 value_buf[0] = value;
2203 break;
2204 default:
2205 return ERROR_COMMAND_SYNTAX_ERROR;
2206 }
2207 for (unsigned i = 0; i < count; i++)
2208 {
2209 retval = target_write_memory(target,
2210 address + i * wordsize, wordsize, 1, value_buf);
2211 if (ERROR_OK != retval)
2212 return retval;
2213 keep_alive();
2214 }
2215
2216 return ERROR_OK;
2217
2218 }
2219
2220 static int parse_load_image_command_args(char **args, int argc,
2221 image_t *image, uint32_t *min_address, uint32_t *max_address)
2222 {
2223 if (argc < 1 || argc > 5)
2224 return ERROR_COMMAND_SYNTAX_ERROR;
2225
2226 /* a base address isn't always necessary,
2227 * default to 0x0 (i.e. don't relocate) */
2228 if (argc >= 2)
2229 {
2230 uint32_t addr;
2231 int retval = parse_u32(args[1], &addr);
2232 if (ERROR_OK != retval)
2233 return ERROR_COMMAND_SYNTAX_ERROR;
2234 image->base_address = addr;
2235 image->base_address_set = 1;
2236 }
2237 else
2238 image->base_address_set = 0;
2239
2240 image->start_address_set = 0;
2241
2242 if (argc >= 4)
2243 {
2244 int retval = parse_u32(args[3], min_address);
2245 if (ERROR_OK != retval)
2246 return ERROR_COMMAND_SYNTAX_ERROR;
2247 }
2248 if (argc == 5)
2249 {
2250 int retval = parse_u32(args[4], max_address);
2251 if (ERROR_OK != retval)
2252 return ERROR_COMMAND_SYNTAX_ERROR;
2253 // use size (given) to find max (required)
2254 *max_address += *min_address;
2255 }
2256
2257 if (*min_address > *max_address)
2258 return ERROR_COMMAND_SYNTAX_ERROR;
2259
2260 return ERROR_OK;
2261 }
2262
2263 static int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2264 {
2265 uint8_t *buffer;
2266 uint32_t buf_cnt;
2267 uint32_t image_size;
2268 uint32_t min_address = 0;
2269 uint32_t max_address = 0xffffffff;
2270 int i;
2271 int retvaltemp;
2272
2273 image_t image;
2274
2275 duration_t duration;
2276 char *duration_text;
2277
2278 int retval = parse_load_image_command_args(args, argc,
2279 &image, &min_address, &max_address);
2280 if (ERROR_OK != retval)
2281 return retval;
2282
2283 target_t *target = get_current_target(cmd_ctx);
2284 duration_start_measure(&duration);
2285
2286 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
2287 {
2288 return ERROR_OK;
2289 }
2290
2291 image_size = 0x0;
2292 retval = ERROR_OK;
2293 for (i = 0; i < image.num_sections; i++)
2294 {
2295 buffer = malloc(image.sections[i].size);
2296 if (buffer == NULL)
2297 {
2298 command_print(cmd_ctx,
2299 "error allocating buffer for section (%d bytes)",
2300 (int)(image.sections[i].size));
2301 break;
2302 }
2303
2304 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2305 {
2306 free(buffer);
2307 break;
2308 }
2309
2310 uint32_t offset = 0;
2311 uint32_t length = buf_cnt;
2312
2313 /* DANGER!!! beware of unsigned comparision here!!! */
2314
2315 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2316 (image.sections[i].base_address < max_address))
2317 {
2318 if (image.sections[i].base_address < min_address)
2319 {
2320 /* clip addresses below */
2321 offset += min_address-image.sections[i].base_address;
2322 length -= offset;
2323 }
2324
2325 if (image.sections[i].base_address + buf_cnt > max_address)
2326 {
2327 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2328 }
2329
2330 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2331 {
2332 free(buffer);
2333 break;
2334 }
2335 image_size += length;
2336 command_print(cmd_ctx, "%u byte written at address 0x%8.8" PRIx32 "",
2337 (unsigned int)length,
2338 image.sections[i].base_address + offset);
2339 }
2340
2341 free(buffer);
2342 }
2343
2344 if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2345 {
2346 image_close(&image);
2347 return retvaltemp;
2348 }
2349
2350 if (retval == ERROR_OK)
2351 {
2352 command_print(cmd_ctx, "downloaded %u byte in %s",
2353 (unsigned int)image_size,
2354 duration_text);
2355 }
2356 free(duration_text);
2357
2358 image_close(&image);
2359
2360 return retval;
2361
2362 }
2363
2364 static int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2365 {
2366 fileio_t fileio;
2367
2368 uint8_t buffer[560];
2369 int retvaltemp;
2370
2371 duration_t duration;
2372 char *duration_text;
2373
2374 target_t *target = get_current_target(cmd_ctx);
2375
2376 if (argc != 3)
2377 {
2378 command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
2379 return ERROR_OK;
2380 }
2381
2382 uint32_t address;
2383 int retval = parse_u32(args[1], &address);
2384 if (ERROR_OK != retval)
2385 return retval;
2386
2387 uint32_t size;
2388 retval = parse_u32(args[2], &size);
2389 if (ERROR_OK != retval)
2390 return retval;
2391
2392 if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2393 {
2394 return ERROR_OK;
2395 }
2396
2397 duration_start_measure(&duration);
2398
2399 while (size > 0)
2400 {
2401 uint32_t size_written;
2402 uint32_t this_run_size = (size > 560) ? 560 : size;
2403
2404 retval = target_read_buffer(target, address, this_run_size, buffer);
2405 if (retval != ERROR_OK)
2406 {
2407 break;
2408 }
2409
2410 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2411 if (retval != ERROR_OK)
2412 {
2413 break;
2414 }
2415
2416 size -= this_run_size;
2417 address += this_run_size;
2418 }
2419
2420 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2421 return retvaltemp;
2422
2423 if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2424 return retvaltemp;
2425
2426 if (retval == ERROR_OK)
2427 {
2428 command_print(cmd_ctx, "dumped %lld byte in %s",
2429 fileio.size, duration_text);
2430 free(duration_text);
2431 }
2432
2433 return retval;
2434 }
2435
2436 static int handle_verify_image_command_internal(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, int verify)
2437 {
2438 uint8_t *buffer;
2439 uint32_t buf_cnt;
2440 uint32_t image_size;
2441 int i;
2442 int retval, retvaltemp;
2443 uint32_t checksum = 0;
2444 uint32_t mem_checksum = 0;
2445
2446 image_t image;
2447
2448 duration_t duration;
2449 char *duration_text;
2450
2451 target_t *target = get_current_target(cmd_ctx);
2452
2453 if (argc < 1)
2454 {
2455 return ERROR_COMMAND_SYNTAX_ERROR;
2456 }
2457
2458 if (!target)
2459 {
2460 LOG_ERROR("no target selected");
2461 return ERROR_FAIL;
2462 }
2463
2464 duration_start_measure(&duration);
2465
2466 if (argc >= 2)
2467 {
2468 uint32_t addr;
2469 retval = parse_u32(args[1], &addr);
2470 if (ERROR_OK != retval)
2471 return ERROR_COMMAND_SYNTAX_ERROR;
2472 image.base_address = addr;
2473 image.base_address_set = 1;
2474 }
2475 else
2476 {
2477 image.base_address_set = 0;
2478 image.base_address = 0x0;
2479 }
2480
2481 image.start_address_set = 0;
2482
2483 if ((retval = image_open(&image, args[0], (argc == 3) ? args[2] : NULL)) != ERROR_OK)
2484 {
2485 return retval;
2486 }
2487
2488 image_size = 0x0;
2489 retval = ERROR_OK;
2490 for (i = 0; i < image.num_sections; i++)
2491 {
2492 buffer = malloc(image.sections[i].size);
2493 if (buffer == NULL)
2494 {
2495 command_print(cmd_ctx,
2496 "error allocating buffer for section (%d bytes)",
2497 (int)(image.sections[i].size));
2498 break;
2499 }
2500 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2501 {
2502 free(buffer);
2503 break;
2504 }
2505
2506 if (verify)
2507 {
2508 /* calculate checksum of image */
2509 image_calculate_checksum(buffer, buf_cnt, &checksum);
2510
2511 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2512 if (retval != ERROR_OK)
2513 {
2514 free(buffer);
2515 break;
2516 }
2517
2518 if (checksum != mem_checksum)
2519 {
2520 /* failed crc checksum, fall back to a binary compare */
2521 uint8_t *data;
2522
2523 command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
2524
2525 data = (uint8_t*)malloc(buf_cnt);
2526
2527 /* Can we use 32bit word accesses? */
2528 int size = 1;
2529 int count = buf_cnt;
2530 if ((count % 4) == 0)
2531 {
2532 size *= 4;
2533 count /= 4;
2534 }
2535 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2536 if (retval == ERROR_OK)
2537 {
2538 uint32_t t;
2539 for (t = 0; t < buf_cnt; t++)
2540 {
2541 if (data[t] != buffer[t])
2542 {
2543 command_print(cmd_ctx,
2544 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2545 (unsigned)(t + image.sections[i].base_address),
2546 data[t],
2547 buffer[t]);
2548 free(data);
2549 free(buffer);
2550 retval = ERROR_FAIL;
2551 goto done;
2552 }
2553 if ((t%16384) == 0)
2554 {
2555 keep_alive();
2556 }
2557 }
2558 }
2559
2560 free(data);
2561 }
2562 } else
2563 {
2564 command_print(cmd_ctx, "address 0x%08" PRIx32 " length 0x%08" PRIx32 "",
2565 image.sections[i].base_address,
2566 buf_cnt);
2567 }
2568
2569 free(buffer);
2570 image_size += buf_cnt;
2571 }
2572 done:
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, "verified %u bytes 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 static int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2594 {
2595 return handle_verify_image_command_internal(cmd_ctx, cmd, args, argc, 1);
2596 }
2597
2598 static int handle_test_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2599 {
2600 return handle_verify_image_command_internal(cmd_ctx, cmd, args, argc, 0);
2601 }
2602
2603 static int handle_bp_command_list(struct command_context_s *cmd_ctx)
2604 {
2605 target_t *target = get_current_target(cmd_ctx);
2606 breakpoint_t *breakpoint = target->breakpoints;
2607 while (breakpoint)
2608 {
2609 if (breakpoint->type == BKPT_SOFT)
2610 {
2611 char* buf = buf_to_str(breakpoint->orig_instr,
2612 breakpoint->length, 16);
2613 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2614 breakpoint->address,
2615 breakpoint->length,
2616 breakpoint->set, buf);
2617 free(buf);
2618 }
2619 else
2620 {
2621 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2622 breakpoint->address,
2623 breakpoint->length, breakpoint->set);
2624 }
2625
2626 breakpoint = breakpoint->next;
2627 }
2628 return ERROR_OK;
2629 }
2630
2631 static int handle_bp_command_set(struct command_context_s *cmd_ctx,
2632 uint32_t addr, uint32_t length, int hw)
2633 {
2634 target_t *target = get_current_target(cmd_ctx);
2635 int retval = breakpoint_add(target, addr, length, hw);
2636 if (ERROR_OK == retval)
2637 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2638 else
2639 LOG_ERROR("Failure setting breakpoint");
2640 return retval;
2641 }
2642
2643 static int handle_bp_command(struct command_context_s *cmd_ctx,
2644 char *cmd, char **args, int argc)
2645 {
2646 if (argc == 0)
2647 return handle_bp_command_list(cmd_ctx);
2648
2649 if (argc < 2 || argc > 3)
2650 {
2651 command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
2652 return ERROR_COMMAND_SYNTAX_ERROR;
2653 }
2654
2655 uint32_t addr;
2656 int retval = parse_u32(args[0], &addr);
2657 if (ERROR_OK != retval)
2658 return retval;
2659
2660 uint32_t length;
2661 retval = parse_u32(args[1], &length);
2662 if (ERROR_OK != retval)
2663 return retval;
2664
2665 int hw = BKPT_SOFT;
2666 if (argc == 3)
2667 {
2668 if (strcmp(args[2], "hw") == 0)
2669 hw = BKPT_HARD;
2670 else
2671 return ERROR_COMMAND_SYNTAX_ERROR;
2672 }
2673
2674 return handle_bp_command_set(cmd_ctx, addr, length, hw);
2675 }
2676
2677 static int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2678 {
2679 if (argc != 1)
2680 return ERROR_COMMAND_SYNTAX_ERROR;
2681
2682 uint32_t addr;
2683 int retval = parse_u32(args[0], &addr);
2684 if (ERROR_OK != retval)
2685 return retval;
2686
2687 target_t *target = get_current_target(cmd_ctx);
2688 breakpoint_remove(target, addr);
2689
2690 return ERROR_OK;
2691 }
2692
2693 static int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2694 {
2695 target_t *target = get_current_target(cmd_ctx);
2696
2697 if (argc == 0)
2698 {
2699 watchpoint_t *watchpoint = target->watchpoints;
2700
2701 while (watchpoint)
2702 {
2703 command_print(cmd_ctx,
2704 "address: 0x%8.8" PRIx32 ", len: 0x%8.8x, r/w/a: %i, value: 0x%8.8" PRIx32 ", mask: 0x%8.8" PRIx32 "",
2705 watchpoint->address,
2706 watchpoint->length,
2707 (int)(watchpoint->rw),
2708 watchpoint->value,
2709 watchpoint->mask);
2710 watchpoint = watchpoint->next;
2711 }
2712 return ERROR_OK;
2713 }
2714
2715 enum watchpoint_rw type = WPT_ACCESS;
2716 uint32_t addr = 0;
2717 uint32_t length = 0;
2718 uint32_t data_value = 0x0;
2719 uint32_t data_mask = 0xffffffff;
2720 int retval;
2721
2722 switch (argc)
2723 {
2724 case 5:
2725 retval = parse_u32(args[4], &data_mask);
2726 if (ERROR_OK != retval)
2727 return retval;
2728 // fall through
2729 case 4:
2730 retval = parse_u32(args[3], &data_value);
2731 if (ERROR_OK != retval)
2732 return retval;
2733 // fall through
2734 case 3:
2735 switch (args[2][0])
2736 {
2737 case 'r':
2738 type = WPT_READ;
2739 break;
2740 case 'w':
2741 type = WPT_WRITE;
2742 break;
2743 case 'a':
2744 type = WPT_ACCESS;
2745 break;
2746 default:
2747 LOG_ERROR("invalid watchpoint mode ('%c')", args[2][0]);
2748 return ERROR_COMMAND_SYNTAX_ERROR;
2749 }
2750 // fall through
2751 case 2:
2752 retval = parse_u32(args[1], &length);
2753 if (ERROR_OK != retval)
2754 return retval;
2755 retval = parse_u32(args[0], &addr);
2756 if (ERROR_OK != retval)
2757 return retval;
2758 break;
2759
2760 default:
2761 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2762 return ERROR_COMMAND_SYNTAX_ERROR;
2763 }
2764
2765 retval = watchpoint_add(target, addr, length, type,
2766 data_value, data_mask);
2767 if (ERROR_OK != retval)
2768 LOG_ERROR("Failure setting watchpoints");
2769
2770 return retval;
2771 }
2772
2773 static int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2774 {
2775 if (argc != 1)
2776 return ERROR_COMMAND_SYNTAX_ERROR;
2777
2778 uint32_t addr;
2779 int retval = parse_u32(args[0], &addr);
2780 if (ERROR_OK != retval)
2781 return retval;
2782
2783 target_t *target = get_current_target(cmd_ctx);
2784 watchpoint_remove(target, addr);
2785
2786 return ERROR_OK;
2787 }
2788
2789
2790 /**
2791 * Translate a virtual address to a physical address.
2792 *
2793 * The low-level target implementation must have logged a detailed error
2794 * which is forwarded to telnet/GDB session.
2795 */
2796 static int handle_virt2phys_command(command_context_t *cmd_ctx,
2797 char *cmd, char **args, int argc)
2798 {
2799 if (argc != 1)
2800 return ERROR_COMMAND_SYNTAX_ERROR;
2801
2802 uint32_t va;
2803 int retval = parse_u32(args[0], &va);
2804 if (ERROR_OK != retval)
2805 return retval;
2806 uint32_t pa;
2807
2808 target_t *target = get_current_target(cmd_ctx);
2809 retval = target->type->virt2phys(target, va, &pa);
2810 if (retval == ERROR_OK)
2811 command_print(cmd_ctx, "Physical address 0x%08" PRIx32 "", pa);
2812
2813 return retval;
2814 }
2815
2816 static void writeData(FILE *f, const void *data, size_t len)
2817 {
2818 size_t written = fwrite(data, 1, len, f);
2819 if (written != len)
2820 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
2821 }
2822
2823 static void writeLong(FILE *f, int l)
2824 {
2825 int i;
2826 for (i = 0; i < 4; i++)
2827 {
2828 char c = (l >> (i*8))&0xff;
2829 writeData(f, &c, 1);
2830 }
2831
2832 }
2833
2834 static void writeString(FILE *f, char *s)
2835 {
2836 writeData(f, s, strlen(s));
2837 }
2838
2839 /* Dump a gmon.out histogram file. */
2840 static void writeGmon(uint32_t *samples, uint32_t sampleNum, char *filename)
2841 {
2842 uint32_t i;
2843 FILE *f = fopen(filename, "w");
2844 if (f == NULL)
2845 return;
2846 writeString(f, "gmon");
2847 writeLong(f, 0x00000001); /* Version */
2848 writeLong(f, 0); /* padding */
2849 writeLong(f, 0); /* padding */
2850 writeLong(f, 0); /* padding */
2851
2852 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
2853 writeData(f, &zero, 1);
2854
2855 /* figure out bucket size */
2856 uint32_t min = samples[0];
2857 uint32_t max = samples[0];
2858 for (i = 0; i < sampleNum; i++)
2859 {
2860 if (min > samples[i])
2861 {
2862 min = samples[i];
2863 }
2864 if (max < samples[i])
2865 {
2866 max = samples[i];
2867 }
2868 }
2869
2870 int addressSpace = (max-min + 1);
2871
2872 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
2873 uint32_t length = addressSpace;
2874 if (length > maxBuckets)
2875 {
2876 length = maxBuckets;
2877 }
2878 int *buckets = malloc(sizeof(int)*length);
2879 if (buckets == NULL)
2880 {
2881 fclose(f);
2882 return;
2883 }
2884 memset(buckets, 0, sizeof(int)*length);
2885 for (i = 0; i < sampleNum;i++)
2886 {
2887 uint32_t address = samples[i];
2888 long long a = address-min;
2889 long long b = length-1;
2890 long long c = addressSpace-1;
2891 int index = (a*b)/c; /* danger!!!! int32 overflows */
2892 buckets[index]++;
2893 }
2894
2895 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2896 writeLong(f, min); /* low_pc */
2897 writeLong(f, max); /* high_pc */
2898 writeLong(f, length); /* # of samples */
2899 writeLong(f, 64000000); /* 64MHz */
2900 writeString(f, "seconds");
2901 for (i = 0; i < (15-strlen("seconds")); i++)
2902 writeData(f, &zero, 1);
2903 writeString(f, "s");
2904
2905 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2906
2907 char *data = malloc(2*length);
2908 if (data != NULL)
2909 {
2910 for (i = 0; i < length;i++)
2911 {
2912 int val;
2913 val = buckets[i];
2914 if (val > 65535)
2915 {
2916 val = 65535;
2917 }
2918 data[i*2]=val&0xff;
2919 data[i*2 + 1]=(val >> 8)&0xff;
2920 }
2921 free(buckets);
2922 writeData(f, data, length * 2);
2923 free(data);
2924 } else
2925 {
2926 free(buckets);
2927 }
2928
2929 fclose(f);
2930 }
2931
2932 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2933 static int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2934 {
2935 target_t *target = get_current_target(cmd_ctx);
2936 struct timeval timeout, now;
2937
2938 gettimeofday(&timeout, NULL);
2939 if (argc != 2)
2940 {
2941 return ERROR_COMMAND_SYNTAX_ERROR;
2942 }
2943 unsigned offset;
2944 int retval = parse_uint(args[0], &offset);
2945 if (ERROR_OK != retval)
2946 return retval;
2947
2948 timeval_add_time(&timeout, offset, 0);
2949
2950 command_print(cmd_ctx, "Starting profiling. Halting and resuming the target as often as we can...");
2951
2952 static const int maxSample = 10000;
2953 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
2954 if (samples == NULL)
2955 return ERROR_OK;
2956
2957 int numSamples = 0;
2958 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2959 reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1);
2960
2961 for (;;)
2962 {
2963 target_poll(target);
2964 if (target->state == TARGET_HALTED)
2965 {
2966 uint32_t t=*((uint32_t *)reg->value);
2967 samples[numSamples++]=t;
2968 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2969 target_poll(target);
2970 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2971 } else if (target->state == TARGET_RUNNING)
2972 {
2973 /* We want to quickly sample the PC. */
2974 if ((retval = target_halt(target)) != ERROR_OK)
2975 {
2976 free(samples);
2977 return retval;
2978 }
2979 } else
2980 {
2981 command_print(cmd_ctx, "Target not halted or running");
2982 retval = ERROR_OK;
2983 break;
2984 }
2985 if (retval != ERROR_OK)
2986 {
2987 break;
2988 }
2989
2990 gettimeofday(&now, NULL);
2991 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
2992 {
2993 command_print(cmd_ctx, "Profiling completed. %d samples.", numSamples);
2994 if ((retval = target_poll(target)) != ERROR_OK)
2995 {
2996 free(samples);
2997 return retval;
2998 }
2999 if (target->state == TARGET_HALTED)
3000 {
3001 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3002 }
3003 if ((retval = target_poll(target)) != ERROR_OK)
3004 {
3005 free(samples);
3006 return retval;
3007 }
3008 writeGmon(samples, numSamples, args[1]);
3009 command_print(cmd_ctx, "Wrote %s", args[1]);
3010 break;
3011 }
3012 }
3013 free(samples);
3014
3015 return ERROR_OK;
3016 }
3017
3018 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3019 {
3020 char *namebuf;
3021 Jim_Obj *nameObjPtr, *valObjPtr;
3022 int result;
3023
3024 namebuf = alloc_printf("%s(%d)", varname, idx);
3025 if (!namebuf)
3026 return JIM_ERR;
3027
3028 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3029 valObjPtr = Jim_NewIntObj(interp, val);
3030 if (!nameObjPtr || !valObjPtr)
3031 {
3032 free(namebuf);
3033 return JIM_ERR;
3034 }
3035
3036 Jim_IncrRefCount(nameObjPtr);
3037 Jim_IncrRefCount(valObjPtr);
3038 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3039 Jim_DecrRefCount(interp, nameObjPtr);
3040 Jim_DecrRefCount(interp, valObjPtr);
3041 free(namebuf);
3042 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3043 return result;
3044 }
3045
3046 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3047 {
3048 command_context_t *context;
3049 target_t *target;
3050
3051 context = Jim_GetAssocData(interp, "context");
3052 if (context == NULL)
3053 {
3054 LOG_ERROR("mem2array: no command context");
3055 return JIM_ERR;
3056 }
3057 target = get_current_target(context);
3058 if (target == NULL)
3059 {
3060 LOG_ERROR("mem2array: no current target");
3061 return JIM_ERR;
3062 }
3063
3064 return target_mem2array(interp, target, argc-1, argv + 1);
3065 }
3066
3067 static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
3068 {
3069 long l;
3070 uint32_t width;
3071 int len;
3072 uint32_t addr;
3073 uint32_t count;
3074 uint32_t v;
3075 const char *varname;
3076 uint8_t buffer[4096];
3077 int n, e, retval;
3078 uint32_t i;
3079
3080 /* argv[1] = name of array to receive the data
3081 * argv[2] = desired width
3082 * argv[3] = memory address
3083 * argv[4] = count of times to read
3084 */
3085 if (argc != 4) {
3086 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3087 return JIM_ERR;
3088 }
3089 varname = Jim_GetString(argv[0], &len);
3090 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3091
3092 e = Jim_GetLong(interp, argv[1], &l);
3093 width = l;
3094 if (e != JIM_OK) {
3095 return e;
3096 }
3097
3098 e = Jim_GetLong(interp, argv[2], &l);
3099 addr = l;
3100 if (e != JIM_OK) {
3101 return e;
3102 }
3103 e = Jim_GetLong(interp, argv[3], &l);
3104 len = l;
3105 if (e != JIM_OK) {
3106 return e;
3107 }
3108 switch (width) {
3109 case 8:
3110 width = 1;
3111 break;
3112 case 16:
3113 width = 2;
3114 break;
3115 case 32:
3116 width = 4;
3117 break;
3118 default:
3119 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3120 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3121 return JIM_ERR;
3122 }
3123 if (len == 0) {
3124 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3125 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3126 return JIM_ERR;
3127 }
3128 if ((addr + (len * width)) < addr) {
3129 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3130 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3131 return JIM_ERR;
3132 }
3133 /* absurd transfer size? */
3134 if (len > 65536) {
3135 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3136 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3137 return JIM_ERR;
3138 }
3139
3140 if ((width == 1) ||
3141 ((width == 2) && ((addr & 1) == 0)) ||
3142 ((width == 4) && ((addr & 3) == 0))) {
3143 /* all is well */
3144 } else {
3145 char buf[100];
3146 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3147 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3148 addr,
3149 width);
3150 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3151 return JIM_ERR;
3152 }
3153
3154 /* Transfer loop */
3155
3156 /* index counter */
3157 n = 0;
3158 /* assume ok */
3159 e = JIM_OK;
3160 while (len) {
3161 /* Slurp... in buffer size chunks */
3162
3163 count = len; /* in objects.. */
3164 if (count > (sizeof(buffer)/width)) {
3165 count = (sizeof(buffer)/width);
3166 }
3167
3168 retval = target_read_memory(target, addr, width, count, buffer);
3169 if (retval != ERROR_OK) {
3170 /* BOO !*/
3171 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3172 (unsigned int)addr,
3173 (int)width,
3174 (int)count);
3175 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3176 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3177 e = JIM_ERR;
3178 len = 0;
3179 } else {
3180 v = 0; /* shut up gcc */
3181 for (i = 0 ;i < count ;i++, n++) {
3182 switch (width) {
3183 case 4:
3184 v = target_buffer_get_u32(target, &buffer[i*width]);
3185 break;
3186 case 2:
3187 v = target_buffer_get_u16(target, &buffer[i*width]);
3188 break;
3189 case 1:
3190 v = buffer[i] & 0x0ff;
3191 break;
3192 }
3193 new_int_array_element(interp, varname, n, v);
3194 }
3195 len -= count;
3196 }
3197 }
3198
3199 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3200
3201 return JIM_OK;
3202 }
3203
3204 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3205 {
3206 char *namebuf;
3207 Jim_Obj *nameObjPtr, *valObjPtr;
3208 int result;
3209 long l;
3210
3211 namebuf = alloc_printf("%s(%d)", varname, idx);
3212 if (!namebuf)
3213 return JIM_ERR;
3214
3215 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3216 if (!nameObjPtr)
3217 {
3218 free(namebuf);
3219 return JIM_ERR;
3220 }
3221
3222 Jim_IncrRefCount(nameObjPtr);
3223 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3224 Jim_DecrRefCount(interp, nameObjPtr);
3225 free(namebuf);
3226 if (valObjPtr == NULL)
3227 return JIM_ERR;
3228
3229 result = Jim_GetLong(interp, valObjPtr, &l);
3230 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3231 *val = l;
3232 return result;
3233 }
3234
3235 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3236 {
3237 command_context_t *context;
3238 target_t *target;
3239
3240 context = Jim_GetAssocData(interp, "context");
3241 if (context == NULL) {
3242 LOG_ERROR("array2mem: no command context");
3243 return JIM_ERR;
3244 }
3245 target = get_current_target(context);
3246 if (target == NULL) {
3247 LOG_ERROR("array2mem: no current target");
3248 return JIM_ERR;
3249 }
3250
3251 return target_array2mem(interp,target, argc-1, argv + 1);
3252 }
3253
3254 static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
3255 {
3256 long l;
3257 uint32_t width;
3258 int len;
3259 uint32_t addr;
3260 uint32_t count;
3261 uint32_t v;
3262 const char *varname;
3263 uint8_t buffer[4096];
3264 int n, e, retval;
3265 uint32_t i;
3266
3267 /* argv[1] = name of array to get the data
3268 * argv[2] = desired width
3269 * argv[3] = memory address
3270 * argv[4] = count to write
3271 */
3272 if (argc != 4) {
3273 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3274 return JIM_ERR;
3275 }
3276 varname = Jim_GetString(argv[0], &len);
3277 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3278
3279 e = Jim_GetLong(interp, argv[1], &l);
3280 width = l;
3281 if (e != JIM_OK) {
3282 return e;
3283 }
3284
3285 e = Jim_GetLong(interp, argv[2], &l);
3286 addr = l;
3287 if (e != JIM_OK) {
3288 return e;
3289 }
3290 e = Jim_GetLong(interp, argv[3], &l);
3291 len = l;
3292 if (e != JIM_OK) {
3293 return e;
3294 }
3295 switch (width) {
3296 case 8:
3297 width = 1;
3298 break;
3299 case 16:
3300 width = 2;
3301 break;
3302 case 32:
3303 width = 4;
3304 break;
3305 default:
3306 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3307 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3308 return JIM_ERR;
3309 }
3310 if (len == 0) {
3311 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3312 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3313 return JIM_ERR;
3314 }
3315 if ((addr + (len * width)) < addr) {
3316 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3317 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3318 return JIM_ERR;
3319 }
3320 /* absurd transfer size? */
3321 if (len > 65536) {
3322 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3323 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3324 return JIM_ERR;
3325 }
3326
3327 if ((width == 1) ||
3328 ((width == 2) && ((addr & 1) == 0)) ||
3329 ((width == 4) && ((addr & 3) == 0))) {
3330 /* all is well */
3331 } else {
3332 char buf[100];
3333 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3334 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3335 (unsigned int)addr,
3336 (int)width);
3337 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3338 return JIM_ERR;
3339 }
3340
3341 /* Transfer loop */
3342
3343 /* index counter */
3344 n = 0;
3345 /* assume ok */
3346 e = JIM_OK;
3347 while (len) {
3348 /* Slurp... in buffer size chunks */
3349
3350 count = len; /* in objects.. */
3351 if (count > (sizeof(buffer)/width)) {
3352 count = (sizeof(buffer)/width);
3353 }
3354
3355 v = 0; /* shut up gcc */
3356 for (i = 0 ;i < count ;i++, n++) {
3357 get_int_array_element(interp, varname, n, &v);
3358 switch (width) {
3359 case 4:
3360 target_buffer_set_u32(target, &buffer[i*width], v);
3361 break;
3362 case 2:
3363 target_buffer_set_u16(target, &buffer[i*width], v);
3364 break;
3365 case 1:
3366 buffer[i] = v & 0x0ff;
3367 break;
3368 }
3369 }
3370 len -= count;
3371
3372 retval = target_write_memory(target, addr, width, count, buffer);
3373 if (retval != ERROR_OK) {
3374 /* BOO !*/
3375 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3376 (unsigned int)addr,
3377 (int)width,
3378 (int)count);
3379 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3380 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3381 e = JIM_ERR;
3382 len = 0;
3383 }
3384 }
3385
3386 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3387
3388 return JIM_OK;
3389 }
3390
3391 void target_all_handle_event(enum target_event e)
3392 {
3393 target_t *target;
3394
3395 LOG_DEBUG("**all*targets: event: %d, %s",
3396 (int)e,
3397 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3398
3399 target = all_targets;
3400 while (target) {
3401 target_handle_event(target, e);
3402 target = target->next;
3403 }
3404 }
3405
3406 void target_handle_event(target_t *target, enum target_event e)
3407 {
3408 target_event_action_t *teap;
3409 int done;
3410
3411 teap = target->event_action;
3412
3413 done = 0;
3414 while (teap) {
3415 if (teap->event == e) {
3416 done = 1;
3417 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3418 target->target_number,
3419 target->cmd_name,
3420 target_get_name(target),
3421 e,
3422 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3423 Jim_GetString(teap->body, NULL));
3424 if (Jim_EvalObj(interp, teap->body) != JIM_OK)
3425 {
3426 Jim_PrintErrorMessage(interp);
3427 }
3428 }
3429 teap = teap->next;
3430 }
3431 if (!done) {
3432 LOG_DEBUG("event: %d %s - no action",
3433 e,
3434 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3435 }
3436 }
3437
3438 enum target_cfg_param {
3439 TCFG_TYPE,
3440 TCFG_EVENT,
3441 TCFG_WORK_AREA_VIRT,
3442 TCFG_WORK_AREA_PHYS,
3443 TCFG_WORK_AREA_SIZE,
3444 TCFG_WORK_AREA_BACKUP,
3445 TCFG_ENDIAN,
3446 TCFG_VARIANT,
3447 TCFG_CHAIN_POSITION,
3448 };
3449
3450 static Jim_Nvp nvp_config_opts[] = {
3451 { .name = "-type", .value = TCFG_TYPE },
3452 { .name = "-event", .value = TCFG_EVENT },
3453 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3454 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3455 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3456 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3457 { .name = "-endian" , .value = TCFG_ENDIAN },
3458 { .name = "-variant", .value = TCFG_VARIANT },
3459 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3460
3461 { .name = NULL, .value = -1 }
3462 };
3463
3464 static int target_configure(Jim_GetOptInfo *goi, target_t *target)
3465 {
3466 Jim_Nvp *n;
3467 Jim_Obj *o;
3468 jim_wide w;
3469 char *cp;
3470 int e;
3471
3472 /* parse config or cget options ... */
3473 while (goi->argc > 0) {
3474 Jim_SetEmptyResult(goi->interp);
3475 /* Jim_GetOpt_Debug(goi); */
3476
3477 if (target->type->target_jim_configure) {
3478 /* target defines a configure function */
3479 /* target gets first dibs on parameters */
3480 e = (*(target->type->target_jim_configure))(target, goi);
3481 if (e == JIM_OK) {
3482 /* more? */
3483 continue;
3484 }
3485 if (e == JIM_ERR) {
3486 /* An error */
3487 return e;
3488 }
3489 /* otherwise we 'continue' below */
3490 }
3491 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3492 if (e != JIM_OK) {
3493 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3494 return e;
3495 }
3496 switch (n->value) {
3497 case TCFG_TYPE:
3498 /* not setable */
3499 if (goi->isconfigure) {
3500 Jim_SetResult_sprintf(goi->interp, "not setable: %s", n->name);
3501 return JIM_ERR;
3502 } else {
3503 no_params:
3504 if (goi->argc != 0) {
3505 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "NO PARAMS");
3506 return JIM_ERR;
3507 }
3508 }
3509 Jim_SetResultString(goi->interp, target_get_name(target), -1);
3510 /* loop for more */
3511 break;
3512 case TCFG_EVENT:
3513 if (goi->argc == 0) {
3514 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3515 return JIM_ERR;
3516 }
3517
3518 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3519 if (e != JIM_OK) {
3520 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3521 return e;
3522 }
3523
3524 if (goi->isconfigure) {
3525 if (goi->argc != 1) {
3526 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3527 return JIM_ERR;
3528 }
3529 } else {
3530 if (goi->argc != 0) {
3531 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3532 return JIM_ERR;
3533 }
3534 }
3535
3536 {
3537 target_event_action_t *teap;
3538
3539 teap = target->event_action;
3540 /* replace existing? */
3541 while (teap) {
3542 if (teap->event == (enum target_event)n->value) {
3543 break;
3544 }
3545 teap = teap->next;
3546 }
3547
3548 if (goi->isconfigure) {
3549 if (teap == NULL) {
3550 /* create new */
3551 teap = calloc(1, sizeof(*teap));
3552 }
3553 teap->event = n->value;
3554 Jim_GetOpt_Obj(goi, &o);
3555 if (teap->body) {
3556 Jim_DecrRefCount(interp, teap->body);
3557 }
3558 teap->body = Jim_DuplicateObj(goi->interp, o);
3559 /*
3560 * FIXME:
3561 * Tcl/TK - "tk events" have a nice feature.
3562 * See the "BIND" command.
3563 * We should support that here.
3564 * You can specify %X and %Y in the event code.
3565 * The idea is: %T - target name.
3566 * The idea is: %N - target number
3567 * The idea is: %E - event name.
3568 */
3569 Jim_IncrRefCount(teap->body);
3570
3571 /* add to head of event list */
3572 teap->next = target->event_action;
3573 target->event_action = teap;
3574 Jim_SetEmptyResult(goi->interp);
3575 } else {
3576 /* get */
3577 if (teap == NULL) {
3578 Jim_SetEmptyResult(goi->interp);
3579 } else {
3580 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3581 }
3582 }
3583 }
3584 /* loop for more */
3585 break;
3586
3587 case TCFG_WORK_AREA_VIRT:
3588 if (goi->isconfigure) {
3589 target_free_all_working_areas(target);
3590 e = Jim_GetOpt_Wide(goi, &w);
3591 if (e != JIM_OK) {
3592 return e;
3593 }
3594 target->working_area_virt = w;
3595 } else {
3596 if (goi->argc != 0) {
3597 goto no_params;
3598 }
3599 }
3600 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3601 /* loop for more */
3602 break;
3603
3604 case TCFG_WORK_AREA_PHYS:
3605 if (goi->isconfigure) {