# Binary Format and Program Loading Relevant source files - [src/kernel/binfmt.zig](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/binfmt.zig) - [src/kernel/bundlefmt.zig](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bundlefmt.zig) - [src/kernel/fs/binary_loader.zig](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/binary_loader.zig) - [tools/pack.sh](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/tools/pack.sh) ## 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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/binfmt.zig#L1-L125)), the `CDZB` program bundle format ([src/kernel/bundlefmt.zig1-72](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bundlefmt.zig#L1-L72)), the build-time packaging script ([tools/pack.sh1-77](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/tools/pack.sh#L1-L77)), and the kernel installation and loading pipeline ([src/kernel/fs/binary_loader.zig1-130](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/binary_loader.zig#L1-L130)). Sources: [src/kernel/binfmt.zig1-125](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/binfmt.zig#L1-L125)[src/kernel/bundlefmt.zig1-72](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bundlefmt.zig#L1-L72)[tools/pack.sh1-77](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/tools/pack.sh#L1-L77)[src/kernel/fs/binary_loader.zig1-130](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/binary_loader.zig#L1-L130) --- ## 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 `BinHeader`[src/kernel/binfmt.zig36-51](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/binfmt.zig#L36-L51) 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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/binfmt.zig#L16-L22) ### `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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/binfmt.zig#L37-L37) | | 0x04 | `version` | `u16` | Binary format version (currently `1`) [src/kernel/binfmt.zig25](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/binfmt.zig#L25-L25)[src/kernel/binfmt.zig40](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/binfmt.zig#L40-L40) | | 0x06 | `flags` | `u16` | Reserved flags (must be zero for version 1) [src/kernel/binfmt.zig42-44](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/binfmt.zig#L42-L44) | | 0x08 | `entry_offset` | `u32` | Byte offset from the image base to the entry point [src/kernel/binfmt.zig46-47](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/binfmt.zig#L46-L47) | | 0x0C | `image_size` | `u32` | Total file-backed size emitted by the linker [src/kernel/binfmt.zig49-50](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/binfmt.zig#L49-L50) | ### 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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/binfmt.zig#L79-L99) 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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/binfmt.zig#L101-L125) Sources: [src/kernel/binfmt.zig1-125](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/binfmt.zig#L1-L125) --- ## 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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bundlefmt.zig#L1-L15) The bundle format uses the magic value `0x425A4443` ("CDZB") [src/kernel/bundlefmt.zig18](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bundlefmt.zig#L18-L18) Its on-disk layout consists of: - **Sector 0**: A 16-byte `BundleHeader` followed by up to 12 `BundleEntry` records (40 bytes each) [src/kernel/bundlefmt.zig12-13](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bundlefmt.zig#L12-L13) - **Sector 1+**: Program images, each padded to align with sector boundaries [src/kernel/bundlefmt.zig14](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bundlefmt.zig#L14-L14) ### Bundle Structures - `BundleHeader`: Contains `magic`, `version` (`1`), `count` (number of bundled programs), and `total_sectors`[src/kernel/bundlefmt.zig24-30](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bundlefmt.zig#L24-L30) - `BundleEntry`: Contains a 32-byte NUL-padded file `name`, `offset_sectors` from the start of the bundle, and `size_bytes`[src/kernel/bundlefmt.zig32-36](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bundlefmt.zig#L32-L36) ### Bundle Parsing Flow ![diagram](mermaids/5.3-binary-format-and-program-loading-diagram-1.png) *Figure 1: Data flow bridging natural bundle bytes to `bundlefmt.zig` validation and entry functions.* Sources: [src/kernel/bundlefmt.zig1-72](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bundlefmt.zig#L1-L72) --- ## 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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/tools/pack.sh#L1-L77) ### Packaging Logic 1. Validates that the number of input programs does not exceed `MAXENT` (`12`) [tools/pack.sh27-29](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/tools/pack.sh#L27-L29) 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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/tools/pack.sh#L45-L55) 3. Prepends the 16-byte `BundleHeader` (`0x425A4443`, version `1`, entry count, and total sectors) to sector 0 [tools/pack.sh59-67](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/tools/pack.sh#L59-L67) 4. Concatenates all program binary payloads into the output file, padding each program with zeroes to the next sector boundary [tools/pack.sh70-75](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/tools/pack.sh#L70-L75) Sources: [tools/pack.sh1-77](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/tools/pack.sh#L1-L77) --- ## 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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/binary_loader.zig#L13-L51) ### Key Functions - `installEmbeddedApps(allocator, fs)`: Reads sector 0 from `APP_LBA_START` using `ata.AtaDevice.readBlocks()`[src/kernel/fs/binary_loader.zig29-30](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/binary_loader.zig#L29-L30) parses the bundle header via `bundle.parseHeader()`[src/kernel/fs/binary_loader.zig32](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/binary_loader.zig#L32-L32) and iterates through each entry to invoke `installOne()`[src/kernel/fs/binary_loader.zig38-50](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/binary_loader.zig#L38-L50) - `installOne(allocator, fs, name, offset_sectors, size)`: 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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/binary_loader.zig#L63-L68) 2. Reads the program's sectors from raw disk storage (`APP_LBA_START + offset_sectors`) [src/kernel/fs/binary_loader.zig70-76](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/binary_loader.zig#L70-L76) 3. Validates the program header using `binfmt.parse()`[src/kernel/fs/binary_loader.zig79](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/binary_loader.zig#L79-L79) 4. Allocates filesystem space for metadata and data blocks using `fs.space_manager.allocate()`[src/kernel/fs/binary_loader.zig84-91](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/binary_loader.zig#L84-L91) 5. Populates a `FileMeta` structure and writes both the metadata block and payload blocks to disk [src/kernel/fs/binary_loader.zig94-112](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/binary_loader.zig#L94-L112) 6. Inserts the directory entry into CodaFS and flushes space manager states [src/kernel/fs/binary_loader.zig115-130](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/binary_loader.zig#L115-L130) ### Installation Architecture ``` ``` *Figure 2: Component interaction bridging disk staging areas to `binary_loader.zig` installation procedures.* Sources: [src/kernel/fs/binary_loader.zig1-130](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/binary_loader.zig#L1-L130)[src/kernel/drivers/ata.zig1-155](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/drivers/ata.zig#L1-L155) --- ## 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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/binfmt.zig#L101-L125) ### Execution Checks - **Header Verification**: `binfmt.readHeader()` ensures the magic bytes match `0x50_5A_44_43` ("CDZP") and version equals `1`[src/kernel/binfmt.zig79-98](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/binfmt.zig#L79-L98) - **Size Bounds**: `binfmt.parse()` verifies that `image_size` does not exceed the allocated buffer size [src/kernel/binfmt.zig111-116](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/binfmt.zig#L111-L116) - **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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/binfmt.zig#L118-L122) Sources: [src/kernel/binfmt.zig1-125](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/binfmt.zig#L1-L125)[src/kernel/fs/binary_loader.zig1-130](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/binary_loader.zig#L1-L130)