flash storage uses 32 bit CRC (#3984)

* better flash logic and warning message

* comment

* s

* ssssss

* simplify logic

* use 32 bit crc

* s

* don't test things that don't exist
This commit is contained in:
Matthew Kennedy 2022-03-08 20:17:35 -08:00 committed by GitHub
parent 4137be13a5
commit 0937de1b84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 60 deletions

View File

@ -15,5 +15,5 @@ typedef struct {
int version;
int size;
persistent_config_s persistentConfiguration;
crc_t value;
uint32_t value;
} persistent_config_container_s;

View File

@ -71,8 +71,8 @@ const MFSConfig mfsd_nor_config = {
* should be in a different sector of flash since complete flash sectors are erased on write.
*/
static crc_t flashStateCrc(const persistent_config_container_s& state) {
return calc_crc((const crc_t*) &state.persistentConfiguration, sizeof(persistent_config_s));
static uint32_t flashStateCrc(const persistent_config_container_s& state) {
return crc32(&state.persistentConfiguration, sizeof(persistent_config_s));
}
#if EFI_FLASH_WRITE_THREAD
@ -229,7 +229,7 @@ static FlashState readOneConfigurationCopy(flashaddr_t address) {
if (flashCrc != persistentState.value) {
// If the stored crc is all 1s, that probably means the flash is actually blank, not that the crc failed.
if (persistentState.value == ((crc_t)-1)) {
if (persistentState.value == ((decltype(persistentState.value))-1)) {
return FlashState::BlankChip;
} else {
return FlashState::CrcFailed;
@ -302,9 +302,7 @@ void readFromFlash() {
case FlashState::CrcFailed:
warning(CUSTOM_ERR_FLASH_CRC_FAILED, "flash CRC failed");
efiPrintf("Need to reset flash to default due to CRC mismatch");
// todo: use [[fallthrough]]? jenkins has different version on Windows
resetConfigurationExt(DEFAULT_ENGINE_TYPE);
break;
[[fallthrough]];
case FlashState::BlankChip:
resetConfigurationExt(DEFAULT_ENGINE_TYPE);
break;

View File

@ -6,43 +6,6 @@
#include "crc.h"
#define WIDTH (8)
#define TOPBIT (1 << (WIDTH - 1))
#define POLYNOMIAL 0xD8
crc_t calc_crc(const crc_t message[], int nBytes) {
crc_t remainder = 0;
/*
* Perform modulo-2 division, a byte at a time.
*/
for (int byte = 0; byte < nBytes; ++byte) {
/*
* Bring the next byte into the remainder.
*/
remainder ^= (message[byte] << (WIDTH - 8));
/*
* Perform modulo-2 division, a bit at a time.
*/
for (unsigned char bit = 8; bit > 0; --bit) {
/*
* Try to divide the current data bit.
*/
if (remainder & TOPBIT) {
remainder = (remainder << 1) ^ POLYNOMIAL;
} else {
remainder = (remainder << 1);
}
}
}
/*
* The final remainder is the CRC result.
*/
return remainder;
}
static const uint32_t crc32_tab[] = { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
@ -95,6 +58,19 @@ uint32_t crc32(const void *buf, uint32_t size) {
return crc32inc(buf, 0, size);
}
uint32_t crc32inc(const void *buf, uint32_t crc, uint32_t size) {
const uint8_t *p;
p = buf;
crc = crc ^ 0xFFFFFFFF;
while (size--) {
crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
}
return crc ^ 0xFFFFFFFF;
}
/**
* http://www.sunshine2k.de/coding/javascript/crc/crc_js.html
* https://stackoverflow.com/questions/38639423/understanding-results-of-crc8-sae-j1850-normal-vs-zero
@ -116,15 +92,3 @@ uint8_t crc8(const uint8_t *data, uint8_t len) {
return crc;
}
uint32_t crc32inc(const void *buf, uint32_t crc, uint32_t size) {
const uint8_t *p;
p = buf;
crc = crc ^ 0xFFFFFFFF;
while (size--) {
crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
}
return crc ^ 0xFFFFFFFF;
}

View File

@ -9,13 +9,10 @@
#include "stdint.h"
typedef unsigned char crc_t;
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
crc_t calc_crc(const crc_t message[], int nBytes);
uint8_t crc8(const uint8_t * buf, uint8_t len);
uint32_t crc32(const void *buf, uint32_t size);
uint32_t crc32inc(const void *buf, uint32_t crc, uint32_t size);

View File

@ -48,7 +48,6 @@ TEST(util, crc) {
const char * A = "A";
ASSERT_EQ( 168, calc_crc((const crc_t *) A, 1)) << "crc8";
uint32_t c = crc32(A, 1);
printf("crc32(A)=%x\r\n", c);
assertEqualsM("crc32 1", 0xd3d99e8b, c);