# Getting Started and Field Build System Relevant source files - [SECURITY.md](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/SECURITY.md?plain=1) - [bochsrc.txt](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/bochsrc.txt) - [bochsrcusb.txt](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/bochsrcusb.txt) - [build.sh](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/build.sh) - [docs/REAL_HARDWARE.md](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/docs/REAL_HARDWARE.md?plain=1) - [docs/RELEASE_NOTES.md](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/docs/RELEASE_NOTES.md?plain=1) ## Purpose and Scope This page details the build system, toolchain prerequisites, execution modes, version generation, and disk image layout for CadenzaOS. It serves as the primary reference for compiling the kernel and bootloader, writing components to the disk image, and launching the OS inside hypervisors such as QEMU or Bochs, as well as preparing images for real hardware deployment. Sources: [build.sh1-12](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/build.sh#L1-L12)[docs/REAL_HARDWARE.md1-15](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/docs/REAL_HARDWARE.md?plain=1#L1-L15) --- ## Toolchain Prerequisites and Environment The CadenzaOS build infrastructure is driven by `build.sh` [build.sh:1-186] and assumes a POSIX-compliant GNU/Linux host environment. The script relies on system utilities including `stat` (GNU syntax), `readelf`, `objcopy`, `as`, `ld`, `nasm`, and `truncate` [build.sh:5-6]. Additionally, the build system requires the **Zig compiler (version 0.16.0)** to compile the freestanding kernel [build.sh:16-20]. If Zig is not found in the host `$PATH`, the `ZIG` environment variable must be explicitly exported pointing to the binary. ``` # Example: Setting custom Zig path if not in PATH export ZIG=/opt/zig/zig-0.16.0/zig ./build.sh ci ``` Sources: [build.sh1-20](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/build.sh#L1-L20) --- ## Build Script Modes (`build.sh`) The `build.sh` script supports four distinct operational modes to control compilation and execution flow [build.sh:8-12]: - **Default (`./build.sh`)**: Performs a full build of the bootloader, kernel, staging areas, and userland bundles. It checks for an existing disk image (`build/disk.img`) and prompts the user whether to preserve or re-initialize the CodaFS filesystem partition [build.sh:8-9, 68-83]. Upon completion, it launches QEMU [build.sh:50-54]. - **`ci` Mode (`./build.sh ci`)**: Executes a non-interactive build suitable for continuous integration pipelines or real hardware flashing [build.sh:10, 65-66]. It forcefully removes any existing disk image to ensure a completely fresh build, compiles all artifacts, and exits without launching QEMU. - **`run` Mode (`./build.sh run`)**: Bypasses the build phase entirely [build.sh:11, 44-54]. It checks for the existence of `build/disk.img` and immediately launches QEMU, preserving all pre-existing disk state. - **`clean` Mode (`./build.sh clean`)**: Removes the compiled disk image (`build/disk.img`) from the local build directory [build.sh:12, 57-61]. Sources: [build.sh1-87](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/build.sh#L1-L87) --- ## Version Generation and Build Metadata During the build phase, `build.sh` extracts repository metadata using Git to generate a runtime version identifier [build.sh:95-116]. It queries the short commit hash, inspects `git status` to determine whether the working tree is dirty, and records the UTC build timestamp [build.sh:96-102]. This data is written directly to `src/kernel/version.zig` [build.sh:112-116], exposing constants accessible throughout the kernel: ``` pub const git_hash: []const u8 = "3ec0288"; pub const git_dirty: bool = true; pub const build_ts: []const u8 = "2026-10-15T03:29:11Z"; ``` Sources: [build.sh95-116](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/build.sh#L95-L116) --- ## Disk Image Layout and Sector Allocation The OS image `build/disk.img` is structured as a 10MB raw binary file containing boot sectors, the kernel binary, userland program bundles, and the CodaFS storage partition [build.sh:26-41, 171-184]. ![diagram](mermaids/1.1-getting-started-and-build-system-diagram-1.png) *Figure 1: Physical disk layout bridging system storage sectors to code and configuration entities (`build.sh`, `config.zig`).* Sources: [build.sh26-41](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/build.sh#L26-L41)[build.sh171-184](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/build.sh#L171-L184) Key sector locations are defined across `build.sh` [build.sh:26-41] and `src/kernel/config.zig`: - **Sector 0**: Master boot record / stage 1 bootloader (`src/boot/boot.asm`), written via `dd` [build.sh:181]. - **Sector 1 onwards**: Stage 2 long-mode transition loader (`src/stage2/stage2.asm`), written with a seek of 1 [build.sh:182]. - **Sector 16 (`KERNEL_LBA`)**: Kernel binary (`kernel.bin`) [build.sh:27, 183]. - **`APP_LBA_START`**: Userland application staging area parsed directly from `src/kernel/config.zig` [build.sh:30-41]. - **Sector 4096 (`PARTITION_LBA`)**: Start of the CodaFS filesystem partition [build.sh:28]. When the filesystem is preserved, sectors below `PARTITION_LBA` are updated while the filesystem is untouched [build.sh:178-180]. Sources: [build.sh26-41](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/build.sh#L26-L41)[build.sh171-184](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/build.sh#L171-L184) --- ## Compilation, Assembly, and Linking Pipeline The kernel build pipeline processes multiple language inputs (Zig, NASM assembly, GAS assembly) into a unified higher-half ELF executable and raw binary image [build.sh:91-132]. ![diagram](mermaids/1.1-getting-started-and-build-system-diagram-2.png) *Figure 2: Compilation and linking dataflow bridging source files to build outputs (`build.sh`, `linker.ld`).* Sources: [build.sh91-137](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/build.sh#L91-L137) 1. **Kernel Object Generation**: `src/kernel/kernel.zig` is compiled using Zig in freestanding x86-64 mode with kernel code model (`-mcmodel=kernel -fPIC -O Debug -fno-stack-protector`) [build.sh:119-121]. 2. **Interrupt and Architecture Stubs**: `irq_stubs.asm` and `arch_utils.s` are assembled into ELF64 objects [build.sh:123-124]. 3. **Linking**: All objects are linked using `linker.ld` with a 4KB page size enforcement (`-z max-page-size=0x1000`) [build.sh:126-129]. 4. **Metadata Extraction**: `build.sh` inspects `kernel.elf` via `readelf` to extract the entry point address and BSS section physical bounds, generating `build/kernel_info.inc` for the bootloader assembly stages [build.sh:130-157]. Sources: [build.sh91-165](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/build.sh#L91-L165) --- ## Emulator Launch Configurations and Real Hardware CadenzaOS is primarily tested and run using QEMU, Bochs, and verified physical hardware (such as the Dell OptiPlex 3020 in Legacy BIOS / IDE mode) [docs/REAL_HARDWARE.md:5-13]. ### QEMU Execution When running `./build.sh` or `./build.sh run`, QEMU is invoked with standard system emulation flags [build.sh:50-53]: ``` qemu-system-x86_64 \ -drive format=raw,file=build/disk.img \ -m 1024 -monitor stdio -no-reboot -no-shutdown -vga std \ -serial file:serial.log ``` This configuration allocates 1024 MB RAM, routes monitor output to stdio, maps standard VGA, prevents automatic reboots on kernel panics (`-no-reboot -no-shutdown`), and mirrors COM1 serial output to `serial.log` [build.sh:50-53]. ### Bochs Configuration Alternatively, Bochs can be used via the provided configuration file `bochsrc.txt` [bochsrc.txt:1-31]: - **Memory**: 1024 MB (`megs: 1024`) [bochsrc.txt:5]. - **CPU Model**: `core2_penryn_t9600` at 50 MIPS [bochsrc.txt:7]. - **Storage**: Primary ATA master disk (`ata0-master`) mapped to `build/disk.img` [bochsrc.txt:14-15]. - **Serial**: COM1 logging directed to `serial.log` [bochsrc.txt:21]. For physical USB testing workflows, `bochsrcusb.txt` maps the primary master disk directly to host device nodes like `/dev/sdb` [bochsrcusb.txt:10-12]. Sources: [build.sh44-54](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/build.sh#L44-L54)[bochsrc.txt1-30](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/bochsrc.txt#L1-L30)[bochsrcusb.txt1-19](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/bochsrcusb.txt#L1-L19)