From 7e709eab1cb6ac73dc6e5feae5b1b9387d6e91a5 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 2 Apr 2012 20:48:41 -0700 Subject: [PATCH] STM32L: Validate flash writes Read-back flash data and make sure it is byte-for-byte the same as the source data. Signed-off-by: Keith Packard --- src/flash/nor/stm32lx.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/flash/nor/stm32lx.c b/src/flash/nor/stm32lx.c index 17ac6f080..6c3063fc8 100644 --- a/src/flash/nor/stm32lx.c +++ b/src/flash/nor/stm32lx.c @@ -377,6 +377,12 @@ static int stm32lx_write(struct flash_bank *bank, uint8_t *buffer, uint32_t bytes_written = 0; int retval; + uint8_t *start = buffer; + uint32_t start_address = address; + uint32_t start_count = count; + uint8_t *validate; + uint32_t check; + if (bank->target->state != TARGET_HALTED) { LOG_ERROR("Target not halted"); return ERROR_TARGET_NOT_HALTED; @@ -451,7 +457,26 @@ static int stm32lx_write(struct flash_bank *bank, uint8_t *buffer, if (retval != ERROR_OK) return retval; - return ERROR_OK; + validate = malloc (start_count); + + retval = target_read_buffer(target, start_address, start_count, validate); + if (retval != ERROR_OK) { + free (validate); + return retval; + } + + for (check = 0; check < start_count; check++) { + if (validate[check] != start[check]) { + LOG_ERROR ("flash corrupted at 0x%08x (%02x != %02x)\n", + start_address + check, start[check], validate[check]); + retval = ERROR_FAIL; + break; + } + } + + free (validate); + + return retval; } static int stm32lx_probe(struct flash_bank *bank) -- 2.30.2