1 /* Copyright (c) 2024 BayLibre SAS
2  *
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * ZMS: Zephyr Memory Storage
6  */
7 
8 #ifndef __ZMS_PRIV_H_
9 #define __ZMS_PRIV_H_
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
15 /*
16  * MASKS AND SHIFT FOR ADDRESSES
17  * an address in zms is an uint64_t where:
18  *   high 4 bytes represent the sector number
19  *   low 4 bytes represent the offset in a sector
20  */
21 #define ADDR_SECT_MASK   GENMASK64(63, 32)
22 #define ADDR_SECT_SHIFT  32
23 #define ADDR_OFFS_MASK   GENMASK64(31, 0)
24 #define SECTOR_NUM(x)    FIELD_GET(ADDR_SECT_MASK, x)
25 #define SECTOR_OFFSET(x) FIELD_GET(ADDR_OFFS_MASK, x)
26 
27 #if defined(CONFIG_ZMS_CUSTOMIZE_BLOCK_SIZE)
28 #define ZMS_BLOCK_SIZE CONFIG_ZMS_CUSTOM_BLOCK_SIZE
29 #else
30 #define ZMS_BLOCK_SIZE 32
31 #endif
32 
33 #define ZMS_LOOKUP_CACHE_NO_ADDR GENMASK64(63, 0)
34 #define ZMS_HEAD_ID              GENMASK(31, 0)
35 
36 #define ZMS_VERSION_MASK        GENMASK(7, 0)
37 #define ZMS_GET_VERSION(x)      FIELD_GET(ZMS_VERSION_MASK, x)
38 #define ZMS_DEFAULT_VERSION     1
39 #define ZMS_MAGIC_NUMBER        0x42 /* murmur3a hash of "ZMS" (MSB) */
40 #define ZMS_MAGIC_NUMBER_MASK   GENMASK(15, 8)
41 #define ZMS_GET_MAGIC_NUMBER(x) FIELD_GET(ZMS_MAGIC_NUMBER_MASK, x)
42 #define ZMS_MIN_ATE_NUM         5
43 
44 #define ZMS_INVALID_SECTOR_NUM -1
45 #define ZMS_DATA_IN_ATE_SIZE   8
46 
47 struct zms_ate {
48 	uint8_t crc8;      /* crc8 check of the entry */
49 	uint8_t cycle_cnt; /* cycle counter for non erasable devices */
50 	uint16_t len;      /* data len within sector */
51 	uint32_t id;       /* data id */
52 	union {
53 		uint8_t data[8]; /* used to store small size data */
54 		struct {
55 			uint32_t offset; /* data offset within sector */
56 			union {
57 				uint32_t data_crc; /*
58 						    * crc for data: The data CRC is checked only
59 						    * when the whole data of the element is read.
60 						    * The data CRC is not checked for a partial
61 						    * read, as it is computed for the complete
62 						    * set of data.
63 						    */
64 				uint32_t metadata; /*
65 						    * Used to store metadata information
66 						    * such as storage version.
67 						    */
68 			};
69 		};
70 	};
71 } __packed;
72 
73 #ifdef __cplusplus
74 }
75 #endif
76 
77 #endif /* __ZMS_PRIV_H_ */
78