Make target_buffer_get_uXX interfaces work with constant buffers.
[openocd.git] / src / target / target.c
index e3590846db2d36c80bbb812360d460fb64fe4439..988312175d468149c740211ac09fb119be2b7950 100644 (file)
@@ -273,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);
@@ -282,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);
@@ -291,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;
 }
@@ -320,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;
 
@@ -957,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*/
@@ -1032,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*/
@@ -1285,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 <ARRAYNAME> <WIDTH=32/16/8> <ADDRESS> <COUNT>");
+       register_jim(cmd_ctx, "ocd_array2mem", jim_array2mem, "convert a TCL array to memory locations and write the values  <ARRAYNAME> <WIDTH=32/16/8> <ADDRESS> <COUNT>");
 
        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");
@@ -1333,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;
        }
@@ -1893,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;
                        }
                }
@@ -1941,27 +1958,11 @@ static int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char
        }
        for (i=0; i<count; i++)
        {
-               int retval;
-               switch (wordsize)
-               {
-                       case 4:
-                               retval = target->type->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;
@@ -2151,7 +2152,8 @@ 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);
        }
 
@@ -2352,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
@@ -2721,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)
@@ -2742,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;
@@ -2903,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)
@@ -2924,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;

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)