The bootloader initiates the boot process of a computer. It typically operates in multiple stages to transition the system from its initial state to running the operating system.
1 First-Stage Bootloader (MBR Bootloader)
The first-stage bootloader is very small, usually 512 bytes in size, and resides in the Master Boot Record (MBR) of a storage device.
Purpose: Initialize basic hardware and load the second-stage bootloader.
Location: The first 512 bytes of the bootable storage device (e.g., hard drive, USB stick).
Responsibilities:
- Perform minimal hardware initialization.
- Load the second-stage bootloader from a known location on the disk.
- Pass control to the second-stage bootloader.
Limitations: Due to its small size, it has very limited functionality and relies on the second-stage bootloader to perform more complex tasks.
2 Second-Stage Bootloader
The second-stage bootloader is more complex and capable than the first-stage bootloader.
Purpose: Provide more extensive hardware initialization, load the operating system kernel into memory, and transition the system to a state where the OS can take over.
Location: Located on the storage device, usually immediately following the first-stage bootloader or in a specific partition.
Responsibilities:
- Initialize additional hardware components (e.g., keyboard, display).
- Set up the memory map and load the OS kernel into memory.
- Transition the CPU from real mode to protected mode or long mode, as required by the OS.
- Pass control to the OS kernel.
Features: Can provide a user interface to select different operating systems or kernels, configure boot parameters, and more.
Detailed Steps in Each Stage
First-Stage Bootloader (MBR Bootloader):
- BIOS/UEFI Initialization: The system firmware (BIOS or UEFI) initializes hardware components and performs a Power-On Self Test (POST).
- Load MBR: The BIOS/UEFI reads the MBR from the boot device into memory at address
0x7C00
and transfers control to it. - MBR Code Execution: The MBR code initializes minimal hardware and locates the second-stage bootloader. This often involves reading the partition table and finding the active partition.
Second-Stage Bootloader:
- Load Second-Stage Loader: The MBR code loads the second-stage bootloader from the disk into memory.
- Hardware Initialization: The second-stage bootloader initializes more hardware components, such as graphics and input devices.
- Memory Setup: It sets up memory management, possibly including the Global Descriptor Table (GDT) and transitioning to protected mode if required.
- Load Kernel: The bootloader reads the OS kernel and optional initial RAM disk into memory.
- Kernel Parameters: It sets up any necessary parameters and environment for the kernel.
- Jump to Kernel: Finally, the bootloader transfers control to the OS kernel by jumping to its entry point.