Mistake on this page?
Report an issue in GitHub or email us

MBRBlockDevice

MBRBlockDevice class hierarchy

The MBRBlockDevice class provides a way to manage a Master Boot Record (MBR) on a storage device, which allows you to partition the device. Without the MBR, you can still format a storage device with a file system, but including the MBR will allow for future partition modifications.

MBRBlockDevices have the following configurable parameters in the constructor:

  • bd - Block device to back the MBRBlockDevice
  • part - Partition to use, 1-4

You can view more information about the configurable settings and functions in the class reference. To configure this class, please see our BlockDevice configuration documentation.

MBRBlockDevice class reference

Public Member Functions
 MBRBlockDevice (BlockDevice *bd, int part)
 Lifetime of the block device. More...
virtual ~MBRBlockDevice ()
 Lifetime of the block device. More...
virtual int init ()
 Initialize a block device. More...
virtual int deinit ()
 Deinitialize a block device. More...
virtual int sync ()
 Ensure data on storage is in sync with the driver. More...
virtual int read (void *buffer, bd_addr_t addr, bd_size_t size)
 Read blocks from a block device. More...
virtual int program (const void *buffer, bd_addr_t addr, bd_size_t size)
 Program blocks to a block device. More...
virtual int erase (bd_addr_t addr, bd_size_t size)
 Erase blocks on a block device. More...
virtual bd_size_t get_read_size () const
 Get the size of a readable block. More...
virtual bd_size_t get_program_size () const
 Get the size of a programmable block. More...
virtual bd_size_t get_erase_size () const
 Get the size of an erasable block. More...
virtual bd_size_t get_erase_size (bd_addr_t addr) const
 Get the size of an erasable block given address. More...
virtual int get_erase_value () const
 Get the value of storage when erased. More...
virtual bd_size_t size () const
 Get the total size of the underlying device. More...
virtual bd_addr_t get_partition_start () const
 Get the offset of the partition on the underlying block device. More...
virtual bd_addr_t get_partition_stop () const
 Get size of partition on underlying block device. More...
virtual uint8_t get_partition_type () const
 Get 8-bit type of the partition. More...
virtual int get_partition_number () const
 Get the partition number. More...
virtual const char * get_type () const
 Get the BlockDevice class type. More...
virtual int trim (bd_addr_t addr, bd_size_t size)
 Mark blocks as no longer in use. More...
virtual bool is_valid_read (bd_addr_t addr, bd_size_t size) const
 Convenience function for checking block read validity. More...
virtual bool is_valid_program (bd_addr_t addr, bd_size_t size) const
 Convenience function for checking block program validity. More...
virtual bool is_valid_erase (bd_addr_t addr, bd_size_t size) const
 Convenience function for checking block erase validity. More...
Static Public Member Functions
static int partition (BlockDevice *bd, int part, uint8_t type, bd_addr_t start)
 Format the MBR to contain the following partition. More...
static int partition (BlockDevice *bd, int part, uint8_t type, bd_addr_t start, bd_addr_t stop)
 Format the MBR to contain the following partition. More...
static BlockDeviceget_default_instance ()
 Return the default block device. More...

MBRBlockDevice example

Partition a heap backed block device into two partitions. This example also uses the HeapBlockDevice.

/*
 * Copyright (c) 2017-2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"
#include "HeapBlockDevice.h"
#include "MBRBlockDevice.h"

#define BLOCK_SIZE 512

int main(void)
{
    // Create a block device with 64 blocks of size 512
    printf("Create a block device with 64 blocks of size 512\n");
    HeapBlockDevice mem(64 * BLOCK_SIZE, BLOCK_SIZE);

    // Partition into two partitions with ~half the blocks
    printf("Partition into two partitions with ~half the blocks\n");
    MBRBlockDevice::partition(&mem, 1, 0x83, 0 * BLOCK_SIZE, 32 * BLOCK_SIZE);
    MBRBlockDevice::partition(&mem, 2, 0x83, 32 * BLOCK_SIZE);

    // Create a block device that maps to the first 32 blocks (excluding MBR block)
    printf("Create a block device that maps to the first 32 blocks (excluding MBR block)\n");
    MBRBlockDevice part1(&mem, 1);

    // Create a block device that maps to the last 32 blocks
    printf("Create a block device that maps to the last 32 blocks\n");
    MBRBlockDevice part2(&mem, 2);
}

Partition an SD card, and format the new partition with a FAT filesystem. A PC will now be able to recognize the SD card.

/*
 * Copyright (c) 2017-2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"
#include "SDBlockDevice.h"
#include "MBRBlockDevice.h"
#include "FATFileSystem.h"

int main(void)
{
    // Create an SD card
    printf("Creating SD block device\n");
    SDBlockDevice sd(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS);

    // Create a partition with 1 GB of space
    printf("Creating a partition with 1GB of space\n");
    MBRBlockDevice::partition(&sd, 1, 0x83, 0, 1024 * 1024);

    // Create the block device that represents the partition
    printf("Create the block device that represents the partition\n");
    MBRBlockDevice part1(&sd, 1);

    // Format the partition with a FAT filesystem
    printf("Format the partition with a FAT filesystem\n");
    FATFileSystem::format(&part1);

    // Create the FAT filesystem instance, files can now be written to
    // the FAT filesystem in partition 1
    printf("Create the FAT filesystem instance, files can now be written to the FAT filesystem in partition 1\n");
    FATFileSystem fat("fat", &part1);

    printf("To verify that the example worked, read the SD on a Windows machine.\n");
}

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.