1 /* 2 * Block device emulated in a file 3 * 4 * Copyright (c) 2022, The littlefs authors. 5 * Copyright (c) 2017, Arm Limited. All rights reserved. 6 * SPDX-License-Identifier: BSD-3-Clause 7 */ 8 #ifndef LFS_FILEBD_H 9 #define LFS_FILEBD_H 10 11 #include "lfs.h" 12 #include "lfs_util.h" 13 14 #ifdef __cplusplus 15 extern "C" 16 { 17 #endif 18 19 20 // Block device specific tracing 21 #ifdef LFS_FILEBD_YES_TRACE 22 #define LFS_FILEBD_TRACE(...) LFS_TRACE(__VA_ARGS__) 23 #else 24 #define LFS_FILEBD_TRACE(...) 25 #endif 26 27 // filebd config (optional) 28 struct lfs_filebd_config { 29 // 8-bit erase value to use for simulating erases. -1 does not simulate 30 // erases, which can speed up testing by avoiding all the extra block-device 31 // operations to store the erase value. 32 int32_t erase_value; 33 }; 34 35 // filebd state 36 typedef struct lfs_filebd { 37 int fd; 38 const struct lfs_filebd_config *cfg; 39 } lfs_filebd_t; 40 41 42 // Create a file block device using the geometry in lfs_config 43 int lfs_filebd_create(const struct lfs_config *cfg, const char *path); 44 int lfs_filebd_createcfg(const struct lfs_config *cfg, const char *path, 45 const struct lfs_filebd_config *bdcfg); 46 47 // Clean up memory associated with block device 48 int lfs_filebd_destroy(const struct lfs_config *cfg); 49 50 // Read a block 51 int lfs_filebd_read(const struct lfs_config *cfg, lfs_block_t block, 52 lfs_off_t off, void *buffer, lfs_size_t size); 53 54 // Program a block 55 // 56 // The block must have previously been erased. 57 int lfs_filebd_prog(const struct lfs_config *cfg, lfs_block_t block, 58 lfs_off_t off, const void *buffer, lfs_size_t size); 59 60 // Erase a block 61 // 62 // A block must be erased before being programmed. The 63 // state of an erased block is undefined. 64 int lfs_filebd_erase(const struct lfs_config *cfg, lfs_block_t block); 65 66 // Sync the block device 67 int lfs_filebd_sync(const struct lfs_config *cfg); 68 69 70 #ifdef __cplusplus 71 } /* extern "C" */ 72 #endif 73 74 #endif 75