param_mirror.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2026, BZY
  3. *
  4. * Anti-loss parameter mirror: store a packed copy of key config fields in
  5. * two dedicated sectors at the tail of the FAL "easyflash" partition.
  6. *
  7. * The mirror is independent from EasyFlash's GC/log areas. It protects
  8. * against:
  9. * - whole sector bit-rot / wear failure
  10. * - partial writes caused by power loss in the middle of EF set
  11. *
  12. * Layout (each slot fits in one 4096-byte sector):
  13. *
  14. * offset field
  15. * ------ -------------------------------------------------
  16. * 0x00 magic (0x50415242 'PARB')
  17. * 0x04 version (layout version, bump on incompatible change)
  18. * 0x08 write_seq (monotonic, used to pick newest copy)
  19. * 0x0C crc32 (over payload only)
  20. * 0x10 payload (param_payload_t, packed)
  21. *
  22. * Slot A : MIRROR_BASE_ADDR .. + MIRROR_SLOT_SIZE
  23. * Slot B : MIRROR_BASE_ADDR + SLOT .. + MIRROR_SLOT_SIZE
  24. */
  25. #ifndef APPLICATIONS_PARAM_MIRROR_H_
  26. #define APPLICATIONS_PARAM_MIRROR_H_
  27. #include <rtthread.h>
  28. #include <stdint.h>
  29. #include <stdbool.h>
  30. /* Packed copy of every user-settable config field.
  31. * Keep this struct append-only. When layout changes, bump PARAM_PAYLOAD_VER
  32. * and (optionally) keep a converter from the old version. */
  33. typedef struct __packed {
  34. /* version + bookkeeping, NOT config fields */
  35. uint32_t magic; /* 'PARB' */
  36. uint32_t version; /* PARAM_PAYLOAD_VER */
  37. uint32_t write_seq; /* monotonic counter */
  38. uint32_t crc32; /* over fields below */
  39. uint8_t iSave;
  40. uint8_t dtype;
  41. uint8_t ds;
  42. uint8_t clt_time;
  43. uint8_t heattime;
  44. uint8_t heatvalue;
  45. uint16_t thsp;
  46. uint8_t sunBar;
  47. uint8_t startBar;
  48. uint8_t stopBar;
  49. uint8_t timeSwitch;
  50. uint8_t tph;
  51. int8_t tpl;
  52. uint8_t shake_ws;
  53. uint8_t shake_sec;
  54. int passWord;
  55. uint16_t dattim;
  56. uint8_t version_type;
  57. uint8_t Power_Type;
  58. uint8_t handle_bugger;
  59. } param_payload_t;
  60. #define PARAM_PAYLOAD_VER 0x00010001U /* bump on incompatible change */
  61. /* Mirror slot layout (one 4096-byte sector per slot).
  62. * Place the mirror after ENV_AREA_SIZE (64KB) and LOG_AREA_SIZE (8KB),
  63. * inside the 3MB "easyflash" FAL partition.
  64. *
  65. * 2026-10-29:ENV_AREA_SIZE 由 1MB 降为 64KB(见 EasyFlash 的 ef_cfg.h),
  66. * 而 ef_log.c:101 的日志区起始 = EF_START_ADDR + ENV_AREA_SIZE,会随之从
  67. * 0x100000 前移到 0x10000。镜像必须同步前移,否则会落在日志区里被互相破坏。
  68. * 布局:ENV 0x00000~0x10000 | LOG 0x10000~0x12000 | 镜像A 0x12000 | 镜像B 0x13000 */
  69. #define MIRROR_SECTOR_SIZE 4096U
  70. #define MIRROR_BASE_OFFSET (64UL * 1024UL + 8UL * 1024UL) /* 0x12000 */
  71. #define MIRROR_SLOT_SIZE MIRROR_SECTOR_SIZE
  72. #define MIRROR_SLOT_A_ADDR (MIRROR_BASE_OFFSET)
  73. #define MIRROR_SLOT_B_ADDR (MIRROR_BASE_OFFSET + MIRROR_SLOT_SIZE)
  74. /**
  75. * Load the latest valid mirror copy into dst. Tries slot A and slot B,
  76. * picks the one with the higher write_seq and a correct CRC.
  77. *
  78. * @return RT_EOK if a valid copy was found, -RT_ERROR otherwise.
  79. */
  80. int param_mirror_load(param_payload_t *dst);
  81. /**
  82. * Erase both mirror slots and invalidate the in-RAM "last payload" cache.
  83. *
  84. * 2026-10-29 新增(供远程擦除 Flash 指令使用)。**必须清缓存**:本模块用
  85. * s_last_payload/s_last_valid 做写放大抑制,若只擦 Flash 不清缓存,后续
  86. * param_mirror_sync() 会因"内容未变化"直接返回,镜像将永久为空、失去防丢保护。
  87. *
  88. * @return RT_EOK on success.
  89. */
  90. int param_mirror_erase_all(void);
  91. /**
  92. * Atomically store src to the mirror. Writes to the older slot first,
  93. * verifies, then writes to the other slot. Bumps write_seq so the next
  94. * load prefers the freshly written copy.
  95. *
  96. * @return RT_EOK on success.
  97. */
  98. int param_mirror_save(const param_payload_t *src);
  99. /**
  100. * Pack the relevant fields from device_status into out.
  101. * Used by Check_ParamConfig() to seed the mirror with defaults.
  102. */
  103. void param_mirror_pack_defaults(param_payload_t *out);
  104. /**
  105. * Convenience: pack current device_status into a payload and write to the
  106. * mirror. Call this from any code path that mutates persistent config so
  107. * the mirror stays in sync with EF.
  108. *
  109. * Implemented in param_mirror.c; safe to call before Check_ParamConfig().
  110. */
  111. void param_mirror_sync(void);
  112. /**
  113. * Apply mirror fields back into device_status. Used during boot to
  114. * recover from a lost iSave when the mirror itself is intact.
  115. *
  116. * @return the mirror's write_seq on success, 0 on any failure (e.g. CRC
  117. * mismatch — caller should fall back to defaults in that case).
  118. */
  119. uint32_t mirror_to_device_status(const param_payload_t *in);
  120. #endif /* APPLICATIONS_PARAM_MIRROR_H_ */