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