mbrSector(): Coder<MbrSector>
Creates a coder for a classic 512-byte MBR sector.
Nothing is validated on encode or decode — bootSignature is written and
read exactly as given. Callers that need to confirm a sector is a valid MBR
should compare the decoded bootSignature against
MBR_BOOT_SIGNATURE themselves.
Decode a known-bytes sector with two partitions
Decode a known-bytes sector with two partitions
import { assertEquals } from "@std/assert"; import { mbrSector, MBR_BOOT_SIGNATURE, MBR_PARTITION_TYPE, MBR_SIZE } from "@binstruct/mbr"; const wire = new Uint8Array(MBR_SIZE); // Partition 1: active Linux partition starting at LBA 2048, 1048576 sectors const entry1 = 446; wire[entry1] = 0x80; wire[entry1 + 4] = MBR_PARTITION_TYPE.LINUX; new DataView(wire.buffer).setUint32(entry1 + 8, 2048, true); new DataView(wire.buffer).setUint32(entry1 + 12, 1048576, true); // Boot signature new DataView(wire.buffer).setUint16(510, MBR_BOOT_SIGNATURE, true); const [decoded, read] = mbrSector().decode(wire); assertEquals(read, MBR_SIZE); assertEquals(decoded.partitions.length, 4); assertEquals(decoded.partitions[0].status, 0x80); assertEquals(decoded.partitions[0].partitionType, MBR_PARTITION_TYPE.LINUX); assertEquals(decoded.partitions[0].lbaFirstSector, 2048); assertEquals(decoded.partitions[0].sectorCount, 1048576); assertEquals(decoded.partitions[1].partitionType, MBR_PARTITION_TYPE.EMPTY); assertEquals(decoded.bootSignature, MBR_BOOT_SIGNATURE);