</>EmbeddedInterview

How does a bootloader jump to the application on Cortex-M?

HardEmbedded C

Short answer: The bootloader validates the application image (magic number, size, CRC or signature), puts the system back into a reset-like state (de-initialise peripherals and clocks it changed, disable and clear all interrupts, stop SysTick), sets SCB->VTOR to the application's vector table, loads the main stack pointer from the first word of that table, and branches to the reset handler whose address is in the second word.

#include <stdint.h>
#include "stm32f4xx.h"              /* or the CMSIS header for your MCU */

#define APP_BASE 0x08008000u

static void jump_to_app(void) __attribute__((noreturn));
static void jump_to_app(void)
{
    const uint32_t *vt = (const uint32_t *)APP_BASE;
    uint32_t app_sp    = vt[0];
    uint32_t app_reset = vt[1];

    __disable_irq();
    SysTick->CTRL = 0;                               /* stop SysTick */
    for (unsigned i = 0; i < 8; i++) {               /* disable and clear every NVIC interrupt */
        NVIC->ICER[i] = 0xFFFFFFFFu;
        NVIC->ICPR[i] = 0xFFFFFFFFu;
    }
    /* HAL_RCC_DeInit(); deinit peripherals the bootloader used (UART, USB, DMA) */

    SCB->VTOR = APP_BASE;                            /* application's vector table */
    __DSB();
    __ISB();

    __set_MSP(app_sp);                               /* from here on, no stack use in C */
    __enable_irq();                                  /* the app expects interrupts enabled after reset (PRIMASK = 0) */
    ((void (*)(void))app_reset)();                   /* bit 0 of the address is already set (Thumb) */
    for (;;) { }
}

Validation before jumping

  • Check that app_sp points into RAM and app_reset points into the application's flash region; an erased flash reads 0xFFFFFFFF.
  • Verify a CRC or a cryptographic signature over the image (secure boot).

Details that cause real-world bugs

  • Setting MSP from C: after __set_MSP, any local variable stored on the old stack is invalid. Load the reset address into a register first (as above, it is computed before), or do the last steps in a small assembly function.
  • Interrupts left enabled or pending from the bootloader fire in the application before it has set up their handlers.
  • Peripherals left running (DMA writing into RAM the application now uses, a USB stack still attached) cause random corruption.
  • Clock configuration: the application's SystemInit may assume reset clock settings; either restore them or make the application tolerant.
  • Privilege and stack selection: if the bootloader used the PSP or unprivileged mode, restore CONTROL to 0.
  • VTOR alignment: the application's vector table must be aligned as the core requires (e.g. 512 bytes on many parts).
  • Application linker script must start at APP_BASE, not at the start of flash.

A cleaner alternative used by some bootloaders: write a "boot application" flag to a .noinit RAM location or backup register, trigger a system reset (NVIC_SystemReset()), and have the bootloader jump immediately at the start of its reset handler before initialising anything. Then the application starts from a true reset state.