Lesson 12 · Operating Systems

Operating System Boot Process

Booting is a relay: each program is just capable enough to load a bigger one. The CPU starts in firmware that knows nothing about files, and a few handoffs later a full kernel is running and user programs start.

Operating System Boot Process concept diagramA visual explanation of the layout and operations shown in this lesson.each stage exists because the one before it is too small to do the next jobreset vectorno RAM0xFFFFFFF0 in flashUEFI firmwareRAM usablePOST, train DRAMboot managerfound .EFIread the ESPshim / GRUB2kernel in RAMload kernel + initramfskernelinitramfs rootpaging, IDT, SMPPID 1login promptsystemd starts unitsSecure Boot verifies each stage's signature before transferring control to itthe initramfs exists because the driver for the root filesystem may live ON the root filesystem
1

Why Booting Happens in Stages

  1. Power on: the CPU runs firmware from the reset vector
  2. Firmware runs POST, sets up RAM and finds devices
  3. Firmware picks a boot device and loads the bootloader
  4. The bootloader loads the kernel and the initramfs into RAM
  5. The kernel sets up memory, interrupts, CPUs and drivers
  6. The initramfs makes the real root file system reachable and mounts it
  7. The kernel starts init (PID 1), which starts services and the login

Booting an operating system is a bootstrap problem. Loading an OS needs code that can drive a disk, read a file system and set up memory, but that code is part of the OS that is not loaded yet. The answer is a chain: each stage is small enough to be loaded by the one before it and capable enough to load the next.

At power-on the CPU starts at a hardwired address, the reset vector. On x86 it is 0xFFFFFFF0, 16 bytes below 4 GB, and the chipset maps it to the firmware's flash chip. Memory is not set up yet and no disk is reachable, so the very first instructions run straight from flash.

  • Each stage only needs to load the next one
  • The first code runs from flash because nothing else exists yet
2

Firmware: POST and Finding a Boot Device

The firmware runs the power-on self test (POST), then trains the memory controller so RAM becomes usable; on servers with lots of memory this is why the screen stays blank for a while. It then finds devices on the buses and builds tables describing the machine (ACPI on PCs), which the kernel reads later.

Finally it chooses where to boot from, following the boot order the user configured, and loads a bootloader from that device. It does not load the kernel directly: firmware's ability to read disks is limited, and the kernel needs options and a prepared environment that a bootloader supplies.

  • POST checks that core hardware works
  • RAM is only usable after memory training
  • The firmware loads a bootloader, not the OS
3

BIOS vs UEFI

The difference between BIOS and UEFI comes down to what the firmware can read. Legacy BIOS loads exactly one 512-byte sector, the master boot record, to address 0x7C00 and jumps to it. After the partition table only 446 bytes are left for code, nowhere near enough to understand a file system. So GRUB's first stage loads a bigger second piece from the sectors right after the MBR, and that piece reads the real files in /boot.

UEFI understands GPT partitions and can read the FAT32 EFI System Partition itself, so it loads an ordinary file, such as \EFI\BOOT\BOOTX64.EFI, with no sector tricks. It starts in 32- or 64-bit mode instead of 16-bit real mode, keeps boot entries in NVRAM, and can check signatures. Replacing a UEFI bootloader means copying a file; on BIOS any OS install could overwrite the MBR, which is why dual-boot setups used to break so easily.

Legacy BIOS vs UEFI boot
Legacy BIOSUEFI
What it loadsOne 512-byte sector (MBR)A file from the EFI System Partition
Partition schemeMBR, disks up to 2 TBGPT
CPU mode at handoff16-bit real mode32- or 64-bit
Boot entriesFixed: first sector of the chosen diskList in NVRAM
Signature checkingNoneSecure Boot
  • BIOS: one raw sector, then a chain of loader stages
  • UEFI: reads a file system and loads a named program
Implementation

Booting a UEFI PC, stage by stage

