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

MBRBlockDevice

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.

MBRBlockDevice class reference

Public Member Functions
 MBRBlockDevice (BlockDevice *bd, int part)
virtual ~MBRBlockDevice ()
virtual int init ()
virtual int deinit ()
virtual int read (void *buffer, bd_addr_t addr, bd_size_t size)
virtual int program (const void *buffer, bd_addr_t addr, bd_size_t size)
virtual int erase (bd_addr_t addr, bd_size_t size)
virtual bd_size_t get_read_size () const
virtual bd_size_t get_program_size () const
virtual bd_size_t get_erase_size () const
virtual bd_size_t size () const
virtual bd_addr_t get_partition_start () const
virtual bd_addr_t get_partition_stop () const
virtual uint8_t get_partition_type () const
virtual int get_partition_number () const
 Public Member Functions inherited from BlockDevice
virtual ~BlockDevice ()
virtual int trim (bd_addr_t addr, bd_size_t size)
bool is_valid_read (bd_addr_t addr, bd_size_t size) const
bool is_valid_program (bd_addr_t addr, bd_size_t size) const
bool is_valid_erase (bd_addr_t addr, bd_size_t size) const
Static Public Member Functions
static int partition (BlockDevice *bd, int part, uint8_t type, bd_addr_t start)
static int partition (BlockDevice *bd, int part, uint8_t type, bd_addr_t start, bd_addr_t stop)
Protected Attributes
BlockDevice_bd
bd_size_t _offset
bd_size_t _size
uint8_t _type
uint8_t _part

MBRBlockDevice example

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

#include "mbed.h"
#include "HeapBlockDevice.h"
#include "MBRBlockDevice.h"
    
int main(void) {
    // Create a block device with 64 blocks of size 512
    HeapBlockDevice mem(64*512, 512);
    
    // Partition into two partitions with ~half the blocks
    MBRBlockDevice::partition(&mem, 1, 0x83, 0*512, 32*512);
    MBRBlockDevice::partition(&mem, 2, 0x83, 32*512);
    
    // Create a block device that maps to the first 32 blocks (excluding MBR block)
    MBRBlockDevice part1(&mem, 1);
    
    // Create a block device that maps to the last 32 blocks
    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.

#include "mbed.h"
#include "SDBlockDevice.h"
#include "MBRBlockDevice.h"
#include "FATFileSystem.h"
    
// Pin mappings for K64F
PinName s0 = PTE3;  // MOSI
PinName s1 = PTE1;  // MISO
PinName s2 = PTE2;  // SCLK
PinName s3 = PTE4;  // CS
    
int main(void) {
    // Create an SD card
    SDBlockDevice sd(s0, s1, s2, s3);
    
    // Create a partition with 1 GB of space
    MBRBlockDevice::partition(&sd, 1, 0x83, 0, 1024*1024);
    
    // Create the block device that represents the partition
    MBRBlockDevice part1(&sd, 1);
    
    // Format the partition with a FAT filesystem
    FATFileSystem::format(&part1);
    
    // Create the FAT filesystem instance, files can now be written to
    // the FAT filesystem in partition 1
    FATFileSystem fat("fat", &part1);
}
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.