ecode.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. /*
  2. * Copyright (c) 2026, QXZ
  3. *
  4. * ecode 事件记录(独立 fal 分区环形存储,掉电保护)
  5. * 布局与 BL 共用,改任一字段必须两端同步。
  6. */
  7. #define DBG_TAG "ecode"
  8. #define DBG_LVL DBG_INFO
  9. #include <rtdbg.h>
  10. #include <rthw.h>
  11. #include <fal.h>
  12. #include <easyflash.h>
  13. #include "flash_ops.h"
  14. #include <string.h>
  15. #include <stdio.h>
  16. #include "ecode.h"
  17. #include "gb2312_uni.h"
  18. // 分区名称
  19. #define ECODE_PART_NAME "ecode"
  20. #define SECTOR_SIZE 4096
  21. // 每条记录固定为 64 字节
  22. #define RECORD_SIZE sizeof(ecode_record_t)
  23. #define RECORDS_PER_SECTOR (SECTOR_SIZE / RECORD_SIZE)
  24. _Static_assert(sizeof(ecode_record_t) == 64, "ecode_record_t size must be 64 bytes");
  25. typedef struct
  26. {
  27. uint32_t magic;
  28. uint32_t version;
  29. uint32_t write_seq;
  30. } ecode_header_t;
  31. // 记录区占头部扇区之后的全部扇区,与 BL 的布局完全一致
  32. static const struct fal_partition *ecode_part = RT_NULL;
  33. static uint32_t max_sectors = 0;
  34. static rt_mutex_t ecode_lock = RT_NULL;
  35. static rt_bool_t initialized = RT_FALSE;
  36. static rt_mutex_t init_mutex = RT_NULL;
  37. static uint8_t s_header_rebuilt = 0;
  38. // 本次启动已确认初始化失败,不再重试
  39. // 否则每次记录都会重新擦 384KB,把开机拖成几十秒
  40. static rt_bool_t s_init_failed = RT_FALSE;
  41. // 全局唯一缓冲区(受 ecode_lock 保护),用于所有 Flash 扇区操作
  42. static uint8_t g_buf[SECTOR_SIZE];
  43. // g_buf 里当前是哪个扇区的内容;0xFFFFFFFF 表示无效。
  44. // 读单条记录要 4KB 扇区,而 6080 条全量读若不去重就是 24MB SPI 传输
  45. static uint32_t s_cache_sector = 0xFFFFFFFFu;
  46. static void cache_invalidate(void)
  47. {
  48. s_cache_sector = 0xFFFFFFFFu;
  49. }
  50. // 把指定扇区读进 g_buf,命中缓存则直接返回
  51. static int load_sector(uint32_t sector_start)
  52. {
  53. if (s_cache_sector == sector_start)
  54. {
  55. return RT_EOK;
  56. }
  57. if (fal_partition_read(ecode_part, sector_start, g_buf, SECTOR_SIZE) < 0)
  58. {
  59. cache_invalidate();
  60. return -RT_ERROR;
  61. }
  62. s_cache_sector = sector_start;
  63. return RT_EOK;
  64. }
  65. // QXZ 的时间源是 sys_config.c 里的全局量与 RTC 刷新函数
  66. extern uint32_t year;
  67. extern uint32_t month;
  68. extern uint32_t day;
  69. extern uint32_t hour;
  70. extern uint32_t min;
  71. extern uint32_t sec;
  72. extern void Reflash_RTC(void);
  73. // 默认开启:现场存量设备 env 里多为 {0,0},那会让所有记录静默丢失
  74. ecode_cfg_t en_recode = {1};
  75. // 新增事件(升级链路、启动来源、诊断)一律强制落盘,不受历史开关影响
  76. static int ecode_always_record(uint16_t code)
  77. {
  78. return (code >= (uint16_t)e_update_start && code < (uint16_t)e_typeMAX) ? 1 : 0;
  79. }
  80. // 清理 detail:只留可打印字符,遇垃圾或半个汉字就截断。
  81. // Flash 里存的就是 UTF-8(落盘时已转好),这里按 UTF-8 边界校验。
  82. static void sanitize_detail(char *detail, size_t size)
  83. {
  84. size_t i;
  85. unsigned char c;
  86. unsigned char c1;
  87. unsigned char c2;
  88. if (size == 0)
  89. {
  90. return;
  91. }
  92. detail[size - 1] = '\0';
  93. for (i = 0; i < size - 1; i++)
  94. {
  95. c = (unsigned char)detail[i];
  96. c1 = (i + 1 < size - 1) ? (unsigned char)detail[i + 1] : 0;
  97. c2 = (i + 2 < size - 1) ? (unsigned char)detail[i + 2] : 0;
  98. if (c == 0x00)
  99. {
  100. break;
  101. }
  102. if (c == 0xFF) // Flash 未写态就是 0xFF,视为未写
  103. {
  104. detail[i] = '\0';
  105. break;
  106. }
  107. if ((c < 0x20) && (c != '\t') && (c != '\n') && (c != '\r'))
  108. {
  109. detail[i] = '\0';
  110. break;
  111. }
  112. if (c < 0x80)
  113. {
  114. continue;
  115. }
  116. // UTF-8:2 字节 C2~DF + 1 续字节;3 字节 E0~EF + 2 续字节
  117. if ((c >= 0xC2) && (c <= 0xDF) && ((c1 & 0xC0) == 0x80))
  118. {
  119. i += 1;
  120. continue;
  121. }
  122. if ((c >= 0xE0) && (c <= 0xEF)
  123. && ((c1 & 0xC0) == 0x80) && ((c2 & 0xC0) == 0x80))
  124. {
  125. i += 2;
  126. continue;
  127. }
  128. // 不是合法 UTF-8 序列:在这里截断,避免后面全是乱码
  129. detail[i] = '\0';
  130. break;
  131. }
  132. }
  133. static uint32_t get_valid_count(uint32_t write_seq)
  134. {
  135. uint32_t max_records = max_sectors * RECORDS_PER_SECTOR;
  136. return (write_seq < max_records) ? write_seq : max_records;
  137. }
  138. // v4 先把序号换算成扇区序再回绕,才是真正的整分区环形
  139. static void rec_pos(uint32_t seq, uint32_t ver, uint32_t *sector_start, uint32_t *offset)
  140. {
  141. uint32_t si;
  142. if (ver >= 4)
  143. {
  144. si = (seq / RECORDS_PER_SECTOR) % max_sectors;
  145. }
  146. else
  147. {
  148. si = seq % max_sectors;
  149. }
  150. *sector_start = SECTOR_SIZE + si * SECTOR_SIZE;
  151. *offset = (seq % RECORDS_PER_SECTOR) * RECORD_SIZE;
  152. }
  153. static int read_header(ecode_header_t *hdr)
  154. {
  155. if (fal_partition_read(ecode_part, 0, (uint8_t *)hdr, sizeof(ecode_header_t)) < 0)
  156. {
  157. LOG_E("read header failed");
  158. return -RT_ERROR;
  159. }
  160. return RT_EOK;
  161. }
  162. static int write_header(const ecode_header_t *hdr)
  163. {
  164. int ret = RT_EOK;
  165. // 这里要借 g_buf 读头扇区,读完 g_buf 里就是头扇区了,缓存必须作废
  166. cache_invalidate();
  167. if (fal_partition_read(ecode_part, 0, g_buf, SECTOR_SIZE) < 0)
  168. {
  169. LOG_E("read header sector failed");
  170. return -RT_ERROR;
  171. }
  172. memcpy(g_buf, hdr, sizeof(ecode_header_t));
  173. if (fal_partition_erase(ecode_part, 0, SECTOR_SIZE) < 0)
  174. {
  175. LOG_E("erase header sector failed");
  176. return -RT_ERROR;
  177. }
  178. wdg_feed_once(); // 擦完头扇区立即喂一次
  179. if (fal_partition_write(ecode_part, 0, g_buf, SECTOR_SIZE) != SECTOR_SIZE)
  180. {
  181. LOG_E("write header sector failed");
  182. ret = -RT_ERROR;
  183. }
  184. return ret;
  185. }
  186. static int erase_whole_partition(void)
  187. {
  188. uint32_t off;
  189. cache_invalidate();
  190. // 384KB 一次擦会让调用方长期无法喂狗,改成分扇区递进
  191. for (off = 0; off < ecode_part->len; off += SECTOR_SIZE)
  192. {
  193. if (fal_partition_erase(ecode_part, off, SECTOR_SIZE) < 0)
  194. {
  195. LOG_E("erase whole partition failed at %u", off);
  196. return -RT_ERROR;
  197. }
  198. wdg_feed_once(); // 每擦完一个扇区立即喂一次
  199. }
  200. return RT_EOK;
  201. }
  202. // 头部无效时扫描数据区重建序号;扫不到任何记录才整分区擦除
  203. static int ecode_rebuild_header(ecode_header_t *hdr)
  204. {
  205. uint32_t i, j;
  206. uint32_t found = 0, max_num = 0;
  207. uint32_t read_fail = 0;
  208. for (i = 0; i < max_sectors; i++)
  209. {
  210. if (fal_partition_read(ecode_part, SECTOR_SIZE + i * SECTOR_SIZE, g_buf, SECTOR_SIZE) < 0)
  211. {
  212. read_fail++;
  213. continue;
  214. }
  215. cache_invalidate();
  216. wdg_feed_once(); // 每扫完一个扇区立即喂一次
  217. for (j = 0; j < RECORDS_PER_SECTOR; j++)
  218. {
  219. ecode_record_t *r = (ecode_record_t *)(g_buf + j * RECORD_SIZE);
  220. // 空白扇区全 0xFF;num 单调递增且可远超单圈容量,故不能用圈容量做上限
  221. if (r->num >= 0xFFFFFF00u)
  222. {
  223. continue;
  224. }
  225. // 用事件码范围排除半写与垃圾数据
  226. if (r->code < (uint16_t)reboot_ec || r->code >= (uint16_t)e_typeMAX)
  227. {
  228. continue;
  229. }
  230. found++;
  231. if (r->num > max_num)
  232. {
  233. max_num = r->num;
  234. }
  235. }
  236. }
  237. // 本分区是从 zk_area 切出来的,可能残留字库等旧数据;扫出来的序号若远超
  238. // 合理范围,说明这些"记录"是垃圾,直接格式化,别按垃圾序号硬恢复
  239. if ((found > 0) && (max_num > (max_sectors * RECORDS_PER_SECTOR * 100u)))
  240. {
  241. LOG_W("rebuild: implausible seq %u, treat as garbage", max_num);
  242. found = 0;
  243. }
  244. if (found == 0)
  245. {
  246. // 读失败的扇区不等于空扇区:宁可暂不可用,也不误擦历史证据
  247. if (read_fail > 0)
  248. {
  249. LOG_E("rebuild: %u sector(s) unreadable, refuse to erase", read_fail);
  250. return -RT_ERROR;
  251. }
  252. LOG_W("no valid record, erase whole partition");
  253. if (erase_whole_partition() != RT_EOK)
  254. {
  255. LOG_E("erase failed");
  256. return -RT_ERROR;
  257. }
  258. hdr->magic = ECODE_MAGIC;
  259. hdr->version = ECODE_VERSION;
  260. hdr->write_seq = 0;
  261. return write_header(hdr);
  262. }
  263. hdr->magic = ECODE_MAGIC;
  264. hdr->version = ECODE_VERSION;
  265. // 序号必须延续原值,截断会使环形落点整体错位并覆盖有效记录
  266. hdr->write_seq = max_num + 1;
  267. s_header_rebuilt = 1;
  268. LOG_W("header rebuilt: found %u records, write_seq=%u", found, hdr->write_seq);
  269. return write_header(hdr);
  270. }
  271. static int ecode_init_core(void)
  272. {
  273. ecode_header_t hdr;
  274. uint32_t part_size;
  275. ecode_part = fal_partition_find(ECODE_PART_NAME);
  276. if (ecode_part == RT_NULL)
  277. {
  278. LOG_E("partition '%s' not found", ECODE_PART_NAME);
  279. return -RT_ERROR;
  280. }
  281. part_size = ecode_part->len;
  282. if (part_size < SECTOR_SIZE * 2)
  283. {
  284. LOG_E("partition too small (need at least %d bytes)", SECTOR_SIZE * 2);
  285. return -RT_ERROR;
  286. }
  287. // 头部占用扇区0,其余全部为数据扇区(与 BL 完全一致)
  288. max_sectors = (part_size - SECTOR_SIZE) / SECTOR_SIZE;
  289. LOG_I("partition %u bytes, max_sectors=%u, records=%u",
  290. part_size, max_sectors, max_sectors * RECORDS_PER_SECTOR);
  291. ecode_lock = rt_mutex_create("ecode", RT_IPC_FLAG_FIFO);
  292. if (ecode_lock == RT_NULL)
  293. {
  294. LOG_E("create mutex failed");
  295. return -RT_ERROR;
  296. }
  297. if (read_header(&hdr) == RT_EOK && hdr.magic == ECODE_MAGIC
  298. && hdr.version >= ECODE_VERSION_MIN && hdr.version <= ECODE_VERSION)
  299. {
  300. LOG_I("ecode ready, write_seq=%u", hdr.write_seq);
  301. return RT_EOK;
  302. }
  303. LOG_W("partition not initialized or invalid, try rebuild...");
  304. if (ecode_rebuild_header(&hdr) != RT_EOK)
  305. {
  306. LOG_E("write header failed");
  307. return -RT_ERROR;
  308. }
  309. return RT_EOK;
  310. }
  311. static int ensure_initialized(void)
  312. {
  313. if (initialized)
  314. {
  315. return RT_EOK;
  316. }
  317. // 分区坏了只试一次:反复重试会每次开机都擦一遍 384KB
  318. if (s_init_failed)
  319. {
  320. return -RT_ERROR;
  321. }
  322. if (init_mutex == RT_NULL)
  323. {
  324. init_mutex = rt_mutex_create("ecode_init", RT_IPC_FLAG_FIFO);
  325. if (init_mutex == RT_NULL)
  326. {
  327. LOG_E("create init mutex failed");
  328. return -RT_ERROR;
  329. }
  330. }
  331. rt_mutex_take(init_mutex, RT_WAITING_FOREVER);
  332. if (!initialized)
  333. {
  334. if (ecode_init_core() == RT_EOK)
  335. {
  336. initialized = RT_TRUE;
  337. }
  338. else
  339. {
  340. LOG_E("ecode_init_core failed, give up for this boot");
  341. s_init_failed = RT_TRUE;
  342. }
  343. }
  344. rt_mutex_release(init_mutex);
  345. return initialized ? RT_EOK : -RT_ERROR;
  346. }
  347. int ecode_init(void)
  348. {
  349. return ensure_initialized();
  350. }
  351. // 写一条已组装好的记录:num 由 header 统一分配,时间由调用方填
  352. static int ecode_write_rec(ecode_record_t *rec)
  353. {
  354. ecode_header_t hdr;
  355. uint32_t seq, record_offset_in_sector;
  356. uint32_t sector_start;
  357. uint32_t k;
  358. int slot_blank;
  359. if (ensure_initialized() != RT_EOK)
  360. {
  361. return -RT_ERROR;
  362. }
  363. rt_mutex_take(ecode_lock, RT_WAITING_FOREVER);
  364. if (read_header(&hdr) != RT_EOK)
  365. {
  366. LOG_E("read header failed");
  367. rt_mutex_release(ecode_lock);
  368. return -RT_ERROR;
  369. }
  370. if (hdr.magic != ECODE_MAGIC || hdr.version < ECODE_VERSION_MIN
  371. || hdr.version > ECODE_VERSION)
  372. {
  373. LOG_E("header corrupted");
  374. rt_mutex_release(ecode_lock);
  375. return -RT_ERROR;
  376. }
  377. seq = hdr.write_seq;
  378. rec->num = seq;
  379. rec_pos(seq, hdr.version, &sector_start, &record_offset_in_sector);
  380. if (load_sector(sector_start) != RT_EOK)
  381. {
  382. LOG_E("read target sector failed");
  383. rt_mutex_release(ecode_lock);
  384. return -RT_ERROR;
  385. }
  386. // 目标槽位是否仍为空(全 0xFF)。NOR 只能把 1 写成 0,空槽位可直接写,无需擦整扇区
  387. slot_blank = 1;
  388. for (k = 0; k < RECORD_SIZE; k++)
  389. {
  390. if (g_buf[record_offset_in_sector + k] != 0xFF)
  391. {
  392. slot_blank = 0;
  393. break;
  394. }
  395. }
  396. memcpy(g_buf + record_offset_in_sector, rec, RECORD_SIZE);
  397. if (slot_blank)
  398. {
  399. // 槽位为空:只写本条 64 字节,擦除窗口由每条一次降到每 64 条一次
  400. if (fal_partition_write(ecode_part, sector_start + record_offset_in_sector,
  401. g_buf + record_offset_in_sector, RECORD_SIZE) != RECORD_SIZE)
  402. {
  403. LOG_E("write record failed, data may be lost!");
  404. rt_mutex_release(ecode_lock);
  405. return -RT_ERROR;
  406. }
  407. }
  408. else
  409. {
  410. // 槽位已被占用(环形回绕到旧位置):必须先擦除整个扇区
  411. if (fal_partition_erase(ecode_part, sector_start, SECTOR_SIZE) < 0)
  412. {
  413. LOG_E("erase target sector failed");
  414. rt_mutex_release(ecode_lock);
  415. return -RT_ERROR;
  416. }
  417. wdg_feed_once(); // 擦完目标扇区立即喂一次
  418. if (fal_partition_write(ecode_part, sector_start, g_buf, SECTOR_SIZE) != SECTOR_SIZE)
  419. {
  420. LOG_E("write target sector failed, data may be lost!");
  421. rt_mutex_release(ecode_lock);
  422. return -RT_ERROR;
  423. }
  424. }
  425. // 写完 flash 后 g_buf 与介质未必一致(写失败时更明显),缓存一律作废
  426. cache_invalidate();
  427. // 序号必须最后落盘:先写数据再推 header,掉电最多丢最后一条而不会写乱环形
  428. hdr.write_seq = seq + 1;
  429. if (write_header(&hdr) != RT_EOK)
  430. {
  431. LOG_E("write header failed, but data written");
  432. }
  433. rt_mutex_release(ecode_lock);
  434. return RT_EOK;
  435. }
  436. // detail 落 Flash 统一存 UTF-8 —— 与 BL v4 的记录完全一致,两边互读不会乱码。
  437. // 源文件是 GBK(工程约定),调用点写的字面量也是 GBK,所以在这里转一次。
  438. // gb2312_str_to_utf8 按码点分段写 1/2/3 字节、写满即停,输出永远是合法 UTF-8,
  439. // 不会留下半个汉字;表里查不到的字退化成 '?',不会静默丢整条文案。
  440. static void copy_detail_utf8(char *dst, size_t dst_size, const char *src)
  441. {
  442. if ((dst == RT_NULL) || (dst_size == 0))
  443. {
  444. return;
  445. }
  446. dst[0] = '\0';
  447. if (src == RT_NULL)
  448. {
  449. return;
  450. }
  451. (void)gb2312_str_to_utf8(src, dst, (uint16_t)dst_size);
  452. }
  453. // 记录一个事件。detail 是 GBK 文案(与源码同编码),调用点直接给,
  454. // 这里转成 UTF-8 后落 Flash —— 与 BL v4 记录编码一致,两边互读不会乱码。
  455. void Recode_e_code(ecode_list code, uint16_t para, const char *detail)
  456. {
  457. ecode_record_t rec;
  458. // 关闭记录时默认不写,但升级/看门狗等关键路径必须留痕
  459. if (!en_recode.en && !ecode_always_record((uint16_t)code))
  460. {
  461. return;
  462. }
  463. if (detail == RT_NULL)
  464. {
  465. detail = "";
  466. }
  467. rt_memset(&rec, 0, sizeof(rec));
  468. // 取时间前先刷 RTC 全局量,保证记录里是上报刷新的即时时间
  469. Reflash_RTC();
  470. rec.time_stamp.tm_year = (uint8_t)((year >= 2000) ? (year - 2000) : 0);
  471. rec.time_stamp.tm_mon = (uint8_t)month;
  472. rec.time_stamp.tm_mday = (uint8_t)day;
  473. rec.time_stamp.tm_hour = (uint8_t)hour;
  474. rec.time_stamp.tm_min = (uint8_t)min;
  475. rec.time_stamp.tm_sec = (uint8_t)sec;
  476. rec.code = (uint16_t)code;
  477. rec.para = para;
  478. copy_detail_utf8(rec.detail, sizeof(rec.detail), detail);
  479. if (ecode_write_rec(&rec) == RT_EOK)
  480. {
  481. LOG_I("ecode c=%u p=%u d=%s", (unsigned)code, para, rec.detail);
  482. }
  483. }
  484. // 旧版 ecode 存 easyflash env:60 条 x 6 字节 + 一个 16 位写指针
  485. #define EF_ECODE_MAX 60
  486. typedef struct
  487. {
  488. uint32_t Time_Stampe;
  489. uint8_t code;
  490. uint8_t para;
  491. } ef_ecode_rec_t;
  492. // 把旧 env 里的记录搬进新分区,搬完删掉旧 key。
  493. // 只在新区为空时执行,重复上电不会重复搬。
  494. void ecode_migrate_from_ef(void)
  495. {
  496. static ef_ecode_rec_t old[EF_ECODE_MAX];
  497. uint16_t ptr = 0;
  498. uint32_t i, start, cnt, months;
  499. ecode_record_t rec;
  500. if (ensure_initialized() != RT_EOK)
  501. {
  502. return;
  503. }
  504. // 迁移完成标记:迁到一半掉电时 marker 还没写,下次会重来。
  505. // 重来前先把新分区清空,避免重复搬出一倍记录
  506. {
  507. uint8_t marker = 0;
  508. if (ef_get_env_blob("ecode_mig", &marker, sizeof(marker), RT_NULL) > 0 && marker == 1)
  509. {
  510. return;
  511. }
  512. }
  513. if (ef_get_env_blob("e_pointer", &ptr, sizeof(ptr), RT_NULL) == 0 || ptr == 0)
  514. {
  515. // 没有旧数据,直接标记为已迁移,别每次都来查一遍
  516. uint8_t marker = 1;
  517. ef_set_env_blob("ecode_mig", &marker, sizeof(marker));
  518. return;
  519. }
  520. if (ef_get_env_blob("e_code", old, sizeof(old), RT_NULL) == 0)
  521. {
  522. return;
  523. }
  524. // 上次可能迁到一半,先清干净再整体搬;新分区本来就空就不用白擦 384KB
  525. if (ecode_get_count() > 0)
  526. {
  527. ecode_clear();
  528. }
  529. cnt = (ptr > EF_ECODE_MAX) ? EF_ECODE_MAX : ptr;
  530. start = (ptr > EF_ECODE_MAX) ? (ptr % EF_ECODE_MAX) : 0;
  531. // 旧区是环形,写到 ptr % 60 处停下,从那里开始才是时间上最旧的一条
  532. for (i = 0; i < cnt; i++)
  533. {
  534. ef_ecode_rec_t *o = &old[(start + i) % EF_ECODE_MAX];
  535. if (o->code < (uint8_t)reboot_ec || o->code >= (uint8_t)e_typeMAX)
  536. {
  537. continue;
  538. }
  539. rt_memset(&rec, 0, sizeof(rec));
  540. months = (o->Time_Stampe & 0xFFC00000u) >> 22;
  541. rec.time_stamp.tm_year = (uint8_t)(months / 12);
  542. rec.time_stamp.tm_mon = (uint8_t)(months % 12);
  543. if (rec.time_stamp.tm_mon == 0 && rec.time_stamp.tm_year != 0)
  544. {
  545. rec.time_stamp.tm_mon = 12;
  546. rec.time_stamp.tm_year--;
  547. }
  548. rec.time_stamp.tm_mday = (uint8_t)((o->Time_Stampe & 0x3E0000u) >> 17);
  549. rec.time_stamp.tm_hour = (uint8_t)((o->Time_Stampe & 0x1F000u) >> 12);
  550. rec.time_stamp.tm_min = (uint8_t)((o->Time_Stampe & 0xFC0u) >> 6);
  551. rec.time_stamp.tm_sec = (uint8_t)(o->Time_Stampe & 0x3Fu);
  552. rec.code = o->code;
  553. rec.para = o->para;
  554. copy_detail_utf8(rec.detail, sizeof(rec.detail), "旧版记录迁移");
  555. ecode_write_rec(&rec);
  556. }
  557. // 汇总条数写进 detail,比只记一个 para 更好查
  558. {
  559. char dsc[40];
  560. rt_snprintf(dsc, sizeof(dsc), "旧记录已迁移 共%u条", (unsigned)cnt);
  561. Recode_e_code(e_ecode_rebuild, (uint16_t)cnt, dsc);
  562. }
  563. {
  564. uint8_t marker = 1;
  565. ef_set_env_blob("ecode_mig", &marker, sizeof(marker));
  566. }
  567. ef_del_env("e_code");
  568. ef_del_env("e_pointer");
  569. LOG_I("migrated %u legacy records", cnt);
  570. }
  571. // 当前最新记录的绝对序号;空库返回 0xFFFFFFFF
  572. uint32_t ecode_newest_seq(void)
  573. {
  574. ecode_header_t hdr;
  575. uint32_t cnt;
  576. uint32_t newest = 0xFFFFFFFFu;
  577. if (ensure_initialized() != RT_EOK)
  578. {
  579. return 0xFFFFFFFFu;
  580. }
  581. rt_mutex_take(ecode_lock, RT_WAITING_FOREVER);
  582. if (read_header(&hdr) == RT_EOK && hdr.magic == ECODE_MAGIC
  583. && hdr.version >= ECODE_VERSION_MIN && hdr.version <= ECODE_VERSION)
  584. {
  585. cnt = get_valid_count(hdr.write_seq);
  586. if (cnt > 0)
  587. {
  588. newest = hdr.write_seq - 1;
  589. }
  590. }
  591. rt_mutex_release(ecode_lock);
  592. return newest;
  593. }
  594. // 按绝对序号读。分包上报必须用它:用 index 读的话,中途来一条新记录
  595. // 就会让所有后续 index 整体后移一位,导致重复读和漏读
  596. int ecode_read_seq(uint32_t seq, ecode_record_t *rec)
  597. {
  598. ecode_header_t hdr;
  599. uint32_t valid_count;
  600. uint32_t record_offset_in_sector;
  601. uint32_t sector_start;
  602. uint32_t i;
  603. uint8_t *p;
  604. int all_ff = 1;
  605. if (ensure_initialized() != RT_EOK)
  606. {
  607. return -RT_ERROR;
  608. }
  609. rt_mutex_take(ecode_lock, RT_WAITING_FOREVER);
  610. if (read_header(&hdr) != RT_EOK)
  611. {
  612. rt_mutex_release(ecode_lock);
  613. return -RT_ERROR;
  614. }
  615. if (hdr.magic != ECODE_MAGIC || hdr.version < ECODE_VERSION_MIN
  616. || hdr.version > ECODE_VERSION)
  617. {
  618. LOG_E("header corrupted");
  619. rt_mutex_release(ecode_lock);
  620. return -RT_ERROR;
  621. }
  622. // 序号必须还没写、且仍在环形保留窗口内;被新记录覆盖了就判读不到
  623. valid_count = get_valid_count(hdr.write_seq);
  624. if ((seq >= hdr.write_seq) || ((hdr.write_seq - seq) > valid_count))
  625. {
  626. rt_mutex_release(ecode_lock);
  627. return -RT_ERROR;
  628. }
  629. rec_pos(seq, hdr.version, &sector_start, &record_offset_in_sector);
  630. if (load_sector(sector_start) != RT_EOK)
  631. {
  632. LOG_E("read sector failed");
  633. rt_mutex_release(ecode_lock);
  634. return -RT_ERROR;
  635. }
  636. memcpy(rec, g_buf + record_offset_in_sector, RECORD_SIZE);
  637. sanitize_detail(rec->detail, sizeof(rec->detail));
  638. p = (uint8_t *)rec;
  639. for (i = 0; i < RECORD_SIZE; i++)
  640. {
  641. if (p[i] != 0xFF)
  642. {
  643. all_ff = 0;
  644. break;
  645. }
  646. }
  647. if (all_ff)
  648. {
  649. rt_mutex_release(ecode_lock);
  650. return -RT_ERROR;
  651. }
  652. if (rec->num != seq)
  653. {
  654. LOG_W("seq %u num mismatch: got %u (layout broken?)", seq, rec->num);
  655. rt_mutex_release(ecode_lock);
  656. return -RT_ERROR;
  657. }
  658. rt_mutex_release(ecode_lock);
  659. return RT_EOK;
  660. }
  661. // 按逻辑序号读记录,index 0 为最新
  662. int ecode_read(uint32_t index, ecode_record_t *rec)
  663. {
  664. ecode_header_t hdr;
  665. uint32_t valid_count;
  666. uint32_t logical_seq, record_offset_in_sector;
  667. uint32_t sector_start;
  668. uint32_t i;
  669. uint8_t *p;
  670. int all_ff = 1;
  671. if (ensure_initialized() != RT_EOK)
  672. {
  673. return -RT_ERROR;
  674. }
  675. rt_mutex_take(ecode_lock, RT_WAITING_FOREVER);
  676. if (read_header(&hdr) != RT_EOK)
  677. {
  678. rt_mutex_release(ecode_lock);
  679. return -RT_ERROR;
  680. }
  681. if (hdr.magic != ECODE_MAGIC || hdr.version < ECODE_VERSION_MIN
  682. || hdr.version > ECODE_VERSION)
  683. {
  684. LOG_E("header corrupted");
  685. rt_mutex_release(ecode_lock);
  686. return -RT_ERROR;
  687. }
  688. valid_count = get_valid_count(hdr.write_seq);
  689. if (index >= valid_count)
  690. {
  691. rt_mutex_release(ecode_lock);
  692. return -RT_ERROR;
  693. }
  694. logical_seq = hdr.write_seq - 1 - index;
  695. rec_pos(logical_seq, hdr.version, &sector_start, &record_offset_in_sector);
  696. if (load_sector(sector_start) != RT_EOK)
  697. {
  698. LOG_E("read sector failed");
  699. rt_mutex_release(ecode_lock);
  700. return -RT_ERROR;
  701. }
  702. memcpy(rec, g_buf + record_offset_in_sector, RECORD_SIZE);
  703. sanitize_detail(rec->detail, sizeof(rec->detail));
  704. p = (uint8_t *)rec;
  705. for (i = 0; i < RECORD_SIZE; i++)
  706. {
  707. if (p[i] != 0xFF)
  708. {
  709. all_ff = 0;
  710. break;
  711. }
  712. }
  713. if (all_ff)
  714. {
  715. rt_mutex_release(ecode_lock);
  716. return -RT_ERROR;
  717. }
  718. // 序号必须与逻辑位置严格对应,错位时宁可报读不到也不返回假数据
  719. if (rec->num != logical_seq)
  720. {
  721. LOG_W("index %u num mismatch: expect %u got %u (layout broken?)",
  722. index, logical_seq, rec->num);
  723. rt_mutex_release(ecode_lock);
  724. return -RT_ERROR;
  725. }
  726. rt_mutex_release(ecode_lock);
  727. return RT_EOK;
  728. }
  729. uint32_t ecode_get_count(void)
  730. {
  731. ecode_header_t hdr;
  732. uint32_t count;
  733. if (ensure_initialized() != RT_EOK)
  734. {
  735. return 0;
  736. }
  737. rt_mutex_take(ecode_lock, RT_WAITING_FOREVER);
  738. if (read_header(&hdr) != RT_EOK)
  739. {
  740. rt_mutex_release(ecode_lock);
  741. return 0;
  742. }
  743. if (hdr.magic != ECODE_MAGIC || hdr.version < ECODE_VERSION_MIN
  744. || hdr.version > ECODE_VERSION)
  745. {
  746. rt_mutex_release(ecode_lock);
  747. return 0;
  748. }
  749. count = get_valid_count(hdr.write_seq);
  750. rt_mutex_release(ecode_lock);
  751. return count;
  752. }
  753. void ecode_clear(void)
  754. {
  755. ecode_header_t hdr;
  756. if (ensure_initialized() != RT_EOK)
  757. {
  758. LOG_E("ecode not initialized");
  759. return;
  760. }
  761. rt_mutex_take(ecode_lock, RT_WAITING_FOREVER);
  762. if (erase_whole_partition() != RT_EOK)
  763. {
  764. LOG_E("erase whole partition failed");
  765. rt_mutex_release(ecode_lock);
  766. return;
  767. }
  768. hdr.magic = ECODE_MAGIC;
  769. hdr.version = ECODE_VERSION;
  770. hdr.write_seq = 0;
  771. if (write_header(&hdr) != RT_EOK)
  772. {
  773. LOG_E("write header failed after clear");
  774. }
  775. else
  776. {
  777. LOG_I("ecode cleared");
  778. }
  779. rt_mutex_release(ecode_lock);
  780. }
  781. uint32_t ecode_max_records(void)
  782. {
  783. if (ensure_initialized() != RT_EOK)
  784. {
  785. return 0;
  786. }
  787. return max_sectors * RECORDS_PER_SECTOR;
  788. }
  789. // 返回并清除"头部曾被重建"标记
  790. int ecode_header_rebuilt(void)
  791. {
  792. int r = s_header_rebuilt;
  793. s_header_rebuilt = 0;
  794. return r;
  795. }
  796. // ====================== MSH 命令 ======================
  797. #include <finsh.h>
  798. static void ecode_print(void)
  799. {
  800. ecode_record_t rec;
  801. uint32_t cnt = ecode_get_count();
  802. uint32_t i;
  803. rt_kprintf("ecode count: %u / max %u\n", cnt, ecode_max_records());
  804. for (i = 0; i < cnt; i++)
  805. {
  806. if (ecode_read(i, &rec) == 0)
  807. {
  808. rt_kprintf("%u - %02u/%02u/%02u %02u:%02u:%02u: (%5u-%-5u) %s\n",
  809. rec.num,
  810. rec.time_stamp.tm_year, rec.time_stamp.tm_mon, rec.time_stamp.tm_mday,
  811. rec.time_stamp.tm_hour, rec.time_stamp.tm_min, rec.time_stamp.tm_sec,
  812. rec.code, rec.para, rec.detail);
  813. }
  814. else
  815. {
  816. rt_kprintf("read error at index %u\n", i);
  817. }
  818. }
  819. }
  820. MSH_CMD_EXPORT_ALIAS(ecode_print, ecode, print all ecode records);
  821. static void recode_test(void)
  822. {
  823. Recode_e_code(e_update_start, 99, "手动测试记录");
  824. }
  825. MSH_CMD_EXPORT(recode_test, write a test ecode);