TRUSTS = {"firmware": "microsoft", "shim": "distro", "grub": "distro"}  # key each stage checks the next with
CHAIN = ["firmware", "shim", "grub", "kernel", "initramfs", "init"]


def boot(signers, initramfs_drivers):
    path = []
    for stage, nxt in zip(CHAIN, CHAIN[1:]):
        path.append(stage)
        if stage in TRUSTS and signers[nxt] != TRUSTS[stage]:
            return " > ".join(path) + f" | stop: {stage} rejects {nxt} (signed by {signers[nxt]})"
        if nxt == "init" and "nvme" not in initramfs_drivers:
            return " > ".join(path) + " | stop: root disk unreachable, kernel panic"
    return " > ".join(CHAIN)


good = {"shim": "microsoft", "grub": "distro", "kernel": "distro"}
print("normal boot:   ", boot(good, {"nvme", "ext4"}))
print("tampered grub: ", boot({**good, "grub": "attacker"}, {"nvme", "ext4"}))
print("no nvme driver:", boot(good, {"ext4"}))
#include <cstdio>
#include <map>
#include <set>
#include <string>
#include <vector>

const std::map<std::string, std::string> TRUSTS = {{"firmware", "microsoft"}, {"shim", "distro"}, {"grub", "distro"}};  // key each stage checks the next with
const std::vector<std::string> CHAIN = {"firmware", "shim", "grub", "kernel", "initramfs", "init"};

std::string boot(std::map<std::string, std::string> signers, const std::set<std::string>& initramfsDrivers) {
    std::string path;
    for (size_t i = 0; i + 1 < CHAIN.size(); i++) {
        const std::string &stage = CHAIN[i], &next = CHAIN[i + 1];
        path += (i ? " > " : "") + stage;
        auto t = TRUSTS.find(stage);
        if (t != TRUSTS.end() && signers[next] != t->second)
            return path + " | stop: " + stage + " rejects " + next + " (signed by " + signers[next] + ")";
        if (next == "init" && !initramfsDrivers.count("nvme"))
            return path + " | stop: root disk unreachable, kernel panic";
    }
    return path + " > " + CHAIN.back();
}

int main() {
    std::map<std::string, std::string> good = {{"shim", "microsoft"}, {"grub", "distro"}, {"kernel", "distro"}};
    auto tampered = good;
    tampered["grub"] = "attacker";
    std::printf("normal boot:    %s\n", boot(good, {"nvme", "ext4"}).c_str());
    std::printf("tampered grub:  %s\n", boot(tampered, {"nvme", "ext4"}).c_str());
    std::printf("no nvme driver: %s\n", boot(good, {"ext4"}).c_str());
}
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Main {
    static final Map<String, String> TRUSTS = Map.of("firmware", "microsoft", "shim", "distro", "grub", "distro");  // key each stage checks the next with
    static final List<String> CHAIN = List.of("firmware", "shim", "grub", "kernel", "initramfs", "init");

    static String boot(Map<String, String> signers, Set<String> initramfsDrivers) {
        StringBuilder path = new StringBuilder();
        for (int i = 0; i + 1 < CHAIN.size(); i++) {
            String stage = CHAIN.get(i), next = CHAIN.get(i + 1);
            path.append(i > 0 ? " > " : "").append(stage);
            if (TRUSTS.containsKey(stage) && !TRUSTS.get(stage).equals(signers.get(next)))
                return path + " | stop: " + stage + " rejects " + next + " (signed by " + signers.get(next) + ")";
            if (next.equals("init") && !initramfsDrivers.contains("nvme"))
                return path + " | stop: root disk unreachable, kernel panic";
        }
        return path + " > " + CHAIN.get(CHAIN.size() - 1);
    }

    public static void main(String[] args) {
        Map<String, String> good = Map.of("shim", "microsoft", "grub", "distro", "kernel", "distro");
        Map<String, String> tampered = new HashMap<>(good);
        tampered.put("grub", "attacker");
        System.out.println("normal boot:    " + boot(good, Set.of("nvme", "ext4")));
        System.out.println("tampered grub:  " + boot(tampered, Set.of("nvme", "ext4")));
        System.out.println("no nvme driver: " + boot(good, Set.of("ext4")));
    }
}
Watch it run

