From ffd1ff4a17ac80903543fb53af6cc169e02a5692 Mon Sep 17 00:00:00 2001 From: rusefillc <48498823+rusefillc@users.noreply.github.com> Date: Wed, 1 Nov 2023 07:50:35 -0400 Subject: [PATCH] reducing code duplication - internal flash order of operations (#5672) * reducing code duplication * reducing code duplication --------- Co-authored-by: rusefillc --- firmware/controllers/flash_main.cpp | 49 +++++++++++------------------ 1 file changed, 19 insertions(+), 30 deletions(-) diff --git a/firmware/controllers/flash_main.cpp b/firmware/controllers/flash_main.cpp index 2ddac7a3dd..8326ce19ef 100644 --- a/firmware/controllers/flash_main.cpp +++ b/firmware/controllers/flash_main.cpp @@ -219,6 +219,23 @@ enum class FlashState { BlankChip, }; +static FlashState validatePersistentState() { + auto flashCrc = flashStateCrc(persistentState); + + if (flashCrc != persistentState.crc) { + // If the stored crc is all 1s, that probably means the flash is actually blank, not that the crc failed. + if (persistentState.crc == ((decltype(persistentState.crc))-1)) { + return FlashState::BlankChip; + } else { + return FlashState::CrcFailed; + } + } else if (persistentState.version != FLASH_DATA_VERSION || persistentState.size != sizeof(persistentState)) { + return FlashState::IncompatibleVersion; + } else { + return FlashState::Ok; + } +} + /** * Read single copy of rusEFI configuration from flash */ @@ -232,20 +249,7 @@ static FlashState readOneConfigurationCopy(flashaddr_t address) { intFlashRead(address, (char *) &persistentState, sizeof(persistentState)); - auto flashCrc = flashStateCrc(persistentState); - - if (flashCrc != persistentState.crc) { - // If the stored crc is all 1s, that probably means the flash is actually blank, not that the crc failed. - if (persistentState.crc == ((decltype(persistentState.crc))-1)) { - return FlashState::BlankChip; - } else { - return FlashState::CrcFailed; - } - } else if (persistentState.version != FLASH_DATA_VERSION || persistentState.size != sizeof(persistentState)) { - return FlashState::IncompatibleVersion; - } else { - return FlashState::Ok; - } + return validatePersistentState(); } /** @@ -264,22 +268,7 @@ static FlashState readConfiguration() { // readed size is not exactly the same if (settings_size != sizeof(persistentState)) return FlashState::IncompatibleVersion; - // claimed size or version is not the same - if (persistentState.version != FLASH_DATA_VERSION || persistentState.size != sizeof(persistentState)) - return FlashState::IncompatibleVersion; - - // now crc - auto flashCrc = flashStateCrc(persistentState); - - if (flashCrc != persistentState.crc) { - // If the stored crc is all 1s, that probably means the flash is actually blank, not that the crc failed. - if (persistentState.crc == ((decltype(persistentState.crc))-1)) { - return FlashState::BlankChip; - } else { - return FlashState::CrcFailed; - } - } - return FlashState::Ok; + return validatePersistentState(); } else { return FlashState::BlankChip; }