CRC8 in FCB #80766
Replies: 4 comments 7 replies
-
OK, looks like this is already possible: #if IS_ENABLED(CONFIG_FCB_ALLOW_FIXED_ENDMARKER)
/* Given the offset in flash sector, calculate the FCB entry data offset and size, and set
* the fixed endmarker.
*/
static int
fcb_elem_endmarker_fixed(struct fcb *_fcb, struct fcb_entry *loc, uint8_t *em)
{
uint8_t tmp_str[2];
int cnt;
uint16_t len;
int rc;
if (loc->fe_elem_off + 2 > loc->fe_sector->fs_size) {
return -ENOTSUP;
}
rc = fcb_flash_read(_fcb, loc->fe_sector, loc->fe_elem_off, tmp_str, 2);
if (rc) {
return -EIO;
}
cnt = fcb_get_len(_fcb, tmp_str, &len);
if (cnt < 0) {
return cnt;
}
loc->fe_data_off = loc->fe_elem_off + fcb_len_in_flash(_fcb, cnt);
loc->fe_data_len = len;
*em = FCB_FIXED_ENDMARKER;
return 0;
}
#endif /* IS_ENABLED(CONFIG_FCB_ALLOW_FIXED_ENDMARKER) */
/* Given the offset in flash sector, calculate the FCB entry data offset and size, and calculate
* the expected endmarker.
*/
int
fcb_elem_endmarker(struct fcb *_fcb, struct fcb_entry *loc, uint8_t *em)
{
#if IS_ENABLED(CONFIG_FCB_ALLOW_FIXED_ENDMARKER)
if (_fcb->f_flags & FCB_FLAGS_CRC_DISABLED) {
return fcb_elem_endmarker_fixed(_fcb, loc, em);
}
#endif /* IS_ENABLED(CONFIG_FCB_ALLOW_FIXED_ENDMARKER) */
return fcb_elem_crc8(_fcb, loc, em);
} But I still think that the CRC8 usage and implementation should be investigated. |
Beta Was this translation helpful? Give feedback.
-
Also needed to set in the config: static struct fcb fcb = {
.f_magic = FCB_MAGIC,
.f_version = ADC_DATA_FCB_VERSION_0,
.f_sector_cnt = ARRAY_SIZE(sectors),
.f_sectors = sectors,
.f_flags = FCB_FLAGS_CRC_DISABLED,
}; The reads are WAY faster cause it's not doing 32 byte at a time CRC over a serial bus anymore. |
Beta Was this translation helpful? Give feedback.
-
Summary:
|
Beta Was this translation helpful? Give feedback.
-
If I understand correctly, the CRC8 is calculated
If the above is correct, then the CRC8 is not valid to detect errors in:
Seeking feedback. |
Beta Was this translation helpful? Give feedback.
-
I'm concerned about the use of CRC8 CCITT-8 in FCB records.
zephyr/subsys/fs/fcb/fcb_elem_info.c
Lines 15 to 67 in e646b7f
CCITT-8 has Hamming Distance = 3 up to 247 bits. That's HD=3 for only ~30 bytes of data.
https://users.ece.cmu.edu/~koopman/crc/crc8.html
I'd propose that the addition of this CRC is optional and off by default. Use of a CRC8 provides a false sense of integrity for entries of any significant length.
Additionally, I am concerned about the overhead of this implementation:
The operation reads 32 bytes at a time from (possibly slow, external, pm runtime managed) flash to the function call stack (the
tmp_str
). I am suggesting that this is unnecessary in all use cases of the API. The reason is that this CRC is being calculated at write time or at read time.Removing the CRC would alleviate these concerns and allow the user to add the CRC that is correct for their data. In my present use case, the records are 1624 bytes (though I'll probably shrunk them to align with sectors better - truly, I think that FCB should write records across sector boundaries!) I am already adding a reasonable CRC32 for the data - one that has good HD for the number of bits I intend to check.
FCB seems to be a promising API for a "flash circular buffer". Perhaps I should abandon it for NVS or other, but at first glance it seems an elegant solution for "storing N latest records".
Beta Was this translation helpful? Give feedback.
All reactions