Kenji Arai / TYBLE16_mbedlized_os5_several_examples_1st

Dependencies:   nRF51_Vdd TextLCD BME280

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MBRBlockDevice.h Source File

MBRBlockDevice.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2017 ARM Limited
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  */
00022 #ifndef MBED_MBR_BLOCK_DEVICE_H
00023 #define MBED_MBR_BLOCK_DEVICE_H
00024 
00025 #include "BlockDevice.h"
00026 
00027 
00028 /** Additional error codes used for MBR records
00029  */
00030 enum {
00031     BD_ERROR_INVALID_MBR       = -3101,
00032     BD_ERROR_INVALID_PARTITION = -3102,
00033 };
00034 
00035 
00036 /** Block device for managing a Master Boot Record
00037  *  https://en.wikipedia.org/wiki/Master_boot_record
00038  *
00039  *  Here is an example of partitioning a heap backed block device
00040  *  @code
00041  *  #include "mbed.h"
00042  *  #include "HeapBlockDevice.h"
00043  *  #include "MBRBlockDevice.h"
00044  *
00045  *  // Create a block device with 64 blocks of size 512
00046  *  HeapBlockDevice mem(64*512, 512);
00047  *
00048  *  // Partition into two partitions with ~half the blocks
00049  *  MBRBlockDevice::partition(&mem, 1, 0x83, 0*512, 32*512);
00050  *  MBRBlockDevice::partition(&mem, 2, 0x83, 32*512);
00051  *
00052  *  // Create a block device that maps to the first 32 blocks (excluding MBR block)
00053  *  MBRBlockDevice part1(&mem, 1);
00054  *
00055  *  // Create a block device that maps to the last 32 blocks
00056  *  MBRBlockDevice part2(&mem, 2);
00057  *  @endcode
00058  *
00059  *  Here is a more realistic example where the MBRBlockDevice is used
00060  *  to partition a region of space on an SD card. When plugged into a computer,
00061  *  the partitions will be recognized appropriately.
00062  *  @code
00063  *  #include "mbed.h"
00064  *  #include "SDBlockDevice.h"
00065  *  #include "MBRBlockDevice.h"
00066  *  #include "FATFileSystem.h"
00067  *
00068  *  // Create an SD card
00069  *  SDBlockDevice sd(s0, s1, s2, s3);
00070  *
00071  *  // Create a partition with 1 GB of space
00072  *  MBRBlockDevice::partition(&sd, 1, 0x83, 0, 1024*1024);
00073  *
00074  *  // Create the block device that represents the partition
00075  *  MBRBlockDevice part1(&sd, 1);
00076  *
00077  *  // Format the partition with a FAT filesystem
00078  *  FATFileSystem::format(&part1);
00079  *
00080  *  // Create the FAT filesystem instance, files can now be written to
00081  *  // the FAT filesystem in partition 1
00082  *  FATFileSystem fat("fat", &part1);
00083  *  @endcode
00084  *
00085  *  @note
00086  *  The MBR partition table is relatively limited:
00087  *  - At most 4 partitions are supported
00088  *  - Extended partitions are currently not supported and will error during init
00089  */
00090 class MBRBlockDevice : public BlockDevice
00091 {
00092 public:
00093     /** Format the MBR to contain the following partition
00094      *
00095      *  @param bd       Block device to partition
00096      *  @param part     Partition to use, 1-4
00097      *  @param type     8-bit partition type to identitfy partition's contents
00098      *  @param start    Start block address to map to block 0 of partition, 
00099      *                  negative addresses are calculated from the end of the
00100      *                  underlying block devices. Block 0 is implicitly ignored
00101      *                  from the range to store the MBR.
00102      *  @return         0 on success or a negative error code on failure
00103      *  @note This is the same as partition(bd, part, type, start, bd->size())
00104      */
00105     static int partition(BlockDevice *bd, int part, uint8_t type, bd_addr_t start);
00106 
00107     /** Format the MBR to contain the following partition
00108      *
00109      *  @param bd       Block device to partition
00110      *  @param part     Partition to use, 1-4
00111      *  @param type     8-bit partition type to identitfy partition's contents
00112      *  @param start    Start block address to map to block 0 of partition, 
00113      *                  negative addresses are calculated from the end of the
00114      *                  underlying block devices. Block 0 is implicitly ignored
00115      *                  from the range to store the MBR.
00116      *  @param stop     End block address to mark the end of the partition,
00117      *                  this block is not mapped, negative addresses are calculated
00118      *                  from the end of the underlying block device.
00119      *  @return         0 on success or a negative error code on failure
00120      */
00121     static int partition(BlockDevice *bd, int part, uint8_t type, bd_addr_t start, bd_addr_t stop);
00122 
00123     /** Lifetime of the block device
00124      *
00125      *  @param bd       Block device to back the MBRBlockDevice
00126      *  @param part     Partition to use, 1-4
00127      */
00128     MBRBlockDevice(BlockDevice *bd, int part);
00129 
00130     /** Lifetime of the block device
00131      */
00132     virtual ~MBRBlockDevice() {};
00133 
00134     /** Initialize a block device
00135      *
00136      *  @return         0 on success or a negative error code on failure
00137      */
00138     virtual int init();
00139 
00140     /** Deinitialize a block device
00141      *
00142      *  @return         0 on success or a negative error code on failure
00143      */
00144     virtual int deinit();
00145 
00146     /** Ensure data on storage is in sync with the driver
00147      *
00148      *  @return         0 on success or a negative error code on failure
00149      */
00150     virtual int sync();
00151 
00152     /** Read blocks from a block device
00153      *
00154      *  @param buffer   Buffer to read blocks into
00155      *  @param addr     Address of block to begin reading from
00156      *  @param size     Size to read in bytes, must be a multiple of read block size
00157      *  @return         0 on success, negative error code on failure
00158      */
00159     virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
00160 
00161     /** Program blocks to a block device
00162      *
00163      *  The blocks must have been erased prior to being programmed
00164      *
00165      *  @param buffer   Buffer of data to write to blocks
00166      *  @param addr     Address of block to begin writing to
00167      *  @param size     Size to write in bytes, must be a multiple of program block size
00168      *  @return         0 on success, negative error code on failure
00169      */
00170     virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
00171 
00172     /** Erase blocks on a block device
00173      *
00174      *  The state of an erased block is undefined until it has been programmed,
00175      *  unless get_erase_value returns a non-negative byte value
00176      *
00177      *  @param addr     Address of block to begin erasing
00178      *  @param size     Size to erase in bytes, must be a multiple of erase block size
00179      *  @return         0 on success, negative error code on failure
00180      */
00181     virtual int erase(bd_addr_t addr, bd_size_t size);
00182 
00183     /** Get the size of a readable block
00184      *
00185      *  @return         Size of a readable block in bytes
00186      */
00187     virtual bd_size_t get_read_size() const;
00188 
00189     /** Get the size of a programmable block
00190      *
00191      *  @return         Size of a programmable block in bytes
00192      *  @note Must be a multiple of the read size
00193      */
00194     virtual bd_size_t get_program_size() const;
00195 
00196     /** Get the size of an erasable block
00197      *
00198      *  @return         Size of an erasable block in bytes
00199      *  @note Must be a multiple of the program size
00200      */
00201     virtual bd_size_t get_erase_size() const;
00202 
00203     /** Get the size of an erasable block given address
00204      *
00205      *  @param addr     Address within the erasable block
00206      *  @return         Size of an erasable block in bytes
00207      *  @note Must be a multiple of the program size
00208      */
00209     virtual bd_size_t get_erase_size(bd_addr_t addr) const;
00210 
00211     /** Get the value of storage when erased
00212      *
00213      *  If get_erase_value returns a non-negative byte value, the underlying
00214      *  storage is set to that value when erased, and storage containing
00215      *  that value can be programmed without another erase.
00216      *
00217      *  @return         The value of storage when erased, or -1 if you can't
00218      *                  rely on the value of erased storage
00219      */
00220     virtual int get_erase_value() const;
00221 
00222     /** Get the total size of the underlying device
00223      *
00224      *  @return         Size of the underlying device in bytes
00225      */
00226     virtual bd_size_t size() const;
00227 
00228     /** Get the offset of the partition on the underlying block device
00229      *  @return         Offset of the partition on the underlying device
00230      */
00231     virtual bd_addr_t get_partition_start() const;
00232 
00233     /** Get size of partition on underlying block device
00234      *  @return         Size of the partition on the underlying device
00235      */
00236     virtual bd_addr_t get_partition_stop() const;
00237 
00238     /** Get 8-bit type of the partition
00239      *  @return         8-bit type of partition assigned during format
00240      */
00241     virtual uint8_t get_partition_type() const;
00242 
00243     /** Get the partition number
00244      *  @return         The partition number, 1-4
00245      */
00246     virtual int get_partition_number() const;
00247 
00248 protected:
00249     BlockDevice *_bd;
00250     bd_size_t _offset;
00251     bd_size_t _size;
00252     uint8_t _type;
00253     uint8_t _part;
00254     uint32_t _init_ref_count;
00255     bool _is_initialized;
00256 };
00257 
00258 
00259 #endif