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

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.