Improve handle_virt2phys_command argument parsing:
[openocd.git] / src / target / target.c
index c2f479add0fe6818c84d16eebfb3d34e14a8eedb..20e82319fbaa55eea517aeead211884cdf9ed844 100644 (file)
@@ -2153,8 +2153,12 @@ static int handle_load_image_command(struct command_context_s *cmd_ctx, char *cm
        /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
        if (argc >= 2)
        {
+               u32 addr;
+               retval = parse_u32(args[1], &addr);
+               if (ERROR_OK != retval)
+                       return ERROR_COMMAND_SYNTAX_ERROR;
+               image.base_address = addr;
                image.base_address_set = 1;
-               image.base_address = strtoul(args[1], NULL, 0);
        }
        else
        {
@@ -2166,11 +2170,17 @@ static int handle_load_image_command(struct command_context_s *cmd_ctx, char *cm
 
        if (argc>=4)
        {
-               min_address=strtoul(args[3], NULL, 0);
+               retval = parse_u32(args[3], &min_address);
+               if (ERROR_OK != retval)
+                       return ERROR_COMMAND_SYNTAX_ERROR;
        }
        if (argc>=5)
        {
-               max_address=strtoul(args[4], NULL, 0)+min_address;
+               retval = parse_u32(args[4], &max_address);
+               if (ERROR_OK != retval)
+                       return ERROR_COMMAND_SYNTAX_ERROR;
+               // use size (given) to find max (required)
+               max_address += min_address;
        }
 
        if (min_address>max_address)
@@ -2256,10 +2266,8 @@ static int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cm
 {
        fileio_t fileio;
 
-       u32 address;
-       u32 size;
        u8 buffer[560];
-       int retval=ERROR_OK, retvaltemp;
+       int retvaltemp;
 
        duration_t duration;
        char *duration_text;
@@ -2272,8 +2280,15 @@ static int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cm
                return ERROR_OK;
        }
 
-       address = strtoul(args[1], NULL, 0);
-       size = strtoul(args[2], NULL, 0);
+       u32 address;
+       int retval = parse_u32(args[1], &address);
+       if (ERROR_OK != retval)
+               return retval;
+
+       u32 size;
+       retval = parse_u32(args[2], &size);
+       if (ERROR_OK != retval)
+               return retval;
 
        if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
        {
@@ -2351,8 +2366,12 @@ static int handle_verify_image_command_internal(struct command_context_s *cmd_ct
 
        if (argc >= 2)
        {
+               u32 addr;
+               retval = parse_u32(args[1], &addr);
+               if (ERROR_OK != retval)
+                       return ERROR_COMMAND_SYNTAX_ERROR;
+               image.base_address = addr;
                image.base_address_set = 1;
-               image.base_address = strtoul(args[1], NULL, 0);
        }
        else
        {
@@ -2522,8 +2541,15 @@ static int handle_bp_command(struct command_context_s *cmd_ctx,
                return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
-       u32 addr = strtoul(args[0], NULL, 0);
-       u32 length = strtoul(args[1], NULL, 0);
+       u32 addr;
+       int retval = parse_u32(args[0], &addr);
+       if (ERROR_OK != retval)
+               return retval;
+
+       u32 length;
+       retval = parse_u32(args[1], &length);
+       if (ERROR_OK != retval)
+               return retval;
 
        int hw = BKPT_SOFT;
        if (argc == 3)
@@ -2539,10 +2565,16 @@ static int handle_bp_command(struct command_context_s *cmd_ctx,
 
 static int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
 {
-       target_t *target = get_current_target(cmd_ctx);
+       if (argc != 1)
+               return ERROR_COMMAND_SYNTAX_ERROR;
 
-       if (argc > 0)
-               breakpoint_remove(target, strtoul(args[0], NULL, 0));
+       u32 addr;
+       int retval = parse_u32(args[0], &addr);
+       if (ERROR_OK != retval)
+               return retval;
+
+       target_t *target = get_current_target(cmd_ctx);
+       breakpoint_remove(target, addr);
 
        return ERROR_OK;
 }
@@ -2550,7 +2582,6 @@ static int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char
 static int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
 {
        target_t *target = get_current_target(cmd_ctx);
-       int retval;
 
        if (argc == 0)
        {
@@ -2561,52 +2592,65 @@ static int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char
                        command_print(cmd_ctx, "address: 0x%8.8x, len: 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);
                        watchpoint = watchpoint->next;
                }
+               return ERROR_OK;
        }
-       else if (argc >= 2)
-       {
-               enum watchpoint_rw type = WPT_ACCESS;
-               u32 data_value = 0x0;
-               u32 data_mask = 0xffffffff;
 
-               if (argc >= 3)
-               {
-                       switch(args[2][0])
-                       {
-                               case 'r':
-                                       type = WPT_READ;
-                                       break;
-                               case 'w':
-                                       type = WPT_WRITE;
-                                       break;
-                               case 'a':
-                                       type = WPT_ACCESS;
-                                       break;
-                               default:
-                                       command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
-                                       return ERROR_OK;
-                       }
-               }
-               if (argc >= 4)
-               {
-                       data_value = strtoul(args[3], NULL, 0);
-               }
-               if (argc >= 5)
-               {
-                       data_mask = strtoul(args[4], NULL, 0);
-               }
+       enum watchpoint_rw type = WPT_ACCESS;
+       u32 addr = 0;
+       u32 length = 0;
+       u32 data_value = 0x0;
+       u32 data_mask = 0xffffffff;
+       int retval;
 
-               if ((retval = watchpoint_add(target, strtoul(args[0], NULL, 0),
-                               strtoul(args[1], NULL, 0), type, data_value, data_mask)) != ERROR_OK)
+       switch (argc)
+       {
+       case 5:
+               retval = parse_u32(args[4], &data_mask);
+               if (ERROR_OK != retval)
+                       return retval;
+               // fall through
+       case 4:
+               retval = parse_u32(args[3], &data_value);
+               if (ERROR_OK != retval)
+                       return retval;
+               // fall through
+       case 3:
+               switch(args[2][0])
                {
-                       LOG_ERROR("Failure setting breakpoints");
+               case 'r':
+                       type = WPT_READ;
+                       break;
+               case 'w':
+                       type = WPT_WRITE;
+                       break;
+               case 'a':
+                       type = WPT_ACCESS;
+                       break;
+               default:
+                       LOG_ERROR("invalid watchpoint mode ('%c')", args[2][0]);
+                       return ERROR_COMMAND_SYNTAX_ERROR;
                }
-       }
-       else
-       {
+               // fall through
+       case 2:
+               retval = parse_u32(args[1], &length);
+               if (ERROR_OK != retval)
+                       return retval;
+               retval = parse_u32(args[0], &addr);
+               if (ERROR_OK != retval)
+                       return retval;
+               break;
+
+       default:
                command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
+               return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
-       return ERROR_OK;
+       retval = watchpoint_add(target, addr, length, type,
+                       data_value, data_mask);
+       if (ERROR_OK != retval)
+               LOG_ERROR("Failure setting watchpoints");
+
+       return retval;
 }
 
 static int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
@@ -2614,8 +2658,13 @@ static int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char
        if (argc != 1)
                return ERROR_COMMAND_SYNTAX_ERROR;
 
+       u32 addr;
+       int retval = parse_u32(args[0], &addr);
+       if (ERROR_OK != retval)
+               return retval;
+
        target_t *target = get_current_target(cmd_ctx);
-       watchpoint_remove(target, strtoul(args[0], NULL, 0));
+       watchpoint_remove(target, addr);
 
        return ERROR_OK;
 }
@@ -2633,11 +2682,14 @@ static int handle_virt2phys_command(command_context_t *cmd_ctx,
        if (argc != 1)
                return ERROR_COMMAND_SYNTAX_ERROR;
 
-       target_t *target = get_current_target(cmd_ctx);
-       u32 va = strtoul(args[0], NULL, 0);
+       u32 va;
+       int retval = parse_u32(args[0], &va);
+       if (ERROR_OK != retval)
+               return retval;
        u32 pa;
 
-       int retval = target->type->virt2phys(target, va, &pa);
+       target_t *target = get_current_target(cmd_ctx);
+       retval = target->type->virt2phys(target, va, &pa);
        if (retval == ERROR_OK)
                command_print(cmd_ctx, "Physical address 0x%08x", pa);
 

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)