jtag: retire one instance of jtag_get_end_state() usage
[openocd.git] / src / target / target.c
index c56265c1d1e8c200adcc43366c46dd1e5b32b4b6..2c88a6fd87d2868d871d93e3e7f41dc3cbc3a90e 100644 (file)
@@ -2,7 +2,7 @@
  *   Copyright (C) 2005 by Dominic Rath                                    *
  *   Dominic.Rath@gmx.de                                                   *
  *                                                                         *
- *   Copyright (C) 2007-2009 Øyvind Harboe                                 *
+ *   Copyright (C) 2007-2010 Øyvind Harboe                                 *
  *   oyvind.harboe@zylin.com                                               *
  *                                                                         *
  *   Copyright (C) 2008, Duane Ellis                                       *
@@ -35,6 +35,7 @@
 
 #include <helper/time_support.h>
 #include <jtag/jtag.h>
+#include <flash/nor/core.h>
 
 #include "target.h"
 #include "target_type.h"
@@ -424,6 +425,36 @@ int target_halt(struct target *target)
        return ERROR_OK;
 }
 
+/**
+ * Make the target (re)start executing using its saved execution
+ * context (possibly with some modifications).
+ *
+ * @param target Which target should start executing.
+ * @param current True to use the target's saved program counter instead
+ *     of the address parameter
+ * @param address Optionally used as the program counter.
+ * @param handle_breakpoints True iff breakpoints at the resumption PC
+ *     should be skipped.  (For example, maybe execution was stopped by
+ *     such a breakpoint, in which case it would be counterprodutive to
+ *     let it re-trigger.
+ * @param debug_execution False if all working areas allocated by OpenOCD
+ *     should be released and/or restored to their original contents.
+ *     (This would for example be true to run some downloaded "helper"
+ *     algorithm code, which resides in one such working buffer and uses
+ *     another for data storage.)
+ *
+ * @todo Resolve the ambiguity about what the "debug_execution" flag
+ * signifies.  For example, Target implementations don't agree on how
+ * it relates to invalidation of the register cache, or to whether
+ * breakpoints and watchpoints should be enabled.  (It would seem wrong
+ * to enable breakpoints when running downloaded "helper" algorithms
+ * (debug_execution true), since the breakpoints would be set to match
+ * target firmware being debugged, not the helper algorithm.... and
+ * enabling them could cause such helpers to malfunction (for example,
+ * by overwriting data with a breakpoint instruction.  On the other
+ * hand the infrastructure for running such helpers might use this
+ * procedure but rely on hardware breakpoint to detect termination.)
+ */
 int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
 {
        int retval;
@@ -435,13 +466,21 @@ int target_resume(struct target *target, int current, uint32_t address, int hand
                return ERROR_FAIL;
        }
 
-       /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
-        * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
-        * the application.
+       /* note that resume *must* be asynchronous. The CPU can halt before
+        * we poll. The CPU can even halt at the current PC as a result of
+        * a software breakpoint being inserted by (a bug?) the application.
         */
        if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
                return retval;
 
+       /* Invalidate any cached protect/erase/... flash status, since
+        * almost all targets will now be able modify the flash by
+        * themselves.  We want flash drivers and infrastructure to
+        * be able to rely on (non-invalidated) cached state.
+        *
+        * REVISIT do the same for NAND ; maybe other flash flavors too...
+        */
+       nor_resume(target);
        return retval;
 }
 
@@ -1739,15 +1778,6 @@ static int sense_handler(void)
        return ERROR_OK;
 }
 
-static void target_call_event_callbacks_all(enum target_event e) {
-       struct target *target;
-       target = all_targets;
-       while (target) {
-               target_call_event_callbacks(target, e);
-               target = target->next;
-       }
-}
-
 /* process target state changes */
 static int handle_target(void *priv)
 {
@@ -1767,7 +1797,7 @@ static int handle_target(void *priv)
                int did_something = 0;
                if (runSrstAsserted)
                {
-                       target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
+                       LOG_INFO("srst asserted detected, running srst_asserted proc.");
                        Jim_Eval(interp, "srst_asserted");
                        did_something = 1;
                }
@@ -1778,7 +1808,7 @@ static int handle_target(void *priv)
                }
                if (runPowerDropout)
                {
-                       target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
+                       LOG_INFO("Power dropout detected, running power_dropout proc.");
                        Jim_Eval(interp, "power_dropout");
                        did_something = 1;
                }
@@ -1820,6 +1850,14 @@ static int handle_target(void *priv)
                        /* polling may fail silently until the target has been examined */
                        if ((retval = target_poll(target)) != ERROR_OK)
                        {
+                               /* FIX!!!!! If we add a LOG_INFO() here to output a line in GDB
+                                * *why* we are aborting GDB, then we'll spam telnet when the
+                                * poll is failing persistently.
+                                *
+                                * If we could implement an event that detected the
+                                * target going from non-pollable to pollable, we could issue
+                                * an error only upon the transition.
+                                */
                                target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
                                return retval;
                        }