Step through it

Running on UEFI x86-64 PC · Secure Boot on · Linux on an encrypted NVMe disk

UEFI PC with Secure Boot · power button pressedreset vectorCPU starts in flashfirmwarePOST, RAM, devicesboot managerNVRAM, EFI partitionshimMicrosoft-signedGRUBloads kernel + initramfskernelmemory, CPUs, driversinitramfsreaches the real rootinit (PID 1)services, loginwhat exists nowfirmware code in flash, a CPU, nothing elseno RAM, no disk, no file system: each stage below must load the nextthe chain starts with the only code the CPU can reach
Step 1

Power on: the CPU knows one address

When power is stable the CPU leaves reset and fetches its first instruction from a fixed address, the reset vector: 0xFFFFFFF0 on x86, 16 bytes below 4 GB. The chipset maps that address to the firmware flash chip. RAM is not usable yet, there is no disk driver and no file system, so the first code has to run straight from flash.

Output
1 / 12
4

Bootloader, Kernel and init

The bootloader (GRUB on most Linux systems, Windows Boot Manager, systemd-boot, or U-Boot on embedded boards) shows a menu if configured, loads the kernel image and the initramfs into memory, passes a command line such as root=UUID=..., and jumps to the kernel's entry point.

The kernel decompresses itself, sets up page tables and interrupt handlers, starts the other CPU cores and initialises its built-in drivers. Then comes a chicken-and-egg problem: the driver for the disk holding the root file system, say NVMe or disk encryption, may be a module stored on that very disk. The initramfs solves it. It is a small archive already in RAM with exactly those modules; its script loads them, unlocks and mounts the real root, and switch_root moves onto it and frees the initramfs.

Last, the kernel runs /sbin/init as process 1. On most Linux systems that is systemd, which starts services in parallel according to their dependencies, where older SysV init ran numbered scripts one by one. When the default target is reached, the login prompt appears. See kernel and system calls for what the kernel does from then on.

  • Bootloader: loads kernel + initramfs, passes options
  • initramfs: carries the driver for the disk it came from
  • PID 1 starts every other user process
5

Cold Booting and Warm Booting

Cold booting and warm booting differ in where the machine starts from. A cold (hard) boot starts from power off: pressing the power button runs every stage, including the full self test and memory training. A warm (soft) boot restarts a machine that is already on, for example a restart from the menu or Ctrl+Alt+Del: power is never cut, the CPU is reset and starts again at the reset vector, the firmware may skip some slow tests, and everything after it runs again.

A warm boot is quicker, but it does not reset hardware as thoroughly; a device stuck in a bad state may need a cold boot to recover. Linux also offers kexec, which loads a new kernel straight from the running one and skips firmware and bootloader entirely, useful for fast restarts on servers.

  • Cold boot: from power off, every stage
  • Warm boot: power stays on, CPU reset, faster
  • kexec: kernel to kernel, no firmware
6

Secure Boot and the Chain of Trust

Code that runs before the OS can control everything after it and hide from every tool the OS offers; malware that does this is called a bootkit. Secure Boot blocks it with a chain of signature checks. The firmware holds trusted keys and checks the bootloader before running it, the bootloader checks the kernel, and the kernel checks the modules it loads.

Linux distributions join the chain through shim, a small loader signed by Microsoft's UEFI key, which nearly every PC trusts. Shim carries the distribution's own key and uses it to check GRUB and the kernel. Measured Boot goes further and records a hash of each stage in the TPM chip, so the machine can later prove what it booted. The tracer shows both a trusted boot and a tampered GRUB being refused.

  • Each stage verifies the next before handing over
  • shim: Microsoft-signed, carries the distribution key
  • Measured Boot records each stage in the TPM