1 /*
2  * Block device emulated in RAM
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_RAMBD_H
9 #define LFS_RAMBD_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_RAMBD_YES_TRACE
22 #define LFS_RAMBD_TRACE(...) LFS_TRACE(__VA_ARGS__)
23 #else
24 #define LFS_RAMBD_TRACE(...)
25 #endif
26 
27 // rambd config (optional)
28 struct lfs_rambd_config {
29     // 8-bit erase value to simulate erasing with. -1 indicates no erase
30     // occurs, which is still a valid block device
31     int32_t erase_value;
32 
33     // Optional statically allocated buffer for the block device.
34     void *buffer;
35 };
36 
37 // rambd state
38 typedef struct lfs_rambd {
39     uint8_t *buffer;
40     const struct lfs_rambd_config *cfg;
41 } lfs_rambd_t;
42 
43 
44 // Create a RAM block device using the geometry in lfs_config
45 int lfs_rambd_create(const struct lfs_config *cfg);
46 int lfs_rambd_createcfg(const struct lfs_config *cfg,
47         const struct lfs_rambd_config *bdcfg);
48 
49 // Clean up memory associated with block device
50 int lfs_rambd_destroy(const struct lfs_config *cfg);
51 
52 // Read a block
53 int lfs_rambd_read(const struct lfs_config *cfg, lfs_block_t block,
54         lfs_off_t off, void *buffer, lfs_size_t size);
55 
56 // Program a block
57 //
58 // The block must have previously been erased.
59 int lfs_rambd_prog(const struct lfs_config *cfg, lfs_block_t block,
60         lfs_off_t off, const void *buffer, lfs_size_t size);
61 
62 // Erase a block
63 //
64 // A block must be erased before being programmed. The
65 // state of an erased block is undefined.
66 int lfs_rambd_erase(const struct lfs_config *cfg, lfs_block_t block);
67 
68 // Sync the block device
69 int lfs_rambd_sync(const struct lfs_config *cfg);
70 
71 
72 #ifdef __cplusplus
73 } /* extern "C" */
74 #endif
75 
76 #endif
77