|
| 1 | +/* boot_state.h |
| 2 | + * |
| 3 | + * Persistent anti-rollback reference and boot audit counters. |
| 4 | + * |
| 5 | + * Copyright (C) 2026 wolfSSL Inc. |
| 6 | + * |
| 7 | + * This file is part of wolfBoot. |
| 8 | + * |
| 9 | + * wolfBoot is free software; you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU General Public License as published by |
| 11 | + * the Free Software Foundation; either version 2 of the License, or |
| 12 | + * (at your option) any later version. |
| 13 | + * |
| 14 | + * wolfBoot is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU General Public License |
| 20 | + * along with this program; if not, write to the Free Software |
| 21 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 22 | + */ |
| 23 | + |
| 24 | +/* Anti-rollback state: anchor = highest version ever booted (trust on first |
| 25 | + * use), record = last booted. See the Anti-rollback section of Targets.md. */ |
| 26 | + |
| 27 | +#ifndef BOOT_STATE_H |
| 28 | +#define BOOT_STATE_H |
| 29 | + |
| 30 | +#include <stdint.h> |
| 31 | + |
| 32 | +#define BOOT_STATE_MAGIC 0x42535431UL /* "BST1" */ |
| 33 | +#define BOOT_STATE_VERSION 1 |
| 34 | + |
| 35 | +/* Result of loading the persisted state. */ |
| 36 | +#define BOOT_STATE_OK 0 /* loaded and trustworthy */ |
| 37 | +#define BOOT_STATE_UNPROVISIONED 1 /* nothing stored yet; not an error */ |
| 38 | +#define BOOT_STATE_CORRUPT (-1) /* stored but unusable -- fatal */ |
| 39 | +#define BOOT_STATE_UNAVAILABLE (-2) /* backend could not be reached */ |
| 40 | + |
| 41 | +/* Failure codes recorded in last_fail_code. */ |
| 42 | +#define BOOT_FAIL_NONE 0 |
| 43 | +#define BOOT_FAIL_NO_IMAGE 1 |
| 44 | +#define BOOT_FAIL_VERIFY_PRIMARY 2 |
| 45 | +#define BOOT_FAIL_VERIFY_BOTH 3 |
| 46 | +#define BOOT_FAIL_ROLLBACK 4 |
| 47 | + |
| 48 | +/* On-storage layout. Packed and explicitly sized so the representation does |
| 49 | + * not shift between the bootloader and any provisioning tool. */ |
| 50 | +struct boot_state { |
| 51 | + uint32_t magic; |
| 52 | + uint32_t anchor; |
| 53 | + uint32_t record; |
| 54 | + uint32_t rollback_count; |
| 55 | + uint32_t last_rollback_from; |
| 56 | + uint32_t last_rollback_to; |
| 57 | + uint32_t boot_fail_count; |
| 58 | + uint32_t last_fail_code; |
| 59 | + uint16_t struct_version; |
| 60 | + uint16_t reserved; |
| 61 | + uint32_t crc; |
| 62 | +} __attribute__((packed)); |
| 63 | + |
| 64 | +/* Backend, implemented per platform. v1 uses TPM NV (src/boot_state_tpm.c). */ |
| 65 | +int boot_state_backend_read(uint8_t *buf, uint32_t len); |
| 66 | +int boot_state_backend_write(const uint8_t *buf, uint32_t len); |
| 67 | +/* Write-locks the record until the next TPM reset, so the booted OS cannot |
| 68 | + * rewrite it. */ |
| 69 | +int boot_state_backend_lock(void); |
| 70 | + |
| 71 | +/* Load the persisted state. Returns one of the BOOT_STATE_* codes above. */ |
| 72 | +int boot_state_load(struct boot_state *st); |
| 73 | + |
| 74 | +/* Called once an image has verified, immediately before handing off. |
| 75 | + * Applies the rollback check, updates anchor and record, and persists. */ |
| 76 | +void boot_state_on_boot(uint32_t booted_version); |
| 77 | +/* Lock the stored state against the OS without recording a boot (golden |
| 78 | + * slot). Halts on failure with ANTI_ROLLBACK_HALT_ON_FAILURE. */ |
| 79 | +void boot_state_lock(void); |
| 80 | + |
| 81 | +/* Called when the boot policy has decided no image may be booted. Records the |
| 82 | + * failure so the evidence survives the power cycle. */ |
| 83 | +void boot_state_on_failure(uint32_t fail_code); |
| 84 | + |
| 85 | +#endif /* BOOT_STATE_H */ |
0 commit comments