1 /*
2  * Copyright (c) 2019 Bolt Innovation Management, LLC
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_INCLUDE_FS_LITTLEFS_H_
8 #define ZEPHYR_INCLUDE_FS_LITTLEFS_H_
9 
10 #include <zephyr/types.h>
11 #include <zephyr/kernel.h>
12 #include <zephyr/storage/flash_map.h>
13 
14 #include <lfs.h>
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 /**
21  * @brief Get the major part of the littlefs disk version
22  *
23  * @param disk_version The disk version of littlefs partition
24  * @return The major part of the littlefs disk version.
25  */
26 #define FS_LITTLEFS_DISK_VERSION_MAJOR_GET(disk_version) FIELD_GET(GENMASK(31, 16), disk_version)
27 
28 /**
29  * @brief Get the minor part of the littlefs disk version
30  *
31  * @param disk_version The disk version of littlefs partition
32  * @return The minor part of the littlefs disk version.
33  */
34 #define FS_LITTLEFS_DISK_VERSION_MINOR_GET(disk_version) FIELD_GET(GENMASK(15, 0), disk_version)
35 
36 /** @brief Filesystem info structure for LittleFS mount */
37 struct fs_littlefs {
38 	/* Defaulted in driver, customizable before mount. */
39 	struct lfs_config cfg;
40 
41 	/* Must be cfg.cache_size */
42 	uint8_t *read_buffer;
43 
44 	/* Must be cfg.cache_size */
45 	uint8_t *prog_buffer;
46 
47 	/* Must be cfg.lookahead_size/4 elements, and
48 	 * cfg.lookahead_size must be a multiple of 8.
49 	 */
50 	uint32_t *lookahead_buffer[CONFIG_FS_LITTLEFS_LOOKAHEAD_SIZE / sizeof(uint32_t)];
51 
52 	/* These structures are filled automatically at mount. */
53 	struct lfs lfs;
54 	void *backend;
55 	struct k_mutex mutex;
56 };
57 
58 /** @brief Define a littlefs configuration with customized size
59  * characteristics.
60  *
61  * This defines static arrays required for caches, and initializes the
62  * littlefs configuration structure to use the provided values instead
63  * of the global Kconfig defaults.  A pointer to the named object must
64  * be stored in the @ref fs_mount_t.fs_data field of a @ref fs_mount_t object.
65  *
66  * To define an instance for the Kconfig defaults, use
67  * @ref FS_LITTLEFS_DECLARE_DEFAULT_CONFIG.
68  *
69  * To completely control file system configuration the application can
70  * directly define and initialize a @ref fs_littlefs
71  * object.  The application is responsible for ensuring the configured
72  * values are consistent with littlefs requirements.
73  *
74  * @note If you use a non-default configuration for cache size, you
75  * must also select @kconfig{CONFIG_FS_LITTLEFS_FC_HEAP_SIZE} to relax
76  * the size constraints on per-file cache allocations.
77  *
78  * @param name the name for the structure.  The defined object has
79  * file scope.
80  * @param alignment needed alignment for read/prog buffer for specific device
81  * @param read_sz see @kconfig{CONFIG_FS_LITTLEFS_READ_SIZE}
82  * @param prog_sz see @kconfig{CONFIG_FS_LITTLEFS_PROG_SIZE}
83  * @param cache_sz see @kconfig{CONFIG_FS_LITTLEFS_CACHE_SIZE}
84  * @param lookahead_sz see @kconfig{CONFIG_FS_LITTLEFS_LOOKAHEAD_SIZE}
85  */
86 #define FS_LITTLEFS_DECLARE_CUSTOM_CONFIG(name, alignment, read_sz, prog_sz, cache_sz,	  \
87 					  lookahead_sz)					  \
88 	static uint8_t __aligned(alignment) name ## _read_buffer[cache_sz];		  \
89 	static uint8_t __aligned(alignment) name ## _prog_buffer[cache_sz];		  \
90 	static uint32_t name ## _lookahead_buffer[(lookahead_sz) / sizeof(uint32_t)];	  \
91 	static struct fs_littlefs name = {						  \
92 		.cfg = {								  \
93 			.read_size = (read_sz),						  \
94 			.prog_size = (prog_sz),						  \
95 			.cache_size = (cache_sz),					  \
96 			.lookahead_size = (lookahead_sz),				  \
97 			.read_buffer = name ## _read_buffer,				  \
98 			.prog_buffer = name ## _prog_buffer,				  \
99 			.lookahead_buffer = name ## _lookahead_buffer,			  \
100 		},									  \
101 	}
102 
103 /** @brief Define a littlefs configuration with default characteristics.
104  *
105  * This defines static arrays and initializes the littlefs
106  * configuration structure to use the default size configuration
107  * provided by Kconfig.
108  *
109  * @param name the name for the structure.  The defined object has
110  * file scope.
111  */
112 #define FS_LITTLEFS_DECLARE_DEFAULT_CONFIG(name)			 \
113 	FS_LITTLEFS_DECLARE_CUSTOM_CONFIG(name,				 \
114 					  4,				 \
115 					  CONFIG_FS_LITTLEFS_READ_SIZE,	 \
116 					  CONFIG_FS_LITTLEFS_PROG_SIZE,	 \
117 					  CONFIG_FS_LITTLEFS_CACHE_SIZE, \
118 					  CONFIG_FS_LITTLEFS_LOOKAHEAD_SIZE)
119 
120 #ifdef __cplusplus
121 }
122 #endif
123 
124 #endif /* ZEPHYR_INCLUDE_FS_LITTLEFS_H_ */
125