From: Xiangfu Liu <[email protected]>
Signed-off-by: Xiangfu Liu <[email protected]> --- Hi Mike here is the v2 of lockflash/unlockflash patches thanks again for review those patches. xiangfu urjtag/include/urjtag/error.h | 1 + urjtag/include/urjtag/flash.h | 2 + urjtag/src/cmd/Makefile.am | 1 + urjtag/src/cmd/cmd_lockflash.c | 88 ++++++++++++++++++++++++++++++++++++++++ urjtag/src/flash/amd.c | 11 +++++ urjtag/src/flash/amd_flash.c | 10 +++++ urjtag/src/flash/flash.c | 75 ++++++++++++++++++++++++++++++++++ urjtag/src/flash/intel.c | 45 ++++++++++++++++++++ urjtag/src/global/log-error.c | 1 + 9 files changed, 234 insertions(+), 0 deletions(-) create mode 100644 urjtag/src/cmd/cmd_lockflash.c diff --git a/urjtag/include/urjtag/error.h b/urjtag/include/urjtag/error.h index 062ab44..958362b 100644 --- a/urjtag/include/urjtag/error.h +++ b/urjtag/include/urjtag/error.h @@ -63,6 +63,7 @@ typedef enum URJ_ERROR URJ_ERROR_FLASH_DETECT, URJ_ERROR_FLASH_PROGRAM, URJ_ERROR_FLASH_ERASE, + URJ_ERROR_FLASH_LOCK, URJ_ERROR_FLASH_UNLOCK, URJ_ERROR_BSDL_VHDL, diff --git a/urjtag/include/urjtag/flash.h b/urjtag/include/urjtag/flash.h index 5a61bff..e0b751e 100644 --- a/urjtag/include/urjtag/flash.h +++ b/urjtag/include/urjtag/flash.h @@ -55,6 +55,8 @@ typedef struct /** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ int (*erase_block) (urj_flash_cfi_array_t *cfi_array, uint32_t adr); /** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ + int (*lock_block) (urj_flash_cfi_array_t *cfi_array, uint32_t adr); + /** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ int (*unlock_block) (urj_flash_cfi_array_t *cfi_array, uint32_t adr); /** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ int (*program) (urj_flash_cfi_array_t *cfi_array, uint32_t adr, diff --git a/urjtag/src/cmd/Makefile.am b/urjtag/src/cmd/Makefile.am index c480273..dc13aa9 100644 --- a/urjtag/src/cmd/Makefile.am +++ b/urjtag/src/cmd/Makefile.am @@ -59,6 +59,7 @@ all_cmd_files = \ cmd_writemem.c \ cmd_flashmem.c \ cmd_eraseflash.c \ + cmd_lockflash.c \ cmd_include.c \ cmd_addpart.c \ cmd_cmd.c \ diff --git a/urjtag/src/cmd/cmd_lockflash.c b/urjtag/src/cmd/cmd_lockflash.c new file mode 100644 index 0000000..829bd2f --- /dev/null +++ b/urjtag/src/cmd/cmd_lockflash.c @@ -0,0 +1,88 @@ +/* + * $Id$ + * + * Copyright (C) 2011 Xiangfu Liu <[email protected]> + * + * 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 (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + */ + +#include <sysdep.h> + +#include <stdio.h> +#include <stdint.h> +#include <string.h> + +#include <urjtag/error.h> +#include <urjtag/bus.h> +#include <urjtag/flash.h> + +#include <urjtag/cmd.h> + +#include "cmd.h" + +static int +cmd_lockflash_run (urj_chain_t *chain, char *params[]) +{ + long unsigned adr = 0; + long unsigned number = 0; + + if (urj_cmd_params (params) != 3) + { + urj_error_set (URJ_ERROR_SYNTAX, + "%s: #parameters should be %d, not %d", + params[0], 3, urj_cmd_params (params)); + return URJ_STATUS_FAIL; + } + + if (urj_cmd_test_cable (chain) != URJ_STATUS_OK) + return URJ_STATUS_FAIL; + if (!urj_bus) + { + urj_error_set (URJ_ERROR_ILLEGAL_STATE, _("Bus driver missing")); + return URJ_STATUS_FAIL; + } + if (urj_cmd_get_number (params[1], &adr) != URJ_STATUS_OK) + return URJ_STATUS_FAIL; + if (urj_cmd_get_number (params[2], &number) != URJ_STATUS_OK) + return URJ_STATUS_FAIL; + + return urj_flashlock (urj_bus, adr, number); +} + +static void +cmd_lockflash_help (void) +{ + urj_log (URJ_LOG_LEVEL_NORMAL, + _("Usage: %s ADDR BLOCKS\n" + "Lock flash memory from ADDR.\n" + "\n" + "ADDR target addres for erasing block\n" + "BLOCKS number of blocks to lock\n" + "\n" + "ADDR and BLOCKS could be in decimal or hexadecimal (prefixed with 0x) form.\n" + "\n" "Supported Flash Memories:\n"), + "lockflash"); + + urj_cmd_show_list (urj_flash_flash_drivers); +} + +const urj_cmd_t urj_cmd_lockflash = { + "lockflash", + N_("lock flash memory by number of blocks"), + cmd_lockflash_help, + cmd_lockflash_run +}; diff --git a/urjtag/src/flash/amd.c b/urjtag/src/flash/amd.c index 40e162d..dd95b55 100644 --- a/urjtag/src/flash/amd.c +++ b/urjtag/src/flash/amd.c @@ -470,6 +470,14 @@ amd_flash_unlock_block (urj_flash_cfi_array_t *cfi_array, uint32_t adr) } static int +amd_flash_lock_block (urj_flash_cfi_array_t *cfi_array, uint32_t adr) +{ + urj_log (URJ_LOG_LEVEL_NORMAL, "flash_lock_block 0x%08lX IGNORE\n", + (long unsigned) adr); + return URJ_STATUS_OK; +} + +static int amd_flash_program_single (urj_flash_cfi_array_t *cfi_array, uint32_t adr, uint32_t data) { @@ -655,6 +663,7 @@ const urj_flash_driver_t urj_flash_amd_32_flash_driver = { amd_flash_autodetect32, amd_flash_print_info, amd_flash_erase_block, + amd_flash_lock_block, amd_flash_unlock_block, amd_flash_program32, amd_flash_read_array, @@ -667,6 +676,7 @@ const urj_flash_driver_t urj_flash_amd_16_flash_driver = { amd_flash_autodetect16, amd_flash_print_info, amd_flash_erase_block, + amd_flash_lock_block, amd_flash_unlock_block, amd_flash_program, amd_flash_read_array, @@ -679,6 +689,7 @@ const urj_flash_driver_t urj_flash_amd_8_flash_driver = { amd_flash_autodetect8, amd_flash_print_info, amd_flash_erase_block, + amd_flash_lock_block, amd_flash_unlock_block, amd_flash_program, amd_flash_read_array, diff --git a/urjtag/src/flash/amd_flash.c b/urjtag/src/flash/amd_flash.c index b54fbc2..581ac77 100644 --- a/urjtag/src/flash/amd_flash.c +++ b/urjtag/src/flash/amd_flash.c @@ -411,6 +411,15 @@ amd_29xx040_unlock_block (urj_flash_cfi_array_t *cfi_array, return URJ_STATUS_OK; } +static int +amd_29xx040_lock_block (urj_flash_cfi_array_t *cfi_array, + uint32_t adr) +{ + urj_log (URJ_LOG_LEVEL_NORMAL, "flash_lock_block 0x%08lX IGNORE\n", + (long unsigned) adr); + return URJ_STATUS_OK; +} + const urj_flash_driver_t urj_flash_amd_29xx040_flash_driver = { N_("AMD Standard Command Set"), @@ -419,6 +428,7 @@ const urj_flash_driver_t urj_flash_amd_29xx040_flash_driver = { amd_29xx040_autodetect, amd_29xx040_print_info, amd_29xx040_erase_block, + amd_29xx040_lock_block, amd_29xx040_unlock_block, amd_29xx040_program, amd_29xx040_read_array, diff --git a/urjtag/src/flash/flash.c b/urjtag/src/flash/flash.c index 2500b0a..9c6089b 100644 --- a/urjtag/src/flash/flash.c +++ b/urjtag/src/flash/flash.c @@ -553,3 +553,78 @@ urj_flasherase (urj_bus_t *bus, uint32_t addr, uint32_t number) return status; } + +int +urj_flashlock (urj_bus_t *bus, uint32_t addr, uint32_t number) +{ + urj_flash_cfi_query_structure_t *cfi; + uint32_t i; + int status = URJ_STATUS_OK; + int bus_width; + int chip_width; + + set_flash_driver (); + if (!urj_flash_cfi_array || !flash_driver) + { + urj_error_set (URJ_ERROR_NOTFOUND, _("no flash driver found")); + return URJ_STATUS_FAIL; + } + cfi = &urj_flash_cfi_array->cfi_chips[0]->cfi; + + bus_width = urj_flash_cfi_array->bus_width; + chip_width = urj_flash_cfi_array->cfi_chips[0]->width; + + urj_log (URJ_LOG_LEVEL_NORMAL, + _("\nLocking %d Flash block%s from address 0x%lx\n"), number, + number > 1 ? "s" : "", (long unsigned) addr); + + for (i = 1; i <= number; i++) + { + int r; + int btr = 0; + int block_no = find_block (cfi, addr - urj_flash_cfi_array->address, + bus_width, chip_width, &btr); + + if (block_no < 0) + { + urj_error_set (URJ_ERROR_FLASH_LOCK, "Cannot find block"); + status = URJ_STATUS_FAIL; + break; + } + + urj_log (URJ_LOG_LEVEL_NORMAL, + _("(%d%% Completed) FLASH Block %d : locking ... "), + i * 100 / number, block_no); + r = flash_driver->lock_block (urj_flash_cfi_array, addr); + if (r == URJ_STATUS_OK) + { + if (i == number) + { + urj_log (URJ_LOG_LEVEL_NORMAL, "\r"); + urj_log (URJ_LOG_LEVEL_NORMAL, + _("(100%% Completed) FLASH Block %d : locking ... Ok.\n"), + block_no); + } + else + { + urj_log (URJ_LOG_LEVEL_NORMAL, _("Ok.")); + urj_log (URJ_LOG_LEVEL_NORMAL, "\r"); + urj_log (URJ_LOG_LEVEL_NORMAL, _("%78s"), ""); + urj_log (URJ_LOG_LEVEL_NORMAL, "\r"); + } + } + else + { + urj_log (URJ_LOG_LEVEL_NORMAL, _("ERROR.\n")); + status = r; + } + addr += btr; + } + + if (status == URJ_STATUS_OK) + urj_log (URJ_LOG_LEVEL_NORMAL, _("\nLocking Completed.\n")); + else + urj_log (URJ_LOG_LEVEL_NORMAL, _("\nLocking (partially) Failed.\n")); + + return status; +} diff --git a/urjtag/src/flash/intel.c b/urjtag/src/flash/intel.c index 6e13a43..8d9aa89 100644 --- a/urjtag/src/flash/intel.c +++ b/urjtag/src/flash/intel.c @@ -295,6 +295,39 @@ intel_flash_unlock_block (urj_flash_cfi_array_t *cfi_array, uint32_t adr) } static int +intel_flash_lock_block (urj_flash_cfi_array_t *cfi_array, uint32_t adr) +{ + uint16_t sr; + urj_bus_t *bus = cfi_array->bus; + + URJ_BUS_WRITE (bus, cfi_array->address, + CFI_INTEL_CMD_CLEAR_STATUS_REGISTER); + URJ_BUS_WRITE (bus, adr, CFI_INTEL_CMD_LOCK_SETUP); + URJ_BUS_WRITE (bus, adr, CFI_INTEL_CMD_LOCK_BLOCK); + + while (!((sr = URJ_BUS_READ (bus, cfi_array->address) & 0xFE) & CFI_INTEL_SR_READY)); /* TODO: add timeout */ + + if (sr != CFI_INTEL_SR_READY) + { + urj_error_set (URJ_ERROR_FLASH_LOCK, + _("unknown error while locking block")); + return URJ_STATUS_FAIL; + } + + URJ_BUS_WRITE (bus, adr + 0x02, CFI_INTEL_CMD_READ_IDENTIFIER); + + sr = URJ_BUS_READ (bus, cfi_array->address & 0x01); + if(!sr) + { + urj_error_set (URJ_ERROR_FLASH_LOCK, + _("locking block failed")); + return URJ_STATUS_FAIL; + } + + return URJ_STATUS_OK; +} + +static int intel_flash_program_single (urj_flash_cfi_array_t *cfi_array, uint32_t adr, uint32_t data) { @@ -468,6 +501,15 @@ intel_flash_unlock_block32 (urj_flash_cfi_array_t *cfi_array, } static int +intel_flash_lock_block32 (urj_flash_cfi_array_t *cfi_array, + uint32_t adr) +{ + urj_log (URJ_LOG_LEVEL_NORMAL, "flash_lock_block32 0x%08lX IGNORE\n", + (long unsigned) adr); + return URJ_STATUS_OK; +} + +static int intel_flash_program32_single (urj_flash_cfi_array_t *cfi_array, uint32_t adr, uint32_t data) { @@ -536,6 +578,7 @@ const urj_flash_driver_t urj_flash_intel_32_flash_driver = { intel_flash_autodetect32, intel_flash_print_info32, intel_flash_erase_block32, + intel_flash_lock_block32, intel_flash_unlock_block32, intel_flash_program32, intel_flash_readarray32, @@ -548,6 +591,7 @@ const urj_flash_driver_t urj_flash_intel_16_flash_driver = { intel_flash_autodetect, intel_flash_print_info, intel_flash_erase_block, + intel_flash_lock_block, intel_flash_unlock_block, intel_flash_program, intel_flash_readarray, @@ -560,6 +604,7 @@ const urj_flash_driver_t urj_flash_intel_8_flash_driver = { intel_flash_autodetect8, intel_flash_print_info, intel_flash_erase_block, + intel_flash_lock_block, intel_flash_unlock_block, intel_flash_program, intel_flash_readarray, diff --git a/urjtag/src/global/log-error.c b/urjtag/src/global/log-error.c index 71569cd..ccc6a65 100644 --- a/urjtag/src/global/log-error.c +++ b/urjtag/src/global/log-error.c @@ -190,6 +190,7 @@ urj_error_string (urj_error_t err) case URJ_ERROR_FLASH_DETECT: return "flash detect"; case URJ_ERROR_FLASH_PROGRAM: return "flash program"; case URJ_ERROR_FLASH_ERASE: return "flash erase"; + case URJ_ERROR_FLASH_LOCK: return "flash locking"; case URJ_ERROR_FLASH_UNLOCK: return "flash unlock"; case URJ_ERROR_BSDL_VHDL: return "vhdl subsystem"; -- 1.7.4.1 ------------------------------------------------------------------------------ uberSVN's rich system and user administration capabilities and model configuration take the hassle out of deploying and managing Subversion and the tools developers use with it. Learn more about uberSVN and get a free download at: http://p.sf.net/sfu/wandisco-dev2dev _______________________________________________ UrJTAG-development mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/urjtag-development
