How I Structure a Bootloader for STM32
By Indeep Sidhu · Nov 11, 2026
The first time a field update goes wrong, you find out whether your device can recover on its own. If it can't, someone is driving out with a programmer. A small custom bootloader is the usual way to avoid that trip. This post covers how I structure one for STM32 and where it tends to bite. Treat the code as a sketch to adapt, not a drop-in.
The idea in one paragraph
The bootloader sits at the start of flash and the application sits after it. On reset the bootloader runs first and decides whether the application is valid. If it is, the bootloader hands over: point the vector table at the app, load the app's initial stack pointer, branch to its reset handler. If it isn't, the bootloader stays in update mode.

This works because of how Cortex-M starts. The core reads the initial stack pointer from the first word of the vector table and the reset handler address from the second word. On many STM32 families, main flash at 0x0800_0000 is aliased to address 0 at boot. On some families (the H7 and L5 are examples) the boot address comes from option bytes instead. Read the boot configuration chapter of your family's reference manual before you assume where your bootloader must sit.
Flash layout
Give every region one clear owner.

Two rules matter more than the exact numbers. Region boundaries must fall on erase-sector boundaries for your part, because you can only erase whole sectors or pages, and sector layouts differ between families. And keep the bootloader small. Everything you add to it is code you can't safely update later.
Linker script changes for the app
The application has to be linked to run from its own region, not from the start of flash. In the GNU linker script, shrink and move the FLASH region:
MEMORY
{
FLASH (rx) : ORIGIN = 0x08008200, LENGTH = 480K - 0x200
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
}The bootloader gets its own script with the complementary region: origin 0x08000000 and a length equal to the bootloader area. These values are examples. Use your chip's real sizes.
If you keep an image header in front of the app, reserve it as its own section or region. Either way, the vector table must be the first thing at the app's origin, or at a known offset from it.
Vector table relocation
Interrupts are dispatched through the vector table whose address lives in SCB->VTOR. If the app keeps running with VTOR pointing at the bootloader's table, every interrupt lands in the wrong handler. The classic symptom is an app that boots, prints its first message, and then hangs or hard-faults the moment the first interrupt fires.
ST's CMSIS startup code usually sets VTOR in SystemInit() from a VECT_TAB_OFFSET define in system_stm32xxxx.c. The exact name and comments differ per family, so check your own template file. Set it to the app's offset from the flash base:
/* system_stm32xxxx.c (check your family's template) */
#define VECT_TAB_OFFSET 0x8200UThe offset must respect the table's alignment requirement. Alignment depends on the number of exceptions and interrupts, rounded up to a power of two, and devices with many interrupts need more than you'd guess. The details are in the VTOR section of your core's programming manual, so check it before you pick the app origin.
I set VTOR in the app's startup and again in the bootloader's jump code. It's redundant, but harmless, and it removes a whole class of "works on my build" problems.
The jump sequence
The goal is to hand the app a chip that looks like it just came out of reset. A common sequence is: disable interrupts, de-initialize HAL and RCC, stop SysTick, clear NVIC enables and pendings, set VTOR, set MSP, jump.
typedef void (*app_entry_t)(void);
void boot_jump(uint32_t app_base)
{
uint32_t app_sp = *(volatile uint32_t *)(app_base);
uint32_t app_reset = *(volatile uint32_t *)(app_base + 4);
__disable_irq();
HAL_DeInit(); /* peripherals the bootloader used */
HAL_RCC_DeInit(); /* back to reset clock config */
SysTick->CTRL = 0;
SysTick->LOAD = 0;
SysTick->VAL = 0;
for (int i = 0; i < 8; i++) { /* enough words for your IRQ count */
NVIC->ICER[i] = 0xFFFFFFFFU;
NVIC->ICPR[i] = 0xFFFFFFFFU;
}
SCB->VTOR = app_base;
__set_MSP(app_sp); /* do this last, right before the branch */
((app_entry_t)app_reset)();
while (1) { } /* never reached */
}Check the stack pointer and reset address before you jump. SP should point into RAM, and the reset address should lie inside the app region. The order of the de-init steps is debated in ST community threads, so test yours on real hardware. On Cortex-M7 parts such as the H7, examples also disable the instruction and data caches first. Verify what your core needs.
Image validation
Never jump to something just because flash isn't empty. Validate first. I put a small header in front of the app:
typedef struct {
uint32_t magic; /* e.g. 0x424F4F54 */
uint32_t version;
uint32_t length; /* app length in bytes */
uint32_t crc32; /* over the app bytes */
} image_header_t;At boot, check the magic, check that the length fits the app region, then compute the CRC over the app and compare it. If any check fails, stay in the bootloader and wait for a new image.
Many STM32 parts have a hardware CRC peripheral, but not all, and its polynomial and settings must match whatever tool generates the CRC on your PC. A software CRC is fine for most image sizes. Have your build produce the CRC automatically so nobody patches it by hand.
Update safety: don't brick it
The bootloader is what rescues you, so protect it.
- Keep it out of the update path. The update code writes only to the app region and refuses any address outside it.
- Write-protect the bootloader sectors with the flash write-protection option bytes on parts that support them, so a bug in app or updater code can't erase it. Your reference manual explains how protection works and how to remove it.
- Make updates restartable. Invalidate the app first, write the new image, and write the valid header last. A power cut mid-update then leaves a clearly invalid image and the bootloader stays in update mode.
- Don't depend on the app to recover. If validation fails, the bootloader must be able to receive a new image on its own, over UART, USB or whatever transport you chose.

Walk through the failure cases on paper. A power cut during steps 1 to 3 leaves a missing or invalid header, so the next boot stays in update mode. A power cut during step 4 leaves a partial header, the CRC check fails, and you get the same result. Only a completed step 4 makes the app bootable.
A fallback to the previous firmware needs a second app slot and a more involved scheme. It costs flash, so decide early whether you need it.
Pitfalls
- Wrong region boundaries. Bootloader and app regions must align with erase sectors, or erasing the app can wipe part of the bootloader.
- Forgetting VTOR. The app runs, but interrupts misbehave or hard-fault.
- Leaving peripherals and SysTick running. A pending interrupt that fires after the jump runs the app's handler (or a missing one) at the wrong time.
- Setting MSP too early. Anything that uses the stack after
__set_MSPbut before the branch can corrupt it. Do it last. - Assuming ST's system bootloader behaves like yours. The built-in system memory bootloader is a separate thing, described in ST's AN2606. Jumping into it from user code has per-device addresses and caveats, so use the AN2606 tables for your device.
- Trusting one example. Jump code from forums and wikis is often tested on a single chip. Treat it as a starting point.
For a reference IAP implementation, start with ST's AN4657 (in-system programming over USART) and compare it against your design.
Takeaway
Keep the bootloader small and write-protected. Give the app its own linker region and vector table. Validate with a header and CRC before every jump, and leave the chip as close to reset as you can. Then test the unhappy paths: power loss mid-update, a corrupt image, an empty app region. That is what a bootloader is for.