Nicolas Pitre <nico@cam.org> fix "halt 0" to only halt and not to poll/wait afterward...
[openocd.git] / src / target / target.c
index 3d0f8e277de7a6e67e647789d6f25e39651a85a0..d2fb1c8af4a52c683b45e249c819b7885d6b91b3 100644 (file)
@@ -76,6 +76,7 @@ int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args,
 int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
 int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
 int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
+int handle_test_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
 int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
 int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
 int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
@@ -232,7 +233,6 @@ const Jim_Nvp nvp_target_debug_reason [] = {
        { .name = NULL, .value = -1 },
 };
 
-
 const Jim_Nvp nvp_target_endian[] = {
        { .name = "big",    .value = TARGET_BIG_ENDIAN },
        { .name = "little", .value = TARGET_LITTLE_ENDIAN },
@@ -249,8 +249,7 @@ const Jim_Nvp nvp_reset_modes[] = {
        { .name = NULL     , .value = -1 },
 };
 
-static int
-max_target_number( void )
+static int max_target_number(void)
 {
        target_t *t;
        int x;
@@ -267,8 +266,7 @@ max_target_number( void )
 }
 
 /* determine the number of the new target */
-static int
-new_target_number( void )
+static int new_target_number(void)
 {
        target_t *t;
        int x;
@@ -1332,6 +1330,7 @@ int target_register_user_commands(struct command_context_s *cmd_ctx)
        register_command(cmd_ctx,  NULL, "load_image", handle_load_image_command, COMMAND_EXEC, "load_image <file> <address> ['bin'|'ihex'|'elf'|'s19'] [min_address] [max_length]");
        register_command(cmd_ctx,  NULL, "dump_image", handle_dump_image_command, COMMAND_EXEC, "dump_image <file> <address> <size>");
        register_command(cmd_ctx,  NULL, "verify_image", handle_verify_image_command, COMMAND_EXEC, "verify_image <file> [offset] [type]");
+       register_command(cmd_ctx,  NULL, "test_image", handle_test_image_command, COMMAND_EXEC, "test_image <file> [offset] [type]");
 
        if((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
                return retval;
@@ -1703,32 +1702,41 @@ int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char
        return target_wait_state(target, TARGET_HALTED, ms);
 }
 
+/* wait for target state to change. The trick here is to have a low
+ * latency for short waits and not to suck up all the CPU time
+ * on longer waits.
+ *
+ * After 500ms, keep_alive() is invoked
+ */
 int target_wait_state(target_t *target, enum target_state state, int ms)
 {
        int retval;
-       struct timeval timeout, now;
+       long long then=0, cur;
        int once=1;
-       gettimeofday(&timeout, NULL);
-       timeval_add_time(&timeout, 0, ms * 1000);
 
        for (;;)
        {
                if ((retval=target_poll(target))!=ERROR_OK)
                        return retval;
-               keep_alive();
                if (target->state == state)
                {
                        break;
                }
+               cur = timeval_ms();
                if (once)
                {
                        once=0;
+                       then = timeval_ms();
                        LOG_DEBUG("waiting for target %s...",
                                Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
                }
 
-               gettimeofday(&now, NULL);
-               if ((now.tv_sec > timeout.tv_sec) || ((now.tv_sec == timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
+               if (cur-then>500)
+               {
+                       keep_alive();
+               }
+
+               if ((cur-then)>ms)
                {
                        LOG_ERROR("timed out while waiting for target %s",
                                Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
@@ -1751,6 +1759,16 @@ int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **arg
                return retval;
        }
 
+       if (argc == 1)
+       {
+               int wait;
+               char *end;
+
+               wait = strtoul(args[0], &end, 0);
+               if (!*end && !wait)
+                       return ERROR_OK;
+       }
+
        return handle_wait_halt_command(cmd_ctx, cmd, args, argc);
 }
 
@@ -2155,7 +2173,7 @@ int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char
        return ERROR_OK;
 }
 
-int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+int handle_verify_image_command_internal(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, int verify)
 {
        u8 *buffer;
        u32 buf_cnt;
@@ -2219,55 +2237,61 @@ int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, ch
                        break;
                }
 
-               /* calculate checksum of image */
-               image_calculate_checksum( buffer, buf_cnt, &checksum );
-
-               retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
-               if( retval != ERROR_OK )
-               {
-                       free(buffer);
-                       break;
-               }
-
-               if( checksum != mem_checksum )
+               if (verify)
                {
-                       /* failed crc checksum, fall back to a binary compare */
-                       u8 *data;
-
-                       command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
-
-                       data = (u8*)malloc(buf_cnt);
+                       /* calculate checksum of image */
+                       image_calculate_checksum( buffer, buf_cnt, &checksum );
 
-                       /* Can we use 32bit word accesses? */
-                       int size = 1;
-                       int count = buf_cnt;
-                       if ((count % 4) == 0)
+                       retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
+                       if( retval != ERROR_OK )
                        {
-                               size *= 4;
-                               count /= 4;
+                               free(buffer);
+                               break;
                        }
-                       retval = target->type->read_memory(target, image.sections[i].base_address, size, count, data);
-                       if (retval == ERROR_OK)
+
+                       if( checksum != mem_checksum )
                        {
-                               int t;
-                               for (t = 0; t < buf_cnt; t++)
+                               /* failed crc checksum, fall back to a binary compare */
+                               u8 *data;
+
+                               command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
+
+                               data = (u8*)malloc(buf_cnt);
+
+                               /* Can we use 32bit word accesses? */
+                               int size = 1;
+                               int count = buf_cnt;
+                               if ((count % 4) == 0)
                                {
-                                       if (data[t] != buffer[t])
-                                       {
-                                               command_print(cmd_ctx, "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n", t + image.sections[i].base_address, data[t], buffer[t]);
-                                               free(data);
-                                               free(buffer);
-                                               retval=ERROR_FAIL;
-                                               goto done;
-                                       }
-                                       if ((t%16384)==0)
+                                       size *= 4;
+                                       count /= 4;
+                               }
+                               retval = target->type->read_memory(target, image.sections[i].base_address, size, count, data);
+                               if (retval == ERROR_OK)
+                               {
+                                       int t;
+                                       for (t = 0; t < buf_cnt; t++)
                                        {
-                                               keep_alive();
+                                               if (data[t] != buffer[t])
+                                               {
+                                                       command_print(cmd_ctx, "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n", t + image.sections[i].base_address, data[t], buffer[t]);
+                                                       free(data);
+                                                       free(buffer);
+                                                       retval=ERROR_FAIL;
+                                                       goto done;
+                                               }
+                                               if ((t%16384)==0)
+                                               {
+                                                       keep_alive();
+                                               }
                                        }
                                }
-                       }
 
-                       free(data);
+                               free(data);
+                       }
+               } else
+               {
+                       command_print(cmd_ctx, "address 0x%08x length 0x%08x", image.sections[i].base_address, buf_cnt);
                }
 
                free(buffer);
@@ -2292,6 +2316,16 @@ done:
        return retval;
 }
 
+int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+{
+       return handle_verify_image_command_internal(cmd_ctx, cmd, args, argc, 1);
+}
+
+int handle_test_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+{
+       return handle_verify_image_command_internal(cmd_ctx, cmd, args, argc, 0);
+}
+
 int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
 {
        int retval;
@@ -3344,6 +3378,8 @@ static int target_configure( Jim_GetOptInfo *goi, target_t *target )
                        break;
                }
        } /* while( goi->argc ) */
+
+
                /* done - we return */
        return JIM_OK;
 }
@@ -3842,6 +3878,13 @@ static int target_create( Jim_GetOptInfo *goi )
        /* Do the rest as "configure" options */
        goi->isconfigure = 1;
        e = target_configure( goi, target);
+
+       if (target->tap == NULL)
+       {
+               Jim_SetResultString( interp, "-chain-position required when creating target", -1);
+               e=JIM_ERR;
+       }
+
        if( e != JIM_OK ){
                free( target->type );
                free( target );
@@ -4017,7 +4060,7 @@ struct FastLoad
 static int fastload_num;
 static struct FastLoad *fastload;
 
-static void free_fastload()
+static void free_fastload(void)
 {
        if (fastload!=NULL)
        {
@@ -4183,17 +4226,18 @@ int handle_fast_load_command(struct command_context_s *cmd_ctx, char *cmd, char
        int i;
        int ms=timeval_ms();
        int size=0;
+       int retval=ERROR_OK;
        for (i=0; i<fastload_num;i++)
        {
-               int retval;
                target_t *target = get_current_target(cmd_ctx);
-               if ((retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data)) != ERROR_OK)
+               command_print(cmd_ctx, "Write to 0x%08x, length 0x%08x", fastload[i].address, fastload[i].length);
+               if (retval==ERROR_OK)
                {
-                       return retval;
+                       retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
                }
                size+=fastload[i].length;
        }
        int after=timeval_ms();
        command_print(cmd_ctx, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
-       return ERROR_OK;
+       return retval;
 }

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)