What is the STM32 boot process and the system bootloader?
This article provides information about the STM32 boot process. In a first part, it gives you an overview of this process and it presents the different boot modes. In a second part, it details you the STM32 system bootloader and the STM32 boot from internal user flash memory. Please note that the STM32MP1 isn’t covered by this FAQ. For more information, see the dedicated wiki page.
1. STM32 boot process overview
All the STM32 microcontrollers have the capability to boot from:
main flash memory, usually where your firmware is located: at the default value address 0x80000000 or a value define in option byte (if possible).
ST embedded bootloader located in the system flash memory: a code flashed at production and which can’t be modified in any manner.
SRAM: usually used for debugging purpose, or a specific action needing high performance or no-access to flash.
For the STM32, these are the only three possible boot targets.
1.1 How to select the boot target?
The answer is available in the reference manual of your STM32 in the chapter Boot configuration.
Example of boot configuration definition for a STM32G071:
Warning: in RDP level 2, the device can only boot from main flash memory
1.2 Details about boot process
When an STM32 is powered on, the embedded flash memory automatically loads the option bytes. During the option bytes loading sequence, the device remains under reset and the embedded flash memory can’t be accessed. The values on the BOOT pin are latched on the 4th rising edge of SYSCLK after reset release and then the boot mode configuration is resolved.
2. STM32 system bootloader
The bootloader is stored in the internal boot ROM (system memory part of the flash) of any STM32 device, and is programmed by ST during production. Its main task is to download the application program to the internal flash memory through one of the available serial peripherals, such as USART, CAN, USB, I2C, or SPI.
Flash loader demonstrator: (UART) considered in maintenance.
3. STM32 startup from main flash memory
All STM32 are based on Arm Cortex-M core. The boot process of this core is:
Take the initial value of the MSP from the address 0x0000_0000.
Take the value of the PC from the address 0x0000_0004.
Continue execution from the address corresponding to this value.
On many STM32 families, the boot address in the internal flash is 0x8000000. This address is remapped to address 0x00000000 by boot mode mechanism. The address located at 0x0000_0004 will point on the reset handler.
On some families like STM32H7 or STM32L5, the boot address could be different and specified thanks to the option bytes. Keep in mind, the first 32bits word of your binary is the Main stack pointer location and the second 32bits word is the address or the reset handler. The reset handler implementation could be found in an assembly file named for example startup_stm32g071xx.s (naming depends on the targeted device). This one should be present in your STM32 project. Depending on your IDE, you can find the implementation in the CMSIS package include in the STM32CubeFirmware.