X-Git-Url: https://review.openocd.org/gitweb?p=openocd.git;a=blobdiff_plain;f=src%2Ftarget%2Ftarget.c;h=dd61b04c12a1d6fa2a00006ed982193de8357fb2;hp=679344138939c2aebac8c50bfe0b7977be3933be;hb=8c1ec4f0e1a7dd37a2549d83b6cd5c8c57eb34aa;hpb=2ccb9ed44cb527398910ebcb4cc661483da1a7f2 diff --git a/src/target/target.c b/src/target/target.c index 6793441389..dd61b04c12 100644 --- a/src/target/target.c +++ b/src/target/target.c @@ -2,12 +2,18 @@ * Copyright (C) 2005 by Dominic Rath * * Dominic.Rath@gmx.de * * * - * Copyright (C) 2007,2008 Øyvind Harboe * + * Copyright (C) 2007,2008 Øyvind Harboe * * oyvind.harboe@zylin.com * * * * Copyright (C) 2008, Duane Ellis * * openocd@duaneeellis.com * * * + * Copyright (C) 2008 by Spencer Oliver * + * spen@spen-soft.co.uk * + * * + * Copyright (C) 2008 by Rick Altherr * + * kc8apf@kc8apf.net> * + * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * @@ -53,32 +59,31 @@ #include #include -int cli_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv); - - -int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); - -int handle_working_area_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); - -int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); -int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); -int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); -int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); -int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); -int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); -int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); -int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); -int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); -int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); -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_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); -int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); -int handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc); -int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); +static int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); + +static int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); +static int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); +static int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); +static int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); +static int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); +static int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); +static int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); +static int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); +static int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); +static int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); +static int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); +static int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); +static int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); +static int handle_test_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); +static int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); +static int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); +static int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); +static int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); +static int handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc); +static int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); +static int handle_fast_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); +static int handle_fast_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc); + static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv); static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv); static int jim_target( Jim_Interp *interp, int argc, Jim_Obj *const *argv); @@ -86,8 +91,6 @@ static int jim_target( Jim_Interp *interp, int argc, Jim_Obj *const *argv); static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv); static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv); - - /* targets */ extern target_type_t arm7tdmi_target; extern target_type_t arm720t_target; @@ -100,6 +103,7 @@ extern target_type_t xscale_target; extern target_type_t cortexm3_target; extern target_type_t arm11_target; extern target_type_t mips_m4k_target; +extern target_type_t avr_target; target_type_t *target_types[] = { @@ -114,6 +118,7 @@ target_type_t *target_types[] = &cortexm3_target, &arm11_target, &mips_m4k_target, + &avr_target, NULL, }; @@ -159,17 +164,18 @@ const char *target_strerror_safe( int err ) } const Jim_Nvp nvp_target_event[] = { - { .value = TARGET_EVENT_OLD_pre_reset , .name = "old-pre_reset" }, { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" }, - { .value = TARGET_EVENT_OLD_post_reset , .name = "old-post_reset" }, { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" }, - + { .value = TARGET_EVENT_EARLY_HALTED, .name = "early-halted" }, { .value = TARGET_EVENT_HALTED, .name = "halted" }, { .value = TARGET_EVENT_RESUMED, .name = "resumed" }, { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" }, { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" }, + { .name = "gdb-start", .value = TARGET_EVENT_GDB_START }, + { .name = "gdb-end", .value = TARGET_EVENT_GDB_END }, + /* historical name */ { .value = TARGET_EVENT_RESET_START, .name = "reset-start" }, @@ -185,13 +191,8 @@ const Jim_Nvp nvp_target_event[] = { { .value = TARGET_EVENT_RESET_INIT , .name = "reset-init" }, { .value = TARGET_EVENT_RESET_END, .name = "reset-end" }, - - - - { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" }, - { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-end" }, - + { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" }, { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" }, { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" }, @@ -199,7 +200,6 @@ const Jim_Nvp nvp_target_event[] = { { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" }, { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" }, - { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" }, { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" }, @@ -222,7 +222,6 @@ const Jim_Nvp nvp_target_state[] = { { .name = NULL, .value = -1 }, }; - const Jim_Nvp nvp_target_debug_reason [] = { { .name = "debug-request" , .value = DBG_REASON_DBGRQ }, { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT }, @@ -234,12 +233,11 @@ 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 }, { .name = "be", .value = TARGET_BIG_ENDIAN }, - { .name = "le", .value = TARGET_LITTLE_ENDIAN }, + { .name = "le", .value = TARGET_LITTLE_ENDIAN }, { .name = NULL, .value = -1 }, }; @@ -251,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; @@ -269,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; @@ -370,7 +366,6 @@ target_t* get_current_target(command_context_t *cmd_ctx) return target; } - int target_poll(struct target_s *target) { /* We can't poll until after examine */ @@ -414,98 +409,27 @@ int target_resume(struct target_s *target, int current, u32 address, int handle_ return retval; } -// Next patch - this turns into TCL... int target_process_reset(struct command_context_s *cmd_ctx, enum target_reset_mode reset_mode) { - int retval = ERROR_OK; - target_t *target; - - target = all_targets; - - target_all_handle_event( TARGET_EVENT_OLD_pre_reset ); - - if ((retval = jtag_init_reset(cmd_ctx)) != ERROR_OK) - return retval; - - keep_alive(); /* we might be running on a very slow JTAG clk */ - - /* First time this is executed after launching OpenOCD, it will read out - * the type of CPU, etc. and init Embedded ICE registers in host - * memory. - * - * It will also set up ICE registers in the target. - * - * However, if we assert TRST later, we need to set up the registers again. - * - * For the "reset halt/init" case we must only set up the registers here. - */ - if ((retval = target_examine()) != ERROR_OK) - return retval; - - keep_alive(); /* we might be running on a very slow JTAG clk */ - - target = all_targets; - while (target) - { - /* we have no idea what state the target is in, so we - * have to drop working areas - */ - target_free_all_working_areas_restore(target, 0); - target->reset_halt=((reset_mode==RESET_HALT)||(reset_mode==RESET_INIT)); - if ((retval = target->type->assert_reset(target))!=ERROR_OK) - return retval; - target = target->next; - } - - target = all_targets; - while (target) - { - if ((retval = target->type->deassert_reset(target))!=ERROR_OK) - return retval; - target = target->next; - } - - target = all_targets; - while (target) - { - /* We can fail to bring the target into the halted state, try after reset has been deasserted */ - if (target->reset_halt) - { - /* wait up to 1 second for halt. */ - target_wait_state(target, TARGET_HALTED, 1000); - if (target->state != TARGET_HALTED) - { - LOG_WARNING("Failed to reset target into halted mode - issuing halt"); - if ((retval = target->type->halt(target))!=ERROR_OK) - return retval; - } - } - - target = target->next; + char buf[100]; + int retval; + Jim_Nvp *n; + n = Jim_Nvp_value2name_simple( nvp_reset_modes, reset_mode ); + if( n->name == NULL ){ + LOG_ERROR("invalid reset mode"); + return ERROR_FAIL; } + sprintf( buf, "ocd_process_reset %s", n->name ); + retval = Jim_Eval( interp, buf ); - LOG_DEBUG("Waiting for halted stated as appropriate"); - - if ((reset_mode == RESET_HALT) || (reset_mode == RESET_INIT)) - { - target = all_targets; - while (target) - { - /* Wait for reset to complete, maximum 5 seconds. */ - if (((retval=target_wait_state(target, TARGET_HALTED, 5000)))==ERROR_OK) - { - if (reset_mode == RESET_INIT){ - target_handle_event( target, TARGET_EVENT_OLD_post_reset ); - } - - } - target = target->next; - } + if(retval != JIM_OK) { + Jim_PrintErrorMessage(interp); + return ERROR_FAIL; } /* We want any events to be processed before the prompt */ - target_call_timer_callbacks_now(); + retval = target_call_timer_callbacks_now(); return retval; } @@ -528,7 +452,6 @@ static int default_examine(struct target_s *target) return ERROR_OK; } - /* Targets that correctly implement init+examine, i.e. * no communication with target during init: * @@ -590,6 +513,7 @@ static int target_run_algorithm_imp(struct target_s *target, int num_mem_params, int target_init(struct command_context_s *cmd_ctx) { target_t *target = all_targets; + int retval; while (target) { @@ -599,10 +523,10 @@ int target_init(struct command_context_s *cmd_ctx) target->type->examine = default_examine; } - if (target->type->init_target(cmd_ctx, target) != ERROR_OK) + if ((retval = target->type->init_target(cmd_ctx, target)) != ERROR_OK) { LOG_ERROR("target '%s' init failed", target->type->name); - exit(-1); + return retval; } /* Set up default functions if none are provided by target */ @@ -623,7 +547,6 @@ int target_init(struct command_context_s *cmd_ctx) target->type->run_algorithm_imp = target->type->run_algorithm; target->type->run_algorithm = target_run_algorithm_imp; - if (target->type->mmu == NULL) { target->type->mmu = default_mmu; @@ -633,8 +556,10 @@ int target_init(struct command_context_s *cmd_ctx) if (all_targets) { - target_register_user_commands(cmd_ctx); - target_register_timer_callback(handle_target, 100, 1, NULL); + if((retval = target_register_user_commands(cmd_ctx)) != ERROR_OK) + return retval; + if((retval = target_register_timer_callback(handle_target, 100, 1, NULL)) != ERROR_OK) + return retval; } return ERROR_OK; @@ -761,6 +686,12 @@ int target_call_event_callbacks(target_t *target, enum target_event event) target_event_callback_t *callback = target_event_callbacks; target_event_callback_t *next_callback; + if (event == TARGET_EVENT_HALTED) + { + /* execute early halted first */ + target_call_event_callbacks(target, TARGET_EVENT_EARLY_HALTED); + } + LOG_DEBUG("target event %i (%s)", event, Jim_Nvp_value2name_simple( nvp_target_event, event )->name ); @@ -783,6 +714,8 @@ static int target_call_timer_callbacks_check_time(int checktime) target_timer_callback_t *next_callback; struct timeval now; + keep_alive(); + gettimeofday(&now, NULL); while (callback) @@ -809,7 +742,11 @@ static int target_call_timer_callbacks_check_time(int checktime) } } else - target_unregister_timer_callback(callback->callback, callback->priv); + { + int retval; + if((retval = target_unregister_timer_callback(callback->callback, callback->priv)) != ERROR_OK) + return retval; + } } } @@ -827,7 +764,7 @@ int target_call_timer_callbacks(void) /* invoke periodic callbacks immediately */ int target_call_timer_callbacks_now(void) { - return target_call_timer_callbacks(); + return target_call_timer_callbacks_check_time(0); } int target_alloc_working_area(struct target_s *target, u32 size, working_area_t **area) @@ -904,8 +841,14 @@ int target_alloc_working_area(struct target_s *target, u32 size, working_area_t if (target->backup_working_area) { + int retval; new_wa->backup = malloc(new_wa->size); - target->type->read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup); + if((retval = target->type->read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK) + { + free(new_wa->backup); + free(new_wa); + return retval; + } } else { @@ -932,7 +875,11 @@ int target_free_working_area_restore(struct target_s *target, working_area_t *ar return ERROR_OK; if (restore&&target->backup_working_area) - target->type->write_memory(target, area->address, 4, area->size / 4, area->backup); + { + int retval; + if((retval = target->type->write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK) + return retval; + } area->free = 1; @@ -948,7 +895,10 @@ int target_free_working_area(struct target_s *target, working_area_t *area) return target_free_working_area_restore(target, area, 1); } -int target_free_all_working_areas_restore(struct target_s *target, int restore) +/* free resources and restore memory, if restoring memory fails, + * free up resources anyway + */ +void target_free_all_working_areas_restore(struct target_s *target, int restore) { working_area_t *c = target->working_areas; @@ -966,29 +916,23 @@ int target_free_all_working_areas_restore(struct target_s *target, int restore) } target->working_areas = NULL; - - return ERROR_OK; } -int target_free_all_working_areas(struct target_s *target) +void target_free_all_working_areas(struct target_s *target) { - return target_free_all_working_areas_restore(target, 1); + target_free_all_working_areas_restore(target, 1); } int target_register_commands(struct command_context_s *cmd_ctx) { - register_command(cmd_ctx, NULL, "targets", handle_targets_command, COMMAND_EXEC, NULL); - register_command(cmd_ctx, NULL, "working_area", handle_working_area_command, COMMAND_ANY, "working_area
<'backup'|'nobackup'> [virtual address]"); - register_command(cmd_ctx, NULL, "virt2phys", handle_virt2phys_command, COMMAND_ANY, "virt2phys "); - register_command(cmd_ctx, NULL, "profile", handle_profile_command, COMMAND_EXEC, "PRELIMINARY! - profile "); + register_command(cmd_ctx, NULL, "targets", handle_targets_command, COMMAND_EXEC, "change the current command line target (one parameter) or lists targets (with no parameter)"); - register_jim(cmd_ctx, "target", jim_target, "configure target" ); - /* script procedures */ - 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, "target", jim_target, "configure target" ); + return ERROR_OK; } @@ -1041,7 +985,7 @@ int target_write_buffer(struct target_s *target, u32 address, u32 size, u8 *buff /* handle unaligned head bytes */ if (address % 4) { - int unaligned = 4 - (address % 4); + u32 unaligned = 4 - (address % 4); if (unaligned > size) unaligned = size; @@ -1086,7 +1030,6 @@ int target_write_buffer(struct target_s *target, u32 address, u32 size, u8 *buff return ERROR_OK; } - /* Single aligned words are guaranteed to use 16 or 32 bit access * mode respectively, otherwise data is handled as quickly as * possible @@ -1117,7 +1060,7 @@ int target_read_buffer(struct target_s *target, u32 address, u32 size, u8 *buffe /* handle unaligned head bytes */ if (address % 4) { - int unaligned = 4 - (address % 4); + u32 unaligned = 4 - (address % 4); if (unaligned > size) unaligned = size; @@ -1157,7 +1100,7 @@ int target_checksum_memory(struct target_s *target, u32 address, u32 size, u32* { u8 *buffer; int retval; - int i; + u32 i; u32 checksum = 0; if (!target->type->examined) { @@ -1166,7 +1109,7 @@ int target_checksum_memory(struct target_s *target, u32 address, u32 size, u32* } if ((retval = target->type->checksum_memory(target, address, - size, &checksum)) == ERROR_TARGET_RESOURCE_NOT_AVAILABLE) + size, &checksum)) != ERROR_OK) { buffer = malloc(size); if (buffer == NULL) @@ -1340,7 +1283,7 @@ int target_write_u8(struct target_s *target, u32 address, u8 value) LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, value); - if ((retval = target->type->read_memory(target, address, 1, 1, &value)) != ERROR_OK) + if ((retval = target->type->write_memory(target, address, 1, 1, &value)) != ERROR_OK) { LOG_DEBUG("failed: %i", retval); } @@ -1350,7 +1293,23 @@ int target_write_u8(struct target_s *target, u32 address, u8 value) int target_register_user_commands(struct command_context_s *cmd_ctx) { - register_command(cmd_ctx, NULL, "reg", handle_reg_command, COMMAND_EXEC, NULL); + int retval = ERROR_OK; + + + /* 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_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"); + + register_command(cmd_ctx, NULL, "fast_load", handle_fast_load_command, COMMAND_ANY, + "loads active fast load image to current target - mainly for profiling purposes"); + + + register_command(cmd_ctx, NULL, "virt2phys", handle_virt2phys_command, COMMAND_ANY, "translate a virtual address into a physical address"); + register_command(cmd_ctx, NULL, "reg", handle_reg_command, COMMAND_EXEC, "display or set a register"); register_command(cmd_ctx, NULL, "poll", handle_poll_command, COMMAND_EXEC, "poll target state"); register_command(cmd_ctx, NULL, "wait_halt", handle_wait_halt_command, COMMAND_EXEC, "wait for target halt [time (s)]"); register_command(cmd_ctx, NULL, "halt", handle_halt_command, COMMAND_EXEC, "halt target"); @@ -1375,14 +1334,17 @@ 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
['bin'|'ihex'|'elf'|'s19'] [min_address] [max_length]"); register_command(cmd_ctx, NULL, "dump_image", handle_dump_image_command, COMMAND_EXEC, "dump_image
"); register_command(cmd_ctx, NULL, "verify_image", handle_verify_image_command, COMMAND_EXEC, "verify_image [offset] [type]"); + register_command(cmd_ctx, NULL, "test_image", handle_test_image_command, COMMAND_EXEC, "test_image [offset] [type]"); - target_request_register_commands(cmd_ctx); - trace_register_commands(cmd_ctx); + if((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK) + return retval; + if((retval = trace_register_commands(cmd_ctx)) != ERROR_OK) + return retval; - return ERROR_OK; + return retval; } -int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) +static int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) { char *cp; target_t *target = all_targets; @@ -1390,7 +1352,7 @@ int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char ** if (argc == 1) { /* try as tcltarget name */ - for( target = all_targets ; target ; target++ ){ + for( target = all_targets ; target ; target = target->next ){ if( target->cmd_name ){ if( 0 == strcmp( args[0], target->cmd_name ) ){ /* MATCH */ @@ -1416,19 +1378,21 @@ int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char ** cmd_ctx->current_target = target->target_number; return ERROR_OK; } - DumpTargets: +DumpTargets: - command_print(cmd_ctx, " CmdName Type Endian ChainPos State "); - command_print(cmd_ctx, "-- ---------- ---------- ---------- -------- ----------"); + target = all_targets; + command_print(cmd_ctx, " CmdName Type Endian AbsChainPos Name State "); + command_print(cmd_ctx, "-- ---------- ---------- ---------- ----------- ------------- ----------"); while (target) { /* XX: abcdefghij abcdefghij abcdefghij abcdefghij */ - command_print(cmd_ctx, "%2d: %-10s %-10s %-10s %8d %s", + command_print(cmd_ctx, "%2d: %-10s %-10s %-10s %10d %14s %s", target->target_number, target->cmd_name, target->type->name, Jim_Nvp_value2name_simple( nvp_target_endian, target->endianness )->name, - target->chain_position, + target->tap->abs_chain_position, + target->tap->dotted_name, Jim_Nvp_value2name_simple( nvp_target_state, target->state )->name ); target = target->next; } @@ -1436,69 +1400,147 @@ int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char ** return ERROR_OK; } +/* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */ + +static int powerDropout; +static int srstAsserted; +static int runPowerRestore; +static int runPowerDropout; +static int runSrstAsserted; +static int runSrstDeasserted; -int handle_working_area_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) +static int sense_handler(void) { - target_t *target = NULL; + static int prevSrstAsserted = 0; + static int prevPowerdropout = 0; - if ((argc < 4) || (argc > 5)) - { - return ERROR_COMMAND_SYNTAX_ERROR; - } + int retval; + if ((retval=jtag_power_dropout(&powerDropout))!=ERROR_OK) + return retval; - target = get_target_by_num(strtoul(args[0], NULL, 0)); - if (!target) + int powerRestored; + powerRestored = prevPowerdropout && !powerDropout; + if (powerRestored) { - return ERROR_COMMAND_SYNTAX_ERROR; + runPowerRestore = 1; } - target_free_all_working_areas(target); - target->working_area_phys = target->working_area_virt = strtoul(args[1], NULL, 0); - if (argc == 5) + long long current = timeval_ms(); + static long long lastPower = 0; + int waitMore = lastPower + 2000 > current; + if (powerDropout && !waitMore) { - target->working_area_virt = strtoul(args[4], NULL, 0); + runPowerDropout = 1; + lastPower = current; } - target->working_area_size = strtoul(args[2], NULL, 0); - if (strcmp(args[3], "backup") == 0) + if ((retval=jtag_srst_asserted(&srstAsserted))!=ERROR_OK) + return retval; + + int srstDeasserted; + srstDeasserted = prevSrstAsserted && !srstAsserted; + + static long long lastSrst = 0; + waitMore = lastSrst + 2000 > current; + if (srstDeasserted && !waitMore) { - target->backup_working_area = 1; + runSrstDeasserted = 1; + lastSrst = current; } - else if (strcmp(args[3], "nobackup") == 0) + + if (!prevSrstAsserted && srstAsserted) { - target->backup_working_area = 0; + runSrstAsserted = 1; } - else + + prevSrstAsserted = srstAsserted; + prevPowerdropout = powerDropout; + + if (srstDeasserted || powerRestored) { - LOG_ERROR("unrecognized argument (%s)", args[3]); - return ERROR_COMMAND_SYNTAX_ERROR; + /* Other than logging the event we can't do anything here. + * Issuing a reset is a particularly bad idea as we might + * be inside a reset already. + */ } return ERROR_OK; } - /* process target state changes */ int handle_target(void *priv) { + int retval = ERROR_OK; + + /* we do not want to recurse here... */ + static int recursive = 0; + if (! recursive) + { + recursive = 1; + sense_handler(); + /* danger! running these procedures can trigger srst assertions and power dropouts. + * We need to avoid an infinite loop/recursion here and we do that by + * clearing the flags after running these events. + */ + int did_something = 0; + if (runSrstAsserted) + { + Jim_Eval( interp, "srst_asserted"); + did_something = 1; + } + if (runSrstDeasserted) + { + Jim_Eval( interp, "srst_deasserted"); + did_something = 1; + } + if (runPowerDropout) + { + Jim_Eval( interp, "power_dropout"); + did_something = 1; + } + if (runPowerRestore) + { + Jim_Eval( interp, "power_restore"); + did_something = 1; + } + + if (did_something) + { + /* clear detect flags */ + sense_handler(); + } + + /* clear action flags */ + + runSrstAsserted=0; + runSrstDeasserted=0; + runPowerRestore=0; + runPowerDropout=0; + + recursive = 0; + } + target_t *target = all_targets; while (target) { - if (target_continous_poll) + + /* only poll target if we've got power and srst isn't asserted */ + if (target_continous_poll&&!powerDropout&&!srstAsserted) { /* polling may fail silently until the target has been examined */ - target_poll(target); + if((retval = target_poll(target)) != ERROR_OK) + return retval; } target = target->next; } - return ERROR_OK; + return retval; } -int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) +static int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) { target_t *target; reg_t *reg = NULL; @@ -1578,11 +1620,6 @@ int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args if (reg->valid == 0) { reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type); - if (arch_type == NULL) - { - LOG_ERROR("BUG: encountered unregistered arch type"); - return ERROR_OK; - } arch_type->get(reg); } value = buf_to_str(reg->value, reg->size, 16); @@ -1598,12 +1635,6 @@ int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0); reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type); - if (arch_type == NULL) - { - LOG_ERROR("BUG: encountered unregistered arch type"); - return ERROR_OK; - } - arch_type->set(reg, buf); value = buf_to_str(reg->value, reg->size, 16); @@ -1620,17 +1651,20 @@ int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args return ERROR_OK; } - -int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) +static int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) { + int retval = ERROR_OK; target_t *target = get_current_target(cmd_ctx); if (argc == 0) { - target_poll(target); - target_arch_state(target); + if((retval = target_poll(target)) != ERROR_OK) + return retval; + if((retval = target_arch_state(target)) != ERROR_OK) + return retval; + } - else + else if (argc==1) { if (strcmp(args[0], "on") == 0) { @@ -1644,13 +1678,15 @@ int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **arg { command_print(cmd_ctx, "arg is \"on\" or \"off\""); } + } else + { + return ERROR_COMMAND_SYNTAX_ERROR; } - - return ERROR_OK; + return retval; } -int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) +static int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) { int ms = 5000; @@ -1670,35 +1706,44 @@ 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); + 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); + Jim_Nvp_value2name_simple(nvp_target_state,state)->name); return ERROR_FAIL; } } @@ -1706,7 +1751,7 @@ int target_wait_state(target_t *target, enum target_state state, int ms) return ERROR_OK; } -int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) +static int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) { int retval; target_t *target = get_current_target(cmd_ctx); @@ -1718,10 +1763,20 @@ 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); } -int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) +static int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) { target_t *target = get_current_target(cmd_ctx); @@ -1732,7 +1787,7 @@ int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, return ERROR_OK; } -int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) +static int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) { const Jim_Nvp *n; enum target_reset_mode reset_mode = RESET_RUN; @@ -1750,7 +1805,8 @@ int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **ar return target_process_reset(cmd_ctx, reset_mode); } -int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) + +static int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) { int retval; target_t *target = get_current_target(cmd_ctx); @@ -1769,22 +1825,22 @@ int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **a return retval; } -int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) +static int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) { target_t *target = get_current_target(cmd_ctx); LOG_DEBUG("-"); if (argc == 0) - target->type->step(target, 1, 0, 1); /* current pc, addr = 0, handle breakpoints */ + return target->type->step(target, 1, 0, 1); /* current pc, addr = 0, handle breakpoints */ if (argc == 1) - target->type->step(target, 0, strtoul(args[0], NULL, 0), 1); /* addr = args[0], handle breakpoints */ + return target->type->step(target, 0, strtoul(args[0], NULL, 0), 1); /* addr = args[0], handle breakpoints */ return ERROR_OK; } -int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) +static int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) { const int line_bytecnt = 32; int count = 1; @@ -1809,7 +1865,6 @@ int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, address = strtoul(args[0], NULL, 0); - switch (cmd[2]) { case 'w': @@ -1862,7 +1917,7 @@ int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, return retval; } -int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) +static int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) { u32 address = 0; u32 value = 0; @@ -1914,6 +1969,8 @@ int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, default: return ERROR_OK; } + keep_alive(); + if (retval!=ERROR_OK) { return retval; @@ -1924,7 +1981,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) +static int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) { u8 *buffer; u32 buf_cnt; @@ -1932,7 +1989,7 @@ int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char u32 min_address=0; u32 max_address=0xffffffff; int i; - int retval; + int retval, retvaltemp; image_t image; @@ -1974,7 +2031,6 @@ int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char return ERROR_COMMAND_SYNTAX_ERROR; } - duration_start_measure(&duration); if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK) @@ -2002,7 +2058,6 @@ int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char u32 offset=0; u32 length=buf_cnt; - /* DANGER!!! beware of unsigned comparision here!!! */ if ((image.sections[i].base_address+buf_cnt>=min_address)&& @@ -2032,7 +2087,12 @@ int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char free(buffer); } - duration_stop_measure(&duration, &duration_text); + if((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK) + { + image_close(&image); + return retvaltemp; + } + if (retval==ERROR_OK) { command_print(cmd_ctx, "downloaded %u byte in %s", image_size, duration_text); @@ -2045,14 +2105,14 @@ int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char } -int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) +static int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) { fileio_t fileio; u32 address; u32 size; u8 buffer[560]; - int retval=ERROR_OK; + int retval=ERROR_OK, retvaltemp; duration_t duration; char *duration_text; @@ -2102,9 +2162,12 @@ int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char address += this_run_size; } - fileio_close(&fileio); + if((retvaltemp = fileio_close(&fileio)) != ERROR_OK) + return retvaltemp; + + if((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK) + return retvaltemp; - duration_stop_measure(&duration, &duration_text); if (retval==ERROR_OK) { command_print(cmd_ctx, "dumped %"PRIi64" byte in %s", fileio.size, duration_text); @@ -2114,13 +2177,13 @@ 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) +static 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; u32 image_size; int i; - int retval; + int retval, retvaltemp; u32 checksum = 0; u32 mem_checksum = 0; @@ -2178,58 +2241,74 @@ 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"); + /* calculate checksum of image */ + image_calculate_checksum( buffer, buf_cnt, &checksum ); - data = (u8*)malloc(buf_cnt); - - /* 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]) + size *= 4; + count /= 4; + } + retval = target->type->read_memory(target, image.sections[i].base_address, size, count, data); + if (retval == ERROR_OK) + { + u32 t; + for (t = 0; t < buf_cnt; 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 (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); image_size += buf_cnt; } done: - duration_stop_measure(&duration, &duration_text); + + if((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK) + { + image_close(&image); + return retvaltemp; + } + if (retval==ERROR_OK) { command_print(cmd_ctx, "verified %u bytes in %s", image_size, duration_text); @@ -2241,7 +2320,17 @@ done: return retval; } -int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) +static 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); +} + +static 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); +} + +static int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) { int retval; target_t *target = get_current_target(cmd_ctx); @@ -2293,7 +2382,7 @@ int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, return ERROR_OK; } -int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) +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); @@ -2303,7 +2392,7 @@ int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args return ERROR_OK; } -int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) +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; @@ -2365,7 +2454,7 @@ int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, return ERROR_OK; } -int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) +static int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) { target_t *target = get_current_target(cmd_ctx); @@ -2375,7 +2464,7 @@ int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args return ERROR_OK; } -int handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc) +static int handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc) { int retval; target_t *target = get_current_target(cmd_ctx); @@ -2401,39 +2490,47 @@ int handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, } return retval; } + +static void writeData(FILE *f, const void *data, size_t len) +{ + size_t written = fwrite(data, len, 1, f); + if (written != len) + LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno)); +} + static void writeLong(FILE *f, int l) { int i; for (i=0; i<4; i++) { char c=(l>>(i*8))&0xff; - fwrite(&c, 1, 1, f); + writeData(f, &c, 1); } } + static void writeString(FILE *f, char *s) { - fwrite(s, 1, strlen(s), f); + writeData(f, s, strlen(s)); } - - -// Dump a gmon.out histogram file. -static void writeGmon(u32 *samples, int sampleNum, char *filename) +/* Dump a gmon.out histogram file. */ +static void writeGmon(u32 *samples, u32 sampleNum, char *filename) { - int i; + u32 i; FILE *f=fopen(filename, "w"); if (f==NULL) return; - fwrite("gmon", 1, 4, f); - writeLong(f, 0x00000001); // Version - writeLong(f, 0); // padding - writeLong(f, 0); // padding - writeLong(f, 0); // padding + writeString(f, "gmon"); + writeLong(f, 0x00000001); /* Version */ + writeLong(f, 0); /* padding */ + writeLong(f, 0); /* padding */ + writeLong(f, 0); /* padding */ - fwrite("", 1, 1, f); // GMON_TAG_TIME_HIST + u8 zero = 0; /* GMON_TAG_TIME_HIST */ + writeData(f, &zero, 1); - // figure out bucket size + /* figure out bucket size */ u32 min=samples[0]; u32 max=samples[0]; for (i=0; i maxBuckets) { length=maxBuckets; @@ -2469,23 +2566,21 @@ static void writeGmon(u32 *samples, int sampleNum, char *filename) long long a=address-min; long long b=length-1; long long c=addressSpace-1; - int index=(a*b)/c; // danger!!!! int32 overflows + int index=(a*b)/c; /* danger!!!! int32 overflows */ buckets[index]++; } - // append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) - writeLong(f, min); // low_pc - writeLong(f, max); // high_pc - writeLong(f, length); // # of samples - writeLong(f, 64000000); // 64MHz + /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */ + writeLong(f, min); /* low_pc */ + writeLong(f, max); /* high_pc */ + writeLong(f, length); /* # of samples */ + writeLong(f, 64000000); /* 64MHz */ writeString(f, "seconds"); for (i=0; i<(15-strlen("seconds")); i++) - { - fwrite("", 1, 1, f); // padding - } + writeData(f, &zero, 1); writeString(f, "s"); -// append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) + /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */ char *data=malloc(2*length); if (data!=NULL) @@ -2502,7 +2597,7 @@ static void writeGmon(u32 *samples, int sampleNum, char *filename) data[i*2+1]=(val>>8)&0xff; } free(buckets); - fwrite(data, 1, length*2, f); + writeData(f, data, length * 2); free(data); } else { @@ -2513,7 +2608,7 @@ static void writeGmon(u32 *samples, int sampleNum, char *filename) } /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */ -int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) +static int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) { target_t *target = get_current_target(cmd_ctx); struct timeval timeout, now; @@ -2539,7 +2634,7 @@ int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char ** int numSamples=0; int retval=ERROR_OK; - // hopefully it is safe to cache! We want to stop/restart as quickly as possible. + /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */ reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1); for (;;) @@ -2551,11 +2646,15 @@ int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char ** samples[numSamples++]=t; retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */ target_poll(target); - alive_sleep(10); // sleep 10ms, i.e. <100 samples/second. + alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */ } else if (target->state == TARGET_RUNNING) { - // We want to quickly sample the PC. - target_halt(target); + /* We want to quickly sample the PC. */ + if((retval = target_halt(target)) != ERROR_OK) + { + free(samples); + return retval; + } } else { command_print(cmd_ctx, "Target not halted or running"); @@ -2571,12 +2670,20 @@ int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char ** if ((numSamples>=maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec))) { command_print(cmd_ctx, "Profiling completed. %d samples.", numSamples); - target_poll(target); + if((retval = target_poll(target)) != ERROR_OK) + { + free(samples); + return retval; + } if (target->state == TARGET_HALTED) { target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */ } - target_poll(target); + if((retval = target_poll(target)) != ERROR_OK) + { + free(samples); + return retval; + } writeGmon(samples, numSamples, args[1]); command_print(cmd_ctx, "Wrote %s", args[1]); break; @@ -2640,13 +2747,14 @@ static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_ { long l; u32 width; - u32 len; + int len; u32 addr; u32 count; u32 v; const char *varname; u8 buffer[4096]; - int i, n, e, retval; + int n, e, retval; + u32 i; /* argv[1] = name of array to receive the data * argv[2] = desired width @@ -2817,18 +2925,18 @@ static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv) return target_array2mem( interp,target, argc, argv ); } - static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv) { long l; u32 width; - u32 len; + int len; u32 addr; u32 count; u32 v; const char *varname; u8 buffer[4096]; - int i, n, e, retval; + int n, e, retval; + u32 i; /* argv[1] = name of array to get the data * argv[2] = desired width @@ -2902,7 +3010,6 @@ static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_ return JIM_ERR; } - /* Transfer loop */ /* index counter */ @@ -2950,15 +3057,13 @@ static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_ return JIM_OK; } -void -target_all_handle_event( enum target_event e ) +void target_all_handle_event( enum target_event e ) { target_t *target; - LOG_DEBUG( "**all*targets: event: %d, %s", - e, - Jim_Nvp_value2name_simple( nvp_target_event, e )->name ); + e, + Jim_Nvp_value2name_simple( nvp_target_event, e )->name ); target = all_targets; while (target){ @@ -2967,8 +3072,7 @@ target_all_handle_event( enum target_event e ) } } -void -target_handle_event( target_t *target, enum target_event e ) +void target_handle_event( target_t *target, enum target_event e ) { target_event_action_t *teap; int done; @@ -2986,7 +3090,10 @@ target_handle_event( target_t *target, enum target_event e ) e, Jim_Nvp_value2name_simple( nvp_target_event, e )->name, Jim_GetString( teap->body, NULL ) ); - Jim_EvalObj( interp, teap->body ); + if (Jim_EvalObj( interp, teap->body )!=JIM_OK) + { + Jim_PrintErrorMessage(interp); + } } teap = teap->next; } @@ -3000,7 +3107,6 @@ target_handle_event( target_t *target, enum target_event e ) enum target_cfg_param { TCFG_TYPE, TCFG_EVENT, - TCFG_RESET, TCFG_WORK_AREA_VIRT, TCFG_WORK_AREA_PHYS, TCFG_WORK_AREA_SIZE, @@ -3010,11 +3116,9 @@ enum target_cfg_param { TCFG_CHAIN_POSITION, }; - static Jim_Nvp nvp_config_opts[] = { { .name = "-type", .value = TCFG_TYPE }, { .name = "-event", .value = TCFG_EVENT }, - { .name = "-reset", .value = TCFG_RESET }, { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT }, { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS }, { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE }, @@ -3026,10 +3130,7 @@ static Jim_Nvp nvp_config_opts[] = { { .name = NULL, .value = -1 } }; - -static int -target_configure( Jim_GetOptInfo *goi, - target_t *target ) +static int target_configure( Jim_GetOptInfo *goi, target_t *target ) { Jim_Nvp *n; Jim_Obj *o; @@ -3037,11 +3138,10 @@ target_configure( Jim_GetOptInfo *goi, char *cp; int e; - /* parse config or cget options ... */ - while( goi->argc ){ + while( goi->argc > 0 ){ Jim_SetEmptyResult( goi->interp ); - //Jim_GetOpt_Debug( goi ); + /* Jim_GetOpt_Debug( goi ); */ if( target->type->target_jim_configure ){ /* target defines a configure function */ @@ -3091,7 +3191,7 @@ target_configure( Jim_GetOptInfo *goi, } if( goi->isconfigure ){ - if( goi->argc == 0 ){ + if( goi->argc != 1 ){ Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?"); return JIM_ERR; } @@ -3102,14 +3202,13 @@ target_configure( Jim_GetOptInfo *goi, } } - { target_event_action_t *teap; teap = target->event_action; /* replace existing? */ while( teap ){ - if( teap->event == n->value ){ + if( teap->event == (enum target_event)n->value ){ break; } teap = teap->next; @@ -3268,33 +3367,37 @@ target_configure( Jim_GetOptInfo *goi, break; case TCFG_CHAIN_POSITION: if( goi->isconfigure ){ + Jim_Obj *o; + jtag_tap_t *tap; target_free_all_working_areas(target); - e = Jim_GetOpt_Wide( goi, &w ); + e = Jim_GetOpt_Obj( goi, &o ); if( e != JIM_OK ){ return e; } + tap = jtag_TapByJimObj( goi->interp, o ); + if( tap == NULL ){ + return JIM_ERR; + } /* make this exactly 1 or 0 */ - target->chain_position = w; + target->tap = tap; } else { if( goi->argc != 0 ){ goto no_params; } } - Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->chain_position ) ); + Jim_SetResultString( interp, target->tap->dotted_name, -1 ); /* loop for more e*/ break; } - } - /* done - we return */ + } /* while( goi->argc ) */ + + + /* done - we return */ return JIM_OK; } - /** this is the 'tcl' handler for the target specific command */ -static int -tcl_target_func( Jim_Interp *interp, - int argc, - Jim_Obj *const *argv ) +static int tcl_target_func( Jim_Interp *interp, int argc, Jim_Obj *const *argv ) { Jim_GetOptInfo goi; jim_wide a,b,c; @@ -3305,7 +3408,6 @@ tcl_target_func( Jim_Interp *interp, struct command_context_s *cmd_ctx; int e; - enum { TS_CMD_CONFIGURE, TS_CMD_CGET, @@ -3321,6 +3423,7 @@ tcl_target_func( Jim_Interp *interp, TS_CMD_WAITSTATE, TS_CMD_EVENTLIST, TS_CMD_CURSTATE, + TS_CMD_INVOKE_EVENT, }; static const Jim_Nvp target_options[] = { @@ -3342,11 +3445,11 @@ tcl_target_func( Jim_Interp *interp, { .name = "arp_reset", .value = TS_CMD_RESET }, { .name = "arp_halt", .value = TS_CMD_HALT }, { .name = "arp_waitstate", .value = TS_CMD_WAITSTATE }, + { .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT }, { .name = NULL, .value = -1 }, }; - /* go past the "command" */ Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 ); @@ -3359,7 +3462,7 @@ tcl_target_func( Jim_Interp *interp, Jim_GetOpt_NvpUnknown( &goi, target_options, 0 ); return e; } - // Assume blank result + /* Assume blank result */ Jim_SetEmptyResult( goi.interp ); switch( n->value ){ @@ -3554,7 +3657,7 @@ tcl_target_func( Jim_Interp *interp, break; case TS_CMD_EXAMINE: if( goi.argc ){ - Jim_WrongNumArgs( goi.interp, 0, argv, "[no parameters]"); + Jim_WrongNumArgs( goi.interp, 2, argv, "[no parameters]"); return JIM_ERR; } e = target->type->examine( target ); @@ -3565,7 +3668,7 @@ tcl_target_func( Jim_Interp *interp, return JIM_OK; case TS_CMD_POLL: if( goi.argc ){ - Jim_WrongNumArgs( goi.interp, 0, argv, "[no parameters]"); + Jim_WrongNumArgs( goi.interp, 2, argv, "[no parameters]"); return JIM_ERR; } if( !(target->type->examined) ){ @@ -3581,8 +3684,8 @@ tcl_target_func( Jim_Interp *interp, } break; case TS_CMD_RESET: - if( goi.argc != 1 ){ - Jim_WrongNumArgs( interp, 1, argv, "reset t|f|assert|deassert"); + if( goi.argc != 2 ){ + Jim_WrongNumArgs( interp, 2, argv, "t|f|assert|deassert BOOL"); return JIM_ERR; } e = Jim_GetOpt_Nvp( &goi, nvp_assert, &n ); @@ -3590,10 +3693,17 @@ tcl_target_func( Jim_Interp *interp, Jim_GetOpt_NvpUnknown( &goi, nvp_assert, 1 ); return e; } - // When this happens - all workareas are invalid. + /* the halt or not param */ + e = Jim_GetOpt_Wide( &goi, &a); + if( e != JIM_OK ){ + return e; + } + /* determine if we should halt or not. */ + target->reset_halt = !!a; + /* When this happens - all workareas are invalid. */ target_free_all_working_areas_restore(target, 0); - // do the assert + /* do the assert */ if( n->value == NVP_ASSERT ){ target->type->assert_reset( target ); } else { @@ -3608,7 +3718,7 @@ tcl_target_func( Jim_Interp *interp, target->type->halt( target ); return JIM_OK; case TS_CMD_WAITSTATE: - // params: statename timeoutmsecs + /* params: statename timeoutmsecs */ if( goi.argc != 2 ){ Jim_SetResult_sprintf( goi.interp, "%s STATENAME TIMEOUTMSECS", n->name ); return JIM_ERR; @@ -3623,12 +3733,12 @@ tcl_target_func( Jim_Interp *interp, return e; } e = target_wait_state( target, n->value, a ); - if( e == ERROR_OK ){ + if( e != ERROR_OK ){ Jim_SetResult_sprintf( goi.interp, - "target: %s wait %s fails %d", + "target: %s wait %s fails (%d) %s", target->cmd_name, n->name, - target_strerror_safe(e) ); + e, target_strerror_safe(e) ); return JIM_ERR; } else { return JIM_OK; @@ -3663,15 +3773,24 @@ tcl_target_func( Jim_Interp *interp, Jim_SetResultString( goi.interp, Jim_Nvp_value2name_simple(nvp_target_state,target->state)->name,-1); return JIM_OK; + case TS_CMD_INVOKE_EVENT: + if( goi.argc != 1 ){ + Jim_SetResult_sprintf( goi.interp, "%s ?EVENTNAME?",n->name); + return JIM_ERR; + } + e = Jim_GetOpt_Nvp( &goi, nvp_target_event, &n ); + if( e != JIM_OK ){ + Jim_GetOpt_NvpUnknown( &goi, nvp_target_event, 1 ); + return e; + } + target_handle_event( target, n->value ); + return JIM_OK; } return JIM_ERR; } - -static int -target_create( Jim_GetOptInfo *goi ) +static int target_create( Jim_GetOptInfo *goi ) { - Jim_Obj *new_cmd; Jim_Cmd *cmd; const char *cp; @@ -3725,7 +3844,6 @@ target_create( Jim_GetOptInfo *goi ) return JIM_ERR; } - /* Create it */ target = calloc(1,sizeof(target_t)); /* set target number */ @@ -3752,6 +3870,8 @@ target_create( Jim_GetOptInfo *goi ) target->next = NULL; target->arch_info = NULL; + target->display = 1; + /* initialize trace information */ target->trace_info = malloc(sizeof(trace_t)); target->trace_info->num_trace_points = 0; @@ -3770,6 +3890,13 @@ 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 ); @@ -3781,6 +3908,10 @@ target_create( Jim_GetOptInfo *goi ) target->endianness = TARGET_LITTLE_ENDIAN; } + /* incase variant is not set */ + if (!target->variant) + target->variant = strdup(""); + /* create the target specific commands */ if( target->type->register_commands ){ (*(target->type->register_commands))( cmd_ctx ); @@ -3810,17 +3941,14 @@ target_create( Jim_GetOptInfo *goi ) target, /* private data */ NULL ); /* no del proc */ - (*(target->type->target_create))( target, goi->interp ); return e; } -static int -jim_target( Jim_Interp *interp, int argc, Jim_Obj *const *argv ) +static int jim_target( Jim_Interp *interp, int argc, Jim_Obj *const *argv ) { int x,r,e; jim_wide w; struct command_context_s *cmd_ctx; - const char *cp; target_t *target; Jim_GetOptInfo goi; enum tcmd { @@ -3835,11 +3963,11 @@ jim_target( Jim_Interp *interp, int argc, Jim_Obj *const *argv ) const char *target_cmds[] = { "create", "types", "names", "current", "number", "count", - NULL // terminate + NULL /* terminate */ }; LOG_DEBUG("Target command params:"); - LOG_DEBUG(Jim_Debug_ArgvString( interp, argc, argv) ); + LOG_DEBUG("%s", Jim_Debug_ArgvString(interp, argc, argv)); cmd_ctx = Jim_GetAssocData( interp, "context" ); @@ -3850,85 +3978,7 @@ jim_target( Jim_Interp *interp, int argc, Jim_Obj *const *argv ) return JIM_ERR; } - /* is this old syntax? */ - /* To determine: We have to peek at argv[0]*/ - cp = Jim_GetString( goi.argv[0], NULL ); - for( x = 0 ; target_types[x] ; x++ ){ - if( 0 == strcmp(cp,target_types[x]->name) ){ - break; - } - } - if( target_types[x] ){ - /* YES IT IS OLD SYNTAX */ - Jim_Obj *new_argv[10]; - int new_argc; - - /* target_old_syntax - * - * argv[0] typename (above) - * argv[1] endian - * argv[2] reset method, deprecated/ignored - * argv[3] = old param - * argv[4] = old param - * - * We will combine all "old params" into a single param. - * Then later, split them again. - */ - if( argc < 4 ){ - Jim_WrongNumArgs( interp, 1, argv, "[OLDSYNTAX] ?TYPE? ?ENDIAN? ?RESET? ?old-params?"); - return JIM_ERR; - } - /* the command */ - new_argv[0] = argv[0]; - new_argv[1] = Jim_NewStringObj( interp, "create", -1 ); - { - char buf[ 30 ]; - sprintf( buf, "target%d", new_target_number() ); - new_argv[2] = Jim_NewStringObj( interp, buf , -1 ); - } - new_argv[3] = goi.argv[0]; /* typename */ - new_argv[4] = Jim_NewStringObj( interp, "-endian", -1 ); - new_argv[5] = goi.argv[1]; - new_argv[6] = Jim_NewStringObj( interp, "-chain-position", -1 ); - new_argv[7] = goi.argv[2]; - new_argv[8] = Jim_NewStringObj( interp, "-variant", -1 ); - new_argv[9] = goi.argv[3]; - new_argc = 10; - /* - * new arg syntax: - * argv[0] = command - * argv[1] = create - * argv[2] = cmdname - * argv[3] = typename - * argv[4] = **FIRST** "configure" option. - * - * Here, we make them: - * - * argv[4] = -endian - * argv[5] = little - * argv[6] = -position - * argv[7] = NUMBER - * argv[8] = -variant - * argv[9] = "somestring" - */ - - /* don't let these be released */ - for( x = 0 ; x < new_argc ; x++ ){ - Jim_IncrRefCount( new_argv[x]); - } - /* call our self */ - LOG_DEBUG("Target OLD SYNTAX - converted to new syntax"); - - r = jim_target( goi.interp, new_argc, new_argv ); - - /* release? these items */ - for( x = 0 ; x < new_argc ; x++ ){ - Jim_DecrRefCount( interp, new_argv[x] ); - } - return r; - } - - //Jim_GetOpt_Debug( &goi ); + /* Jim_GetOpt_Debug( &goi ); */ r = Jim_GetOpt_Enum( &goi, target_cmds, &x ); if( r != JIM_OK ){ return r; @@ -4006,13 +4056,200 @@ jim_target( Jim_Interp *interp, int argc, Jim_Obj *const *argv ) Jim_NewIntObj( goi.interp, max_target_number())); return JIM_OK; } + + return JIM_ERR; } +struct FastLoad +{ + u32 address; + u8 *data; + int length; -/* - * Local Variables: *** - * c-basic-offset: 4 *** - * tab-width: 4 *** - * End: *** - */ +}; + +static int fastload_num; +static struct FastLoad *fastload; + +static void free_fastload(void) +{ + if (fastload!=NULL) + { + int i; + for (i=0; i 5)) + { + return ERROR_COMMAND_SYNTAX_ERROR; + } + + /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */ + if (argc >= 2) + { + image.base_address_set = 1; + image.base_address = strtoul(args[1], NULL, 0); + } + else + { + image.base_address_set = 0; + } + + + image.start_address_set = 0; + + if (argc>=4) + { + min_address=strtoul(args[3], NULL, 0); + } + if (argc>=5) + { + max_address=strtoul(args[4], NULL, 0)+min_address; + } + + if (min_address>max_address) + { + return ERROR_COMMAND_SYNTAX_ERROR; + } + + duration_start_measure(&duration); + + if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK) + { + return ERROR_OK; + } + + image_size = 0x0; + retval = ERROR_OK; + fastload_num=image.num_sections; + fastload=(struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections); + if (fastload==NULL) + { + image_close(&image); + return ERROR_FAIL; + } + memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections); + for (i = 0; i < image.num_sections; i++) + { + buffer = malloc(image.sections[i].size); + if (buffer == NULL) + { + command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size); + break; + } + + if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK) + { + free(buffer); + break; + } + + u32 offset=0; + u32 length=buf_cnt; + + + /* DANGER!!! beware of unsigned comparision here!!! */ + + if ((image.sections[i].base_address+buf_cnt>=min_address)&& + (image.sections[i].base_addressmax_address) + { + length-=(image.sections[i].base_address+buf_cnt)-max_address; + } + + fastload[i].address=image.sections[i].base_address+offset; + fastload[i].data=malloc(length); + if (fastload[i].data==NULL) + { + free(buffer); + break; + } + memcpy(fastload[i].data, buffer+offset, length); + fastload[i].length=length; + + image_size += length; + command_print(cmd_ctx, "%u byte written at address 0x%8.8x", length, image.sections[i].base_address+offset); + } + + free(buffer); + } + + duration_stop_measure(&duration, &duration_text); + if (retval==ERROR_OK) + { + command_print(cmd_ctx, "Loaded %u bytes in %s", image_size, duration_text); + command_print(cmd_ctx, "NB!!! image has not been loaded to target, issue a subsequent 'fast_load' to do so."); + } + free(duration_text); + + image_close(&image); + + if (retval!=ERROR_OK) + { + free_fastload(); + } + + return retval; +} + +static int handle_fast_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) +{ + if (argc>0) + return ERROR_COMMAND_SYNTAX_ERROR; + if (fastload==NULL) + { + LOG_ERROR("No image in memory"); + return ERROR_FAIL; + } + int i; + int ms=timeval_ms(); + int size=0; + int retval=ERROR_OK; + for (i=0; i