default

Master Boot Record (classic MBR) sector encoding and decoding.

Examples

Round-trip a sector with a single FAT32 partition

import { assertEquals } from "@std/assert";
import {
  mbrSector,
  MBR_BOOT_SIGNATURE,
  MBR_PARTITION_TYPE,
  MBR_SIZE,
} from "@binstruct/mbr";

const coder = mbrSector();
const sector = {
  bootstrapCode: new Uint8Array(446),
  partitions: [
    {
      status: 0x80,
      chsFirst: new Uint8Array([0x00, 0x01, 0x00]),
      partitionType: MBR_PARTITION_TYPE.FAT32_LBA,
      chsLast: new Uint8Array([0xfe, 0xff, 0xff]),
      lbaFirstSector: 2048,
      sectorCount: 204800,
    },
    { status: 0, chsFirst: new Uint8Array(3), partitionType: MBR_PARTITION_TYPE.EMPTY, chsLast: new Uint8Array(3), lbaFirstSector: 0, sectorCount: 0 },
    { status: 0, chsFirst: new Uint8Array(3), partitionType: MBR_PARTITION_TYPE.EMPTY, chsLast: new Uint8Array(3), lbaFirstSector: 0, sectorCount: 0 },
    { status: 0, chsFirst: new Uint8Array(3), partitionType: MBR_PARTITION_TYPE.EMPTY, chsLast: new Uint8Array(3), lbaFirstSector: 0, sectorCount: 0 },
  ],
  bootSignature: MBR_BOOT_SIGNATURE,
};

const buffer = new Uint8Array(MBR_SIZE);
const written = coder.encode(sector, buffer);
const [decoded, read] = coder.decode(buffer);

assertEquals(written, MBR_SIZE);
assertEquals(read, MBR_SIZE);
assertEquals(decoded.partitions[0].partitionType, MBR_PARTITION_TYPE.FAT32_LBA);
assertEquals(decoded.partitions[0].sectorCount, 204800);
assertEquals(decoded.bootSignature, MBR_BOOT_SIGNATURE);

Functions

f
mbrSector(): Coder<MbrSector>

Creates a coder for a classic 512-byte MBR sector.

Interfaces

Variables

v
MBR_BOOT_SIGNATURE: 43605

The boot signature that must terminate a valid MBR sector, as a little-endian 16-bit value (bytes 0x55 0xaa on the wire).

v
MBR_PARTITION_COUNT: 4

Number of partition table entries in a classic MBR.

v
MBR_PARTITION_TYPE: { EMPTY: number; NTFS_EXFAT: number; FAT32_LBA: number; LINUX_SWAP: number; LINUX: number; EFI_PROTECTIVE: number; }

Well-known partitionType byte values. Not exhaustive — hundreds of values are assigned across operating systems — but these cover the partitions most commonly encountered today.

v
MBR_SIZE: 512

Total size in bytes of an MBR sector.

v
PARTITION_ENTRY_SIZE: 16

Size in bytes of a single partition table entry.