Skip to content

2.1 Bootloader Stages and Linking

Glen edited this page Nov 14, 2026 · 2 revisions

Bootloader Stages and Linking

Relevant source files

Purpose and Scope

This page provides full technical documentation for the CadenzaOS boot sequence and memory layout configuration. It details the transition from the 512-byte Master Boot Record (src/boot/boot.asm) through Stage 2 long-mode initialization and E820 memory map collection (src/stage2/stage2.asm), higher-half kernel linking (linker.ld), and build-time metadata injection (kernel_info.inc).

Sources: src/boot/boot.asm:1-111, src/stage2/stage2.asm:1-180, linker.ld:1-50


1. Stage 1 Master Boot Record (boot.asm)

The Stage 1 bootloader resides in sector 0 of the storage medium and is loaded by the BIOS into physical address 0x7C00src/boot/boot.asm6-11 Its execution flow initializes a real-mode stack, validates extended BIOS disk services, loads the Stage 2 binary from disk, and transfers control.

Upon entry, segment registers (ds, es, ss) are cleared to 0x0000, and the stack pointer sp is set to 0x7C00, growing downward away from the MBR code src/boot/boot.asm18-24 The boot drive identifier passed by the BIOS in dl is stored in [boot_drive]src/boot/boot.asm28-29

Stage 1 checks for LBA support using INT 13h AH=41h with magic value 0x55AA in bxsrc/boot/boot.asm39-46 It then reads 20 sectors (10 KiB) of Stage 2 from starting LBA 1 into physical address 0x7E00 via a Disk Address Packet (dap) structure using INT 13h AH=42hsrc/boot/boot.asm56-65 Finally, execution jumps to 0x7E00src/boot/boot.asm70

Title: Stage 1 Boot Execution Flow


Sources: src/boot/boot.asm:11-111


2. Stage 2 Bootloader, Long-Mode Transition, and E820 Collection (stage2.asm)

The Stage 2 bootloader executes between memory addresses 0x7E00 and 0xA5FFsrc/stage2/stage2.asm6 Its primary responsibilities are:

  1. Initializing serial logging (serial_init16) src/stage2/stage2.asm81-104
  2. Transitioning through Unreal Mode to read memory above 1 MiB while in 16-bit mode src/stage2/stage2.asm153-174
  3. Constructing early page tables for identity and higher-half mapping src/stage2/stage2.asm37-43
  4. Collecting physical memory layout via BIOS E820 routines into E820_BUF (0x9000) src/stage2/stage2.asm9 and MMAP_COUNT (0x8FF8) src/stage2/stage2.asm27
  5. Loading the kernel binary from disk into physical address 0x00100000 (1 MiB) src/stage2/stage2.asm30
  6. Activating Long Mode and jumping to the higher-half kernel entry point.

Unreal Mode

To load the kernel image without fully entering protected mode immediately, Stage 2 temporarily enables Protected Mode (PE=1 in cr0), loads flat 4 GiB descriptors into data segment registers (ds, es, fs, gs), and returns to Real Mode (PE=0) src/stage2/stage2.asm153-174 This permits 32-bit addressing bounds while retaining 16-bit instruction execution.

Page Table Architecture

Stage 2 allocates early page tables in physical memory between 0x1000 and 0x5000src/stage2/stage2.asm7:

Title: Stage 2 Page Table Hierarchy


Sources: src/stage2/stage2.asm:6-180


3. Higher-Half Linking (linker.ld)

The kernel linker script (linker.ld) enforces a higher-half virtual memory layout. All kernel code and data are linked to execute at virtual addresses starting at 0xFFFFFF8000000000 (KERNEL_OFFSET), while being physically loaded at 0x00100000 (KERNEL_PHYS, 1 MiB) linker.ld3-6

Segments and Sections

The linker script defines two loadable program headers (PHDRS) linker.ld12-18:

  1. :text (PT_LOAD, flags read/write/execute 7) linker.ld15: Contains .text, .rodata, and .data sections linker.ld26-37
  2. :bss (PT_LOAD, flags read/write 6) linker.ld17: Contains uninitialized data (.bss and COMMON), mapped separately to avoid inflating the on-disk binary size linker.ld39-43

Title: Linker Layout and Higher-Half Virtual Mapping


Sources: linker.ld:1-50


4. Kernel Information Generation (kernel_info.inc)

During the build pipeline, metadata is extracted from the compiled ELF kernel and written to build/kernel_info.inc. This file is directly included by stage2.asm to synchronize build parameters with assembly-level constants src/stage2/stage2.asm31

Generated variables include:

  • KERNEL_SECTORS: Total sector count of the kernel binary.
  • KERNEL_ENTRY: Resolved virtual entry point address (kernel_entry).
  • BSS_PHYS: Physical address of the .bss section.
  • BSS_SIZE_DWORDS: Size of the .bss section in 32-bit dwords for zero-initialization.

Title: Build Pipeline Metadata Generation Flow


Sources: src/stage2/stage2.asm:24-36

Clone this wiki locally