A field firmware update is a feature that must always work – including when someone pulls the power halfway through writing, or the radio link drops. Below is how we design a custom bootloader for STM32 and which mistakes we guard against. When image authentication and encryption are required, we start from ST’s ready-made solutions (link at the end), but the principles stay the same.
1. Flash layout
The simplest robust layout has three areas:
- the bootloader at the start of flash (boot address
0x0800 0000) – small, rarely changed, write-protected; - the active slot holding the application;
- the download slot, where the application writes the new image – the old one keeps running until the new one has been verified.
Area boundaries must match the erase units: 2 KB pages on STM32G0, 16, 64 and 128 KB sectors on STM32F4 – on F4 this often forces a non-obvious layout. Families with two flash banks (e.g. STM32L4, G4, H7) can swap banks, which simplifies switching versions and lets you write one bank while executing from the other.
2. Jumping to the application correctly
This is where bootloaders tend to work “almost always”. The application must start in a state close to that after reset:
#define APP_ADDR 0x08008000u
#define RAM_START 0x20000000u
#define RAM_END (RAM_START + 144u * 1024u) /* the device's SRAM size */
typedef void (*entry_t)(void);
void jump_to_app(void)
{
uint32_t sp = *(volatile uint32_t *)APP_ADDR;
uint32_t pc = *(volatile uint32_t *)(APP_ADDR + 4u);
if (sp <= RAM_START || sp > RAM_END) {
return; /* no valid application */
}
HAL_RCC_DeInit(); /* system clock back to HSI/MSI */
HAL_DeInit(); /* reset peripherals */
__disable_irq();
SysTick->CTRL = 0;
SysTick->LOAD = 0;
SysTick->VAL = 0;
for (uint32_t i = 0; i < sizeof(NVIC->ICER) / sizeof(NVIC->ICER[0]); i++) {
NVIC->ICER[i] = 0xFFFFFFFFu; /* disable interrupts */
NVIC->ICPR[i] = 0xFFFFFFFFu; /* clear pending */
}
SCB->VTOR = APP_ADDR;
__DSB();
__ISB();
__set_MSP(sp);
((entry_t)pc)();
}
What we watch for:
- Order.
HAL_RCC_DeInit()usesHAL_GetTick()for its timeouts, so we call it before stopping SysTick and masking interrupts. - PRIMASK.
__disable_irq()stays in effect after the jump – the application must call__enable_irq()early, or no interrupt (including the HAL SysTick) will fire. - VTOR on the application side. The application is linked at the slot address (
FLASH ORIGINin the linker script), and insystem_stm32xxxx.cwe setUSER_VECT_TAB_ADDRESSandVECT_TAB_OFFSETso thatSystemInit()does not restore the bootloader’s vector table. - Vector table alignment. The table address must be aligned to a power of two no smaller than the table size – e.g. 512 B with about 100 interrupts (Cortex-M4/M7), 256 B on STM32G0 (Cortex-M0+). The start of a flash page or sector satisfies this.
- STM32F0 (Cortex-M0) has no VTOR – the application copies its vector table to the start of SRAM and switches the memory mapping (
SYSCFG_CFGR1.MEM_MODE) to SRAM.
3. Image header and integrity
A bootloader must not jump to an image just because its first word looks like a stack pointer. Every image carries a header with a magic value, version, size and CRC32 (or a SHA-256 hash and signature when authentication is needed). We check it before every jump and after every download.
Watch out for the hardware CRC unit: by default it computes CRC-32 with polynomial 0x04C11DB7 and initial value 0xFFFFFFFF, but without bit reversal and without a final XOR. The result therefore differs from the “usual” CRC32 (zlib, Python binascii.crc32) used by PC tools. On families with a configurable CRC (e.g. G0, L4, G4) we enable input and output reversal (REV_IN, REV_OUT) and XOR the result with 0xFFFFFFFF – or compute the CRC with the same algorithm on both sides.
4. Power-loss safety
- The old image stays intact until the end. The new image goes into the download slot. Only after the CRC check does the bootloader copy it or swap banks.
- Update state stored in flash. After a reset the bootloader must know whether a copy was in progress – we store state markers in a separate page so every step can be repeated.
- Confirming the new version (rollback). A new image starts as a “trial”. The application confirms it only once it has started correctly (e.g. established communication). If there is no confirmation after a few resets – e.g. from the watchdog – the bootloader reverts to the previous version. This requires keeping both images.
- Watchdog and erase time. Erasing a large sector (e.g. 128 KB on STM32F4) can take several seconds, and reads from the same bank stall meanwhile. The IWDG period must allow for that, or the erase must be split up with the watchdog refreshed between operations.
- Write granularity. STM32G0 and L4 program flash in double words (64 bits), STM32H7 in 256-bit flash words. Align the write buffer and the image size to that unit.
5. Protection and a recovery path
- WRP (write protection in the option bytes) over the bootloader area – a bug in the application cannot erase the bootloader.
- RDP – level 1 blocks flash readout through the debugger; level 2 disables debug permanently and is irreversible on the classic families. AN5156 discusses how to choose.
- The system bootloader in ROM (AN2606) – every STM32 has a factory bootloader supporting USART and, depending on the family, USB DFU, I²C, SPI or CAN. If the board keeps access to the BOOT0 pin and the right interface, the device can be recovered even if your own bootloader is damaged. With RDP level 2 the system bootloader is not available.
RedCan checklist: bootloader and updates
- Flash layout aligned to page/sector boundaries, a separate page for update state.
- Jump to application: clocks and peripherals de-initialised, SysTick and NVIC cleared, VTOR, MSP.
- Application: correct address in the linker script, VTOR in SystemInit,
__enable_irq(). - Image header with version, size and CRC/signature, checked before every jump.
- CRC computed with the same algorithm on the PC and on the STM32.
- New image in a separate slot, confirmation after start-up, automatic rollback.
- Test: cutting power at random points during an update (ideally automated, hundreds of times).
- Watchdog allows for flash erase time.
- WRP on the bootloader, RDP level chosen deliberately.
- Access to BOOT0 and a system-bootloader interface as a recovery path.
Related reading: DMA and D-cache coherency and low-power Stop-mode design.
Further reading
- AN2606 – Introduction to system memory boot mode on STM32 MCUs (STMicroelectronics) – the ROM bootloader, activation conditions and supported interfaces for every family.
- AN5156 – Introduction to security for STM32 MCUs (STMicroelectronics) – RDP, WRP, PCROP and building a chain of trust.
- X-CUBE-SBSFU – ST’s reference secure boot and secure firmware update.
- The family reference manual – the FLASH (erase, programming, option bytes) and CRC chapters.
We design bootloaders and OTA/DFU updates as part of firmware and embedded software development.