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

A classic MBR is exactly one 512-byte disk sector: 446 bytes of bootstrap code, four fixed-size 16-byte partition table entries, and a 2-byte boot signature:

 0                                                       445
+-----------------------------------------------------------+
|              Bootstrap Code (446 bytes)                   |
+-----------------------------------------------------------+
|            Partition Entry 1 (16 bytes)                   |
+-----------------------------------------------------------+
|            Partition Entry 2 (16 bytes)                   |
+-----------------------------------------------------------+
|            Partition Entry 3 (16 bytes)                   |
+-----------------------------------------------------------+
|            Partition Entry 4 (16 bytes)                   |
+-----------------------------------------------------------+
|         Boot Signature (0xaa55, 2 bytes)                  |
+-----------------------------------------------------------+

Each partition entry is laid out as:

 0        1        2   3   4        5        6   7
+--------+--------+--------+--------+--------+--------+
| Status |   CHS First (3) | Type   |   CHS Last (3)   |
+--------+--------+--------+--------+--------+--------+
|          LBA First Sector         |     Sector Count |
+--------+--------+--------+--------+--------+--------+

All multi-byte numeric fields are little-endian, matching the format's origin on x86 hardware.

Scope for v0.0.1 is deliberately shallow: this coder covers the sector-level header only. The CHS (cylinder-head-sector) fields are kept as raw 3-byte slices rather than decoded into cylinder/head/sector numbers — CHS addressing has been obsolete since LBA became universal, and decoding it correctly requires bit-packing rules (10-bit cylinder split across two bytes) that add complexity with no practical benefit for modern disks. Extended partitions (nested MBRs referenced by a 0x05/0x0f entry) and GPT protective-MBR semantics are also left to the caller — this coder only describes the fixed 512-byte wire layout.

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);