kfsw-services/src/boot_budget.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | |||
| 3 | #include <zephyr/drivers/hwinfo.h> | ||
| 4 | #include <zephyr/init.h> | ||
| 5 | #include <zephyr/kernel.h> | ||
| 6 | #include <zephyr/sys/reboot.h> | ||
| 7 | |||
| 8 | #include <kfsw/platform/boot_store.h> | ||
| 9 | #include <kfsw/services/boot.h> | ||
| 10 | #include <kfsw/services/boot_budget.h> | ||
| 11 | #if CONFIG_KFSW_GOLDEN | ||
| 12 | #include <kfsw/services/golden.h> | ||
| 13 | #endif | ||
| 14 | #if CONFIG_KFSW_HEALTH | ||
| 15 | #include <kfsw/services/health.h> | ||
| 16 | #endif | ||
| 17 | #define KFSW_LOG_MODULE KFSW_LOG_MODULE_BOOT | ||
| 18 | #include <kfsw/services/log.h> | ||
| 19 | |||
| 20 | BUILD_ASSERT(CONFIG_KFSW_BOOT_ATTEMPTS_MAX == 3 || CONFIG_KFSW_BOOT_ATTEMPTS_MAX == 5 || | ||
| 21 | CONFIG_KFSW_BOOT_ATTEMPTS_MAX == 10, | ||
| 22 | "boot attempt limit must be 3, 5 or 10"); | ||
| 23 | |||
| 24 | static uint8_t attempts_left = UINT8_MAX; | ||
| 25 | static uint8_t attempts_max = UINT8_MAX; | ||
| 26 | static bool attempts_spent; | ||
| 27 | /* Parameter writes and FWU installation share the same durable record. */ | ||
| 28 | K_MUTEX_DEFINE(budget_lock); | ||
| 29 | |||
| 30 | 108 | static bool valid_max(uint8_t maximum) | |
| 31 | { | ||
| 32 | 108 | return maximum == 3U || maximum == 5U || maximum == 10U; | |
| 33 | } | ||
| 34 | |||
| 35 | 74 | static int load_budget(void) | |
| 36 | { | ||
| 37 | 74 | int result = kfsw_boot_store_open(); | |
| 38 | uint8_t left; | ||
| 39 | uint8_t maximum; | ||
| 40 | |||
| 41 | 74 | if (result != 0) { | |
| 42 | ✗ | return result; | |
| 43 | } | ||
| 44 | 74 | result = kfsw_boot_store_read(&left, &maximum); | |
| 45 | 74 | if ((result == 0) && (!valid_max(maximum) || (left > maximum))) { | |
| 46 | 6 | result = -EBADMSG; | |
| 47 | } | ||
| 48 | 74 | if (result == -EBADMSG) { | |
| 49 | /* Refusing a damaged record would switch recovery off for the rest | ||
| 50 | of the mission, and the ground and FWU could not rewrite it. */ | ||
| 51 | 6 | kfsw_log_warning("Boot budget record damaged; restarting from %u", | |
| 52 | CONFIG_KFSW_BOOT_ATTEMPTS_MAX); | ||
| 53 | } | ||
| 54 | 74 | if ((result == -ENOENT) || (result == -EBADMSG)) { | |
| 55 | 20 | left = CONFIG_KFSW_BOOT_ATTEMPTS_MAX; | |
| 56 | 20 | maximum = CONFIG_KFSW_BOOT_ATTEMPTS_MAX; | |
| 57 | 54 | } else if (result != 0) { | |
| 58 | 4 | return result; | |
| 59 | } | ||
| 60 | 70 | attempts_left = left; | |
| 61 | 70 | attempts_max = maximum; | |
| 62 | 70 | return 0; | |
| 63 | } | ||
| 64 | |||
| 65 | 120 | static int save_budget(uint8_t left, uint8_t maximum) | |
| 66 | { | ||
| 67 | 120 | int result = kfsw_boot_store_write(left, maximum); | |
| 68 | |||
| 69 | 120 | if (result == 0) { | |
| 70 | 116 | attempts_left = left; | |
| 71 | 116 | attempts_max = maximum; | |
| 72 | } | ||
| 73 | 120 | return result; | |
| 74 | } | ||
| 75 | |||
| 76 | 14 | bool kfsw_boot_budget_commanded(uint32_t cause, bool valid_commanded_note) | |
| 77 | { | ||
| 78 | /* PIN can accompany a software reset on STM32. Any stronger cause wins. */ | ||
| 79 | 14 | return valid_commanded_note && (cause & RESET_SOFTWARE) != 0U && | |
| 80 | (cause & ~(RESET_SOFTWARE | RESET_PIN)) == 0U; | ||
| 81 | } | ||
| 82 | |||
| 83 | 72 | int kfsw_boot_budget_start(bool commanded) | |
| 84 | { | ||
| 85 | int result; | ||
| 86 | |||
| 87 | 72 | k_mutex_lock(&budget_lock, K_FOREVER); | |
| 88 | 72 | attempts_left = attempts_max = UINT8_MAX; | |
| 89 | 72 | result = load_budget(); | |
| 90 | /* A budget of three is three startups, so the startup that spends the | ||
| 91 | last one still runs the application; the next one finds nothing left. */ | ||
| 92 | 72 | attempts_spent = result == 0 && attempts_left == 0U; | |
| 93 | 72 | if (result == 0 && !commanded && attempts_left > 0U) { | |
| 94 | /* Keep the in-memory decision even when the flash write fails. */ | ||
| 95 | 60 | attempts_left--; | |
| 96 | 60 | result = kfsw_boot_store_write(attempts_left, attempts_max); | |
| 97 | } | ||
| 98 | 72 | k_mutex_unlock(&budget_lock); | |
| 99 | 72 | return result; | |
| 100 | } | ||
| 101 | |||
| 102 | 18 | bool kfsw_boot_budget_spent(void) | |
| 103 | { | ||
| 104 | 18 | return attempts_spent; | |
| 105 | } | ||
| 106 | |||
| 107 | 78 | void kfsw_boot_budget_get(uint8_t *left, uint8_t *maximum) | |
| 108 | { | ||
| 109 | 78 | k_mutex_lock(&budget_lock, K_FOREVER); | |
| 110 | 78 | if (attempts_max == UINT8_MAX) { | |
| 111 | /* Recovery never consumes, so this is the first read of the | ||
| 112 | record in that image, and the ground is asking for it. */ | ||
| 113 | 2 | (void)load_budget(); | |
| 114 | } | ||
| 115 | 78 | *left = attempts_left; | |
| 116 | 78 | *maximum = attempts_max; | |
| 117 | 78 | k_mutex_unlock(&budget_lock); | |
| 118 | 78 | } | |
| 119 | |||
| 120 | 18 | int kfsw_boot_budget_set_left(uint8_t left) | |
| 121 | { | ||
| 122 | int result; | ||
| 123 | |||
| 124 | 18 | k_mutex_lock(&budget_lock, K_FOREVER); | |
| 125 | 18 | result = attempts_max == UINT8_MAX ? load_budget() : 0; | |
| 126 | 18 | if (result == 0) { | |
| 127 | 18 | result = left <= attempts_max ? save_budget(left, attempts_max) : -ERANGE; | |
| 128 | } | ||
| 129 | 18 | k_mutex_unlock(&budget_lock); | |
| 130 | 18 | return result; | |
| 131 | } | ||
| 132 | |||
| 133 | 52 | int kfsw_boot_budget_set_max(uint8_t maximum) | |
| 134 | { | ||
| 135 | int result; | ||
| 136 | |||
| 137 | 52 | if (!valid_max(maximum)) { | |
| 138 | 6 | return -EINVAL; | |
| 139 | } | ||
| 140 | 46 | k_mutex_lock(&budget_lock, K_FOREVER); | |
| 141 | 46 | result = attempts_max == UINT8_MAX ? load_budget() : 0; | |
| 142 | 46 | if (result == 0) { | |
| 143 | 46 | result = save_budget(MIN(attempts_left, maximum), maximum); | |
| 144 | } | ||
| 145 | 46 | k_mutex_unlock(&budget_lock); | |
| 146 | 46 | return result; | |
| 147 | } | ||
| 148 | |||
| 149 | 52 | int kfsw_boot_budget_refill(void) | |
| 150 | { | ||
| 151 | int result; | ||
| 152 | |||
| 153 | 52 | k_mutex_lock(&budget_lock, K_FOREVER); | |
| 154 | 52 | result = attempts_max == UINT8_MAX ? load_budget() : 0; | |
| 155 | 52 | if (result == 0) { | |
| 156 | 52 | result = save_budget(attempts_max, attempts_max); | |
| 157 | } | ||
| 158 | 52 | k_mutex_unlock(&budget_lock); | |
| 159 | 52 | return result; | |
| 160 | } | ||
| 161 | |||
| 162 | 8 | int kfsw_boot_budget_mark_stable(void) | |
| 163 | { | ||
| 164 | int result; | ||
| 165 | |||
| 166 | 8 | k_mutex_lock(&budget_lock, K_FOREVER); | |
| 167 | 8 | result = attempts_max == UINT8_MAX ? load_budget() : 0; | |
| 168 | 8 | if (result == 0 && attempts_left < attempts_max) { | |
| 169 | 6 | result = save_budget(attempts_max, attempts_max); | |
| 170 | } | ||
| 171 | 8 | k_mutex_unlock(&budget_lock); | |
| 172 | 8 | return result; | |
| 173 | } | ||
| 174 | |||
| 175 | #if CONFIG_KFSW_BOOT_BUDGET | ||
| 176 | /* GCOVR_EXCL_START | ||
| 177 | * A SYS_INIT handler that runs before any test and reboots on two of its | ||
| 178 | * paths, so unit coverage cannot reach it. What it decides is proved on the | ||
| 179 | * bench by tests/hil/golden-auto-smoke.sh. | ||
| 180 | */ | ||
| 181 | #if CONFIG_KFSW_GOLDEN && CONFIG_KFSW_LASTWORDS | ||
| 182 | /* This startup took the note that says why the last run ended. Leave it again | ||
| 183 | for the recovery image, which otherwise reports a plain software reset. */ | ||
| 184 | − | static void leave_note_for_golden(void) | |
| 185 | { | ||
| 186 | − | const struct kfsw_lastwords *note = kfsw_boot_get_lastwords(); | |
| 187 | |||
| 188 | − | if (note->reason != KFSW_LASTWORDS_NONE) { | |
| 189 | − | kfsw_lastwords_write(note->reason, note->detail, note->uptime_ms, note->boot_count); | |
| 190 | } | ||
| 191 | − | } | |
| 192 | #endif | ||
| 193 | |||
| 194 | #if CONFIG_KFSW_BOOT_BUDGET_STABLE_S > 0 | ||
| 195 | static void stable_check(struct k_work *work); | ||
| 196 | static K_WORK_DELAYABLE_DEFINE(stable_work, stable_check); | ||
| 197 | #if CONFIG_KFSW_HEALTH | ||
| 198 | static uint32_t faults_seen; | ||
| 199 | #endif | ||
| 200 | |||
| 201 | /* Supervision that never started, is faulted now, or faulted at any time in | ||
| 202 | the window leaves the run unproven until a whole window passes cleanly. */ | ||
| 203 | − | static bool window_was_healthy(void) | |
| 204 | { | ||
| 205 | #if CONFIG_KFSW_HEALTH | ||
| 206 | struct kfsw_health_status status = {0}; | ||
| 207 | bool healthy = kfsw_health_get_status(&status) == 0 && status.state == KFSW_HEALTH_OK && | ||
| 208 | status.faults == faults_seen; | ||
| 209 | |||
| 210 | faults_seen = status.faults; | ||
| 211 | return healthy; | ||
| 212 | #else | ||
| 213 | − | return true; | |
| 214 | #endif | ||
| 215 | } | ||
| 216 | |||
| 217 | − | static void stable_check(struct k_work *work) | |
| 218 | { | ||
| 219 | uint8_t left; | ||
| 220 | uint8_t maximum; | ||
| 221 | − | int result = -EAGAIN; | |
| 222 | |||
| 223 | ARG_UNUSED(work); | ||
| 224 | − | if (window_was_healthy()) { | |
| 225 | − | result = kfsw_boot_budget_mark_stable(); | |
| 226 | } | ||
| 227 | − | if (result != 0) { | |
| 228 | − | if (result != -EAGAIN) { | |
| 229 | − | kfsw_log_warning("Boot budget not refilled after a stable run: %d", result); | |
| 230 | } | ||
| 231 | − | (void)k_work_schedule(&stable_work, K_SECONDS(CONFIG_KFSW_BOOT_BUDGET_STABLE_S)); | |
| 232 | − | return; | |
| 233 | } | ||
| 234 | − | kfsw_boot_budget_get(&left, &maximum); | |
| 235 | − | kfsw_log_info("Stable for %d s; boot budget left=%u max=%u", | |
| 236 | CONFIG_KFSW_BOOT_BUDGET_STABLE_S, left, maximum); | ||
| 237 | } | ||
| 238 | #endif | ||
| 239 | |||
| 240 | − | static int early_budget(void) | |
| 241 | { | ||
| 242 | − | bool commanded = false; | |
| 243 | int result; | ||
| 244 | |||
| 245 | #if CONFIG_KFSW_LASTWORDS && !CONFIG_ARCH_POSIX | ||
| 246 | commanded = kfsw_boot_budget_commanded(kfsw_boot_get_reset_cause(), | ||
| 247 | kfsw_boot_get_lastwords()->reason == | ||
| 248 | KFSW_LASTWORDS_COMMANDED); | ||
| 249 | #endif | ||
| 250 | − | result = kfsw_boot_budget_start(commanded); | |
| 251 | − | if (result != 0) { | |
| 252 | − | kfsw_log_warning("Boot budget store unavailable: %d; startup continues", result); | |
| 253 | } | ||
| 254 | − | kfsw_log_info("Boot budget: left=%u max=%u commanded=%u", attempts_left, attempts_max, | |
| 255 | commanded); | ||
| 256 | #if CONFIG_KFSW_BOOT_BUDGET_STABLE_S > 0 | ||
| 257 | − | (void)k_work_schedule(&stable_work, K_SECONDS(CONFIG_KFSW_BOOT_BUDGET_STABLE_S)); | |
| 258 | #endif | ||
| 259 | − | if (kfsw_boot_budget_spent()) { | |
| 260 | /* left=0 is printed by both the startup that spends the last attempt | ||
| 261 | and the one after it. Only this one acts, so say which it is. */ | ||
| 262 | − | kfsw_log_warning("Boot attempts exhausted; recovering"); | |
| 263 | #if CONFIG_KFSW_GOLDEN | ||
| 264 | enum kfsw_golden_outcome outcome; | ||
| 265 | |||
| 266 | − | result = kfsw_golden_check(&outcome); | |
| 267 | − | if (result == -EBUSY) { | |
| 268 | − | kfsw_log_warning("Recovery deferred to a pending revert; restarting"); | |
| 269 | − | sys_reboot(SYS_REBOOT_COLD); | |
| 270 | } | ||
| 271 | − | if (result == 0 && outcome == KFSW_GOLDEN_INSTALLED) { | |
| 272 | − | kfsw_log_warning("Recovery image installed; restarting into it"); | |
| 273 | #if CONFIG_KFSW_LASTWORDS | ||
| 274 | − | leave_note_for_golden(); | |
| 275 | #endif | ||
| 276 | − | sys_reboot(SYS_REBOOT_COLD); | |
| 277 | } | ||
| 278 | − | if (result != 0) { | |
| 279 | − | kfsw_log_warning("Golden unavailable: %d; startup continues", result); | |
| 280 | } | ||
| 281 | #else | ||
| 282 | − | kfsw_log_warning("Boot budget exhausted; no Golden support; startup continues"); | |
| 283 | #endif | ||
| 284 | } | ||
| 285 | − | return 0; | |
| 286 | } | ||
| 287 | |||
| 288 | SYS_INIT(early_budget, POST_KERNEL, 52); | ||
| 289 | /* GCOVR_EXCL_STOP */ | ||
| 290 | #endif | ||
| 291 | |||
| 292 | #if CONFIG_KFSW_GOLDEN_IMAGE | ||
| 293 | /* GCOVR_EXCL_START | ||
| 294 | * Compiled only into the recovery image, which the unit suites do not build. | ||
| 295 | * tests/boot-budget-smoke.sh proves it in the hosted bootloader model. | ||
| 296 | */ | ||
| 297 | /* Recovery spends no attempts and compiles none of the startup above, so | ||
| 298 | nothing in this image would return the record to a usable count. The install | ||
| 299 | that brought recovery in does refill it, but that write may fail without | ||
| 300 | failing the install, and no later path retries it. This is that retry, and | ||
| 301 | it puts the count among the first things recovery sets. | ||
| 302 | */ | ||
| 303 | static int golden_budget(void) | ||
| 304 | { | ||
| 305 | int result = kfsw_boot_budget_refill(); | ||
| 306 | |||
| 307 | if (result != 0) { | ||
| 308 | kfsw_log_warning("Boot budget not refilled in recovery: %d", result); | ||
| 309 | } | ||
| 310 | return 0; | ||
| 311 | } | ||
| 312 | |||
| 313 | SYS_INIT(golden_budget, POST_KERNEL, 52); | ||
| 314 | /* GCOVR_EXCL_STOP */ | ||
| 315 | #endif | ||
| 316 |