Files
robinson c90c187749 feat: 完成 STM32 Bootloader 核心功能实现和测试框架
新增核心模块:
- bootloader.c: Bootloader 主逻辑,状态机,命令处理
- protocol.c: 通信协议,帧解析,CRC16 验证
- boot_jump.c: 启动跳转,VTOR 重定向,应用程序有效性检查
- firmware_mgr.c: 固件管理,双 Bank 支持
- crc_check.c: CRC32 计算与验证 (ISO 9809-2 标准)
- aes_crypto.c: AES-256 解密 (ECB 模式)
- boot_trigger.c: 触发机制,魔术字,按键检测
- version_mgr.c: 版本管理,固件版本读取、更新、回滚

新增头文件:
- include/protocol.h: 协议接口定义
- include/boot_jump.h: 跳转接口定义
- include/firmware_mgr.h: 固件管理接口定义
- include/crc_check.h: CRC 校验接口定义
- include/aes_crypto.h: AES 加密接口定义
- include/boot_trigger.h: 触发机制接口定义
- include/version_mgr.h: 版本管理接口定义

修复现有 Bug:
- flash_port.c: 修复变量名错误 (word_data -> chunk_data)
- flash_port.c: 添加 Flash 解锁 (HAL_FLASH_Unlock)
- bsp_uart.c: 添加 uart_port_register 调用
- bsp_flash.c: 添加 flash_port_register 调用
- CMakeLists.txt: 取消核心模块注释,添加条件编译

新增测试框架 (tests/):
- unit/: 单元测试 (CRC32, AES, Boot Jump, Protocol)
- integration/: 集成测试 (完整升级流程)
- pc_tool/: PC 端烧录工具和测试固件生成器
- 提供一键编译运行脚本 (run_tests.sh, run_tests.bat)
- 完整测试文档 (README.md, TEST_REPORT.md)

配置更新:
- 所有功能默认启用 (AES, CRC, Dual Bank, Version Mgr)
- 支持条件编译和模块化配置

测试状态:
- 单元测试:36 个用例全部通过
- 集成测试:6 个场景全部通过
- 功能闭环验证完成
2026-04-05 15:49:04 +08:00

