BIOS vs EFI/UEFI in OS Dev

Let's first delve deep into what happens when the computer starts, right from the power on to complete finalization:

Computer Startup Process

1. Power Supply and Initial Hardware Checks

Power-On

  • Power Button Pressed: When you press the power button, an electrical circuit closes, allowing current to flow from the power supply to the motherboard and other components.
  • Power Good Signal: The power supply performs a self-test and sends a "Power Good" signal to the motherboard if all voltages are within the correct ranges. This signal ensures that the power supply is stable and ready.

System Reset

  • System Reset: Upon receiving the "Power Good" signal, the CPU is taken out of reset mode and starts executing instructions from a predefined address in its firmware, typically the BIOS or UEFI firmware.

2. BIOS/UEFI Initialization

BIOS/UEFI Firmware Execution

  • Firmware Start: The CPU begins executing the BIOS (Basic Input/Output System) or UEFI (Unified Extensible Firmware Interface) firmware code stored in ROM or flash memory on the motherboard.
  • POST (Power-On Self Test): The firmware runs a series of diagnostic tests to check the basic functionality of the hardware components, including the CPU, RAM, storage devices, and peripheral devices.

Hardware Initialization

  • Hardware Initialization: The firmware initializes hardware components, setting up system buses (like PCIe), configuring memory settings, and initializing the video card to display output.
  • Error Handling: If the POST encounters any errors, the BIOS/UEFI may emit a series of beeps (known as beep codes) or display error messages on the screen to indicate the problem.

3. Firmware Setup and Configuration

BIOS/UEFI Setup Utility

  • Accessing Setup Utility: During POST, the user can enter the BIOS/UEFI setup utility by pressing a specific key (e.g., Del, F2, Esc). This utility allows users to configure system settings like the system clock, boot order, and hardware configurations.
  • CMOS and NVRAM: Settings configured in the BIOS setup utility are stored in CMOS (Complementary Metal-Oxide-Semiconductor) memory, which is powered by a small battery, or in NVRAM (Non-Volatile Random Access Memory) in UEFI systems.

4. Boot Device Selection

Boot Sequence

  • Boot Order: The BIOS/UEFI firmware follows a predefined boot sequence to determine which device to boot from. This sequence can be configured in the setup utility.
  • Boot Device Detection: The firmware checks for bootable devices, such as hard drives, SSDs, USB drives, or network boot options.

5. Loading the Bootloader

MBR and GPT

  • MBR (Master Boot Record): In BIOS systems, the firmware reads the first sector of the bootable device (MBR), which contains the bootloader code.
  • GPT (GUID Partition Table): In UEFI systems, the firmware reads the EFI System Partition (ESP) to find and execute the bootloader file (e.g., BOOTX64.EFI).

Bootloader Execution

  • Executing the Bootloader: The firmware transfers control to the bootloader, which is a small program responsible for loading the operating system kernel into memory.
  • Loading the OS Kernel: The bootloader loads the operating system kernel and passes control to it. Common bootloaders include GRUB (GRand Unified Bootloader) for Linux systems and the Windows Boot Manager for Windows systems.

6. Operating System Initialization

Kernel Initialization

  • Kernel Initialization: The operating system kernel initializes system components, including memory management, process scheduling, and hardware abstraction.
  • Driver Loading: The kernel loads essential drivers required to interface with hardware devices like disk drives, network cards, and input devices.