@@ -2170,7 +2208,7 @@ static void handle_md_output(struct command_context *cmd_ctx,
        const char *value_fmt;
        switch (size) {
        case 4: value_fmt = "%8.8x "; break;
-       case 2: value_fmt = "%4.2x "; break;
+       case 2: value_fmt = "%4.4x "; break;
        case 1: value_fmt = "%2.2x "; break;
        default:
                /* "can't happen", caller checked */
@@ -2256,6 +2294,76 @@ COMMAND_HANDLER(handle_md_command)
        return retval;
 }
 
+typedef int (*target_write_fn)(struct target *target,
+               uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
+
+static int target_write_memory_fast(struct target *target,
+               uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
+{
+       return target_write_buffer(target, address, size * count, buffer);
+}
+
+static int target_fill_mem(struct target *target,
+               uint32_t address,
+               target_write_fn fn,
+               unsigned data_size,
+               /* value */
+               uint32_t b,
+               /* count */
+               unsigned c)
+{
+       /* We have to write in reasonably large chunks to be able
+        * to fill large memory areas with any sane speed */
+       const unsigned chunk_size = 16384;
+       uint8_t *target_buf = malloc(chunk_size * data_size);
+       if (target_buf == NULL)
+       {
+               LOG_ERROR("Out of memory");
+               return ERROR_FAIL;
+       }
+
+       for (unsigned i = 0; i < chunk_size; i ++)
+       {
+               switch (data_size)
+               {
+               case 4:
+                       target_buffer_set_u32(target, target_buf + i*data_size, b);
+                       break;
+               case 2:
+                       target_buffer_set_u16(target, target_buf + i*data_size, b);
+                       break;
+               case 1:
+                       target_buffer_set_u8(target, target_buf + i*data_size, b);
+                       break;
+               default:
+                       exit(-1);
+               }
+       }
+
+       int retval = ERROR_OK;
+
+       for (unsigned x = 0; x < c; x += chunk_size)
+       {
+               unsigned current;
+               current = c - x;
+               if (current > chunk_size)
+               {
+                       current = chunk_size;
+               }
+               int retval = fn(target, address + x * data_size, data_size, current, target_buf);
+               if (retval != ERROR_OK)
+               {
+                       break;
+               }
+               /* avoid GDB timeouts */
+               keep_alive();
+       }
+       free(target_buf);
+
+       return retval;
+}
+
+
 COMMAND_HANDLER(handle_mw_command)
 {
        if (CMD_ARGC < 2)
@@ -2263,8 +2371,7 @@ COMMAND_HANDLER(handle_mw_command)
                return ERROR_COMMAND_SYNTAX_ERROR;
        }
        bool physical=strcmp(CMD_ARGV[0], "phys")==0;
-       int (*fn)(struct target *target,
-                       uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
+       target_write_fn fn;
        if (physical)
        {
                CMD_ARGC--;
@@ -2272,7 +2379,7 @@ COMMAND_HANDLER(handle_mw_command)
                fn=target_write_phys_memory;
        } else
        {
-               fn=target_write_memory;
+               fn = target_write_memory_fast;
        }
        if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
                return ERROR_COMMAND_SYNTAX_ERROR;
@@ -2289,35 +2396,22 @@ COMMAND_HANDLER(handle_mw_command)
 
        struct target *target = get_current_target(CMD_CTX);
        unsigned wordsize;
-       uint8_t value_buf[4];
        switch (CMD_NAME[2])
        {
                case 'w':
                        wordsize = 4;
-                       target_buffer_set_u32(target, value_buf, value);
                        break;
                case 'h':
                        wordsize = 2;
-                       target_buffer_set_u16(target, value_buf, value);
                        break;
                case 'b':
                        wordsize = 1;
-                       value_buf[0] = value;
                        break;
                default:
                        return ERROR_COMMAND_SYNTAX_ERROR;
        }
-       for (unsigned i = 0; i < count; i++)
-       {
-               int retval = fn(target,
-                               address + i * wordsize, wordsize, 1, value_buf);
-               if (ERROR_OK != retval)
-                       return retval;
-               keep_alive();
-       }
-
-       return ERROR_OK;
 
+       return target_fill_mem(target, address, fn, wordsize, value, count);
 }
 
 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
@@ -3841,13 +3935,17 @@ static int jim_target_mw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
        Jim_GetOptInfo goi;
        Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
 
-       if (goi.argc != 2 && goi.argc != 3)
+       /* danger! goi.argc will be modified below! */
+       argc = goi.argc;
+
+       if (argc != 2 && argc != 3)
        {
                Jim_SetResult_sprintf(goi.interp,
                                "usage: %s <address> <data> [<count>]", cmd_name);
                return JIM_ERR;
        }
 
+
        jim_wide a;
        int e = Jim_GetOpt_Wide(&goi, &a);
        if (e != JIM_OK)
@@ -3859,7 +3957,7 @@ static int jim_target_mw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
                return e;
 
        jim_wide c = 1;
-       if (goi.argc == 3)
+       if (argc == 3)
        {
                e = Jim_GetOpt_Wide(&goi, &c);
                if (e != JIM_OK)
@@ -3867,36 +3965,21 @@ static int jim_target_mw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
        }
 
        struct target *target = Jim_CmdPrivData(goi.interp);
-       uint8_t  target_buf[32];
+       unsigned data_size;
        if (strcasecmp(cmd_name, "mww") == 0) {
-               target_buffer_set_u32(target, target_buf, b);
-               b = 4;
+               data_size = 4;
        }
        else if (strcasecmp(cmd_name, "mwh") == 0) {
-               target_buffer_set_u16(target, target_buf, b);
-               b = 2;
+               data_size = 2;
        }
        else if (strcasecmp(cmd_name, "mwb") == 0) {
-               target_buffer_set_u8(target, target_buf, b);
-               b = 1;
+               data_size = 1;
        } else {
                LOG_ERROR("command '%s' unknown: ", cmd_name);
                return JIM_ERR;
        }
 
-       for (jim_wide x = 0; x < c; x++)
-       {
-               e = target_write_memory(target, a, b, 1, target_buf);
-               if (e != ERROR_OK)
-               {
-                       Jim_SetResult_sprintf(interp,
-                                       "Error writing @ 0x%08x: %d\n", (int)(a), e);
-                       return JIM_ERR;
-               }
-               /* b = width */
-               a = a + b;
-       }
-       return JIM_OK;
+       return (target_fill_mem(target, a, target_write_memory_fast, data_size, b, c) == ERROR_OK) ? JIM_OK : JIM_ERR;
 }
 
 static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
@@ -3906,7 +3989,10 @@ static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
        Jim_GetOptInfo goi;
        Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
 
-       if ((goi.argc == 2) || (goi.argc == 3))
+       /* danger! goi.argc will be modified below! */
+       argc = goi.argc;
+
+       if ((argc != 1) && (argc != 2))
        {
                Jim_SetResult_sprintf(goi.interp,
                                "usage: %s <address> [<count>]", cmd_name);
@@ -3919,7 +4005,7 @@ static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
                return JIM_ERR;
        }
        jim_wide c;
-       if (goi.argc) {
+       if (argc == 2) {
                e = Jim_GetOpt_Wide(&goi, &c);
                if (e != JIM_OK) {
                        return JIM_ERR;
@@ -3961,7 +4047,7 @@ static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
                case 4:
                        for (x = 0; x < 16 && x < y; x += 4)
                        {
-                               z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
+                               z = target_buffer_get_u32(target, &(target_buf[ x ]));
                                Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
                        }
                        for (; (x < 16) ; x += 4) {
@@ -3971,7 +4057,7 @@ static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
                case 2:
                        for (x = 0; x < 16 && x < y; x += 2)
                        {
-                               z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
+                               z = target_buffer_get_u16(target, &(target_buf[ x ]));
                                Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
                        }
                        for (; (x < 16) ; x += 2) {
@@ -3981,7 +4067,7 @@ static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
                case 1:
                default:
                        for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
-                               z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
+                               z = target_buffer_get_u8(target, &(target_buf[ x ]));
                                Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
                        }
                        for (; (x < 16) ; x += 1) {
@@ -4055,6 +4141,21 @@ static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv
        return JIM_OK;
 }
 
+static int jim_target_halt_gdb(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+       if (argc != 1)
+       {
+               Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
+               return JIM_ERR;
+       }
+       struct target *target = Jim_CmdPrivData(interp);
+
+       if (target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT) != ERROR_OK)
+               return JIM_ERR;
+
+       return JIM_OK;
+}
+
 static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
 {
        if (argc != 1)
@@ -4335,6 +4436,12 @@ static const struct command_registration target_instance_command_handlers[] = {
                .jim_handler = jim_target_examine,
                .help = "used internally for reset processing",
        },
+       {
+               .name = "arp_halt_gdb",
+               .mode = COMMAND_EXEC,
+               .jim_handler = jim_target_halt_gdb,
+               .help = "used internally for reset processing to halt GDB",
+       },
        {
                .name = "arp_poll",
                .mode = COMMAND_EXEC,

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)