1 /*
2 * Copyright (c) 2017-2023 Nordic Semiconductor ASA
3 * Copyright (c) 2015 Runtime Inc
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #ifndef __FCB_PRIV_H_
9 #define __FCB_PRIV_H_
10
11 #include <stdint.h>
12
13 #include <zephyr/fs/fcb.h>
14
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
19 #define FCB_CRC_SZ sizeof(uint8_t)
20 #define FCB_TMP_BUF_SZ 32
21
22 #define FCB_ID_GT(a, b) (((int16_t)(a) - (int16_t)(b)) > 0)
23
24 #define MK32(val) ((((uint32_t)(val)) << 24) | \
25 (((uint32_t)((val) & 0xff)) << 16) | \
26 (((uint32_t)((val) & 0xff)) << 8) | \
27 (((uint32_t)((val) & 0xff))))
28
29
30 /* @brief Gets magic value in flash formatted version
31 *
32 * Magic, the fcb->f_magic, prior to being written to flash, is xored with
33 * binary inversion of fcb->f_erase_value to avoid it matching a 4 consecutive
34 * bytes of flash erase value, which is used to recognize end of records,
35 * by accident. Only the value of 0xFFFFFFFF will be always written as
36 * 4 bytes of erase value.
37 *
38 * @param fcb pointer to initialized fcb structure
39 *
40 * @return uin32_t formatted magic value
41 */
fcb_flash_magic(const struct fcb * fcbp)42 static inline uint32_t fcb_flash_magic(const struct fcb *fcbp)
43 {
44 const uint8_t ev = fcbp->f_erase_value;
45
46 return (fcbp->f_magic ^ ~MK32(ev));
47 }
48
49 struct fcb_disk_area {
50 uint32_t fd_magic;
51 uint8_t fd_ver;
52 uint8_t _pad;
53 uint16_t fd_id;
54 };
55
56 int fcb_put_len(const struct fcb *fcbp, uint8_t *buf, uint16_t len);
57 int fcb_get_len(const struct fcb *fcbp, uint8_t *buf, uint16_t *len);
58
fcb_len_in_flash(struct fcb * fcbp,uint16_t len)59 static inline int fcb_len_in_flash(struct fcb *fcbp, uint16_t len)
60 {
61 if (fcbp->f_align <= 1U) {
62 return len;
63 }
64 return (len + (fcbp->f_align - 1U)) & ~(fcbp->f_align - 1U);
65 }
66
67 const struct flash_area *fcb_open_flash(const struct fcb *fcbp);
68 uint8_t fcb_get_align(const struct fcb *fcbp);
69 int fcb_erase_sector(const struct fcb *fcbp, const struct flash_sector *sector);
70
71 int fcb_getnext_in_sector(struct fcb *fcbp, struct fcb_entry *loc);
72 struct flash_sector *fcb_getnext_sector(struct fcb *fcbp, struct flash_sector *sector);
73 int fcb_getnext_nolock(struct fcb *fcbp, struct fcb_entry *loc);
74
75 int fcb_elem_info(struct fcb *fcbp, struct fcb_entry *loc);
76 int fcb_elem_endmarker(struct fcb *fcbp, struct fcb_entry *loc, uint8_t *crc8p);
77
78 int fcb_sector_hdr_init(struct fcb *fcbp, struct flash_sector *sector, uint16_t id);
79 int fcb_sector_hdr_read(struct fcb *fcbp, struct flash_sector *sector, struct fcb_disk_area *fdap);
80
81 #ifdef __cplusplus
82 }
83 #endif
84
85 #endif /* __FCB_PRIV_H_ */
86