X-Git-Url: https://review.openocd.org/gitweb?a=blobdiff_plain;f=src%2Ftarget%2Ftarget.c;h=988312175d468149c740211ac09fb119be2b7950;hb=12df0f00908d849e79fafa4d34065fc11290c8fa;hp=8a38f1fb53a4466c5b9c55d78a348c7a8916a774;hpb=7a93100c2dfe743749d2bc512498b096f4332b9b;p=openocd.git diff --git a/src/target/target.c b/src/target/target.c index 8a38f1fb53..988312175d 100644 --- a/src/target/target.c +++ b/src/target/target.c @@ -33,31 +33,16 @@ #include "config.h" #endif -#include "replacements.h" #include "target.h" #include "target_request.h" - -#include "log.h" -#include "configuration.h" -#include "binarybuffer.h" +#include "time_support.h" +#include "register.h" +#include "trace.h" +#include "image.h" #include "jtag.h" -#include -#include #include -#include -#include -#include -#include - -#include -#include - -#include - -#include -#include static int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); @@ -288,7 +273,7 @@ static int new_target_number(void) static int target_continous_poll = 1; /* read a u32 from a buffer in target memory endianness */ -u32 target_buffer_get_u32(target_t *target, u8 *buffer) +u32 target_buffer_get_u32(target_t *target, const u8 *buffer) { if (target->endianness == TARGET_LITTLE_ENDIAN) return le_to_h_u32(buffer); @@ -297,7 +282,7 @@ u32 target_buffer_get_u32(target_t *target, u8 *buffer) } /* read a u16 from a buffer in target memory endianness */ -u16 target_buffer_get_u16(target_t *target, u8 *buffer) +u16 target_buffer_get_u16(target_t *target, const u8 *buffer) { if (target->endianness == TARGET_LITTLE_ENDIAN) return le_to_h_u16(buffer); @@ -306,7 +291,7 @@ u16 target_buffer_get_u16(target_t *target, u8 *buffer) } /* read a u8 from a buffer in target memory endianness */ -u8 target_buffer_get_u8(target_t *target, u8 *buffer) +u8 target_buffer_get_u8(target_t *target, const u8 *buffer) { return *buffer & 0x0ff; } @@ -335,8 +320,36 @@ void target_buffer_set_u8(target_t *target, u8 *buffer, u8 value) *buffer = value; } +/* return a pointer to a configured target; id is name or number */ +target_t *get_target(const char *id) +{ + target_t *target; + char *endptr; + int num; + + /* try as tcltarget name */ + for (target = all_targets; target; target = target->next) { + if (target->cmd_name == NULL) + continue; + if (strcmp(id, target->cmd_name) == 0) + return target; + } + + /* no match, try as number */ + num = strtoul(id, &endptr, 0); + if (*endptr != 0) + return NULL; + + for (target = all_targets; target; target = target->next) { + if (target->target_number == num) + return target; + } + + return NULL; +} + /* returns a pointer to the n-th configured target */ -target_t* get_target_by_num(int num) +static target_t *get_target_by_num(int num) { target_t *target = all_targets; @@ -972,6 +985,10 @@ int target_write_buffer(struct target_s *target, u32 address, u32 size, u8 *buff return ERROR_FAIL; } + if (size == 0) { + return ERROR_OK; + } + if ((address + size - 1) < address) { /* GDB can request this when e.g. PC is 0xfffffffc*/ @@ -1047,6 +1064,10 @@ int target_read_buffer(struct target_s *target, u32 address, u32 size, u8 *buffe return ERROR_FAIL; } + if (size == 0) { + return ERROR_OK; + } + if ((address + size - 1) < address) { /* GDB can request this when e.g. PC is 0xfffffffc*/ @@ -1300,8 +1321,8 @@ int target_register_user_commands(struct command_context_s *cmd_ctx) /* script procedures */ register_command(cmd_ctx, NULL, "profile", handle_profile_command, COMMAND_EXEC, "profiling samples the CPU PC"); - register_jim(cmd_ctx, "ocd_mem2array", jim_mem2array, "read memory and return as a TCL array for script processing"); - register_jim(cmd_ctx, "ocd_array2mem", jim_array2mem, "convert a TCL array to memory locations and write the values"); + register_jim(cmd_ctx, "ocd_mem2array", jim_mem2array, "read memory and return as a TCL array for script processing
"); + register_jim(cmd_ctx, "ocd_array2mem", jim_array2mem, "convert a TCL array to memory locations and write the values
"); register_command(cmd_ctx, NULL, "fast_load_image", handle_fast_load_image_command, COMMAND_ANY, "same args as load_image, image stored in memory - mainly for profiling purposes"); @@ -1348,35 +1369,16 @@ int target_register_user_commands(struct command_context_s *cmd_ctx) static int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) { - char *cp; target_t *target = all_targets; if (argc == 1) { - /* try as tcltarget name */ - for( target = all_targets ; target ; target = target->next ){ - if( target->cmd_name ){ - if( 0 == strcmp( args[0], target->cmd_name ) ){ - /* MATCH */ - goto Match; - } - } - } - /* no match, try as number */ - - int num = strtoul(args[0], &cp, 0 ); - if( *cp != 0 ){ - /* then it was not a number */ - command_print( cmd_ctx, "Target: %s unknown, try one of:\n", args[0] ); - goto DumpTargets; - } - - target = get_target_by_num( num ); - if( target == NULL ){ + target = get_target(args[0]); + if (target == NULL) { command_print(cmd_ctx,"Target: %s is unknown, try one of:\n", args[0] ); goto DumpTargets; } - Match: + cmd_ctx->current_target = target->target_number; return ERROR_OK; } @@ -1908,7 +1910,7 @@ static int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char if ((i%line_modulo == line_modulo-1) || (i == count - 1)) { - command_print(cmd_ctx, output); + command_print(cmd_ctx, "%s", output); output_len = 0; } } @@ -1956,27 +1958,11 @@ static int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char } for (i=0; itype->write_memory(target, address + i*wordsize, 4, 1, value_buf); - break; - case 2: - retval = target->type->write_memory(target, address + i*wordsize, 2, 1, value_buf); - break; - case 1: - retval = target->type->write_memory(target, address + i*wordsize, 1, 1, value_buf); - break; - default: - return ERROR_OK; - } - keep_alive(); - - if (retval!=ERROR_OK) - { + int retval = target->type->write_memory(target, + address + i * wordsize, wordsize, 1, value_buf); + if (ERROR_OK != retval) return retval; - } + keep_alive(); } return ERROR_OK; @@ -2130,12 +2116,6 @@ static int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cm address = strtoul(args[1], NULL, 0); size = strtoul(args[2], NULL, 0); - if ((address & 3) || (size & 3)) - { - command_print(cmd_ctx, "only 32-bit aligned address and size are supported"); - return ERROR_OK; - } - if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK) { return ERROR_OK; @@ -2148,7 +2128,7 @@ static int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cm u32 size_written; u32 this_run_size = (size > 560) ? 560 : size; - retval = target->type->read_memory(target, address, 4, this_run_size / 4, buffer); + retval = target_read_buffer(target, address, this_run_size, buffer); if (retval != ERROR_OK) { break; @@ -2172,11 +2152,12 @@ static int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cm if (retval==ERROR_OK) { - command_print(cmd_ctx, "dumped %"PRIi64" byte in %s", fileio.size, duration_text); + command_print(cmd_ctx, "dumped %lld byte in %s", + fileio.size, duration_text); + free(duration_text); } - free(duration_text); - return ERROR_OK; + return retval; } static int handle_verify_image_command_internal(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, int verify) @@ -2373,7 +2354,8 @@ static int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char } else { - command_print(cmd_ctx, "breakpoint added at address 0x%8.8x", strtoul(args[0], NULL, 0)); + command_print(cmd_ctx, "breakpoint added at address 0x%8.8lx", + strtoul(args[0], NULL, 0)); } } else @@ -2742,7 +2724,7 @@ static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv) return JIM_ERR; } - return target_mem2array(interp, target, argc,argv); + return target_mem2array(interp, target, argc-1, argv+1); } static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv) @@ -2763,25 +2745,25 @@ static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_ * argv[3] = memory address * argv[4] = count of times to read */ - if (argc != 5) { + if (argc != 4) { Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems"); return JIM_ERR; } - varname = Jim_GetString(argv[1], &len); + varname = Jim_GetString(argv[0], &len); /* given "foo" get space for worse case "foo(%d)" .. add 20 */ - e = Jim_GetLong(interp, argv[2], &l); + e = Jim_GetLong(interp, argv[1], &l); width = l; if (e != JIM_OK) { return e; } - e = Jim_GetLong(interp, argv[3], &l); + e = Jim_GetLong(interp, argv[2], &l); addr = l; if (e != JIM_OK) { return e; } - e = Jim_GetLong(interp, argv[4], &l); + e = Jim_GetLong(interp, argv[3], &l); len = l; if (e != JIM_OK) { return e; @@ -2924,7 +2906,7 @@ static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv) return JIM_ERR; } - return target_array2mem( interp,target, argc, argv ); + return target_array2mem( interp,target, argc-1, argv+1 ); } static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv) @@ -2945,25 +2927,25 @@ static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_ * argv[3] = memory address * argv[4] = count to write */ - if (argc != 5) { + if (argc != 4) { Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems"); return JIM_ERR; } - varname = Jim_GetString(argv[1], &len); + varname = Jim_GetString(argv[0], &len); /* given "foo" get space for worse case "foo(%d)" .. add 20 */ - e = Jim_GetLong(interp, argv[2], &l); + e = Jim_GetLong(interp, argv[1], &l); width = l; if (e != JIM_OK) { return e; } - e = Jim_GetLong(interp, argv[3], &l); + e = Jim_GetLong(interp, argv[2], &l); addr = l; if (e != JIM_OK) { return e; } - e = Jim_GetLong(interp, argv[4], &l); + e = Jim_GetLong(interp, argv[3], &l); len = l; if (e != JIM_OK) { return e;