How davidgfnet/stm32-dfu-bootloader works
Project Type and Purpose
• This is an embedded‐firmware project: a minimal USB DFU bootloader for the STM32F103 microcontroller.
• Purpose: allow flashing of arbitrary user payloads over USB, enforce integrity checks and various protections, then jump to a validated user application.
File Structure Analysis
• Top‑level files:
• main.c, init.c, usb.c, watchdog.h, flash.h, reboot.h – core bootloader logic
• stm32f103.ld – linker script placing code at 0x08000000
• Makefile, .github/workflows/flavour-build.yml – build & CI
• checksum.py – helper for computing payload checksums
• upgrade/ – sample “client” project to bundle/send firmware via DFU
• .gitmodules → libopencm3 submodule under upgrade/libopencm3
• Patterns:
• Single binary bootloader <4 KB at flash base
• Linker‑script–driven memory mapping
• Compile‑time flags controlling features (DFU upload, write‑protect, etc.)
• Upgrade folder demonstrates how an external host tool interacts via DFU
Key Insights from README
• Boot strategy: on reset, read RAM magic or GPIO, watchdog reset, or NRST pin → DFU mode
• Protection: RDP, write‑protections, safe‑erase, checksum validation
• Payload format: vector table offset 0x1000, size @0x20, XOR checksum zero
• Config flags set compile‑time via Makefile
Components
a. Main Components
1. MCU Core (STM32F103)
• CPU, NVIC, VTOR
2. Memory Subsystems
• Flash[0x0800_0000–0x0800_1000): Bootloader region
• Flash[0x0800_1000–end): User application region
• SRAM[0x2000_0000–): RAM for stack + magic tag
• Option bytes / RDP protection registers
3. Peripherals
• USB DFU interface (USB FS peripheral)
• GPIO DFU‐boot pin
• Independent Watchdog (IWDG)
4. External Host
• PC running DFU client (e.g., upgrade/client.c or dfu-util)
b. Relationships & Data Flows
• On reset → Bootloader reads:
• Last 8 bytes of SRAM (magic tag)
• GPIO pin state or NRST cause
• If DFU mode:
• USB ←→ DFU state machine → erase/write flash → optional upload
• On valid payload:
• Check stack pointer, size, checksum
• If success → set VTOR to 0x0800_1000 → jump to app
• Watchdog timeout → reset → forced DFU
• Option bytes logic sets RDP/write‑protect → hardware block
c. Architectural Patterns & Principles
• Memory‑mapped bootloader vs. application
• State machine for DFU commands
• Minimalist code (<4 KB) for reliability and fit in protected region
• Compile‑time feature toggles for security/performance trade‑offs
d. Technologies & Frameworks
• C with libopencm3 (via submodule)
• GNU Make + linker scripts
• USB DFU protocol (USB descriptors in usb.c)
• Python helper for checksum (checksum.py)
Diagramming Conventions
• Use rectangles for major blocks (MCU, Memory, Host)
• Blue: bootloader logic
• Green: user application region
• Orange: hardware peripherals
• Gray: configuration/option bytes
Detailed Capture of Essential Elements
• Reset handler & init
• DFU state machine
• Protection enforcement (RDP, write‑prot)
• Jump logic to application
• Interactions:
• Magic‑tag in SRAM
• GPIO and NRST checks
• Watchdog service/reset
• Upgrade/client role as example host component that drives DFU sequence.