1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
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. *
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. *
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 ***************************************************************************/
24 #include "replacements.h"
28 #include "configuration.h"
29 #include "binarybuffer.h"
35 #include <sys/types.h>
43 #include <time_support.h>
48 int cli_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv);
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);
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);
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);
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;
86 target_type_t *target_types[] =
99 target_t *targets = NULL;
100 target_event_callback_t *target_event_callbacks = NULL;
101 target_timer_callback_t *target_timer_callbacks = NULL;
103 char *target_state_strings[] =
112 char *target_debug_reason_strings[] =
114 "debug request", "breakpoint", "watchpoint",
115 "watchpoint and breakpoint", "single step",
119 char *target_endianess_strings[] =
125 enum daemon_startup_mode startup_mode = DAEMON_ATTACH;
127 static int target_continous_poll = 1;
129 /* read a u32 from a buffer in target memory endianness */
130 u32 target_buffer_get_u32(target_t *target, u8 *buffer)
132 if (target->endianness == TARGET_LITTLE_ENDIAN)
133 return le_to_h_u32(buffer);
135 return be_to_h_u32(buffer);
138 /* read a u16 from a buffer in target memory endianness */
139 u16 target_buffer_get_u16(target_t *target, u8 *buffer)
141 if (target->endianness == TARGET_LITTLE_ENDIAN)
142 return le_to_h_u16(buffer);
144 return be_to_h_u16(buffer);
147 /* write a u32 to a buffer in target memory endianness */
148 void target_buffer_set_u32(target_t *target, u8 *buffer, u32 value)
150 if (target->endianness == TARGET_LITTLE_ENDIAN)
151 h_u32_to_le(buffer, value);
153 h_u32_to_be(buffer, value);
156 /* write a u16 to a buffer in target memory endianness */
157 void target_buffer_set_u16(target_t *target, u8 *buffer, u16 value)
159 if (target->endianness == TARGET_LITTLE_ENDIAN)
160 h_u16_to_le(buffer, value);
162 h_u16_to_be(buffer, value);
165 /* returns a pointer to the n-th configured target */
166 target_t* get_target_by_num(int num)
168 target_t *target = targets;
175 target = target->next;
182 int get_num_by_target(target_t *query_target)
184 target_t *target = targets;
189 if (target == query_target)
191 target = target->next;
198 target_t* get_current_target(command_context_t *cmd_ctx)
200 target_t *target = get_target_by_num(cmd_ctx->current_target);
204 ERROR("BUG: current_target out of bounds");
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
214 int target_init_handler(struct target_s *target, enum target_event event, void *priv)
217 struct command_context_s *cmd_ctx = priv;
219 if ((event == TARGET_EVENT_HALTED) && (target->reset_script))
221 target_unregister_event_callback(target_init_handler, priv);
223 script = fopen(target->reset_script, "r");
226 ERROR("couldn't open script file %s", target->reset_script);
230 INFO("executing reset script '%s'", target->reset_script);
231 command_run_file(cmd_ctx, script, COMMAND_EXEC);
234 jtag_execute_queue();
240 int target_run_and_halt_handler(void *priv)
242 target_t *target = priv;
244 target->type->halt(target);
249 int target_process_reset(struct command_context_s *cmd_ctx)
251 int retval = ERROR_OK;
254 /* prepare reset_halt where necessary */
258 switch (target->reset_mode)
262 target->type->prepare_reset_halt(target);
267 target = target->next;
273 target->type->assert_reset(target);
274 target = target->next;
276 jtag_execute_queue();
278 /* request target halt if necessary, and schedule further action */
282 switch (target->reset_mode)
285 /* nothing to do if target just wants to be run */
287 case RESET_RUN_AND_HALT:
289 target_register_timer_callback(target_run_and_halt_handler, target->run_and_halt_time, 0, target);
291 case RESET_RUN_AND_INIT:
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);
297 target->type->halt(target);
300 target->type->halt(target);
301 target_register_event_callback(target_init_handler, cmd_ctx);
304 ERROR("BUG: unknown target->reset_mode");
306 target = target->next;
312 target->type->deassert_reset(target);
313 target = target->next;
315 jtag_execute_queue();
320 int target_init(struct command_context_s *cmd_ctx)
322 target_t *target = targets;
326 if (target->type->init_target(cmd_ctx, target) != ERROR_OK)
328 ERROR("target '%s' init failed", target->type->name);
331 target = target->next;
336 target_register_user_commands(cmd_ctx);
337 target_register_timer_callback(handle_target, 100, 1, NULL);
340 if (startup_mode == DAEMON_RESET)
341 target_process_reset(cmd_ctx);
346 int target_register_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
348 target_event_callback_t **callbacks_p = &target_event_callbacks;
350 if (callback == NULL)
352 return ERROR_INVALID_ARGUMENTS;
357 while ((*callbacks_p)->next)
358 callbacks_p = &((*callbacks_p)->next);
359 callbacks_p = &((*callbacks_p)->next);
362 (*callbacks_p) = malloc(sizeof(target_event_callback_t));
363 (*callbacks_p)->callback = callback;
364 (*callbacks_p)->priv = priv;
365 (*callbacks_p)->next = NULL;
370 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
372 target_timer_callback_t **callbacks_p = &target_timer_callbacks;
375 if (callback == NULL)
377 return ERROR_INVALID_ARGUMENTS;
382 while ((*callbacks_p)->next)
383 callbacks_p = &((*callbacks_p)->next);
384 callbacks_p = &((*callbacks_p)->next);
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;
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)
398 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
399 (*callbacks_p)->when.tv_sec += 1;
402 (*callbacks_p)->priv = priv;
403 (*callbacks_p)->next = NULL;
408 int target_unregister_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
410 target_event_callback_t **p = &target_event_callbacks;
411 target_event_callback_t *c = target_event_callbacks;
413 if (callback == NULL)
415 return ERROR_INVALID_ARGUMENTS;
420 target_event_callback_t *next = c->next;
421 if ((c->callback == callback) && (c->priv == priv))
435 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
437 target_timer_callback_t **p = &target_timer_callbacks;
438 target_timer_callback_t *c = target_timer_callbacks;
440 if (callback == NULL)
442 return ERROR_INVALID_ARGUMENTS;
447 target_timer_callback_t *next = c->next;
448 if ((c->callback == callback) && (c->priv == priv))
462 int target_call_event_callbacks(target_t *target, enum target_event event)
464 target_event_callback_t *callback = target_event_callbacks;
465 target_event_callback_t *next_callback;
467 DEBUG("target event %i", event);
471 next_callback = callback->next;
472 callback->callback(target, event, callback->priv);
473 callback = next_callback;
479 int target_call_timer_callbacks()
481 target_timer_callback_t *callback = target_timer_callbacks;
482 target_timer_callback_t *next_callback;
485 gettimeofday(&now, NULL);
489 next_callback = callback->next;
491 if (((now.tv_sec >= callback->when.tv_sec) && (now.tv_usec >= callback->when.tv_usec))
492 || (now.tv_sec > callback->when.tv_sec))
494 callback->callback(callback->priv);
495 if (callback->periodic)
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)
503 callback->when.tv_usec = callback->when.tv_usec - 1000000;
504 callback->when.tv_sec += 1;
508 target_unregister_timer_callback(callback->callback, callback->priv);
511 callback = next_callback;
517 int target_alloc_working_area(struct target_s *target, u32 size, working_area_t **area)
519 working_area_t *c = target->working_areas;
520 working_area_t *new_wa = NULL;
522 /* only allocate multiples of 4 byte */
525 ERROR("BUG: code tried to allocate unaligned number of bytes, padding");
526 size = CEIL(size, 4);
529 /* see if there's already a matching working area */
532 if ((c->free) && (c->size == size))
540 /* if not, allocate a new one */
543 working_area_t **p = &target->working_areas;
544 u32 first_free = target->working_area;
545 u32 free_size = target->working_area_size;
547 DEBUG("allocating new working area");
549 c = target->working_areas;
552 first_free += c->size;
553 free_size -= c->size;
558 if (free_size < size)
560 WARNING("not enough working area available");
561 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
564 new_wa = malloc(sizeof(working_area_t));
567 new_wa->address = first_free;
569 if (target->backup_working_area)
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);
576 new_wa->backup = NULL;
579 /* put new entry in list */
583 /* mark as used, and return the new (reused) area */
593 int target_free_working_area(struct target_s *target, working_area_t *area)
598 if (target->backup_working_area)
599 target->type->write_memory(target, area->address, 4, area->size / 4, area->backup);
603 /* mark user pointer invalid */
610 int target_free_all_working_areas(struct target_s *target)
612 working_area_t *c = target->working_areas;
616 working_area_t *next = c->next;
617 target_free_working_area(target, c);
627 target->working_areas = NULL;
632 int target_register_commands(struct command_context_s *cmd_ctx)
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);
644 int target_write_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
648 DEBUG("writing buffer of %i byte at 0x%8.8x", size, address);
650 /* handle writes of less than 4 byte */
653 if ((retval = target->type->write_memory(target, address, 1, size, buffer)) != ERROR_OK)
657 /* handle unaligned head bytes */
660 int unaligned = 4 - (address % 4);
662 if ((retval = target->type->write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
666 address += unaligned;
670 /* handle aligned words */
673 int aligned = size - (size % 4);
675 /* use bulk writes above a certain limit. This may have to be changed */
678 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
683 if ((retval = target->type->write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
692 /* handle tail writes of less than 4 bytes */
695 if ((retval = target->type->write_memory(target, address, 1, size, buffer)) != ERROR_OK)
702 int target_read_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
706 DEBUG("reading buffer of %i byte at 0x%8.8x", size, address);
708 /* handle reads of less than 4 byte */
711 if ((retval = target->type->read_memory(target, address, 1, size, buffer)) != ERROR_OK)
715 /* handle unaligned head bytes */
718 int unaligned = 4 - (address % 4);
720 if ((retval = target->type->read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
724 address += unaligned;
728 /* handle aligned words */
731 int aligned = size - (size % 4);
733 if ((retval = target->type->read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
741 /* handle tail writes of less than 4 bytes */
744 if ((retval = target->type->read_memory(target, address, 1, size, buffer)) != ERROR_OK)
751 int target_read_u32(struct target_s *target, u32 address, u32 *value)
755 int retval = target->type->read_memory(target, address, 4, 1, value_buf);
757 if (retval == ERROR_OK)
759 *value = target_buffer_get_u32(target, value_buf);
760 DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, *value);
765 DEBUG("address: 0x%8.8x failed", address);
771 int target_read_u16(struct target_s *target, u32 address, u16 *value)
775 int retval = target->type->read_memory(target, address, 2, 1, value_buf);
777 if (retval == ERROR_OK)
779 *value = target_buffer_get_u16(target, value_buf);
780 DEBUG("address: 0x%8.8x, value: 0x%4.4x", address, *value);
785 DEBUG("address: 0x%8.8x failed", address);
791 int target_read_u8(struct target_s *target, u32 address, u8 *value)
793 int retval = target->type->read_memory(target, address, 1, 1, value);
795 if (retval == ERROR_OK)
797 DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, *value);
802 DEBUG("address: 0x%8.8x failed", address);
808 int target_write_u32(struct target_s *target, u32 address, u32 value)
813 DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
815 target_buffer_set_u32(target, value_buf, value);
816 if ((retval = target->type->write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
818 DEBUG("failed: %i", retval);
824 int target_write_u16(struct target_s *target, u32 address, u16 value)
829 DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
831 target_buffer_set_u16(target, value_buf, value);
832 if ((retval = target->type->write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
834 DEBUG("failed: %i", retval);
840 int target_write_u8(struct target_s *target, u32 address, u8 value)
844 DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, value);
846 if ((retval = target->type->read_memory(target, address, 1, 1, &value)) != ERROR_OK)
848 DEBUG("failed: %i", retval);
854 int target_register_user_commands(struct command_context_s *cmd_ctx)
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");
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]");
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>");
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>");
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>");
886 int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
888 target_t *target = targets;
893 int num = strtoul(args[0], NULL, 0);
898 target = target->next;
902 cmd_ctx->current_target = num;
904 command_print(cmd_ctx, "%i is out of bounds, only %i targets are configured", num, count);
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;
918 int handle_target_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
925 ERROR("target command requires at least three arguments: <type> <endianess> <reset_mode>");
929 /* search for the specified target */
930 if (args[0] && (args[0][0] != 0))
932 for (i = 0; target_types[i]; i++)
934 if (strcmp(args[0], target_types[i]->name) == 0)
936 target_t **last_target_p = &targets;
938 /* register target specific commands */
939 if (target_types[i]->register_commands(cmd_ctx) != ERROR_OK)
941 ERROR("couldn't register '%s' commands", args[0]);
947 while ((*last_target_p)->next)
948 last_target_p = &((*last_target_p)->next);
949 last_target_p = &((*last_target_p)->next);
952 *last_target_p = malloc(sizeof(target_t));
954 (*last_target_p)->type = target_types[i];
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;
962 ERROR("endianness must be either 'little' or 'big', not '%s'", args[1]);
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;
979 ERROR("unknown target startup mode %s", args[2]);
982 (*last_target_p)->run_and_halt_time = 1000; /* default 1s */
984 (*last_target_p)->reset_script = NULL;
985 (*last_target_p)->post_halt_script = NULL;
986 (*last_target_p)->pre_resume_script = NULL;
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;
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;
1000 (*last_target_p)->type->target_command(cmd_ctx, cmd, args, argc, *last_target_p);
1008 /* no matching target found */
1011 ERROR("target '%s' not found", args[0]);
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)
1021 target_t *target = NULL;
1025 ERROR("incomplete target_script command");
1029 target = get_target_by_num(strtoul(args[0], NULL, 0));
1033 ERROR("target number '%s' not defined", args[0]);
1037 if (strcmp(args[1], "reset") == 0)
1039 if (target->reset_script)
1040 free(target->reset_script);
1041 target->reset_script = strdup(args[2]);
1043 else if (strcmp(args[1], "post_halt") == 0)
1045 if (target->post_halt_script)
1046 free(target->post_halt_script);
1047 target->post_halt_script = strdup(args[2]);
1049 else if (strcmp(args[1], "pre_resume") == 0)
1051 if (target->pre_resume_script)
1052 free(target->pre_resume_script);
1053 target->pre_resume_script = strdup(args[2]);
1057 ERROR("unknown event type: '%s", args[1]);
1064 int handle_run_and_halt_time_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1066 target_t *target = NULL;
1070 ERROR("incomplete run_and_halt_time command");
1074 target = get_target_by_num(strtoul(args[0], NULL, 0));
1078 ERROR("target number '%s' not defined", args[0]);
1082 target->run_and_halt_time = strtoul(args[1], NULL, 0);
1087 int handle_working_area_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1089 target_t *target = NULL;
1093 ERROR("incomplete working_area command. usage: working_area <target#> <address> <size> <'backup'|'nobackup'>");
1097 target = get_target_by_num(strtoul(args[0], NULL, 0));
1101 ERROR("target number '%s' not defined", args[0]);
1105 target->working_area = strtoul(args[1], NULL, 0);
1106 target->working_area_size = strtoul(args[2], NULL, 0);
1108 if (strcmp(args[3], "backup") == 0)
1110 target->backup_working_area = 1;
1112 else if (strcmp(args[3], "nobackup") == 0)
1114 target->backup_working_area = 0;
1118 ERROR("unrecognized <backup|nobackup> argument (%s)", args[3]);
1126 /* process target state changes */
1127 int handle_target(void *priv)
1130 target_t *target = targets;
1134 /* only poll if target isn't already halted */
1135 if (target->state != TARGET_HALTED)
1137 if (target_continous_poll)
1138 if ((retval = target->type->poll(target)) < 0)
1140 ERROR("couldn't poll target, exiting");
1145 target = target->next;
1151 int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1160 target = get_current_target(cmd_ctx);
1162 /* list all available registers for the current target */
1165 reg_cache_t *cache = target->reg_cache;
1171 for (i = 0; i < cache->num_regs; i++)
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);
1177 cache = cache->next;
1183 /* access a single register by its ordinal number */
1184 if ((args[0][0] >= '0') && (args[0][0] <= '9'))
1186 int num = strtoul(args[0], NULL, 0);
1187 reg_cache_t *cache = target->reg_cache;
1193 for (i = 0; i < cache->num_regs; i++)
1197 reg = &cache->reg_list[i];
1203 cache = cache->next;
1208 command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1211 } else /* access a single register by its name */
1213 reg = register_get_by_name(target->reg_cache, args[0], 1);
1217 command_print(cmd_ctx, "register %s not found in current target", args[0]);
1222 /* display a register */
1223 if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
1225 if ((argc == 2) && (strcmp(args[1], "force") == 0))
1228 if (reg->valid == 0)
1230 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1231 if (arch_type == NULL)
1233 ERROR("BUG: encountered unregistered arch type");
1236 arch_type->get(reg);
1238 value = buf_to_str(reg->value, reg->size, 16);
1239 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1244 /* set register value */
1247 u8 *buf = malloc(CEIL(reg->size, 8));
1248 str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
1250 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1251 if (arch_type == NULL)
1253 ERROR("BUG: encountered unregistered arch type");
1257 arch_type->set(reg, buf);
1259 value = buf_to_str(reg->value, reg->size, 16);
1260 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1268 command_print(cmd_ctx, "usage: reg <#|name> [value]");
1273 int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1275 target_t *target = get_current_target(cmd_ctx);
1280 command_print(cmd_ctx, "target state: %s", target_state_strings[target->type->poll(target)]);
1281 if (target->state == TARGET_HALTED)
1283 target->type->arch_state(target, buffer, 512);
1285 command_print(cmd_ctx, "%s", buffer);
1290 if (strcmp(args[0], "on") == 0)
1292 target_continous_poll = 1;
1294 else if (strcmp(args[0], "off") == 0)
1296 target_continous_poll = 0;
1304 int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1306 target_t *target = get_current_target(cmd_ctx);
1307 struct timeval timeout, now;
1309 gettimeofday(&timeout, NULL);
1311 timeval_add_time(&timeout, 5, 0);
1315 timeval_add_time(&timeout, strtoul(args[0], &end, 0), 0);
1317 command_print(cmd_ctx, "usage: wait_halt [seconds]");
1322 command_print(cmd_ctx, "waiting for target halted...");
1324 while(target->type->poll(target))
1326 if (target->state == TARGET_HALTED)
1328 command_print(cmd_ctx, "target halted");
1331 target_call_timer_callbacks();
1333 gettimeofday(&now, NULL);
1334 if ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec))
1336 command_print(cmd_ctx, "timed out while waiting for target halt");
1337 ERROR("timed out while waiting for target halt");
1345 int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1348 target_t *target = get_current_target(cmd_ctx);
1352 command_print(cmd_ctx, "requesting target halt...");
1354 if ((retval = target->type->halt(target)) != ERROR_OK)
1358 case ERROR_TARGET_ALREADY_HALTED:
1359 command_print(cmd_ctx, "target already halted");
1361 case ERROR_TARGET_TIMEOUT:
1362 command_print(cmd_ctx, "target timed out... shutting down");
1365 command_print(cmd_ctx, "unknown error... shutting down");
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)
1379 if (strcmp(args[0], "attach") == 0)
1381 startup_mode = DAEMON_ATTACH;
1384 else if (strcmp(args[0], "reset") == 0)
1386 startup_mode = DAEMON_RESET;
1391 WARNING("invalid daemon_startup configuration directive: %s", args[0]);
1396 int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1398 target_t *target = get_current_target(cmd_ctx);
1401 command_print(cmd_ctx, "requesting target halt and executing a soft reset");
1403 if ((retval = target->type->soft_reset_halt(target)) != ERROR_OK)
1407 case ERROR_TARGET_TIMEOUT:
1408 command_print(cmd_ctx, "target timed out... shutting down");
1411 command_print(cmd_ctx, "unknown error... shutting down");
1419 int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1421 target_t *target = get_current_target(cmd_ctx);
1422 enum target_reset_mode reset_mode = RESET_RUN;
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)
1436 reset_mode = RESET_RUN_AND_HALT;
1439 target->run_and_halt_time = strtoul(args[1], NULL, 0);
1442 else if (strcmp("run_and_init", args[0]) == 0)
1444 reset_mode = RESET_RUN_AND_INIT;
1447 target->run_and_halt_time = strtoul(args[1], NULL, 0);
1452 command_print(cmd_ctx, "usage: reset ['run', 'halt', 'init', 'run_and_halt', 'run_and_init]");
1455 target->reset_mode = reset_mode;
1458 target_process_reset(cmd_ctx);
1463 int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1466 target_t *target = get_current_target(cmd_ctx);
1471 retval = target->type->resume(target, 1, 0, 1, 0); /* current pc, addr = 0, handle breakpoints, not debugging */
1473 retval = target->type->resume(target, 0, strtoul(args[0], NULL, 0), 1, 0); /* addr = args[0], handle breakpoints, not debugging */
1476 command_print(cmd_ctx, "usage: resume [address]");
1480 if (retval != ERROR_OK)
1484 case ERROR_TARGET_NOT_HALTED:
1485 command_print(cmd_ctx, "target not halted");
1488 command_print(cmd_ctx, "unknown error... shutting down");
1496 int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1498 target_t *target = get_current_target(cmd_ctx);
1503 target->type->step(target, 1, 0, 1); /* current pc, addr = 0, handle breakpoints */
1506 target->type->step(target, 0, strtoul(args[0], NULL, 0), 1); /* addr = args[0], handle breakpoints */
1511 int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1524 target_t *target = get_current_target(cmd_ctx);
1530 count = strtoul(args[1], NULL, 0);
1532 address = strtoul(args[0], NULL, 0);
1550 buffer = calloc(count, size);
1551 if ((retval = target->type->read_memory(target, address, size, count, buffer)) != ERROR_OK)
1555 case ERROR_TARGET_UNALIGNED_ACCESS:
1556 command_print(cmd_ctx, "error: address not aligned");
1558 case ERROR_TARGET_NOT_HALTED:
1559 command_print(cmd_ctx, "error: target must be halted for memory accesses");
1561 case ERROR_TARGET_DATA_ABORT:
1562 command_print(cmd_ctx, "error: access caused data abort, system possibly corrupted");
1565 command_print(cmd_ctx, "error: unknown error");
1573 for (i = 0; i < count; i++)
1576 output_len += snprintf(output + output_len, 128 - output_len, "0x%8.8x: ", address + (i*size));
1581 output_len += snprintf(output + output_len, 128 - output_len, "%8.8x ", target_buffer_get_u32(target, &buffer[i*4]));
1584 output_len += snprintf(output + output_len, 128 - output_len, "%4.4x ", target_buffer_get_u16(target, &buffer[i*2]));
1587 output_len += snprintf(output + output_len, 128 - output_len, "%2.2x ", buffer[i*1]);
1591 if ((i%8 == 7) || (i == count - 1))
1593 command_print(cmd_ctx, output);
1603 int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1608 target_t *target = get_current_target(cmd_ctx);
1614 address = strtoul(args[0], NULL, 0);
1615 value = strtoul(args[1], NULL, 0);
1620 target_buffer_set_u32(target, value_buf, value);
1621 retval = target->type->write_memory(target, address, 4, 1, value_buf);
1624 target_buffer_set_u16(target, value_buf, value);
1625 retval = target->type->write_memory(target, address, 2, 1, value_buf);
1628 value_buf[0] = value;
1629 retval = target->type->write_memory(target, address, 1, 1, value_buf);
1637 case ERROR_TARGET_UNALIGNED_ACCESS:
1638 command_print(cmd_ctx, "error: address not aligned");
1640 case ERROR_TARGET_DATA_ABORT:
1641 command_print(cmd_ctx, "error: access caused data abort, system possibly corrupted");
1643 case ERROR_TARGET_NOT_HALTED:
1644 command_print(cmd_ctx, "error: target must be halted for memory accesses");
1649 command_print(cmd_ctx, "error: unknown error");
1657 int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1667 duration_t duration;
1668 char *duration_text;
1670 target_t *target = get_current_target(cmd_ctx);
1674 command_print(cmd_ctx, "usage: load_image <filename> [address] [type]");
1678 /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
1681 image.base_address_set = 1;
1682 image.base_address = strtoul(args[1], NULL, 0);
1686 image.base_address_set = 0;
1689 image.start_address_set = 0;
1691 duration_start_measure(&duration);
1693 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
1695 command_print(cmd_ctx, "load_image error: %s", image.error_str);
1700 for (i = 0; i < image.num_sections; i++)
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)
1705 ERROR("image_read_section failed with error code: %i", retval);
1706 command_print(cmd_ctx, "image reading failed, download aborted");
1708 image_close(&image);
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);
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);
1722 image_close(&image);
1728 int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1736 duration_t duration;
1737 char *duration_text;
1739 target_t *target = get_current_target(cmd_ctx);
1743 command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
1747 address = strtoul(args[1], NULL, 0);
1748 size = strtoul(args[2], NULL, 0);
1750 if ((address & 3) || (size & 3))
1752 command_print(cmd_ctx, "only 32-bit aligned address and size are supported");
1756 if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
1758 command_print(cmd_ctx, "dump_image error: %s", fileio.error_str);
1762 duration_start_measure(&duration);
1767 u32 this_run_size = (size > 560) ? 560 : size;
1769 target->type->read_memory(target, address, 4, this_run_size / 4, buffer);
1770 fileio_write(&fileio, this_run_size, buffer, &size_written);
1772 size -= this_run_size;
1773 address += this_run_size;
1776 fileio_close(&fileio);
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);
1786 int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1789 target_t *target = get_current_target(cmd_ctx);
1793 breakpoint_t *breakpoint = target->breakpoints;
1797 if (breakpoint->type == BKPT_SOFT)
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);
1805 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i", breakpoint->address, breakpoint->length, breakpoint->set);
1807 breakpoint = breakpoint->next;
1815 length = strtoul(args[1], NULL, 0);
1818 if (strcmp(args[2], "hw") == 0)
1821 if ((retval = breakpoint_add(target, strtoul(args[0], NULL, 0), length, hw)) != ERROR_OK)
1825 case ERROR_TARGET_NOT_HALTED:
1826 command_print(cmd_ctx, "target must be halted to set breakpoints");
1828 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
1829 command_print(cmd_ctx, "no more breakpoints available");
1832 command_print(cmd_ctx, "unknown error, breakpoint not set");
1838 command_print(cmd_ctx, "breakpoint added at address 0x%8.8x", strtoul(args[0], NULL, 0));
1843 command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
1849 int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1851 target_t *target = get_current_target(cmd_ctx);
1854 breakpoint_remove(target, strtoul(args[0], NULL, 0));
1859 int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1861 target_t *target = get_current_target(cmd_ctx);
1865 watchpoint_t *watchpoint = target->watchpoints;
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;
1875 enum watchpoint_rw type = WPT_ACCESS;
1876 u32 data_value = 0x0;
1877 u32 data_mask = 0xffffffff;
1893 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
1899 data_value = strtoul(args[3], NULL, 0);
1903 data_mask = strtoul(args[4], NULL, 0);
1905 watchpoint_add(target, strtoul(args[0], NULL, 0), strtoul(args[1], NULL, 0), type, data_value, data_mask);
1909 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
1915 int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1917 target_t *target = get_current_target(cmd_ctx);
1920 watchpoint_remove(target, strtoul(args[0], NULL, 0));