The Computer Boot Process

June 16, 2018

The Ancient Era

At this stage the computer lives in a purely electrical world, with almost no notion of software (not entirely accurate—more on that later).

The computer runs a series of hardware self-tests, in particular checking that essential hardware is usable. This is the POST (Power-On Self-Test). If it passes, the machine moves on to the next step: BIOS mode or UEFI mode.

When computer hardware is faulty, the boot usually fails here during the self-test and is forced to stop. This stage checks the CPU, memory, and I/O chip modules such as the 8255 (modern peripheral chips are far more capable and feature-rich, so they're not necessarily relics like the 8255).

Everything above is a pure hardware self-test with almost no software involved. In the next step—BIOS or UEFI—software programs formally join in.

For heterogeneous platforms such as embedded devices, the design may differ. The self-test may be transparent and nearly imperceptible, since it all happens inside a single SoC and is largely software-based. The embedded flow is out of scope here.

Legacy BIOS

After the self-test passes, the system starts the BIOS program stored in ROM. The BIOS runs in real mode and ranges from a few hundred KB to a few MB. It is the lowest control layer over the hardware; you can enter the BIOS setup screen at boot to configure the system, and the available settings vary widely between vendors.

Once started, the BIOS performs initialization, mainly building the interrupt (IRQ) table, setting up various registers, and allocating I/O ports.

When all that is done, the BIOS copies the boot partition specified in its settings into RAM (running the MBR boot code) and loads the bootloader in it (the bootloader is copied to RAM starting at address 0x00007c00).

Modern UEFI

UEFI is the interface used by modern computers as a replacement for BIOS. UEFI stands for Unified Extensible Firmware Interface. It has a modular design and runs on multiple architectures, including AMD64, IA32, and IA64.

Why UEFI?

BIOS evolved slowly. After decades it still had to drop into 16-bit real mode to initialize, and there was no unified firmware interface specification across the many vendors, so everyone kept relying on the old BIOS approach. Intel later designed UEFI, which defined a unified interface specification, improved software operability by design, and removed BIOS's limitations—these advanced extensions broke the deadlock BIOS had been stuck in.

UEFI works much like BIOS, but with more advanced features, and it makes updating hardware firmware easy.

After UEFI initialization, it also locates a disk from its settings—but instead of the MBR code on a boot partition, it finds the EFI partition and launches an EFI program from the corresponding directory. That EFI boot program is the system's bootloader.

This loading scheme is clearly more secure. Some viruses used to modify the MBR code to embed themselves deep in the system. Under UEFI, if the boot program is loaded with a signature, UEFI verifies it, so a virus can no longer implant itself by tampering with the boot program.

Classical Civilization

This part covers the bootloader.

Common bootloaders

GRUB

GRUB is a very popular Linux bootloader. It can boot different operating systems, such as Linux, Windows, and FreeBSD.

Once GRUB starts, it loads its modules, reads its configuration file, and shows the bootable entries. Alternatively you can switch to its interactive command mode and issue commands like in a shell to specify how to boot the system.

LILO

LILO is a Linux bootloader older than GRUB, and some distributions still use it. It can also load different operating systems, but it's more minimal than GRUB in terms of features.

BOOTMGR

BOOTMGR is the bootloader that appeared around the release of Windows Vista. It is a successor to NTLDR, loads the kernel according to the BCD configuration, and was designed for better compatibility with 64-bit systems.

NTLDR

This is the bootloader used by Windows XP and earlier. Its job is to parse the BOOT.INI configuration file and load the kernel.

What a bootloader does

Taking GRUB as an example for convenience: after GRUB starts, it reads its configuration and prints a menu listing the bootable systems. When the user selects an entry, it boots the kernel with the parameters configured for that entry. These parameters passed to the OS can be edited directly, and the edited settings are passed on to the kernel.

How GRUB loads

GRUB loading here refers to booting via BIOS.

  • After the GRUB code in the MBR partition is copied to RAM address 0x00007c00, GRUB starts running
  • This small piece of GRUB code runs in real mode and sets up its own stack
  • It reads the configuration from disk, then loads the first 512 bytes of the Linux kernel file and copies them to RAM address 0x00090000 (by convention, this part of the kernel has its entry point at relative offset 0x0200, i.e. 0x00090200)
  • It then copies the rest of the kernel to 0x00010000 (for kernels built with make zImage) or to 0x00100000 (for kernels built with make bzImage)
  • When copying is done, it jumps directly to 0x00090200 to execute code—the kernel entry point mentioned above

For kernels booted by UEFI, there's no need to copy and jump manually, because UEFI loads PE files. The PE file headers tell UEFI how to load the PE kernel, such as the entry address.

The role of the bootloader

GRUB itself is not an operating system; it is just a tool for loading one. This makes it easy to maintain the system and roll back to a previous kernel when a faulty kernel won't boot.

As the middle layer between the OS and BIOS/UEFI, the bootloader provides a bridging mechanism that avoids the awkward situation of being unable to repair the OS because the kernel itself is broken.

In Windows, NTLDR and BOOTMGR boot the kernel in a similar way. If booting fails, they show a boot error screen—that's the bootloader at work.

The Modern Era

After all these stages, we finally reach the operating system. What happens here varies by OS, but common operations typically include:

Operating system initialization

Switching from real mode to protected mode, including:

  • Initializing the GDT and LDT registers
  • Setting up PTE mappings for paging
  • Setting up the TSS task-management mechanism
  • Setting up the IDT to manage IRQ interrupts
  • Initializing the file system
  • Initializing I/O devices

The operating-system part is fairly large; I may cover it in a later post.

I also have an unfinished demo on GitHub—click here to take a look.

References: