kfsw-services/src/boot-parameters/boot_table.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <stdint.h> | ||
| 3 | #include <string.h> | ||
| 4 | |||
| 5 | #include <zephyr/sys/util.h> | ||
| 6 | |||
| 7 | #include <kfsw/services/boot.h> | ||
| 8 | #if CONFIG_KFSW_BOOT_STORE | ||
| 9 | #include <kfsw/services/boot_budget.h> | ||
| 10 | #endif | ||
| 11 | #include <kfsw/services/parameter.h> | ||
| 12 | |||
| 13 | #if CONFIG_BOOTLOADER_MCUBOOT && CONFIG_MCUBOOT_IMG_MANAGER | ||
| 14 | #include <zephyr/dfu/mcuboot.h> | ||
| 15 | #endif | ||
| 16 | |||
| 17 | #define KFSW_BOOT_IMAGE_SIZE 40U | ||
| 18 | /* Seven repositories, each a label, eight hex digits and a dirty mark. */ | ||
| 19 | #define KFSW_BOOT_REVISIONS_SIZE KFSW_PARAM_STRING_MAX | ||
| 20 | |||
| 21 | BUILD_ASSERT(sizeof(KFSW_BUILD_REVISIONS) <= KFSW_BOOT_REVISIONS_SIZE, | ||
| 22 | "the revisions of every repository must fit the parameter"); | ||
| 23 | |||
| 24 | static char boot_image[KFSW_BOOT_IMAGE_SIZE]; | ||
| 25 | static char boot_revisions[KFSW_BOOT_REVISIONS_SIZE]; | ||
| 26 | #if CONFIG_KFSW_LASTWORDS | ||
| 27 | /* Retained reset note. All zero means no valid note was found. */ | ||
| 28 | static uint8_t boot_last_reason; | ||
| 29 | static uint32_t boot_last_detail; | ||
| 30 | static uint32_t boot_last_uptime_ms; | ||
| 31 | |||
| 32 | ✗ | static void sample_last_reason(void *value) | |
| 33 | { | ||
| 34 | ✗ | *(uint8_t *)value = (uint8_t)kfsw_boot_get_lastwords()->reason; | |
| 35 | ✗ | } | |
| 36 | |||
| 37 | ✗ | static void sample_last_detail(void *value) | |
| 38 | { | ||
| 39 | ✗ | *(uint32_t *)value = kfsw_boot_get_lastwords()->detail; | |
| 40 | ✗ | } | |
| 41 | |||
| 42 | ✗ | static void sample_last_uptime_ms(void *value) | |
| 43 | { | ||
| 44 | ✗ | *(uint32_t *)value = kfsw_boot_get_lastwords()->uptime_ms; | |
| 45 | ✗ | } | |
| 46 | #endif | ||
| 47 | static uint32_t boot_count; | ||
| 48 | static uint32_t boot_reset_cause; | ||
| 49 | static uint8_t boot_confirmed; | ||
| 50 | #if CONFIG_KFSW_BOOT_STORE | ||
| 51 | static uint8_t boot_attempts_left; | ||
| 52 | static uint8_t boot_attempts_max; | ||
| 53 | |||
| 54 | ✗ | static void sample_left(void *value) | |
| 55 | { | ||
| 56 | uint8_t maximum; | ||
| 57 | |||
| 58 | ✗ | kfsw_boot_budget_get(value, &maximum); | |
| 59 | ✗ | } | |
| 60 | |||
| 61 | ✗ | static void sample_max(void *value) | |
| 62 | { | ||
| 63 | uint8_t left; | ||
| 64 | |||
| 65 | ✗ | kfsw_boot_budget_get(&left, value); | |
| 66 | ✗ | } | |
| 67 | |||
| 68 | 4 | static int apply_left(const union kfsw_param_scalar *value) | |
| 69 | { | ||
| 70 | 4 | return kfsw_boot_budget_set_left(value->u8); | |
| 71 | } | ||
| 72 | |||
| 73 | 4 | static int apply_max(const union kfsw_param_scalar *value) | |
| 74 | { | ||
| 75 | 4 | return kfsw_boot_budget_set_max(value->u8); | |
| 76 | } | ||
| 77 | #endif | ||
| 78 | |||
| 79 | 2 | static void copy_text(void *value, const char *source, size_t capacity) | |
| 80 | { | ||
| 81 | 2 | size_t length = 0U; | |
| 82 | 2 | char *text = value; | |
| 83 | |||
| 84 | 38 | while ((length + 1U < capacity) && (source[length] != '\0')) { | |
| 85 | 36 | text[length] = source[length]; | |
| 86 | 36 | length++; | |
| 87 | } | ||
| 88 | 2 | text[length] = '\0'; | |
| 89 | 2 | } | |
| 90 | |||
| 91 | 2 | static void sample_image(void *value) | |
| 92 | { | ||
| 93 | 2 | copy_text(value, kfsw_boot_get_image_version(), KFSW_BOOT_IMAGE_SIZE); | |
| 94 | 2 | } | |
| 95 | |||
| 96 | ✗ | static void sample_revisions(void *value) | |
| 97 | { | ||
| 98 | ✗ | copy_text(value, kfsw_boot_get_revisions(), KFSW_BOOT_REVISIONS_SIZE); | |
| 99 | ✗ | } | |
| 100 | |||
| 101 | ✗ | static void sample_reset_cause(void *value) | |
| 102 | { | ||
| 103 | /* From the boot service; reading the platform register again would clear it. */ | ||
| 104 | ✗ | *(uint32_t *)value = kfsw_boot_get_reset_cause(); | |
| 105 | ✗ | } | |
| 106 | |||
| 107 | ✗ | static void sample_confirmed(void *value) | |
| 108 | { | ||
| 109 | #if CONFIG_BOOTLOADER_MCUBOOT && CONFIG_MCUBOOT_IMG_MANAGER | ||
| 110 | *(uint8_t *)value = boot_is_img_confirmed() ? 1U : 0U; | ||
| 111 | #else | ||
| 112 | ✗ | *(uint8_t *)value = 0U; | |
| 113 | #endif | ||
| 114 | ✗ | } | |
| 115 | |||
| 116 | /* | ||
| 117 | * Writing 1 confirms the running image; writing 0 does nothing. 0 is accepted | ||
| 118 | * because it is the default, and the sample callback reads it back as 0. | ||
| 119 | */ | ||
| 120 | 6 | static int validate_confirmed(const union kfsw_param_scalar *value) | |
| 121 | { | ||
| 122 | 6 | if (value->u8 > 1U) { | |
| 123 | ✗ | return -EINVAL; | |
| 124 | } | ||
| 125 | #if CONFIG_BOOTLOADER_MCUBOOT && CONFIG_MCUBOOT_IMG_MANAGER | ||
| 126 | return 0; | ||
| 127 | #else | ||
| 128 | /* Without a bootloader there is nothing to confirm, so a write of 1 is refused. */ | ||
| 129 | 6 | return (value->u8 == 0U) ? 0 : -ENOTSUP; | |
| 130 | #endif | ||
| 131 | } | ||
| 132 | |||
| 133 | /* Fallible, so a confirmation the bootloader refused fails the write. */ | ||
| 134 | ✗ | static int apply_confirmed(const union kfsw_param_scalar *value) | |
| 135 | { | ||
| 136 | #if CONFIG_BOOTLOADER_MCUBOOT && CONFIG_MCUBOOT_IMG_MANAGER | ||
| 137 | return (value->u8 == 1U) ? kfsw_boot_confirm_image() : 0; | ||
| 138 | #else | ||
| 139 | ARG_UNUSED(value); | ||
| 140 | ✗ | return 0; | |
| 141 | #endif | ||
| 142 | } | ||
| 143 | |||
| 144 | static uint32_t boot_attempts; | ||
| 145 | static uint8_t boot_revert_reason; | ||
| 146 | static uint8_t boot_trial_valid; | ||
| 147 | static uint8_t boot_trial_quarantined; | ||
| 148 | |||
| 149 | 2 | static void sample_attempts(void *value) | |
| 150 | { | ||
| 151 | struct kfsw_boot_diagnostics diagnostic; | ||
| 152 | |||
| 153 | 2 | kfsw_boot_get_diagnostics(&diagnostic); | |
| 154 | 2 | *(uint32_t *)value = diagnostic.attempts; | |
| 155 | 2 | } | |
| 156 | |||
| 157 | 2 | static void sample_revert_reason(void *value) | |
| 158 | { | ||
| 159 | struct kfsw_boot_diagnostics diagnostic; | ||
| 160 | |||
| 161 | 2 | kfsw_boot_get_diagnostics(&diagnostic); | |
| 162 | 2 | *(uint8_t *)value = diagnostic.revert_reason; | |
| 163 | 2 | } | |
| 164 | |||
| 165 | 2 | static void sample_trial_valid(void *value) | |
| 166 | { | ||
| 167 | struct kfsw_boot_diagnostics diagnostic; | ||
| 168 | |||
| 169 | 2 | kfsw_boot_get_diagnostics(&diagnostic); | |
| 170 | 2 | *(uint8_t *)value = diagnostic.valid ? 1U : 0U; | |
| 171 | 2 | } | |
| 172 | |||
| 173 | 4 | static void sample_trial_quarantined(void *value) | |
| 174 | { | ||
| 175 | struct kfsw_boot_diagnostics diagnostic; | ||
| 176 | |||
| 177 | 4 | kfsw_boot_get_diagnostics(&diagnostic); | |
| 178 | 4 | *(uint8_t *)value = diagnostic.quarantined; | |
| 179 | 4 | } | |
| 180 | |||
| 181 | static const struct kfsw_param_definition boot_param_definitions[] = { | ||
| 182 | #if CONFIG_KFSW_BOOT_STORE | ||
| 183 | { | ||
| 184 | .offset = 0x2bU, | ||
| 185 | .type = KFSW_PARAM_U8, | ||
| 186 | .flags = KFSW_PARAM_FLAG_PERSISTENT | KFSW_PARAM_FLAG_OWNER_STORE, | ||
| 187 | .name = "boot_attempts_left", | ||
| 188 | .description = "Attempts remaining; ground writes persist in early flash", | ||
| 189 | .value = &boot_attempts_left, | ||
| 190 | .default_value = {.u8 = CONFIG_KFSW_BOOT_ATTEMPTS_MAX}, | ||
| 191 | .apply = apply_left, | ||
| 192 | .sample = sample_left, | ||
| 193 | }, | ||
| 194 | { | ||
| 195 | .offset = 0x3cU, | ||
| 196 | .type = KFSW_PARAM_U8, | ||
| 197 | .flags = KFSW_PARAM_FLAG_PERSISTENT | KFSW_PARAM_FLAG_OWNER_STORE, | ||
| 198 | .name = "boot_attempts_max", | ||
| 199 | .description = "Boot attempt limit: 3, 5 or 10; raising never refills", | ||
| 200 | .value = &boot_attempts_max, | ||
| 201 | .default_value = {.u8 = CONFIG_KFSW_BOOT_ATTEMPTS_MAX}, | ||
| 202 | .apply = apply_max, | ||
| 203 | .sample = sample_max, | ||
| 204 | }, | ||
| 205 | #endif | ||
| 206 | { | ||
| 207 | .offset = 0x2cU, | ||
| 208 | .type = KFSW_PARAM_U32, | ||
| 209 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 210 | .name = "boot_attempts", | ||
| 211 | .description = "Persisted unconfirmed boots of this image; UINT32_MAX invalid", | ||
| 212 | .value = &boot_attempts, | ||
| 213 | .sample = sample_attempts, | ||
| 214 | }, | ||
| 215 | { | ||
| 216 | .offset = 0x29U, | ||
| 217 | .type = KFSW_PARAM_U8, | ||
| 218 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 219 | .name = "boot_revert_reason", | ||
| 220 | .description = "0 none, 1 missed confirmation, 2 replaced unknown, 255 invalid", | ||
| 221 | .value = &boot_revert_reason, | ||
| 222 | .sample = sample_revert_reason, | ||
| 223 | }, | ||
| 224 | { | ||
| 225 | .offset = 0x2aU, | ||
| 226 | .type = KFSW_PARAM_U8, | ||
| 227 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 228 | .name = "boot_trial_valid", | ||
| 229 | .description = "Trial diagnostics were recovered and saved successfully", | ||
| 230 | .value = &boot_trial_valid, | ||
| 231 | .sample = sample_trial_valid, | ||
| 232 | }, | ||
| 233 | { | ||
| 234 | .offset = 0x3dU, | ||
| 235 | .type = KFSW_PARAM_U8, | ||
| 236 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 237 | .name = "boot_trial_quarantined", | ||
| 238 | .description = "Rejected trial records moved to boot-trial.bad since startup", | ||
| 239 | .value = &boot_trial_quarantined, | ||
| 240 | .sample = sample_trial_quarantined, | ||
| 241 | }, | ||
| 242 | { | ||
| 243 | .offset = 0x00U, | ||
| 244 | .type = KFSW_PARAM_STRING, | ||
| 245 | .capacity = KFSW_BOOT_IMAGE_SIZE, | ||
| 246 | .flags = KFSW_PARAM_FLAG_READ_ONLY | KFSW_PARAM_FLAG_SYSTEM_INFO, | ||
| 247 | .name = "boot_image", | ||
| 248 | .description = "Version of the running image, from the build", | ||
| 249 | .value = boot_image, | ||
| 250 | .sample = sample_image, | ||
| 251 | }, | ||
| 252 | { | ||
| 253 | .offset = 0x40U, | ||
| 254 | .type = KFSW_PARAM_STRING, | ||
| 255 | .capacity = KFSW_BOOT_REVISIONS_SIZE, | ||
| 256 | .flags = KFSW_PARAM_FLAG_READ_ONLY | KFSW_PARAM_FLAG_SYSTEM_INFO, | ||
| 257 | .name = "boot_revisions", | ||
| 258 | .description = "Short revision of each repository compiled into this image", | ||
| 259 | .value = boot_revisions, | ||
| 260 | .sample = sample_revisions, | ||
| 261 | }, | ||
| 262 | { | ||
| 263 | .offset = 0x20U, | ||
| 264 | .type = KFSW_PARAM_U32, | ||
| 265 | /* Read-only and persistent: saved in the snapshot but not writable. */ | ||
| 266 | .flags = KFSW_PARAM_FLAG_READ_ONLY | KFSW_PARAM_FLAG_PERSISTENT, | ||
| 267 | .name = "boot_count", | ||
| 268 | .description = "Restarts recorded across the life of the node", | ||
| 269 | .value = &boot_count, | ||
| 270 | .default_value = {.u32 = 0U}, | ||
| 271 | }, | ||
| 272 | { | ||
| 273 | .offset = 0x24U, | ||
| 274 | .type = KFSW_PARAM_X32, | ||
| 275 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 276 | .name = "boot_reset_cause", | ||
| 277 | .description = "Cause latched at boot, before the flags were cleared", | ||
| 278 | .value = &boot_reset_cause, | ||
| 279 | .sample = sample_reset_cause, | ||
| 280 | }, | ||
| 281 | #if CONFIG_KFSW_LASTWORDS | ||
| 282 | { | ||
| 283 | .offset = 0x30U, | ||
| 284 | .type = KFSW_PARAM_U8, | ||
| 285 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 286 | .name = "last_reason", | ||
| 287 | .description = "0 quiet, 1 commanded, 2 brownout, 3 fatal, 4 starved, 5 unknown", | ||
| 288 | .value = &boot_last_reason, | ||
| 289 | .sample = sample_last_reason, | ||
| 290 | }, | ||
| 291 | { | ||
| 292 | .offset = 0x34U, | ||
| 293 | .type = KFSW_PARAM_X32, | ||
| 294 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 295 | .name = "last_detail", | ||
| 296 | .description = "Meaningful with the reason: the faulting address for a fatal", | ||
| 297 | .value = &boot_last_detail, | ||
| 298 | .sample = sample_last_detail, | ||
| 299 | }, | ||
| 300 | { | ||
| 301 | .offset = 0x38U, | ||
| 302 | .type = KFSW_PARAM_U32, | ||
| 303 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 304 | .name = "last_uptime_ms", | ||
| 305 | .unit = "ms", | ||
| 306 | .description = "How long the previous run had been up when it went away", | ||
| 307 | .value = &boot_last_uptime_ms, | ||
| 308 | .sample = sample_last_uptime_ms, | ||
| 309 | }, | ||
| 310 | #endif | ||
| 311 | { | ||
| 312 | .offset = 0x28U, | ||
| 313 | .type = KFSW_PARAM_U8, | ||
| 314 | .flags = KFSW_PARAM_FLAG_CONFIGURATION | KFSW_PARAM_FLAG_LIVE, | ||
| 315 | .name = "boot_confirmed", | ||
| 316 | .description = "Mark the running image good; can only be set, never cleared", | ||
| 317 | .value = &boot_confirmed, | ||
| 318 | .default_value = {.u8 = 0U}, | ||
| 319 | .validate = validate_confirmed, | ||
| 320 | .apply = apply_confirmed, | ||
| 321 | .sample = sample_confirmed, | ||
| 322 | }, | ||
| 323 | }; | ||
| 324 | |||
| 325 | KFSW_PARAM_TABLE_FITS_THE_LINK(boot_param_definitions); | ||
| 326 | |||
| 327 | const struct kfsw_param_definition_set kfsw_boot_param_definitions = { | ||
| 328 | .table = KFSW_BOOT_PARAM_TABLE_ID, | ||
| 329 | .name = KFSW_BOOT_PARAM_TABLE_NAME, | ||
| 330 | .description = "Running image, revisions, restarts", | ||
| 331 | .definitions = boot_param_definitions, | ||
| 332 | .count = ARRAY_SIZE(boot_param_definitions), | ||
| 333 | }; | ||
| 334 | |||
| 335 | ✗ | void kfsw_boot_count_restart(void) | |
| 336 | { | ||
| 337 | ✗ | if (boot_count < UINT32_MAX) { | |
| 338 | ✗ | boot_count++; | |
| 339 | } | ||
| 340 | ✗ | } | |
| 341 | |||
| 342 | ✗ | uint32_t kfsw_boot_get_count(void) | |
| 343 | { | ||
| 344 | ✗ | return boot_count; | |
| 345 | } | ||
| 346 |