- merged support for Cortex-M3 from cortex-m3 branch (thanks to Magnus Lundin)
[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
27 #include "log.h"
28 #include "configuration.h"
29 #include "binarybuffer.h"
30 #include "jtag.h"
31
32 #include <string.h>
33 #include <stdlib.h>
34
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
38 #include <errno.h>
39
40 #include <sys/time.h>
41 #include <time.h>
42
43 #include <time_support.h>
44
45 #include <fileio.h>
46 #include <image.h>
47
48 int cli_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv);
49
50 int handle_target_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
51 int handle_daemon_startup_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
52 int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
53
54 int handle_target_script_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
55 int handle_run_and_halt_time_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
56 int handle_working_area_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
57
58 int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
59 int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
60 int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
61 int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
62 int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
63 int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
64 int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
65 int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
66 int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
67 int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
68 int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
69 int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
70 int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
71 int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
72 int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
73 int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
74
75 /* targets
76 */
77 extern target_type_t arm7tdmi_target;
78 extern target_type_t arm720t_target;
79 extern target_type_t arm9tdmi_target;
80 extern target_type_t arm920t_target;
81 extern target_type_t arm966e_target;
82 extern target_type_t arm926ejs_target;
83 extern target_type_t xscale_target;
84 extern target_type_t cortexm3_target;
85
86 target_type_t *target_types[] =
87 {
88 &arm7tdmi_target,
89 &arm9tdmi_target,
90 &arm920t_target,
91 &arm720t_target,
92 &arm966e_target,
93 &arm926ejs_target,
94 &xscale_target,
95 &cortexm3_target,
96 NULL,
97 };
98
99 target_t *targets = NULL;
100 target_event_callback_t *target_event_callbacks = NULL;
101 target_timer_callback_t *target_timer_callbacks = NULL;
102
103 char *target_state_strings[] =
104 {
105 "unknown",
106 "running",
107 "halted",
108 "reset",
109 "debug_running",
110 };
111
112 char *target_debug_reason_strings[] =
113 {
114 "debug request", "breakpoint", "watchpoint",
115 "watchpoint and breakpoint", "single step",
116 "target not halted"
117 };
118
119 char *target_endianess_strings[] =
120 {
121 "big endian",
122 "little endian",
123 };
124
125 enum daemon_startup_mode startup_mode = DAEMON_ATTACH;
126
127 static int target_continous_poll = 1;
128
129 /* read a u32 from a buffer in target memory endianness */
130 u32 target_buffer_get_u32(target_t *target, u8 *buffer)
131 {
132 if (target->endianness == TARGET_LITTLE_ENDIAN)
133 return le_to_h_u32(buffer);
134 else
135 return be_to_h_u32(buffer);
136 }
137
138 /* read a u16 from a buffer in target memory endianness */
139 u16 target_buffer_get_u16(target_t *target, u8 *buffer)
140 {
141 if (target->endianness == TARGET_LITTLE_ENDIAN)
142 return le_to_h_u16(buffer);
143 else
144 return be_to_h_u16(buffer);
145 }
146
147 /* write a u32 to a buffer in target memory endianness */
148 void target_buffer_set_u32(target_t *target, u8 *buffer, u32 value)
149 {
150 if (target->endianness == TARGET_LITTLE_ENDIAN)
151 h_u32_to_le(buffer, value);
152 else
153 h_u32_to_be(buffer, value);
154 }
155
156 /* write a u16 to a buffer in target memory endianness */
157 void target_buffer_set_u16(target_t *target, u8 *buffer, u16 value)
158 {
159 if (target->endianness == TARGET_LITTLE_ENDIAN)
160 h_u16_to_le(buffer, value);
161 else
162 h_u16_to_be(buffer, value);
163 }
164
165 /* returns a pointer to the n-th configured target */
166 target_t* get_target_by_num(int num)
167 {
168 target_t *target = targets;
169 int i = 0;
170
171 while (target)
172 {
173 if (num == i)
174 return target;
175 target = target->next;
176 i++;
177 }
178
179 return NULL;
180 }
181
182 int get_num_by_target(target_t *query_target)
183 {
184 target_t *target = targets;
185 int i = 0;
186
187 while (target)
188 {
189 if (target == query_target)
190 return i;
191 target = target->next;
192 i++;
193 }
194
195 return -1;
196 }
197
198 target_t* get_current_target(command_context_t *cmd_ctx)
199 {
200 target_t *target = get_target_by_num(cmd_ctx->current_target);
201
202 if (target == NULL)
203 {
204 ERROR("BUG: current_target out of bounds");
205 exit(-1);
206 }
207
208 return target;
209 }
210
211 /* Process target initialization, when target entered debug out of reset
212 * the handler is unregistered at the end of this function, so it's only called once
213 */
214 int target_init_handler(struct target_s *target, enum target_event event, void *priv)
215 {
216 FILE *script;
217 struct command_context_s *cmd_ctx = priv;
218
219 if ((event == TARGET_EVENT_HALTED) && (target->reset_script))
220 {
221 target_unregister_event_callback(target_init_handler, priv);
222
223 script = fopen(target->reset_script, "r");
224 if (!script)
225 {
226 ERROR("couldn't open script file %s", target->reset_script);
227 return ERROR_OK;
228 }
229
230 INFO("executing reset script '%s'", target->reset_script);
231 command_run_file(cmd_ctx, script, COMMAND_EXEC);
232 fclose(script);
233
234 jtag_execute_queue();
235 }
236
237 return ERROR_OK;
238 }
239
240 int target_run_and_halt_handler(void *priv)
241 {
242 target_t *target = priv;
243
244 target->type->halt(target);
245
246 return ERROR_OK;
247 }
248
249 int target_process_reset(struct command_context_s *cmd_ctx)
250 {
251 int retval = ERROR_OK;
252 target_t *target;
253
254 /* prepare reset_halt where necessary */
255 target = targets;
256 while (target)
257 {
258 switch (target->reset_mode)
259 {
260 case RESET_HALT:
261 case RESET_INIT:
262 target->type->prepare_reset_halt(target);
263 break;
264 default:
265 break;
266 }
267 target = target->next;
268 }
269
270 target = targets;
271 while (target)
272 {
273 target->type->assert_reset(target);
274 target = target->next;
275 }
276 jtag_execute_queue();
277
278 /* request target halt if necessary, and schedule further action */
279 target = targets;
280 while (target)
281 {
282 switch (target->reset_mode)
283 {
284 case RESET_RUN:
285 /* nothing to do if target just wants to be run */
286 break;
287 case RESET_RUN_AND_HALT:
288 /* schedule halt */
289 target_register_timer_callback(target_run_and_halt_handler, target->run_and_halt_time, 0, target);
290 break;
291 case RESET_RUN_AND_INIT:
292 /* schedule halt */
293 target_register_timer_callback(target_run_and_halt_handler, target->run_and_halt_time, 0, target);
294 target_register_event_callback(target_init_handler, cmd_ctx);
295 break;
296 case RESET_HALT:
297 target->type->halt(target);
298 break;
299 case RESET_INIT:
300 target->type->halt(target);
301 target_register_event_callback(target_init_handler, cmd_ctx);
302 break;
303 default:
304 ERROR("BUG: unknown target->reset_mode");
305 }
306 target = target->next;
307 }
308
309 target = targets;
310 while (target)
311 {
312 target->type->deassert_reset(target);
313 target = target->next;
314 }
315 jtag_execute_queue();
316
317 return retval;
318 }
319
320 int target_init(struct command_context_s *cmd_ctx)
321 {
322 target_t *target = targets;
323
324 while (target)
325 {
326 if (target->type->init_target(cmd_ctx, target) != ERROR_OK)
327 {
328 ERROR("target '%s' init failed", target->type->name);
329 exit(-1);
330 }
331 target = target->next;
332 }
333
334 if (targets)
335 {
336 target_register_user_commands(cmd_ctx);
337 target_register_timer_callback(handle_target, 100, 1, NULL);
338 }
339
340 if (startup_mode == DAEMON_RESET)
341 target_process_reset(cmd_ctx);
342
343 return ERROR_OK;
344 }
345
346 int target_register_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
347 {
348 target_event_callback_t **callbacks_p = &target_event_callbacks;
349
350 if (callback == NULL)
351 {
352 return ERROR_INVALID_ARGUMENTS;
353 }
354
355 if (*callbacks_p)
356 {
357 while ((*callbacks_p)->next)
358 callbacks_p = &((*callbacks_p)->next);
359 callbacks_p = &((*callbacks_p)->next);
360 }
361
362 (*callbacks_p) = malloc(sizeof(target_event_callback_t));
363 (*callbacks_p)->callback = callback;
364 (*callbacks_p)->priv = priv;
365 (*callbacks_p)->next = NULL;
366
367 return ERROR_OK;
368 }
369
370 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
371 {
372 target_timer_callback_t **callbacks_p = &target_timer_callbacks;
373 struct timeval now;
374
375 if (callback == NULL)
376 {
377 return ERROR_INVALID_ARGUMENTS;
378 }
379
380 if (*callbacks_p)
381 {
382 while ((*callbacks_p)->next)
383 callbacks_p = &((*callbacks_p)->next);
384 callbacks_p = &((*callbacks_p)->next);
385 }
386
387 (*callbacks_p) = malloc(sizeof(target_timer_callback_t));
388 (*callbacks_p)->callback = callback;
389 (*callbacks_p)->periodic = periodic;
390 (*callbacks_p)->time_ms = time_ms;
391
392 gettimeofday(&now, NULL);
393 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
394 time_ms -= (time_ms % 1000);
395 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
396 if ((*callbacks_p)->when.tv_usec > 1000000)
397 {
398 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
399 (*callbacks_p)->when.tv_sec += 1;
400 }
401
402 (*callbacks_p)->priv = priv;
403 (*callbacks_p)->next = NULL;
404
405 return ERROR_OK;
406 }
407
408 int target_unregister_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
409 {
410 target_event_callback_t **p = &target_event_callbacks;
411 target_event_callback_t *c = target_event_callbacks;
412
413 if (callback == NULL)
414 {
415 return ERROR_INVALID_ARGUMENTS;
416 }
417
418 while (c)
419 {
420 target_event_callback_t *next = c->next;
421 if ((c->callback == callback) && (c->priv == priv))
422 {
423 *p = next;
424 free(c);
425 return ERROR_OK;
426 }
427 else
428 p = &(c->next);
429 c = next;
430 }
431
432 return ERROR_OK;
433 }
434
435 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
436 {
437 target_timer_callback_t **p = &target_timer_callbacks;
438 target_timer_callback_t *c = target_timer_callbacks;
439
440 if (callback == NULL)
441 {
442 return ERROR_INVALID_ARGUMENTS;
443 }
444
445 while (c)
446 {
447 target_timer_callback_t *next = c->next;
448 if ((c->callback == callback) && (c->priv == priv))
449 {
450 *p = next;
451 free(c);
452 return ERROR_OK;
453 }
454 else
455 p = &(c->next);
456 c = next;
457 }
458
459 return ERROR_OK;
460 }
461
462 int target_call_event_callbacks(target_t *target, enum target_event event)
463 {
464 target_event_callback_t *callback = target_event_callbacks;
465 target_event_callback_t *next_callback;
466
467 DEBUG("target event %i", event);
468
469 while (callback)
470 {
471 next_callback = callback->next;
472 callback->callback(target, event, callback->priv);
473 callback = next_callback;
474 }
475
476 return ERROR_OK;
477 }
478
479 int target_call_timer_callbacks()
480 {
481 target_timer_callback_t *callback = target_timer_callbacks;
482 target_timer_callback_t *next_callback;
483 struct timeval now;
484
485 gettimeofday(&now, NULL);
486
487 while (callback)
488 {
489 next_callback = callback->next;
490
491 if (((now.tv_sec >= callback->when.tv_sec) && (now.tv_usec >= callback->when.tv_usec))
492 || (now.tv_sec > callback->when.tv_sec))
493 {
494 callback->callback(callback->priv);
495 if (callback->periodic)
496 {
497 int time_ms = callback->time_ms;
498 callback->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
499 time_ms -= (time_ms % 1000);
500 callback->when.tv_sec = now.tv_sec + time_ms / 1000;
501 if (callback->when.tv_usec > 1000000)
502 {
503 callback->when.tv_usec = callback->when.tv_usec - 1000000;
504 callback->when.tv_sec += 1;
505 }
506 }
507 else
508 target_unregister_timer_callback(callback->callback, callback->priv);
509 }
510
511 callback = next_callback;
512 }
513
514 return ERROR_OK;
515 }
516
517 int target_alloc_working_area(struct target_s *target, u32 size, working_area_t **area)
518 {
519 working_area_t *c = target->working_areas;
520 working_area_t *new_wa = NULL;
521
522 /* only allocate multiples of 4 byte */
523 if (size % 4)
524 {
525 ERROR("BUG: code tried to allocate unaligned number of bytes, padding");
526 size = CEIL(size, 4);
527 }
528
529 /* see if there's already a matching working area */
530 while (c)
531 {
532 if ((c->free) && (c->size == size))
533 {
534 new_wa = c;
535 break;
536 }
537 c = c->next;
538 }
539
540 /* if not, allocate a new one */
541 if (!new_wa)
542 {
543 working_area_t **p = &target->working_areas;
544 u32 first_free = target->working_area;
545 u32 free_size = target->working_area_size;
546
547 DEBUG("allocating new working area");
548
549 c = target->working_areas;
550 while (c)
551 {
552 first_free += c->size;
553 free_size -= c->size;
554 p = &c->next;
555 c = c->next;
556 }
557
558 if (free_size < size)
559 {
560 WARNING("not enough working area available");
561 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
562 }
563
564 new_wa = malloc(sizeof(working_area_t));
565 new_wa->next = NULL;
566 new_wa->size = size;
567 new_wa->address = first_free;
568
569 if (target->backup_working_area)
570 {
571 new_wa->backup = malloc(new_wa->size);
572 target->type->read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup);
573 }
574 else
575 {
576 new_wa->backup = NULL;
577 }
578
579 /* put new entry in list */
580 *p = new_wa;
581 }
582
583 /* mark as used, and return the new (reused) area */
584 new_wa->free = 0;
585 *area = new_wa;
586
587 /* user pointer */
588 new_wa->user = area;
589
590 return ERROR_OK;
591 }
592
593 int target_free_working_area(struct target_s *target, working_area_t *area)
594 {
595 if (area->free)
596 return ERROR_OK;
597
598 if (target->backup_working_area)
599 target->type->write_memory(target, area->address, 4, area->size / 4, area->backup);
600
601 area->free = 1;
602
603 /* mark user pointer invalid */
604 *area->user = NULL;
605 area->user = NULL;
606
607 return ERROR_OK;
608 }
609
610 int target_free_all_working_areas(struct target_s *target)
611 {
612 working_area_t *c = target->working_areas;
613
614 while (c)
615 {
616 working_area_t *next = c->next;
617 target_free_working_area(target, c);
618
619 if (c->backup)
620 free(c->backup);
621
622 free(c);
623
624 c = next;
625 }
626
627 target->working_areas = NULL;
628
629 return ERROR_OK;
630 }
631
632 int target_register_commands(struct command_context_s *cmd_ctx)
633 {
634 register_command(cmd_ctx, NULL, "target", handle_target_command, COMMAND_CONFIG, NULL);
635 register_command(cmd_ctx, NULL, "targets", handle_targets_command, COMMAND_EXEC, NULL);
636 register_command(cmd_ctx, NULL, "daemon_startup", handle_daemon_startup_command, COMMAND_CONFIG, NULL);
637 register_command(cmd_ctx, NULL, "target_script", handle_target_script_command, COMMAND_CONFIG, NULL);
638 register_command(cmd_ctx, NULL, "run_and_halt_time", handle_run_and_halt_time_command, COMMAND_CONFIG, NULL);
639 register_command(cmd_ctx, NULL, "working_area", handle_working_area_command, COMMAND_CONFIG, NULL);
640
641 return ERROR_OK;
642 }
643
644 int target_write_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
645 {
646 int retval;
647
648 DEBUG("writing buffer of %i byte at 0x%8.8x", size, address);
649
650 /* handle writes of less than 4 byte */
651 if (size < 4)
652 {
653 if ((retval = target->type->write_memory(target, address, 1, size, buffer)) != ERROR_OK)
654 return retval;
655 }
656
657 /* handle unaligned head bytes */
658 if (address % 4)
659 {
660 int unaligned = 4 - (address % 4);
661
662 if ((retval = target->type->write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
663 return retval;
664
665 buffer += unaligned;
666 address += unaligned;
667 size -= unaligned;
668 }
669
670 /* handle aligned words */
671 if (size >= 4)
672 {
673 int aligned = size - (size % 4);
674
675 /* use bulk writes above a certain limit. This may have to be changed */
676 if (aligned > 128)
677 {
678 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
679 return retval;
680 }
681 else
682 {
683 if ((retval = target->type->write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
684 return retval;
685 }
686
687 buffer += aligned;
688 address += aligned;
689 size -= aligned;
690 }
691
692 /* handle tail writes of less than 4 bytes */
693 if (size > 0)
694 {
695 if ((retval = target->type->write_memory(target, address, 1, size, buffer)) != ERROR_OK)
696 return retval;
697 }
698
699 return ERROR_OK;
700 }
701
702 int target_read_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
703 {
704 int retval;
705
706 DEBUG("reading buffer of %i byte at 0x%8.8x", size, address);
707
708 /* handle reads of less than 4 byte */
709 if (size < 4)
710 {
711 if ((retval = target->type->read_memory(target, address, 1, size, buffer)) != ERROR_OK)
712 return retval;
713 }
714
715 /* handle unaligned head bytes */
716 if (address % 4)
717 {
718 int unaligned = 4 - (address % 4);
719
720 if ((retval = target->type->read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
721 return retval;
722
723 buffer += unaligned;
724 address += unaligned;
725 size -= unaligned;
726 }
727
728 /* handle aligned words */
729 if (size >= 4)
730 {
731 int aligned = size - (size % 4);
732
733 if ((retval = target->type->read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
734 return retval;
735
736 buffer += aligned;
737 address += aligned;
738 size -= aligned;
739 }
740
741 /* handle tail writes of less than 4 bytes */
742 if (size > 0)
743 {
744 if ((retval = target->type->read_memory(target, address, 1, size, buffer)) != ERROR_OK)
745 return retval;
746 }
747
748 return ERROR_OK;
749 }
750
751 int target_read_u32(struct target_s *target, u32 address, u32 *value)
752 {
753 u8 value_buf[4];
754
755 int retval = target->type->read_memory(target, address, 4, 1, value_buf);
756
757 if (retval == ERROR_OK)
758 {
759 *value = target_buffer_get_u32(target, value_buf);
760 DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, *value);
761 }
762 else
763 {
764 *value = 0x0;
765 DEBUG("address: 0x%8.8x failed", address);
766 }
767
768 return retval;
769 }
770
771 int target_read_u16(struct target_s *target, u32 address, u16 *value)
772 {
773 u8 value_buf[2];
774
775 int retval = target->type->read_memory(target, address, 2, 1, value_buf);
776
777 if (retval == ERROR_OK)
778 {
779 *value = target_buffer_get_u16(target, value_buf);
780 DEBUG("address: 0x%8.8x, value: 0x%4.4x", address, *value);
781 }
782 else
783 {
784 *value = 0x0;
785 DEBUG("address: 0x%8.8x failed", address);
786 }
787
788 return retval;
789 }
790
791 int target_read_u8(struct target_s *target, u32 address, u8 *value)
792 {
793 int retval = target->type->read_memory(target, address, 1, 1, value);
794
795 if (retval == ERROR_OK)
796 {
797 DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, *value);
798 }
799 else
800 {
801 *value = 0x0;
802 DEBUG("address: 0x%8.8x failed", address);
803 }
804
805 return retval;
806 }
807
808 int target_write_u32(struct target_s *target, u32 address, u32 value)
809 {
810 int retval;
811 u8 value_buf[4];
812
813 DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
814
815 target_buffer_set_u32(target, value_buf, value);
816 if ((retval = target->type->write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
817 {
818 DEBUG("failed: %i", retval);
819 }
820
821 return retval;
822 }
823
824 int target_write_u16(struct target_s *target, u32 address, u16 value)
825 {
826 int retval;
827 u8 value_buf[2];
828
829 DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
830
831 target_buffer_set_u16(target, value_buf, value);
832 if ((retval = target->type->write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
833 {
834 DEBUG("failed: %i", retval);
835 }
836
837 return retval;
838 }
839
840 int target_write_u8(struct target_s *target, u32 address, u8 value)
841 {
842 int retval;
843
844 DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, value);
845
846 if ((retval = target->type->read_memory(target, address, 1, 1, &value)) != ERROR_OK)
847 {
848 DEBUG("failed: %i", retval);
849 }
850
851 return retval;
852 }
853
854 int target_register_user_commands(struct command_context_s *cmd_ctx)
855 {
856 register_command(cmd_ctx, NULL, "reg", handle_reg_command, COMMAND_EXEC, NULL);
857 register_command(cmd_ctx, NULL, "poll", handle_poll_command, COMMAND_EXEC, "poll target state");
858 register_command(cmd_ctx, NULL, "wait_halt", handle_wait_halt_command, COMMAND_EXEC, "wait for target halt [time (s)]");
859 register_command(cmd_ctx, NULL, "halt", handle_halt_command, COMMAND_EXEC, "halt target");
860 register_command(cmd_ctx, NULL, "resume", handle_resume_command, COMMAND_EXEC, "resume target [addr]");
861 register_command(cmd_ctx, NULL, "step", handle_step_command, COMMAND_EXEC, "step one instruction");
862 register_command(cmd_ctx, NULL, "reset", handle_reset_command, COMMAND_EXEC, "reset target [run|halt|init|run_and_halt|run_and_init]");
863 register_command(cmd_ctx, NULL, "soft_reset_halt", handle_soft_reset_halt_command, COMMAND_EXEC, "halt the target and do a soft reset");
864
865 register_command(cmd_ctx, NULL, "mdw", handle_md_command, COMMAND_EXEC, "display memory words <addr> [count]");
866 register_command(cmd_ctx, NULL, "mdh", handle_md_command, COMMAND_EXEC, "display memory half-words <addr> [count]");
867 register_command(cmd_ctx, NULL, "mdb", handle_md_command, COMMAND_EXEC, "display memory bytes <addr> [count]");
868
869 register_command(cmd_ctx, NULL, "mww", handle_mw_command, COMMAND_EXEC, "write memory word <addr> <value>");
870 register_command(cmd_ctx, NULL, "mwh", handle_mw_command, COMMAND_EXEC, "write memory half-word <addr> <value>");
871 register_command(cmd_ctx, NULL, "mwb", handle_mw_command, COMMAND_EXEC, "write memory byte <addr> <value>");
872
873 register_command(cmd_ctx, NULL, "bp", handle_bp_command, COMMAND_EXEC, "set breakpoint <address> <length> [hw]");
874 register_command(cmd_ctx, NULL, "rbp", handle_rbp_command, COMMAND_EXEC, "remove breakpoint <adress>");
875 register_command(cmd_ctx, NULL, "wp", handle_wp_command, COMMAND_EXEC, "set watchpoint <address> <length> <r/w/a> [value] [mask]");
876 register_command(cmd_ctx, NULL, "rwp", handle_rwp_command, COMMAND_EXEC, "remove watchpoint <adress>");
877
878 register_command(cmd_ctx, NULL, "load_image", handle_load_image_command, COMMAND_EXEC, "load_image <file> <address> ['bin'|'ihex'|'elf']");
879 register_command(cmd_ctx, NULL, "dump_image", handle_dump_image_command, COMMAND_EXEC, "dump_image <file> <address> <size>");
880 register_command(cmd_ctx, NULL, "load_binary", handle_load_image_command, COMMAND_EXEC, "[DEPRECATED] load_binary <file> <address>");
881 register_command(cmd_ctx, NULL, "dump_binary", handle_dump_image_command, COMMAND_EXEC, "[DEPRECATED] dump_binary <file> <address> <size>");
882
883 return ERROR_OK;
884 }
885
886 int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
887 {
888 target_t *target = targets;
889 int count = 0;
890
891 if (argc == 1)
892 {
893 int num = strtoul(args[0], NULL, 0);
894
895 while (target)
896 {
897 count++;
898 target = target->next;
899 }
900
901 if (num < count)
902 cmd_ctx->current_target = num;
903 else
904 command_print(cmd_ctx, "%i is out of bounds, only %i targets are configured", num, count);
905
906 return ERROR_OK;
907 }
908
909 while (target)
910 {
911 command_print(cmd_ctx, "%i: %s (%s), state: %s", count++, target->type->name, target_endianess_strings[target->endianness], target_state_strings[target->state]);
912 target = target->next;
913 }
914
915 return ERROR_OK;
916 }
917
918 int handle_target_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
919 {
920 int i;
921 int found = 0;
922
923 if (argc < 3)
924 {
925 ERROR("target command requires at least three arguments: <type> <endianess> <reset_mode>");
926 exit(-1);
927 }
928
929 /* search for the specified target */
930 if (args[0] && (args[0][0] != 0))
931 {
932 for (i = 0; target_types[i]; i++)
933 {
934 if (strcmp(args[0], target_types[i]->name) == 0)
935 {
936 target_t **last_target_p = &targets;
937
938 /* register target specific commands */
939 if (target_types[i]->register_commands(cmd_ctx) != ERROR_OK)
940 {
941 ERROR("couldn't register '%s' commands", args[0]);
942 exit(-1);
943 }
944
945 if (*last_target_p)
946 {
947 while ((*last_target_p)->next)
948 last_target_p = &((*last_target_p)->next);
949 last_target_p = &((*last_target_p)->next);
950 }
951
952 *last_target_p = malloc(sizeof(target_t));
953
954 (*last_target_p)->type = target_types[i];
955
956 if (strcmp(args[1], "big") == 0)
957 (*last_target_p)->endianness = TARGET_BIG_ENDIAN;
958 else if (strcmp(args[1], "little") == 0)
959 (*last_target_p)->endianness = TARGET_LITTLE_ENDIAN;
960 else
961 {
962 ERROR("endianness must be either 'little' or 'big', not '%s'", args[1]);
963 exit(-1);
964 }
965
966 /* what to do on a target reset */
967 if (strcmp(args[2], "reset_halt") == 0)
968 (*last_target_p)->reset_mode = RESET_HALT;
969 else if (strcmp(args[2], "reset_run") == 0)
970 (*last_target_p)->reset_mode = RESET_RUN;
971 else if (strcmp(args[2], "reset_init") == 0)
972 (*last_target_p)->reset_mode = RESET_INIT;
973 else if (strcmp(args[2], "run_and_halt") == 0)
974 (*last_target_p)->reset_mode = RESET_RUN_AND_HALT;
975 else if (strcmp(args[2], "run_and_init") == 0)
976 (*last_target_p)->reset_mode = RESET_RUN_AND_INIT;
977 else
978 {
979 ERROR("unknown target startup mode %s", args[2]);
980 exit(-1);
981 }
982 (*last_target_p)->run_and_halt_time = 1000; /* default 1s */
983
984 (*last_target_p)->reset_script = NULL;
985 (*last_target_p)->post_halt_script = NULL;
986 (*last_target_p)->pre_resume_script = NULL;
987
988 (*last_target_p)->working_area = 0x0;
989 (*last_target_p)->working_area_size = 0x0;
990 (*last_target_p)->working_areas = NULL;
991 (*last_target_p)->backup_working_area = 0;
992
993 (*last_target_p)->state = TARGET_UNKNOWN;
994 (*last_target_p)->reg_cache = NULL;
995 (*last_target_p)->breakpoints = NULL;
996 (*last_target_p)->watchpoints = NULL;
997 (*last_target_p)->next = NULL;
998 (*last_target_p)->arch_info = NULL;
999
1000 (*last_target_p)->type->target_command(cmd_ctx, cmd, args, argc, *last_target_p);
1001
1002 found = 1;
1003 break;
1004 }
1005 }
1006 }
1007
1008 /* no matching target found */
1009 if (!found)
1010 {
1011 ERROR("target '%s' not found", args[0]);
1012 exit(-1);
1013 }
1014
1015 return ERROR_OK;
1016 }
1017
1018 /* usage: target_script <target#> <event> <script_file> */
1019 int handle_target_script_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1020 {
1021 target_t *target = NULL;
1022
1023 if (argc < 3)
1024 {
1025 ERROR("incomplete target_script command");
1026 exit(-1);
1027 }
1028
1029 target = get_target_by_num(strtoul(args[0], NULL, 0));
1030
1031 if (!target)
1032 {
1033 ERROR("target number '%s' not defined", args[0]);
1034 exit(-1);
1035 }
1036
1037 if (strcmp(args[1], "reset") == 0)
1038 {
1039 if (target->reset_script)
1040 free(target->reset_script);
1041 target->reset_script = strdup(args[2]);
1042 }
1043 else if (strcmp(args[1], "post_halt") == 0)
1044 {
1045 if (target->post_halt_script)
1046 free(target->post_halt_script);
1047 target->post_halt_script = strdup(args[2]);
1048 }
1049 else if (strcmp(args[1], "pre_resume") == 0)
1050 {
1051 if (target->pre_resume_script)
1052 free(target->pre_resume_script);
1053 target->pre_resume_script = strdup(args[2]);
1054 }
1055 else
1056 {
1057 ERROR("unknown event type: '%s", args[1]);
1058 exit(-1);
1059 }
1060
1061 return ERROR_OK;
1062 }
1063
1064 int handle_run_and_halt_time_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1065 {
1066 target_t *target = NULL;
1067
1068 if (argc < 2)
1069 {
1070 ERROR("incomplete run_and_halt_time command");
1071 exit(-1);
1072 }
1073
1074 target = get_target_by_num(strtoul(args[0], NULL, 0));
1075
1076 if (!target)
1077 {
1078 ERROR("target number '%s' not defined", args[0]);
1079 exit(-1);
1080 }
1081
1082 target->run_and_halt_time = strtoul(args[1], NULL, 0);
1083
1084 return ERROR_OK;
1085 }
1086
1087 int handle_working_area_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1088 {
1089 target_t *target = NULL;
1090
1091 if (argc < 4)
1092 {
1093 ERROR("incomplete working_area command. usage: working_area <target#> <address> <size> <'backup'|'nobackup'>");
1094 exit(-1);
1095 }
1096
1097 target = get_target_by_num(strtoul(args[0], NULL, 0));
1098
1099 if (!target)
1100 {
1101 ERROR("target number '%s' not defined", args[0]);
1102 exit(-1);
1103 }
1104
1105 target->working_area = strtoul(args[1], NULL, 0);
1106 target->working_area_size = strtoul(args[2], NULL, 0);
1107
1108 if (strcmp(args[3], "backup") == 0)
1109 {
1110 target->backup_working_area = 1;
1111 }
1112 else if (strcmp(args[3], "nobackup") == 0)
1113 {
1114 target->backup_working_area = 0;
1115 }
1116 else
1117 {
1118 ERROR("unrecognized <backup|nobackup> argument (%s)", args[3]);
1119 exit(-1);
1120 }
1121
1122 return ERROR_OK;
1123 }
1124
1125
1126 /* process target state changes */
1127 int handle_target(void *priv)
1128 {
1129 int retval;
1130 target_t *target = targets;
1131
1132 while (target)
1133 {
1134 /* only poll if target isn't already halted */
1135 if (target->state != TARGET_HALTED)
1136 {
1137 if (target_continous_poll)
1138 if ((retval = target->type->poll(target)) < 0)
1139 {
1140 ERROR("couldn't poll target, exiting");
1141 exit(-1);
1142 }
1143 }
1144
1145 target = target->next;
1146 }
1147
1148 return ERROR_OK;
1149 }
1150
1151 int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1152 {
1153 target_t *target;
1154 reg_t *reg = NULL;
1155 int count = 0;
1156 char *value;
1157
1158 DEBUG("-");
1159
1160 target = get_current_target(cmd_ctx);
1161
1162 /* list all available registers for the current target */
1163 if (argc == 0)
1164 {
1165 reg_cache_t *cache = target->reg_cache;
1166
1167 count = 0;
1168 while(cache)
1169 {
1170 int i;
1171 for (i = 0; i < cache->num_regs; i++)
1172 {
1173 value = buf_to_str(cache->reg_list[i].value, cache->reg_list[i].size, 16);
1174 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);
1175 free(value);
1176 }
1177 cache = cache->next;
1178 }
1179
1180 return ERROR_OK;
1181 }
1182
1183 /* access a single register by its ordinal number */
1184 if ((args[0][0] >= '0') && (args[0][0] <= '9'))
1185 {
1186 int num = strtoul(args[0], NULL, 0);
1187 reg_cache_t *cache = target->reg_cache;
1188
1189 count = 0;
1190 while(cache)
1191 {
1192 int i;
1193 for (i = 0; i < cache->num_regs; i++)
1194 {
1195 if (count++ == num)
1196 {
1197 reg = &cache->reg_list[i];
1198 break;
1199 }
1200 }
1201 if (reg)
1202 break;
1203 cache = cache->next;
1204 }
1205
1206 if (!reg)
1207 {
1208 command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1209 return ERROR_OK;
1210 }
1211 } else /* access a single register by its name */
1212 {
1213 reg = register_get_by_name(target->reg_cache, args[0], 1);
1214
1215 if (!reg)
1216 {
1217 command_print(cmd_ctx, "register %s not found in current target", args[0]);
1218 return ERROR_OK;
1219 }
1220 }
1221
1222 /* display a register */
1223 if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
1224 {
1225 if ((argc == 2) && (strcmp(args[1], "force") == 0))
1226 reg->valid = 0;
1227
1228 if (reg->valid == 0)
1229 {
1230 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1231 if (arch_type == NULL)
1232 {
1233 ERROR("BUG: encountered unregistered arch type");
1234 return ERROR_OK;
1235 }
1236 arch_type->get(reg);
1237 }
1238 value = buf_to_str(reg->value, reg->size, 16);
1239 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1240 free(value);
1241 return ERROR_OK;
1242 }
1243
1244 /* set register value */
1245 if (argc == 2)
1246 {
1247 u8 *buf = malloc(CEIL(reg->size, 8));
1248 str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
1249
1250 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1251 if (arch_type == NULL)
1252 {
1253 ERROR("BUG: encountered unregistered arch type");
1254 return ERROR_OK;
1255 }
1256
1257 arch_type->set(reg, buf);
1258
1259 value = buf_to_str(reg->value, reg->size, 16);
1260 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1261 free(value);
1262
1263 free(buf);
1264
1265 return ERROR_OK;
1266 }
1267
1268 command_print(cmd_ctx, "usage: reg <#|name> [value]");
1269
1270 return ERROR_OK;
1271 }
1272
1273 int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1274 {
1275 target_t *target = get_current_target(cmd_ctx);
1276 char buffer[512];
1277
1278 if (argc == 0)
1279 {
1280 command_print(cmd_ctx, "target state: %s", target_state_strings[target->type->poll(target)]);
1281 if (target->state == TARGET_HALTED)
1282 {
1283 target->type->arch_state(target, buffer, 512);
1284 buffer[511] = 0;
1285 command_print(cmd_ctx, "%s", buffer);
1286 }
1287 }
1288 else
1289 {
1290 if (strcmp(args[0], "on") == 0)
1291 {
1292 target_continous_poll = 1;
1293 }
1294 else if (strcmp(args[0], "off") == 0)
1295 {
1296 target_continous_poll = 0;
1297 }
1298 }
1299
1300
1301 return ERROR_OK;
1302 }
1303
1304 int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1305 {
1306 target_t *target = get_current_target(cmd_ctx);
1307 struct timeval timeout, now;
1308
1309 gettimeofday(&timeout, NULL);
1310 if (!argc)
1311 timeval_add_time(&timeout, 5, 0);
1312 else {
1313 char *end;
1314
1315 timeval_add_time(&timeout, strtoul(args[0], &end, 0), 0);
1316 if (*end) {
1317 command_print(cmd_ctx, "usage: wait_halt [seconds]");
1318 return ERROR_OK;
1319 }
1320 }
1321
1322 command_print(cmd_ctx, "waiting for target halted...");
1323
1324 while(target->type->poll(target))
1325 {
1326 if (target->state == TARGET_HALTED)
1327 {
1328 command_print(cmd_ctx, "target halted");
1329 break;
1330 }
1331 target_call_timer_callbacks();
1332
1333 gettimeofday(&now, NULL);
1334 if ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec))
1335 {
1336 command_print(cmd_ctx, "timed out while waiting for target halt");
1337 ERROR("timed out while waiting for target halt");
1338 break;
1339 }
1340 }
1341
1342 return ERROR_OK;
1343 }
1344
1345 int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1346 {
1347 int retval;
1348 target_t *target = get_current_target(cmd_ctx);
1349
1350 DEBUG("-");
1351
1352 command_print(cmd_ctx, "requesting target halt...");
1353
1354 if ((retval = target->type->halt(target)) != ERROR_OK)
1355 {
1356 switch (retval)
1357 {
1358 case ERROR_TARGET_ALREADY_HALTED:
1359 command_print(cmd_ctx, "target already halted");
1360 break;
1361 case ERROR_TARGET_TIMEOUT:
1362 command_print(cmd_ctx, "target timed out... shutting down");
1363 exit(-1);
1364 default:
1365 command_print(cmd_ctx, "unknown error... shutting down");
1366 exit(-1);
1367 }
1368 }
1369
1370 return ERROR_OK;
1371
1372 }
1373
1374 /* what to do on daemon startup */
1375 int handle_daemon_startup_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1376 {
1377 if (argc == 1)
1378 {
1379 if (strcmp(args[0], "attach") == 0)
1380 {
1381 startup_mode = DAEMON_ATTACH;
1382 return ERROR_OK;
1383 }
1384 else if (strcmp(args[0], "reset") == 0)
1385 {
1386 startup_mode = DAEMON_RESET;
1387 return ERROR_OK;
1388 }
1389 }
1390
1391 WARNING("invalid daemon_startup configuration directive: %s", args[0]);
1392 return ERROR_OK;
1393
1394 }
1395
1396 int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1397 {
1398 target_t *target = get_current_target(cmd_ctx);
1399 int retval;
1400
1401 command_print(cmd_ctx, "requesting target halt and executing a soft reset");
1402
1403 if ((retval = target->type->soft_reset_halt(target)) != ERROR_OK)
1404 {
1405 switch (retval)
1406 {
1407 case ERROR_TARGET_TIMEOUT:
1408 command_print(cmd_ctx, "target timed out... shutting down");
1409 exit(-1);
1410 default:
1411 command_print(cmd_ctx, "unknown error... shutting down");
1412 exit(-1);
1413 }
1414 }
1415
1416 return ERROR_OK;
1417 }
1418
1419 int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1420 {
1421 target_t *target = get_current_target(cmd_ctx);
1422 enum target_reset_mode reset_mode = RESET_RUN;
1423
1424 DEBUG("-");
1425
1426 if (argc >= 1)
1427 {
1428 if (strcmp("run", args[0]) == 0)
1429 reset_mode = RESET_RUN;
1430 else if (strcmp("halt", args[0]) == 0)
1431 reset_mode = RESET_HALT;
1432 else if (strcmp("init", args[0]) == 0)
1433 reset_mode = RESET_INIT;
1434 else if (strcmp("run_and_halt", args[0]) == 0)
1435 {
1436 reset_mode = RESET_RUN_AND_HALT;
1437 if (argc >= 2)
1438 {
1439 target->run_and_halt_time = strtoul(args[1], NULL, 0);
1440 }
1441 }
1442 else if (strcmp("run_and_init", args[0]) == 0)
1443 {
1444 reset_mode = RESET_RUN_AND_INIT;
1445 if (argc >= 2)
1446 {
1447 target->run_and_halt_time = strtoul(args[1], NULL, 0);
1448 }
1449 }
1450 else
1451 {
1452 command_print(cmd_ctx, "usage: reset ['run', 'halt', 'init', 'run_and_halt', 'run_and_init]");
1453 return ERROR_OK;
1454 }
1455 target->reset_mode = reset_mode;
1456 }
1457
1458 target_process_reset(cmd_ctx);
1459
1460 return ERROR_OK;
1461 }
1462
1463 int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1464 {
1465 int retval;
1466 target_t *target = get_current_target(cmd_ctx);
1467
1468 DEBUG("-");
1469
1470 if (argc == 0)
1471 retval = target->type->resume(target, 1, 0, 1, 0); /* current pc, addr = 0, handle breakpoints, not debugging */
1472 else if (argc == 1)
1473 retval = target->type->resume(target, 0, strtoul(args[0], NULL, 0), 1, 0); /* addr = args[0], handle breakpoints, not debugging */
1474 else
1475 {
1476 command_print(cmd_ctx, "usage: resume [address]");
1477 return ERROR_OK;
1478 }
1479
1480 if (retval != ERROR_OK)
1481 {
1482 switch (retval)
1483 {
1484 case ERROR_TARGET_NOT_HALTED:
1485 command_print(cmd_ctx, "target not halted");
1486 break;
1487 default:
1488 command_print(cmd_ctx, "unknown error... shutting down");
1489 exit(-1);
1490 }
1491 }
1492
1493 return ERROR_OK;
1494 }
1495
1496 int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1497 {
1498 target_t *target = get_current_target(cmd_ctx);
1499
1500 DEBUG("-");
1501
1502 if (argc == 0)
1503 target->type->step(target, 1, 0, 1); /* current pc, addr = 0, handle breakpoints */
1504
1505 if (argc == 1)
1506 target->type->step(target, 0, strtoul(args[0], NULL, 0), 1); /* addr = args[0], handle breakpoints */
1507
1508 return ERROR_OK;
1509 }
1510
1511 int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1512 {
1513 int count = 1;
1514 int size = 4;
1515 u32 address = 0;
1516 int i;
1517
1518 char output[128];
1519 int output_len;
1520
1521 int retval;
1522
1523 u8 *buffer;
1524 target_t *target = get_current_target(cmd_ctx);
1525
1526 if (argc < 1)
1527 return ERROR_OK;
1528
1529 if (argc == 2)
1530 count = strtoul(args[1], NULL, 0);
1531
1532 address = strtoul(args[0], NULL, 0);
1533
1534
1535 switch (cmd[2])
1536 {
1537 case 'w':
1538 size = 4;
1539 break;
1540 case 'h':
1541 size = 2;
1542 break;
1543 case 'b':
1544 size = 1;
1545 break;
1546 default:
1547 return ERROR_OK;
1548 }
1549
1550 buffer = calloc(count, size);
1551 if ((retval = target->type->read_memory(target, address, size, count, buffer)) != ERROR_OK)
1552 {
1553 switch (retval)
1554 {
1555 case ERROR_TARGET_UNALIGNED_ACCESS:
1556 command_print(cmd_ctx, "error: address not aligned");
1557 break;
1558 case ERROR_TARGET_NOT_HALTED:
1559 command_print(cmd_ctx, "error: target must be halted for memory accesses");
1560 break;
1561 case ERROR_TARGET_DATA_ABORT:
1562 command_print(cmd_ctx, "error: access caused data abort, system possibly corrupted");
1563 break;
1564 default:
1565 command_print(cmd_ctx, "error: unknown error");
1566 break;
1567 }
1568 return ERROR_OK;
1569 }
1570
1571 output_len = 0;
1572
1573 for (i = 0; i < count; i++)
1574 {
1575 if (i%8 == 0)
1576 output_len += snprintf(output + output_len, 128 - output_len, "0x%8.8x: ", address + (i*size));
1577
1578 switch (size)
1579 {
1580 case 4:
1581 output_len += snprintf(output + output_len, 128 - output_len, "%8.8x ", target_buffer_get_u32(target, &buffer[i*4]));
1582 break;
1583 case 2:
1584 output_len += snprintf(output + output_len, 128 - output_len, "%4.4x ", target_buffer_get_u16(target, &buffer[i*2]));
1585 break;
1586 case 1:
1587 output_len += snprintf(output + output_len, 128 - output_len, "%2.2x ", buffer[i*1]);
1588 break;
1589 }
1590
1591 if ((i%8 == 7) || (i == count - 1))
1592 {
1593 command_print(cmd_ctx, output);
1594 output_len = 0;
1595 }
1596 }
1597
1598 free(buffer);
1599
1600 return ERROR_OK;
1601 }
1602
1603 int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1604 {
1605 u32 address = 0;
1606 u32 value = 0;
1607 int retval;
1608 target_t *target = get_current_target(cmd_ctx);
1609 u8 value_buf[4];
1610
1611 if (argc < 2)
1612 return ERROR_OK;
1613
1614 address = strtoul(args[0], NULL, 0);
1615 value = strtoul(args[1], NULL, 0);
1616
1617 switch (cmd[2])
1618 {
1619 case 'w':
1620 target_buffer_set_u32(target, value_buf, value);
1621 retval = target->type->write_memory(target, address, 4, 1, value_buf);
1622 break;
1623 case 'h':
1624 target_buffer_set_u16(target, value_buf, value);
1625 retval = target->type->write_memory(target, address, 2, 1, value_buf);
1626 break;
1627 case 'b':
1628 value_buf[0] = value;
1629 retval = target->type->write_memory(target, address, 1, 1, value_buf);
1630 break;
1631 default:
1632 return ERROR_OK;
1633 }
1634
1635 switch (retval)
1636 {
1637 case ERROR_TARGET_UNALIGNED_ACCESS:
1638 command_print(cmd_ctx, "error: address not aligned");
1639 break;
1640 case ERROR_TARGET_DATA_ABORT:
1641 command_print(cmd_ctx, "error: access caused data abort, system possibly corrupted");
1642 break;
1643 case ERROR_TARGET_NOT_HALTED:
1644 command_print(cmd_ctx, "error: target must be halted for memory accesses");
1645 break;
1646 case ERROR_OK:
1647 break;
1648 default:
1649 command_print(cmd_ctx, "error: unknown error");
1650 break;
1651 }
1652
1653 return ERROR_OK;
1654
1655 }
1656
1657 int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1658 {
1659 u8 *buffer;
1660 u32 buf_cnt;
1661 u32 image_size;
1662 int i;
1663 int retval;
1664
1665 image_t image;
1666
1667 duration_t duration;
1668 char *duration_text;
1669
1670 target_t *target = get_current_target(cmd_ctx);
1671
1672 if (argc < 1)
1673 {
1674 command_print(cmd_ctx, "usage: load_image <filename> [address] [type]");
1675 return ERROR_OK;
1676 }
1677
1678 /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
1679 if (argc >= 2)
1680 {
1681 image.base_address_set = 1;
1682 image.base_address = strtoul(args[1], NULL, 0);
1683 }
1684 else
1685 {
1686 image.base_address_set = 0;
1687 }
1688
1689 image.start_address_set = 0;
1690
1691 duration_start_measure(&duration);
1692
1693 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
1694 {
1695 command_print(cmd_ctx, "load_image error: %s", image.error_str);
1696 return ERROR_OK;
1697 }
1698
1699 image_size = 0x0;
1700 for (i = 0; i < image.num_sections; i++)
1701 {
1702 buffer = malloc(image.sections[i].size);
1703 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
1704 {
1705 ERROR("image_read_section failed with error code: %i", retval);
1706 command_print(cmd_ctx, "image reading failed, download aborted");
1707 free(buffer);
1708 image_close(&image);
1709 return ERROR_OK;
1710 }
1711 target_write_buffer(target, image.sections[i].base_address, buf_cnt, buffer);
1712 image_size += buf_cnt;
1713 command_print(cmd_ctx, "%u byte written at address 0x%8.8x", buf_cnt, image.sections[i].base_address);
1714
1715 free(buffer);
1716 }
1717
1718 duration_stop_measure(&duration, &duration_text);
1719 command_print(cmd_ctx, "downloaded %u byte in %s", image_size, duration_text);
1720 free(duration_text);
1721
1722 image_close(&image);
1723
1724 return ERROR_OK;
1725
1726 }
1727
1728 int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1729 {
1730 fileio_t fileio;
1731
1732 u32 address;
1733 u32 size;
1734 u8 buffer[560];
1735
1736 duration_t duration;
1737 char *duration_text;
1738
1739 target_t *target = get_current_target(cmd_ctx);
1740
1741 if (argc != 3)
1742 {
1743 command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
1744 return ERROR_OK;
1745 }
1746
1747 address = strtoul(args[1], NULL, 0);
1748 size = strtoul(args[2], NULL, 0);
1749
1750 if ((address & 3) || (size & 3))
1751 {
1752 command_print(cmd_ctx, "only 32-bit aligned address and size are supported");
1753 return ERROR_OK;
1754 }
1755
1756 if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
1757 {
1758 command_print(cmd_ctx, "dump_image error: %s", fileio.error_str);
1759 return ERROR_OK;
1760 }
1761
1762 duration_start_measure(&duration);
1763
1764 while (size > 0)
1765 {
1766 u32 size_written;
1767 u32 this_run_size = (size > 560) ? 560 : size;
1768
1769 target->type->read_memory(target, address, 4, this_run_size / 4, buffer);
1770 fileio_write(&fileio, this_run_size, buffer, &size_written);
1771
1772 size -= this_run_size;
1773 address += this_run_size;
1774 }
1775
1776 fileio_close(&fileio);
1777
1778 duration_stop_measure(&duration, &duration_text);
1779 command_print(cmd_ctx, "dumped %lli byte in %s", fileio.size, duration_text);
1780 free(duration_text);
1781
1782 return ERROR_OK;
1783
1784 }
1785
1786 int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1787 {
1788 int retval;
1789 target_t *target = get_current_target(cmd_ctx);
1790
1791 if (argc == 0)
1792 {
1793 breakpoint_t *breakpoint = target->breakpoints;
1794
1795 while (breakpoint)
1796 {
1797 if (breakpoint->type == BKPT_SOFT)
1798 {
1799 char* buf = buf_to_str(breakpoint->orig_instr, breakpoint->length, 16);
1800 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i, 0x%s", breakpoint->address, breakpoint->length, breakpoint->set, buf);
1801 free(buf);
1802 }
1803 else
1804 {
1805 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i", breakpoint->address, breakpoint->length, breakpoint->set);
1806 }
1807 breakpoint = breakpoint->next;
1808 }
1809 }
1810 else if (argc >= 2)
1811 {
1812 int hw = BKPT_SOFT;
1813 u32 length = 0;
1814
1815 length = strtoul(args[1], NULL, 0);
1816
1817 if (argc >= 3)
1818 if (strcmp(args[2], "hw") == 0)
1819 hw = BKPT_HARD;
1820
1821 if ((retval = breakpoint_add(target, strtoul(args[0], NULL, 0), length, hw)) != ERROR_OK)
1822 {
1823 switch (retval)
1824 {
1825 case ERROR_TARGET_NOT_HALTED:
1826 command_print(cmd_ctx, "target must be halted to set breakpoints");
1827 break;
1828 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
1829 command_print(cmd_ctx, "no more breakpoints available");
1830 break;
1831 default:
1832 command_print(cmd_ctx, "unknown error, breakpoint not set");
1833 break;
1834 }
1835 }
1836 else
1837 {
1838 command_print(cmd_ctx, "breakpoint added at address 0x%8.8x", strtoul(args[0], NULL, 0));
1839 }
1840 }
1841 else
1842 {
1843 command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
1844 }
1845
1846 return ERROR_OK;
1847 }
1848
1849 int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1850 {
1851 target_t *target = get_current_target(cmd_ctx);
1852
1853 if (argc > 0)
1854 breakpoint_remove(target, strtoul(args[0], NULL, 0));
1855
1856 return ERROR_OK;
1857 }
1858
1859 int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1860 {
1861 target_t *target = get_current_target(cmd_ctx);
1862
1863 if (argc == 0)
1864 {
1865 watchpoint_t *watchpoint = target->watchpoints;
1866
1867 while (watchpoint)
1868 {
1869 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);
1870 watchpoint = watchpoint->next;
1871 }
1872 }
1873 else if (argc >= 2)
1874 {
1875 enum watchpoint_rw type = WPT_ACCESS;
1876 u32 data_value = 0x0;
1877 u32 data_mask = 0xffffffff;
1878
1879 if (argc >= 3)
1880 {
1881 switch(args[2][0])
1882 {
1883 case 'r':
1884 type = WPT_READ;
1885 break;
1886 case 'w':
1887 type = WPT_WRITE;
1888 break;
1889 case 'a':
1890 type = WPT_ACCESS;
1891 break;
1892 default:
1893 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
1894 return ERROR_OK;
1895 }
1896 }
1897 if (argc >= 4)
1898 {
1899 data_value = strtoul(args[3], NULL, 0);
1900 }
1901 if (argc >= 5)
1902 {
1903 data_mask = strtoul(args[4], NULL, 0);
1904 }
1905 watchpoint_add(target, strtoul(args[0], NULL, 0), strtoul(args[1], NULL, 0), type, data_value, data_mask);
1906 }
1907 else
1908 {
1909 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
1910 }
1911
1912 return ERROR_OK;
1913 }
1914
1915 int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1916 {
1917 target_t *target = get_current_target(cmd_ctx);
1918
1919 if (argc > 0)
1920 watchpoint_remove(target, strtoul(args[0], NULL, 0));
1921
1922 return ERROR_OK;
1923 }
1924

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)