![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
BBR 1 Ebene
mbed-os/features/filesystem/bd/MBRBlockDevice.h@0:fbdae7e6d805, 2018-05-14 (annotated)
- Committer:
- borlanic
- Date:
- Mon May 14 11:29:06 2018 +0000
- Revision:
- 0:fbdae7e6d805
BBR
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
borlanic | 0:fbdae7e6d805 | 1 | /* mbed Microcontroller Library |
borlanic | 0:fbdae7e6d805 | 2 | * Copyright (c) 2017 ARM Limited |
borlanic | 0:fbdae7e6d805 | 3 | * |
borlanic | 0:fbdae7e6d805 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
borlanic | 0:fbdae7e6d805 | 5 | * of this software and associated documentation files (the "Software"), to deal |
borlanic | 0:fbdae7e6d805 | 6 | * in the Software without restriction, including without limitation the rights |
borlanic | 0:fbdae7e6d805 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
borlanic | 0:fbdae7e6d805 | 8 | * copies of the Software, and to permit persons to whom the Software is |
borlanic | 0:fbdae7e6d805 | 9 | * furnished to do so, subject to the following conditions: |
borlanic | 0:fbdae7e6d805 | 10 | * |
borlanic | 0:fbdae7e6d805 | 11 | * The above copyright notice and this permission notice shall be included in |
borlanic | 0:fbdae7e6d805 | 12 | * all copies or substantial portions of the Software. |
borlanic | 0:fbdae7e6d805 | 13 | * |
borlanic | 0:fbdae7e6d805 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
borlanic | 0:fbdae7e6d805 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
borlanic | 0:fbdae7e6d805 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
borlanic | 0:fbdae7e6d805 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
borlanic | 0:fbdae7e6d805 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
borlanic | 0:fbdae7e6d805 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
borlanic | 0:fbdae7e6d805 | 20 | * SOFTWARE. |
borlanic | 0:fbdae7e6d805 | 21 | */ |
borlanic | 0:fbdae7e6d805 | 22 | #ifndef MBED_MBR_BLOCK_DEVICE_H |
borlanic | 0:fbdae7e6d805 | 23 | #define MBED_MBR_BLOCK_DEVICE_H |
borlanic | 0:fbdae7e6d805 | 24 | |
borlanic | 0:fbdae7e6d805 | 25 | #include "BlockDevice.h" |
borlanic | 0:fbdae7e6d805 | 26 | #include "mbed.h" |
borlanic | 0:fbdae7e6d805 | 27 | |
borlanic | 0:fbdae7e6d805 | 28 | |
borlanic | 0:fbdae7e6d805 | 29 | /** Additional error codes used for MBR records |
borlanic | 0:fbdae7e6d805 | 30 | */ |
borlanic | 0:fbdae7e6d805 | 31 | enum { |
borlanic | 0:fbdae7e6d805 | 32 | BD_ERROR_INVALID_MBR = -3101, |
borlanic | 0:fbdae7e6d805 | 33 | BD_ERROR_INVALID_PARTITION = -3102, |
borlanic | 0:fbdae7e6d805 | 34 | }; |
borlanic | 0:fbdae7e6d805 | 35 | |
borlanic | 0:fbdae7e6d805 | 36 | |
borlanic | 0:fbdae7e6d805 | 37 | /** Block device for managing a Master Boot Record |
borlanic | 0:fbdae7e6d805 | 38 | * https://en.wikipedia.org/wiki/Master_boot_record |
borlanic | 0:fbdae7e6d805 | 39 | * |
borlanic | 0:fbdae7e6d805 | 40 | * Here is an example of partitioning a heap backed block device |
borlanic | 0:fbdae7e6d805 | 41 | * @code |
borlanic | 0:fbdae7e6d805 | 42 | * #include "mbed.h" |
borlanic | 0:fbdae7e6d805 | 43 | * #include "HeapBlockDevice.h" |
borlanic | 0:fbdae7e6d805 | 44 | * #include "MBRBlockDevice.h" |
borlanic | 0:fbdae7e6d805 | 45 | * |
borlanic | 0:fbdae7e6d805 | 46 | * // Create a block device with 64 blocks of size 512 |
borlanic | 0:fbdae7e6d805 | 47 | * HeapBlockDevice mem(64*512, 512); |
borlanic | 0:fbdae7e6d805 | 48 | * |
borlanic | 0:fbdae7e6d805 | 49 | * // Partition into two partitions with ~half the blocks |
borlanic | 0:fbdae7e6d805 | 50 | * MBRBlockDevice::partition(&mem, 1, 0x83, 0*512, 32*512); |
borlanic | 0:fbdae7e6d805 | 51 | * MBRBlockDevice::partition(&mem, 2, 0x83, 32*512); |
borlanic | 0:fbdae7e6d805 | 52 | * |
borlanic | 0:fbdae7e6d805 | 53 | * // Create a block device that maps to the first 32 blocks (excluding MBR block) |
borlanic | 0:fbdae7e6d805 | 54 | * MBRBlockDevice part1(&mem, 1); |
borlanic | 0:fbdae7e6d805 | 55 | * |
borlanic | 0:fbdae7e6d805 | 56 | * // Create a block device that maps to the last 32 blocks |
borlanic | 0:fbdae7e6d805 | 57 | * MBRBlockDevice part2(&mem, 2); |
borlanic | 0:fbdae7e6d805 | 58 | * @endcode |
borlanic | 0:fbdae7e6d805 | 59 | * |
borlanic | 0:fbdae7e6d805 | 60 | * Here is a more realistic example where the MBRBlockDevice is used |
borlanic | 0:fbdae7e6d805 | 61 | * to partition a region of space on an SD card. When plugged into a computer, |
borlanic | 0:fbdae7e6d805 | 62 | * the partitions will be recognized appropriately. |
borlanic | 0:fbdae7e6d805 | 63 | * @code |
borlanic | 0:fbdae7e6d805 | 64 | * #include "mbed.h" |
borlanic | 0:fbdae7e6d805 | 65 | * #include "SDBlockDevice.h" |
borlanic | 0:fbdae7e6d805 | 66 | * #include "MBRBlockDevice.h" |
borlanic | 0:fbdae7e6d805 | 67 | * #include "FATFileSystem.h" |
borlanic | 0:fbdae7e6d805 | 68 | * |
borlanic | 0:fbdae7e6d805 | 69 | * // Create an SD card |
borlanic | 0:fbdae7e6d805 | 70 | * SDBlockDevice sd(s0, s1, s2, s3); |
borlanic | 0:fbdae7e6d805 | 71 | * |
borlanic | 0:fbdae7e6d805 | 72 | * // Create a partition with 1 GB of space |
borlanic | 0:fbdae7e6d805 | 73 | * MBRBlockDevice::partition(&sd, 1, 0x83, 0, 1024*1024); |
borlanic | 0:fbdae7e6d805 | 74 | * |
borlanic | 0:fbdae7e6d805 | 75 | * // Create the block device that represents the partition |
borlanic | 0:fbdae7e6d805 | 76 | * MBRBlockDevice part1(&sd, 1); |
borlanic | 0:fbdae7e6d805 | 77 | * |
borlanic | 0:fbdae7e6d805 | 78 | * // Format the partition with a FAT filesystem |
borlanic | 0:fbdae7e6d805 | 79 | * FATFileSystem::format(&part1); |
borlanic | 0:fbdae7e6d805 | 80 | * |
borlanic | 0:fbdae7e6d805 | 81 | * // Create the FAT filesystem instance, files can now be written to |
borlanic | 0:fbdae7e6d805 | 82 | * // the FAT filesystem in partition 1 |
borlanic | 0:fbdae7e6d805 | 83 | * FATFileSystem fat("fat", &part1); |
borlanic | 0:fbdae7e6d805 | 84 | * @endcode |
borlanic | 0:fbdae7e6d805 | 85 | * |
borlanic | 0:fbdae7e6d805 | 86 | * @note |
borlanic | 0:fbdae7e6d805 | 87 | * The MBR partition table is relatively limited: |
borlanic | 0:fbdae7e6d805 | 88 | * - At most 4 partitions are supported |
borlanic | 0:fbdae7e6d805 | 89 | * - Extended partitions are currently not supported and will error during init |
borlanic | 0:fbdae7e6d805 | 90 | */ |
borlanic | 0:fbdae7e6d805 | 91 | class MBRBlockDevice : public BlockDevice |
borlanic | 0:fbdae7e6d805 | 92 | { |
borlanic | 0:fbdae7e6d805 | 93 | public: |
borlanic | 0:fbdae7e6d805 | 94 | /** Format the MBR to contain the following partition |
borlanic | 0:fbdae7e6d805 | 95 | * |
borlanic | 0:fbdae7e6d805 | 96 | * @param bd Block device to partition |
borlanic | 0:fbdae7e6d805 | 97 | * @param part Partition to use, 1-4 |
borlanic | 0:fbdae7e6d805 | 98 | * @param type 8-bit partition type to identitfy partition's contents |
borlanic | 0:fbdae7e6d805 | 99 | * @param start Start block address to map to block 0 of partition, |
borlanic | 0:fbdae7e6d805 | 100 | * negative addresses are calculated from the end of the |
borlanic | 0:fbdae7e6d805 | 101 | * underlying block devices. Block 0 is implicitly ignored |
borlanic | 0:fbdae7e6d805 | 102 | * from the range to store the MBR. |
borlanic | 0:fbdae7e6d805 | 103 | * @return 0 on success or a negative error code on failure |
borlanic | 0:fbdae7e6d805 | 104 | * @note This is the same as partition(bd, part, type, start, bd->size()) |
borlanic | 0:fbdae7e6d805 | 105 | */ |
borlanic | 0:fbdae7e6d805 | 106 | static int partition(BlockDevice *bd, int part, uint8_t type, bd_addr_t start); |
borlanic | 0:fbdae7e6d805 | 107 | |
borlanic | 0:fbdae7e6d805 | 108 | /** Format the MBR to contain the following partition |
borlanic | 0:fbdae7e6d805 | 109 | * |
borlanic | 0:fbdae7e6d805 | 110 | * @param bd Block device to partition |
borlanic | 0:fbdae7e6d805 | 111 | * @param part Partition to use, 1-4 |
borlanic | 0:fbdae7e6d805 | 112 | * @param type 8-bit partition type to identitfy partition's contents |
borlanic | 0:fbdae7e6d805 | 113 | * @param start Start block address to map to block 0 of partition, |
borlanic | 0:fbdae7e6d805 | 114 | * negative addresses are calculated from the end of the |
borlanic | 0:fbdae7e6d805 | 115 | * underlying block devices. Block 0 is implicitly ignored |
borlanic | 0:fbdae7e6d805 | 116 | * from the range to store the MBR. |
borlanic | 0:fbdae7e6d805 | 117 | * @param stop End block address to mark the end of the partition, |
borlanic | 0:fbdae7e6d805 | 118 | * this block is not mapped, negative addresses are calculated |
borlanic | 0:fbdae7e6d805 | 119 | * from the end of the underlying block device. |
borlanic | 0:fbdae7e6d805 | 120 | * @return 0 on success or a negative error code on failure |
borlanic | 0:fbdae7e6d805 | 121 | */ |
borlanic | 0:fbdae7e6d805 | 122 | static int partition(BlockDevice *bd, int part, uint8_t type, bd_addr_t start, bd_addr_t stop); |
borlanic | 0:fbdae7e6d805 | 123 | |
borlanic | 0:fbdae7e6d805 | 124 | /** Lifetime of the block device |
borlanic | 0:fbdae7e6d805 | 125 | * |
borlanic | 0:fbdae7e6d805 | 126 | * @param bd Block device to back the MBRBlockDevice |
borlanic | 0:fbdae7e6d805 | 127 | * @param part Partition to use, 1-4 |
borlanic | 0:fbdae7e6d805 | 128 | */ |
borlanic | 0:fbdae7e6d805 | 129 | MBRBlockDevice(BlockDevice *bd, int part); |
borlanic | 0:fbdae7e6d805 | 130 | |
borlanic | 0:fbdae7e6d805 | 131 | /** Lifetime of the block device |
borlanic | 0:fbdae7e6d805 | 132 | */ |
borlanic | 0:fbdae7e6d805 | 133 | virtual ~MBRBlockDevice() {}; |
borlanic | 0:fbdae7e6d805 | 134 | |
borlanic | 0:fbdae7e6d805 | 135 | /** Initialize a block device |
borlanic | 0:fbdae7e6d805 | 136 | * |
borlanic | 0:fbdae7e6d805 | 137 | * @return 0 on success or a negative error code on failure |
borlanic | 0:fbdae7e6d805 | 138 | */ |
borlanic | 0:fbdae7e6d805 | 139 | virtual int init(); |
borlanic | 0:fbdae7e6d805 | 140 | |
borlanic | 0:fbdae7e6d805 | 141 | /** Deinitialize a block device |
borlanic | 0:fbdae7e6d805 | 142 | * |
borlanic | 0:fbdae7e6d805 | 143 | * @return 0 on success or a negative error code on failure |
borlanic | 0:fbdae7e6d805 | 144 | */ |
borlanic | 0:fbdae7e6d805 | 145 | virtual int deinit(); |
borlanic | 0:fbdae7e6d805 | 146 | |
borlanic | 0:fbdae7e6d805 | 147 | /** Ensure data on storage is in sync with the driver |
borlanic | 0:fbdae7e6d805 | 148 | * |
borlanic | 0:fbdae7e6d805 | 149 | * @return 0 on success or a negative error code on failure |
borlanic | 0:fbdae7e6d805 | 150 | */ |
borlanic | 0:fbdae7e6d805 | 151 | virtual int sync(); |
borlanic | 0:fbdae7e6d805 | 152 | |
borlanic | 0:fbdae7e6d805 | 153 | /** Read blocks from a block device |
borlanic | 0:fbdae7e6d805 | 154 | * |
borlanic | 0:fbdae7e6d805 | 155 | * @param buffer Buffer to read blocks into |
borlanic | 0:fbdae7e6d805 | 156 | * @param addr Address of block to begin reading from |
borlanic | 0:fbdae7e6d805 | 157 | * @param size Size to read in bytes, must be a multiple of read block size |
borlanic | 0:fbdae7e6d805 | 158 | * @return 0 on success, negative error code on failure |
borlanic | 0:fbdae7e6d805 | 159 | */ |
borlanic | 0:fbdae7e6d805 | 160 | virtual int read(void *buffer, bd_addr_t addr, bd_size_t size); |
borlanic | 0:fbdae7e6d805 | 161 | |
borlanic | 0:fbdae7e6d805 | 162 | /** Program blocks to a block device |
borlanic | 0:fbdae7e6d805 | 163 | * |
borlanic | 0:fbdae7e6d805 | 164 | * The blocks must have been erased prior to being programmed |
borlanic | 0:fbdae7e6d805 | 165 | * |
borlanic | 0:fbdae7e6d805 | 166 | * @param buffer Buffer of data to write to blocks |
borlanic | 0:fbdae7e6d805 | 167 | * @param addr Address of block to begin writing to |
borlanic | 0:fbdae7e6d805 | 168 | * @param size Size to write in bytes, must be a multiple of program block size |
borlanic | 0:fbdae7e6d805 | 169 | * @return 0 on success, negative error code on failure |
borlanic | 0:fbdae7e6d805 | 170 | */ |
borlanic | 0:fbdae7e6d805 | 171 | virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size); |
borlanic | 0:fbdae7e6d805 | 172 | |
borlanic | 0:fbdae7e6d805 | 173 | /** Erase blocks on a block device |
borlanic | 0:fbdae7e6d805 | 174 | * |
borlanic | 0:fbdae7e6d805 | 175 | * The state of an erased block is undefined until it has been programmed, |
borlanic | 0:fbdae7e6d805 | 176 | * unless get_erase_value returns a non-negative byte value |
borlanic | 0:fbdae7e6d805 | 177 | * |
borlanic | 0:fbdae7e6d805 | 178 | * @param addr Address of block to begin erasing |
borlanic | 0:fbdae7e6d805 | 179 | * @param size Size to erase in bytes, must be a multiple of erase block size |
borlanic | 0:fbdae7e6d805 | 180 | * @return 0 on success, negative error code on failure |
borlanic | 0:fbdae7e6d805 | 181 | */ |
borlanic | 0:fbdae7e6d805 | 182 | virtual int erase(bd_addr_t addr, bd_size_t size); |
borlanic | 0:fbdae7e6d805 | 183 | |
borlanic | 0:fbdae7e6d805 | 184 | /** Get the size of a readable block |
borlanic | 0:fbdae7e6d805 | 185 | * |
borlanic | 0:fbdae7e6d805 | 186 | * @return Size of a readable block in bytes |
borlanic | 0:fbdae7e6d805 | 187 | */ |
borlanic | 0:fbdae7e6d805 | 188 | virtual bd_size_t get_read_size() const; |
borlanic | 0:fbdae7e6d805 | 189 | |
borlanic | 0:fbdae7e6d805 | 190 | /** Get the size of a programmable block |
borlanic | 0:fbdae7e6d805 | 191 | * |
borlanic | 0:fbdae7e6d805 | 192 | * @return Size of a programmable block in bytes |
borlanic | 0:fbdae7e6d805 | 193 | * @note Must be a multiple of the read size |
borlanic | 0:fbdae7e6d805 | 194 | */ |
borlanic | 0:fbdae7e6d805 | 195 | virtual bd_size_t get_program_size() const; |
borlanic | 0:fbdae7e6d805 | 196 | |
borlanic | 0:fbdae7e6d805 | 197 | /** Get the size of an erasable block |
borlanic | 0:fbdae7e6d805 | 198 | * |
borlanic | 0:fbdae7e6d805 | 199 | * @return Size of an erasable block in bytes |
borlanic | 0:fbdae7e6d805 | 200 | * @note Must be a multiple of the program size |
borlanic | 0:fbdae7e6d805 | 201 | */ |
borlanic | 0:fbdae7e6d805 | 202 | virtual bd_size_t get_erase_size() const; |
borlanic | 0:fbdae7e6d805 | 203 | |
borlanic | 0:fbdae7e6d805 | 204 | /** Get the size of an erasable block given address |
borlanic | 0:fbdae7e6d805 | 205 | * |
borlanic | 0:fbdae7e6d805 | 206 | * @param addr Address within the erasable block |
borlanic | 0:fbdae7e6d805 | 207 | * @return Size of an erasable block in bytes |
borlanic | 0:fbdae7e6d805 | 208 | * @note Must be a multiple of the program size |
borlanic | 0:fbdae7e6d805 | 209 | */ |
borlanic | 0:fbdae7e6d805 | 210 | virtual bd_size_t get_erase_size(bd_addr_t addr) const; |
borlanic | 0:fbdae7e6d805 | 211 | |
borlanic | 0:fbdae7e6d805 | 212 | /** Get the value of storage when erased |
borlanic | 0:fbdae7e6d805 | 213 | * |
borlanic | 0:fbdae7e6d805 | 214 | * If get_erase_value returns a non-negative byte value, the underlying |
borlanic | 0:fbdae7e6d805 | 215 | * storage is set to that value when erased, and storage containing |
borlanic | 0:fbdae7e6d805 | 216 | * that value can be programmed without another erase. |
borlanic | 0:fbdae7e6d805 | 217 | * |
borlanic | 0:fbdae7e6d805 | 218 | * @return The value of storage when erased, or -1 if you can't |
borlanic | 0:fbdae7e6d805 | 219 | * rely on the value of erased storage |
borlanic | 0:fbdae7e6d805 | 220 | */ |
borlanic | 0:fbdae7e6d805 | 221 | virtual int get_erase_value() const; |
borlanic | 0:fbdae7e6d805 | 222 | |
borlanic | 0:fbdae7e6d805 | 223 | /** Get the total size of the underlying device |
borlanic | 0:fbdae7e6d805 | 224 | * |
borlanic | 0:fbdae7e6d805 | 225 | * @return Size of the underlying device in bytes |
borlanic | 0:fbdae7e6d805 | 226 | */ |
borlanic | 0:fbdae7e6d805 | 227 | virtual bd_size_t size() const; |
borlanic | 0:fbdae7e6d805 | 228 | |
borlanic | 0:fbdae7e6d805 | 229 | /** Get the offset of the partition on the underlying block device |
borlanic | 0:fbdae7e6d805 | 230 | * @return Offset of the partition on the underlying device |
borlanic | 0:fbdae7e6d805 | 231 | */ |
borlanic | 0:fbdae7e6d805 | 232 | virtual bd_addr_t get_partition_start() const; |
borlanic | 0:fbdae7e6d805 | 233 | |
borlanic | 0:fbdae7e6d805 | 234 | /** Get size of partition on underlying block device |
borlanic | 0:fbdae7e6d805 | 235 | * @return Size of the partition on the underlying device |
borlanic | 0:fbdae7e6d805 | 236 | */ |
borlanic | 0:fbdae7e6d805 | 237 | virtual bd_addr_t get_partition_stop() const; |
borlanic | 0:fbdae7e6d805 | 238 | |
borlanic | 0:fbdae7e6d805 | 239 | /** Get 8-bit type of the partition |
borlanic | 0:fbdae7e6d805 | 240 | * @return 8-bit type of partition assigned during format |
borlanic | 0:fbdae7e6d805 | 241 | */ |
borlanic | 0:fbdae7e6d805 | 242 | virtual uint8_t get_partition_type() const; |
borlanic | 0:fbdae7e6d805 | 243 | |
borlanic | 0:fbdae7e6d805 | 244 | /** Get the partition number |
borlanic | 0:fbdae7e6d805 | 245 | * @return The partition number, 1-4 |
borlanic | 0:fbdae7e6d805 | 246 | */ |
borlanic | 0:fbdae7e6d805 | 247 | virtual int get_partition_number() const; |
borlanic | 0:fbdae7e6d805 | 248 | |
borlanic | 0:fbdae7e6d805 | 249 | protected: |
borlanic | 0:fbdae7e6d805 | 250 | BlockDevice *_bd; |
borlanic | 0:fbdae7e6d805 | 251 | bd_size_t _offset; |
borlanic | 0:fbdae7e6d805 | 252 | bd_size_t _size; |
borlanic | 0:fbdae7e6d805 | 253 | uint8_t _type; |
borlanic | 0:fbdae7e6d805 | 254 | uint8_t _part; |
borlanic | 0:fbdae7e6d805 | 255 | }; |
borlanic | 0:fbdae7e6d805 | 256 | |
borlanic | 0:fbdae7e6d805 | 257 | |
borlanic | 0:fbdae7e6d805 | 258 | #endif |