reducing code duplication - internal flash order of operations (#5672)

* reducing code duplication

* reducing code duplication

---------

Co-authored-by: rusefillc <sdfsdfqsf2334234234>
This commit is contained in:
rusefillc 2023-11-01 07:50:35 -04:00 committed by GitHub
parent 3235b3da48
commit ffd1ff4a17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 30 deletions

View File

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