47 #define PAIR(a, b) (((unsigned)(a) << 2U) | (unsigned)(b)) 52 #define RET_ON_ERROR(err) do { \ 53 mfs_error_t e = (err); \ 54 if (e != MFS_NO_ERROR) { \ 67 static const uint16_t crc16_table[256] = {
68 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
69 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
70 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
71 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
72 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
73 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
74 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
75 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
76 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
77 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
78 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
79 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
80 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
81 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
82 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
83 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
84 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
85 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
86 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
87 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
88 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
89 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
90 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
91 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
92 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
93 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
94 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
95 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
96 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
97 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
98 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
99 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
106 uint16_t crc16(uint16_t crc,
const uint8_t *data,
size_t n) {
109 crc = (crc << 8U) ^ crc16_table[(crc >> 8U) ^ (uint16_t)*data];
117 static void mfs_state_reset(
MFSDriver *mfsp) {
152 size_t n, uint8_t *rp) {
156 if (ferr != FLASH_NO_ERROR) {
157 mfsp->
state = MFS_ERROR;
158 return MFS_ERR_FLASH_FAILURE;
184 if (ferr != FLASH_NO_ERROR) {
185 mfsp->
state = MFS_ERROR;
186 return MFS_ERR_FLASH_FAILURE;
189 #if MFS_CFG_WRITE_VERIFY == TRUE 194 if (memcmp((
void *)mfsp->
buffer.data8, (
void *)wp, chunk)) {
195 mfsp->
state = MFS_ERROR;
196 return MFS_ERR_FLASH_FAILURE;
266 for (i = 0; i < 3; i++) {
269 if ((dhdrp->fields.
magic != MFS_HEADER_MAGIC) ||
270 (dhdrp->fields.
id < (uint16_t)1) ||
273 *sts = MFS_RECORD_GARBAGE;
276 #if MFS_CFG_STRONG_CHECKING == TRUE 283 *sts = MFS_RECORD_OK;
289 *sts = MFS_RECORD_ERASED;
305 if (bank == MFS_BANK_0) {
314 while (sector < end) {
318 if (ferr != FLASH_NO_ERROR) {
319 mfsp->
state = MFS_ERROR;
320 return MFS_ERR_FLASH_FAILURE;
323 if (ferr != FLASH_NO_ERROR) {
324 mfsp->
state = MFS_ERROR;
325 return MFS_ERR_FLASH_FAILURE;
328 if (ferr != FLASH_NO_ERROR) {
329 mfsp->
state = MFS_ERROR;
330 return MFS_ERR_FLASH_FAILURE;
351 if (bank == MFS_BANK_0) {
360 while (sector < end) {
364 if (ferr == FLASH_ERROR_VERIFY) {
365 return MFS_ERR_NOT_ERASED;
367 if (ferr != FLASH_NO_ERROR) {
368 mfsp->
state = MFS_ERROR;
369 return MFS_ERR_FLASH_FAILURE;
395 if (bank == MFS_BANK_0) {
402 bhdr.fields.
magic1 = MFS_BANK_MAGIC_1;
403 bhdr.fields.
magic2 = MFS_BANK_MAGIC_2;
406 bhdr.fields.
crc = crc16(0xFFFFU, bhdr.hdr8,
435 bool warning =
false;
437 start_offset = mfs_flash_get_bank_offset(mfsp, bank);
442 while (hdr_offset < end_offset) {
448 (
void *)&mfsp->
buffer.dhdr));
452 hdr_offset, end_offset, &sts));
453 if (sts == MFS_RECORD_ERASED) {
457 else if (sts == MFS_RECORD_OK) {
471 else if (sts == MFS_RECORD_CRC) {
482 hdr_offset = hdr_offset +
487 if (hdr_offset > end_offset) {
488 return MFS_ERR_INTERNAL;
495 *statep = MFS_BANK_PARTIAL;
498 *statep = MFS_BANK_OK;
530 *statep = MFS_BANK_GARBAGE;
536 (
void *)&mfsp->
buffer.bhdr));
539 for (i = 0; i < 4; i++) {
543 if ((mfsp->
buffer.bhdr.fields.
magic1 != MFS_BANK_MAGIC_1) ||
544 (mfsp->
buffer.bhdr.fields.
magic2 != MFS_BANK_MAGIC_2) ||
551 crc = crc16(0xFFFFU, mfsp->
buffer.bhdr.hdr8,
553 if (crc != mfsp->
buffer.bhdr.fields.
crc) {
557 *statep = MFS_BANK_OK;
566 if (err == MFS_NO_ERROR) {
567 *statep = MFS_BANK_ERASED;
595 mfs_state_reset(mfsp);
628 if (sbank == MFS_BANK_0) {
636 dest_offset = mfs_flash_get_bank_offset(mfsp, dbank) +
647 dest_offset += totsize;
676 uint32_t cnt0 = 0, cnt1 = 0;
677 bool warning =
false;
685 switch (PAIR(sts0, sts1)) {
687 case PAIR(MFS_BANK_ERASED, MFS_BANK_ERASED):
693 case PAIR(MFS_BANK_OK, MFS_BANK_OK):
709 case PAIR(MFS_BANK_GARBAGE, MFS_BANK_GARBAGE):
718 case PAIR(MFS_BANK_ERASED, MFS_BANK_OK):
723 case PAIR(MFS_BANK_OK, MFS_BANK_ERASED):
728 case PAIR(MFS_BANK_ERASED, MFS_BANK_GARBAGE):
736 case PAIR(MFS_BANK_GARBAGE, MFS_BANK_ERASED):
744 case PAIR(MFS_BANK_OK, MFS_BANK_GARBAGE):
751 case PAIR(MFS_BANK_GARBAGE, MFS_BANK_OK):
759 return MFS_ERR_INTERNAL;
766 if ((sts == MFS_BANK_ERASED) || (sts == MFS_BANK_GARBAGE)) {
767 return MFS_ERR_INTERNAL;
772 if (sts == MFS_BANK_PARTIAL) {
777 return warning ? MFS_WARN_REPAIR : MFS_NO_ERROR;
797 mfs_state_reset(mfsp);
804 if (err == MFS_ERR_INTERNAL) {
807 mfsp->
state = MFS_ERROR;
810 if (!MFS_IS_ERROR(err)) {
811 mfsp->
state = MFS_READY;
817 mfsp->
state = MFS_ERROR;
818 return MFS_ERR_FLASH_FAILURE;
836 mfsp->
state = MFS_STOP;
858 (mfsp->
state == MFS_ERROR),
"invalid state");
877 (mfsp->
state == MFS_ERROR),
"invalid state");
880 mfsp->
state = MFS_STOP;
900 if (mfsp->
state != MFS_READY) {
901 return MFS_ERR_INV_STATE;
932 size_t *np, uint8_t *buffer) {
937 (np != NULL) && (buffer != NULL));
939 if (mfsp->
state != MFS_READY) {
940 return MFS_ERR_INV_STATE;
945 return MFS_ERR_NOT_FOUND;
949 if (*np < mfsp->descriptors[
id - 1U].size) {
950 return MFS_ERR_INV_SIZE;
967 crc = crc16(0xFFFFU, buffer, *np);
968 if (crc != mfsp->
buffer.dhdr.fields.
crc) {
969 mfsp->
state = MFS_ERROR;
970 return MFS_ERR_FLASH_FAILURE;
997 size_t n,
const uint8_t *buffer) {
999 bool warning =
false;
1003 (n > 0U) && (buffer != NULL));
1005 if (mfsp->
state != MFS_READY) {
1006 return MFS_ERR_INV_STATE;
1016 return MFS_ERR_OUT_OF_MEM;
1020 free = (mfs_flash_get_bank_offset(mfsp, mfsp->
current_bank) +
1022 if (required > free) {
1031 mfsp->
buffer.dhdr.fields.
id = (uint16_t)id;
1033 mfsp->
buffer.dhdr.fields.
crc = crc16(0xFFFFU, buffer, n);
1046 mfsp->
buffer.dhdr.fields.
magic = (uint32_t)MFS_HEADER_MAGIC;
1065 return warning ? MFS_WARN_GC : MFS_NO_ERROR;
1086 bool warning =
false;
1091 if (mfsp->
state != MFS_READY) {
1092 return MFS_ERR_INV_STATE;
1097 return MFS_ERR_NOT_FOUND;
1104 return MFS_ERR_INTERNAL;
1108 free = (mfs_flash_get_bank_offset(mfsp, mfsp->
current_bank) +
1110 if (required > free) {
1119 mfsp->
buffer.dhdr.fields.
magic = (uint32_t)MFS_HEADER_MAGIC;
1120 mfsp->
buffer.dhdr.fields.
id = (uint16_t)
id;
1122 mfsp->
buffer.dhdr.fields.
crc = (uint16_t)0;
1135 return warning ? MFS_WARN_GC : MFS_NO_ERROR;
1157 if (mfsp->
state != MFS_READY) {
1158 return MFS_ERR_INV_STATE;
uint32_t flash_sector_t
Type of a flash sector number.
uint32_t erased
Erased value.
union MFSDriver::@2 buffer
Transient buffer.
mfs_record_descriptor_t descriptors[MFS_CFG_MAX_RECORDS]
Offsets of the most recent instance of the records.
mfs_state_t state
Driver state.
void mfsStop(MFSDriver *mfsp)
Deactivates a MFS driver.
#define flashProgram(ip, offset, n, pp)
Program operation.
static mfs_error_t mfs_garbage_collect(MFSDriver *mfsp)
Enforces a garbage collection.
#define flashVerifyErase(ip, sector)
Returns the erase state of a sector.
static mfs_error_t mfs_bank_verify_erase(MFSDriver *mfsp, mfs_bank_t bank)
Erases and verifies all sectors belonging to a bank.
Type of a MFS configuration structure.
static mfs_error_t mfs_bank_write_header(MFSDriver *mfsp, mfs_bank_t bank, uint32_t cnt)
Writes the validation header in a bank.
flash_offset_t used_space
Used space in the current bank without considering erased records.
static mfs_error_t mfs_bank_erase(MFSDriver *mfsp, mfs_bank_t bank)
Erases and verifies all sectors belonging to a bank.
mfs_error_t mfsReadRecord(MFSDriver *mfsp, mfs_id_t id, size_t *np, uint8_t *buffer)
Retrieves and reads a data record.
flash_offset_t bank_size
Banks size.
mfs_bank_state_t
Type of a bank state assessment.
#define MFS_CFG_BUFFER_SIZE
Size of the buffer used for data copying.
static mfs_error_t mfs_flash_copy(MFSDriver *mfsp, flash_offset_t doffset, flash_offset_t soffset, uint32_t n)
Flash copy.
static mfs_error_t mfs_flash_read(MFSDriver *mfsp, flash_offset_t offset, size_t n, uint8_t *rp)
Flash read.
static mfs_error_t mfs_try_mount(MFSDriver *mfsp)
Performs a flash partition mount attempt.
mfs_error_t mfsWriteRecord(MFSDriver *mfsp, mfs_id_t id, size_t n, const uint8_t *buffer)
Creates or updates a data record.
static mfs_error_t mfs_flash_write(MFSDriver *mfsp, flash_offset_t offset, size_t n, const uint8_t *wp)
Flash write.
mfs_error_t mfsStart(MFSDriver *mfsp, const MFSConfig *config)
Configures and activates a MFS driver.
mfs_bank_t
Type of a flash bank.
Managed Flash Storage module header.
#define MFS_CFG_MAX_RECORDS
Maximum number of indexed records in the managed storage.
flash_sector_t bank1_start
Base sector index for bank 1.
mfs_error_t
Type of an MFS error code.
#define flashRead(ip, offset, n, rp)
Read operation.
flash_error_t
Type of a flash error code.
#define osalDbgCheck(c)
Function parameters check.
flash_sector_t bank0_sectors
Number of sectors for bank 0.
flash_error_t flashWaitErase(BaseFlash *devp)
Waits until the current erase operation is finished.
#define flashStartEraseSector(ip, sector)
Starts an sector erase operation.
static mfs_error_t mfs_bank_get_state(MFSDriver *mfsp, mfs_bank_t bank, mfs_bank_state_t *statep, uint32_t *cntp)
Determines the state of a bank.
flash_sector_t bank1_sectors
Number of sectors for bank 1.
flash_offset_t next_offset
Pointer to the next free position in the current bank.
static mfs_error_t mfs_bank_scan_records(MFSDriver *mfsp, mfs_bank_t bank, mfs_bank_state_t *statep)
Scans blocks searching for records.
uint32_t flash_offset_t
Type of a flash offset.
mfs_error_t mfsErase(MFSDriver *mfsp)
Destroys the state of the managed storage by erasing the flash.
mfs_record_state_t
Type of a record state assessment.
#define RET_ON_ERROR(err)
Error check helper.
mfs_error_t mfsEraseRecord(MFSDriver *mfsp, mfs_id_t id)
Erases a data record.
mfs_error_t mfsPerformGarbageCollection(MFSDriver *mfsp)
Enforces a garbage collection operation.
void mfsObjectInit(MFSDriver *mfsp)
Initializes an instance.
mfs_bank_t current_bank
Bank currently in use.
uint32_t current_counter
Usage counter of the current bank.
mfs_error_t mfs_mount(MFSDriver *mfsp)
Configures and activates a MFS driver.
uint32_t mfs_id_t
Type of a record identifier.
#define osalDbgAssert(c, remark)
Condition assertion.
flash_offset_t flashGetSectorOffset(BaseFlash *devp, flash_sector_t sector)
Returns the offset of a sector.
flash_sector_t bank0_start
Base sector index for bank 0.
BaseFlash * flashp
Flash driver associated to this MFS instance.
static mfs_error_t mfs_record_check(MFSDriver *mfsp, mfs_data_header_t *dhdrp, flash_offset_t offset, flash_offset_t limit, mfs_record_state_t *sts)
Verifies integrity of a record.
#define MFS_CFG_MAX_REPAIR_ATTEMPTS
Maximum number of repair attempts on partition mount.
static mfs_error_t mfs_bank_mount(MFSDriver *mfsp, mfs_bank_t bank, mfs_bank_state_t *statep)
Selects a bank as current.
const MFSConfig * config
Current configuration data.