Mbed OS Reference
Loading...
Searching...
No Matches
MBRBlockDevice.h
1/* mbed Microcontroller Library
2 * Copyright (c) 2017-2020 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_MBR_BLOCK_DEVICE_H
20#define MBED_MBR_BLOCK_DEVICE_H
21
22#include "BlockDevice.h"
23
24namespace mbed {
25
26/** \addtogroup storage-blockdevice */
27/** @{*/
28
29/** Additional error codes used for MBR records
30 */
31enum {
32 BD_ERROR_INVALID_MBR = -3101,
33 BD_ERROR_INVALID_PARTITION = -3102,
34};
35
36
37/** Block device for managing a Master Boot Record
38 * https://en.wikipedia.org/wiki/Master_boot_record
39 *
40 * @note
41 * The MBR partition table is relatively limited:
42 * - At most 4 partitions are supported
43 * - Extended partitions are currently not supported and will error during init
44 */
46public:
47 /** Format the MBR to contain the following partition
48 *
49 * @param bd Block device to partition
50 * @param part Partition to use, 1-4
51 * @param type 8-bit partition type to specify the filesystem to use in this partition.
52 * It can be found in the filesystem documentation or refer to
53 * https://en.wikipedia.org/wiki/Partition_type#List_of_partition_IDs
54 * @param start Start block address to map to block 0 of partition.
55 * Negative addresses are calculated from the end of the
56 * underlying block devices. Block 0 is implicitly ignored
57 * from the range to store the MBR.
58 * @return 0 on success or a negative error code on failure.
59 * @note This is the same as partition(bd, part, type, start, bd->size())
60 */
61 static int partition(BlockDevice *bd, int part, uint8_t type, bd_addr_t start);
62
63 /** Format the MBR to contain the following partition
64 *
65 * @param bd Block device to partition
66 * @param part Partition to use, 1-4
67 * @param type 8-bit partition type to specify the filesystem to use in this partition.
68 * It can be found in the filesystem documentation or refer to
69 * https://en.wikipedia.org/wiki/Partition_type#List_of_partition_IDs
70 * @param start Start block address to map to block 0 of partition,
71 * negative addresses are calculated from the end of the
72 * underlying block devices. Block 0 is implicitly ignored
73 * from the range to store the MBR.
74 * @param stop End block address to mark the end of the partition.
75 * This block is not mapped: negative addresses are calculated
76 * from the end of the underlying block device.
77 * @return 0 on success or a negative error code on failure.
78 */
79 static int partition(BlockDevice *bd, int part, uint8_t type, bd_addr_t start, bd_addr_t stop);
80
81 /** Lifetime of the block device
82 *
83 * @param bd Block device to back the MBRBlockDevice
84 * @param part Partition to use, 1-4
85 */
87
88 /** Lifetime of the block device
89 */
90 virtual ~MBRBlockDevice() {};
91
92 /** Initialize a block device
93 *
94 * @return 0 on success or a negative error code on failure
95 */
96 virtual int init();
97
98 /** Deinitialize a block device
99 *
100 * @return 0 on success or a negative error code on failure
101 */
102 virtual int deinit();
103
104 /** Ensure data on storage is in sync with the driver
105 *
106 * @return 0 on success or a negative error code on failure
107 */
108 virtual int sync();
109
110 /** Read blocks from a block device
111 *
112 * @param buffer Buffer to read blocks into
113 * @param addr Address of block to begin reading from
114 * @param size Size to read in bytes, must be a multiple of read block size
115 * @return 0 on success or a negative error code on failure
116 */
117 virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
118
119 /** Program blocks to a block device
120 *
121 * The blocks must have been erased prior to being programmed
122 *
123 * @param buffer Buffer of data to write to blocks
124 * @param addr Address of block to begin writing to
125 * @param size Size to write in bytes, must be a multiple of program block size
126 * @return 0 on success or a negative error code on failure
127 */
128 virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
129
130 /** Erase blocks on a block device
131 *
132 * The state of an erased block is undefined until it has been programmed,
133 * unless get_erase_value returns a non-negative byte value
134 *
135 * @param addr Address of block to begin erasing
136 * @param size Size to erase in bytes, must be a multiple of erase block size
137 * @return 0 on success or a negative error code on failure
138 */
139 virtual int erase(bd_addr_t addr, bd_size_t size);
140
141 /** Get the size of a readable block
142 *
143 * @return Size of a readable block in bytes
144 */
145 virtual bd_size_t get_read_size() const;
146
147 /** Get the size of a programmable block
148 *
149 * @return Size of a programmable block in bytes
150 * @note Must be a multiple of the read size
151 */
153
154 /** Get the size of an erasable block
155 *
156 * @return Size of an erasable block in bytes
157 * @note Must be a multiple of the program size
158 */
159 virtual bd_size_t get_erase_size() const;
160
161 /** Get the size of an erasable block given address
162 *
163 * @param addr Address within the erasable block
164 * @return Size of an erasable block in bytes
165 * @note Must be a multiple of the program size
166 */
167 virtual bd_size_t get_erase_size(bd_addr_t addr) const;
168
169 /** Get the value of storage when erased
170 *
171 * If get_erase_value returns a non-negative byte value, the underlying
172 * storage is set to that value when erased, and storage containing
173 * that value can be programmed without another erase.
174 *
175 * @return The value of storage when erased, or -1 if you can't
176 * rely on the value of erased storage
177 */
178 virtual int get_erase_value() const;
179
180 /** Get the total size of the underlying device
181 *
182 * @return Size of the underlying device in bytes
183 */
184 virtual bd_size_t size() const;
185
186 /** Get the offset of the partition on the underlying block device
187 * @return Offset of the partition on the underlying device
188 */
190
191 /** Get size of partition on underlying block device
192 * @return Size of the partition on the underlying device
193 */
195
196 /** Get 8-bit type of the partition
197 * @return 8-bit type of partition assigned during format
198 */
199 virtual uint8_t get_partition_type() const;
200
201 /** Get the partition number
202 * @return The partition number, 1-4
203 */
204 virtual int get_partition_number() const;
205
206 /** Get the BlockDevice class type.
207 *
208 * @return A string represent the BlockDevice class type.
209 */
210 virtual const char *get_type() const;
211
212protected:
213 BlockDevice *_bd;
214 bd_size_t _offset;
215 bd_size_t _size;
216 uint8_t _type;
217 uint8_t _part;
218 uint32_t _init_ref_count;
219 bool _is_initialized;
220};
221
222/** @}*/
223
224} // namespace mbed
225
226// Added "using" for backwards compatibility
227#ifndef MBED_NO_GLOBAL_USING_DIRECTIVE
229#endif
230
231#endif
232
A hardware device capable of writing and reading blocks.
Definition: BlockDevice.h:53
Block device for managing a Master Boot Record https://en.wikipedia.org/wiki/Master_boot_record.
virtual const char * get_type() const
Get the BlockDevice class type.
MBRBlockDevice(BlockDevice *bd, int part)
Lifetime of the block device.
static int partition(BlockDevice *bd, int part, uint8_t type, bd_addr_t start, bd_addr_t stop)
Format the MBR to contain the following partition.
virtual int erase(bd_addr_t addr, bd_size_t size)
Erase blocks on a block device.
virtual bd_size_t get_program_size() const
Get the size of a programmable block.
virtual uint8_t get_partition_type() const
Get 8-bit type of the partition.
virtual ~MBRBlockDevice()
Lifetime of the block device.
virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size)
Program blocks to a block device.
virtual int sync()
Ensure data on storage is in sync with the driver.
virtual bd_size_t get_erase_size() const
Get the size of an erasable block.
virtual int deinit()
Deinitialize a block device.
virtual bd_size_t get_read_size() const
Get the size of a readable block.
static int partition(BlockDevice *bd, int part, uint8_t type, bd_addr_t start)
Format the MBR to contain the following partition.
virtual bd_size_t size() const
Get the total size of the underlying device.
virtual int read(void *buffer, bd_addr_t addr, bd_size_t size)
Read blocks from a block device.
virtual int init()
Initialize a block device.
virtual bd_addr_t get_partition_start() const
Get the offset of the partition on the underlying block device.
virtual int get_erase_value() const
Get the value of storage when erased.
virtual int get_partition_number() const
Get the partition number.
virtual bd_addr_t get_partition_stop() const
Get size of partition on underlying block device.
virtual bd_size_t get_erase_size(bd_addr_t addr) const
Get the size of an erasable block given address.
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