ecode.c 25 KB

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