A bootloader is the first code that runs when your device powers on. It decides what happens next. Understanding bootloaders is essential for any firmware developer.
What a bootloader does
When power is applied, the processor starts execution at a fixed address (the reset vector). At that address lives the bootloader. It checks if a firmware update is available. If yes, it downloads the new firmware and writes it to flash. If no, it jumps to the main application.
Many devices come with a factory bootloader (like STM32's built-in DFU bootloader). For production devices, you typically write your own.
Memory layout
Flash memory layout:
┌─────────────────┐ 0x08000000
│ Bootloader │ (e.g., 32KB)
├─────────────────┤ 0x08008000
│ Application │ (remaining flash)
│ │
└─────────────────┘
The bootloader lives at the start of flash. The application lives at a higher address. The bootloader must know where the application starts in order to jump to it.
Jumping to the application
typedef void (*pFunction)(void);
void jump_to_application(uint32_t app_address) {
uint32_t *stack_ptr = (uint32_t *)app_address;
uint32_t *reset_handler = (uint32_t *)(app_address + 4);
// Set the main stack pointer
__set_MSP(*stack_ptr);
// Jump to the reset handler
pFunction app_reset = (pFunction)(*reset_handler);
app_reset();
}
Secure firmware updates
For production devices, sign your firmware images with a private key. The bootloader has the corresponding public key and verifies the signature before accepting an update. This prevents loading unauthorized firmware.
Use a cryptographic library like mbedTLS. Store signing keys in a hardware security module or at minimum in a write-protected memory region.
A/B firmware updates
A robust approach keeps two application partitions. The bootloader runs the primary. During an update, the new firmware is written to the secondary partition. After a successful verification, the bootloader switches the active partition. If the new firmware fails, it rolls back automatically.



