2025-12-27 19:20:23 +08:00
|
|
|
#include "main.h"
|
2025-12-25 12:07:06 +08:00
|
|
|
#include "Can.h"
|
2026-01-01 01:39:19 +08:00
|
|
|
#include "aes128.h"
|
2025-12-27 19:20:23 +08:00
|
|
|
#include "initial.h"
|
2026-01-03 18:00:28 +08:00
|
|
|
#include "sys/OTA.h"
|
|
|
|
|
#include "sys/config.h"
|
2025-12-25 12:07:06 +08:00
|
|
|
#include "sys/systick.h"
|
2025-12-27 19:20:23 +08:00
|
|
|
#include <cstdio>
|
2026-01-01 01:39:19 +08:00
|
|
|
|
2026-01-03 18:00:28 +08:00
|
|
|
|
2025-12-27 19:20:23 +08:00
|
|
|
extern CAN_transmiter CAN0_transmiter; // CAN初始化
|
2025-12-25 12:07:06 +08:00
|
|
|
|
2025-12-27 19:20:23 +08:00
|
|
|
BootloaderSystem sys;
|
|
|
|
|
extern OTAHandler ota;
|
2026-01-01 01:39:19 +08:00
|
|
|
|
2025-12-27 19:20:23 +08:00
|
|
|
void BootloaderSystem::initHardware() {
|
2026-01-01 01:39:19 +08:00
|
|
|
systick_config(); // 系统时钟初始化
|
|
|
|
|
USART0_init(); // 调试串口初始化
|
|
|
|
|
|
|
|
|
|
rcu_periph_clock_enable(RCU_CRC); // 使能CRC外设时钟
|
|
|
|
|
crc_deinit(); // 初始化CRC模块
|
2025-12-27 19:20:23 +08:00
|
|
|
CAN0_transmiter.init();
|
|
|
|
|
}
|
2026-01-03 18:00:28 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 扫描APP地址范围内的Flash内容
|
|
|
|
|
* @param print_content 是否打印Flash内容,默认为true
|
|
|
|
|
* @param page_count 扫描的页数,默认为0表示扫描整个APP区域
|
|
|
|
|
* @retval 无
|
|
|
|
|
*/
|
|
|
|
|
void scanAppFlashContent(bool print_content = true, u32 page_count = 0) {
|
|
|
|
|
u32 start_addr = APP_START_ADDR;
|
|
|
|
|
u32 total_size = 0xC000;
|
|
|
|
|
|
|
|
|
|
// 如果指定了页数,则按页数计算大小
|
|
|
|
|
if (page_count > 0) {
|
|
|
|
|
total_size = page_count * FMC_PAGE_SIZE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u32 end_addr = start_addr + total_size;
|
|
|
|
|
|
|
|
|
|
#if DEBUG_OTA
|
|
|
|
|
printf("Scanning Flash from 0x%08X to 0x%08X (%d bytes)\r\n", start_addr,
|
|
|
|
|
end_addr, total_size);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
u32 page_count_scanned = 0;
|
|
|
|
|
u32 empty_pages = 0;
|
|
|
|
|
u32 data_pages = 0;
|
|
|
|
|
|
|
|
|
|
for (u32 addr = start_addr; addr < end_addr; addr += FMC_PAGE_SIZE) {
|
|
|
|
|
bool page_empty = true;
|
|
|
|
|
|
|
|
|
|
// 检查当前页是否全为0xFF(空页)
|
|
|
|
|
for (u32 offset = 0; offset < FMC_PAGE_SIZE; offset += 4) {
|
|
|
|
|
u32 data = *(u32 *)(addr + offset);
|
|
|
|
|
|
|
|
|
|
if (data != 0xFFFFFFFF) {
|
|
|
|
|
page_empty = false;
|
|
|
|
|
break; // 找到非空数据,立即退出检查
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (page_empty) {
|
|
|
|
|
empty_pages++;
|
|
|
|
|
} else {
|
|
|
|
|
data_pages++;
|
|
|
|
|
|
|
|
|
|
// 只有在非空页面且需要打印时才输出内容
|
|
|
|
|
if (print_content) {
|
|
|
|
|
printf("Page 0x%08X: \r\n", addr);
|
|
|
|
|
|
|
|
|
|
// 打印所有内容
|
|
|
|
|
for (u32 offset = 0; offset < FMC_PAGE_SIZE;
|
|
|
|
|
offset += 16) { // 每行打印16字节
|
|
|
|
|
// 打印地址偏移
|
|
|
|
|
printf(" +%03X: ", offset);
|
|
|
|
|
|
|
|
|
|
// 打印十六进制数据
|
|
|
|
|
for (u32 byte_idx = 0; byte_idx < 16; byte_idx++) {
|
|
|
|
|
if ((offset + byte_idx) < FMC_PAGE_SIZE) {
|
|
|
|
|
u8 data = *(__IO u8 *)(addr + offset + byte_idx);
|
|
|
|
|
printf("%02X ", data);
|
|
|
|
|
} else {
|
|
|
|
|
printf(" "); // 空白填充
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 每8字节加个小空格,便于阅读
|
|
|
|
|
if (byte_idx == 7) {
|
|
|
|
|
printf(" ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 打印可打印字符
|
|
|
|
|
printf("|");
|
|
|
|
|
for (u32 byte_idx = 0; byte_idx < 16; byte_idx++) {
|
|
|
|
|
if ((offset + byte_idx) < FMC_PAGE_SIZE) {
|
|
|
|
|
u8 data = *(__IO u8 *)(addr + offset + byte_idx);
|
|
|
|
|
if (data >= 32 && data <= 126) { // 可打印字符范围
|
|
|
|
|
printf("%c", data);
|
|
|
|
|
} else {
|
|
|
|
|
printf(".");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
printf(" "); // 空白填充
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
printf("|\r\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
page_count_scanned++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if DEBUG_OTA
|
|
|
|
|
printf("Scan complete: %d pages scanned, %d empty pages, %d data pages\r\n",
|
|
|
|
|
page_count_scanned, empty_pages, data_pages);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-27 19:20:23 +08:00
|
|
|
void BootloaderSystem::run() {
|
|
|
|
|
// 检查是否触发OTA模式
|
2026-01-03 18:00:28 +08:00
|
|
|
if (!validateApplication()) {
|
|
|
|
|
printf("OTA Mode\r\n");
|
2025-12-27 19:20:23 +08:00
|
|
|
// OTA模式
|
|
|
|
|
while (1) {
|
|
|
|
|
CAN0_transmiter.communication_check();
|
|
|
|
|
ota.process();
|
2025-12-25 12:07:06 +08:00
|
|
|
}
|
2025-12-27 19:20:23 +08:00
|
|
|
} else {
|
2026-01-03 18:00:28 +08:00
|
|
|
printf("Normal Mode\r\n");
|
2025-12-27 19:20:23 +08:00
|
|
|
// 正常模式:跳转到APP
|
|
|
|
|
ota.jumpToApplication();
|
2025-12-25 12:07:06 +08:00
|
|
|
}
|
2025-12-27 19:20:23 +08:00
|
|
|
}
|
2025-12-25 12:07:06 +08:00
|
|
|
int main(void) {
|
|
|
|
|
sys.initHardware();
|
2026-01-03 18:00:28 +08:00
|
|
|
// scanAppFlashContent();
|
2026-01-01 01:39:19 +08:00
|
|
|
// test_decrypt();
|
2025-12-25 12:07:06 +08:00
|
|
|
sys.run();
|
|
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
|
; // 不应该执行到这里
|
|
|
|
|
}
|