Add a production-ready, educational bootloader extension for STM32 MCUs. Core features: - Custom binary protocol with CRC16/CRC32 verification - AES-256 encryption for firmware security - Dual-bank firmware management with rollback support - Version management and firmware validation - Modular architecture with BSP abstraction layer Project structure: - include/: Header files (bootloader.h, bsp_flash.h, bsp_uart.h) - src/: Core implementation (bootloader, protocol, crypto, firmware manager) - port/: MCU-specific adaptation layer (STM32F4xx) - docs/: Documentation (integration guide, porting guide) Supported platforms: - STM32F4xx (primary) - STM32F1xx (via porting) Quick start: 1. Copy extension module to project 2. Configure bootloader_config.h 3. Modify linker script for APP_BASE_ADDR 4. Build and flash bootloader to 0x08000000 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
102 lines
3.2 KiB
CMake
102 lines
3.2 KiB
CMake
cmake_minimum_required(VERSION 3.22)
|
|
|
|
|
|
|
|
set(MCU_FAMILY "stm32f4xx" CACHE STRING "MCU family")
|
|
|
|
set_property(CACHE MCU_FAMILY PROPERTY STRINGS
|
|
"stm32f4xx"
|
|
"stm32f1xx"
|
|
)
|
|
|
|
option(BOOT_ENABLE_AES "Enable AES encryption" ON)
|
|
option(BOOT_ENABLE_CRC "Enable CRC verification" ON)
|
|
option(BOOT_ENABLE_DUAL_BANK "Enable dual bank support" ON)
|
|
option(BOOT_ENABLE_VERSION_MGR "Enable version management" ON)
|
|
option(BOOT_ENABLE_LOG "Enable logging" ON)
|
|
|
|
add_library(bootloader STATIC)
|
|
|
|
set(BOOTLOADER_CORE_SRC
|
|
${CMAKE_CURRENT_SOURCE_DIR}/src/bootloader.c
|
|
${CMAKE_CURRENT_SOURCE_DIR}/src/bsp_flash.c
|
|
${CMAKE_CURRENT_SOURCE_DIR}/src/bsp_uart.c
|
|
# ${CMAKE_CURRENT_SOURCE_DIR}/src/protocol.c
|
|
# ${CMAKE_CURRENT_SOURCE_DIR}/src/crc_check.c
|
|
# ${CMAKE_CURRENT_SOURCE_DIR}/src/firmware_mgr.c
|
|
# ${CMAKE_CURRENT_SOURCE_DIR}/src/boot_jump.c
|
|
)
|
|
|
|
# 加密模块(条件编译)
|
|
if(BOOT_ENABLE_AES)
|
|
list(APPEND BOOTLOADER_CORE_SRC
|
|
${CMAKE_CURRENT_SOURCE_DIR}/src/aes_crypto.c
|
|
)
|
|
endif()
|
|
|
|
|
|
set(BOOTLOADER_PORT_SRC
|
|
${CMAKE_CURRENT_SOURCE_DIR}/port/${MCU_FAMILY}/flash_port.c
|
|
${CMAKE_CURRENT_SOURCE_DIR}/port/${MCU_FAMILY}/uart_port.c
|
|
)
|
|
|
|
target_sources(bootloader PRIVATE
|
|
${BOOTLOADER_CORE_SRC}
|
|
${BOOTLOADER_PORT_SRC}
|
|
)
|
|
|
|
target_include_directories(bootloader PUBLIC
|
|
${CMAKE_CURRENT_SOURCE_DIR}/include
|
|
${CMAKE_CURRENT_SOURCE_DIR}/port/${MCU_FAMILY}
|
|
)
|
|
|
|
if(BOOT_ENABLE_AES)
|
|
target_compile_definitions(bootloader PRIVATE BOOT_ENABLE_AES=1)
|
|
else()
|
|
target_compile_definitions(bootloader PRIVATE BOOT_ENABLE_AES=0)
|
|
endif()
|
|
if(BOOT_ENABLE_CRC)
|
|
target_compile_definitions(bootloader PRIVATE BOOT_ENABLE_CRC=1)
|
|
else()
|
|
target_compile_definitions(bootloader PRIVATE BOOT_ENABLE_CRC=0)
|
|
endif()
|
|
if(BOOT_ENABLE_DUAL_BANK)
|
|
target_compile_definitions(bootloader PRIVATE BOOT_ENABLE_DUAL_BANK=1)
|
|
else()
|
|
target_compile_definitions(bootloader PRIVATE BOOT_ENABLE_DUAL_BANK=0)
|
|
endif()
|
|
if(BOOT_ENABLE_VERSION_MGR)
|
|
target_compile_definitions(bootloader PRIVATE BOOT_ENABLE_VERSION_MGR=1)
|
|
else()
|
|
target_compile_definitions(bootloader PRIVATE BOOT_ENABLE_VERSION_MGR=0)
|
|
endif()
|
|
if(BOOT_ENABLE_LOG)
|
|
target_compile_definitions(bootloader PRIVATE BOOT_ENABLE_LOG=1)
|
|
else()
|
|
target_compile_definitions(bootloader PRIVATE BOOT_ENABLE_LOG=0)
|
|
endif()
|
|
|
|
if(MCU_FAMILY STREQUAL "stm32f4xx")
|
|
target_compile_definitions(bootloader PRIVATE MCU_FAMILY_STM32F4XX=1)
|
|
elseif(MCU_FAMILY STREQUAL "stm32f1xx")
|
|
target_compile_definitions(bootloader PRIVATE MCU_FAMILY_STM32F1XX=1)
|
|
endif()
|
|
|
|
target_link_libraries(bootloader PUBLIC
|
|
stm32cubemx
|
|
)
|
|
target_compile_options(bootloader PRIVATE
|
|
-Wall # 所有警告
|
|
-Wextra # 额外警告
|
|
-Wpedantic # 严格ISO C标准
|
|
-fdata-sections # 数据分段(链接器优化)
|
|
-ffunction-sections # 函数分段(链接器优化)
|
|
)
|
|
message(STATUS "=== Bootloader Configuration ===")
|
|
message(STATUS "MCU Family: ${MCU_FAMILY}")
|
|
message(STATUS "AES Encryption: ${BOOT_ENABLE_AES}")
|
|
message(STATUS "CRC Verification: ${BOOT_ENABLE_CRC}")
|
|
message(STATUS "Dual Bank: ${BOOT_ENABLE_DUAL_BANK}")
|
|
message(STATUS "Version Manager: ${BOOT_ENABLE_VERSION_MGR}")
|
|
message(STATUS "Logging: ${BOOT_ENABLE_LOG}")
|
|
message(STATUS "===============================") |