13 #if EFI_CONFIGURATION_STORAGE
25 #if EFI_STORAGE_MFS == TRUE
31 #ifndef EFI_STORAGE_MFS_EXTERNAL
32 #define EFI_STORAGE_MFS_EXTERNAL FALSE
35 #ifndef EFI_FLASH_WRITE_THREAD
36 #define EFI_FLASH_WRITE_THREAD FALSE
40 #if (EFI_STORAGE_MFS_EXTERNAL == TRUE) && (EFI_FLASH_WRITE_THREAD == FALSE)
41 #error EFI_FLASH_WRITE_THREAD should be enabled if MFS is used for external flash
47 #if EFI_STORAGE_MFS == TRUE
52 #define EFI_MFS_SETTINGS_RECORD_ID 1
70 #if (EFI_FLASH_WRITE_THREAD == TRUE)
73 #if EFI_STORAGE_MFS == TRUE
81 chRegSetThreadName(
"flash writer");
101 efiPrintf(
"Scheduling configuration write");
104 #if (EFI_FLASH_WRITE_THREAD == TRUE)
117 #if (EFI_FLASH_WRITE_THREAD == TRUE)
138 template <
typename TStorage>
141 if (!storageAddress) {
147 criticalError(
"Failed to erase flash at 0x%08x: %d", storageAddress, err);
151 err =
intFlashWrite(storageAddress,
reinterpret_cast<const char*
>(&data),
sizeof(TStorage));
153 criticalError(
"Failed to write flash at 0x%08x: %d", storageAddress, err);
164 bool isSuccess =
false;
170 efiPrintf(
"Writing pending configuration... %d bytes",
sizeof(
persistentState));
182 #if EFI_STORAGE_MFS == TRUE
188 err = mfsWriteRecord(&
mfsd, EFI_MFS_SETTINGS_RECORD_ID,
191 if (err >= MFS_NO_ERROR)
195 #if EFI_STORAGE_INT_FLASH == TRUE
213 int elapsed_Ms = US2MS(NT2US(endNt - startNt));
215 #if EFI_STORAGE_MFS == TRUE
216 efiPrintf(
"FLASH_SUCCESS after %d mS MFS status %d", elapsed_Ms, err);
218 efiPrintf(
"FLASH_SUCCESS after %d mS", elapsed_Ms);
221 efiPrintf(
"Flashing failed");
259 #if EFI_STORAGE_INT_FLASH == TRUE
264 efiPrintf(
"readFromFlash %x", address);
284 #if EFI_STORAGE_MFS == TRUE
286 mfs_error_t err = mfsReadRecord(&
mfsd, EFI_MFS_SETTINGS_RECORD_ID,
289 if (err >= MFS_NO_ERROR) {
299 #if EFI_STORAGE_INT_FLASH == TRUE
315 efiPrintf(
"Reading second configuration copy");
343 efiPrintf(
"Need to reset flash to default due to CRC mismatch");
356 efiPrintf(
"Read valid configuration from flash!");
371 #if EFI_STORAGE_MFS == TRUE
376 err = mfsErase(&
mfsd);
379 int elapsed_Ms = US2MS(NT2US(endNt - startNt));
380 efiPrintf(
"erase done %d mS err %d", elapsed_Ms, err);
385 #if EFI_STORAGE_MFS == TRUE
390 mfsObjectInit(&
mfsd);
391 mfs_error_t err = mfsStart(&
mfsd, mfsConfig);
392 if (err < MFS_NO_ERROR) {
413 #if (EFI_FLASH_WRITE_THREAD == TRUE)
415 chThdCreateStatic(flashWriteStack,
sizeof(flashWriteStack), PRIO_FLASH_WRITE,
flashWriteThread,
nullptr);
417 efiPrintf(
"EFI_FLASH_WRITE_THREAD is enabled, but not used");
bool mcuCanFlashWhileRunning()
static void inhibitTimeouts(bool inhibit)
void addConsoleAction(const char *token, Void callback)
Register console action without parameters.
return FLASH_RETURN_SUCCESS
Console package entry point header.
void applyNonPersistentConfiguration()
void resetConfigurationExt(configuration_callback_t boardCallback, engine_type_e engineType)
persistent_config_container_s persistentState
bool warning(ObdCode code, const char *fmt,...)
int intFlashErase(flashaddr_t address, size_t size)
Erase the sectors containing the span of size bytes starting at address.
uintptr_t flashaddr_t
Address in the flash memory.
int intFlashWrite(flashaddr_t address, const char *buffer, size_t size)
Copy data from a buffer to the flash memory.
uintptr_t getFlashAddrFirstCopy(void)
int intFlashRead(flashaddr_t source, char *destination, size_t size)
Copy data from the flash memory to a destination.
uintptr_t getFlashAddrSecondCopy(void)
static void flashWriteThread(void *)
int eraseAndFlashCopy(flashaddr_t storageAddress, const TStorage &data)
static FlashState readOneConfigurationCopy(flashaddr_t address)
void setNeedToWriteConfiguration()
bool allowFlashWhileRunning()
chibios_rt::BinarySemaphore flashWriteSemaphore(true)
static bool needToWriteConfiguration
const MFSConfig * boardGetMfsConfig(void)
static void doResetConfiguration()
static uint32_t flashStateCrc(const persistent_config_container_s &state)
static THD_WORKING_AREA(flashWriteStack, 3 *UTILITY_THREAD_STACK_SIZE)
static FlashState validatePersistentState()
static void rewriteConfig()
void writeToFlashIfPending()
bool getNeedToWriteConfiguration()
static FlashState readConfiguration()
static void eraseConfig()
@ CUSTOM_ERR_FLASH_CRC_FAILED
engine_configuration_s * engineConfiguration
persistent_config_s persistentConfiguration
warning_message_t warning_message