param_mirror.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 (1MB) and LOG_AREA_SIZE (8KB),
  63. * inside the 3MB "easyflash" FAL partition. */
  64. #define MIRROR_SECTOR_SIZE 4096U
  65. #define MIRROR_BASE_OFFSET (1UL * 1024UL * 1024UL + 8UL * 1024UL) /* 0x102000 */
  66. #define MIRROR_SLOT_SIZE MIRROR_SECTOR_SIZE
  67. #define MIRROR_SLOT_A_ADDR (MIRROR_BASE_OFFSET)
  68. #define MIRROR_SLOT_B_ADDR (MIRROR_BASE_OFFSET + MIRROR_SLOT_SIZE)
  69. /**
  70. * Load the latest valid mirror copy into dst. Tries slot A and slot B,
  71. * picks the one with the higher write_seq and a correct CRC.
  72. *
  73. * @return RT_EOK if a valid copy was found, -RT_ERROR otherwise.
  74. */
  75. int param_mirror_load(param_payload_t *dst);
  76. /**
  77. * Atomically store src to the mirror. Writes to the older slot first,
  78. * verifies, then writes to the other slot. Bumps write_seq so the next
  79. * load prefers the freshly written copy.
  80. *
  81. * @return RT_EOK on success.
  82. */
  83. int param_mirror_save(const param_payload_t *src);
  84. /**
  85. * Pack the relevant fields from device_status into out.
  86. * Used by Check_ParamConfig() to seed the mirror with defaults.
  87. */
  88. void param_mirror_pack_defaults(param_payload_t *out);
  89. /**
  90. * Convenience: pack current device_status into a payload and write to the
  91. * mirror. Call this from any code path that mutates persistent config so
  92. * the mirror stays in sync with EF.
  93. *
  94. * Implemented in param_mirror.c; safe to call before Check_ParamConfig().
  95. */
  96. void param_mirror_sync(void);
  97. /**
  98. * Apply mirror fields back into device_status. Used during boot to
  99. * recover from a lost iSave when the mirror itself is intact.
  100. *
  101. * @return the mirror's write_seq on success, 0 on any failure (e.g. CRC
  102. * mismatch — caller should fall back to defaults in that case).
  103. */
  104. uint32_t mirror_to_device_status(const param_payload_t *in);
  105. #endif /* APPLICATIONS_PARAM_MIRROR_H_ */