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

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)