Contents

Bootloaders in Computer

Bootloaders: High-Level Overview

1. What is a Bootloader?

  • A bootloader is a small program that runs before an operating system (OS) starts.
  • Its main job: initialize the system and load the main OS or application code into memory.
  • Found in PCs, embedded systems, smartphones, microcontrollers, routers, and more.

2. Why Do We Need a Bootloader?

  • Hardware is “dumb” at power-on—it needs instructions on what to do next.
  • The OS is usually stored on disk/flash; it cannot run directly from reset.
  • Bootloader bridges the gap: sets up hardware, locates OS image, loads it into RAM, and jumps to it.
  • Enables:
    • OS/app updates and recovery (flashing new images)
    • Secure boot (verify code signature before running)
    • Selection between multiple OSes (dual-boot)
    • Diagnostics or factory tests before main software

3. High-Level Bootloader Workflow

  1. System Reset / Power-On

    • CPU fetches the very first instructions from a fixed memory location (e.g., ROM, Flash, or reset vector).
  2. Stage 1: Minimal Hardware Initialization

    • Set up CPU registers, clock, and stack pointer.
    • Initialize memory controller, basic RAM access.
    • (Embedded) Configure essential peripherals (UART for debug, LEDs).
  3. Stage 2: Bootloader Code Execution

    • Bootloader code runs (in ROM/Flash or copied to RAM).
    • May present a menu or delay for user input (e.g., “Press F12 for boot menu”).
  4. Stage 3: Image Selection and Verification

    • Locate OS/application image (disk, flash, SD card, network).
    • (Secure systems) Verify image signature (cryptographic check, anti-tamper).
  5. Stage 4: Load OS/Application into Memory

    • Copy or map OS kernel (or app) into RAM.
    • Set up environment: memory map, device tree, arguments.
  6. Stage 5: Jump to Entry Point

    • Bootloader sets program counter to the OS/application entry point.
    • Transfers control—OS/application starts executing.

4. Real-World Bootloader Examples

4.1 PC Bootloaders

  • BIOS (legacy): First-stage bootloader in PC, initializes hardware, loads MBR from disk.
  • UEFI (modern): More complex, can load large executables, supports secure boot.
  • GRUB: Common Linux bootloader, presents boot menu, loads chosen OS kernel.

4.2 Embedded Bootloaders

  • MCU Boot ROM: Minimal bootloader built into chip, checks for valid user app, jumps to it or enters programming mode.
  • U-Boot: Widely used embedded Linux bootloader, supports many platforms, can boot from flash, network, or USB.
  • Microcontroller Bootloaders: Simple code to allow firmware update (UART, USB, I2C, CAN, etc.).

4.3 Smartphone Bootloaders

  • Android devices have multi-stage bootloaders (SPL, secondary, fastboot/recovery).
  • Secure boot enforces cryptographic signature checks.

5. Practical Example: Embedded Boot Sequence (MCU, e.g., STM32)

  1. Power-On Reset: CPU executes from Boot ROM.
  2. Boot ROM: Checks BOOT pins or flags; may jump to:
    • User Flash (user application)
    • System Memory (factory bootloader for programming)
    • SRAM (for debugging/testing)
  3. User Bootloader (optional): Receives firmware over UART/USB, writes to Flash, or jumps to main app.
  4. Main Application: Executes after bootloader finishes.

6. Bootloader Features (Advanced)

  • Fail-safe/Recovery: If the main app is corrupt, bootloader can enter update mode.
  • Dual-bank/dual-image: Two images for A/B updates (always bootable, even after failed update).
  • Secure boot: Checks digital signatures or hashes before running any code (prevents malware).
  • Chainloading: Bootloader loads another bootloader (e.g., UEFI loads GRUB, which loads Linux).

7. Summary Table

StepDescription
Reset VectorCPU fetches 1st instruction from fixed address
Stage 1 InitMinimal hardware setup (RAM, clock, peripherals)
Stage 2 BootloaderLoads/starts bootloader program
Image SelectionFinds and verifies OS/application image
Load/JumpLoads code into RAM and jumps to entry point
Application RunsOS or firmware executes as main program