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 #ifndef LFS_RAMBD_TRACE
22 #ifdef LFS_RAMBD_YES_TRACE
23 #define LFS_RAMBD_TRACE(...) LFS_TRACE(__VA_ARGS__)
24 #else
25 #define LFS_RAMBD_TRACE(...)
26 #endif
27 #endif
28 
29 // rambd config
30 struct lfs_rambd_config {
31     // Minimum size of a read operation in bytes.
32     lfs_size_t read_size;
33 
34     // Minimum size of a program operation in bytes.
35     lfs_size_t prog_size;
36 
37     // Size of an erase operation in bytes.
38     lfs_size_t erase_size;
39 
40     // Number of erase blocks on the device.
41     lfs_size_t erase_count;
42 
43     // Optional statically allocated buffer for the block device.
44     void *buffer;
45 };
46 
47 // rambd state
48 typedef struct lfs_rambd {
49     uint8_t *buffer;
50     const struct lfs_rambd_config *cfg;
51 } lfs_rambd_t;
52 
53 
54 // Create a RAM block device
55 int lfs_rambd_create(const struct lfs_config *cfg,
56         const struct lfs_rambd_config *bdcfg);
57 
58 // Clean up memory associated with block device
59 int lfs_rambd_destroy(const struct lfs_config *cfg);
60 
61 // Read a block
62 int lfs_rambd_read(const struct lfs_config *cfg, lfs_block_t block,
63         lfs_off_t off, void *buffer, lfs_size_t size);
64 
65 // Program a block
66 //
67 // The block must have previously been erased.
68 int lfs_rambd_prog(const struct lfs_config *cfg, lfs_block_t block,
69         lfs_off_t off, const void *buffer, lfs_size_t size);
70 
71 // Erase a block
72 //
73 // A block must be erased before being programmed. The
74 // state of an erased block is undefined.
75 int lfs_rambd_erase(const struct lfs_config *cfg, lfs_block_t block);
76 
77 // Sync the block device
78 int lfs_rambd_sync(const struct lfs_config *cfg);
79 
80 
81 #ifdef __cplusplus
82 } /* extern "C" */
83 #endif
84 
85 #endif
86