# Boot and Early Kernel Initialization Relevant source files - [src/boot/boot.asm](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/boot/boot.asm) - [src/kernel/kernel.zig](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig) This page provides a high-level overview of the boot path in CadenzaOS, tracing execution from raw BIOS boot sectors through multi-stage bootloaders, memory map collection, long-mode transition, and finally into the higher-half kernel entry point and `kmain` orchestration [src/kernel/kernel.zig1-14](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L1-L14)[src/boot/boot.asm1-15](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/boot/boot.asm#L1-L15) CadenzaOS operates as a 64-bit x86-64 bare-metal operating system, leveraging a higher-half layout [src/kernel/kernel.zig88](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L88-L88) For detailed technical breakdowns of specific sub-components, refer to the child pages: - [Bootloader Stages and Linking](/GlenMeehan/CadenzaOS/2.1-bootloader-stages-and-linking) — `boot.asm` stage 1, stage2.asm long-mode transition and E820 collection, linker.ld higher-half layout, kernel_info.inc generation. - [kmain Boot Sequence](/GlenMeehan/CadenzaOS/2.2-kmain-boot-sequence) — Phase-by-phase walkthrough of kernel_entry/kmain: serial, framebuffer, stack switch, FS recovery, memory reservation, APIC/IDT setup, scheduler handoff. - [Splash Screen and Branding](/GlenMeehan/CadenzaOS/2.3-splash-screen-and-branding) — `splash.zig`, `logo.zig`, `Splash.rgb` asset and boot-time visual presentation. --- ## Boot Path and System Architecture The boot sequence transforms the CPU state from 16-bit real mode into a fully paged 64-bit long-mode environment with active interrupt management, hardware timers, and memory allocation. Title: Boot Path Code Entity Mapping ![diagram](mermaids/2-boot-and-early-kernel-initialization-diagram-1.png) Sources: [src/boot/boot.asm1-110](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/boot/boot.asm#L1-L110)[src/kernel/kernel.zig1-14](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L1-L14) --- ## 2.1. Bootloader Stages and Linking The bootloader is split into distinct stages that progressively configure the hardware platform before handing control to the Zig kernel image. Stage 1 resides in the master boot record (`src/boot/boot.asm`) and uses BIOS interrupts (`INT 13h` extended read) to load Stage 2 from disk [src/boot/boot.asm1-110](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/boot/boot.asm#L1-L110) Stage 2 executes in real mode, transitions through unreal mode to access memory above 1 MiB, collects physical memory maps via BIOS `INT 0x15` (E820), configures early page tables, and enables 64-bit long mode. The kernel binary layout is governed by linker scripts which enforce a higher-half virtual base mapped against a physical load address of 1 MiB. During the build process, sector counts and entry symbols are automatically generated into `build/kernel_info.inc` to inform Stage 2 of the exact payload size required for disk reads. For complete implementation details, see [Bootloader Stages and Linking](/GlenMeehan/CadenzaOS/2.1-bootloader-stages-and-linking). Sources: [src/boot/boot.asm1-110](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/boot/boot.asm#L1-L110) --- ## 2.2. kmain Boot Sequence Once long mode is established, the CPU jumps to `kernel_entry`, which sets up essential segment registers and stacks before invoking `kmain` in [src/kernel/kernel.zig](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig)[src/kernel/kernel.zig1-14](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L1-L14) The `kmain` routine executes an early-boot orchestration sequence: 1. Initialises serial output drivers and framebuffers for logging [src/kernel/kernel.zig46-47](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L46-L47) 2. Configures bootstrap memory allocators (`FixedBufferAllocator`) utilizing `heap_buffer`[src/kernel/kernel.zig71-81](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L71-L81) 3. Restores and mounts the CodaFS filesystem instance (`fs_global`) over the RAM disk region mapped at `RAMDISK_VIRT_ADDR` (`0xFFFFFF8002000000`) [src/kernel/kernel.zig87-101](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L87-L101) 4. Sets up interrupt descriptor tables, local APIC structures, and task state segments [src/kernel/kernel.zig27-50](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L27-L50) 5. Hands control over to the task scheduler to spawn the initial user-facing shell environment [src/kernel/kernel.zig33](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L33-L33) Title: Kernel Memory and Stack Topology ![diagram](mermaids/2-boot-and-early-kernel-initialization-diagram-2.png) Sources: [src/kernel/kernel.zig52-116](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L52-L116) For a phase-by-phase walkthrough of this sequence, see [kmain Boot Sequence](/GlenMeehan/CadenzaOS/2.2-kmain-boot-sequence). Sources: [src/kernel/kernel.zig1-116](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L1-L116)[src/boot/boot.asm1-110](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/boot/boot.asm#L1-L110) --- ## 2.3. Splash Screen and Branding CadenzaOS incorporates early-boot visual presentation routines handled by `splash.zig` and rendered via the VESA VBE framebuffer or text-mode console. Utilizing compiled-in raw RGB assets (`Splash.rgb`) and modular font glyph generators (`logo.zig`), the kernel displays branding elements during the hardware initialization phase before transitioning to the interactive shell console. For details on asset packaging and rendering mechanics, see [Splash Screen and Branding](/GlenMeehan/CadenzaOS/2.3-splash-screen-and-branding). Sources: [src/kernel/kernel.zig49](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L49-L49)