- OCaml 81%
- C 18%
- Shell 1%
fat #
Warning. This is a work-in-progress experimental fork of ocaml-fat. Do not use it in production; use ocaml-fat instead.
FAT12/FAT16/FAT32 filesystem on Eio block devices, in pure OCaml.
Overview #
Firmware, bootloaders and virtual machines still exchange files on FAT: an
EFI System Partition, a Raspberry Pi boot partition and a cloud-init config
drive are all FAT volumes. Making one usually takes mkfs.vfat and a loop
mount, or mtools, on the build host. nox-fat reads and writes FAT disk
images from OCaml, and mtools, dosfstools, Linux and Windows all read
the images it writes. The on-disk structures (the boot sector, the file
allocation table, and the 8.3 and long-filename directory entries) are
described once with the wire
combinator library and decoded and encoded through bytesrw. The
filesystem sits on the block
Eio device abstraction, so it runs on an in-memory disk, a file or any
other block backend.
The code comes from mirage/ocaml-fat. It was ported from Lwt, cstruct and mirage-block to Eio, wire and block, and FAT12 support was added.
Features #
- Files and directories can be read and written on FAT12, FAT16 and FAT32 volumes.
- A new volume can be formatted with the variant chosen from its cluster count, or with a forced variant, since an EFI System Partition must be FAT32 whatever its size. A cluster is the allocation unit of a FAT volume, a fixed run of sectors, and the file allocation table has one entry per cluster that chains a file's clusters together. The variant depends only on the number of data clusters: fewer than 4,085 is FAT12, fewer than 65,525 is FAT16, and anything above is FAT32 (Microsoft FAT specification, section 3.5). A 64 MiB volume with 4 KiB clusters has about 16,000 of them, a FAT16 count, and reaches a FAT32 count only with 512-byte clusters (about 131,000); this is how a small EFI System Partition is formatted.
- A whole volume can be built in memory from a list of files, with the same bytes every time.
- Names are UTF-8. Long names are stored as UTF-16LE, a run of long entries
names its short entry only when their ordinals and
LDIR_Chksummatch, and names match without regard to case, following section 7 of the Microsoft FAT specification. A path with a segment that no entry can carry, such as..or a name holding*, is refused with`Invalid_pathbefore the device is touched. - The on-disk structures are
wirecodecs, except the FAT32 boot sector, whose extended fields are still written and read at fixed offsets. - I/O is direct-style Eio over the
blockabstraction (in memory, a file, and so on). - The interop tests check that images work with
dosfstoolsandmtools.
Installation #
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam install nox-fat
Usage #
This formats a volume in memory, then creates, writes and reads files:
let () =
let blk = Block.of_memory ~sector_size:512 ~sectors:32768L in
let t = Result.get_ok (Fat.Fs.format blk (Int64.mul 16L 1048576L)) in
assert (Fat.Fs.file t "/HELLO.TXT" = Ok ());
assert (Fat.Fs.write t "/HELLO.TXT" 0 "Hello, FAT world!" = Ok ());
assert (Fat.Fs.mkdir t "/SUB" = Ok ());
assert (Fat.Fs.file t "/SUB/INNER.DAT" = Ok ());
assert (Fat.Fs.write t "/SUB/INNER.DAT" 0 "nested" = Ok ());
assert (Fat.Fs.read t "/HELLO.TXT" 0 5 = Ok "Hello");
assert (Fat.Fs.listdir t "/" = Ok [ "SUB"; "HELLO.TXT" ])
A whole volume, such as an EFI System Partition or a config drive for a virtual machine, can be built in one call from files held in memory:
let image =
let files =
[
{ Fat.Image.path = "/config.txt"; contents = "kernel=k\n" };
{ Fat.Image.path = "/EFI/BOOT/BOOTX64.EFI"; contents = "MZ..." };
]
in
let size_bytes =
Fat.Image.size_for
(List.map (fun f -> (f.Fat.Image.path, String.length f.contents)) files)
in
Result.get_ok (Fat.Image.build ~size_bytes files)
To mount an existing image read-only and list its root directory:
let mount_and_list bytes =
let blk = Block.of_string ~sector_size:512 bytes in
match Fat.Fs.connect blk with
| Ok t -> Fat.Fs.listdir t "/"
| Error msg -> failwith msg
Command line #
The fat executable inspects images written by any FAT implementation:
fat info disk.img # boot sector geometry and FAT variant
fat ls disk.img / # list a directory
fat cat disk.img /A.TXT # write a file's contents to stdout
API #
Fat.Fs: the filesystem (connect,format,read,write,file,mkdir,destroy,stat,size,listdir).Fat.Image: whole volumes laid out in memory (size_for,build).Fat.Boot_sector: the boot sector and BIOS parameter block codec.Fat.Entry: the FAT12, FAT16 and FAT32 entry codec and cluster chains.Fat.Name: 8.3 and long-filename directory entries.Fat.Format,Fat.Path,Fat.Sector_mapandFat.Update: supporting types.
References #
- Microsoft, FAT: General Overview of On-Disk Format.
- ECMA-107, Volume and File Structure of Disk Cartridges for Information Interchange.
Licence #
ISC. See LICENSE.md.