wait up to 1 second for halted state upon reset init/halt.
[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 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "replacements.h"
28 #include "target.h"
29 #include "target_request.h"
30
31 #include "log.h"
32 #include "configuration.h"
33 #include "binarybuffer.h"
34 #include "jtag.h"
35
36 #include <string.h>
37 #include <stdlib.h>
38 #include <inttypes.h>
39
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <unistd.h>
43 #include <errno.h>
44
45 #include <sys/time.h>
46 #include <time.h>
47
48 #include <time_support.h>
49
50 #include <fileio.h>
51 #include <image.h>
52
53 int cli_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv);
54
55 int handle_target_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
56 int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
57
58 int handle_working_area_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
59
60 int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
61 int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
62 int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
63 int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
64 int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
65 int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
66 int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
67 int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
68 int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
69 int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
70 int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
71 int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
72 int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
73 int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
74 int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
75 int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
76 int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
77 int handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc);
78 int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
79 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
80 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
81
82
83 /* targets */
84 extern target_type_t arm7tdmi_target;
85 extern target_type_t arm720t_target;
86 extern target_type_t arm9tdmi_target;
87 extern target_type_t arm920t_target;
88 extern target_type_t arm966e_target;
89 extern target_type_t arm926ejs_target;
90 extern target_type_t feroceon_target;
91 extern target_type_t xscale_target;
92 extern target_type_t cortexm3_target;
93 extern target_type_t arm11_target;
94 extern target_type_t mips_m4k_target;
95
96 target_type_t *target_types[] =
97 {
98 &arm7tdmi_target,
99 &arm9tdmi_target,
100 &arm920t_target,
101 &arm720t_target,
102 &arm966e_target,
103 &arm926ejs_target,
104 &feroceon_target,
105 &xscale_target,
106 &cortexm3_target,
107 &arm11_target,
108 &mips_m4k_target,
109 NULL,
110 };
111
112 target_t *targets = NULL;
113 target_event_callback_t *target_event_callbacks = NULL;
114 target_timer_callback_t *target_timer_callbacks = NULL;
115
116 char *target_state_strings[] =
117 {
118 "unknown",
119 "running",
120 "halted",
121 "reset",
122 "debug_running",
123 };
124
125 char *target_debug_reason_strings[] =
126 {
127 "debug request", "breakpoint", "watchpoint",
128 "watchpoint and breakpoint", "single step",
129 "target not halted", "undefined"
130 };
131
132 char *target_endianess_strings[] =
133 {
134 "big endian",
135 "little endian",
136 };
137
138 static int target_continous_poll = 1;
139
140 /* read a u32 from a buffer in target memory endianness */
141 u32 target_buffer_get_u32(target_t *target, u8 *buffer)
142 {
143 if (target->endianness == TARGET_LITTLE_ENDIAN)
144 return le_to_h_u32(buffer);
145 else
146 return be_to_h_u32(buffer);
147 }
148
149 /* read a u16 from a buffer in target memory endianness */
150 u16 target_buffer_get_u16(target_t *target, u8 *buffer)
151 {
152 if (target->endianness == TARGET_LITTLE_ENDIAN)
153 return le_to_h_u16(buffer);
154 else
155 return be_to_h_u16(buffer);
156 }
157
158 /* write a u32 to a buffer in target memory endianness */
159 void target_buffer_set_u32(target_t *target, u8 *buffer, u32 value)
160 {
161 if (target->endianness == TARGET_LITTLE_ENDIAN)
162 h_u32_to_le(buffer, value);
163 else
164 h_u32_to_be(buffer, value);
165 }
166
167 /* write a u16 to a buffer in target memory endianness */
168 void target_buffer_set_u16(target_t *target, u8 *buffer, u16 value)
169 {
170 if (target->endianness == TARGET_LITTLE_ENDIAN)
171 h_u16_to_le(buffer, value);
172 else
173 h_u16_to_be(buffer, value);
174 }
175
176 /* returns a pointer to the n-th configured target */
177 target_t* get_target_by_num(int num)
178 {
179 target_t *target = targets;
180 int i = 0;
181
182 while (target)
183 {
184 if (num == i)
185 return target;
186 target = target->next;
187 i++;
188 }
189
190 return NULL;
191 }
192
193 int get_num_by_target(target_t *query_target)
194 {
195 target_t *target = targets;
196 int i = 0;
197
198 while (target)
199 {
200 if (target == query_target)
201 return i;
202 target = target->next;
203 i++;
204 }
205
206 return -1;
207 }
208
209 target_t* get_current_target(command_context_t *cmd_ctx)
210 {
211 target_t *target = get_target_by_num(cmd_ctx->current_target);
212
213 if (target == NULL)
214 {
215 LOG_ERROR("BUG: current_target out of bounds");
216 exit(-1);
217 }
218
219 return target;
220 }
221
222
223 int target_poll(struct target_s *target)
224 {
225 /* We can't poll until after examine */
226 if (!target->type->examined)
227 {
228 /* Fail silently lest we pollute the log */
229 return ERROR_FAIL;
230 }
231 return target->type->poll(target);
232 }
233
234 int target_halt(struct target_s *target)
235 {
236 /* We can't poll until after examine */
237 if (!target->type->examined)
238 {
239 LOG_ERROR("Target not examined yet");
240 return ERROR_FAIL;
241 }
242 return target->type->halt(target);
243 }
244
245 int target_resume(struct target_s *target, int current, u32 address, int handle_breakpoints, int debug_execution)
246 {
247 int retval;
248
249 /* We can't poll until after examine */
250 if (!target->type->examined)
251 {
252 LOG_ERROR("Target not examined yet");
253 return ERROR_FAIL;
254 }
255
256 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
257 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
258 * the application.
259 */
260 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
261 return retval;
262
263 return retval;
264 }
265
266 int target_process_reset(struct command_context_s *cmd_ctx, enum target_reset_mode reset_mode)
267 {
268 int retval = ERROR_OK;
269 target_t *target;
270
271 target = targets;
272 while (target)
273 {
274 target_invoke_script(cmd_ctx, target, "pre_reset");
275 target = target->next;
276 }
277
278 if ((retval = jtag_init_reset(cmd_ctx)) != ERROR_OK)
279 return retval;
280
281 keep_alive(); /* we might be running on a very slow JTAG clk */
282
283 /* First time this is executed after launching OpenOCD, it will read out
284 * the type of CPU, etc. and init Embedded ICE registers in host
285 * memory.
286 *
287 * It will also set up ICE registers in the target.
288 *
289 * However, if we assert TRST later, we need to set up the registers again.
290 *
291 * For the "reset halt/init" case we must only set up the registers here.
292 */
293 if ((retval = target_examine(cmd_ctx)) != ERROR_OK)
294 return retval;
295
296 keep_alive(); /* we might be running on a very slow JTAG clk */
297
298 target = targets;
299 while (target)
300 {
301 /* we have no idea what state the target is in, so we
302 * have to drop working areas
303 */
304 target_free_all_working_areas_restore(target, 0);
305 target->reset_halt=((reset_mode==RESET_HALT)||(reset_mode==RESET_INIT));
306 target->type->assert_reset(target);
307 target = target->next;
308 }
309 if ((retval = jtag_execute_queue()) != ERROR_OK)
310 {
311 LOG_WARNING("JTAG communication failed asserting reset.");
312 retval = ERROR_OK;
313 }
314
315 /* request target halt if necessary, and schedule further action */
316 target = targets;
317 while (target)
318 {
319 if (reset_mode!=RESET_RUN)
320 {
321 if ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)
322 target_halt(target);
323 }
324 target = target->next;
325 }
326
327 if ((retval = jtag_execute_queue()) != ERROR_OK)
328 {
329 LOG_WARNING("JTAG communication failed while reset was asserted. Consider using srst_only for reset_config.");
330 retval = ERROR_OK;
331 }
332
333 target = targets;
334 while (target)
335 {
336 target->type->deassert_reset(target);
337 /* We can fail to bring the target into the halted state */
338 if (target->reset_halt)
339 {
340 /* wait up to 1 second for halt. */
341 target_wait_state(target, TARGET_HALTED, 1000);
342 if (target->state != TARGET_HALTED)
343 {
344 LOG_WARNING("Failed to reset target into halted mode - issuing halt");
345 target->type->halt(target);
346 }
347 }
348
349 target = target->next;
350 }
351
352 if ((retval = jtag_execute_queue()) != ERROR_OK)
353 {
354 LOG_WARNING("JTAG communication failed while deasserting reset.");
355 retval = ERROR_OK;
356 }
357
358 if (jtag_reset_config & RESET_SRST_PULLS_TRST)
359 {
360 /* If TRST was asserted we need to set up registers again */
361 if ((retval = target_examine(cmd_ctx)) != ERROR_OK)
362 return retval;
363 }
364
365 LOG_DEBUG("Waiting for halted stated as appropriate");
366
367 if ((reset_mode == RESET_HALT) || (reset_mode == RESET_INIT))
368 {
369 target = targets;
370 while (target)
371 {
372 /* Wait for reset to complete, maximum 5 seconds. */
373 if (((retval=target_wait_state(target, TARGET_HALTED, 5000)))==ERROR_OK)
374 {
375 if (reset_mode == RESET_INIT)
376 target_invoke_script(cmd_ctx, target, "post_reset");
377 }
378 target = target->next;
379 }
380 }
381
382 /* We want any events to be processed before the prompt */
383 target_call_timer_callbacks_now();
384
385 return retval;
386 }
387
388 static int default_virt2phys(struct target_s *target, u32 virtual, u32 *physical)
389 {
390 *physical = virtual;
391 return ERROR_OK;
392 }
393
394 static int default_mmu(struct target_s *target, int *enabled)
395 {
396 *enabled = 0;
397 return ERROR_OK;
398 }
399
400 static int default_examine(struct command_context_s *cmd_ctx, struct target_s *target)
401 {
402 target->type->examined = 1;
403 return ERROR_OK;
404 }
405
406
407 /* Targets that correctly implement init+examine, i.e.
408 * no communication with target during init:
409 *
410 * XScale
411 */
412 int target_examine(struct command_context_s *cmd_ctx)
413 {
414 int retval = ERROR_OK;
415 target_t *target = targets;
416 while (target)
417 {
418 if ((retval = target->type->examine(cmd_ctx, target))!=ERROR_OK)
419 return retval;
420 target = target->next;
421 }
422 return retval;
423 }
424
425 static int target_write_memory_imp(struct target_s *target, u32 address, u32 size, u32 count, u8 *buffer)
426 {
427 if (!target->type->examined)
428 {
429 LOG_ERROR("Target not examined yet");
430 return ERROR_FAIL;
431 }
432 return target->type->write_memory_imp(target, address, size, count, buffer);
433 }
434
435 static int target_read_memory_imp(struct target_s *target, u32 address, u32 size, u32 count, u8 *buffer)
436 {
437 if (!target->type->examined)
438 {
439 LOG_ERROR("Target not examined yet");
440 return ERROR_FAIL;
441 }
442 return target->type->read_memory_imp(target, address, size, count, buffer);
443 }
444
445 static int target_soft_reset_halt_imp(struct target_s *target)
446 {
447 if (!target->type->examined)
448 {
449 LOG_ERROR("Target not examined yet");
450 return ERROR_FAIL;
451 }
452 return target->type->soft_reset_halt_imp(target);
453 }
454
455 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, u32 entry_point, u32 exit_point, int timeout_ms, void *arch_info)
456 {
457 if (!target->type->examined)
458 {
459 LOG_ERROR("Target not examined yet");
460 return ERROR_FAIL;
461 }
462 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);
463 }
464
465 int target_init(struct command_context_s *cmd_ctx)
466 {
467 target_t *target = targets;
468
469 while (target)
470 {
471 target->type->examined = 0;
472 if (target->type->examine == NULL)
473 {
474 target->type->examine = default_examine;
475 }
476
477 if (target->type->init_target(cmd_ctx, target) != ERROR_OK)
478 {
479 LOG_ERROR("target '%s' init failed", target->type->name);
480 exit(-1);
481 }
482
483 /* Set up default functions if none are provided by target */
484 if (target->type->virt2phys == NULL)
485 {
486 target->type->virt2phys = default_virt2phys;
487 }
488 target->type->virt2phys = default_virt2phys;
489 /* a non-invasive way(in terms of patches) to add some code that
490 * runs before the type->write/read_memory implementation
491 */
492 target->type->write_memory_imp = target->type->write_memory;
493 target->type->write_memory = target_write_memory_imp;
494 target->type->read_memory_imp = target->type->read_memory;
495 target->type->read_memory = target_read_memory_imp;
496 target->type->soft_reset_halt_imp = target->type->soft_reset_halt;
497 target->type->soft_reset_halt = target_soft_reset_halt_imp;
498 target->type->run_algorithm_imp = target->type->run_algorithm;
499 target->type->run_algorithm = target_run_algorithm_imp;
500
501
502 if (target->type->mmu == NULL)
503 {
504 target->type->mmu = default_mmu;
505 }
506 target = target->next;
507 }
508
509 if (targets)
510 {
511 target_register_user_commands(cmd_ctx);
512 target_register_timer_callback(handle_target, 100, 1, NULL);
513 }
514
515 return ERROR_OK;
516 }
517
518 int target_register_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
519 {
520 target_event_callback_t **callbacks_p = &target_event_callbacks;
521
522 if (callback == NULL)
523 {
524 return ERROR_INVALID_ARGUMENTS;
525 }
526
527 if (*callbacks_p)
528 {
529 while ((*callbacks_p)->next)
530 callbacks_p = &((*callbacks_p)->next);
531 callbacks_p = &((*callbacks_p)->next);
532 }
533
534 (*callbacks_p) = malloc(sizeof(target_event_callback_t));
535 (*callbacks_p)->callback = callback;
536 (*callbacks_p)->priv = priv;
537 (*callbacks_p)->next = NULL;
538
539 return ERROR_OK;
540 }
541
542 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
543 {
544 target_timer_callback_t **callbacks_p = &target_timer_callbacks;
545 struct timeval now;
546
547 if (callback == NULL)
548 {
549 return ERROR_INVALID_ARGUMENTS;
550 }
551
552 if (*callbacks_p)
553 {
554 while ((*callbacks_p)->next)
555 callbacks_p = &((*callbacks_p)->next);
556 callbacks_p = &((*callbacks_p)->next);
557 }
558
559 (*callbacks_p) = malloc(sizeof(target_timer_callback_t));
560 (*callbacks_p)->callback = callback;
561 (*callbacks_p)->periodic = periodic;
562 (*callbacks_p)->time_ms = time_ms;
563
564 gettimeofday(&now, NULL);
565 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
566 time_ms -= (time_ms % 1000);
567 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
568 if ((*callbacks_p)->when.tv_usec > 1000000)
569 {
570 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
571 (*callbacks_p)->when.tv_sec += 1;
572 }
573
574 (*callbacks_p)->priv = priv;
575 (*callbacks_p)->next = NULL;
576
577 return ERROR_OK;
578 }
579
580 int target_unregister_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
581 {
582 target_event_callback_t **p = &target_event_callbacks;
583 target_event_callback_t *c = target_event_callbacks;
584
585 if (callback == NULL)
586 {
587 return ERROR_INVALID_ARGUMENTS;
588 }
589
590 while (c)
591 {
592 target_event_callback_t *next = c->next;
593 if ((c->callback == callback) && (c->priv == priv))
594 {
595 *p = next;
596 free(c);
597 return ERROR_OK;
598 }
599 else
600 p = &(c->next);
601 c = next;
602 }
603
604 return ERROR_OK;
605 }
606
607 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
608 {
609 target_timer_callback_t **p = &target_timer_callbacks;
610 target_timer_callback_t *c = target_timer_callbacks;
611
612 if (callback == NULL)
613 {
614 return ERROR_INVALID_ARGUMENTS;
615 }
616
617 while (c)
618 {
619 target_timer_callback_t *next = c->next;
620 if ((c->callback == callback) && (c->priv == priv))
621 {
622 *p = next;
623 free(c);
624 return ERROR_OK;
625 }
626 else
627 p = &(c->next);
628 c = next;
629 }
630
631 return ERROR_OK;
632 }
633
634 int target_call_event_callbacks(target_t *target, enum target_event event)
635 {
636 target_event_callback_t *callback = target_event_callbacks;
637 target_event_callback_t *next_callback;
638
639 LOG_DEBUG("target event %i", event);
640
641 while (callback)
642 {
643 next_callback = callback->next;
644 callback->callback(target, event, callback->priv);
645 callback = next_callback;
646 }
647
648 return ERROR_OK;
649 }
650
651 static int target_call_timer_callbacks_check_time(int checktime)
652 {
653 target_timer_callback_t *callback = target_timer_callbacks;
654 target_timer_callback_t *next_callback;
655 struct timeval now;
656
657 keep_alive();
658
659 gettimeofday(&now, NULL);
660
661 while (callback)
662 {
663 next_callback = callback->next;
664
665 if ((!checktime&&callback->periodic)||
666 (((now.tv_sec >= callback->when.tv_sec) && (now.tv_usec >= callback->when.tv_usec))
667 || (now.tv_sec > callback->when.tv_sec)))
668 {
669 if(callback->callback != NULL)
670 {
671 callback->callback(callback->priv);
672 if (callback->periodic)
673 {
674 int time_ms = callback->time_ms;
675 callback->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
676 time_ms -= (time_ms % 1000);
677 callback->when.tv_sec = now.tv_sec + time_ms / 1000;
678 if (callback->when.tv_usec > 1000000)
679 {
680 callback->when.tv_usec = callback->when.tv_usec - 1000000;
681 callback->when.tv_sec += 1;
682 }
683 }
684 else
685 target_unregister_timer_callback(callback->callback, callback->priv);
686 }
687 }
688
689 callback = next_callback;
690 }
691
692 return ERROR_OK;
693 }
694
695 int target_call_timer_callbacks()
696 {
697 return target_call_timer_callbacks_check_time(1);
698 }
699
700 /* invoke periodic callbacks immediately */
701 int target_call_timer_callbacks_now()
702 {
703 return target_call_timer_callbacks(0);
704 }
705
706 int target_alloc_working_area(struct target_s *target, u32 size, working_area_t **area)
707 {
708 working_area_t *c = target->working_areas;
709 working_area_t *new_wa = NULL;
710
711 /* Reevaluate working area address based on MMU state*/
712 if (target->working_areas == NULL)
713 {
714 int retval;
715 int enabled;
716 retval = target->type->mmu(target, &enabled);
717 if (retval != ERROR_OK)
718 {
719 return retval;
720 }
721 if (enabled)
722 {
723 target->working_area = target->working_area_virt;
724 }
725 else
726 {
727 target->working_area = target->working_area_phys;
728 }
729 }
730
731 /* only allocate multiples of 4 byte */
732 if (size % 4)
733 {
734 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes, padding");
735 size = CEIL(size, 4);
736 }
737
738 /* see if there's already a matching working area */
739 while (c)
740 {
741 if ((c->free) && (c->size == size))
742 {
743 new_wa = c;
744 break;
745 }
746 c = c->next;
747 }
748
749 /* if not, allocate a new one */
750 if (!new_wa)
751 {
752 working_area_t **p = &target->working_areas;
753 u32 first_free = target->working_area;
754 u32 free_size = target->working_area_size;
755
756 LOG_DEBUG("allocating new working area");
757
758 c = target->working_areas;
759 while (c)
760 {
761 first_free += c->size;
762 free_size -= c->size;
763 p = &c->next;
764 c = c->next;
765 }
766
767 if (free_size < size)
768 {
769 LOG_WARNING("not enough working area available(requested %d, free %d)", size, free_size);
770 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
771 }
772
773 new_wa = malloc(sizeof(working_area_t));
774 new_wa->next = NULL;
775 new_wa->size = size;
776 new_wa->address = first_free;
777
778 if (target->backup_working_area)
779 {
780 new_wa->backup = malloc(new_wa->size);
781 target->type->read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup);
782 }
783 else
784 {
785 new_wa->backup = NULL;
786 }
787
788 /* put new entry in list */
789 *p = new_wa;
790 }
791
792 /* mark as used, and return the new (reused) area */
793 new_wa->free = 0;
794 *area = new_wa;
795
796 /* user pointer */
797 new_wa->user = area;
798
799 return ERROR_OK;
800 }
801
802 int target_free_working_area_restore(struct target_s *target, working_area_t *area, int restore)
803 {
804 if (area->free)
805 return ERROR_OK;
806
807 if (restore&&target->backup_working_area)
808 target->type->write_memory(target, area->address, 4, area->size / 4, area->backup);
809
810 area->free = 1;
811
812 /* mark user pointer invalid */
813 *area->user = NULL;
814 area->user = NULL;
815
816 return ERROR_OK;
817 }
818
819 int target_free_working_area(struct target_s *target, working_area_t *area)
820 {
821 return target_free_working_area_restore(target, area, 1);
822 }
823
824 int target_free_all_working_areas_restore(struct target_s *target, int restore)
825 {
826 working_area_t *c = target->working_areas;
827
828 while (c)
829 {
830 working_area_t *next = c->next;
831 target_free_working_area_restore(target, c, restore);
832
833 if (c->backup)
834 free(c->backup);
835
836 free(c);
837
838 c = next;
839 }
840
841 target->working_areas = NULL;
842
843 return ERROR_OK;
844 }
845
846 int target_free_all_working_areas(struct target_s *target)
847 {
848 return target_free_all_working_areas_restore(target, 1);
849 }
850
851 int target_register_commands(struct command_context_s *cmd_ctx)
852 {
853 register_command(cmd_ctx, NULL, "target", handle_target_command, COMMAND_CONFIG, "target <cpu> [reset_init default - DEPRECATED] <chainpos> <endianness> <variant> [cpu type specifc args]");
854 register_command(cmd_ctx, NULL, "targets", handle_targets_command, COMMAND_EXEC, NULL);
855 register_command(cmd_ctx, NULL, "working_area", handle_working_area_command, COMMAND_ANY, "working_area <target#> <address> <size> <'backup'|'nobackup'> [virtual address]");
856 register_command(cmd_ctx, NULL, "virt2phys", handle_virt2phys_command, COMMAND_ANY, "virt2phys <virtual address>");
857 register_command(cmd_ctx, NULL, "profile", handle_profile_command, COMMAND_EXEC, "PRELIMINARY! - profile <seconds> <gmon.out>");
858
859
860 /* script procedures */
861 register_jim(cmd_ctx, "ocd_mem2array", jim_mem2array, "read memory and return as a TCL array for script processing");
862 register_jim(cmd_ctx, "ocd_array2mem", jim_array2mem, "convert a TCL array to memory locations and write the values");
863 return ERROR_OK;
864 }
865
866 int target_arch_state(struct target_s *target)
867 {
868 int retval;
869 if (target==NULL)
870 {
871 LOG_USER("No target has been configured");
872 return ERROR_OK;
873 }
874
875 LOG_USER("target state: %s", target_state_strings[target->state]);
876
877 if (target->state!=TARGET_HALTED)
878 return ERROR_OK;
879
880 retval=target->type->arch_state(target);
881 return retval;
882 }
883
884 /* Single aligned words are guaranteed to use 16 or 32 bit access
885 * mode respectively, otherwise data is handled as quickly as
886 * possible
887 */
888 int target_write_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
889 {
890 int retval;
891 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x", size, address);
892
893 if (!target->type->examined)
894 {
895 LOG_ERROR("Target not examined yet");
896 return ERROR_FAIL;
897 }
898
899 if (address+size<address)
900 {
901 /* GDB can request this when e.g. PC is 0xfffffffc*/
902 LOG_ERROR("address+size wrapped(0x%08x, 0x%08x)", address, size);
903 return ERROR_FAIL;
904 }
905
906 if (((address % 2) == 0) && (size == 2))
907 {
908 return target->type->write_memory(target, address, 2, 1, buffer);
909 }
910
911 /* handle unaligned head bytes */
912 if (address % 4)
913 {
914 int unaligned = 4 - (address % 4);
915
916 if (unaligned > size)
917 unaligned = size;
918
919 if ((retval = target->type->write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
920 return retval;
921
922 buffer += unaligned;
923 address += unaligned;
924 size -= unaligned;
925 }
926
927 /* handle aligned words */
928 if (size >= 4)
929 {
930 int aligned = size - (size % 4);
931
932 /* use bulk writes above a certain limit. This may have to be changed */
933 if (aligned > 128)
934 {
935 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
936 return retval;
937 }
938 else
939 {
940 if ((retval = target->type->write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
941 return retval;
942 }
943
944 buffer += aligned;
945 address += aligned;
946 size -= aligned;
947 }
948
949 /* handle tail writes of less than 4 bytes */
950 if (size > 0)
951 {
952 if ((retval = target->type->write_memory(target, address, 1, size, buffer)) != ERROR_OK)
953 return retval;
954 }
955
956 return ERROR_OK;
957 }
958
959
960 /* Single aligned words are guaranteed to use 16 or 32 bit access
961 * mode respectively, otherwise data is handled as quickly as
962 * possible
963 */
964 int target_read_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
965 {
966 int retval;
967 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x", size, address);
968
969 if (!target->type->examined)
970 {
971 LOG_ERROR("Target not examined yet");
972 return ERROR_FAIL;
973 }
974
975 if (address+size<address)
976 {
977 /* GDB can request this when e.g. PC is 0xfffffffc*/
978 LOG_ERROR("address+size wrapped(0x%08x, 0x%08x)", address, size);
979 return ERROR_FAIL;
980 }
981
982 if (((address % 2) == 0) && (size == 2))
983 {
984 return target->type->read_memory(target, address, 2, 1, buffer);
985 }
986
987 /* handle unaligned head bytes */
988 if (address % 4)
989 {
990 int unaligned = 4 - (address % 4);
991
992 if (unaligned > size)
993 unaligned = size;
994
995 if ((retval = target->type->read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
996 return retval;
997
998 buffer += unaligned;
999 address += unaligned;
1000 size -= unaligned;
1001 }
1002
1003 /* handle aligned words */
1004 if (size >= 4)
1005 {
1006 int aligned = size - (size % 4);
1007
1008 if ((retval = target->type->read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1009 return retval;
1010
1011 buffer += aligned;
1012 address += aligned;
1013 size -= aligned;
1014 }
1015
1016 /* handle tail writes of less than 4 bytes */
1017 if (size > 0)
1018 {
1019 if ((retval = target->type->read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1020 return retval;
1021 }
1022
1023 return ERROR_OK;
1024 }
1025
1026 int target_checksum_memory(struct target_s *target, u32 address, u32 size, u32* crc)
1027 {
1028 u8 *buffer;
1029 int retval;
1030 int i;
1031 u32 checksum = 0;
1032 if (!target->type->examined)
1033 {
1034 LOG_ERROR("Target not examined yet");
1035 return ERROR_FAIL;
1036 }
1037
1038 if ((retval = target->type->checksum_memory(target, address,
1039 size, &checksum)) == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
1040 {
1041 buffer = malloc(size);
1042 if (buffer == NULL)
1043 {
1044 LOG_ERROR("error allocating buffer for section (%d bytes)", size);
1045 return ERROR_INVALID_ARGUMENTS;
1046 }
1047 retval = target_read_buffer(target, address, size, buffer);
1048 if (retval != ERROR_OK)
1049 {
1050 free(buffer);
1051 return retval;
1052 }
1053
1054 /* convert to target endianess */
1055 for (i = 0; i < (size/sizeof(u32)); i++)
1056 {
1057 u32 target_data;
1058 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(u32)]);
1059 target_buffer_set_u32(target, &buffer[i*sizeof(u32)], target_data);
1060 }
1061
1062 retval = image_calculate_checksum( buffer, size, &checksum );
1063 free(buffer);
1064 }
1065
1066 *crc = checksum;
1067
1068 return retval;
1069 }
1070
1071 int target_blank_check_memory(struct target_s *target, u32 address, u32 size, u32* blank)
1072 {
1073 int retval;
1074 if (!target->type->examined)
1075 {
1076 LOG_ERROR("Target not examined yet");
1077 return ERROR_FAIL;
1078 }
1079
1080 if (target->type->blank_check_memory == 0)
1081 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1082
1083 retval = target->type->blank_check_memory(target, address, size, blank);
1084
1085 return retval;
1086 }
1087
1088 int target_read_u32(struct target_s *target, u32 address, u32 *value)
1089 {
1090 u8 value_buf[4];
1091 if (!target->type->examined)
1092 {
1093 LOG_ERROR("Target not examined yet");
1094 return ERROR_FAIL;
1095 }
1096
1097 int retval = target->type->read_memory(target, address, 4, 1, value_buf);
1098
1099 if (retval == ERROR_OK)
1100 {
1101 *value = target_buffer_get_u32(target, value_buf);
1102 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, *value);
1103 }
1104 else
1105 {
1106 *value = 0x0;
1107 LOG_DEBUG("address: 0x%8.8x failed", address);
1108 }
1109
1110 return retval;
1111 }
1112
1113 int target_read_u16(struct target_s *target, u32 address, u16 *value)
1114 {
1115 u8 value_buf[2];
1116 if (!target->type->examined)
1117 {
1118 LOG_ERROR("Target not examined yet");
1119 return ERROR_FAIL;
1120 }
1121
1122 int retval = target->type->read_memory(target, address, 2, 1, value_buf);
1123
1124 if (retval == ERROR_OK)
1125 {
1126 *value = target_buffer_get_u16(target, value_buf);
1127 LOG_DEBUG("address: 0x%8.8x, value: 0x%4.4x", address, *value);
1128 }
1129 else
1130 {
1131 *value = 0x0;
1132 LOG_DEBUG("address: 0x%8.8x failed", address);
1133 }
1134
1135 return retval;
1136 }
1137
1138 int target_read_u8(struct target_s *target, u32 address, u8 *value)
1139 {
1140 int retval = target->type->read_memory(target, address, 1, 1, value);
1141 if (!target->type->examined)
1142 {
1143 LOG_ERROR("Target not examined yet");
1144 return ERROR_FAIL;
1145 }
1146
1147 if (retval == ERROR_OK)
1148 {
1149 LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, *value);
1150 }
1151 else
1152 {
1153 *value = 0x0;
1154 LOG_DEBUG("address: 0x%8.8x failed", address);
1155 }
1156
1157 return retval;
1158 }
1159
1160 int target_write_u32(struct target_s *target, u32 address, u32 value)
1161 {
1162 int retval;
1163 u8 value_buf[4];
1164 if (!target->type->examined)
1165 {
1166 LOG_ERROR("Target not examined yet");
1167 return ERROR_FAIL;
1168 }
1169
1170 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
1171
1172 target_buffer_set_u32(target, value_buf, value);
1173 if ((retval = target->type->write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1174 {
1175 LOG_DEBUG("failed: %i", retval);
1176 }
1177
1178 return retval;
1179 }
1180
1181 int target_write_u16(struct target_s *target, u32 address, u16 value)
1182 {
1183 int retval;
1184 u8 value_buf[2];
1185 if (!target->type->examined)
1186 {
1187 LOG_ERROR("Target not examined yet");
1188 return ERROR_FAIL;
1189 }
1190
1191 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
1192
1193 target_buffer_set_u16(target, value_buf, value);
1194 if ((retval = target->type->write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1195 {
1196 LOG_DEBUG("failed: %i", retval);
1197 }
1198
1199 return retval;
1200 }
1201
1202 int target_write_u8(struct target_s *target, u32 address, u8 value)
1203 {
1204 int retval;
1205 if (!target->type->examined)
1206 {
1207 LOG_ERROR("Target not examined yet");
1208 return ERROR_FAIL;
1209 }
1210
1211 LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, value);
1212
1213 if ((retval = target->type->read_memory(target, address, 1, 1, &value)) != ERROR_OK)
1214 {
1215 LOG_DEBUG("failed: %i", retval);
1216 }
1217
1218 return retval;
1219 }
1220
1221 int target_register_user_commands(struct command_context_s *cmd_ctx)
1222 {
1223 register_command(cmd_ctx, NULL, "reg", handle_reg_command, COMMAND_EXEC, NULL);
1224 register_command(cmd_ctx, NULL, "poll", handle_poll_command, COMMAND_EXEC, "poll target state");
1225 register_command(cmd_ctx, NULL, "wait_halt", handle_wait_halt_command, COMMAND_EXEC, "wait for target halt [time (s)]");
1226 register_command(cmd_ctx, NULL, "halt", handle_halt_command, COMMAND_EXEC, "halt target");
1227 register_command(cmd_ctx, NULL, "resume", handle_resume_command, COMMAND_EXEC, "resume target [addr]");
1228 register_command(cmd_ctx, NULL, "step", handle_step_command, COMMAND_EXEC, "step one instruction from current PC or [addr]");
1229 register_command(cmd_ctx, NULL, "reset", handle_reset_command, COMMAND_EXEC, "reset target [run|halt|init]");
1230 register_command(cmd_ctx, NULL, "soft_reset_halt", handle_soft_reset_halt_command, COMMAND_EXEC, "halt the target and do a soft reset");
1231
1232 register_command(cmd_ctx, NULL, "mdw", handle_md_command, COMMAND_EXEC, "display memory words <addr> [count]");
1233 register_command(cmd_ctx, NULL, "mdh", handle_md_command, COMMAND_EXEC, "display memory half-words <addr> [count]");
1234 register_command(cmd_ctx, NULL, "mdb", handle_md_command, COMMAND_EXEC, "display memory bytes <addr> [count]");
1235
1236 register_command(cmd_ctx, NULL, "mww", handle_mw_command, COMMAND_EXEC, "write memory word <addr> <value> [count]");
1237 register_command(cmd_ctx, NULL, "mwh", handle_mw_command, COMMAND_EXEC, "write memory half-word <addr> <value> [count]");
1238 register_command(cmd_ctx, NULL, "mwb", handle_mw_command, COMMAND_EXEC, "write memory byte <addr> <value> [count]");
1239
1240 register_command(cmd_ctx, NULL, "bp", handle_bp_command, COMMAND_EXEC, "set breakpoint <address> <length> [hw]");
1241 register_command(cmd_ctx, NULL, "rbp", handle_rbp_command, COMMAND_EXEC, "remove breakpoint <adress>");
1242 register_command(cmd_ctx, NULL, "wp", handle_wp_command, COMMAND_EXEC, "set watchpoint <address> <length> <r/w/a> [value] [mask]");
1243 register_command(cmd_ctx, NULL, "rwp", handle_rwp_command, COMMAND_EXEC, "remove watchpoint <adress>");
1244
1245 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]");
1246 register_command(cmd_ctx, NULL, "dump_image", handle_dump_image_command, COMMAND_EXEC, "dump_image <file> <address> <size>");
1247 register_command(cmd_ctx, NULL, "verify_image", handle_verify_image_command, COMMAND_EXEC, "verify_image <file> [offset] [type]");
1248
1249 target_request_register_commands(cmd_ctx);
1250 trace_register_commands(cmd_ctx);
1251
1252 return ERROR_OK;
1253 }
1254
1255 int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1256 {
1257 target_t *target = targets;
1258 int count = 0;
1259
1260 if (argc == 1)
1261 {
1262 int num = strtoul(args[0], NULL, 0);
1263
1264 while (target)
1265 {
1266 count++;
1267 target = target->next;
1268 }
1269
1270 if (num < count)
1271 cmd_ctx->current_target = num;
1272 else
1273 command_print(cmd_ctx, "%i is out of bounds, only %i targets are configured", num, count);
1274
1275 return ERROR_OK;
1276 }
1277
1278 while (target)
1279 {
1280 command_print(cmd_ctx, "%i: %s (%s), state: %s", count++, target->type->name, target_endianess_strings[target->endianness], target_state_strings[target->state]);
1281 target = target->next;
1282 }
1283
1284 return ERROR_OK;
1285 }
1286
1287 int handle_target_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1288 {
1289 int i;
1290 int found = 0;
1291
1292 if (argc < 3)
1293 {
1294 return ERROR_COMMAND_SYNTAX_ERROR;
1295 }
1296
1297 /* search for the specified target */
1298 if (args[0] && (args[0][0] != 0))
1299 {
1300 for (i = 0; target_types[i]; i++)
1301 {
1302 if (strcmp(args[0], target_types[i]->name) == 0)
1303 {
1304 target_t **last_target_p = &targets;
1305
1306 /* register target specific commands */
1307 if (target_types[i]->register_commands(cmd_ctx) != ERROR_OK)
1308 {
1309 LOG_ERROR("couldn't register '%s' commands", args[0]);
1310 exit(-1);
1311 }
1312
1313 if (*last_target_p)
1314 {
1315 while ((*last_target_p)->next)
1316 last_target_p = &((*last_target_p)->next);
1317 last_target_p = &((*last_target_p)->next);
1318 }
1319
1320 *last_target_p = malloc(sizeof(target_t));
1321
1322 /* allocate memory for each unique target type */
1323 (*last_target_p)->type = (target_type_t*)malloc(sizeof(target_type_t));
1324 *((*last_target_p)->type) = *target_types[i];
1325
1326 if (strcmp(args[1], "big") == 0)
1327 (*last_target_p)->endianness = TARGET_BIG_ENDIAN;
1328 else if (strcmp(args[1], "little") == 0)
1329 (*last_target_p)->endianness = TARGET_LITTLE_ENDIAN;
1330 else
1331 {
1332 LOG_ERROR("endianness must be either 'little' or 'big', not '%s'", args[1]);
1333 return ERROR_COMMAND_SYNTAX_ERROR;
1334 }
1335
1336 if (strcmp(args[2], "reset_halt") == 0)
1337 {
1338 LOG_WARNING("reset_mode argument is obsolete.");
1339 return ERROR_COMMAND_SYNTAX_ERROR;
1340 }
1341 else if (strcmp(args[2], "reset_run") == 0)
1342 {
1343 LOG_WARNING("reset_mode argument is obsolete.");
1344 return ERROR_COMMAND_SYNTAX_ERROR;
1345 }
1346 else if (strcmp(args[2], "reset_init") == 0)
1347 {
1348 LOG_WARNING("reset_mode argument is obsolete.");
1349 return ERROR_COMMAND_SYNTAX_ERROR;
1350 }
1351 else if (strcmp(args[2], "run_and_halt") == 0)
1352 {
1353 LOG_WARNING("reset_mode argument is obsolete.");
1354 return ERROR_COMMAND_SYNTAX_ERROR;
1355 }
1356 else if (strcmp(args[2], "run_and_init") == 0)
1357 {
1358 LOG_WARNING("reset_mode argument is obsolete.");
1359 return ERROR_COMMAND_SYNTAX_ERROR;
1360 }
1361 else
1362 {
1363 /* Kludge! we want to make this reset arg optional while remaining compatible! */
1364 args--;
1365 argc++;
1366 }
1367
1368 (*last_target_p)->working_area = 0x0;
1369 (*last_target_p)->working_area_size = 0x0;
1370 (*last_target_p)->working_areas = NULL;
1371 (*last_target_p)->backup_working_area = 0;
1372
1373 (*last_target_p)->state = TARGET_UNKNOWN;
1374 (*last_target_p)->debug_reason = DBG_REASON_UNDEFINED;
1375 (*last_target_p)->reg_cache = NULL;
1376 (*last_target_p)->breakpoints = NULL;
1377 (*last_target_p)->watchpoints = NULL;
1378 (*last_target_p)->next = NULL;
1379 (*last_target_p)->arch_info = NULL;
1380
1381 /* initialize trace information */
1382 (*last_target_p)->trace_info = malloc(sizeof(trace_t));
1383 (*last_target_p)->trace_info->num_trace_points = 0;
1384 (*last_target_p)->trace_info->trace_points_size = 0;
1385 (*last_target_p)->trace_info->trace_points = NULL;
1386 (*last_target_p)->trace_info->trace_history_size = 0;
1387 (*last_target_p)->trace_info->trace_history = NULL;
1388 (*last_target_p)->trace_info->trace_history_pos = 0;
1389 (*last_target_p)->trace_info->trace_history_overflowed = 0;
1390
1391 (*last_target_p)->dbgmsg = NULL;
1392 (*last_target_p)->dbg_msg_enabled = 0;
1393
1394 (*last_target_p)->type->target_command(cmd_ctx, cmd, args, argc, *last_target_p);
1395
1396 found = 1;
1397 break;
1398 }
1399 }
1400 }
1401
1402 /* no matching target found */
1403 if (!found)
1404 {
1405 LOG_ERROR("target '%s' not found", args[0]);
1406 return ERROR_COMMAND_SYNTAX_ERROR;
1407 }
1408
1409 return ERROR_OK;
1410 }
1411
1412 int target_invoke_script(struct command_context_s *cmd_ctx, target_t *target, char *name)
1413 {
1414 return command_run_linef(cmd_ctx, " if {[catch {info body target_%d_%s} t]==0} {target_%d_%s}",
1415 get_num_by_target(target), name,
1416 get_num_by_target(target), name);
1417 }
1418
1419 int handle_working_area_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1420 {
1421 target_t *target = NULL;
1422
1423 if ((argc < 4) || (argc > 5))
1424 {
1425 return ERROR_COMMAND_SYNTAX_ERROR;
1426 }
1427
1428 target = get_target_by_num(strtoul(args[0], NULL, 0));
1429 if (!target)
1430 {
1431 return ERROR_COMMAND_SYNTAX_ERROR;
1432 }
1433 target_free_all_working_areas(target);
1434
1435 target->working_area_phys = target->working_area_virt = strtoul(args[1], NULL, 0);
1436 if (argc == 5)
1437 {
1438 target->working_area_virt = strtoul(args[4], NULL, 0);
1439 }
1440 target->working_area_size = strtoul(args[2], NULL, 0);
1441
1442 if (strcmp(args[3], "backup") == 0)
1443 {
1444 target->backup_working_area = 1;
1445 }
1446 else if (strcmp(args[3], "nobackup") == 0)
1447 {
1448 target->backup_working_area = 0;
1449 }
1450 else
1451 {
1452 LOG_ERROR("unrecognized <backup|nobackup> argument (%s)", args[3]);
1453 return ERROR_COMMAND_SYNTAX_ERROR;
1454 }
1455
1456 return ERROR_OK;
1457 }
1458
1459
1460 /* process target state changes */
1461 int handle_target(void *priv)
1462 {
1463 target_t *target = targets;
1464
1465 while (target)
1466 {
1467 if (target_continous_poll)
1468 {
1469 /* polling may fail silently until the target has been examined */
1470 target_poll(target);
1471 }
1472
1473 target = target->next;
1474 }
1475
1476 return ERROR_OK;
1477 }
1478
1479 int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1480 {
1481 target_t *target;
1482 reg_t *reg = NULL;
1483 int count = 0;
1484 char *value;
1485
1486 LOG_DEBUG("-");
1487
1488 target = get_current_target(cmd_ctx);
1489
1490 /* list all available registers for the current target */
1491 if (argc == 0)
1492 {
1493 reg_cache_t *cache = target->reg_cache;
1494
1495 count = 0;
1496 while(cache)
1497 {
1498 int i;
1499 for (i = 0; i < cache->num_regs; i++)
1500 {
1501 value = buf_to_str(cache->reg_list[i].value, cache->reg_list[i].size, 16);
1502 command_print(cmd_ctx, "(%i) %s (/%i): 0x%s (dirty: %i, valid: %i)", count++, cache->reg_list[i].name, cache->reg_list[i].size, value, cache->reg_list[i].dirty, cache->reg_list[i].valid);
1503 free(value);
1504 }
1505 cache = cache->next;
1506 }
1507
1508 return ERROR_OK;
1509 }
1510
1511 /* access a single register by its ordinal number */
1512 if ((args[0][0] >= '0') && (args[0][0] <= '9'))
1513 {
1514 int num = strtoul(args[0], NULL, 0);
1515 reg_cache_t *cache = target->reg_cache;
1516
1517 count = 0;
1518 while(cache)
1519 {
1520 int i;
1521 for (i = 0; i < cache->num_regs; i++)
1522 {
1523 if (count++ == num)
1524 {
1525 reg = &cache->reg_list[i];
1526 break;
1527 }
1528 }
1529 if (reg)
1530 break;
1531 cache = cache->next;
1532 }
1533
1534 if (!reg)
1535 {
1536 command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1537 return ERROR_OK;
1538 }
1539 } else /* access a single register by its name */
1540 {
1541 reg = register_get_by_name(target->reg_cache, args[0], 1);
1542
1543 if (!reg)
1544 {
1545 command_print(cmd_ctx, "register %s not found in current target", args[0]);
1546 return ERROR_OK;
1547 }
1548 }
1549
1550 /* display a register */
1551 if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
1552 {
1553 if ((argc == 2) && (strcmp(args[1], "force") == 0))
1554 reg->valid = 0;
1555
1556 if (reg->valid == 0)
1557 {
1558 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1559 if (arch_type == NULL)
1560 {
1561 LOG_ERROR("BUG: encountered unregistered arch type");
1562 return ERROR_OK;
1563 }
1564 arch_type->get(reg);
1565 }
1566 value = buf_to_str(reg->value, reg->size, 16);
1567 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1568 free(value);
1569 return ERROR_OK;
1570 }
1571
1572 /* set register value */
1573 if (argc == 2)
1574 {
1575 u8 *buf = malloc(CEIL(reg->size, 8));
1576 str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
1577
1578 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1579 if (arch_type == NULL)
1580 {
1581 LOG_ERROR("BUG: encountered unregistered arch type");
1582 return ERROR_OK;
1583 }
1584
1585 arch_type->set(reg, buf);
1586
1587 value = buf_to_str(reg->value, reg->size, 16);
1588 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1589 free(value);
1590
1591 free(buf);
1592
1593 return ERROR_OK;
1594 }
1595
1596 command_print(cmd_ctx, "usage: reg <#|name> [value]");
1597
1598 return ERROR_OK;
1599 }
1600
1601
1602 int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1603 {
1604 target_t *target = get_current_target(cmd_ctx);
1605
1606 if (argc == 0)
1607 {
1608 target_poll(target);
1609 target_arch_state(target);
1610 }
1611 else
1612 {
1613 if (strcmp(args[0], "on") == 0)
1614 {
1615 target_continous_poll = 1;
1616 }
1617 else if (strcmp(args[0], "off") == 0)
1618 {
1619 target_continous_poll = 0;
1620 }
1621 else
1622 {
1623 command_print(cmd_ctx, "arg is \"on\" or \"off\"");
1624 }
1625 }
1626
1627
1628 return ERROR_OK;
1629 }
1630
1631 int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1632 {
1633 int ms = 5000;
1634
1635 if (argc > 0)
1636 {
1637 char *end;
1638
1639 ms = strtoul(args[0], &end, 0) * 1000;
1640 if (*end)
1641 {
1642 command_print(cmd_ctx, "usage: %s [seconds]", cmd);
1643 return ERROR_OK;
1644 }
1645 }
1646 target_t *target = get_current_target(cmd_ctx);
1647
1648 return target_wait_state(target, TARGET_HALTED, ms);
1649 }
1650
1651 int target_wait_state(target_t *target, enum target_state state, int ms)
1652 {
1653 int retval;
1654 struct timeval timeout, now;
1655 int once=1;
1656 gettimeofday(&timeout, NULL);
1657 timeval_add_time(&timeout, 0, ms * 1000);
1658
1659 for (;;)
1660 {
1661 if ((retval=target_poll(target))!=ERROR_OK)
1662 return retval;
1663 target_call_timer_callbacks_now();
1664 if (target->state == state)
1665 {
1666 break;
1667 }
1668 if (once)
1669 {
1670 once=0;
1671 LOG_USER("waiting for target %s...", target_state_strings[state]);
1672 }
1673
1674 gettimeofday(&now, NULL);
1675 if ((now.tv_sec > timeout.tv_sec) || ((now.tv_sec == timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
1676 {
1677 LOG_ERROR("timed out while waiting for target %s", target_state_strings[state]);
1678 return ERROR_FAIL;
1679 }
1680 }
1681
1682 return ERROR_OK;
1683 }
1684
1685 int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1686 {
1687 int retval;
1688 target_t *target = get_current_target(cmd_ctx);
1689
1690 LOG_DEBUG("-");
1691
1692 if ((retval = target_halt(target)) != ERROR_OK)
1693 {
1694 return retval;
1695 }
1696
1697 return handle_wait_halt_command(cmd_ctx, cmd, args, argc);
1698 }
1699
1700 int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1701 {
1702 target_t *target = get_current_target(cmd_ctx);
1703
1704 LOG_USER("requesting target halt and executing a soft reset");
1705
1706 target->type->soft_reset_halt(target);
1707
1708 return ERROR_OK;
1709 }
1710
1711 int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1712 {
1713 enum target_reset_mode reset_mode = RESET_RUN;
1714
1715 if (argc >= 1)
1716 {
1717 if (strcmp("run", args[0]) == 0)
1718 reset_mode = RESET_RUN;
1719 else if (strcmp("halt", args[0]) == 0)
1720 reset_mode = RESET_HALT;
1721 else if (strcmp("init", args[0]) == 0)
1722 reset_mode = RESET_INIT;
1723 else
1724 {
1725 return ERROR_COMMAND_SYNTAX_ERROR;
1726 }
1727 }
1728
1729 /* reset *all* targets */
1730 target_process_reset(cmd_ctx, reset_mode);
1731
1732 return ERROR_OK;
1733 }
1734
1735 int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1736 {
1737 int retval;
1738 target_t *target = get_current_target(cmd_ctx);
1739
1740 target_invoke_script(cmd_ctx, target, "pre_resume");
1741
1742 if (argc == 0)
1743 retval = target_resume(target, 1, 0, 1, 0); /* current pc, addr = 0, handle breakpoints, not debugging */
1744 else if (argc == 1)
1745 retval = target_resume(target, 0, strtoul(args[0], NULL, 0), 1, 0); /* addr = args[0], handle breakpoints, not debugging */
1746 else
1747 {
1748 return ERROR_COMMAND_SYNTAX_ERROR;
1749 }
1750
1751 return retval;
1752 }
1753
1754 int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1755 {
1756 target_t *target = get_current_target(cmd_ctx);
1757
1758 LOG_DEBUG("-");
1759
1760 if (argc == 0)
1761 target->type->step(target, 1, 0, 1); /* current pc, addr = 0, handle breakpoints */
1762
1763 if (argc == 1)
1764 target->type->step(target, 0, strtoul(args[0], NULL, 0), 1); /* addr = args[0], handle breakpoints */
1765
1766 return ERROR_OK;
1767 }
1768
1769 int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1770 {
1771 const int line_bytecnt = 32;
1772 int count = 1;
1773 int size = 4;
1774 u32 address = 0;
1775 int line_modulo;
1776 int i;
1777
1778 char output[128];
1779 int output_len;
1780
1781 int retval;
1782
1783 u8 *buffer;
1784 target_t *target = get_current_target(cmd_ctx);
1785
1786 if (argc < 1)
1787 return ERROR_OK;
1788
1789 if (argc == 2)
1790 count = strtoul(args[1], NULL, 0);
1791
1792 address = strtoul(args[0], NULL, 0);
1793
1794
1795 switch (cmd[2])
1796 {
1797 case 'w':
1798 size = 4; line_modulo = line_bytecnt / 4;
1799 break;
1800 case 'h':
1801 size = 2; line_modulo = line_bytecnt / 2;
1802 break;
1803 case 'b':
1804 size = 1; line_modulo = line_bytecnt / 1;
1805 break;
1806 default:
1807 return ERROR_OK;
1808 }
1809
1810 buffer = calloc(count, size);
1811 retval = target->type->read_memory(target, address, size, count, buffer);
1812 if (retval == ERROR_OK)
1813 {
1814 output_len = 0;
1815
1816 for (i = 0; i < count; i++)
1817 {
1818 if (i%line_modulo == 0)
1819 output_len += snprintf(output + output_len, 128 - output_len, "0x%8.8x: ", address + (i*size));
1820
1821 switch (size)
1822 {
1823 case 4:
1824 output_len += snprintf(output + output_len, 128 - output_len, "%8.8x ", target_buffer_get_u32(target, &buffer[i*4]));
1825 break;
1826 case 2:
1827 output_len += snprintf(output + output_len, 128 - output_len, "%4.4x ", target_buffer_get_u16(target, &buffer[i*2]));
1828 break;
1829 case 1:
1830 output_len += snprintf(output + output_len, 128 - output_len, "%2.2x ", buffer[i*1]);
1831 break;
1832 }
1833
1834 if ((i%line_modulo == line_modulo-1) || (i == count - 1))
1835 {
1836 command_print(cmd_ctx, output);
1837 output_len = 0;
1838 }
1839 }
1840 }
1841
1842 free(buffer);
1843
1844 return retval;
1845 }
1846
1847 int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1848 {
1849 u32 address = 0;
1850 u32 value = 0;
1851 int count = 1;
1852 int i;
1853 int wordsize;
1854 target_t *target = get_current_target(cmd_ctx);
1855 u8 value_buf[4];
1856
1857 if ((argc < 2) || (argc > 3))
1858 return ERROR_COMMAND_SYNTAX_ERROR;
1859
1860 address = strtoul(args[0], NULL, 0);
1861 value = strtoul(args[1], NULL, 0);
1862 if (argc == 3)
1863 count = strtoul(args[2], NULL, 0);
1864
1865 switch (cmd[2])
1866 {
1867 case 'w':
1868 wordsize = 4;
1869 target_buffer_set_u32(target, value_buf, value);
1870 break;
1871 case 'h':
1872 wordsize = 2;
1873 target_buffer_set_u16(target, value_buf, value);
1874 break;
1875 case 'b':
1876 wordsize = 1;
1877 value_buf[0] = value;
1878 break;
1879 default:
1880 return ERROR_COMMAND_SYNTAX_ERROR;
1881 }
1882 for (i=0; i<count; i++)
1883 {
1884 int retval;
1885 switch (wordsize)
1886 {
1887 case 4:
1888 retval = target->type->write_memory(target, address + i*wordsize, 4, 1, value_buf);
1889 break;
1890 case 2:
1891 retval = target->type->write_memory(target, address + i*wordsize, 2, 1, value_buf);
1892 break;
1893 case 1:
1894 retval = target->type->write_memory(target, address + i*wordsize, 1, 1, value_buf);
1895 break;
1896 default:
1897 return ERROR_OK;
1898 }
1899 if (retval!=ERROR_OK)
1900 {
1901 return retval;
1902 }
1903 }
1904
1905 return ERROR_OK;
1906
1907 }
1908
1909 int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1910 {
1911 u8 *buffer;
1912 u32 buf_cnt;
1913 u32 image_size;
1914 u32 min_address=0;
1915 u32 max_address=0xffffffff;
1916 int i;
1917 int retval;
1918
1919 image_t image;
1920
1921 duration_t duration;
1922 char *duration_text;
1923
1924 target_t *target = get_current_target(cmd_ctx);
1925
1926 if ((argc < 1)||(argc > 5))
1927 {
1928 return ERROR_COMMAND_SYNTAX_ERROR;
1929 }
1930
1931 /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
1932 if (argc >= 2)
1933 {
1934 image.base_address_set = 1;
1935 image.base_address = strtoul(args[1], NULL, 0);
1936 }
1937 else
1938 {
1939 image.base_address_set = 0;
1940 }
1941
1942
1943 image.start_address_set = 0;
1944
1945 if (argc>=4)
1946 {
1947 min_address=strtoul(args[3], NULL, 0);
1948 }
1949 if (argc>=5)
1950 {
1951 max_address=strtoul(args[4], NULL, 0)+min_address;
1952 }
1953
1954 if (min_address>max_address)
1955 {
1956 return ERROR_COMMAND_SYNTAX_ERROR;
1957 }
1958
1959
1960 duration_start_measure(&duration);
1961
1962 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
1963 {
1964 return ERROR_OK;
1965 }
1966
1967 image_size = 0x0;
1968 retval = ERROR_OK;
1969 for (i = 0; i < image.num_sections; i++)
1970 {
1971 buffer = malloc(image.sections[i].size);
1972 if (buffer == NULL)
1973 {
1974 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
1975 break;
1976 }
1977
1978 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
1979 {
1980 free(buffer);
1981 break;
1982 }
1983
1984 u32 offset=0;
1985 u32 length=buf_cnt;
1986
1987
1988 /* DANGER!!! beware of unsigned comparision here!!! */
1989
1990 if ((image.sections[i].base_address+buf_cnt>=min_address)&&
1991 (image.sections[i].base_address<max_address))
1992 {
1993 if (image.sections[i].base_address<min_address)
1994 {
1995 /* clip addresses below */
1996 offset+=min_address-image.sections[i].base_address;
1997 length-=offset;
1998 }
1999
2000 if (image.sections[i].base_address+buf_cnt>max_address)
2001 {
2002 length-=(image.sections[i].base_address+buf_cnt)-max_address;
2003 }
2004
2005 if ((retval = target_write_buffer(target, image.sections[i].base_address+offset, length, buffer+offset)) != ERROR_OK)
2006 {
2007 free(buffer);
2008 break;
2009 }
2010 image_size += length;
2011 command_print(cmd_ctx, "%u byte written at address 0x%8.8x", length, image.sections[i].base_address+offset);
2012 }
2013
2014 free(buffer);
2015 }
2016
2017 duration_stop_measure(&duration, &duration_text);
2018 if (retval==ERROR_OK)
2019 {
2020 command_print(cmd_ctx, "downloaded %u byte in %s", image_size, duration_text);
2021 }
2022 free(duration_text);
2023
2024 image_close(&image);
2025
2026 return retval;
2027
2028 }
2029
2030 int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2031 {
2032 fileio_t fileio;
2033
2034 u32 address;
2035 u32 size;
2036 u8 buffer[560];
2037 int retval=ERROR_OK;
2038
2039 duration_t duration;
2040 char *duration_text;
2041
2042 target_t *target = get_current_target(cmd_ctx);
2043
2044 if (argc != 3)
2045 {
2046 command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
2047 return ERROR_OK;
2048 }
2049
2050 address = strtoul(args[1], NULL, 0);
2051 size = strtoul(args[2], NULL, 0);
2052
2053 if ((address & 3) || (size & 3))
2054 {
2055 command_print(cmd_ctx, "only 32-bit aligned address and size are supported");
2056 return ERROR_OK;
2057 }
2058
2059 if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2060 {
2061 return ERROR_OK;
2062 }
2063
2064 duration_start_measure(&duration);
2065
2066 while (size > 0)
2067 {
2068 u32 size_written;
2069 u32 this_run_size = (size > 560) ? 560 : size;
2070
2071 retval = target->type->read_memory(target, address, 4, this_run_size / 4, buffer);
2072 if (retval != ERROR_OK)
2073 {
2074 break;
2075 }
2076
2077 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2078 if (retval != ERROR_OK)
2079 {
2080 break;
2081 }
2082
2083 size -= this_run_size;
2084 address += this_run_size;
2085 }
2086
2087 fileio_close(&fileio);
2088
2089 duration_stop_measure(&duration, &duration_text);
2090 if (retval==ERROR_OK)
2091 {
2092 command_print(cmd_ctx, "dumped %"PRIi64" byte in %s", fileio.size, duration_text);
2093 }
2094 free(duration_text);
2095
2096 return ERROR_OK;
2097 }
2098
2099 int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2100 {
2101 u8 *buffer;
2102 u32 buf_cnt;
2103 u32 image_size;
2104 int i;
2105 int retval;
2106 u32 checksum = 0;
2107 u32 mem_checksum = 0;
2108
2109 image_t image;
2110
2111 duration_t duration;
2112 char *duration_text;
2113
2114 target_t *target = get_current_target(cmd_ctx);
2115
2116 if (argc < 1)
2117 {
2118 return ERROR_COMMAND_SYNTAX_ERROR;
2119 }
2120
2121 if (!target)
2122 {
2123 LOG_ERROR("no target selected");
2124 return ERROR_FAIL;
2125 }
2126
2127 duration_start_measure(&duration);
2128
2129 if (argc >= 2)
2130 {
2131 image.base_address_set = 1;
2132 image.base_address = strtoul(args[1], NULL, 0);
2133 }
2134 else
2135 {
2136 image.base_address_set = 0;
2137 image.base_address = 0x0;
2138 }
2139
2140 image.start_address_set = 0;
2141
2142 if ((retval=image_open(&image, args[0], (argc == 3) ? args[2] : NULL)) != ERROR_OK)
2143 {
2144 return retval;
2145 }
2146
2147 image_size = 0x0;
2148 retval=ERROR_OK;
2149 for (i = 0; i < image.num_sections; i++)
2150 {
2151 buffer = malloc(image.sections[i].size);
2152 if (buffer == NULL)
2153 {
2154 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
2155 break;
2156 }
2157 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2158 {
2159 free(buffer);
2160 break;
2161 }
2162
2163 /* calculate checksum of image */
2164 image_calculate_checksum( buffer, buf_cnt, &checksum );
2165
2166 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2167 if( retval != ERROR_OK )
2168 {
2169 free(buffer);
2170 break;
2171 }
2172
2173 if( checksum != mem_checksum )
2174 {
2175 /* failed crc checksum, fall back to a binary compare */
2176 u8 *data;
2177
2178 command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
2179
2180 data = (u8*)malloc(buf_cnt);
2181
2182 /* Can we use 32bit word accesses? */
2183 int size = 1;
2184 int count = buf_cnt;
2185 if ((count % 4) == 0)
2186 {
2187 size *= 4;
2188 count /= 4;
2189 }
2190 retval = target->type->read_memory(target, image.sections[i].base_address, size, count, data);
2191 if (retval == ERROR_OK)
2192 {
2193 int t;
2194 for (t = 0; t < buf_cnt; t++)
2195 {
2196 if (data[t] != buffer[t])
2197 {
2198 command_print(cmd_ctx, "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n", t + image.sections[i].base_address, data[t], buffer[t]);
2199 free(data);
2200 free(buffer);
2201 retval=ERROR_FAIL;
2202 goto done;
2203 }
2204 }
2205 }
2206
2207 free(data);
2208 }
2209
2210 free(buffer);
2211 image_size += buf_cnt;
2212 }
2213 done:
2214 duration_stop_measure(&duration, &duration_text);
2215 if (retval==ERROR_OK)
2216 {
2217 command_print(cmd_ctx, "verified %u bytes in %s", image_size, duration_text);
2218 }
2219 free(duration_text);
2220
2221 image_close(&image);
2222
2223 return retval;
2224 }
2225
2226 int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2227 {
2228 int retval;
2229 target_t *target = get_current_target(cmd_ctx);
2230
2231 if (argc == 0)
2232 {
2233 breakpoint_t *breakpoint = target->breakpoints;
2234
2235 while (breakpoint)
2236 {
2237 if (breakpoint->type == BKPT_SOFT)
2238 {
2239 char* buf = buf_to_str(breakpoint->orig_instr, breakpoint->length, 16);
2240 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i, 0x%s", breakpoint->address, breakpoint->length, breakpoint->set, buf);
2241 free(buf);
2242 }
2243 else
2244 {
2245 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i", breakpoint->address, breakpoint->length, breakpoint->set);
2246 }
2247 breakpoint = breakpoint->next;
2248 }
2249 }
2250 else if (argc >= 2)
2251 {
2252 int hw = BKPT_SOFT;
2253 u32 length = 0;
2254
2255 length = strtoul(args[1], NULL, 0);
2256
2257 if (argc >= 3)
2258 if (strcmp(args[2], "hw") == 0)
2259 hw = BKPT_HARD;
2260
2261 if ((retval = breakpoint_add(target, strtoul(args[0], NULL, 0), length, hw)) != ERROR_OK)
2262 {
2263 LOG_ERROR("Failure setting breakpoints");
2264 }
2265 else
2266 {
2267 command_print(cmd_ctx, "breakpoint added at address 0x%8.8x", strtoul(args[0], NULL, 0));
2268 }
2269 }
2270 else
2271 {
2272 command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
2273 }
2274
2275 return ERROR_OK;
2276 }
2277
2278 int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2279 {
2280 target_t *target = get_current_target(cmd_ctx);
2281
2282 if (argc > 0)
2283 breakpoint_remove(target, strtoul(args[0], NULL, 0));
2284
2285 return ERROR_OK;
2286 }
2287
2288 int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2289 {
2290 target_t *target = get_current_target(cmd_ctx);
2291 int retval;
2292
2293 if (argc == 0)
2294 {
2295 watchpoint_t *watchpoint = target->watchpoints;
2296
2297 while (watchpoint)
2298 {
2299 command_print(cmd_ctx, "address: 0x%8.8x, len: 0x%8.8x, r/w/a: %i, value: 0x%8.8x, mask: 0x%8.8x", watchpoint->address, watchpoint->length, watchpoint->rw, watchpoint->value, watchpoint->mask);
2300 watchpoint = watchpoint->next;
2301 }
2302 }
2303 else if (argc >= 2)
2304 {
2305 enum watchpoint_rw type = WPT_ACCESS;
2306 u32 data_value = 0x0;
2307 u32 data_mask = 0xffffffff;
2308
2309 if (argc >= 3)
2310 {
2311 switch(args[2][0])
2312 {
2313 case 'r':
2314 type = WPT_READ;
2315 break;
2316 case 'w':
2317 type = WPT_WRITE;
2318 break;
2319 case 'a':
2320 type = WPT_ACCESS;
2321 break;
2322 default:
2323 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2324 return ERROR_OK;
2325 }
2326 }
2327 if (argc >= 4)
2328 {
2329 data_value = strtoul(args[3], NULL, 0);
2330 }
2331 if (argc >= 5)
2332 {
2333 data_mask = strtoul(args[4], NULL, 0);
2334 }
2335
2336 if ((retval = watchpoint_add(target, strtoul(args[0], NULL, 0),
2337 strtoul(args[1], NULL, 0), type, data_value, data_mask)) != ERROR_OK)
2338 {
2339 LOG_ERROR("Failure setting breakpoints");
2340 }
2341 }
2342 else
2343 {
2344 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2345 }
2346
2347 return ERROR_OK;
2348 }
2349
2350 int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2351 {
2352 target_t *target = get_current_target(cmd_ctx);
2353
2354 if (argc > 0)
2355 watchpoint_remove(target, strtoul(args[0], NULL, 0));
2356
2357 return ERROR_OK;
2358 }
2359
2360 int handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc)
2361 {
2362 int retval;
2363 target_t *target = get_current_target(cmd_ctx);
2364 u32 va;
2365 u32 pa;
2366
2367 if (argc != 1)
2368 {
2369 return ERROR_COMMAND_SYNTAX_ERROR;
2370 }
2371 va = strtoul(args[0], NULL, 0);
2372
2373 retval = target->type->virt2phys(target, va, &pa);
2374 if (retval == ERROR_OK)
2375 {
2376 command_print(cmd_ctx, "Physical address 0x%08x", pa);
2377 }
2378 else
2379 {
2380 /* lower levels will have logged a detailed error which is
2381 * forwarded to telnet/GDB session.
2382 */
2383 }
2384 return retval;
2385 }
2386 static void writeLong(FILE *f, int l)
2387 {
2388 int i;
2389 for (i=0; i<4; i++)
2390 {
2391 char c=(l>>(i*8))&0xff;
2392 fwrite(&c, 1, 1, f);
2393 }
2394
2395 }
2396 static void writeString(FILE *f, char *s)
2397 {
2398 fwrite(s, 1, strlen(s), f);
2399 }
2400
2401
2402
2403 // Dump a gmon.out histogram file.
2404 static void writeGmon(u32 *samples, int sampleNum, char *filename)
2405 {
2406 int i;
2407 FILE *f=fopen(filename, "w");
2408 if (f==NULL)
2409 return;
2410 fwrite("gmon", 1, 4, f);
2411 writeLong(f, 0x00000001); // Version
2412 writeLong(f, 0); // padding
2413 writeLong(f, 0); // padding
2414 writeLong(f, 0); // padding
2415
2416 fwrite("", 1, 1, f); // GMON_TAG_TIME_HIST
2417
2418 // figure out bucket size
2419 u32 min=samples[0];
2420 u32 max=samples[0];
2421 for (i=0; i<sampleNum; i++)
2422 {
2423 if (min>samples[i])
2424 {
2425 min=samples[i];
2426 }
2427 if (max<samples[i])
2428 {
2429 max=samples[i];
2430 }
2431 }
2432
2433 int addressSpace=(max-min+1);
2434
2435 static int const maxBuckets=256*1024; // maximum buckets.
2436 int length=addressSpace;
2437 if (length > maxBuckets)
2438 {
2439 length=maxBuckets;
2440 }
2441 int *buckets=malloc(sizeof(int)*length);
2442 if (buckets==NULL)
2443 {
2444 fclose(f);
2445 return;
2446 }
2447 memset(buckets, 0, sizeof(int)*length);
2448 for (i=0; i<sampleNum;i++)
2449 {
2450 u32 address=samples[i];
2451 long long a=address-min;
2452 long long b=length-1;
2453 long long c=addressSpace-1;
2454 int index=(a*b)/c; // danger!!!! int32 overflows
2455 buckets[index]++;
2456 }
2457
2458 // append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr))
2459 writeLong(f, min); // low_pc
2460 writeLong(f, max); // high_pc
2461 writeLong(f, length); // # of samples
2462 writeLong(f, 64000000); // 64MHz
2463 writeString(f, "seconds");
2464 for (i=0; i<(15-strlen("seconds")); i++)
2465 {
2466 fwrite("", 1, 1, f); // padding
2467 }
2468 writeString(f, "s");
2469
2470 // append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size)
2471
2472 char *data=malloc(2*length);
2473 if (data!=NULL)
2474 {
2475 for (i=0; i<length;i++)
2476 {
2477 int val;
2478 val=buckets[i];
2479 if (val>65535)
2480 {
2481 val=65535;
2482 }
2483 data[i*2]=val&0xff;
2484 data[i*2+1]=(val>>8)&0xff;
2485 }
2486 free(buckets);
2487 fwrite(data, 1, length*2, f);
2488 free(data);
2489 } else
2490 {
2491 free(buckets);
2492 }
2493
2494 fclose(f);
2495 }
2496
2497 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2498 int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2499 {
2500 target_t *target = get_current_target(cmd_ctx);
2501 struct timeval timeout, now;
2502
2503 gettimeofday(&timeout, NULL);
2504 if (argc!=2)
2505 {
2506 return ERROR_COMMAND_SYNTAX_ERROR;
2507 }
2508 char *end;
2509 timeval_add_time(&timeout, strtoul(args[0], &end, 0), 0);
2510 if (*end)
2511 {
2512 return ERROR_OK;
2513 }
2514
2515 command_print(cmd_ctx, "Starting profiling. Halting and resuming the target as often as we can...");
2516
2517 static const int maxSample=10000;
2518 u32 *samples=malloc(sizeof(u32)*maxSample);
2519 if (samples==NULL)
2520 return ERROR_OK;
2521
2522 int numSamples=0;
2523 int retval=ERROR_OK;
2524 // hopefully it is safe to cache! We want to stop/restart as quickly as possible.
2525 reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1);
2526
2527 for (;;)
2528 {
2529 target_poll(target);
2530 if (target->state == TARGET_HALTED)
2531 {
2532 u32 t=*((u32 *)reg->value);
2533 samples[numSamples++]=t;
2534 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2535 target_poll(target);
2536 usleep(10*1000); // sleep 10ms, i.e. <100 samples/second.
2537 } else if (target->state == TARGET_RUNNING)
2538 {
2539 // We want to quickly sample the PC.
2540 target_halt(target);
2541 } else
2542 {
2543 command_print(cmd_ctx, "Target not halted or running");
2544 retval=ERROR_OK;
2545 break;
2546 }
2547 if (retval!=ERROR_OK)
2548 {
2549 break;
2550 }
2551
2552 gettimeofday(&now, NULL);
2553 if ((numSamples>=maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
2554 {
2555 command_print(cmd_ctx, "Profiling completed. %d samples.", numSamples);
2556 target_poll(target);
2557 if (target->state == TARGET_HALTED)
2558 {
2559 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2560 }
2561 target_poll(target);
2562 writeGmon(samples, numSamples, args[1]);
2563 command_print(cmd_ctx, "Wrote %s", args[1]);
2564 break;
2565 }
2566 }
2567 free(samples);
2568
2569 return ERROR_OK;
2570 }
2571
2572 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, u32 val)
2573 {
2574 char *namebuf;
2575 Jim_Obj *nameObjPtr, *valObjPtr;
2576 int result;
2577
2578 namebuf = alloc_printf("%s(%d)", varname, idx);
2579 if (!namebuf)
2580 return JIM_ERR;
2581
2582 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
2583 valObjPtr = Jim_NewIntObj(interp, val);
2584 if (!nameObjPtr || !valObjPtr)
2585 {
2586 free(namebuf);
2587 return JIM_ERR;
2588 }
2589
2590 Jim_IncrRefCount(nameObjPtr);
2591 Jim_IncrRefCount(valObjPtr);
2592 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
2593 Jim_DecrRefCount(interp, nameObjPtr);
2594 Jim_DecrRefCount(interp, valObjPtr);
2595 free(namebuf);
2596 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
2597 return result;
2598 }
2599
2600 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
2601 {
2602 target_t *target;
2603 command_context_t *context;
2604 long l;
2605 u32 width;
2606 u32 len;
2607 u32 addr;
2608 u32 count;
2609 u32 v;
2610 const char *varname;
2611 u8 buffer[4096];
2612 int i, n, e, retval;
2613
2614 /* argv[1] = name of array to receive the data
2615 * argv[2] = desired width
2616 * argv[3] = memory address
2617 * argv[4] = count of times to read
2618 */
2619 if (argc != 5) {
2620 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
2621 return JIM_ERR;
2622 }
2623 varname = Jim_GetString(argv[1], &len);
2624 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
2625
2626 e = Jim_GetLong(interp, argv[2], &l);
2627 width = l;
2628 if (e != JIM_OK) {
2629 return e;
2630 }
2631
2632 e = Jim_GetLong(interp, argv[3], &l);
2633 addr = l;
2634 if (e != JIM_OK) {
2635 return e;
2636 }
2637 e = Jim_GetLong(interp, argv[4], &l);
2638 len = l;
2639 if (e != JIM_OK) {
2640 return e;
2641 }
2642 switch (width) {
2643 case 8:
2644 width = 1;
2645 break;
2646 case 16:
2647 width = 2;
2648 break;
2649 case 32:
2650 width = 4;
2651 break;
2652 default:
2653 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2654 Jim_AppendStrings( interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL );
2655 return JIM_ERR;
2656 }
2657 if (len == 0) {
2658 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2659 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
2660 return JIM_ERR;
2661 }
2662 if ((addr + (len * width)) < addr) {
2663 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2664 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
2665 return JIM_ERR;
2666 }
2667 /* absurd transfer size? */
2668 if (len > 65536) {
2669 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2670 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
2671 return JIM_ERR;
2672 }
2673
2674 if ((width == 1) ||
2675 ((width == 2) && ((addr & 1) == 0)) ||
2676 ((width == 4) && ((addr & 3) == 0))) {
2677 /* all is well */
2678 } else {
2679 char buf[100];
2680 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2681 sprintf(buf, "mem2array address: 0x%08x is not aligned for %d byte reads", addr, width);
2682 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
2683 return JIM_ERR;
2684 }
2685
2686 context = Jim_GetAssocData(interp, "context");
2687 if (context == NULL)
2688 {
2689 LOG_ERROR("mem2array: no command context");
2690 return JIM_ERR;
2691 }
2692 target = get_current_target(context);
2693 if (target == NULL)
2694 {
2695 LOG_ERROR("mem2array: no current target");
2696 return JIM_ERR;
2697 }
2698
2699 /* Transfer loop */
2700
2701 /* index counter */
2702 n = 0;
2703 /* assume ok */
2704 e = JIM_OK;
2705 while (len) {
2706 /* Slurp... in buffer size chunks */
2707
2708 count = len; /* in objects.. */
2709 if (count > (sizeof(buffer)/width)) {
2710 count = (sizeof(buffer)/width);
2711 }
2712
2713 retval = target->type->read_memory( target, addr, width, count, buffer );
2714 if (retval != ERROR_OK) {
2715 /* BOO !*/
2716 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed", addr, width, count);
2717 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2718 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
2719 e = JIM_ERR;
2720 len = 0;
2721 } else {
2722 v = 0; /* shut up gcc */
2723 for (i = 0 ;i < count ;i++, n++) {
2724 switch (width) {
2725 case 4:
2726 v = target_buffer_get_u32(target, &buffer[i*width]);
2727 break;
2728 case 2:
2729 v = target_buffer_get_u16(target, &buffer[i*width]);
2730 break;
2731 case 1:
2732 v = buffer[i] & 0x0ff;
2733 break;
2734 }
2735 new_int_array_element(interp, varname, n, v);
2736 }
2737 len -= count;
2738 }
2739 }
2740
2741 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2742
2743 return JIM_OK;
2744 }
2745
2746 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, u32 *val)
2747 {
2748 char *namebuf;
2749 Jim_Obj *nameObjPtr, *valObjPtr;
2750 int result;
2751 long l;
2752
2753 namebuf = alloc_printf("%s(%d)", varname, idx);
2754 if (!namebuf)
2755 return JIM_ERR;
2756
2757 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
2758 if (!nameObjPtr)
2759 {
2760 free(namebuf);
2761 return JIM_ERR;
2762 }
2763
2764 Jim_IncrRefCount(nameObjPtr);
2765 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
2766 Jim_DecrRefCount(interp, nameObjPtr);
2767 free(namebuf);
2768 if (valObjPtr == NULL)
2769 return JIM_ERR;
2770
2771 result = Jim_GetLong(interp, valObjPtr, &l);
2772 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
2773 *val = l;
2774 return result;
2775 }
2776
2777 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
2778 {
2779 target_t *target;
2780 command_context_t *context;
2781 long l;
2782 u32 width;
2783 u32 len;
2784 u32 addr;
2785 u32 count;
2786 u32 v;
2787 const char *varname;
2788 u8 buffer[4096];
2789 int i, n, e, retval;
2790
2791 /* argv[1] = name of array to get the data
2792 * argv[2] = desired width
2793 * argv[3] = memory address
2794 * argv[4] = count to write
2795 */
2796 if (argc != 5) {
2797 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
2798 return JIM_ERR;
2799 }
2800 varname = Jim_GetString(argv[1], &len);
2801 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
2802
2803 e = Jim_GetLong(interp, argv[2], &l);
2804 width = l;
2805 if (e != JIM_OK) {
2806 return e;
2807 }
2808
2809 e = Jim_GetLong(interp, argv[3], &l);
2810 addr = l;
2811 if (e != JIM_OK) {
2812 return e;
2813 }
2814 e = Jim_GetLong(interp, argv[4], &l);
2815 len = l;
2816 if (e != JIM_OK) {
2817 return e;
2818 }
2819 switch (width) {
2820 case 8:
2821 width = 1;
2822 break;
2823 case 16:
2824 width = 2;
2825 break;
2826 case 32:
2827 width = 4;
2828 break;
2829 default:
2830 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2831 Jim_AppendStrings( interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL );
2832 return JIM_ERR;
2833 }
2834 if (len == 0) {
2835 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2836 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
2837 return JIM_ERR;
2838 }
2839 if ((addr + (len * width)) < addr) {
2840 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2841 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
2842 return JIM_ERR;
2843 }
2844 /* absurd transfer size? */
2845 if (len > 65536) {
2846 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2847 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
2848 return JIM_ERR;
2849 }
2850
2851 if ((width == 1) ||
2852 ((width == 2) && ((addr & 1) == 0)) ||
2853 ((width == 4) && ((addr & 3) == 0))) {
2854 /* all is well */
2855 } else {
2856 char buf[100];
2857 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2858 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads", addr, width);
2859 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
2860 return JIM_ERR;
2861 }
2862
2863 context = Jim_GetAssocData(interp, "context");
2864 if (context == NULL)
2865 {
2866 LOG_ERROR("array2mem: no command context");
2867 return JIM_ERR;
2868 }
2869 target = get_current_target(context);
2870 if (target == NULL)
2871 {
2872 LOG_ERROR("array2mem: no current target");
2873 return JIM_ERR;
2874 }
2875
2876 /* Transfer loop */
2877
2878 /* index counter */
2879 n = 0;
2880 /* assume ok */
2881 e = JIM_OK;
2882 while (len) {
2883 /* Slurp... in buffer size chunks */
2884
2885 count = len; /* in objects.. */
2886 if (count > (sizeof(buffer)/width)) {
2887 count = (sizeof(buffer)/width);
2888 }
2889
2890 v = 0; /* shut up gcc */
2891 for (i = 0 ;i < count ;i++, n++) {
2892 get_int_array_element(interp, varname, n, &v);
2893 switch (width) {
2894 case 4:
2895 target_buffer_set_u32(target, &buffer[i*width], v);
2896 break;
2897 case 2:
2898 target_buffer_set_u16(target, &buffer[i*width], v);
2899 break;
2900 case 1:
2901 buffer[i] = v & 0x0ff;
2902 break;
2903 }
2904 }
2905 len -= count;
2906
2907 retval = target->type->write_memory(target, addr, width, count, buffer);
2908 if (retval != ERROR_OK) {
2909 /* BOO !*/
2910 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed", addr, width, count);
2911 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2912 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
2913 e = JIM_ERR;
2914 len = 0;
2915 }
2916 }
2917
2918 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2919
2920 return JIM_OK;
2921 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)