1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-13 03:20:00 +03:00

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 <ledvinap@hp124.ekotip.cz>
This commit is contained in:
Petr Ledvina 2024-04-09 21:12:34 +02:00 committed by GitHub
parent 5457032838
commit aa9c4ad6c0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 16 additions and 3 deletions

View file

@ -32,3 +32,6 @@ void writeConfigToEEPROM(void);
uint16_t getEEPROMConfigSize(void);
size_t getEEPROMStorageSize(void);
bool saveEEPROMToSDCard(void);
void saveEEPROMToMemoryMappedFlash(void);

View file

@ -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

View file

@ -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;