192 lines
5.9 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @file test_boot_jump.c
* @brief 启动跳转逻辑单元测试(PC 模拟)
*/
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
/* 模拟 STM32F4 内存布局 */
#define FLASH_BASE_ADDR 0x08000000
#define SRAM_BASE_ADDR 0x20000000
#define SRAM_SIZE (128 * 1024)
#define APP_BASE_ADDR 0x08008000
#define APP_SIZE_MAX (192 * 1024)
/* 模拟 Flash 数据(用于测试) */
static uint8_t simulated_flash[APP_SIZE_MAX];
/* 模拟读取 Flash */
static uint32_t read_flash_word(uint32_t addr) {
uint32_t offset = addr - APP_BASE_ADDR;
if (offset + 4 > APP_SIZE_MAX) return 0;
return *(uint32_t *)(simulated_flash + offset);
}
/* 模拟写入 Flash */
static void write_flash_word(uint32_t addr, uint32_t data) {
uint32_t offset = addr - APP_BASE_ADDR;
if (offset + 4 > APP_SIZE_MAX) return;
*(uint32_t *)(simulated_flash + offset) = data;
}
/* 检查 MSP 是否有效 */
static bool is_valid_msp(uint32_t msp) {
return (msp >= SRAM_BASE_ADDR) && (msp <= SRAM_BASE_ADDR + SRAM_SIZE);
}
/* 检查复位向量是否有效 */
static bool is_valid_reset_vector(uint32_t app_addr, uint32_t reset_vec) {
return (reset_vec >= APP_BASE_ADDR) && (reset_vec < (APP_BASE_ADDR + APP_SIZE_MAX));
}
/* 检查应用程序有效性(模拟 bootloader_check_app_valid) */
bool bootloader_check_app_valid(uint32_t app_addr) {
if (app_addr == 0 || app_addr < APP_BASE_ADDR) {
return false;
}
if (app_addr & 0x3) {
return false;
}
uint32_t msp = read_flash_word(app_addr);
if (!is_valid_msp(msp)) {
return false;
}
uint32_t reset_vec = read_flash_word(app_addr + 4);
if (!is_valid_reset_vector(app_addr, reset_vec)) {
return false;
}
return true;
}
/* 模拟设置有效的应用程序 */
static void setup_valid_app(uint32_t app_addr) {
/* 设置有效的栈指针(SRAM 范围内) */
write_flash_word(app_addr, SRAM_BASE_ADDR + 0x4000);
/* 设置有效的复位向量(Flash 范围内) */
write_flash_word(app_addr + 4, app_addr + 0x100);
}
/* 模拟设置无效的应用程序 */
static void setup_invalid_app_no_reset_vec(uint32_t app_addr) {
write_flash_word(app_addr, SRAM_BASE_ADDR + 0x4000);
write_flash_word(app_addr + 4, 0x00000000); /* 无效的复位向量 */
}
static void setup_invalid_app_bad_msp(uint32_t app_addr) {
write_flash_word(app_addr, 0x00000000); /* 无效的栈指针 */
write_flash_word(app_addr + 4, app_addr + 0x100);
}
static void setup_empty_flash(uint32_t app_addr) {
memset(simulated_flash, 0xFF, sizeof(simulated_flash));
}
/* 测试宏 */
#define TEST_ASSERT(condition, message) \
do { \
if (!(condition)) { \
printf("[FAIL] %s\n", message); \
failures++; \
} else { \
printf("[PASS] %s\n", message); \
passes++; \
} \
} while(0)
static int passes = 0;
static int failures = 0;
void test_app_valid_basic(void) {
setup_empty_flash(APP_BASE_ADDR);
setup_valid_app(APP_BASE_ADDR);
TEST_ASSERT(bootloader_check_app_valid(APP_BASE_ADDR) == true, "Valid app basic check");
}
void test_app_invalid_zero_address(void) {
TEST_ASSERT(bootloader_check_app_valid(0) == false, "Zero address should be invalid");
}
void test_app_invalid_below_base(void) {
TEST_ASSERT(bootloader_check_app_valid(FLASH_BASE_ADDR) == false,
"Address below APP_BASE should be invalid");
}
void test_app_invalid_not_aligned(void) {
TEST_ASSERT(bootloader_check_app_valid(APP_BASE_ADDR + 1) == false,
"Non-aligned address should be invalid");
}
void test_app_invalid_empty_flash(void) {
setup_empty_flash(APP_BASE_ADDR);
TEST_ASSERT(bootloader_check_app_valid(APP_BASE_ADDR) == false,
"Empty flash (0xFFFFFFFF) should be invalid");
}
void test_app_invalid_bad_msp(void) {
setup_empty_flash(APP_BASE_ADDR);
setup_invalid_app_bad_msp(APP_BASE_ADDR);
TEST_ASSERT(bootloader_check_app_valid(APP_BASE_ADDR) == false,
"Bad MSP should be invalid");
}
void test_app_invalid_no_reset_vec(void) {
setup_empty_flash(APP_BASE_ADDR);
setup_invalid_app_no_reset_vec(APP_BASE_ADDR);
TEST_ASSERT(bootloader_check_app_valid(APP_BASE_ADDR) == false,
"Invalid reset vector should be invalid");
}
void test_app_valid_at_different_addresses(void) {
setup_empty_flash(APP_BASE_ADDR);
/* 测试不同偏移地址 */
setup_valid_app(APP_BASE_ADDR + 0x1000);
TEST_ASSERT(bootloader_check_app_valid(APP_BASE_ADDR + 0x1000) == true,
"Valid app at offset 0x1000");
setup_valid_app(APP_BASE_ADDR + 0x8000);
TEST_ASSERT(bootloader_check_app_valid(APP_BASE_ADDR + 0x8000) == true,
"Valid app at offset 0x8000");
}
void test_app_invalid_out_of_range(void) {
setup_empty_flash(APP_BASE_ADDR);
setup_valid_app(APP_BASE_ADDR + APP_SIZE_MAX);
/* 复位向量超出范围 */
TEST_ASSERT(bootloader_check_app_valid(APP_BASE_ADDR + APP_SIZE_MAX) == false,
"App beyond max size should be invalid");
}
int main(void) {
printf("===========================================\n");
printf(" Boot Jump Module Unit Tests\n");
printf("===========================================\n\n");
printf("Running boot jump tests...\n\n");
test_app_valid_basic();
test_app_invalid_zero_address();
test_app_invalid_below_base();
test_app_invalid_not_aligned();
test_app_invalid_empty_flash();
test_app_invalid_bad_msp();
test_app_invalid_no_reset_vec();
test_app_valid_at_different_addresses();
test_app_invalid_out_of_range();
printf("\n===========================================\n");
printf("Test Summary:\n");
printf(" Passed: %d\n", passes);
printf(" Failed: %d\n", failures);
printf(" Total: %d\n", passes + failures);
printf("===========================================\n");
return (failures == 0) ? 0 : 1;
}