#mbr #parser #raspberry-pi #ms-dos #master-boot-record

no-std mbrs

Master boot record parsing, manipulation and binary export

9 releases

0.3.1 Sep 26, 2023
0.3.0 Sep 26, 2023
0.2.4 Sep 21, 2023
0.1.1 Sep 21, 2023

#781 in Parser implementations

Download history 4/week @ 2024-02-22 2/week @ 2024-02-29 2/week @ 2024-03-14 39/week @ 2024-03-28 19/week @ 2024-04-04

58 downloads per month

MIT/Apache

31KB
775 lines

mbrs

Master boot record parsing, manipulation and binary export.

The MBR is a legacy boot sector format that was for example used on old windows systems and is still being used on raspberry pis.

I wrote this library for personal use because I was annoyed by using parted to work with raspberry pi images, so I mainly paid attention to that usecase. This means not all the different historical MBR structures are supported (for example disk timestamps) and some niche partition type identifiers might not be implemented. Currently we implement the MBR precisely as described on the German wikipedia page. If you encounter a need for other partition types, timestamps or whatever please file an issue (or PR) and I might look into it.

I don't see why you'd ever need it but this crate supports no_std - just disable the std feature that's enabled by default.

Example

let raspios_img = File::open("./raspios.img").unwrap();
let mbr = Mbr::try_from_reader(raspios_img).unwrap();
// print it
dbg!(mbr);
// read out the drive signature
let partuuid = format!("{:x}", u32::from_le_bytes(mbr.drive_signature));
println!("PARTUUID: {}", partuuid);

// read the MBR off a raspberry pi image file
let raspios_img = File::open("./raspios.img").unwrap();
let mut mbr = Mbr::try_from_reader(raspios_img).unwrap();
// modify the drive signature
mbr.drive_signature = 0x090b3d33_u32.to_le_bytes();
// add a new BTRFS 1024 sector big partition 
mbr.partition_table.entries[2] = Some(
    PartInfo::try_from_lba(
        true,
        mbr.partition_table.entries[1].unwrap().end_sector_lba(),
        1024,
        PartType::btrfs(),
    )
    .unwrap(),
);
// write the modified MBR to a new file
let mut out_file = File::create("./out.img").unwrap();
let buf = <[u8; 512]>::try_from(&mbr).unwrap();
out_file.write_all(&buf).unwrap();

Dependencies

~78–305KB