Home / Learn Chip Programming / Writing a Bootloader: Concept and Memory Layout

Writing a Bootloader: Concept and Memory Layout

A tiny program at the start of flash that can start, verify or replace the application.

In shortA tiny program at the start of flash that can start, verify or replace the application.

Theory in plain words

Flash map0x08000000: bootloader (16 KB) with vectors0x08004000: header (size, version, CRC32)0x08004100: application vectors + code0x0803C000: config / log area
Flash map

What it is

A bootloader runs first after reset, decides what to do (start the app, enter update mode), optionally verifies the app, and jumps to it. The app lives at a higher flash address.

Why we use it

Field updates over UART/USB/OTA, safe rollback and firmware signing all need a bootloader.

How it works

Layout: [bootloader 16 KB][app 200 KB][config]. The bootloader reads the app's vector table, sets MSP, reloads VTOR and jumps to its reset handler. The app is linked at its own origin.

Where it is used

  • STM32 custom bootloader project
  • USB DFU / UART update
  • Secure boot

Boot decision

What does the bootloader do?Try it Yourself

A bootloader must never leave the user with a dead device: if the application is invalid it must fall back to update mode.

Jump to the application (Cortex-M)
#define APP_BASE 0x08004100u
typedef void (*app_t)(void);
void jump_to_app(void) {
    uint32_t sp = *(volatile uint32_t *)APP_BASE;           // word 0: initial stack pointer
    uint32_t pc = *(volatile uint32_t *)(APP_BASE + 4);     // word 1: reset handler
    if ((sp & 0x2FFE0000) != 0x20000000) return;            // sanity: SP must point into RAM
    __disable_irq();
    SysTick->CTRL = 0; NVIC->ICER[0] = 0xFFFFFFFF;          // de-initialise what the bootloader used
    SCB->VTOR = APP_BASE;                                   // application has its own vector table
    __set_MSP(sp);
    ((app_t)pc)();
}

Quick quiz

1. What must a bootloader set before jumping to the app?

2. If the app CRC fails...