1 /*
2 * Copyright (c) 2017, Arm Limited. All rights reserved.
3 * Copyright (c) 2022 Nordic Semiconductor ASA
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 *
7 * Origin of this file is the LittleFS lfs_util.h file adjusted for Zephyr
8 * specific needs.
9 */
10
11 #ifndef ZEPHYR_LFS_CONFIG_H
12 #define ZEPHYR_LFS_CONFIG_H
13
14 /* System includes */
15 #include <stdint.h>
16 #include <stdbool.h>
17 #include <string.h>
18 #include <inttypes.h>
19
20 #ifndef LFS_NO_MALLOC
21 #include <stdlib.h>
22 #endif
23 #ifndef LFS_NO_ASSERT
24 #include <zephyr/sys/__assert.h>
25 #endif
26
27 #if !defined(LFS_NO_DEBUG) || \
28 !defined(LFS_NO_WARN) || \
29 !defined(LFS_NO_ERROR) || \
30 defined(LFS_YES_TRACE)
31
32 #include <zephyr/logging/log.h>
33
34 #ifdef LFS_LOG_REGISTER
35 LOG_MODULE_REGISTER(littlefs, CONFIG_FS_LOG_LEVEL);
36 #endif
37
38 #include <stdio.h>
39 #endif
40
41 #ifdef __cplusplus
42 extern "C"
43 {
44 #endif
45
46 #ifdef CONFIG_FS_LITTLEFS_DISK_VERSION
47 #define LFS_MULTIVERSION
48 #endif
49
50 /* Logging functions when using LittleFS with Zephyr. */
51 #ifndef LFS_TRACE
52 #ifdef LFS_YES_TRACE
53 #define LFS_TRACE(fmt, ...) LOG_DBG("%s:%d:trace: " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
54 #else
55 #define LFS_TRACE(...)
56 #endif
57 #endif
58
59 #ifndef LFS_DEBUG
60 #define LFS_DEBUG(fmt, ...) LOG_DBG("%s:%d: " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
61 #endif
62
63 #ifndef LFS_WARN
64 #define LFS_WARN(fmt, ...) LOG_WRN("%s:%d: " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
65 #endif
66
67 #ifndef LFS_ERROR
68 #define LFS_ERROR(fmt, ...) LOG_ERR("%s:%d: " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
69 #endif
70
71 /* Runtime assertions */
72 #ifndef LFS_ASSERT
73 #define LFS_ASSERT(test) __ASSERT_NO_MSG(test)
74 #endif
75
76
77 /* Builtin functions, these may be replaced by more efficient */
78 /* toolchain-specific implementations. LFS_NO_INTRINSICS falls back to a more */
79 /* expensive basic C implementation for debugging purposes */
80
81 /* Min/max functions for unsigned 32-bit numbers */
lfs_max(uint32_t a,uint32_t b)82 static inline uint32_t lfs_max(uint32_t a, uint32_t b)
83 {
84 return (a > b) ? a : b;
85 }
86
lfs_min(uint32_t a,uint32_t b)87 static inline uint32_t lfs_min(uint32_t a, uint32_t b)
88 {
89 return (a < b) ? a : b;
90 }
91
92 /* Align to nearest multiple of a size */
lfs_aligndown(uint32_t a,uint32_t alignment)93 static inline uint32_t lfs_aligndown(uint32_t a, uint32_t alignment)
94 {
95 return a - (a % alignment);
96 }
97
lfs_alignup(uint32_t a,uint32_t alignment)98 static inline uint32_t lfs_alignup(uint32_t a, uint32_t alignment)
99 {
100 return lfs_aligndown(a + alignment-1, alignment);
101 }
102
103 /* Find the smallest power of 2 greater than or equal to a */
lfs_npw2(uint32_t a)104 static inline uint32_t lfs_npw2(uint32_t a)
105 {
106 #if !defined(LFS_NO_INTRINSICS) && (defined(__GNUC__) || defined(__CC_ARM))
107 return 32 - __builtin_clz(a-1);
108 #else
109 uint32_t r = 0;
110 uint32_t s;
111
112 a -= 1;
113 s = (a > 0xffff) << 4; a >>= s; r |= s;
114 s = (a > 0xff) << 3; a >>= s; r |= s;
115 s = (a > 0xf) << 2; a >>= s; r |= s;
116 s = (a > 0x3) << 1; a >>= s; r |= s;
117 return (r | (a >> 1)) + 1;
118 #endif
119 }
120
121 /* Count the number of trailing binary zeros in a */
122 /* lfs_ctz(0) may be undefined */
lfs_ctz(uint32_t a)123 static inline uint32_t lfs_ctz(uint32_t a)
124 {
125 #if !defined(LFS_NO_INTRINSICS) && defined(__GNUC__)
126 return __builtin_ctz(a);
127 #else
128 return lfs_npw2((a & -a) + 1) - 1;
129 #endif
130 }
131
132 /* Count the number of binary ones in a */
lfs_popc(uint32_t a)133 static inline uint32_t lfs_popc(uint32_t a)
134 {
135 #if !defined(LFS_NO_INTRINSICS) && (defined(__GNUC__) || defined(__CC_ARM))
136 return __builtin_popcount(a);
137 #else
138 a = a - ((a >> 1) & 0x55555555);
139 a = (a & 0x33333333) + ((a >> 2) & 0x33333333);
140 return (((a + (a >> 4)) & 0xf0f0f0f) * 0x1010101) >> 24;
141 #endif
142 }
143
144 /* Find the sequence comparison of a and b, this is the distance */
145 /* between a and b ignoring overflow */
lfs_scmp(uint32_t a,uint32_t b)146 static inline int lfs_scmp(uint32_t a, uint32_t b)
147 {
148 return (int)(unsigned int)(a - b);
149 }
150
151 /* Convert between 32-bit little-endian and native order */
lfs_fromle32(uint32_t a)152 static inline uint32_t lfs_fromle32(uint32_t a)
153 {
154 #if defined(CONFIG_LITTLE_ENDIAN)
155 return a;
156 #elif !defined(LFS_NO_INTRINSICS)
157 return __builtin_bswap32(a);
158 #else
159 return (((uint8_t *)&a)[0] << 0) |
160 (((uint8_t *)&a)[1] << 8) |
161 (((uint8_t *)&a)[2] << 16) |
162 (((uint8_t *)&a)[3] << 24);
163 #endif
164 }
165
lfs_tole32(uint32_t a)166 static inline uint32_t lfs_tole32(uint32_t a)
167 {
168 return lfs_fromle32(a);
169 }
170
171 /* Convert between 32-bit big-endian and native order */
lfs_frombe32(uint32_t a)172 static inline uint32_t lfs_frombe32(uint32_t a)
173 {
174 #if defined(CONFIG_BIG_ENDIAN)
175 return a;
176 #elif !defined(LFS_NO_INTRINSICS)
177 return __builtin_bswap32(a);
178 #else
179 return (((uint8_t *)&a)[0] << 24) |
180 (((uint8_t *)&a)[1] << 16) |
181 (((uint8_t *)&a)[2] << 8) |
182 (((uint8_t *)&a)[3] << 0);
183 #endif
184 }
185
lfs_tobe32(uint32_t a)186 static inline uint32_t lfs_tobe32(uint32_t a)
187 {
188 return lfs_frombe32(a);
189 }
190
191 /* Calculate CRC-32 with polynomial = 0x04c11db7 */
192 uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size);
193
194 /* Allocate memory, only used if buffers are not provided to littlefs */
195 /* Note, memory must be 64-bit aligned */
lfs_malloc(size_t size)196 static inline void *lfs_malloc(size_t size)
197 {
198 #ifndef LFS_NO_MALLOC
199 return malloc(size);
200 #else
201 (void)size;
202 return NULL;
203 #endif
204 }
205
206 /* Deallocate memory, only used if buffers are not provided to littlefs */
lfs_free(void * p)207 static inline void lfs_free(void *p)
208 {
209 #ifndef LFS_NO_MALLOC
210 free(p);
211 #else
212 (void)p;
213 #endif
214 }
215
216
217 #ifdef __cplusplus
218 } /* extern "C" */
219 #endif
220
221 #endif /* ZEPHYR_LFS_CONFIG_H */
222