Mbed OS Reference
Loading...
Searching...
No Matches
lfs2_testbd.h
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#ifndef LFS2_TESTBD_H
9#define LFS2_TESTBD_H
10
11#include "lfs2.h"
12#include "lfs2_util.h"
13#include "bd/lfs2_rambd.h"
14#include "bd/lfs2_filebd.h"
15
16#ifdef __cplusplus
17extern "C"
18{
19#endif
20
21
22// Block device specific tracing
23#ifdef LFS2_TESTBD_YES_TRACE
24#define LFS2_TESTBD_TRACE(...) LFS2_TRACE(__VA_ARGS__)
25#else
26#define LFS2_TESTBD_TRACE(...)
27#endif
28
29// Mode determining how "bad blocks" behave during testing. This simulates
30// some real-world circumstances such as progs not sticking (prog-noop),
31// a readonly disk (erase-noop), and ECC failures (read-error).
32//
33// Not that read-noop is not allowed. Read _must_ return a consistent (but
34// may be arbitrary) value on every read.
35enum lfs2_testbd_badblock_behavior {
36 LFS2_TESTBD_BADBLOCK_PROGERROR,
37 LFS2_TESTBD_BADBLOCK_ERASEERROR,
38 LFS2_TESTBD_BADBLOCK_READERROR,
39 LFS2_TESTBD_BADBLOCK_PROGNOOP,
40 LFS2_TESTBD_BADBLOCK_ERASENOOP,
41};
42
43// Type for measuring wear
44typedef uint32_t lfs2_testbd_wear_t;
45typedef int32_t lfs2_testbd_swear_t;
46
47// testbd config, this is required for testing
49 // 8-bit erase value to use for simulating erases. -1 does not simulate
50 // erases, which can speed up testing by avoiding all the extra block-device
51 // operations to store the erase value.
52 int32_t erase_value;
53
54 // Number of erase cycles before a block becomes "bad". The exact behavior
55 // of bad blocks is controlled by the badblock_mode.
56 uint32_t erase_cycles;
57
58 // The mode determining how bad blocks fail
59 uint8_t badblock_behavior;
60
61 // Number of write operations (erase/prog) before forcefully killing
62 // the program with exit. Simulates power-loss. 0 disables.
63 uint32_t power_cycles;
64
65 // Optional buffer for RAM block device.
66 void *buffer;
67
68 // Optional buffer for wear
69 void *wear_buffer;
70};
71
72// testbd state
73typedef struct lfs2_testbd {
74 union {
75 struct {
77 struct lfs2_filebd_config cfg;
78 } file;
79 struct {
80 lfs2_rambd_t bd;
81 struct lfs2_rambd_config cfg;
82 } ram;
83 } u;
84
85 bool persist;
86 uint32_t power_cycles;
87 lfs2_testbd_wear_t *wear;
88
89 const struct lfs2_testbd_config *cfg;
91
92
93/// Block device API ///
94
95// Create a test block device using the geometry in lfs2_config
96//
97// Note that filebd is used if a path is provided, if path is NULL
98// testbd will use rambd which can be much faster.
99int lfs2_testbd_create(const struct lfs2_config *cfg, const char *path);
100int lfs2_testbd_createcfg(const struct lfs2_config *cfg, const char *path,
101 const struct lfs2_testbd_config *bdcfg);
102
103// Clean up memory associated with block device
104int lfs2_testbd_destroy(const struct lfs2_config *cfg);
105
106// Read a block
107int lfs2_testbd_read(const struct lfs2_config *cfg, lfs2_block_t block,
108 lfs2_off_t off, void *buffer, lfs2_size_t size);
109
110// Program a block
111//
112// The block must have previously been erased.
113int lfs2_testbd_prog(const struct lfs2_config *cfg, lfs2_block_t block,
114 lfs2_off_t off, const void *buffer, lfs2_size_t size);
115
116// Erase a block
117//
118// A block must be erased before being programmed. The
119// state of an erased block is undefined.
120int lfs2_testbd_erase(const struct lfs2_config *cfg, lfs2_block_t block);
121
122// Sync the block device
123int lfs2_testbd_sync(const struct lfs2_config *cfg);
124
125
126/// Additional extended API for driving test features ///
127
128// Get simulated wear on a given block
129lfs2_testbd_swear_t lfs2_testbd_getwear(const struct lfs2_config *cfg,
130 lfs2_block_t block);
131
132// Manually set simulated wear on a given block
133int lfs2_testbd_setwear(const struct lfs2_config *cfg,
134 lfs2_block_t block, lfs2_testbd_wear_t wear);
135
136
137#ifdef __cplusplus
138} /* extern "C" */
139#endif
140
141#endif