|
1 | | -/** |
2 | | - * @file eos_bootctl.h |
3 | | - * @brief Boot control block management for eBootloader |
4 | | - * |
5 | | - * The boot control block stores persistent state required to make |
6 | | - * deterministic boot decisions across reset cycles. It is stored |
7 | | - * in a dedicated flash sector with CRC protection and optional |
8 | | - * redundancy. |
9 | | - */ |
10 | | - |
11 | | -#ifndef EOS_BOOTCTL_H |
12 | | -#define EOS_BOOTCTL_H |
13 | | - |
14 | | -#include "eos_types.h" |
15 | | - |
16 | | -/* ---------------- Boot Control Block ---------------- */ |
17 | | - |
18 | | -typedef struct { |
19 | | - uint32_t magic; /* EOS_BOOTCTL_MAGIC */ |
20 | | - uint32_t version; /* Control block format version */ |
21 | | - uint32_t active_slot; /* Currently active slot (eos_slot_t) */ |
22 | | - uint32_t pending_slot; /* Slot pending test boot (eos_slot_t) */ |
23 | | - uint32_t confirmed_slot; /* Last confirmed-good slot (eos_slot_t) */ |
24 | | - uint32_t boot_attempts; /* Current consecutive boot attempt count */ |
25 | | - uint32_t max_attempts; /* Maximum allowed boot attempts */ |
26 | | - uint32_t last_reset_reason; /* Last reset cause (eos_reset_reason_t) */ |
27 | | - uint32_t flags; /* EOS_FLAG_* */ |
28 | | - uint32_t img_a_version; /* Version of image in Slot A */ |
29 | | - uint32_t img_b_version; /* Version of image in Slot B */ |
30 | | - uint32_t img_a_crc; /* CRC32 of image in Slot A */ |
31 | | - uint32_t img_b_crc; /* CRC32 of image in Slot B */ |
32 | | - uint32_t log_head; /* Index of next log entry to write */ |
33 | | - uint32_t boot_count; /* Total boot count since factory */ |
34 | | - uint32_t reserved[7]; /* Reserved for future use */ |
35 | | - uint32_t crc32; /* CRC32 of this structure (excluding this field) */ |
36 | | -} eos_bootctl_t; |
37 | | - |
38 | | -#define EOS_BOOTCTL_VERSION 1 |
39 | | - |
40 | | -/* ---------------- Boot Control API ---------------- */ |
41 | | - |
42 | | -/** |
43 | | - * @brief Load the boot control block from flash. |
44 | | - * Reads the primary copy; falls back to the backup copy if the primary |
45 | | - * is corrupt. |
46 | | - * @param bctl Pointer to structure to populate. |
47 | | - * @return EOS_OK on success, EOS_ERR_CRC if both copies are corrupt. |
48 | | - */ |
49 | | -int eos_bootctl_load(eos_bootctl_t *bctl); |
50 | | - |
51 | | -/** |
52 | | - * @brief Save the boot control block to flash. |
53 | | - * Writes both primary and backup copies atomically with CRC. |
54 | | - * @param bctl Pointer to structure to save. |
55 | | - * @return EOS_OK on success, EOS_ERR_FLASH on write failure. |
56 | | - */ |
57 | | -int eos_bootctl_save(eos_bootctl_t *bctl); |
58 | | - |
59 | | -/** |
60 | | - * @brief Initialize a fresh boot control block with defaults. |
61 | | - * Used on first boot or after factory reset. |
62 | | - * @param bctl Pointer to structure to initialize. |
63 | | - */ |
64 | | -void eos_bootctl_init_defaults(eos_bootctl_t *bctl); |
65 | | - |
66 | | -/** |
67 | | - * @brief Validate the CRC of a boot control block. |
68 | | - * @param bctl Pointer to structure to validate. |
69 | | - * @return true if CRC matches, false otherwise. |
70 | | - */ |
71 | | -bool eos_bootctl_validate(const eos_bootctl_t *bctl); |
72 | | - |
73 | | -/** |
74 | | - * @brief Increment the boot attempt counter and save. |
75 | | - * @param bctl Pointer to active boot control block. |
76 | | - * @return EOS_OK on success. |
77 | | - */ |
78 | | -int eos_bootctl_increment_attempts(eos_bootctl_t *bctl); |
79 | | - |
80 | | -/** |
81 | | - * @brief Reset boot attempt counter to zero and save. |
82 | | - * @param bctl Pointer to active boot control block. |
83 | | - * @return EOS_OK on success. |
84 | | - */ |
85 | | -int eos_bootctl_reset_attempts(eos_bootctl_t *bctl); |
86 | | - |
87 | | -/** |
88 | | - * @brief Set the pending slot for test boot. |
89 | | - * @param bctl Pointer to active boot control block. |
90 | | - * @param slot Target slot. |
91 | | - * @return EOS_OK on success. |
92 | | - */ |
93 | | -int eos_bootctl_set_pending(eos_bootctl_t *bctl, eos_slot_t slot); |
94 | | - |
95 | | -/** |
96 | | - * @brief Clear the pending slot after a failed verification. |
97 | | - * @param bctl Pointer to active boot control block. |
98 | | - * @return EOS_OK on success. |
99 | | - */ |
100 | | -int eos_bootctl_clear_pending(eos_bootctl_t *bctl); |
101 | | - |
102 | | -/** |
103 | | - * @brief Confirm the active slot as known-good. |
104 | | - * Clears the test boot flag and marks the active slot as confirmed. |
105 | | - * @param bctl Pointer to active boot control block. |
106 | | - * @return EOS_OK on success. |
107 | | - */ |
108 | | -int eos_bootctl_confirm(eos_bootctl_t *bctl); |
109 | | - |
110 | | -/** |
111 | | - * @brief Set the force-recovery flag. |
112 | | - * @param bctl Pointer to active boot control block. |
113 | | - * @return EOS_OK on success. |
114 | | - */ |
115 | | -int eos_bootctl_request_recovery(eos_bootctl_t *bctl); |
116 | | - |
117 | | -/** |
118 | | - * @brief Set the factory reset flag. |
119 | | - * @param bctl Pointer to active boot control block. |
120 | | - * @return EOS_OK on success. |
121 | | - */ |
122 | | -int eos_bootctl_request_factory_reset(eos_bootctl_t *bctl); |
123 | | - |
124 | | -/** |
125 | | - * @brief Get the alternate slot. |
126 | | - * @param slot Current slot. |
127 | | - * @return The other slot (A→B, B→A), or EOS_SLOT_NONE. |
128 | | - */ |
129 | | -eos_slot_t eos_bootctl_other_slot(eos_slot_t slot); |
130 | | - |
131 | | -#endif /* EOS_BOOTCTL_H */ |
| 1 | +/** |
| 2 | + * @file eos_bootctl.h |
| 3 | + * @brief Boot control block management for eBootloader |
| 4 | + * |
| 5 | + * The boot control block stores persistent state required to make |
| 6 | + * deterministic boot decisions across reset cycles. It is stored |
| 7 | + * in a dedicated flash sector with CRC protection and optional |
| 8 | + * redundancy. |
| 9 | + */ |
| 10 | + |
| 11 | +#ifndef EOS_BOOTCTL_H |
| 12 | +#define EOS_BOOTCTL_H |
| 13 | + |
| 14 | +#include "eos_types.h" |
| 15 | + |
| 16 | +#ifdef __cplusplus |
| 17 | +extern "C" { |
| 18 | +#endif |
| 19 | + |
| 20 | +/* ---------------- Boot Control Block ---------------- */ |
| 21 | + |
| 22 | +typedef struct { |
| 23 | + uint32_t magic; /* EOS_BOOTCTL_MAGIC */ |
| 24 | + uint32_t version; /* Control block format version */ |
| 25 | + uint32_t active_slot; /* Currently active slot (eos_slot_t) */ |
| 26 | + uint32_t pending_slot; /* Slot pending test boot (eos_slot_t) */ |
| 27 | + uint32_t confirmed_slot; /* Last confirmed-good slot (eos_slot_t) */ |
| 28 | + uint32_t boot_attempts; /* Current consecutive boot attempt count */ |
| 29 | + uint32_t max_attempts; /* Maximum allowed boot attempts */ |
| 30 | + uint32_t last_reset_reason; /* Last reset cause (eos_reset_reason_t) */ |
| 31 | + uint32_t flags; /* EOS_FLAG_* */ |
| 32 | + uint32_t img_a_version; /* Version of image in Slot A */ |
| 33 | + uint32_t img_b_version; /* Version of image in Slot B */ |
| 34 | + uint32_t img_a_crc; /* CRC32 of image in Slot A */ |
| 35 | + uint32_t img_b_crc; /* CRC32 of image in Slot B */ |
| 36 | + uint32_t log_head; /* Index of next log entry to write */ |
| 37 | + uint32_t boot_count; /* Total boot count since factory */ |
| 38 | + uint32_t reserved[7]; /* Reserved for future use */ |
| 39 | + uint32_t crc32; /* CRC32 of this structure (excluding this field) */ |
| 40 | +} eos_bootctl_t; |
| 41 | + |
| 42 | +#define EOS_BOOTCTL_VERSION 1 |
| 43 | + |
| 44 | +/* ---------------- Boot Control API ---------------- */ |
| 45 | + |
| 46 | +/** |
| 47 | + * @brief Load the boot control block from flash. |
| 48 | + * Reads the primary copy; falls back to the backup copy if the primary |
| 49 | + * is corrupt. |
| 50 | + * @param bctl Pointer to structure to populate. |
| 51 | + * @return EOS_OK on success, EOS_ERR_CRC if both copies are corrupt. |
| 52 | + */ |
| 53 | +int eos_bootctl_load(eos_bootctl_t *bctl); |
| 54 | + |
| 55 | +/** |
| 56 | + * @brief Save the boot control block to flash. |
| 57 | + * Writes both primary and backup copies atomically with CRC. |
| 58 | + * @param bctl Pointer to structure to save. |
| 59 | + * @return EOS_OK on success, EOS_ERR_FLASH on write failure. |
| 60 | + */ |
| 61 | +int eos_bootctl_save(eos_bootctl_t *bctl); |
| 62 | + |
| 63 | +/** |
| 64 | + * @brief Initialize a fresh boot control block with defaults. |
| 65 | + * Used on first boot or after factory reset. |
| 66 | + * @param bctl Pointer to structure to initialize. |
| 67 | + */ |
| 68 | +void eos_bootctl_init_defaults(eos_bootctl_t *bctl); |
| 69 | + |
| 70 | +/** |
| 71 | + * @brief Validate the CRC of a boot control block. |
| 72 | + * @param bctl Pointer to structure to validate. |
| 73 | + * @return true if CRC matches, false otherwise. |
| 74 | + */ |
| 75 | +bool eos_bootctl_validate(const eos_bootctl_t *bctl); |
| 76 | + |
| 77 | +/** |
| 78 | + * @brief Increment the boot attempt counter and save. |
| 79 | + * @param bctl Pointer to active boot control block. |
| 80 | + * @return EOS_OK on success. |
| 81 | + */ |
| 82 | +int eos_bootctl_increment_attempts(eos_bootctl_t *bctl); |
| 83 | + |
| 84 | +/** |
| 85 | + * @brief Reset boot attempt counter to zero and save. |
| 86 | + * @param bctl Pointer to active boot control block. |
| 87 | + * @return EOS_OK on success. |
| 88 | + */ |
| 89 | +int eos_bootctl_reset_attempts(eos_bootctl_t *bctl); |
| 90 | + |
| 91 | +/** |
| 92 | + * @brief Set the pending slot for test boot. |
| 93 | + * @param bctl Pointer to active boot control block. |
| 94 | + * @param slot Target slot. |
| 95 | + * @return EOS_OK on success. |
| 96 | + */ |
| 97 | +int eos_bootctl_set_pending(eos_bootctl_t *bctl, eos_slot_t slot); |
| 98 | + |
| 99 | +/** |
| 100 | + * @brief Clear the pending slot after a failed verification. |
| 101 | + * @param bctl Pointer to active boot control block. |
| 102 | + * @return EOS_OK on success. |
| 103 | + */ |
| 104 | +int eos_bootctl_clear_pending(eos_bootctl_t *bctl); |
| 105 | + |
| 106 | +/** |
| 107 | + * @brief Confirm the active slot as known-good. |
| 108 | + * Clears the test boot flag and marks the active slot as confirmed. |
| 109 | + * @param bctl Pointer to active boot control block. |
| 110 | + * @return EOS_OK on success. |
| 111 | + */ |
| 112 | +int eos_bootctl_confirm(eos_bootctl_t *bctl); |
| 113 | + |
| 114 | +/** |
| 115 | + * @brief Set the force-recovery flag. |
| 116 | + * @param bctl Pointer to active boot control block. |
| 117 | + * @return EOS_OK on success. |
| 118 | + */ |
| 119 | +int eos_bootctl_request_recovery(eos_bootctl_t *bctl); |
| 120 | + |
| 121 | +/** |
| 122 | + * @brief Set the factory reset flag. |
| 123 | + * @param bctl Pointer to active boot control block. |
| 124 | + * @return EOS_OK on success. |
| 125 | + */ |
| 126 | +int eos_bootctl_request_factory_reset(eos_bootctl_t *bctl); |
| 127 | + |
| 128 | +/** |
| 129 | + * @brief Get the alternate slot. |
| 130 | + * @param slot Current slot. |
| 131 | + * @return The other slot (A→B, B→A), or EOS_SLOT_NONE. |
| 132 | + */ |
| 133 | +eos_slot_t eos_bootctl_other_slot(eos_slot_t slot); |
| 134 | + |
| 135 | +#ifdef __cplusplus |
| 136 | +} |
| 137 | +#endif |
| 138 | +#endif /* EOS_BOOTCTL_H */ |
0 commit comments