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