|
|
@@ -0,0 +1,969 @@
|
|
|
+/*
|
|
|
+ * Copyright (c) 2026, QXZ
|
|
|
+ *
|
|
|
+ * ecode 事件记录(独立 fal 分区环形存储,掉电保护)
|
|
|
+ * 布局与 BL 共用,改任一字段必须两端同步。
|
|
|
+ */
|
|
|
+#define DBG_TAG "ecode"
|
|
|
+#define DBG_LVL DBG_INFO
|
|
|
+#include <rtdbg.h>
|
|
|
+#include <rthw.h>
|
|
|
+#include <fal.h>
|
|
|
+#include <easyflash.h>
|
|
|
+#include "flash_ops.h"
|
|
|
+#include <string.h>
|
|
|
+#include <stdio.h>
|
|
|
+#include "ecode.h"
|
|
|
+#include "ecode_detail.h"
|
|
|
+
|
|
|
+// 分区名称
|
|
|
+#define ECODE_PART_NAME "ecode"
|
|
|
+#define SECTOR_SIZE 4096
|
|
|
+// 每条记录固定为 64 字节
|
|
|
+#define RECORD_SIZE sizeof(ecode_record_t)
|
|
|
+#define RECORDS_PER_SECTOR (SECTOR_SIZE / RECORD_SIZE)
|
|
|
+
|
|
|
+_Static_assert(sizeof(ecode_record_t) == 64, "ecode_record_t size must be 64 bytes");
|
|
|
+
|
|
|
+typedef struct
|
|
|
+{
|
|
|
+ uint32_t magic;
|
|
|
+ uint32_t version;
|
|
|
+ uint32_t write_seq;
|
|
|
+} ecode_header_t;
|
|
|
+
|
|
|
+// 记录区占头部扇区之后的全部扇区,与 BL 的布局完全一致
|
|
|
+static const struct fal_partition *ecode_part = RT_NULL;
|
|
|
+static uint32_t max_sectors = 0;
|
|
|
+static rt_mutex_t ecode_lock = RT_NULL;
|
|
|
+static rt_bool_t initialized = RT_FALSE;
|
|
|
+static rt_mutex_t init_mutex = RT_NULL;
|
|
|
+static uint8_t s_header_rebuilt = 0;
|
|
|
+// 本次启动已确认初始化失败,不再重试
|
|
|
+// 否则每次记录都会重新擦 384KB,把开机拖成几十秒
|
|
|
+static rt_bool_t s_init_failed = RT_FALSE;
|
|
|
+
|
|
|
+// 全局唯一缓冲区(受 ecode_lock 保护),用于所有 Flash 扇区操作
|
|
|
+static uint8_t g_buf[SECTOR_SIZE];
|
|
|
+// g_buf 里当前是哪个扇区的内容;0xFFFFFFFF 表示无效。
|
|
|
+// 读单条记录要 4KB 扇区,而 6080 条全量读若不去重就是 24MB SPI 传输
|
|
|
+static uint32_t s_cache_sector = 0xFFFFFFFFu;
|
|
|
+
|
|
|
+static void cache_invalidate(void)
|
|
|
+{
|
|
|
+ s_cache_sector = 0xFFFFFFFFu;
|
|
|
+}
|
|
|
+
|
|
|
+// 把指定扇区读进 g_buf,命中缓存则直接返回
|
|
|
+static int load_sector(uint32_t sector_start)
|
|
|
+{
|
|
|
+ if (s_cache_sector == sector_start)
|
|
|
+ {
|
|
|
+ return RT_EOK;
|
|
|
+ }
|
|
|
+ if (fal_partition_read(ecode_part, sector_start, g_buf, SECTOR_SIZE) < 0)
|
|
|
+ {
|
|
|
+ cache_invalidate();
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+ s_cache_sector = sector_start;
|
|
|
+ return RT_EOK;
|
|
|
+}
|
|
|
+
|
|
|
+// QXZ 的时间源是 sys_config.c 里的全局量与 RTC 刷新函数
|
|
|
+extern uint32_t year;
|
|
|
+extern uint32_t month;
|
|
|
+extern uint32_t day;
|
|
|
+extern uint32_t hour;
|
|
|
+extern uint32_t min;
|
|
|
+extern uint32_t sec;
|
|
|
+extern void Reflash_RTC(void);
|
|
|
+
|
|
|
+// 默认开启:现场存量设备 env 里多为 {0,0},那会让所有记录静默丢失
|
|
|
+ecode_cfg_t en_recode = {1};
|
|
|
+
|
|
|
+// 新增事件(升级链路、启动来源、诊断)一律强制落盘,不受历史开关影响
|
|
|
+static int ecode_always_record(uint16_t code)
|
|
|
+{
|
|
|
+ return (code >= (uint16_t)e_update_start && code < (uint16_t)e_typeMAX) ? 1 : 0;
|
|
|
+}
|
|
|
+
|
|
|
+// 清理 detail,确保只含可打印字符(保留中文 UTF-8),遇垃圾或坏序列截断
|
|
|
+// 记录里可能同时存在 UTF-8(新)和 GBK(旧)两种 detail:
|
|
|
+// UTF-8 判定放在前面,GBK 兜底,两种编码都完整保留,只有真非法才截断
|
|
|
+static void sanitize_detail(char *detail, size_t size)
|
|
|
+{
|
|
|
+ size_t i;
|
|
|
+ unsigned char c;
|
|
|
+ unsigned char c1;
|
|
|
+
|
|
|
+ if (size == 0)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ detail[size - 1] = '\0';
|
|
|
+
|
|
|
+ for (i = 0; i < size - 1; i++)
|
|
|
+ {
|
|
|
+ c = (unsigned char)detail[i];
|
|
|
+ c1 = (i + 1 < size - 1) ? (unsigned char)detail[i + 1] : 0;
|
|
|
+
|
|
|
+ if (c == 0x00)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (c == 0xFF) // Flash 擦除态,视为未写入
|
|
|
+ {
|
|
|
+ detail[i] = '\0';
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if ((c < 0x20) && (c != '\t') && (c != '\n') && (c != '\r'))
|
|
|
+ {
|
|
|
+ detail[i] = '\0';
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (c < 0x80)
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // UTF-8:2 字节
|
|
|
+ if (((c & 0xE0) == 0xC0) && ((c1 & 0xC0) == 0x80))
|
|
|
+ {
|
|
|
+ i += 1;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // UTF-8:3 字节
|
|
|
+ if (((c & 0xF0) == 0xE0) && (i + 2 < size - 1)
|
|
|
+ && ((c1 & 0xC0) == 0x80) && (((unsigned char)detail[i + 2] & 0xC0) == 0x80))
|
|
|
+ {
|
|
|
+ i += 2;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // UTF-8:4 字节
|
|
|
+ if (((c & 0xF8) == 0xF0) && (i + 3 < size - 1)
|
|
|
+ && ((c1 & 0xC0) == 0x80)
|
|
|
+ && (((unsigned char)detail[i + 2] & 0xC0) == 0x80)
|
|
|
+ && (((unsigned char)detail[i + 3] & 0xC0) == 0x80))
|
|
|
+ {
|
|
|
+ i += 3;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // GBK:首 0x81~0xFE,次 0x40~0xFE(0x7F 除外)
|
|
|
+ if ((c >= 0x81) && (c <= 0xFE)
|
|
|
+ && (c1 >= 0x40) && (c1 <= 0xFE) && (c1 != 0x7F))
|
|
|
+ {
|
|
|
+ i += 1;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 都不是合法多字节:在这里截断,避免后面全是乱码
|
|
|
+ detail[i] = '\0';
|
|
|
+ break;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static uint32_t get_valid_count(uint32_t write_seq)
|
|
|
+{
|
|
|
+ uint32_t max_records = max_sectors * RECORDS_PER_SECTOR;
|
|
|
+
|
|
|
+ return (write_seq < max_records) ? write_seq : max_records;
|
|
|
+}
|
|
|
+
|
|
|
+// v4 先把序号换算成扇区序再回绕,才是真正的整分区环形
|
|
|
+static void rec_pos(uint32_t seq, uint32_t ver, uint32_t *sector_start, uint32_t *offset)
|
|
|
+{
|
|
|
+ uint32_t si;
|
|
|
+
|
|
|
+ if (ver >= 4)
|
|
|
+ {
|
|
|
+ si = (seq / RECORDS_PER_SECTOR) % max_sectors;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ si = seq % max_sectors;
|
|
|
+ }
|
|
|
+ *sector_start = SECTOR_SIZE + si * SECTOR_SIZE;
|
|
|
+ *offset = (seq % RECORDS_PER_SECTOR) * RECORD_SIZE;
|
|
|
+}
|
|
|
+
|
|
|
+static int read_header(ecode_header_t *hdr)
|
|
|
+{
|
|
|
+ if (fal_partition_read(ecode_part, 0, (uint8_t *)hdr, sizeof(ecode_header_t)) < 0)
|
|
|
+ {
|
|
|
+ LOG_E("read header failed");
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+ return RT_EOK;
|
|
|
+}
|
|
|
+
|
|
|
+static int write_header(const ecode_header_t *hdr)
|
|
|
+{
|
|
|
+ int ret = RT_EOK;
|
|
|
+
|
|
|
+ // 这里要借 g_buf 读头扇区,读完 g_buf 里就是头扇区了,缓存必须作废
|
|
|
+ cache_invalidate();
|
|
|
+ if (fal_partition_read(ecode_part, 0, g_buf, SECTOR_SIZE) < 0)
|
|
|
+ {
|
|
|
+ LOG_E("read header sector failed");
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+ memcpy(g_buf, hdr, sizeof(ecode_header_t));
|
|
|
+
|
|
|
+ if (fal_partition_erase(ecode_part, 0, SECTOR_SIZE) < 0)
|
|
|
+ {
|
|
|
+ LOG_E("erase header sector failed");
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+ wdg_feed_once(); // 擦完头扇区立即喂一次
|
|
|
+ if (fal_partition_write(ecode_part, 0, g_buf, SECTOR_SIZE) != SECTOR_SIZE)
|
|
|
+ {
|
|
|
+ LOG_E("write header sector failed");
|
|
|
+ ret = -RT_ERROR;
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+static int erase_whole_partition(void)
|
|
|
+{
|
|
|
+ uint32_t off;
|
|
|
+
|
|
|
+ cache_invalidate();
|
|
|
+ // 384KB 一次擦会让调用方长期无法喂狗,改成分扇区递进
|
|
|
+ for (off = 0; off < ecode_part->len; off += SECTOR_SIZE)
|
|
|
+ {
|
|
|
+ if (fal_partition_erase(ecode_part, off, SECTOR_SIZE) < 0)
|
|
|
+ {
|
|
|
+ LOG_E("erase whole partition failed at %u", off);
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+ wdg_feed_once(); // 每擦完一个扇区立即喂一次
|
|
|
+ }
|
|
|
+ return RT_EOK;
|
|
|
+}
|
|
|
+
|
|
|
+// 头部无效时扫描数据区重建序号;扫不到任何记录才整分区擦除
|
|
|
+static int ecode_rebuild_header(ecode_header_t *hdr)
|
|
|
+{
|
|
|
+ uint32_t i, j;
|
|
|
+ uint32_t found = 0, max_num = 0;
|
|
|
+ uint32_t read_fail = 0;
|
|
|
+
|
|
|
+ for (i = 0; i < max_sectors; i++)
|
|
|
+ {
|
|
|
+ if (fal_partition_read(ecode_part, SECTOR_SIZE + i * SECTOR_SIZE, g_buf, SECTOR_SIZE) < 0)
|
|
|
+ {
|
|
|
+ read_fail++;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ cache_invalidate();
|
|
|
+ wdg_feed_once(); // 每扫完一个扇区立即喂一次
|
|
|
+ for (j = 0; j < RECORDS_PER_SECTOR; j++)
|
|
|
+ {
|
|
|
+ ecode_record_t *r = (ecode_record_t *)(g_buf + j * RECORD_SIZE);
|
|
|
+
|
|
|
+ // 空白扇区全 0xFF;num 单调递增且可远超单圈容量,故不能用圈容量做上限
|
|
|
+ if (r->num >= 0xFFFFFF00u)
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 用事件码范围排除半写与垃圾数据
|
|
|
+ if (r->code < (uint16_t)reboot_ec || r->code >= (uint16_t)e_typeMAX)
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ found++;
|
|
|
+ if (r->num > max_num)
|
|
|
+ {
|
|
|
+ max_num = r->num;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 本分区是从 zk_area 切出来的,可能残留字库等旧数据;扫出来的序号若远超
|
|
|
+ // 合理范围,说明这些"记录"是垃圾,直接格式化,别按垃圾序号硬恢复
|
|
|
+ if ((found > 0) && (max_num > (max_sectors * RECORDS_PER_SECTOR * 100u)))
|
|
|
+ {
|
|
|
+ LOG_W("rebuild: implausible seq %u, treat as garbage", max_num);
|
|
|
+ found = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (found == 0)
|
|
|
+ {
|
|
|
+ // 读失败的扇区不等于空扇区:宁可暂不可用,也不误擦历史证据
|
|
|
+ if (read_fail > 0)
|
|
|
+ {
|
|
|
+ LOG_E("rebuild: %u sector(s) unreadable, refuse to erase", read_fail);
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+ LOG_W("no valid record, erase whole partition");
|
|
|
+ if (erase_whole_partition() != RT_EOK)
|
|
|
+ {
|
|
|
+ LOG_E("erase failed");
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+ hdr->magic = ECODE_MAGIC;
|
|
|
+ hdr->version = ECODE_VERSION;
|
|
|
+ hdr->write_seq = 0;
|
|
|
+ return write_header(hdr);
|
|
|
+ }
|
|
|
+
|
|
|
+ hdr->magic = ECODE_MAGIC;
|
|
|
+ hdr->version = ECODE_VERSION;
|
|
|
+ // 序号必须延续原值,截断会使环形落点整体错位并覆盖有效记录
|
|
|
+ hdr->write_seq = max_num + 1;
|
|
|
+ s_header_rebuilt = 1;
|
|
|
+ LOG_W("header rebuilt: found %u records, write_seq=%u", found, hdr->write_seq);
|
|
|
+ return write_header(hdr);
|
|
|
+}
|
|
|
+
|
|
|
+static int ecode_init_core(void)
|
|
|
+{
|
|
|
+ ecode_header_t hdr;
|
|
|
+ uint32_t part_size;
|
|
|
+
|
|
|
+ ecode_part = fal_partition_find(ECODE_PART_NAME);
|
|
|
+ if (ecode_part == RT_NULL)
|
|
|
+ {
|
|
|
+ LOG_E("partition '%s' not found", ECODE_PART_NAME);
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+
|
|
|
+ part_size = ecode_part->len;
|
|
|
+ if (part_size < SECTOR_SIZE * 2)
|
|
|
+ {
|
|
|
+ LOG_E("partition too small (need at least %d bytes)", SECTOR_SIZE * 2);
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+ // 头部占用扇区0,其余全部为数据扇区(与 BL 完全一致)
|
|
|
+ max_sectors = (part_size - SECTOR_SIZE) / SECTOR_SIZE;
|
|
|
+ LOG_I("partition %u bytes, max_sectors=%u, records=%u",
|
|
|
+ part_size, max_sectors, max_sectors * RECORDS_PER_SECTOR);
|
|
|
+
|
|
|
+ ecode_lock = rt_mutex_create("ecode", RT_IPC_FLAG_FIFO);
|
|
|
+ if (ecode_lock == RT_NULL)
|
|
|
+ {
|
|
|
+ LOG_E("create mutex failed");
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (read_header(&hdr) == RT_EOK && hdr.magic == ECODE_MAGIC
|
|
|
+ && hdr.version >= ECODE_VERSION_MIN && hdr.version <= ECODE_VERSION)
|
|
|
+ {
|
|
|
+ LOG_I("ecode ready, write_seq=%u", hdr.write_seq);
|
|
|
+ return RT_EOK;
|
|
|
+ }
|
|
|
+
|
|
|
+ LOG_W("partition not initialized or invalid, try rebuild...");
|
|
|
+ if (ecode_rebuild_header(&hdr) != RT_EOK)
|
|
|
+ {
|
|
|
+ LOG_E("write header failed");
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+ return RT_EOK;
|
|
|
+}
|
|
|
+
|
|
|
+static int ensure_initialized(void)
|
|
|
+{
|
|
|
+ if (initialized)
|
|
|
+ {
|
|
|
+ return RT_EOK;
|
|
|
+ }
|
|
|
+ // 分区坏了只试一次:反复重试会每次开机都擦一遍 384KB
|
|
|
+ if (s_init_failed)
|
|
|
+ {
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+ if (init_mutex == RT_NULL)
|
|
|
+ {
|
|
|
+ init_mutex = rt_mutex_create("ecode_init", RT_IPC_FLAG_FIFO);
|
|
|
+ if (init_mutex == RT_NULL)
|
|
|
+ {
|
|
|
+ LOG_E("create init mutex failed");
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ rt_mutex_take(init_mutex, RT_WAITING_FOREVER);
|
|
|
+ if (!initialized)
|
|
|
+ {
|
|
|
+ if (ecode_init_core() == RT_EOK)
|
|
|
+ {
|
|
|
+ initialized = RT_TRUE;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ LOG_E("ecode_init_core failed, give up for this boot");
|
|
|
+ s_init_failed = RT_TRUE;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ rt_mutex_release(init_mutex);
|
|
|
+
|
|
|
+ return initialized ? RT_EOK : -RT_ERROR;
|
|
|
+}
|
|
|
+
|
|
|
+int ecode_init(void)
|
|
|
+{
|
|
|
+ return ensure_initialized();
|
|
|
+}
|
|
|
+
|
|
|
+// 写一条已组装好的记录:num 由 header 统一分配,时间由调用方填
|
|
|
+static int ecode_write_rec(ecode_record_t *rec)
|
|
|
+{
|
|
|
+ ecode_header_t hdr;
|
|
|
+ uint32_t seq, record_offset_in_sector;
|
|
|
+ uint32_t sector_start;
|
|
|
+ uint32_t k;
|
|
|
+ int slot_blank;
|
|
|
+
|
|
|
+ if (ensure_initialized() != RT_EOK)
|
|
|
+ {
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+ rt_mutex_take(ecode_lock, RT_WAITING_FOREVER);
|
|
|
+
|
|
|
+ if (read_header(&hdr) != RT_EOK)
|
|
|
+ {
|
|
|
+ LOG_E("read header failed");
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+ if (hdr.magic != ECODE_MAGIC || hdr.version < ECODE_VERSION_MIN
|
|
|
+ || hdr.version > ECODE_VERSION)
|
|
|
+ {
|
|
|
+ LOG_E("header corrupted");
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+
|
|
|
+ seq = hdr.write_seq;
|
|
|
+ rec->num = seq;
|
|
|
+ rec_pos(seq, hdr.version, §or_start, &record_offset_in_sector);
|
|
|
+
|
|
|
+ if (load_sector(sector_start) != RT_EOK)
|
|
|
+ {
|
|
|
+ LOG_E("read target sector failed");
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 目标槽位是否仍为空(全 0xFF)。NOR 只能把 1 写成 0,空槽位可直接写,无需擦整扇区
|
|
|
+ slot_blank = 1;
|
|
|
+ for (k = 0; k < RECORD_SIZE; k++)
|
|
|
+ {
|
|
|
+ if (g_buf[record_offset_in_sector + k] != 0xFF)
|
|
|
+ {
|
|
|
+ slot_blank = 0;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ memcpy(g_buf + record_offset_in_sector, rec, RECORD_SIZE);
|
|
|
+
|
|
|
+ if (slot_blank)
|
|
|
+ {
|
|
|
+ // 槽位为空:只写本条 64 字节,擦除窗口由每条一次降到每 64 条一次
|
|
|
+ if (fal_partition_write(ecode_part, sector_start + record_offset_in_sector,
|
|
|
+ g_buf + record_offset_in_sector, RECORD_SIZE) != RECORD_SIZE)
|
|
|
+ {
|
|
|
+ LOG_E("write record failed, data may be lost!");
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ // 槽位已被占用(环形回绕到旧位置):必须先擦除整个扇区
|
|
|
+ if (fal_partition_erase(ecode_part, sector_start, SECTOR_SIZE) < 0)
|
|
|
+ {
|
|
|
+ LOG_E("erase target sector failed");
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+ wdg_feed_once(); // 擦完目标扇区立即喂一次
|
|
|
+ if (fal_partition_write(ecode_part, sector_start, g_buf, SECTOR_SIZE) != SECTOR_SIZE)
|
|
|
+ {
|
|
|
+ LOG_E("write target sector failed, data may be lost!");
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 写完 flash 后 g_buf 与介质未必一致(写失败时更明显),缓存一律作废
|
|
|
+ cache_invalidate();
|
|
|
+
|
|
|
+ // 序号必须最后落盘:先写数据再推 header,掉电最多丢最后一条而不会写乱环形
|
|
|
+ hdr.write_seq = seq + 1;
|
|
|
+ if (write_header(&hdr) != RT_EOK)
|
|
|
+ {
|
|
|
+ LOG_E("write header failed, but data written");
|
|
|
+ }
|
|
|
+
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+ return RT_EOK;
|
|
|
+}
|
|
|
+
|
|
|
+// 按 UTF-8 字符边界拷贝:超长时整字丢弃,不留半个汉字
|
|
|
+static void copy_detail_utf8(char *dst, size_t dst_size, const char *src)
|
|
|
+{
|
|
|
+ size_t n = 0;
|
|
|
+ size_t need;
|
|
|
+ unsigned char c;
|
|
|
+
|
|
|
+ if ((dst == RT_NULL) || (dst_size == 0))
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ dst[0] = '\0';
|
|
|
+ if (src == RT_NULL)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ while ((src[n] != '\0') && (n < dst_size - 1))
|
|
|
+ {
|
|
|
+ c = (unsigned char)src[n];
|
|
|
+ if (c < 0x80)
|
|
|
+ {
|
|
|
+ need = 1;
|
|
|
+ }
|
|
|
+ else if ((c & 0xE0) == 0xC0)
|
|
|
+ {
|
|
|
+ need = 2;
|
|
|
+ }
|
|
|
+ else if ((c & 0xF0) == 0xE0)
|
|
|
+ {
|
|
|
+ need = 3;
|
|
|
+ }
|
|
|
+ else if ((c & 0xF8) == 0xF0)
|
|
|
+ {
|
|
|
+ need = 4;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ break; // 非法前导字节,就此结束
|
|
|
+ }
|
|
|
+ if ((n + need) > (dst_size - 1))
|
|
|
+ {
|
|
|
+ break; // 放不下完整的一个字,整字丢弃
|
|
|
+ }
|
|
|
+ memcpy(dst + n, src + n, need);
|
|
|
+ n += need;
|
|
|
+ }
|
|
|
+ dst[n] = '\0';
|
|
|
+}
|
|
|
+
|
|
|
+// 记录一个事件:文案由 ecode_detail 表按 (code, para) 决定,调用方不传字符串
|
|
|
+void Recode_e_code(ecode_list code, uint16_t para)
|
|
|
+{
|
|
|
+ const char *detail;
|
|
|
+ ecode_record_t rec;
|
|
|
+
|
|
|
+ // 关闭记录时默认不写,但升级/看门狗等关键路径必须留痕
|
|
|
+ if (!en_recode.en && !ecode_always_record((uint16_t)code))
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ detail = ecode_detail_text((uint16_t)code, para);
|
|
|
+
|
|
|
+ rt_memset(&rec, 0, sizeof(rec));
|
|
|
+ // 取时间前先刷 RTC 全局量,保证记录里是上报刷新的即时时间
|
|
|
+ Reflash_RTC();
|
|
|
+ rec.time_stamp.tm_year = (uint8_t)((year >= 2000) ? (year - 2000) : 0);
|
|
|
+ rec.time_stamp.tm_mon = (uint8_t)month;
|
|
|
+ rec.time_stamp.tm_mday = (uint8_t)day;
|
|
|
+ rec.time_stamp.tm_hour = (uint8_t)hour;
|
|
|
+ rec.time_stamp.tm_min = (uint8_t)min;
|
|
|
+ rec.time_stamp.tm_sec = (uint8_t)sec;
|
|
|
+ rec.code = (uint16_t)code;
|
|
|
+ rec.para = para;
|
|
|
+ copy_detail_utf8(rec.detail, sizeof(rec.detail), detail);
|
|
|
+
|
|
|
+ if (ecode_write_rec(&rec) == RT_EOK)
|
|
|
+ {
|
|
|
+ LOG_I("ecode c=%u p=%u d=%s", (unsigned)code, para, rec.detail);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 旧版 ecode 存 easyflash env:60 条 x 6 字节 + 一个 16 位写指针
|
|
|
+#define EF_ECODE_MAX 60
|
|
|
+typedef struct
|
|
|
+{
|
|
|
+ uint32_t Time_Stampe;
|
|
|
+ uint8_t code;
|
|
|
+ uint8_t para;
|
|
|
+} ef_ecode_rec_t;
|
|
|
+
|
|
|
+// 把旧 env 里的记录搬进新分区,搬完删掉旧 key。
|
|
|
+// 只在新区为空时执行,重复上电不会重复搬。
|
|
|
+void ecode_migrate_from_ef(void)
|
|
|
+{
|
|
|
+ static ef_ecode_rec_t old[EF_ECODE_MAX];
|
|
|
+ uint16_t ptr = 0;
|
|
|
+ uint32_t i, start, cnt, months;
|
|
|
+ ecode_record_t rec;
|
|
|
+
|
|
|
+ if (ensure_initialized() != RT_EOK)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 迁移完成标记:迁到一半掉电时 marker 还没写,下次会重来。
|
|
|
+ // 重来前先把新分区清空,避免重复搬出一倍记录
|
|
|
+ {
|
|
|
+ uint8_t marker = 0;
|
|
|
+
|
|
|
+ if (ef_get_env_blob("ecode_mig", &marker, sizeof(marker), RT_NULL) > 0 && marker == 1)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (ef_get_env_blob("e_pointer", &ptr, sizeof(ptr), RT_NULL) == 0 || ptr == 0)
|
|
|
+ {
|
|
|
+ // 没有旧数据,直接标记为已迁移,别每次都来查一遍
|
|
|
+ uint8_t marker = 1;
|
|
|
+
|
|
|
+ ef_set_env_blob("ecode_mig", &marker, sizeof(marker));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (ef_get_env_blob("e_code", old, sizeof(old), RT_NULL) == 0)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 上次可能迁到一半,先清干净再整体搬;新分区本来就空就不用白擦 384KB
|
|
|
+ if (ecode_get_count() > 0)
|
|
|
+ {
|
|
|
+ ecode_clear();
|
|
|
+ }
|
|
|
+
|
|
|
+ cnt = (ptr > EF_ECODE_MAX) ? EF_ECODE_MAX : ptr;
|
|
|
+ start = (ptr > EF_ECODE_MAX) ? (ptr % EF_ECODE_MAX) : 0;
|
|
|
+
|
|
|
+ // 旧区是环形,写到 ptr % 60 处停下,从那里开始才是时间上最旧的一条
|
|
|
+ for (i = 0; i < cnt; i++)
|
|
|
+ {
|
|
|
+ ef_ecode_rec_t *o = &old[(start + i) % EF_ECODE_MAX];
|
|
|
+
|
|
|
+ if (o->code < (uint8_t)reboot_ec || o->code >= (uint8_t)e_typeMAX)
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ rt_memset(&rec, 0, sizeof(rec));
|
|
|
+ months = (o->Time_Stampe & 0xFFC00000u) >> 22;
|
|
|
+ rec.time_stamp.tm_year = (uint8_t)(months / 12);
|
|
|
+ rec.time_stamp.tm_mon = (uint8_t)(months % 12);
|
|
|
+ if (rec.time_stamp.tm_mon == 0 && rec.time_stamp.tm_year != 0)
|
|
|
+ {
|
|
|
+ rec.time_stamp.tm_mon = 12;
|
|
|
+ rec.time_stamp.tm_year--;
|
|
|
+ }
|
|
|
+ rec.time_stamp.tm_mday = (uint8_t)((o->Time_Stampe & 0x3E0000u) >> 17);
|
|
|
+ rec.time_stamp.tm_hour = (uint8_t)((o->Time_Stampe & 0x1F000u) >> 12);
|
|
|
+ rec.time_stamp.tm_min = (uint8_t)((o->Time_Stampe & 0xFC0u) >> 6);
|
|
|
+ rec.time_stamp.tm_sec = (uint8_t)(o->Time_Stampe & 0x3Fu);
|
|
|
+ rec.code = o->code;
|
|
|
+ rec.para = o->para;
|
|
|
+ copy_detail_utf8(rec.detail, sizeof(rec.detail), ecode_detail_text((uint16_t)e_ecode_rebuild, 0));
|
|
|
+ ecode_write_rec(&rec);
|
|
|
+ }
|
|
|
+
|
|
|
+ Recode_e_code(e_ecode_rebuild, (uint16_t)cnt);
|
|
|
+
|
|
|
+ {
|
|
|
+ uint8_t marker = 1;
|
|
|
+
|
|
|
+ ef_set_env_blob("ecode_mig", &marker, sizeof(marker));
|
|
|
+ }
|
|
|
+ ef_del_env("e_code");
|
|
|
+ ef_del_env("e_pointer");
|
|
|
+ LOG_I("migrated %u legacy records", cnt);
|
|
|
+}
|
|
|
+
|
|
|
+// 当前最新记录的绝对序号;空库返回 0xFFFFFFFF
|
|
|
+uint32_t ecode_newest_seq(void)
|
|
|
+{
|
|
|
+ ecode_header_t hdr;
|
|
|
+ uint32_t cnt;
|
|
|
+ uint32_t newest = 0xFFFFFFFFu;
|
|
|
+
|
|
|
+ if (ensure_initialized() != RT_EOK)
|
|
|
+ {
|
|
|
+ return 0xFFFFFFFFu;
|
|
|
+ }
|
|
|
+ rt_mutex_take(ecode_lock, RT_WAITING_FOREVER);
|
|
|
+ if (read_header(&hdr) == RT_EOK && hdr.magic == ECODE_MAGIC
|
|
|
+ && hdr.version >= ECODE_VERSION_MIN && hdr.version <= ECODE_VERSION)
|
|
|
+ {
|
|
|
+ cnt = get_valid_count(hdr.write_seq);
|
|
|
+ if (cnt > 0)
|
|
|
+ {
|
|
|
+ newest = hdr.write_seq - 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+ return newest;
|
|
|
+}
|
|
|
+
|
|
|
+// 按绝对序号读。分包上报必须用它:用 index 读的话,中途来一条新记录
|
|
|
+// 就会让所有后续 index 整体后移一位,导致重复读和漏读
|
|
|
+int ecode_read_seq(uint32_t seq, ecode_record_t *rec)
|
|
|
+{
|
|
|
+ ecode_header_t hdr;
|
|
|
+ uint32_t valid_count;
|
|
|
+ uint32_t record_offset_in_sector;
|
|
|
+ uint32_t sector_start;
|
|
|
+ uint32_t i;
|
|
|
+ uint8_t *p;
|
|
|
+ int all_ff = 1;
|
|
|
+
|
|
|
+ if (ensure_initialized() != RT_EOK)
|
|
|
+ {
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+ rt_mutex_take(ecode_lock, RT_WAITING_FOREVER);
|
|
|
+
|
|
|
+ if (read_header(&hdr) != RT_EOK)
|
|
|
+ {
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+ if (hdr.magic != ECODE_MAGIC || hdr.version < ECODE_VERSION_MIN
|
|
|
+ || hdr.version > ECODE_VERSION)
|
|
|
+ {
|
|
|
+ LOG_E("header corrupted");
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 序号必须还没写、且仍在环形保留窗口内;被新记录覆盖了就判读不到
|
|
|
+ valid_count = get_valid_count(hdr.write_seq);
|
|
|
+ if ((seq >= hdr.write_seq) || ((hdr.write_seq - seq) > valid_count))
|
|
|
+ {
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+
|
|
|
+ rec_pos(seq, hdr.version, §or_start, &record_offset_in_sector);
|
|
|
+
|
|
|
+ if (load_sector(sector_start) != RT_EOK)
|
|
|
+ {
|
|
|
+ LOG_E("read sector failed");
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+ memcpy(rec, g_buf + record_offset_in_sector, RECORD_SIZE);
|
|
|
+ sanitize_detail(rec->detail, sizeof(rec->detail));
|
|
|
+
|
|
|
+ p = (uint8_t *)rec;
|
|
|
+ for (i = 0; i < RECORD_SIZE; i++)
|
|
|
+ {
|
|
|
+ if (p[i] != 0xFF)
|
|
|
+ {
|
|
|
+ all_ff = 0;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (all_ff)
|
|
|
+ {
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (rec->num != seq)
|
|
|
+ {
|
|
|
+ LOG_W("seq %u num mismatch: got %u (layout broken?)", seq, rec->num);
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+ return RT_EOK;
|
|
|
+}
|
|
|
+
|
|
|
+// 按逻辑序号读记录,index 0 为最新
|
|
|
+int ecode_read(uint32_t index, ecode_record_t *rec)
|
|
|
+{
|
|
|
+ ecode_header_t hdr;
|
|
|
+ uint32_t valid_count;
|
|
|
+ uint32_t logical_seq, record_offset_in_sector;
|
|
|
+ uint32_t sector_start;
|
|
|
+ uint32_t i;
|
|
|
+ uint8_t *p;
|
|
|
+ int all_ff = 1;
|
|
|
+
|
|
|
+ if (ensure_initialized() != RT_EOK)
|
|
|
+ {
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+ rt_mutex_take(ecode_lock, RT_WAITING_FOREVER);
|
|
|
+
|
|
|
+ if (read_header(&hdr) != RT_EOK)
|
|
|
+ {
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+ if (hdr.magic != ECODE_MAGIC || hdr.version < ECODE_VERSION_MIN
|
|
|
+ || hdr.version > ECODE_VERSION)
|
|
|
+ {
|
|
|
+ LOG_E("header corrupted");
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+
|
|
|
+ valid_count = get_valid_count(hdr.write_seq);
|
|
|
+ if (index >= valid_count)
|
|
|
+ {
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+
|
|
|
+ logical_seq = hdr.write_seq - 1 - index;
|
|
|
+ rec_pos(logical_seq, hdr.version, §or_start, &record_offset_in_sector);
|
|
|
+
|
|
|
+ if (load_sector(sector_start) != RT_EOK)
|
|
|
+ {
|
|
|
+ LOG_E("read sector failed");
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+ memcpy(rec, g_buf + record_offset_in_sector, RECORD_SIZE);
|
|
|
+ sanitize_detail(rec->detail, sizeof(rec->detail));
|
|
|
+
|
|
|
+ p = (uint8_t *)rec;
|
|
|
+ for (i = 0; i < RECORD_SIZE; i++)
|
|
|
+ {
|
|
|
+ if (p[i] != 0xFF)
|
|
|
+ {
|
|
|
+ all_ff = 0;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (all_ff)
|
|
|
+ {
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 序号必须与逻辑位置严格对应,错位时宁可报读不到也不返回假数据
|
|
|
+ if (rec->num != logical_seq)
|
|
|
+ {
|
|
|
+ LOG_W("index %u num mismatch: expect %u got %u (layout broken?)",
|
|
|
+ index, logical_seq, rec->num);
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+ return -RT_ERROR;
|
|
|
+ }
|
|
|
+
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+ return RT_EOK;
|
|
|
+}
|
|
|
+
|
|
|
+uint32_t ecode_get_count(void)
|
|
|
+{
|
|
|
+ ecode_header_t hdr;
|
|
|
+ uint32_t count;
|
|
|
+
|
|
|
+ if (ensure_initialized() != RT_EOK)
|
|
|
+ {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ rt_mutex_take(ecode_lock, RT_WAITING_FOREVER);
|
|
|
+ if (read_header(&hdr) != RT_EOK)
|
|
|
+ {
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ if (hdr.magic != ECODE_MAGIC || hdr.version < ECODE_VERSION_MIN
|
|
|
+ || hdr.version > ECODE_VERSION)
|
|
|
+ {
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ count = get_valid_count(hdr.write_seq);
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+ return count;
|
|
|
+}
|
|
|
+
|
|
|
+void ecode_clear(void)
|
|
|
+{
|
|
|
+ ecode_header_t hdr;
|
|
|
+
|
|
|
+ if (ensure_initialized() != RT_EOK)
|
|
|
+ {
|
|
|
+ LOG_E("ecode not initialized");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ rt_mutex_take(ecode_lock, RT_WAITING_FOREVER);
|
|
|
+
|
|
|
+ if (erase_whole_partition() != RT_EOK)
|
|
|
+ {
|
|
|
+ LOG_E("erase whole partition failed");
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ hdr.magic = ECODE_MAGIC;
|
|
|
+ hdr.version = ECODE_VERSION;
|
|
|
+ hdr.write_seq = 0;
|
|
|
+ if (write_header(&hdr) != RT_EOK)
|
|
|
+ {
|
|
|
+ LOG_E("write header failed after clear");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ LOG_I("ecode cleared");
|
|
|
+ }
|
|
|
+ rt_mutex_release(ecode_lock);
|
|
|
+}
|
|
|
+
|
|
|
+uint32_t ecode_max_records(void)
|
|
|
+{
|
|
|
+ if (ensure_initialized() != RT_EOK)
|
|
|
+ {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ return max_sectors * RECORDS_PER_SECTOR;
|
|
|
+}
|
|
|
+
|
|
|
+// 返回并清除"头部曾被重建"标记
|
|
|
+int ecode_header_rebuilt(void)
|
|
|
+{
|
|
|
+ int r = s_header_rebuilt;
|
|
|
+
|
|
|
+ s_header_rebuilt = 0;
|
|
|
+ return r;
|
|
|
+}
|
|
|
+
|
|
|
+// ====================== MSH 命令 ======================
|
|
|
+#include <finsh.h>
|
|
|
+
|
|
|
+static void pre(void)
|
|
|
+{
|
|
|
+ ecode_record_t rec;
|
|
|
+ uint32_t cnt = ecode_get_count();
|
|
|
+ uint32_t i;
|
|
|
+
|
|
|
+ rt_kprintf("ecode count: %u / max %u\n", cnt, ecode_max_records());
|
|
|
+ for (i = 0; i < cnt; i++)
|
|
|
+ {
|
|
|
+ if (ecode_read(i, &rec) == 0)
|
|
|
+ {
|
|
|
+ rt_kprintf("%u - %02u/%02u/%02u %02u:%02u:%02u: (%5u-%-5u) %s\n",
|
|
|
+ rec.num,
|
|
|
+ rec.time_stamp.tm_year, rec.time_stamp.tm_mon, rec.time_stamp.tm_mday,
|
|
|
+ rec.time_stamp.tm_hour, rec.time_stamp.tm_min, rec.time_stamp.tm_sec,
|
|
|
+ rec.code, rec.para, rec.detail);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ rt_kprintf("read error at index %u\n", i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+MSH_CMD_EXPORT(pre, print all ecode records);
|
|
|
+
|
|
|
+static void recode_test(void)
|
|
|
+{
|
|
|
+ Recode_e_code(e_update_start, 99);
|
|
|
+}
|
|
|
+MSH_CMD_EXPORT(recode_test, write a test ecode);
|