GizStackGizStack
View .mdOpen in ChatGPTOpen in Claude

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.

gizlang
import * as Gpt from "gizlang/gpt";

Types

Partition

gizlang
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: i64
  • kind: bytes
  • unique: bytes
  • first: i64
  • last: i64

Table

gizlang
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: bytes
  • entries: bytes
  • sectors: i64
  • partitions: Vec<gizlang_gpt.Partition>

Gap

gizlang
type Gap = { first: i64; last: i64; }

A run of sectors: its first and last (first > last when it is empty).

  • first: i64
  • last: i64

Functions

guid

gizlang
function guid(text: string): bytes

A GUID's 16 bytes as a table stores them: its first three fields little-endian.

Effects: fail, alloc.

ofType

gizlang
function ofType(table: Table, kind: string): Vec<Partition>

The partitions of TABLE of type KIND.

Effects: fail, alloc.

read

gizlang
function read(disk: Stream, sectors: i64): Option<Table> throws string

The 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

gizlang
function blank(sectors: i64): Table

A new table on a disk of SECTORS: its header (before the CRCs) and empty entries.

Effects: fail, io, alloc.

added

gizlang
function added(table: Table, kind: string, first: i64, last: i64, name: string): Table throws string

TABLE with a partition of KIND from FIRST to LAST in its first empty entry, named NAME.

Effects: fail, read, write, io, alloc.

largestGap

gizlang
function largestGap(table: Table): Gap

The largest free run of TABLE's usable sectors, aligned.

Effects: fail, alloc.

write

gizlang
function write(disk: Stream, table: Table, blankDisk: bool): void throws IoError

Write TABLE to the disk: a protective MBR first on a new one, then both copies.

Effects: fail, io, alloc.

Constants

SECTOR

gizlang
const SECTOR = 512

The bytes of a sector.

ALIGN

gizlang
const ALIGN = 2048

The sectors a partition starts on a multiple of: a MiB, as Linux's tools place them.

ESP

gizlang
const ESP = "C12A7328-F81F-11D2-BA4B-00A0C93EC93B"

The EFI system partition's type.