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