Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

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 
00023 /** \addtogroup storage */
00024 /** @{*/
00025 
00026 #ifndef MBED_MBR_BLOCK_DEVICE_H
00027 #define MBED_MBR_BLOCK_DEVICE_H
00028 
00029 #include "BlockDevice.h"
00030 
00031 namespace mbed {
00032 
00033 /** Additional error codes used for MBR records
00034  */
00035 enum {
00036     BD_ERROR_INVALID_MBR       = -3101,
00037     BD_ERROR_INVALID_PARTITION = -3102,
00038 };
00039 
00040 
00041 /** Block device for managing a Master Boot Record
00042  *  https://en.wikipedia.org/wiki/Master_boot_record
00043  *
00044  *  @note
00045  *  The MBR partition table is relatively limited:
00046  *  - At most 4 partitions are supported
00047  *  - Extended partitions are currently not supported and will error during init
00048  */
00049 class MBRBlockDevice : public BlockDevice {
00050 public:
00051     /** Format the MBR to contain the following partition
00052      *
00053      *  @param bd       Block device to partition
00054      *  @param part     Partition to use, 1-4
00055      *  @param type     8-bit partition type to identify partition contents
00056      *  @param start    Start block address to map to block 0 of partition.
00057      *                  Negative addresses are calculated from the end of the
00058      *                  underlying block devices. Block 0 is implicitly ignored
00059      *                  from the range to store the MBR.
00060      *  @return         0 on success or a negative error code on failure.
00061      *  @note This is the same as partition(bd, part, type, start, bd->size())
00062      */
00063     static int partition(BlockDevice *bd, int part, uint8_t type, bd_addr_t start);
00064 
00065     /** Format the MBR to contain the following partition
00066      *
00067      *  @param bd       Block device to partition
00068      *  @param part     Partition to use, 1-4
00069      *  @param type     8-bit partition type to identify partition contents
00070      *  @param start    Start block address to map to block 0 of partition,
00071      *                  negative addresses are calculated from the end of the
00072      *                  underlying block devices. Block 0 is implicitly ignored
00073      *                  from the range to store the MBR.
00074      *  @param stop     End block address to mark the end of the partition.
00075      *                  This block is not mapped: negative addresses are calculated
00076      *                  from the end of the underlying block device.
00077      *  @return         0 on success or a negative error code on failure.
00078      */
00079     static int partition(BlockDevice *bd, int part, uint8_t type, bd_addr_t start, bd_addr_t stop);
00080 
00081     /** Lifetime of the block device
00082      *
00083      *  @param bd       Block device to back the MBRBlockDevice
00084      *  @param part     Partition to use, 1-4
00085      */
00086     MBRBlockDevice(BlockDevice *bd, int part);
00087 
00088     /** Lifetime of the block device
00089      */
00090     virtual ~MBRBlockDevice() {};
00091 
00092     /** Initialize a block device
00093      *
00094      *  @return         0 on success or a negative error code on failure
00095      */
00096     virtual int init();
00097 
00098     /** Deinitialize a block device
00099      *
00100      *  @return         0 on success or a negative error code on failure
00101      */
00102     virtual int deinit();
00103 
00104     /** Ensure data on storage is in sync with the driver
00105      *
00106      *  @return         0 on success or a negative error code on failure
00107      */
00108     virtual int sync();
00109 
00110     /** Read blocks from a block device
00111      *
00112      *  @param buffer   Buffer to read blocks into
00113      *  @param addr     Address of block to begin reading from
00114      *  @param size     Size to read in bytes, must be a multiple of read block size
00115      *  @return         0 on success or a negative error code on failure
00116      */
00117     virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
00118 
00119     /** Program blocks to a block device
00120      *
00121      *  The blocks must have been erased prior to being programmed
00122      *
00123      *  @param buffer   Buffer of data to write to blocks
00124      *  @param addr     Address of block to begin writing to
00125      *  @param size     Size to write in bytes, must be a multiple of program block size
00126      *  @return         0 on success or a negative error code on failure
00127      */
00128     virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
00129 
00130     /** Erase blocks on a block device
00131      *
00132      *  The state of an erased block is undefined until it has been programmed,
00133      *  unless get_erase_value returns a non-negative byte value
00134      *
00135      *  @param addr     Address of block to begin erasing
00136      *  @param size     Size to erase in bytes, must be a multiple of erase block size
00137      *  @return         0 on success or a negative error code on failure
00138      */
00139     virtual int erase(bd_addr_t addr, bd_size_t size);
00140 
00141     /** Get the size of a readable block
00142      *
00143      *  @return         Size of a readable block in bytes
00144      */
00145     virtual bd_size_t get_read_size() const;
00146 
00147     /** Get the size of a programmable block
00148      *
00149      *  @return         Size of a programmable block in bytes
00150      *  @note Must be a multiple of the read size
00151      */
00152     virtual bd_size_t get_program_size() const;
00153 
00154     /** Get the size of an erasable block
00155      *
00156      *  @return         Size of an erasable block in bytes
00157      *  @note Must be a multiple of the program size
00158      */
00159     virtual bd_size_t get_erase_size() const;
00160 
00161     /** Get the size of an erasable block given address
00162      *
00163      *  @param addr     Address within the erasable block
00164      *  @return         Size of an erasable block in bytes
00165      *  @note Must be a multiple of the program size
00166      */
00167     virtual bd_size_t get_erase_size(bd_addr_t addr) const;
00168 
00169     /** Get the value of storage when erased
00170      *
00171      *  If get_erase_value returns a non-negative byte value, the underlying
00172      *  storage is set to that value when erased, and storage containing
00173      *  that value can be programmed without another erase.
00174      *
00175      *  @return         The value of storage when erased, or -1 if you can't
00176      *                  rely on the value of erased storage
00177      */
00178     virtual int get_erase_value() const;
00179 
00180     /** Get the total size of the underlying device
00181      *
00182      *  @return         Size of the underlying device in bytes
00183      */
00184     virtual bd_size_t size() const;
00185 
00186     /** Get the offset of the partition on the underlying block device
00187      *  @return         Offset of the partition on the underlying device
00188      */
00189     virtual bd_addr_t get_partition_start() const;
00190 
00191     /** Get size of partition on underlying block device
00192      *  @return         Size of the partition on the underlying device
00193      */
00194     virtual bd_addr_t get_partition_stop() const;
00195 
00196     /** Get 8-bit type of the partition
00197      *  @return         8-bit type of partition assigned during format
00198      */
00199     virtual uint8_t get_partition_type() const;
00200 
00201     /** Get the partition number
00202      *  @return         The partition number, 1-4
00203      */
00204     virtual int get_partition_number() const;
00205 
00206     /** Get the BlockDevice class type.
00207      *
00208      *  @return         A string represent the BlockDevice class type.
00209      */
00210     virtual const char *get_type() const;
00211 
00212 protected:
00213     BlockDevice *_bd;
00214     bd_size_t _offset;
00215     bd_size_t _size;
00216     uint8_t _type;
00217     uint8_t _part;
00218     uint32_t _init_ref_count;
00219     bool _is_initialized;
00220 };
00221 
00222 } // namespace mbed
00223 
00224 // Added "using" for backwards compatibility
00225 #ifndef MBED_NO_GLOBAL_USING_DIRECTIVE
00226 using mbed::MBRBlockDevice;
00227 #endif
00228 
00229 #endif
00230 
00231 /** @}*/