- OCaml 76%
- C 24%
gpt #
GUID Partition Table reading and writing in pure OCaml.
Building a bootable disk image means laying out partitions in the format UEFI
firmware reads. An LBA (logical block address) is the index of a sector,
counted from 0 at the start of the disk. LBA 0 holds a protective MBR, a
legacy Master Boot Record that declares one partition of type 0xEE covering
the disk, so a tool that knows only MBR sees the disk as in use rather than
empty. LBA 1 holds the 92-byte GPT header, an array of 128 partition entries
of 128 bytes follows it, and a backup copy of both sits at the end of the
disk. The header stores two CRC-32 checksums: one over the header itself,
computed with its own checksum field set to zero, and one over the whole
partition-entry array. A reader recomputes both and refuses a mismatch, and
UEFI firmware that finds the primary copy damaged boots from the backup
(UEFI 2.10, section 5.3.2). gpt builds, encodes and decodes that
layout for 512-byte and 4096-byte sectors, validates partitions against the
usable range, and produces the matching protective MBR. It also reads and
writes the ChromeOS cgpt priority bits (Gpt.Gptprio) that A/B update
schemes store in a partition's attributes, and converts partition names to and
from their UTF-16LE on-disk form.
Installation #
Install with opam:
$ opam install nox-gpt
If opam cannot find the package, it may not yet be released in the public
opam-repository. Add the overlay repository, then install it:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install nox-gpt
Usage #
Creating partitions and a GPT #
Gpt.Partition.v takes the first and last LBA of a partition as positional
arguments. Gpt.v checks the partitions against the disk geometry and
returns an error if one overlaps another or falls outside the usable range.
let type_guid =
Uuidm.of_string "C12A7328-F81F-11D2-BA4B-00A0C93EC93B" |> Option.get
let part =
match Gpt.Partition.v ~type_guid ~attributes:0L 2048L 1048575L with
| Ok p -> p
| Error e -> failwith e
let gpt =
match Gpt.v ~disk_sectors:2097152L ~sector_size:512 [ part ] with
| Ok g -> g
| Error e -> failwith e
let () = Fmt.pr "%a@." Gpt.pp gpt
Without ~partition_guid and ~disk_guid, both GUIDs are fresh random ones,
so pass them explicitly to build reproducible images.
Reading from bytes #
Gpt.of_string decodes the header and returns where the partition table is,
with a continuation that decodes it once the caller has read those sectors:
let parse_gpt ~read_sectors header_bytes =
match Gpt.of_string header_bytes ~sector_size:512 with
| Error e -> failwith e
| Ok (`Read_partition_table (lba, num_sectors), k) ->
let table_bytes = read_sectors lba num_sectors in
(match k table_bytes with
| Ok gpt -> gpt
| Error e -> failwith e)
Streaming I/O with bytesrw #
let read_gpt reader =
match Gpt.read reader ~sector_size:512 with
| Ok gpt -> gpt
| Error e -> failwith e
let write_gpt writer gpt =
Gpt.write_header writer ~sector_size:512 ~primary:true gpt;
Gpt.write_partition_table writer ~sector_size:512 gpt
Protective MBR #
let mbr = Gpt.protective_mbr ~sector_size:512 gpt
API #
Gpt.Partition: partition entries (v,is_zero_partition,encode_name,decode_name, and the 128-bytecodec).Gpt.Gptprio: A/B boot priority, remaining tries and success flag, packed into a partition's attribute bits.Gpt.v,Gpt.ppandGpt.first_usable_lba: building a GPT and reading its geometry.Gpt.bytes_of_marshal_headerandGpt.string_of_marshal_partition_table: the primary or backup header sector and the partition table as strings.Gpt.of_string,Gpt.read,Gpt.write_headerandGpt.write_partition_table: decoding and encoding with bytesrw.Gpt.header_codec: the wire codec for the 92-byte header.Gpt.protective_mbr: the LBA 0 record for a GPT disk.
References #
- UEFI Specification 2.10, chapter 5, "GUID Partition Table (GPT) Disk Layout".
License #
ISC. See LICENSE.md.