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