Master Boot Record (classic MBR) sector encoding and decoding.
Round-trip a sector with a single FAT32 partition
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);
Creates a coder for a classic 512-byte MBR sector.
Decoded MBR partition table entry.
Decoded MBR sector.
The boot signature that must terminate a valid MBR sector, as a
little-endian 16-bit value (bytes 0x55 0xaa on the wire).
Number of partition table entries in a classic MBR.
Well-known partitionType byte values. Not exhaustive — hundreds of values
are assigned across operating systems — but these cover the partitions most
commonly encountered today.
-
EFI_PROTECTIVE: number
GPT protective MBR — the whole disk is one entry covering the GPT.
-
EMPTY: number
Entry unused.
-
FAT32_LBA: number
FAT32 with LBA addressing.
-
LINUX: number
Linux native filesystem (ext2/3/4, etc).
-
LINUX_SWAP: number
Linux swap.
-
NTFS_EXFAT: number
NTFS or exFAT.
Total size in bytes of an MBR sector.
Size in bytes of a single partition table entry.