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 LFS_TESTBD_H 9 #define LFS_TESTBD_H 10 11 #include "lfs.h" 12 #include "lfs_util.h" 13 #include "bd/lfs_rambd.h" 14 #include "bd/lfs_filebd.h" 15 16 #ifdef __cplusplus 17 extern "C" 18 { 19 #endif 20 21 22 // Block device specific tracing 23 #ifdef LFS_TESTBD_YES_TRACE 24 #define LFS_TESTBD_TRACE(...) LFS_TRACE(__VA_ARGS__) 25 #else 26 #define LFS_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. 35 enum lfs_testbd_badblock_behavior { 36 LFS_TESTBD_BADBLOCK_PROGERROR, 37 LFS_TESTBD_BADBLOCK_ERASEERROR, 38 LFS_TESTBD_BADBLOCK_READERROR, 39 LFS_TESTBD_BADBLOCK_PROGNOOP, 40 LFS_TESTBD_BADBLOCK_ERASENOOP, 41 }; 42 43 // Type for measuring wear 44 typedef uint32_t lfs_testbd_wear_t; 45 typedef int32_t lfs_testbd_swear_t; 46 47 // testbd config, this is required for testing 48 struct lfs_testbd_config { 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 73 typedef struct lfs_testbd { 74 union { 75 struct { 76 lfs_filebd_t bd; 77 struct lfs_filebd_config cfg; 78 } file; 79 struct { 80 lfs_rambd_t bd; 81 struct lfs_rambd_config cfg; 82 } ram; 83 } u; 84 85 bool persist; 86 uint32_t power_cycles; 87 lfs_testbd_wear_t *wear; 88 89 const struct lfs_testbd_config *cfg; 90 } lfs_testbd_t; 91 92 93 /// Block device API /// 94 95 // Create a test block device using the geometry in lfs_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. 99 int lfs_testbd_create(const struct lfs_config *cfg, const char *path); 100 int lfs_testbd_createcfg(const struct lfs_config *cfg, const char *path, 101 const struct lfs_testbd_config *bdcfg); 102 103 // Clean up memory associated with block device 104 int lfs_testbd_destroy(const struct lfs_config *cfg); 105 106 // Read a block 107 int lfs_testbd_read(const struct lfs_config *cfg, lfs_block_t block, 108 lfs_off_t off, void *buffer, lfs_size_t size); 109 110 // Program a block 111 // 112 // The block must have previously been erased. 113 int lfs_testbd_prog(const struct lfs_config *cfg, lfs_block_t block, 114 lfs_off_t off, const void *buffer, lfs_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. 120 int lfs_testbd_erase(const struct lfs_config *cfg, lfs_block_t block); 121 122 // Sync the block device 123 int lfs_testbd_sync(const struct lfs_config *cfg); 124 125 126 /// Additional extended API for driving test features /// 127 128 // Get simulated wear on a given block 129 lfs_testbd_swear_t lfs_testbd_getwear(const struct lfs_config *cfg, 130 lfs_block_t block); 131 132 // Manually set simulated wear on a given block 133 int lfs_testbd_setwear(const struct lfs_config *cfg, 134 lfs_block_t block, lfs_testbd_wear_t wear); 135 136 137 #ifdef __cplusplus 138 } /* extern "C" */ 139 #endif 140 141 #endif 142