Mbed OS Reference
Loading...
Searching...
No Matches
BlockDevice.h
1/* mbed Microcontroller Library
2 * Copyright (c) 2017 ARM Limited
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
19#ifndef MBED_BLOCK_DEVICE_H
20#define MBED_BLOCK_DEVICE_H
21
22#include <stdint.h>
23
24namespace mbed {
25
26/** \defgroup storage Storage
27 * \ingroup mbed-os-public
28 * @{
29 * \defgroup storage-blockdevice BlockDevice
30 * \ingroup storage */
31/** @{ */
32
33/** Enum of standard error codes
34 *
35 * @enum bd_error
36 */
38 BD_ERROR_OK = 0, /*!< no error */
39 BD_ERROR_DEVICE_ERROR = -4001, /*!< device specific error */
40};
41
42/** Type representing the address of a specific block
43 */
44typedef uint64_t bd_addr_t;
45
46/** Type representing a quantity of 8-bit bytes
47 */
48typedef uint64_t bd_size_t;
49
50
51/** A hardware device capable of writing and reading blocks
52 */
54public:
55
56 /** Return the default block device
57 *
58 * Returns the default block device based on the configuration JSON.
59 * Use the components in target.json or application config to change
60 * the default block device.
61 *
62 * An application can override all target settings by implementing
63 * BlockDevice::get_default_instance() - the default
64 * definition is weak, and calls get_target_default_instance().
65 */
67
68 /** Lifetime of a block device
69 */
70 virtual ~BlockDevice() {};
71
72 /** Initialize a block device
73 *
74 * @return 0 on success or a negative error code on failure
75 */
76 virtual int init() = 0;
77
78 /** Deinitialize a block device
79 *
80 * @return 0 on success or a negative error code on failure
81 */
82 virtual int deinit() = 0;
83
84 /** Ensure data on storage is in sync with the driver
85 *
86 * @return 0 on success or a negative error code on failure
87 */
88 virtual int sync()
89 {
90 return 0;
91 }
92
93 /** Read blocks from a block device
94 *
95 * If a failure occurs, it is not possible to determine how many bytes succeeded
96 *
97 * @param buffer Buffer to write blocks to
98 * @param addr Address of block to begin reading from
99 * @param size Size to read in bytes, must be a multiple of the read block size
100 * @return 0 on success or a negative error code on failure
101 */
102 virtual int read(void *buffer, bd_addr_t addr, bd_size_t size) = 0;
103
104 /** Program blocks to a block device
105 *
106 * The blocks must have been erased prior to being programmed
107 *
108 * If a failure occurs, it is not possible to determine how many bytes succeeded
109 *
110 * @param buffer Buffer of data to write to blocks
111 * @param addr Address of block to begin writing to
112 * @param size Size to write in bytes, must be a multiple of the program block size
113 * @return 0 on success or a negative error code on failure
114 */
115 virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size) = 0;
116
117 /** Erase blocks on a block device
118 *
119 * The state of an erased block is undefined until it has been programmed,
120 * unless get_erase_value returns a non-negative byte value
121 *
122 * @param addr Address of block to begin erasing
123 * @param size Size to erase in bytes, must be a multiple of the erase block size
124 * @return 0 on success or a negative error code on failure
125 */
126 virtual int erase(bd_addr_t addr, bd_size_t size)
127 {
128 return 0;
129 }
130
131 /** Mark blocks as no longer in use
132 *
133 * This function provides a hint to the underlying block device that a region of blocks
134 * is no longer in use and may be erased without side effects. Erase must still be called
135 * before programming, but trimming allows flash-translation-layers to schedule erases when
136 * the device is not busy.
137 *
138 * @param addr Address of block to mark as unused
139 * @param size Size to mark as unused in bytes, must be a multiple of the erase block size
140 * @return 0 on success or a negative error code on failure
141 */
142 virtual int trim(bd_addr_t addr, bd_size_t size)
143 {
144 return 0;
145 }
146
147 /** Get the size of a readable block
148 *
149 * @return Size of a readable block in bytes
150 */
151 virtual bd_size_t get_read_size() const = 0;
152
153 /** Get the size of a programmable block
154 *
155 * @return Size of a programmable block in bytes
156 * @note Must be a multiple of the read size
157 */
158 virtual bd_size_t get_program_size() const = 0;
159
160 /** Get the size of an erasable block
161 *
162 * @return Size of an erasable block in bytes
163 * @note Must be a multiple of the program size
164 */
165 virtual bd_size_t get_erase_size() const
166 {
167 return get_program_size();
168 }
169
170 /** Get the size of an erasable block given address
171 *
172 * @param addr Address within the erasable block
173 * @return Size of an erasable block in bytes
174 * @note Must be a multiple of the program size
175 */
177 {
178 return get_erase_size();
179 }
180
181 /** Get the value of storage when erased
182 *
183 * If get_erase_value returns a non-negative byte value, the underlying
184 * storage is set to that value when erased, and storage containing
185 * that value can be programmed without another erase.
186 *
187 * @return The value of storage when erased, or -1 if you can't
188 * rely on the value of the erased storage
189 */
190 virtual int get_erase_value() const
191 {
192 return -1;
193 }
194
195 /** Get the total size of the underlying device
196 *
197 * @return Size of the underlying device in bytes
198 */
199 virtual bd_size_t size() const = 0;
200
201 /** Convenience function for checking block read validity
202 *
203 * @param addr Address of block to begin reading from
204 * @param size Size to read in bytes
205 * @return True if read is valid for underlying block device
206 */
207 virtual bool is_valid_read(bd_addr_t addr, bd_size_t size) const
208 {
209 return (
210 addr % get_read_size() == 0 &&
211 size % get_read_size() == 0 &&
212 addr + size <= this->size());
213 }
214
215 /** Convenience function for checking block program validity
216 *
217 * @param addr Address of block to begin writing to
218 * @param size Size to write in bytes
219 * @return True if program is valid for underlying block device
220 */
221 virtual bool is_valid_program(bd_addr_t addr, bd_size_t size) const
222 {
223 return (
224 addr % get_program_size() == 0 &&
225 size % get_program_size() == 0 &&
226 addr + size <= this->size());
227 }
228
229 /** Convenience function for checking block erase validity
230 *
231 * @param addr Address of block to begin erasing
232 * @param size Size to erase in bytes
233 * @return True if erase is valid for underlying block device
234 */
235 virtual bool is_valid_erase(bd_addr_t addr, bd_size_t size) const
236 {
237 return (
238 addr % get_erase_size(addr) == 0 &&
239 (addr + size) % get_erase_size(addr + size - 1) == 0 &&
240 addr + size <= this->size());
241 }
242
243 /** Get the BlockDevice class type.
244 *
245 * @return A string represent the BlockDevice class type.
246 */
247 virtual const char *get_type() const = 0;
248};
249
250/** @}
251 * @}
252 */
253
254} // namespace mbed
255
256// Added "using" for backwards compatibility
257#ifndef MBED_NO_GLOBAL_USING_DIRECTIVE
259using mbed::bd_addr_t;
260using mbed::bd_size_t;
263#endif
264
265#endif
266
A hardware device capable of writing and reading blocks.
Definition: BlockDevice.h:53
virtual bd_size_t get_read_size() const =0
Get the size of a readable block.
virtual bd_size_t get_program_size() const =0
Get the size of a programmable block.
virtual int deinit()=0
Deinitialize a block device.
virtual int erase(bd_addr_t addr, bd_size_t size)
Erase blocks on a block device.
Definition: BlockDevice.h:126
virtual const char * get_type() const =0
Get the BlockDevice class type.
virtual ~BlockDevice()
Lifetime of a block device.
Definition: BlockDevice.h:70
virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size)=0
Program blocks to a block device.
virtual bool is_valid_read(bd_addr_t addr, bd_size_t size) const
Convenience function for checking block read validity.
Definition: BlockDevice.h:207
virtual int read(void *buffer, bd_addr_t addr, bd_size_t size)=0
Read blocks from a block device.
virtual int sync()
Ensure data on storage is in sync with the driver.
Definition: BlockDevice.h:88
virtual bd_size_t get_erase_size() const
Get the size of an erasable block.
Definition: BlockDevice.h:165
virtual bool is_valid_erase(bd_addr_t addr, bd_size_t size) const
Convenience function for checking block erase validity.
Definition: BlockDevice.h:235
static BlockDevice * get_default_instance()
Return the default block device.
virtual int trim(bd_addr_t addr, bd_size_t size)
Mark blocks as no longer in use.
Definition: BlockDevice.h:142
virtual int init()=0
Initialize a block device.
virtual bool is_valid_program(bd_addr_t addr, bd_size_t size) const
Convenience function for checking block program validity.
Definition: BlockDevice.h:221
virtual int get_erase_value() const
Get the value of storage when erased.
Definition: BlockDevice.h:190
virtual bd_size_t size() const =0
Get the total size of the underlying device.
virtual bd_size_t get_erase_size(bd_addr_t addr) const
Get the size of an erasable block given address.
Definition: BlockDevice.h:176
uint64_t bd_size_t
Type representing a quantity of 8-bit bytes.
Definition: BlockDevice.h:48
uint64_t bd_addr_t
Type representing the address of a specific block.
Definition: BlockDevice.h:44
bd_error
Enum of standard error codes.
Definition: BlockDevice.h:37
@ BD_ERROR_DEVICE_ERROR
Definition: BlockDevice.h:39
@ BD_ERROR_OK
Definition: BlockDevice.h:38