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

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)