1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//! Initialization. Per lfs.c lfs_init, lfs_deinit.
use cratelfs_cache_zero;
use crate;
use crate;
/// Per lfs.c lfs_init (lines 4198-4369)
///
/// C:
/// ```c
/// static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
/// lfs->cfg = cfg;
/// lfs->block_count = cfg->block_count; // May be 0
/// int err = 0;
///
/// #ifdef LFS_MULTIVERSION
/// // this driver only supports minor version < current minor version
/// LFS_ASSERT(!lfs->cfg->disk_version || (
/// (0xffff & (lfs->cfg->disk_version >> 16))
/// == LFS_DISK_VERSION_MAJOR
/// && (0xffff & (lfs->cfg->disk_version >> 0))
/// <= LFS_DISK_VERSION_MINOR));
/// #endif
///
/// // check that bool is a truthy-preserving type
/// //
/// // note the most common reason for this failure is a before-c99 compiler,
/// // which littlefs currently does not support
/// LFS_ASSERT((bool)0x80000000);
///
/// // check that the required io functions are provided
/// LFS_ASSERT(lfs->cfg->read != NULL);
/// #ifndef LFS_READONLY
/// LFS_ASSERT(lfs->cfg->prog != NULL);
/// LFS_ASSERT(lfs->cfg->erase != NULL);
/// LFS_ASSERT(lfs->cfg->sync != NULL);
/// #endif
///
/// // validate that the lfs-cfg sizes were initiated properly before
/// // performing any arithmetic logics with them
/// LFS_ASSERT(lfs->cfg->read_size != 0);
/// LFS_ASSERT(lfs->cfg->prog_size != 0);
/// LFS_ASSERT(lfs->cfg->cache_size != 0);
///
/// // check that block size is a multiple of cache size is a multiple
/// // of prog and read sizes
/// LFS_ASSERT(lfs->cfg->cache_size % lfs->cfg->read_size == 0);
/// LFS_ASSERT(lfs->cfg->cache_size % lfs->cfg->prog_size == 0);
/// LFS_ASSERT(lfs->cfg->block_size % lfs->cfg->cache_size == 0);
///
/// // check that the block size is large enough to fit all ctz pointers
/// LFS_ASSERT(lfs->cfg->block_size >= 128);
/// // this is the exact calculation for all ctz pointers, if this fails
/// // and the simpler assert above does not, math must be broken
/// LFS_ASSERT(4*lfs_npw2(0xffffffff / (lfs->cfg->block_size-2*4))
/// <= lfs->cfg->block_size);
///
/// // block_cycles = 0 is no longer supported.
/// //
/// // block_cycles is the number of erase cycles before littlefs evicts
/// // metadata logs as a part of wear leveling. Suggested values are in the
/// // range of 100-1000, or set block_cycles to -1 to disable block-level
/// // wear-leveling.
/// LFS_ASSERT(lfs->cfg->block_cycles != 0);
///
/// // check that compact_thresh makes sense
/// //
/// // metadata can't be compacted below block_size/2, and metadata can't
/// // exceed a block_size
/// LFS_ASSERT(lfs->cfg->compact_thresh == 0
/// || lfs->cfg->compact_thresh >= lfs->cfg->block_size/2);
/// LFS_ASSERT(lfs->cfg->compact_thresh == (lfs_size_t)-1
/// || lfs->cfg->compact_thresh <= lfs->cfg->block_size);
///
/// // check that metadata_max is a multiple of read_size and prog_size,
/// // and a factor of the block_size
/// LFS_ASSERT(!lfs->cfg->metadata_max
/// || lfs->cfg->metadata_max % lfs->cfg->read_size == 0);
/// LFS_ASSERT(!lfs->cfg->metadata_max
/// || lfs->cfg->metadata_max % lfs->cfg->prog_size == 0);
/// LFS_ASSERT(!lfs->cfg->metadata_max
/// || lfs->cfg->block_size % lfs->cfg->metadata_max == 0);
///
/// // setup read cache
/// if (lfs->cfg->read_buffer) {
/// lfs->rcache.buffer = lfs->cfg->read_buffer;
/// } else {
/// lfs->rcache.buffer = lfs_malloc(lfs->cfg->cache_size);
/// if (!lfs->rcache.buffer) {
/// err = LFS_ERR_NOMEM;
/// goto cleanup;
/// }
/// }
///
/// // setup program cache
/// if (lfs->cfg->prog_buffer) {
/// lfs->pcache.buffer = lfs->cfg->prog_buffer;
/// } else {
/// lfs->pcache.buffer = lfs_malloc(lfs->cfg->cache_size);
/// if (!lfs->pcache.buffer) {
/// err = LFS_ERR_NOMEM;
/// goto cleanup;
/// }
/// }
///
/// // zero to avoid information leaks
/// lfs_cache_zero(lfs, &lfs->rcache);
/// lfs_cache_zero(lfs, &lfs->pcache);
///
/// // setup lookahead buffer, note mount finishes initializing this after
/// // we establish a decent pseudo-random seed
/// LFS_ASSERT(lfs->cfg->lookahead_size > 0);
/// if (lfs->cfg->lookahead_buffer) {
/// lfs->lookahead.buffer = lfs->cfg->lookahead_buffer;
/// } else {
/// lfs->lookahead.buffer = lfs_malloc(lfs->cfg->lookahead_size);
/// if (!lfs->lookahead.buffer) {
/// err = LFS_ERR_NOMEM;
/// goto cleanup;
/// }
/// }
///
/// // check that the size limits are sane
/// LFS_ASSERT(lfs->cfg->name_max <= LFS_NAME_MAX);
/// lfs->name_max = lfs->cfg->name_max;
/// if (!lfs->name_max) {
/// lfs->name_max = LFS_NAME_MAX;
/// }
///
/// LFS_ASSERT(lfs->cfg->file_max <= LFS_FILE_MAX);
/// lfs->file_max = lfs->cfg->file_max;
/// if (!lfs->file_max) {
/// lfs->file_max = LFS_FILE_MAX;
/// }
///
/// LFS_ASSERT(lfs->cfg->attr_max <= LFS_ATTR_MAX);
/// lfs->attr_max = lfs->cfg->attr_max;
/// if (!lfs->attr_max) {
/// lfs->attr_max = LFS_ATTR_MAX;
/// }
///
/// LFS_ASSERT(lfs->cfg->metadata_max <= lfs->cfg->block_size);
///
/// LFS_ASSERT(lfs->cfg->inline_max == (lfs_size_t)-1
/// || lfs->cfg->inline_max <= lfs->cfg->cache_size);
/// LFS_ASSERT(lfs->cfg->inline_max == (lfs_size_t)-1
/// || lfs->cfg->inline_max <= lfs->attr_max);
/// LFS_ASSERT(lfs->cfg->inline_max == (lfs_size_t)-1
/// || lfs->cfg->inline_max <= ((lfs->cfg->metadata_max)
/// ? lfs->cfg->metadata_max
/// : lfs->cfg->block_size)/8);
/// lfs->inline_max = lfs->cfg->inline_max;
/// if (lfs->inline_max == (lfs_size_t)-1) {
/// lfs->inline_max = 0;
/// } else if (lfs->inline_max == 0) {
/// lfs->inline_max = lfs_min(
/// lfs->cfg->cache_size,
/// lfs_min(
/// lfs->attr_max,
/// ((lfs->cfg->metadata_max)
/// ? lfs->cfg->metadata_max
/// : lfs->cfg->block_size)/8));
/// }
///
/// // setup default state
/// lfs->root[0] = LFS_BLOCK_NULL;
/// lfs->root[1] = LFS_BLOCK_NULL;
/// lfs->mlist = NULL;
/// lfs->seed = 0;
/// lfs->gdisk = (lfs_gstate_t){0};
/// lfs->gstate = (lfs_gstate_t){0};
/// lfs->gdelta = (lfs_gstate_t){0};
/// #ifdef LFS_MIGRATE
/// lfs->lfs1 = NULL;
/// #endif
///
/// return 0;
///
/// cleanup:
/// lfs_deinit(lfs);
/// return err;
/// }
/// ```
/// Per lfs.c lfs_deinit (lines 4371-4389)
///
/// C:
/// ```c
/// static int lfs_deinit(lfs_t *lfs) {
/// // free allocated memory
/// if (!lfs->cfg->read_buffer) {
/// lfs_free(lfs->rcache.buffer);
/// }
///
/// if (!lfs->cfg->prog_buffer) {
/// lfs_free(lfs->pcache.buffer);
/// }
///
/// if (!lfs->cfg->lookahead_buffer) {
/// lfs_free(lfs->lookahead.buffer);
/// }
///
/// return 0;
/// }
///
///
///
/// ```