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 个场景全部通过 - 功能闭环验证完成
This commit is contained in:
1 parent
d2b8bd7940
commit
c90c187749
36 files changed
+4983
-6
No files matched your search
@@ -0,0 +1,21 @@
|
||||
#ifndef AES_CRYPTO_H
|
||||
#define AES_CRYPTO_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "bootloader_config.h"
|
||||
|
||||
#if BOOT_ENABLE_AES
|
||||
|
||||
/* 初始化 AES */
|
||||
int aes_init(void);
|
||||
|
||||
/* AES-256 解密(ECB 模式) */
|
||||
int aes_decrypt(const uint8_t *input, uint8_t *output, uint32_t length);
|
||||
|
||||
/* AES-256 加密(暂未实现) */
|
||||
int aes_encrypt(const uint8_t *input, uint8_t *output, uint32_t length);
|
||||
|
||||
#endif /* BOOT_ENABLE_AES */
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef BOOT_JUMP_H
|
||||
#define BOOT_JUMP_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "bootloader.h"
|
||||
|
||||
/* 跳转到应用程序 */
|
||||
void bootloader_jump_to_app(uint32_t app_addr);
|
||||
|
||||
/* 检查应用程序是否有效 */
|
||||
bool bootloader_check_app_valid(uint32_t app_addr);
|
||||
|
||||
#if BOOT_ENABLE_DUAL_BANK
|
||||
/* 获取当前活动 Bank */
|
||||
uint32_t bootloader_get_active_bank(void);
|
||||
|
||||
/* 切换活动 Bank */
|
||||
void bootloader_switch_bank(void);
|
||||
|
||||
/* 获取备用 Bank 地址 */
|
||||
uint32_t bootloader_get_standby_bank_addr(void);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,28 @@
|
||||
#ifndef BOOT_TRIGGER_H
|
||||
#define BOOT_TRIGGER_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/* 进入 Bootloader 的触发条件检查 */
|
||||
bool boot_trigger_check(void);
|
||||
|
||||
/* 设置进入 Bootloader 的魔术字 */
|
||||
void boot_trigger_set_magic(void);
|
||||
|
||||
/* 清除魔术字 */
|
||||
void boot_trigger_clear_magic(void);
|
||||
|
||||
/* 标记应用程序有效 */
|
||||
void boot_trigger_mark_app_valid(void);
|
||||
|
||||
/* 检查应用程序是否标记为有效 */
|
||||
bool boot_trigger_is_app_marked_valid(void);
|
||||
|
||||
/* GPIO 初始化(弱实现,可由用户重写) */
|
||||
void boot_gpio_init(void);
|
||||
|
||||
/* GPIO 读取(弱实现,可由用户重写) */
|
||||
int boot_gpio_read(void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,23 @@
|
||||
#ifndef CRC_CHECK_H
|
||||
#define CRC_CHECK_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "bootloader_config.h"
|
||||
|
||||
#if BOOT_ENABLE_CRC
|
||||
|
||||
/* 计算 CRC32 */
|
||||
uint32_t crc32_calc(const uint8_t *data, uint32_t length);
|
||||
|
||||
/* 验证 CRC32 */
|
||||
bool crc32_verify(const uint8_t *data, uint32_t length, uint32_t expected_crc);
|
||||
|
||||
/* 使用硬件 CRC 外设计算 CRC32 (STM32F4xx) */
|
||||
#if defined(STM32F4XX) || defined(STM32F407xx)
|
||||
uint32_t crc32_calc_hw(const uint8_t *data, uint32_t length);
|
||||
#endif
|
||||
|
||||
#endif /* BOOT_ENABLE_CRC */
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,46 @@
|
||||
#ifndef FIRMWARE_MGR_H
|
||||
#define FIRMWARE_MGR_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "bootloader.h"
|
||||
#include "bootloader_config.h"
|
||||
|
||||
/* 固件接收状态 */
|
||||
typedef enum {
|
||||
FW_STATE_IDLE = 0,
|
||||
FW_STATE_RECEIVING,
|
||||
FW_STATE_VERIFIED,
|
||||
FW_STATE_WRITTEN,
|
||||
FW_STATE_ERROR,
|
||||
} firmware_state_t;
|
||||
|
||||
/* 初始化固件管理器 */
|
||||
int firmware_mgr_init(void);
|
||||
|
||||
/* 开始接收固件 */
|
||||
int firmware_mgr_start(uint32_t total_size, uint32_t crc32);
|
||||
|
||||
/* 接收固件数据包 */
|
||||
int firmware_mgr_receive(const uint8_t *data, uint32_t length);
|
||||
|
||||
/* 验证固件 */
|
||||
int firmware_mgr_verify(void);
|
||||
|
||||
/* 获取固件状态 */
|
||||
firmware_state_t firmware_mgr_get_state(void);
|
||||
|
||||
/* 获取接收进度 (0-100) */
|
||||
uint32_t firmware_mgr_get_progress(void);
|
||||
|
||||
/* 重置固件管理器 */
|
||||
void firmware_mgr_reset(void);
|
||||
|
||||
#if BOOT_ENABLE_DUAL_BANK
|
||||
/* 双 Bank 固件管理 */
|
||||
int firmware_mgr_start_bank(uint32_t bank_id, uint32_t total_size, uint32_t crc32);
|
||||
int firmware_mgr_switch_bank(void);
|
||||
int firmware_mgr_boot_standby(void);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifndef PROTOCOL_H
|
||||
#define PROTOCOL_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/* 协议命令定义 */
|
||||
typedef enum {
|
||||
CMD_HANDSHAKE = 0x01,
|
||||
CMD_ERASE_FLASH = 0x02,
|
||||
CMD_WRITE_FLASH = 0x03,
|
||||
CMD_VERIFY_FLASH = 0x04,
|
||||
CMD_READ_FLASH = 0x05,
|
||||
CMD_JUMP_APP = 0x10,
|
||||
CMD_CHECK_APP = 0x11,
|
||||
CMD_RESET_TARGET = 0x12,
|
||||
CMD_ACK = 0x80,
|
||||
CMD_NACK = 0x81,
|
||||
} protocol_cmd_t;
|
||||
|
||||
/* 协议应答状态 */
|
||||
typedef enum {
|
||||
ACK_OK = 0x00,
|
||||
ACK_ERR_FLASH = 0x01,
|
||||
ACK_ERR_CRC = 0x02,
|
||||
ACK_ERR_INVALID = 0x03,
|
||||
ACK_ERR_TIMEOUT = 0x04,
|
||||
ACK_ERR_UNKNOWN_CMD = 0xFF,
|
||||
} protocol_ack_t;
|
||||
|
||||
/* 协议初始化 */
|
||||
int protocol_init(void);
|
||||
|
||||
/* 协议主处理函数,返回 true 表示处理完一帧 */
|
||||
bool protocol_process(void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,43 @@
|
||||
#ifndef VERSION_MGR_H
|
||||
#define VERSION_MGR_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "bootloader.h"
|
||||
#include "bootloader_config.h"
|
||||
|
||||
#if BOOT_ENABLE_VERSION_MGR
|
||||
|
||||
/* 初始化版本管理器 */
|
||||
int version_mgr_init(void);
|
||||
|
||||
/* 获取当前版本信息 */
|
||||
firmware_version_t *version_mgr_get_version(void);
|
||||
|
||||
/* 更新版本信息 */
|
||||
int version_mgr_update(uint32_t version, uint32_t build_time, uint32_t crc32,
|
||||
uint32_t size, uint32_t bank_id);
|
||||
|
||||
/* 将版本信息写入 Flash */
|
||||
int version_mgr_commit(void);
|
||||
|
||||
/* 标记固件为有效 */
|
||||
int version_mgr_mark_valid(void);
|
||||
|
||||
/* 标记固件为无效 */
|
||||
int version_mgr_mark_invalid(void);
|
||||
|
||||
/* 获取固件状态 */
|
||||
uint32_t version_mgr_get_status(void);
|
||||
|
||||
/* 获取版本字符串 */
|
||||
const char *version_mgr_get_version_string(void);
|
||||
|
||||
#if BOOT_ENABLE_DUAL_BANK
|
||||
/* 版本回滚(切换到备用 Bank) */
|
||||
int version_mgr_rollback(void);
|
||||
#endif
|
||||
|
||||
#endif /* BOOT_ENABLE_VERSION_MGR */
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user