Skip to content

5.3 Binary Format and Program Loading

Glen edited this page Nov 14, 2026 · 2 revisions

Binary Format and Program Loading

Relevant source files

Purpose and Scope

This page details the binary layout, packaging, and program loading subsystems of CadenzaOS. It covers the 16-byte CDZP flat binary format used by userland executables (src/kernel/binfmt.zig1-125), the CDZB program bundle format (src/kernel/bundlefmt.zig1-72), the build-time packaging script (tools/pack.sh1-77), and the kernel installation and loading pipeline (src/kernel/fs/binary_loader.zig1-130).

Sources: src/kernel/binfmt.zig1-125src/kernel/bundlefmt.zig1-72tools/pack.sh1-77src/kernel/fs/binary_loader.zig1-130


1. Flat Binary Header Format (CDZP)

Every userland executable in CadenzaOS begins with a fixed 16-byte header emitted by the application linker script, defined as BinHeadersrc/kernel/binfmt.zig36-51

The header uses a magic value of 0x50_5A_44_43, which corresponds to the ASCII byte sequence 'C', 'D', 'Z', 'P' in little-endian representation src/kernel/binfmt.zig16-22

BinHeader Structure

The layout of the executable header is defined as follows:

Offset Field Type Description
0x00 magic u32 Format magic (0x50_5A_44_43 / "CDZP") src/kernel/binfmt.zig37
0x04 version u16 Binary format version (currently 1) src/kernel/binfmt.zig25src/kernel/binfmt.zig40
0x06 flags u16 Reserved flags (must be zero for version 1) src/kernel/binfmt.zig42-44
0x08 entry_offset u32 Byte offset from the image base to the entry point src/kernel/binfmt.zig46-47
0x0C image_size u32 Total file-backed size emitted by the linker src/kernel/binfmt.zig49-50

Validation Functions

The kernel provides two tiers of validation for binary images:

  1. readHeader(bytes: []const u8): Validates the magic value, format version, and claimed image size using only the initial header bytes src/kernel/binfmt.zig79-99
  2. parse(image: []const u8): Performs full verification on a fully loaded image buffer, ensuring image_size does not exceed the buffer length and that entry_offset points past the header and within the executable payload src/kernel/binfmt.zig101-125

Sources: src/kernel/binfmt.zig1-125


2. Program Bundle Format (CDZB)

To stage multiple userland programs onto the disk image before the filesystem is initialized, CadenzaOS packs all binaries under userland/ into a single container known as a bundle src/kernel/bundlefmt.zig1-15

The bundle format uses the magic value 0x425A4443 ("CDZB") src/kernel/bundlefmt.zig18 Its on-disk layout consists of:

Bundle Structures

Bundle Parsing Flow

diagram

Figure 1: Data flow bridging natural bundle bytes to bundlefmt.zig validation and entry functions.

Sources: src/kernel/bundlefmt.zig1-72


3. Build-Time Bundling with tools/pack.sh

The packaging tool tools/pack.sh constructs the binary bundle from compiled .bin userland applications tools/pack.sh1-77

Packaging Logic

  1. Validates that the number of input programs does not exceed MAXENT (12) tools/pack.sh27-29
  2. Computes each program's size via stat, calculates sector counts, and appends a 40-byte BundleEntry record into a temporary table tools/pack.sh45-55
  3. Prepends the 16-byte BundleHeader (0x425A4443, version 1, entry count, and total sectors) to sector 0 tools/pack.sh59-67
  4. Concatenates all program binary payloads into the output file, padding each program with zeroes to the next sector boundary tools/pack.sh70-75

Sources: tools/pack.sh1-77


4. First-Boot App Installation (binary_loader.zig)

At first boot, the kernel installs embedded applications from the raw staging area (APP_LBA_START) into the CodaFS filesystem src/kernel/fs/binary_loader.zig13-51

Key Functions

  1. Checks if the file already exists in the root directory via fs.findFile(). If it exists, installation is skipped to preserve user changes src/kernel/fs/binary_loader.zig63-68
  2. Reads the program's sectors from raw disk storage (APP_LBA_START + offset_sectors) src/kernel/fs/binary_loader.zig70-76
  3. Validates the program header using binfmt.parse()src/kernel/fs/binary_loader.zig79
  4. Allocates filesystem space for metadata and data blocks using fs.space_manager.allocate()src/kernel/fs/binary_loader.zig84-91
  5. Populates a FileMeta structure and writes both the metadata block and payload blocks to disk src/kernel/fs/binary_loader.zig94-112
  6. Inserts the directory entry into CodaFS and flushes space manager states src/kernel/fs/binary_loader.zig115-130

Installation Architecture


Figure 2: Component interaction bridging disk staging areas to binary_loader.zig installation procedures.

Sources: src/kernel/fs/binary_loader.zig1-130src/kernel/drivers/ata.zig1-155


5. Spawn-Time Image Loading and Validation

When a userland program is executed (spawned) from the shell, the kernel loads the file contents from CodaFS into a memory buffer and subjects it to strict validation before execution src/kernel/binfmt.zig101-125

Execution Checks

  • Header Verification: binfmt.readHeader() ensures the magic bytes match 0x50_5A_44_43 ("CDZP") and version equals 1src/kernel/binfmt.zig79-98
  • Size Bounds: binfmt.parse() verifies that image_size does not exceed the allocated buffer size src/kernel/binfmt.zig111-116
  • Entry Point Bounds: Ensures entry_offset is strictly greater than or equal to @sizeOf(BinHeader) (16 bytes) and strictly less than image_size, preventing execution flow from targeting header metadata or unmapped memory src/kernel/binfmt.zig118-122

Sources: src/kernel/binfmt.zig1-125src/kernel/fs/binary_loader.zig1-130

Clone this wiki locally