X-Git-Url: https://review.openocd.org/gitweb?a=blobdiff_plain;f=src%2Ftarget%2Ftarget.c;h=73594fb0b9e478b0073cbb53bac78b770af47307;hb=a8a9eddca0f8f5d8ab73f22c5a159fe1748272d5;hp=9596302d7db41cbd5829f1e93444e0ef59bdd6f4;hpb=d72e90ae4b070cc08799e800c111dd422ac6b1a4;p=openocd.git diff --git a/src/target/target.c b/src/target/target.c index 9596302d7d..73594fb0b9 100644 --- a/src/target/target.c +++ b/src/target/target.c @@ -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 #include +#include #include "target.h" #include "target_type.h" @@ -49,6 +50,7 @@ static int target_array2mem(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv); static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv); +static int target_register_user_commands(struct command_context *cmd_ctx); /* targets */ extern struct target_type arm7tdmi_target; @@ -69,7 +71,7 @@ extern struct target_type avr_target; extern struct target_type dsp563xx_target; extern struct target_type testee_target; -struct target_type *target_types[] = +static struct target_type *target_types[] = { &arm7tdmi_target, &arm9tdmi_target, @@ -92,8 +94,8 @@ struct target_type *target_types[] = }; struct target *all_targets = NULL; -struct target_event_callback *target_event_callbacks = NULL; -struct target_timer_callback *target_timer_callbacks = NULL; +static struct target_event_callback *target_event_callbacks = NULL; +static struct target_timer_callback *target_timer_callbacks = NULL; static const Jim_Nvp nvp_assert[] = { { .name = "assert", NVP_ASSERT }, @@ -120,7 +122,7 @@ static const Jim_Nvp nvp_error_target[] = { { .value = -1, .name = NULL } }; -const char *target_strerror_safe(int err) +static const char *target_strerror_safe(int err) { const Jim_Nvp *n; @@ -281,7 +283,7 @@ uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer) } /* read a uint8_t from a buffer in target memory endianness */ -uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer) +static uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer) { return *buffer & 0x0ff; } @@ -305,7 +307,7 @@ void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t valu } /* write a uint8_t to a buffer in target memory endianness */ -void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value) +static void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value) { *buffer = value; } @@ -472,10 +474,23 @@ int target_resume(struct target *target, int current, uint32_t address, int hand 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. + * + * For now we require that algorithms provided by OpenOCD are + * used only by code which properly maintains that cached state. + * state + * + * REVISIT do the same for NAND ; maybe other flash flavors too... + */ + if (!target->running_alg) + nor_resume(target); return retval; } -int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode) +static int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode) { char buf[100]; int retval; @@ -620,23 +635,55 @@ static int target_soft_reset_halt_imp(struct target *target) return target->type->soft_reset_halt_imp(target); } -static int target_run_algorithm_imp(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, uint32_t entry_point, uint32_t exit_point, int timeout_ms, void *arch_info) +/** + * Downloads a target-specific native code algorithm to the target, + * and executes it. * Note that some targets may need to set up, enable, + * and tear down a breakpoint (hard or * soft) to detect algorithm + * termination, while others may support lower overhead schemes where + * soft breakpoints embedded in the algorithm automatically terminate the + * algorithm. + * + * @param target used to run the algorithm + * @param arch_info target-specific description of the algorithm. + */ +int target_run_algorithm(struct target *target, + int num_mem_params, struct mem_param *mem_params, + int num_reg_params, struct reg_param *reg_param, + uint32_t entry_point, uint32_t exit_point, + int timeout_ms, void *arch_info) { + int retval = ERROR_FAIL; + if (!target_was_examined(target)) { LOG_ERROR("Target not examined yet"); - return ERROR_FAIL; + goto done; } - return target->type->run_algorithm_imp(target, num_mem_params, mem_params, num_reg_params, reg_param, entry_point, exit_point, timeout_ms, arch_info); + if (!target->type->run_algorithm) { + LOG_ERROR("Target type '%s' does not support %s", + target_type_name(target), __func__); + goto done; + } + + target->running_alg = true; + retval = target->type->run_algorithm(target, + num_mem_params, mem_params, + num_reg_params, reg_param, + entry_point, exit_point, timeout_ms, arch_info); + target->running_alg = false; + +done: + return retval; } + int target_read_memory(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer) { return target->type->read_memory(target, address, size, count, buffer); } -int target_read_phys_memory(struct target *target, +static int target_read_phys_memory(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer) { return target->type->read_phys_memory(target, address, size, count, buffer); @@ -648,7 +695,7 @@ int target_write_memory(struct target *target, return target->type->write_memory(target, address, size, count, buffer); } -int target_write_phys_memory(struct target *target, +static int target_write_phys_memory(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer) { return target->type->write_phys_memory(target, address, size, count, buffer); @@ -702,17 +749,6 @@ int target_step(struct target *target, } -int target_run_algorithm(struct target *target, - int num_mem_params, struct mem_param *mem_params, - int num_reg_params, struct reg_param *reg_param, - uint32_t entry_point, uint32_t exit_point, - int timeout_ms, void *arch_info) -{ - return target->type->run_algorithm(target, - num_mem_params, mem_params, num_reg_params, reg_param, - entry_point, exit_point, timeout_ms, arch_info); -} - /** * Reset the @c examined flag for the given target. * Pure paranoia -- targets are zeroed on allocation. @@ -776,9 +812,6 @@ static int target_init_one(struct command_context *cmd_ctx, type->soft_reset_halt_imp = target->type->soft_reset_halt; type->soft_reset_halt = target_soft_reset_halt_imp; - type->run_algorithm_imp = target->type->run_algorithm; - type->run_algorithm = target_run_algorithm_imp; - /* Sanity-check MMU support ... stub in what we must, to help * implement it in stages, but warn if we need to do so. */ @@ -822,7 +855,7 @@ static int target_init_one(struct command_context *cmd_ctx, return ERROR_OK; } -int target_init(struct command_context *cmd_ctx) +static int target_init(struct command_context *cmd_ctx) { struct target *target; int retval; @@ -955,7 +988,7 @@ int target_unregister_event_callback(int (*callback)(struct target *target, enum return ERROR_OK; } -int target_unregister_timer_callback(int (*callback)(void *priv), void *priv) +static int target_unregister_timer_callback(int (*callback)(void *priv), void *priv) { struct target_timer_callback **p = &target_timer_callbacks; struct target_timer_callback *c = target_timer_callbacks; @@ -1198,7 +1231,7 @@ int target_alloc_working_area(struct target *target, uint32_t size, struct worki return ERROR_OK; } -int target_free_working_area_restore(struct target *target, struct working_area *area, int restore) +static int target_free_working_area_restore(struct target *target, struct working_area *area, int restore) { if (area->free) return ERROR_OK; @@ -1227,7 +1260,7 @@ int target_free_working_area(struct target *target, struct working_area *area) /* free resources and restore memory, if restoring memory fails, * free up resources anyway */ -void target_free_all_working_areas_restore(struct target *target, int restore) +static void target_free_all_working_areas_restore(struct target *target, int restore) { struct working_area *c = target->working_areas; @@ -1775,6 +1808,12 @@ static int handle_target(void *priv) Jim_Interp *interp = (Jim_Interp *)priv; int retval = ERROR_OK; + if (!is_jtag_poll_safe()) + { + /* polling is disabled currently */ + return ERROR_OK; + } + /* we do not want to recurse here... */ static int recursive = 0; if (! recursive) @@ -2285,6 +2324,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) @@ -2292,8 +2401,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--; @@ -2301,7 +2409,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; @@ -2318,35 +2426,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, @@ -3485,22 +3580,6 @@ static int target_array2mem(Jim_Interp *interp, struct target *target, return JIM_OK; } -void target_all_handle_event(enum target_event e) -{ - struct target *target; - - LOG_DEBUG("**all*targets: event: %d, %s", - (int)e, - Jim_Nvp_value2name_simple(nvp_target_event, e)->name); - - target = all_targets; - while (target) { - target_handle_event(target, e); - target = target->next; - } -} - - /* FIX? should we propagate errors here rather than printing them * and continuing? */ @@ -3870,13 +3949,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
[]", cmd_name); return JIM_ERR; } + jim_wide a; int e = Jim_GetOpt_Wide(&goi, &a); if (e != JIM_OK) @@ -3888,7 +3971,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) @@ -3896,36 +3979,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) @@ -3935,7 +4003,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
[]", cmd_name); @@ -3948,7 +4019,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; @@ -3990,7 +4061,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) { @@ -4000,7 +4071,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) { @@ -4010,7 +4081,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) { @@ -5176,7 +5247,7 @@ static const struct command_registration target_exec_command_handlers[] = { }, COMMAND_REGISTRATION_DONE }; -int target_register_user_commands(struct command_context *cmd_ctx) +static int target_register_user_commands(struct command_context *cmd_ctx) { int retval = ERROR_OK; if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)