ChibiOS 21.11.5
hal_mfs.h
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2006-2026 Giovanni Di Sirio.
3
4 This file is part of ChibiOS.
5
6 ChibiOS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation version 3 of the License.
9
10 ChibiOS is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/**
20 * @file hal_mfs.h
21 * @brief Managed Flash Storage module header.
22 *
23 * @addtogroup HAL_MFS
24 * @{
25 */
26
27#ifndef HAL_MFS_H
28#define HAL_MFS_H
29
30#include "hal_flash.h"
31
32#if defined(MFS_USE_MFSCONF)
33#include "mfsconf.h"
34#endif
35
36/*===========================================================================*/
37/* Driver constants. */
38/*===========================================================================*/
39
40#define MFS_BANK_MAGIC_1 0xEC705ADEU
41#define MFS_BANK_MAGIC_2 0xF0339CC5U
42#define MFS_HEADER_MAGIC_1 0x5FAE45F0U
43#define MFS_HEADER_MAGIC_2 0xF045AE5FU
44
45/*===========================================================================*/
46/* Driver pre-compile time settings. */
47/*===========================================================================*/
48
49/**
50 * @name Configuration options
51 * @{
52 */
53/**
54 * @brief Maximum number of indexed records in the managed storage.
55 * @note Record indexes go from 1 to @p MFS_CFG_MAX_RECORDS.
56 */
57#if !defined(MFS_CFG_MAX_RECORDS) || defined(__DOXYGEN__)
58#define MFS_CFG_MAX_RECORDS 32
59#endif
60
61/**
62 * @brief Maximum record size in the managed storage.
63 */
64#if !defined(MFS_CFG_MAX_RECORD_SIZE) || defined(__DOXYGEN__)
65#define MFS_CFG_MAX_RECORD_SIZE 16384
66#endif
67
68/**
69 * @brief Maximum number of repair attempts on partition mount.
70 */
71#if !defined(MFS_CFG_MAX_REPAIR_ATTEMPTS) || defined(__DOXYGEN__)
72#define MFS_CFG_MAX_REPAIR_ATTEMPTS 3
73#endif
74
75/**
76 * @brief Verify written data.
77 */
78#if !defined(MFS_CFG_WRITE_VERIFY) || defined(__DOXYGEN__)
79#define MFS_CFG_WRITE_VERIFY TRUE
80#endif
81
82/**
83 * @brief Enables a stronger and slower check procedure on mount.
84 * @details Strong checking requires reading of the whole written data and
85 * this can be slow, normal checking only checks integrity of
86 * metadata, data errors would be detected on read.
87 */
88#if !defined(MFS_CFG_STRONG_CHECKING) || defined(__DOXYGEN__)
89#define MFS_CFG_STRONG_CHECKING TRUE
90#endif
91
92/**
93 * @brief Size of the buffer used for data copying.
94 * @note The buffer size must be a power of two and not smaller than
95 * 16 bytes.
96 * @note Larger buffers improve performance, buffers with size multiple
97 * of the flash program page size work better.
98 */
99#if !defined(MFS_CFG_BUFFER_SIZE) || defined(__DOXYGEN__)
100#define MFS_CFG_BUFFER_SIZE 32
101#endif
102
103/**
104 * @brief Enforced memory alignment.
105 * @details This value must be a power of two, it enforces a memory alignment
106 * for records in the flash array. This is required when alignment
107 * constraints exist, for example when using a DTR mode on OSPI
108 * devices.
109 */
110#if !defined(MFS_CFG_MEMORY_ALIGNMENT) || defined(__DOXYGEN__)
111#define MFS_CFG_MEMORY_ALIGNMENT 2
112#endif
113
114/**
115 * @brief Maximum number of objects writable in a single transaction.
116 */
117#if !defined(MFS_CFG_TRANSACTION_MAX) || defined(__DOXYGEN__)
118#define MFS_CFG_TRANSACTION_MAX 16
119#endif
120
121/**
122 * @brief Enables mutual exclusion on flash accesses.
123 * @details Mutual exclusion on flash is required when multiple threads are
124 * accessing different flash areas through multiple MFS instances
125 * or different modules.
126 * @note Requires exclusive access support in the associated flash driver.
127 */
128#if !defined(MFS_USE_FLASH_MUTUAL_EXCLUSION) || defined(__DOXYGEN__)
129#define MFS_USE_FLASH_MUTUAL_EXCLUSION FALSE
130#endif
131/** @} */
132
133/*===========================================================================*/
134/* Derived constants and error checks. */
135/*===========================================================================*/
136
137#if (MFS_CFG_MAX_RECORDS < 0) || (MFS_CFG_MAX_RECORDS > 65535)
138#error "invalid MFS_CFG_MAX_RECORDS value"
139#endif
140
141#if (MFS_CFG_MAX_REPAIR_ATTEMPTS < 1) || \
142 (MFS_CFG_MAX_REPAIR_ATTEMPTS > 10)
143#error "invalid MFS_MAX_REPAIR_ATTEMPTS value"
144#endif
145
146#if MFS_CFG_BUFFER_SIZE < 16
147#error "invalid MFS_CFG_BUFFER_SIZE value"
148#endif
149
150#if (MFS_CFG_BUFFER_SIZE & (MFS_CFG_BUFFER_SIZE - 1)) != 0
151#error "MFS_CFG_BUFFER_SIZE is not a power of two"
152#endif
153
154#if (MFS_CFG_MEMORY_ALIGNMENT < 1) || \
155 (MFS_CFG_MEMORY_ALIGNMENT > MFS_CFG_BUFFER_SIZE)
156#error "invalid MFS_CFG_MEMORY_ALIGNMENT value"
157#endif
158
159#if (MFS_CFG_MEMORY_ALIGNMENT & (MFS_CFG_MEMORY_ALIGNMENT - 1)) != 0
160#error "MFS_CFG_MEMORY_ALIGNMENT is not a power of two"
161#endif
162
163#if (MFS_CFG_TRANSACTION_MAX < 0) || \
164 (MFS_CFG_TRANSACTION_MAX > MFS_CFG_MAX_RECORDS)
165#error "invalid MFS_CFG_TRANSACTION_MAX value"
166#endif
167
168/*===========================================================================*/
169/* Driver data structures and types. */
170/*===========================================================================*/
171
172/**
173 * @brief Type of a flash bank.
174 */
175typedef enum {
178} mfs_bank_t;
179
180/**
181 * @brief Type of driver state machine states.
182 */
190
191/**
192 * @brief Type of an MFS error code.
193 * @note Errors are negative integers, informative warnings are positive
194 * integers.
195 */
210
211/**
212 * @brief Type of a bank state assessment.
213 */
219
220/**
221 * @brief Type of a record identifier.
222 */
223typedef uint32_t mfs_id_t;
224
225/**
226 * @brief Type of a bank header.
227 * @note The header resides in the first 16 bytes of a bank.
228 */
229typedef union {
230 struct {
231 /**
232 * @brief Bank magic 1.
233 */
234 uint32_t magic1;
235 /**
236 * @brief Bank magic 2.
237 */
238 uint32_t magic2;
239 /**
240 * @brief Usage counter of the bank.
241 * @details This value is increased each time a bank swap is performed. It
242 * indicates how much wearing the flash has already endured.
243 */
244 uint32_t counter;
245 /**
246 * @brief Reserved field.
247 */
248 uint16_t reserved1;
249 /**
250 * @brief Header CRC.
251 */
252 uint16_t crc;
253 } fields;
254 uint8_t hdr8[16];
255 uint32_t hdr32[4];
257
258/**
259 * @brief Type of a data block header.
260 * @details This structure is placed before each written data block.
261 */
262typedef union {
263 struct {
264 /**
265 * @brief Data header magic 1.
266 */
267 uint32_t magic1;
268 /**
269 * @brief Data header magic 2.
270 */
271 uint32_t magic2;
272 /**
273 * @brief Record identifier.
274 */
275 uint16_t id;
276 /**
277 * @brief Data CRC.
278 */
279 uint16_t crc;
280 /**
281 * @brief Data size.
282 * @note The next record is located at @p MFS_ALIGN_NEXT(size).
283 */
284 uint32_t size;
285 } fields;
286 uint8_t hdr8[16];
287 uint32_t hdr32[4];
289
290typedef struct {
291 /**
292 * @brief Offset of the record header.
293 */
295 /**
296 * @brief Record data size.
297 */
298 uint32_t size;
300
301/**
302 * @brief Type of a MFS configuration structure.
303 */
304typedef struct {
305 /**
306 * @brief Flash driver associated to this MFS instance.
307 */
309 /**
310 * @brief Erased value.
311 */
312 uint32_t erased;
313 /**
314 * @brief Banks size.
315 */
317 /**
318 * @brief Base sector index for bank 0.
319 */
321 /**
322 * @brief Number of sectors for bank 0.
323 * @note The total size of bank0 sectors must be greater or equal to
324 * @p bank_size.
325 */
327 /**
328 * @brief Base sector index for bank 1.
329 */
331 /**
332 * @brief Number of sectors for bank 1.
333 * @note The total size of bank1 sectors must be greater or equal to
334 * @p bank_size.
335 */
337} MFSConfig;
338
339/**
340 * @brief Type of a buffered write/erase operation within a transaction.
341 */
342typedef struct {
343 /**
344 * @brief Written header offset.
345 */
347 /**
348 * @brief Written data size.
349 */
350 size_t size;
351 /**
352 * @brief Record identifier.
353 */
356
357/**
358 * @brief Type of an non-cacheable MFS buffer.
359 */
367
368/**
369 * @brief Type of an MFS instance.
370 */
371typedef struct {
372 /**
373 * @brief Driver state.
374 */
376 /**
377 * @brief Current configuration data.
378 */
380 /**
381 * @brief Bank currently in use.
382 */
384 /**
385 * @brief Usage counter of the current bank.
386 */
388 /**
389 * @brief Pointer to the next free position in the current bank.
390 */
392 /**
393 * @brief Used space in the current bank without considering erased records.
394 */
396 /**
397 * @brief Offsets of the most recent instance of the records.
398 * @note Zero means that there is not a record with that id.
399 */
401#if (MFS_CFG_TRANSACTION_MAX > 0) || defined(__DOXYGEN__)
402 /**
403 * @brief Next write offset for current transaction.
404 */
406 /**
407 * @brief Maximum offset for the transaction.
408 */
410 /**
411 * @brief Number of buffered operations in current transaction.
412 */
413 uint32_t tr_nops;
414 /**
415 * @brief Buffered operations in current transaction.
416 */
418#endif
419 /**
420 * @brief Associated non-cacheable buffer.
421 */
423} MFSDriver;
424
425/*===========================================================================*/
426/* Driver macros. */
427/*===========================================================================*/
428
429/**
430 * @name Error codes handling macros
431 * @{
432 */
433#define MFS_IS_ERROR(err) ((err) < MFS_NO_ERROR)
434#define MFS_IS_WARNING(err) ((err) > MFS_NO_ERROR)
435/** @} */
436
437/**
438 * @name Alignment macros
439 * @{
440 */
441#define MFS_ALIGN_MASK ((uint32_t)MFS_CFG_MEMORY_ALIGNMENT - 1U)
442#define MFS_IS_ALIGNED(v) (((uint32_t)(v) & MFS_ALIGN_MASK) == 0U)
443#define MFS_ALIGN_PREV(v) ((uint32_t)(v) & ~MFS_ALIGN_MASK)
444#define MFS_ALIGN_NEXT(v) (MFS_ALIGN_PREV(((uint32_t)(v) - 1U)) + \
445 MFS_CFG_MEMORY_ALIGNMENT)
446/** @} */
447
448/*===========================================================================*/
449/* External declarations. */
450/*===========================================================================*/
451
452#ifdef __cplusplus
453extern "C" {
454#endif
455 void mfsObjectInit(MFSDriver *devp, mfs_nocache_buffer_t *ncbuf);
456 mfs_error_t mfsStart(MFSDriver *devp, const MFSConfig *config);
457 void mfsStop(MFSDriver *devp);
460 size_t *np, uint8_t *buffer);
462 size_t n, const uint8_t *buffer);
465#if MFS_CFG_TRANSACTION_MAX > 0
466 mfs_error_t mfsStartTransaction(MFSDriver *mfsp, size_t size);
469#endif /* MFS_CFG_TRANSACTION_MAX > 0 */
470#ifdef __cplusplus
471}
472#endif
473
474#endif /* HAL_MFS_H */
475
476/** @} */
uint32_t flash_sector_t
Type of a flash sector number.
Definition hal_flash.h:117
uint32_t flash_offset_t
Type of a flash offset.
Definition hal_flash.h:112
union mfs_nocache_buffer mfs_nocache_buffer_t
Type of an non-cacheable MFS buffer.
mfs_error_t mfsEraseRecord(MFSDriver *mfsp, mfs_id_t id)
Erases a data record.
Definition hal_mfs.c:1218
#define MFS_CFG_MAX_RECORDS
Maximum number of indexed records in the managed storage.
Definition hal_mfs.h:58
#define MFS_CFG_BUFFER_SIZE
Size of the buffer used for data copying.
Definition hal_mfs.h:100
mfs_error_t mfsReadRecord(MFSDriver *mfsp, mfs_id_t id, size_t *np, uint8_t *buffer)
Retrieves and reads a data record.
Definition hal_mfs.c:993
mfs_error_t
Type of an MFS error code.
Definition hal_mfs.h:196
mfs_state_t
Type of driver state machine states.
Definition hal_mfs.h:183
mfs_error_t mfsStart(MFSDriver *mfsp, const MFSConfig *config)
Configures and activates a MFS driver.
Definition hal_mfs.c:910
#define MFS_CFG_TRANSACTION_MAX
Maximum number of objects writable in a single transaction.
Definition hal_mfs.h:118
mfs_error_t mfsPerformGarbageCollection(MFSDriver *mfsp)
Enforces a garbage collection operation.
Definition hal_mfs.c:1344
void mfsObjectInit(MFSDriver *mfsp, mfs_nocache_buffer_t *ncbuf)
Initializes an instance.
Definition hal_mfs.c:884
mfs_error_t mfsWriteRecord(MFSDriver *mfsp, mfs_id_t id, size_t n, const uint8_t *buffer)
Creates or updates a data record.
Definition hal_mfs.c:1066
mfs_error_t mfsStartTransaction(MFSDriver *mfsp, size_t size)
Puts the driver in transaction mode.
Definition hal_mfs.c:1385
uint32_t mfs_id_t
Type of a record identifier.
Definition hal_mfs.h:223
mfs_error_t mfsRollbackTransaction(MFSDriver *mfsp)
A transaction is rolled back atomically.
Definition hal_mfs.c:1522
mfs_bank_t
Type of a flash bank.
Definition hal_mfs.h:175
mfs_error_t mfsCommitTransaction(MFSDriver *mfsp)
A transaction is committed and finalized atomically.
Definition hal_mfs.c:1441
void mfsStop(MFSDriver *mfsp)
Deactivates a MFS driver.
Definition hal_mfs.c:929
mfs_error_t mfsErase(MFSDriver *mfsp)
Destroys the state of the managed storage by erasing the flash.
Definition hal_mfs.c:955
mfs_bank_state_t
Type of a bank state assessment.
Definition hal_mfs.h:214
@ MFS_ERR_INV_SIZE
Definition hal_mfs.h:201
@ MFS_ERR_FLASH_FAILURE
Definition hal_mfs.h:207
@ MFS_ERR_NOT_ERASED
Definition hal_mfs.h:206
@ MFS_ERR_INTERNAL
Definition hal_mfs.h:208
@ MFS_ERR_TRANSACTION_SIZE
Definition hal_mfs.h:205
@ MFS_ERR_NOT_FOUND
Definition hal_mfs.h:202
@ MFS_WARN_GC
Definition hal_mfs.h:199
@ MFS_ERR_TRANSACTION_NUM
Definition hal_mfs.h:204
@ MFS_NO_ERROR
Definition hal_mfs.h:197
@ MFS_ERR_OUT_OF_MEM
Definition hal_mfs.h:203
@ MFS_WARN_REPAIR
Definition hal_mfs.h:198
@ MFS_ERR_INV_STATE
Definition hal_mfs.h:200
@ MFS_TRANSACTION
Definition hal_mfs.h:187
@ MFS_ERROR
Definition hal_mfs.h:188
@ MFS_UNINIT
Definition hal_mfs.h:184
@ MFS_STOP
Definition hal_mfs.h:185
@ MFS_READY
Definition hal_mfs.h:186
@ MFS_BANK_1
Definition hal_mfs.h:177
@ MFS_BANK_0
Definition hal_mfs.h:176
@ MFS_BANK_GARBAGE
Definition hal_mfs.h:217
@ MFS_BANK_ERASED
Definition hal_mfs.h:215
@ MFS_BANK_OK
Definition hal_mfs.h:216
Generic flash driver class header.
Base flash class.
Definition hal_flash.h:224
Type of a MFS configuration structure.
Definition hal_mfs.h:304
uint32_t erased
Erased value.
Definition hal_mfs.h:312
flash_sector_t bank1_start
Base sector index for bank 1.
Definition hal_mfs.h:330
flash_sector_t bank0_start
Base sector index for bank 0.
Definition hal_mfs.h:320
flash_sector_t bank0_sectors
Number of sectors for bank 0.
Definition hal_mfs.h:326
flash_offset_t bank_size
Banks size.
Definition hal_mfs.h:316
BaseFlash * flashp
Flash driver associated to this MFS instance.
Definition hal_mfs.h:308
flash_sector_t bank1_sectors
Number of sectors for bank 1.
Definition hal_mfs.h:336
Type of an MFS instance.
Definition hal_mfs.h:371
uint32_t current_counter
Usage counter of the current bank.
Definition hal_mfs.h:387
flash_offset_t next_offset
Pointer to the next free position in the current bank.
Definition hal_mfs.h:391
mfs_transaction_op_t tr_ops[MFS_CFG_TRANSACTION_MAX]
Buffered operations in current transaction.
Definition hal_mfs.h:417
mfs_bank_t current_bank
Bank currently in use.
Definition hal_mfs.h:383
mfs_record_descriptor_t descriptors[MFS_CFG_MAX_RECORDS]
Offsets of the most recent instance of the records.
Definition hal_mfs.h:400
flash_offset_t tr_limit_offset
Maximum offset for the transaction.
Definition hal_mfs.h:409
const MFSConfig * config
Current configuration data.
Definition hal_mfs.h:379
uint32_t tr_nops
Number of buffered operations in current transaction.
Definition hal_mfs.h:413
flash_offset_t tr_next_offset
Next write offset for current transaction.
Definition hal_mfs.h:405
mfs_nocache_buffer_t * ncbuf
Associated non-cacheable buffer.
Definition hal_mfs.h:422
flash_offset_t used_space
Used space in the current bank without considering erased records.
Definition hal_mfs.h:395
mfs_state_t state
Driver state.
Definition hal_mfs.h:375
flash_offset_t offset
Offset of the record header.
Definition hal_mfs.h:294
uint32_t size
Record data size.
Definition hal_mfs.h:298
Type of a buffered write/erase operation within a transaction.
Definition hal_mfs.h:342
flash_offset_t offset
Written header offset.
Definition hal_mfs.h:346
size_t size
Written data size.
Definition hal_mfs.h:350
mfs_id_t id
Record identifier.
Definition hal_mfs.h:354
Type of a bank header.
Definition hal_mfs.h:229
uint32_t hdr32[4]
Definition hal_mfs.h:255
uint16_t crc
Header CRC.
Definition hal_mfs.h:252
uint16_t reserved1
Reserved field.
Definition hal_mfs.h:248
uint32_t magic2
Bank magic 2.
Definition hal_mfs.h:238
uint32_t counter
Usage counter of the bank.
Definition hal_mfs.h:244
uint8_t hdr8[16]
Definition hal_mfs.h:254
uint32_t magic1
Bank magic 1.
Definition hal_mfs.h:234
Type of a data block header.
Definition hal_mfs.h:262
uint8_t hdr8[16]
Definition hal_mfs.h:286
uint16_t id
Record identifier.
Definition hal_mfs.h:275
uint32_t hdr32[4]
Definition hal_mfs.h:287
uint32_t magic1
Data header magic 1.
Definition hal_mfs.h:267
uint32_t magic2
Data header magic 2.
Definition hal_mfs.h:271
uint16_t crc
Data CRC.
Definition hal_mfs.h:279
uint32_t size
Data size.
Definition hal_mfs.h:284
Type of an non-cacheable MFS buffer.
Definition hal_mfs.h:360
mfs_bank_header_t bhdr
Definition hal_mfs.h:362
uint8_t data8[MFS_CFG_BUFFER_SIZE]
Definition hal_mfs.h:363
uint16_t data16[MFS_CFG_BUFFER_SIZE/sizeof(uint16_t)]
Definition hal_mfs.h:364
uint32_t data32[MFS_CFG_BUFFER_SIZE/sizeof(uint32_t)]
Definition hal_mfs.h:365
mfs_data_header_t dhdr
Definition hal_mfs.h:361