upstream: https://github.com/mirage/ocaml-mbr
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

OCaml 58.9%
C 34.2%
PHP 1.9%
Roff 1.2%
Dune 0.9%
Other 2.9%
57 1 0

Clone this repository

https://tangled.org/gazagnaire.org/ocaml-mbr https://tangled.org/did:plc:jhift2vwcxhou52p3sewcrpx/ocaml-mbr
git@git.recoil.org:gazagnaire.org/ocaml-mbr git@git.recoil.org:did:plc:jhift2vwcxhou52p3sewcrpx/ocaml-mbr

For self-hosted knots, clone URLs may differ based on your setup.

Download tar.gz
README.md

mbr#

A library to manipulate Master Boot Records in pure OCaml.

Overview#

Pure OCaml library for reading and writing Master Boot Records (MBR). Supports creating disk images with primary partition tables, CHS geometry translation, and streaming I/O via bytesrw.

Features#

  • MBR header and partition table encoding/decoding
  • Up to 4 primary partitions with overlap and bootable flag validation
  • CHS geometry synthesis for pre-LBA compatibility
  • Wire codecs for zero-copy partition entry access
  • Streaming I/O with bytesrw readers/writers
  • Pretty-printer

Installation#

Install with opam:

$ opam install mbr

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 mbr

Usage#

Creating an MBR#

(* FAT16 (type 6), starting at sector 2048, 32MB. *)
let part =
  match Mbr.Partition.make ~active:true ~partition_type:6 2048l 65536l with
  | Ok p -> p
  | Error e -> failwith e

let mbr =
  match Mbr.v [ part ] with
  | Ok m -> m
  | Error e -> failwith e

let () = Fmt.pr "%a@." Mbr.pp mbr

Serialization#

let bytes = Mbr.to_string mbr

let () =
  match Mbr.of_string bytes with
  | Ok mbr ->
      List.iter
        (fun p ->
          Fmt.pr "Partition: start=%Ld, size=%Ld@."
            (Mbr.Partition.sector_start p)
            (Mbr.Partition.size_sectors p))
        (Mbr.partitions mbr)
  | Error e -> failwith e

Streaming I/O with bytesrw#

let read_mbr reader =
  match Mbr.read reader with
  | Ok mbr ->
      Fmt.pr "%a@." Mbr.pp mbr;
      mbr
  | Error e -> failwith e

let write_mbr writer mbr = Mbr.write writer mbr

CHS Geometry#

(* Synthesise geometry for a 4GB disk. *)
let () =
  match Mbr.Geometry.of_lba_size 8388608L with
  | Ok geom ->
      Fmt.pr "C=%d H=%d S=%d@." geom.cylinders geom.heads geom.sectors
  | Error e -> failwith e

API#

Partition#

  • Mbr.Partition.make -- Create a partition (~partition_type:int -> int32 -> int32 -> (t, string) result)
  • Mbr.Partition.make' -- Create a partition with int64 args
  • Mbr.Partition.sector_start -- LBA start as int64
  • Mbr.Partition.size_sectors -- Size in sectors as int64
  • Mbr.Partition.codec -- Wire codec for 16-byte partition entry

MBR#

  • Mbr.v -- Create an MBR with up to 4 partitions (validates overlaps, active flags)
  • Mbr.partitions -- Extract partition list
  • Mbr.pp -- Pretty-print
  • Mbr.sizeof -- Size in bytes (512)
  • Mbr.default_partition_start -- Default first partition offset

Serialization#

  • Mbr.of_string / Mbr.to_string -- String-based I/O
  • Mbr.read / Mbr.write -- Bytesrw streaming I/O

Geometry#

  • Mbr.Geometry.of_lba_size -- Synthesise CHS from sector count
  • Mbr.Geometry.to_chs -- Convert LBA offset to CHS

Requirements#

  • OCaml >= 5.1
  • bytesrw, wire, fmt

Licence#

ISC