0
0
Cnc-programmingHow-ToBeginner · 4 min read

How Does ARM Bootloader Work: Simple Explanation and Example

An ARM bootloader is a small program that runs first when an ARM device powers on. It initializes the hardware, sets up memory, and loads the main operating system or firmware into memory to start execution.
📐

Syntax

The ARM bootloader process involves several stages, typically starting with a primary bootloader in ROM, followed by a secondary bootloader in flash or storage. The basic flow is:

  • Reset Vector: The CPU starts execution here after reset.
  • Primary Bootloader: Initializes minimal hardware and loads the next stage.
  • Secondary Bootloader: Sets up more hardware and loads the OS kernel.
  • OS Kernel: The main operating system starts running.

Each stage has a specific role and location in memory or storage.

c
/* Pseudocode for ARM bootloader stages */
void reset_vector() {
    initialize_minimal_hardware();
    load_secondary_bootloader();
    jump_to_secondary_bootloader();
}

void secondary_bootloader() {
    initialize_full_hardware();
    load_os_kernel();
    jump_to_os_kernel();
}
💻

Example

This example shows a simple ARM bootloader sequence in C-like pseudocode. It demonstrates initializing hardware and loading the next stage from memory.

c
void reset_vector() {
    // Minimal hardware setup
    setup_clock();
    setup_memory_controller();

    // Load secondary bootloader from flash to RAM
    copy_memory(FLASH_ADDRESS, RAM_ADDRESS, SIZE);

    // Jump to secondary bootloader
    void (*next_stage)() = (void (*)())RAM_ADDRESS;
    next_stage();
}

void secondary_bootloader() {
    // Full hardware initialization
    setup_peripherals();
    setup_interrupts();

    // Load OS kernel
    copy_memory(OS_FLASH_ADDRESS, OS_RAM_ADDRESS, OS_SIZE);

    // Jump to OS kernel
    void (*os_kernel)() = (void (*)())OS_RAM_ADDRESS;
    os_kernel();
}
⚠️

Common Pitfalls

Common mistakes when working with ARM bootloaders include:

  • Incorrect memory addresses causing failure to load the next stage.
  • Not properly initializing hardware like clocks or memory controllers.
  • Skipping cache or MMU setup leading to unstable execution.
  • Forgetting to disable interrupts before jumping to the next stage.

Always verify memory layout and hardware initialization order carefully.

c
/* Wrong: Jumping before hardware setup */
void reset_vector_wrong() {
    void (*next_stage)() = (void (*)())RAM_ADDRESS;
    next_stage(); // Jump without setup causes failure
}

/* Right: Setup before jump */
void reset_vector_right() {
    setup_clock();
    setup_memory_controller();
    void (*next_stage)() = (void (*)())RAM_ADDRESS;
    next_stage();
}
📊

Quick Reference

Key points to remember about ARM bootloaders:

  • The bootloader runs immediately after reset at the reset vector.
  • It initializes essential hardware like clocks and memory.
  • It loads the next stage (secondary bootloader or OS) from non-volatile storage to RAM.
  • Proper memory addresses and hardware setup are critical for success.
  • Bootloader stages can vary by device but follow this general pattern.

Key Takeaways

The ARM bootloader starts execution at the reset vector to initialize hardware and load the OS.
Bootloader stages include primary (in ROM) and secondary (in flash or storage) loaders.
Correct memory setup and hardware initialization are essential to avoid boot failures.
The bootloader copies the OS kernel from storage to RAM and transfers control to it.
Each ARM device may have a slightly different bootloader sequence but follows this core process.