Skip to content

Commit 01db819

Browse files
feat: flag to create pmbr marked as bootable (#132)
We're using gptman to build a bootable disk image, that needs to boot on both BIOS and UEFI systems. We've found that some legacy BIOS systems don't consider a disk to be bootable unless the bootable flag is set on the [PMBR](https://www.rodsbooks.com/gdisk/bios.html).
1 parent f01d5b6 commit 01db819

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/lib.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1231,15 +1231,48 @@ impl GPT {
12311231

12321232
/// This function writes a protective MBR in the first sector of the disk
12331233
/// starting at byte 446 and ending at byte 511. Any existing data will be overwritten.
1234+
///
1235+
/// See also: [`Self::write_bootable_protective_mbr_into`].
12341236
pub fn write_protective_mbr_into<W: ?Sized>(mut writer: &mut W, sector_size: u64) -> Result<()>
1237+
where
1238+
W: Write + Seek,
1239+
{
1240+
Self::write_protective_mbr_into_impl(&mut writer, sector_size, false)
1241+
}
1242+
1243+
/// This function writes a protective MBR in the first sector of the disk
1244+
/// starting at byte 446 and ending at byte 511. Any existing data will be overwritten.
1245+
/// This function differs from [`Self::write_protective_mbr_into`] in that the partition in the
1246+
/// MBR partition table is marked as bootable. Some legacy BIOS systems do not consider a disk
1247+
/// to be bootable if there isn't an MBR partition marked as bootable in the MBR partition
1248+
/// table.
1249+
pub fn write_bootable_protective_mbr_into<W: ?Sized>(
1250+
mut writer: &mut W,
1251+
sector_size: u64,
1252+
) -> Result<()>
1253+
where
1254+
W: Write + Seek,
1255+
{
1256+
Self::write_protective_mbr_into_impl(&mut writer, sector_size, true)
1257+
}
1258+
1259+
fn write_protective_mbr_into_impl<W: ?Sized>(
1260+
mut writer: &mut W,
1261+
sector_size: u64,
1262+
bootable: bool,
1263+
) -> Result<()>
12351264
where
12361265
W: Write + Seek,
12371266
{
12381267
let size = writer.seek(SeekFrom::End(0))? / sector_size - 1;
12391268
writer.seek(SeekFrom::Start(446))?;
12401269
// partition 1
1270+
if bootable {
1271+
writer.write_all(&[0x80])?;
1272+
} else {
1273+
writer.write_all(&[0x00])?;
1274+
}
12411275
writer.write_all(&[
1242-
0x00, // status
12431276
0x00, 0x02, 0x00, // CHS address of first absolute sector
12441277
0xee, // partition type
12451278
0xff, 0xff, 0xff, // CHS address of last absolute sector

0 commit comments

Comments
 (0)