gizlang/gpt
The GUID partition table (UEFI specification, chapter 5) of a disk or a disk image: read, checked, changed and written. An installer that partitions a drive and a tool that makes a disk image use the same table.
import * as Gpt from "gizlang/gpt";Types
Partition
type Partition = { slot: i64; kind: bytes; unique: bytes; first: i64; last: i64; }A partition of a GUID table: its entry's index (its number less one), its type and unique GUIDs (16 bytes as stored), and its first and last sectors.
slot:i64kind:bytesunique:bytesfirst:i64last:i64
Table
type Table = { header: bytes; entries: bytes; sectors: i64; partitions: Vec<Partition>; }A disk's GUID table: the header and entries of its primary copy, and the disk's sectors.
header:bytesentries:bytessectors:i64partitions:Vec<gizlang_gpt.Partition>
Gap
type Gap = { first: i64; last: i64; }A run of sectors: its first and last (first > last when it is empty).
first:i64last:i64
Functions
guid
function guid(text: string): bytesA GUID's 16 bytes as a table stores them: its first three fields little-endian.
Effects: fail, alloc.
ofType
function ofType(table: Table, kind: string): Vec<Partition>The partitions of TABLE of type KIND.
Effects: fail, alloc.
read
function read(disk: Stream, sectors: i64): Option<Table> throws stringThe disk's GUID table, or none: a disk without one is blank only if it holds no MBR partition. A table made for a smaller disk (a disk image written to a larger drive) is read as one of this disk's SECTORS: its backup copy and last usable sector move to the disk's end when it is written. A table that fails its checks (the CRC-32 of its header and of its entries, the entries' count and size, each partition inside the usable sectors, no two partitions shared) is refused: it throws, and nothing is written over it.
Effects: fail, read, io, alloc.
blank
function blank(sectors: i64): TableA new table on a disk of SECTORS: its header (before the CRCs) and empty entries.
Effects: fail, io, alloc.
added
function added(table: Table, kind: string, first: i64, last: i64, name: string): Table throws stringTABLE with a partition of KIND from FIRST to LAST in its first empty entry, named NAME.
Effects: fail, read, write, io, alloc.
largestGap
function largestGap(table: Table): GapThe largest free run of TABLE's usable sectors, aligned.
Effects: fail, alloc.
write
function write(disk: Stream, table: Table, blankDisk: bool): void throws IoErrorWrite TABLE to the disk: a protective MBR first on a new one, then both copies.
Effects: fail, io, alloc.
Constants
SECTOR
const SECTOR = 512The bytes of a sector.
ALIGN
const ALIGN = 2048The sectors a partition starts on a multiple of: a MiB, as Linux's tools place them.
ESP
const ESP = "C12A7328-F81F-11D2-BA4B-00A0C93EC93B"The EFI system partition's type.