Skip to content

Booting LibreToy

Two Kinds of Firmware

Every computer starts with a program that lives on the mainboard, not on the disk: the firmware. For our purposes it has one job — find the code that boots the system and jump into it. There are two generations of it, and they look in different places.

The older one is the BIOS. It reads the first 512-byte sector of the disk, the master boot record (MBR), and jumps into the first 440 bytes of it. Those bytes are machine code, not a file: the BIOS has no filesystem driver, so whatever runs there has to find everything else on its own. Sectors are counted from 0; a sector's number is called its logical block address (LBA).

The newer one is UEFI. It understands the GUID partition table (GPT) and the FAT filesystem, and it loads a file. On removable media it looks for \EFI\BOOT\BOOTX64.EFI on the first FAT partition it finds — the EFI system partition (ESP).

flowchart LR
  power[power on] --> fw{firmware}
  fw -->|BIOS| mbr["440 bytes at LBA 0"]
  fw -->|UEFI| efi["EFI/BOOT/BOOTX64.EFI<br/>on the ESP"]
  mbr --> grub[GRUB]
  efi --> grub
  grub --> menu[image menu]

Both paths end in GRUB, the bootloader that shows the menu and starts the selected image. How it gets there is the only difference.

What LibreToy Puts on the Disk

An image that has to boot on both kinds of machine carries both boot paths.

  • The BIOS boot partition (BBP, GPT type EF02) exists for the legacy path, the BIOS. The MBR is only 512 bytes, and GPT keeps its own header in the sector right behind it, so there is no unused space where GRUB could hide the rest of its code. The BBP gives it a dedicated partition instead.
  • The EFI system partition (ESP, GPT type EF00, FAT) exists for the UEFI path: it holds EFI/BOOT/BOOTX64.EFI.

If the image only ever had to boot on UEFI machines, the BBP would be unnecessary; if it only had to boot on BIOS machines, the ESP would be. Because it has to do both, both partitions are always present.

The ESP is used on both paths: whichever way GRUB was started, it reads its menu from /grub/grub.cfg there. The menu is defined once, not twice.

mkLibreToyImage — our build function, defined in src/libretoy-image/default.nix — produces exactly this layout: GPT, a 1 MiB BBP, a 100 MiB ESP, and both boot paths inside. The sizes are defaults; the layout is computed from the configuration, not assembled by hand.

The Stages of a Legacy Boot

On a BIOS machine the boot runs in three stages. Each is larger than the last, because each can do more.

Stage Where it lives File Space available Current build What it does
1 LBA 0, first 440 bytes boot.img 440 B 440 B finds core.img and jumps into it
2 BBP, from its first sector core.img 1 MiB 156 886 B (≈153 KiB) carries the drivers, reads the configuration
3 ESP, /grub/ grub.cfg, *.mod 100 MiB 786 B + ≈2 MiB of modules the menu and the commands it needs

Stage 1 cannot read a filesystem, so it does not search for core.img — it is told where it is, by a single number stored inside the 440 bytes. Stage 2 has drivers for GPT and FAT and can search: it looks for /grub/grub.cfg on whichever device carries it. Stage 3 is the menu, and it is the same file the UEFI path reads.

Requirements and Challenges

Doing this by hand means meeting constraints that a normal bootloader installer would hide.

The first stage has 440 bytes and no driver. It cannot open a file, so the position of core.img has to be patched into boot.img before the image is written. GRUB ships a placeholder there — the number 1 — and the real value has to come from outside.

The rest of LBA 0 is taken. Bytes 446 to 511 hold the partition table and the boot signature. Only the first 440 bytes may be overwritten; everything behind them has to survive.

core.img must fit into the BBP. The partition is 1 MiB by default. The build checks the size before writing and aborts if it does not fit.

The BIOS does not reliably report the boot drive. It passes a drive number in the DL register, but some firmware reports 0x00 for the stick instead of 0x80. GRUB's first stage contains a fix-up that lifts such values to 0x80, and that fix-up sits behind a two-byte jump. Replacing the jump with two NOP instructions (0x90 0x90) enables it. The second safety net is the configuration: it searches for /grub/grub.cfg instead of assuming a device.

grub-bios-setup refuses a regular file. It wants to determine the root device of the system it installs to, and a file has none. That is why the bytes are written by hand.

Every byte has to be reproducible. Disk and partition identifiers are derived from the image name, variant and version, and the timestamp is fixed, so two builds of the same configuration produce the same file.

The Layout, Field by Field

The table lists every place the build touches. Each position is an LBA plus a byte offset inside that sector; all numbers are little-endian, the least significant byte first.

Position Length Content Expected value
LBA 0, 0x000 440 B boot code first 440 bytes of boot.img
LBA 0, 0x05C 8 B address of core.img start of the BBP, 2048
LBA 0, 0x066 2 B the jump, replaced 90 90
LBA 0, 0x1BE 64 B partition table written by sgdisk, untouched
LBA 0, 0x1FE 2 B boot signature 55 AA, untouched
LBA 2048, 0x000 512 B first sector of core.img code, blocklist at its end
LBA 2048 + 500 12 B blocklist entry sector 2049, length 306, segment 0x0820
LBA 2049 306 sectors rest of core.img the remaining 156 374 B

A blocklist is GRUB's way of describing a file that has no filesystem: a list of entries, each with the sector where a piece starts, the number of sectors, and the memory segment it is loaded into. core.img needs exactly one entry, because it is stored contiguously. The segment comes from grub-mkimage and is left alone.

How LibreToy Builds the Image

assemble.sh runs four steps, in this order.

  1. GPT. truncate creates the file at its final size; sgdisk writes the partition table with fixed disk and partition identifiers, types and labels.
  2. Filesystems. Each partition's filesystem is built separately and copied into place with dd.
  3. BIOS boot chain. The first 440 bytes of boot.img go to LBA 0; the jump at 0x66 becomes 90 90; core.img is written at the first sector of the BBP; its address is patched into LBA 0 at 0x5C; the blocklist is written at offset 500 of core.img's first sector, pointing at the sector behind it.
  4. Verification. sgdisk -v has to report "No problems found", otherwise the build aborts.

The UEFI path needs none of this: grub-mkimage writes EFI/BOOT/BOOTX64.EFI, the firmware loads it from the ESP, and the file already carries the configuration that searches for /grub/grub.cfg.

Two checks inspect the result. checks/structure.nix re-reads what the script wrote — the address at 0x5C, the 90 90 pair, the blocklist — and looks for BOOTX64.EFI and the module files inside the ESP. checks/boot.nix boots the image with SeaBIOS and with OVMF — the BIOS and the UEFI implementation QEMU ships — and waits for a marker on the serial console.