Mistake on this page?
Report an issue in GitHub or email us
MBRBlockDevice.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2017 ARM Limited
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
23 /** \addtogroup storage */
24 /** @{*/
25 
26 #ifndef MBED_MBR_BLOCK_DEVICE_H
27 #define MBED_MBR_BLOCK_DEVICE_H
28 
29 #include "BlockDevice.h"
30 
31 namespace mbed {
32 
33 /** Additional error codes used for MBR records
34  */
35 enum {
36  BD_ERROR_INVALID_MBR = -3101,
37  BD_ERROR_INVALID_PARTITION = -3102,
38 };
39 
40 
41 /** Block device for managing a Master Boot Record
42  * https://en.wikipedia.org/wiki/Master_boot_record
43  *
44  * @note
45  * The MBR partition table is relatively limited:
46  * - At most 4 partitions are supported
47  * - Extended partitions are currently not supported and will error during init
48  */
49 class MBRBlockDevice : public BlockDevice {
50 public:
51  /** Format the MBR to contain the following partition
52  *
53  * @param bd Block device to partition
54  * @param part Partition to use, 1-4
55  * @param type 8-bit partition type to identify partition contents
56  * @param start Start block address to map to block 0 of partition.
57  * Negative addresses are calculated from the end of the
58  * underlying block devices. Block 0 is implicitly ignored
59  * from the range to store the MBR.
60  * @return 0 on success or a negative error code on failure.
61  * @note This is the same as partition(bd, part, type, start, bd->size())
62  */
63  static int partition(BlockDevice *bd, int part, uint8_t type, bd_addr_t start);
64 
65  /** Format the MBR to contain the following partition
66  *
67  * @param bd Block device to partition
68  * @param part Partition to use, 1-4
69  * @param type 8-bit partition type to identify partition contents
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  */
86  MBRBlockDevice(BlockDevice *bd, int part);
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  */
152  virtual bd_size_t get_program_size() const;
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  */
189  virtual bd_addr_t get_partition_start() const;
190 
191  /** Get size of partition on underlying block device
192  * @return Size of the partition on the underlying device
193  */
194  virtual bd_addr_t get_partition_stop() const;
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 
212 protected:
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 } // namespace mbed
223 
224 // Added "using" for backwards compatibility
225 #ifndef MBED_NO_GLOBAL_USING_DIRECTIVE
227 #endif
228 
229 #endif
230 
231 /** @}*/
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:47
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.
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.