Nucleo-F401RE: a boot loader with A/B slots and CRC
A boot loader checks two firmware images with the STM32's CRC unit, rejects the newer one because a word was written wrong, and starts the older, intact one.
Firmware · Advanced
In a real product: Field-updatable controller (Energy): A charger or inverter keeps two firmware slots, so a failed update never leaves it unable to start.
A device that updates itself in the field must never be left unable to boot, even if the power fails halfway through writing new firmware. The usual answer is two slots. The update goes into the slot that isn't running, and at every start the boot loader checks both images and runs the newest one that is intact.
Here the two images are in this program's flash. Slot A holds version 3, but one word of it was written wrong. Slot B holds version 2, intact. The boot loader checks each with the STM32's hardware CRC unit, refuses A, and starts B. B's application blinks the LED on D7.
What you'll learn
- An image header: magic number, version, length, the CRC of the payload, and the entry point
- Driving the STM32F4's CRC unit (
CRC->CR,CRC->DR): CRC-32 with polynomial 0x04C11DB7, a word at a time - Checking the hardware against a CRC computed in software, bit by bit
- Choosing a slot: the newest valid version wins, and a bad image falls back to the other one
- Why a CRC proves integrity but not authenticity, and what a secure boot adds
How it works
Each slot starts with a header. The magic word says a slot holds an image at all, which an erased slot (all FF) does not. The version decides which slot is newer. The CRC was computed when the image was built, over the eight payload words.
At boot the loader resets the CRC unit (CR = RESET, which loads 0xFFFFFFFF), writes the payload's words one by one into DR, and reads DR back. If that isn't the CRC in the header, the image is damaged. Slot A's word 3 has one bit flipped (3000 became 3016), which changes the CRC completely. That is the job of a CRC: any single-bit error, and any burst of errors up to 32 bits long, is always caught.
The loader also runs the same calculation in software, so you can see the two agree. Then it calls the chosen image's entry point. A real boot loader would set the stack pointer and VTOR from the image's own vector table and jump to its reset handler. Here the entry is a function in this program, so everything runs in one build.
A CRC only catches accidents. Anyone can change an image and compute a new CRC. Secure boot stores a public key in the chip (in OTP memory, or in a boot loader that can't be overwritten) and checks a signature over the image, usually ECDSA over a SHA-256 hash. Only the holder of the private key can make a valid signature. Read protection (RDP) and write protection (WRP) then stop anyone from replacing the boot loader itself.
Try this
- Repair slot A (change 3016 back to 3000): the loader starts version 3 instead.
- Erase slot B's magic (0xFFFFFFFF) as well as breaking A: the loader finds nothing to start and blinks LD2 fast.
- Change one word in B's payload and compute its new CRC with
crc_software: write the value into B's header and it boots again. This is exactly why a CRC alone is not security.
main.c
// Bare-metal C on the Nucleo-F401RE: no Arduino core and no HAL. CMSIS names every register.
// A boot loader with two slots: each image's payload is checked with the CRC unit; the newest valid one starts.
#include <stdio.h>
#include "stm32f4xx.h"
#define MAGIC 0x54455746u // "TEFW"
#define WORDS 8
typedef struct {
uint32_t magic;
uint32_t version;
uint32_t crc; // CRC-32 (0x04C11DB7, start FFFFFFFF, no reflection) of the payload words
void (*entry)(void);
uint32_t payload[WORDS]; // the application's data: here a signature word and its settings
} Image;
static volatile uint32_t ms;
void SysTick_Handler(void) { ms++; }
static void delay_ms(uint32_t t) { uint32_t s = ms; while (ms - s < t) {} }
int __io_putchar(int ch) {
while (!(USART2->SR & USART_SR_TXE)) {}
USART2->DR = ch;
return ch;
}
// The two applications: each blinks the LED on D7 (PA8) at its own rate.
static void app_v3(void) { for (;;) { GPIOA->ODR ^= 1u << 8; delay_ms(150); } }
static void app_v2(void) {
printf("app v2 running\r\n");
for (;;) { GPIOA->ODR ^= 1u << 8; delay_ms(500); }
}
// Slot A: version 3, word 3 written wrong (3000 -> 3016). Slot B: version 2, intact.
static const Image slot_a = { MAGIC, 3, 0xE87DBD0Cu, app_v3, { 0x54455633, 1500, 2200, 3016, 0x00C0FFEE, 850, 6500, 92 } };
static const Image slot_b = { MAGIC, 2, 0x7D50A5B7u, app_v2, { 0x54455632, 1500, 2100, 2900, 0x00C0FFEE, 800, 6200, 95 } };
static uint32_t crc_hardware(const uint32_t *w, int n) {
CRC->CR = CRC_CR_RESET; // back to 0xFFFFFFFF
for (int i = 0; i < n; i++) CRC->DR = w[i];
return CRC->DR;
}
static uint32_t crc_software(const uint32_t *w, int n) {
uint32_t crc = 0xFFFFFFFF;
for (int i = 0; i < n; i++) {
crc ^= w[i];
for (int b = 0; b < 32; b++) crc = crc & 0x80000000u ? (crc << 1) ^ 0x04C11DB7u : crc << 1;
}
return crc;
}
static int valid(const char *name, const Image *img) {
if (img->magic != MAGIC) { printf("slot %s: empty\r\n", name); return 0; }
uint32_t hw = crc_hardware(img->payload, WORDS), sw = crc_software(img->payload, WORDS);
int ok = hw == img->crc && (uint32_t)img->entry >= FLASH_BASE && (uint32_t)img->entry < FLASH_BASE + 0x80000;
printf("slot %s: version %lu, CRC 0x%08lx (software 0x%08lx), header says 0x%08lx: %s\r\n", name,
(unsigned long)img->version, (unsigned long)hw, (unsigned long)sw, (unsigned long)img->crc,
ok ? "valid" : "DAMAGED");
return ok;
}
int main(void) {
SysTick_Config(SystemCoreClock / 1000);
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_CRCEN;
RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
// PA5 (LD2) and PA8 (D7) outputs, PA2 USART2 TX.
GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER5 | GPIO_MODER_MODER8 | GPIO_MODER_MODER2))
| GPIO_MODER_MODER5_0 | GPIO_MODER_MODER8_0 | GPIO_MODER_MODER2_1;
GPIOA->AFR[0] = (GPIOA->AFR[0] & ~(0xFu << 8)) | (7u << 8);
USART2->BRR = SystemCoreClock / 115200;
USART2->CR1 = USART_CR1_TE | USART_CR1_UE;
printf("Boot loader\r\n");
int a = valid("A", &slot_a), b = valid("B", &slot_b);
const Image *pick = a && (!b || slot_a.version > slot_b.version) ? &slot_a : b ? &slot_b : 0;
if (!pick) {
printf("No valid image: staying in the boot loader\r\n");
for (;;) { GPIOA->ODR ^= 1u << 5; delay_ms(100); }
}
printf("Starting version %lu from slot %s\r\n", (unsigned long)pick->version, pick == &slot_a ? "A" : "B");
pick->entry();
}