From aa9c4ad6c08e29a85f9de27be06a6fee693ff538 Mon Sep 17 00:00:00 2001 From: Petr Ledvina Date: Tue, 9 Apr 2024 21:12:34 +0200 Subject: [PATCH] Fix config write (#13503) * Config - add error checking into config_streamer.c, CONFIG_STREAMER_BUFFER_SIZE does not match actually writen data in some cases * Config - fix buffer read ovefrflow CONFIG_IN_RAM and CONFIG_IN_SDCARD used 4 byte byffer, but 32 bytes got actually copied CONFIG_IN_MEMORY_MAPPED_FLASH used 8byte buffer, but but 32 bytes got actually copied * Config - add write functions into header --------- Co-authored-by: Petr Ledvina --- src/main/config/config_eeprom.h | 3 +++ src/main/config/config_streamer.c | 13 ++++++++++--- src/main/config/config_streamer.h | 3 +++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/main/config/config_eeprom.h b/src/main/config/config_eeprom.h index 9a1fc2d78b..e5bd08c8a5 100644 --- a/src/main/config/config_eeprom.h +++ b/src/main/config/config_eeprom.h @@ -32,3 +32,6 @@ void writeConfigToEEPROM(void); uint16_t getEEPROMConfigSize(void); size_t getEEPROMStorageSize(void); + +bool saveEEPROMToSDCard(void); +void saveEEPROMToMemoryMappedFlash(void); diff --git a/src/main/config/config_streamer.c b/src/main/config/config_streamer.c index dcb59813f1..c6dc2a21c8 100644 --- a/src/main/config/config_streamer.c +++ b/src/main/config/config_streamer.c @@ -25,6 +25,7 @@ #include "drivers/system.h" #include "drivers/flash.h" +#include "config/config_eeprom.h" #include "config/config_streamer.h" #if !defined(CONFIG_IN_FLASH) @@ -374,7 +375,8 @@ static int write_word(config_streamer_t *c, config_streamer_buffer_align_type_t uint64_t *dest_addr = (uint64_t *)c->address; uint64_t *src_addr = (uint64_t*)buffer; - uint8_t row_index = 4; + uint8_t row_index = CONFIG_STREAMER_BUFFER_SIZE / sizeof(uint64_t); + STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE % sizeof(uint64_t) == 0, "CONFIG_STREAMER_BUFFER_SIZE does not match written size"); /* copy the 256 bits flash word */ do { @@ -389,6 +391,7 @@ static int write_word(config_streamer_t *c, config_streamer_buffer_align_type_t return -1; } } + STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint32_t), "CONFIG_STREAMER_BUFFER_SIZE does not match written size"); const FLASH_Status status = FLASH_ProgramWord(c->address, *buffer); if (status != FLASH_COMPLETE) { return -2; @@ -415,6 +418,7 @@ static int write_word(config_streamer_t *c, config_streamer_buffer_align_type_t // For H7 // HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t DataAddress); + STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint32_t) * FLASH_NB_32BITWORD_IN_FLASHWORD, "CONFIG_STREAMER_BUFFER_SIZE does not match written size"); const HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, c->address, (uint64_t)(uint32_t)buffer); if (status != HAL_OK) { return -2; @@ -434,6 +438,7 @@ static int write_word(config_streamer_t *c, config_streamer_buffer_align_type_t } } + STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint32_t) * 1, "CONFIG_STREAMER_BUFFER_SIZE does not match written size"); // For F7 // HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data); const HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, c->address, (uint64_t)*buffer); @@ -455,6 +460,7 @@ static int write_word(config_streamer_t *c, config_streamer_buffer_align_type_t } } + STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint32_t) * 2, "CONFIG_STREAMER_BUFFER_SIZE does not match written size"); const HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, c->address, (uint64_t)*buffer); if (status != HAL_OK) { return -2; @@ -467,6 +473,7 @@ static int write_word(config_streamer_t *c, config_streamer_buffer_align_type_t } } + STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint32_t) * 1, "CONFIG_STREAMER_BUFFER_SIZE does not match written size"); const flash_status_type status = flash_word_program(c->address, (uint32_t)*buffer); if (status != FLASH_OPERATE_DONE) { return -2; @@ -478,6 +485,8 @@ static int write_word(config_streamer_t *c, config_streamer_buffer_align_type_t return -1; } } + + STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint32_t) * 1, "CONFIG_STREAMER_BUFFER_SIZE does not match written size"); const FLASH_Status status = FLASH_ProgramWord(c->address, *buffer); if (status != FLASH_COMPLETE) { return -2; @@ -520,12 +529,10 @@ int config_streamer_finish(config_streamer_t *c) { if (c->unlocked) { #if defined(CONFIG_IN_SDCARD) - bool saveEEPROMToSDCard(void); // forward declaration to avoid circular dependency between config_streamer / config_eeprom saveEEPROMToSDCard(); #elif defined(CONFIG_IN_EXTERNAL_FLASH) flashFlush(); #elif defined(CONFIG_IN_MEMORY_MAPPED_FLASH) - void saveEEPROMToMemoryMappedFlash(void); // forward declaration to avoid circular dependency between config_streamer / config_eeprom saveEEPROMToMemoryMappedFlash(); #elif defined(CONFIG_IN_RAM) // NOP diff --git a/src/main/config/config_streamer.h b/src/main/config/config_streamer.h index 9868008998..ce2f7967b7 100644 --- a/src/main/config/config_streamer.h +++ b/src/main/config/config_streamer.h @@ -29,6 +29,9 @@ #if defined(CONFIG_IN_EXTERNAL_FLASH) || defined(CONFIG_IN_MEMORY_MAPPED_FLASH) #define CONFIG_STREAMER_BUFFER_SIZE 8 // Must not be greater than the smallest flash page size of all compiled-in flash devices. typedef uint32_t config_streamer_buffer_align_type_t; +#elif defined(CONFIG_IN_RAM) || defined(CONFIG_IN_SDCARD) +#define CONFIG_STREAMER_BUFFER_SIZE 32 +typedef uint64_t config_streamer_buffer_align_type_t; #elif defined(STM32H743xx) || defined(STM32H750xx) || defined(STM32H723xx) || defined(STM32H725xx) #define CONFIG_STREAMER_BUFFER_SIZE 32 // Flash word = 256-bits typedef uint64_t config_streamer_buffer_align_type_t;