1 /*
2  * Testing block device, wraps filebd and rambd while providing a bunch
3  * of hooks for testing littlefs in various conditions.
4  *
5  * Copyright (c) 2017, Arm Limited. All rights reserved.
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 #include "bd/lfs_testbd.h"
9 
10 #include <stdlib.h>
11 
12 
lfs_testbd_createcfg(const struct lfs_config * cfg,const char * path,const struct lfs_testbd_config * bdcfg)13 int lfs_testbd_createcfg(const struct lfs_config *cfg, const char *path,
14         const struct lfs_testbd_config *bdcfg) {
15     LFS_TESTBD_TRACE("lfs_testbd_createcfg(%p {.context=%p, "
16                 ".read=%p, .prog=%p, .erase=%p, .sync=%p, "
17                 ".read_size=%"PRIu32", .prog_size=%"PRIu32", "
18                 ".block_size=%"PRIu32", .block_count=%"PRIu32"}, "
19                 "\"%s\", "
20                 "%p {.erase_value=%"PRId32", .erase_cycles=%"PRIu32", "
21                 ".badblock_behavior=%"PRIu8", .power_cycles=%"PRIu32", "
22                 ".buffer=%p, .wear_buffer=%p})",
23             (void*)cfg, cfg->context,
24             (void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog,
25             (void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync,
26             cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count,
27             path, (void*)bdcfg, bdcfg->erase_value, bdcfg->erase_cycles,
28             bdcfg->badblock_behavior, bdcfg->power_cycles,
29             bdcfg->buffer, bdcfg->wear_buffer);
30     lfs_testbd_t *bd = cfg->context;
31     bd->cfg = bdcfg;
32 
33     // setup testing things
34     bd->persist = path;
35     bd->power_cycles = bd->cfg->power_cycles;
36 
37     if (bd->cfg->erase_cycles) {
38         if (bd->cfg->wear_buffer) {
39             bd->wear = bd->cfg->wear_buffer;
40         } else {
41             bd->wear = lfs_malloc(sizeof(lfs_testbd_wear_t)*cfg->block_count);
42             if (!bd->wear) {
43                 LFS_TESTBD_TRACE("lfs_testbd_createcfg -> %d", LFS_ERR_NOMEM);
44                 return LFS_ERR_NOMEM;
45             }
46         }
47 
48         memset(bd->wear, 0, sizeof(lfs_testbd_wear_t) * cfg->block_count);
49     }
50 
51     // create underlying block device
52     if (bd->persist) {
53         bd->u.file.cfg = (struct lfs_filebd_config){
54             .erase_value = bd->cfg->erase_value,
55         };
56         int err = lfs_filebd_createcfg(cfg, path, &bd->u.file.cfg);
57         LFS_TESTBD_TRACE("lfs_testbd_createcfg -> %d", err);
58         return err;
59     } else {
60         bd->u.ram.cfg = (struct lfs_rambd_config){
61             .erase_value = bd->cfg->erase_value,
62             .buffer = bd->cfg->buffer,
63         };
64         int err = lfs_rambd_createcfg(cfg, &bd->u.ram.cfg);
65         LFS_TESTBD_TRACE("lfs_testbd_createcfg -> %d", err);
66         return err;
67     }
68 }
69 
lfs_testbd_create(const struct lfs_config * cfg,const char * path)70 int lfs_testbd_create(const struct lfs_config *cfg, const char *path) {
71     LFS_TESTBD_TRACE("lfs_testbd_create(%p {.context=%p, "
72                 ".read=%p, .prog=%p, .erase=%p, .sync=%p, "
73                 ".read_size=%"PRIu32", .prog_size=%"PRIu32", "
74                 ".block_size=%"PRIu32", .block_count=%"PRIu32"}, "
75                 "\"%s\")",
76             (void*)cfg, cfg->context,
77             (void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog,
78             (void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync,
79             cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count,
80             path);
81     static const struct lfs_testbd_config defaults = {.erase_value=-1};
82     int err = lfs_testbd_createcfg(cfg, path, &defaults);
83     LFS_TESTBD_TRACE("lfs_testbd_create -> %d", err);
84     return err;
85 }
86 
lfs_testbd_destroy(const struct lfs_config * cfg)87 int lfs_testbd_destroy(const struct lfs_config *cfg) {
88     LFS_TESTBD_TRACE("lfs_testbd_destroy(%p)", (void*)cfg);
89     lfs_testbd_t *bd = cfg->context;
90     if (bd->cfg->erase_cycles && !bd->cfg->wear_buffer) {
91         lfs_free(bd->wear);
92     }
93 
94     if (bd->persist) {
95         int err = lfs_filebd_destroy(cfg);
96         LFS_TESTBD_TRACE("lfs_testbd_destroy -> %d", err);
97         return err;
98     } else {
99         int err = lfs_rambd_destroy(cfg);
100         LFS_TESTBD_TRACE("lfs_testbd_destroy -> %d", err);
101         return err;
102     }
103 }
104 
105 /// Internal mapping to block devices ///
lfs_testbd_rawread(const struct lfs_config * cfg,lfs_block_t block,lfs_off_t off,void * buffer,lfs_size_t size)106 static int lfs_testbd_rawread(const struct lfs_config *cfg, lfs_block_t block,
107         lfs_off_t off, void *buffer, lfs_size_t size) {
108     lfs_testbd_t *bd = cfg->context;
109     if (bd->persist) {
110         return lfs_filebd_read(cfg, block, off, buffer, size);
111     } else {
112         return lfs_rambd_read(cfg, block, off, buffer, size);
113     }
114 }
115 
lfs_testbd_rawprog(const struct lfs_config * cfg,lfs_block_t block,lfs_off_t off,const void * buffer,lfs_size_t size)116 static int lfs_testbd_rawprog(const struct lfs_config *cfg, lfs_block_t block,
117         lfs_off_t off, const void *buffer, lfs_size_t size) {
118     lfs_testbd_t *bd = cfg->context;
119     if (bd->persist) {
120         return lfs_filebd_prog(cfg, block, off, buffer, size);
121     } else {
122         return lfs_rambd_prog(cfg, block, off, buffer, size);
123     }
124 }
125 
lfs_testbd_rawerase(const struct lfs_config * cfg,lfs_block_t block)126 static int lfs_testbd_rawerase(const struct lfs_config *cfg,
127         lfs_block_t block) {
128     lfs_testbd_t *bd = cfg->context;
129     if (bd->persist) {
130         return lfs_filebd_erase(cfg, block);
131     } else {
132         return lfs_rambd_erase(cfg, block);
133     }
134 }
135 
lfs_testbd_rawsync(const struct lfs_config * cfg)136 static int lfs_testbd_rawsync(const struct lfs_config *cfg) {
137     lfs_testbd_t *bd = cfg->context;
138     if (bd->persist) {
139         return lfs_filebd_sync(cfg);
140     } else {
141         return lfs_rambd_sync(cfg);
142     }
143 }
144 
145 /// block device API ///
lfs_testbd_read(const struct lfs_config * cfg,lfs_block_t block,lfs_off_t off,void * buffer,lfs_size_t size)146 int lfs_testbd_read(const struct lfs_config *cfg, lfs_block_t block,
147         lfs_off_t off, void *buffer, lfs_size_t size) {
148     LFS_TESTBD_TRACE("lfs_testbd_read(%p, "
149                 "0x%"PRIx32", %"PRIu32", %p, %"PRIu32")",
150             (void*)cfg, block, off, buffer, size);
151     lfs_testbd_t *bd = cfg->context;
152 
153     // check if read is valid
154     LFS_ASSERT(off  % cfg->read_size == 0);
155     LFS_ASSERT(size % cfg->read_size == 0);
156     LFS_ASSERT(block < cfg->block_count);
157 
158     // block bad?
159     if (bd->cfg->erase_cycles && bd->wear[block] >= bd->cfg->erase_cycles &&
160             bd->cfg->badblock_behavior == LFS_TESTBD_BADBLOCK_READERROR) {
161         LFS_TESTBD_TRACE("lfs_testbd_read -> %d", LFS_ERR_CORRUPT);
162         return LFS_ERR_CORRUPT;
163     }
164 
165     // read
166     int err = lfs_testbd_rawread(cfg, block, off, buffer, size);
167     LFS_TESTBD_TRACE("lfs_testbd_read -> %d", err);
168     return err;
169 }
170 
lfs_testbd_prog(const struct lfs_config * cfg,lfs_block_t block,lfs_off_t off,const void * buffer,lfs_size_t size)171 int lfs_testbd_prog(const struct lfs_config *cfg, lfs_block_t block,
172         lfs_off_t off, const void *buffer, lfs_size_t size) {
173     LFS_TESTBD_TRACE("lfs_testbd_prog(%p, "
174                 "0x%"PRIx32", %"PRIu32", %p, %"PRIu32")",
175             (void*)cfg, block, off, buffer, size);
176     lfs_testbd_t *bd = cfg->context;
177 
178     // check if write is valid
179     LFS_ASSERT(off  % cfg->prog_size == 0);
180     LFS_ASSERT(size % cfg->prog_size == 0);
181     LFS_ASSERT(block < cfg->block_count);
182 
183     // block bad?
184     if (bd->cfg->erase_cycles && bd->wear[block] >= bd->cfg->erase_cycles) {
185         if (bd->cfg->badblock_behavior ==
186                 LFS_TESTBD_BADBLOCK_PROGERROR) {
187             LFS_TESTBD_TRACE("lfs_testbd_prog -> %d", LFS_ERR_CORRUPT);
188             return LFS_ERR_CORRUPT;
189         } else if (bd->cfg->badblock_behavior ==
190                 LFS_TESTBD_BADBLOCK_PROGNOOP ||
191                 bd->cfg->badblock_behavior ==
192                 LFS_TESTBD_BADBLOCK_ERASENOOP) {
193             LFS_TESTBD_TRACE("lfs_testbd_prog -> %d", 0);
194             return 0;
195         }
196     }
197 
198     // prog
199     int err = lfs_testbd_rawprog(cfg, block, off, buffer, size);
200     if (err) {
201         LFS_TESTBD_TRACE("lfs_testbd_prog -> %d", err);
202         return err;
203     }
204 
205     // lose power?
206     if (bd->power_cycles > 0) {
207         bd->power_cycles -= 1;
208         if (bd->power_cycles == 0) {
209             // sync to make sure we persist the last changes
210             assert(lfs_testbd_rawsync(cfg) == 0);
211             // simulate power loss
212             exit(33);
213         }
214     }
215 
216     LFS_TESTBD_TRACE("lfs_testbd_prog -> %d", 0);
217     return 0;
218 }
219 
lfs_testbd_erase(const struct lfs_config * cfg,lfs_block_t block)220 int lfs_testbd_erase(const struct lfs_config *cfg, lfs_block_t block) {
221     LFS_TESTBD_TRACE("lfs_testbd_erase(%p, 0x%"PRIx32")", (void*)cfg, block);
222     lfs_testbd_t *bd = cfg->context;
223 
224     // check if erase is valid
225     LFS_ASSERT(block < cfg->block_count);
226 
227     // block bad?
228     if (bd->cfg->erase_cycles) {
229         if (bd->wear[block] >= bd->cfg->erase_cycles) {
230             if (bd->cfg->badblock_behavior ==
231                     LFS_TESTBD_BADBLOCK_ERASEERROR) {
232                 LFS_TESTBD_TRACE("lfs_testbd_erase -> %d", LFS_ERR_CORRUPT);
233                 return LFS_ERR_CORRUPT;
234             } else if (bd->cfg->badblock_behavior ==
235                     LFS_TESTBD_BADBLOCK_ERASENOOP) {
236                 LFS_TESTBD_TRACE("lfs_testbd_erase -> %d", 0);
237                 return 0;
238             }
239         } else {
240             // mark wear
241             bd->wear[block] += 1;
242         }
243     }
244 
245     // erase
246     int err = lfs_testbd_rawerase(cfg, block);
247     if (err) {
248         LFS_TESTBD_TRACE("lfs_testbd_erase -> %d", err);
249         return err;
250     }
251 
252     // lose power?
253     if (bd->power_cycles > 0) {
254         bd->power_cycles -= 1;
255         if (bd->power_cycles == 0) {
256             // sync to make sure we persist the last changes
257             assert(lfs_testbd_rawsync(cfg) == 0);
258             // simulate power loss
259             exit(33);
260         }
261     }
262 
263     LFS_TESTBD_TRACE("lfs_testbd_prog -> %d", 0);
264     return 0;
265 }
266 
lfs_testbd_sync(const struct lfs_config * cfg)267 int lfs_testbd_sync(const struct lfs_config *cfg) {
268     LFS_TESTBD_TRACE("lfs_testbd_sync(%p)", (void*)cfg);
269     int err = lfs_testbd_rawsync(cfg);
270     LFS_TESTBD_TRACE("lfs_testbd_sync -> %d", err);
271     return err;
272 }
273 
274 
275 /// simulated wear operations ///
lfs_testbd_getwear(const struct lfs_config * cfg,lfs_block_t block)276 lfs_testbd_swear_t lfs_testbd_getwear(const struct lfs_config *cfg,
277         lfs_block_t block) {
278     LFS_TESTBD_TRACE("lfs_testbd_getwear(%p, %"PRIu32")", (void*)cfg, block);
279     lfs_testbd_t *bd = cfg->context;
280 
281     // check if block is valid
282     LFS_ASSERT(bd->cfg->erase_cycles);
283     LFS_ASSERT(block < cfg->block_count);
284 
285     LFS_TESTBD_TRACE("lfs_testbd_getwear -> %"PRIu32, bd->wear[block]);
286     return bd->wear[block];
287 }
288 
lfs_testbd_setwear(const struct lfs_config * cfg,lfs_block_t block,lfs_testbd_wear_t wear)289 int lfs_testbd_setwear(const struct lfs_config *cfg,
290         lfs_block_t block, lfs_testbd_wear_t wear) {
291     LFS_TESTBD_TRACE("lfs_testbd_setwear(%p, %"PRIu32")", (void*)cfg, block);
292     lfs_testbd_t *bd = cfg->context;
293 
294     // check if block is valid
295     LFS_ASSERT(bd->cfg->erase_cycles);
296     LFS_ASSERT(block < cfg->block_count);
297 
298     bd->wear[block] = wear;
299 
300     LFS_TESTBD_TRACE("lfs_testbd_setwear -> %d", 0);
301     return 0;
302 }
303