aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / boot / slot_format.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* slot_format.h -- AIENOS native image slots: the on-disk formats a loader |
| 18 | * reads to choose which native image boots, and a kernel writes to install, |
| 19 | * update and roll back WITHOUT Linux (native v1 plan P11, gate N7). |
| 20 | * |
| 21 | * STATUS (docs/NATIVE_IMAGE_SLOTS.md): format v1. Read by SLOTLDR.EFI |
| 22 | * (native/boot/slot_loader.c, P11 cut 2a, owner decision 2026-10-12), a |
| 23 | * separate chain loader in FRONT of the unchanged stub + kernel image |
| 24 | * (native/boot/efi_main.c is not modified). The loader writes the control |
| 25 | * record only to consume a candidate try. The service kernel |
| 26 | * (native/kernel/svc/slot_service.c, cut 2b) promotes a healthy candidate and |
| 27 | * abandons an exhausted or invalid one over its own NVMe path, told which slot |
| 28 | * it is by the loader's boot record (below). Staging is the cut 3 updater |
| 29 | * (slot_update.h, inbox partition type below). This header, |
| 30 | * slot_select.c and the host tool native/kernel/tools/ck_slot_tool.c pin |
| 31 | * the format and the decision rules (mutation-tested in make slot-test). |
| 32 | * |
| 33 | * Accepted design this follows: ADR 0006 (transactional A/B slots, health |
| 34 | * milestones before promotion) and TRUST-1 Q19 (A = known-good, B = |
| 35 | * candidate as ROLES; manifest fields; "write inactive, verify, boot once, |
| 36 | * health, promote, retain rollback; bounded attempts, no boot loop, last-good |
| 37 | * never erased by a candidate; dual-invalid = recovery"). |
| 38 | * |
| 39 | * Where it lives: its own GPT partition with the type GUID below, NOT inside |
| 40 | * the AIENOS Store partition (dev/disk_layout.h is untouched). In 4096-byte |
| 41 | * units from the partition's first LBA: |
| 42 | * |
| 43 | * unit 0..3 control record, torn-write safe (native/store/torn_slot.c |
| 44 | * protocol: body A, commit A, body B, commit B) |
| 45 | * unit 4 .. slot A: manifest unit, then the image bytes |
| 46 | * unit 4+CAP .. slot B: manifest unit, then the image bytes |
| 47 | * CAP = (U - 4) / 2 units, U = whole units in the partition. The image of |
| 48 | * a slot occupies at most (CAP - 1) * 4096 bytes. Equal halves, so the |
| 49 | * loader needs no stored geometry: the partition size is the geometry. |
| 50 | * |
| 51 | * All integers little endian. Reserved bytes are zero and are checked. |
| 52 | * |
| 53 | * The MANIFEST (one unit) is signed by the Owner Root Ed25519 key (TRUST-1 |
| 54 | * Q13; file format tools/ck_owner_gen.c "owner_root_ed25519 <64 hex>"): the |
| 55 | * first CK_SLOT_TBS_BYTES bytes are the signed message. The signature covers |
| 56 | * the image through image_sha512; the loader recomputes SHA-512 over exactly |
| 57 | * image_len bytes of the slot. The kernel is linked into the image |
| 58 | * (payload_kind 1 = the BOOTAA64.EFI PE built by native/kernel), so |
| 59 | * kernel_sha512 MUST equal image_sha512 in v1; the field exists so a future |
| 60 | * split payload keeps the layout. |
| 61 | * |
| 62 | * The CONTROL record (one unit, inside the torn-slot region) is NOT signed: |
| 63 | * it is operational state written by the kernel (stage, promote, abandon) and |
| 64 | * by the loader to decrement a candidate's remaining tries before boot. |
| 65 | * Torn-slot commit digests detect accidental corruption, not replay or |
| 66 | * forgery. A missing or corrupt control record requires recovery; it must |
| 67 | * never reset the effective floor to zero. |
| 68 | * |
| 69 | * generation_floor is only a floor relative to the currently read disk |
| 70 | * control record. Promotion raises this value, but offline disk replay can |
| 71 | * restore an older record and signed image. Hardware-backed, independently |
| 72 | * authenticated monotonic persistence is required before treating the |
| 73 | * floor as an anti-rollback security boundary. */ |
| 74 | #ifndef AIENOS_CK_SLOT_FORMAT_H |
| 75 | #define AIENOS_CK_SLOT_FORMAT_H |
| 76 | #include <stdint.h> |
| 77 | #include <stddef.h> |
| 78 | |
| 79 | #define CK_SLOT_UNIT 4096u |
| 80 | #define CK_SLOT_CONTROL_UNITS 4u /* torn-slot region (TS_UNITS) */ |
| 81 | #define CK_SLOT_FIRST_UNIT 4u /* slot A starts here */ |
| 82 | #define CK_SLOT_COUNT 2u |
| 83 | #define CK_SLOT_NONE 0xFFu |
| 84 | #define CK_SLOT_MAX_TRIES 3u /* bounded candidate boot attempts */ |
| 85 | #define CK_SLOT_MIN_CAP_UNITS 2u /* manifest + at least one image unit */ |
| 86 | |
| 87 | /* Geometry from the partition size alone (U = whole 4096-byte units in the |
| 88 | * partition): CAP = (U - 4) / 2 units per slot; slot s starts at unit |
| 89 | * 4 + s * CAP (its manifest), its image at the next unit, at most |
| 90 | * (CAP - 1) * 4096 bytes. Shared by SLOTLDR.EFI and ck_slot_tool. */ |
| 91 | static inline uint64_t ck_slot_cap_units(uint64_t part_units) |
| 92 | { |
| 93 | return part_units < CK_SLOT_FIRST_UNIT ? 0 : (part_units - CK_SLOT_FIRST_UNIT) / CK_SLOT_COUNT; |
| 94 | } |
| 95 | static inline uint64_t ck_slot_manifest_unit(unsigned slot, uint64_t cap_units) |
| 96 | { |
| 97 | return CK_SLOT_FIRST_UNIT + (uint64_t)slot * cap_units; |
| 98 | } |
| 99 | |
| 100 | /* 8B1096CD-7928-4E08-B5E4-C19B6B532760: AIENOS image slots partition type. */ |
| 101 | #define CK_SLOT_PART_TYPE_TEXT "8B1096CD-7928-4E08-B5E4-C19B6B532760" |
| 102 | #define CK_SLOT_PART_TYPE_BYTES \ |
| 103 | {0xcd, 0x96, 0x10, 0x8b, 0x28, 0x79, 0x08, 0x4e, 0xb5, 0xe4, 0xc1, 0x9b, 0x6b, 0x53, 0x27, 0x60} |
| 104 | |
| 105 | /* 58D9C90E-497A-4848-95DD-EB7E22B0CE96: AIENOS update inbox partition type (P11 |
| 106 | * cut 3, native/boot/slot_update.h). Same shape as ONE slot: unit 0 is the |
| 107 | * signed manifest, the image starts at unit 1. The desk tool writes it |
| 108 | * (ck_slot_tool inbox-image); the kernel only reads it. */ |
| 109 | #define CK_SLOT_INBOX_TYPE_TEXT "58D9C90E-497A-4848-95DD-EB7E22B0CE96" |
| 110 | #define CK_SLOT_INBOX_TYPE_BYTES \ |
| 111 | {0x0e, 0xc9, 0xd9, 0x58, 0x7a, 0x49, 0x48, 0x48, 0x95, 0xdd, 0xeb, 0x7e, 0x22, 0xb0, 0xce, 0x96} |
| 112 | |
| 113 | /* ---- manifest ------------------------------------------------------------ */ |
| 114 | #define CK_SLOT_MANIFEST_MAGIC "AIENSLT1" |
| 115 | #define CK_SLOT_MANIFEST_VERSION 1u |
| 116 | #define CK_SLOT_PAYLOAD_BOOTAA64 1u /* stub + kernel in one PE image */ |
| 117 | #define CK_SLOT_LOADER_ABI 1u |
| 118 | #define CK_SLOT_KERNEL_ABI 3u /* CHANDOF3, BOOT_HANDOFF_CONTRACT.md */ |
| 119 | #define CK_SLOT_TBS_BYTES 448u /* signed message = bytes [0, 448) */ |
| 120 | #define CK_SLOT_SIG_OFFSET 448u |
| 121 | #define CK_SLOT_COMMIT_CHARS 40u |
| 122 | #define CK_SLOT_PROVENANCE_CHARS 64u |
| 123 | |
| 124 | struct ck_slot_manifest { |
| 125 | uint8_t magic[8]; /* 0 "AIENSLT1" */ |
| 126 | uint32_t version; /* 8 1 */ |
| 127 | uint32_t payload_kind; /* 12 1 */ |
| 128 | uint32_t loader_abi; /* 16 1 */ |
| 129 | uint32_t kernel_abi; /* 20 3 */ |
| 130 | uint64_t generation; /* 24 strictly increasing per release */ |
| 131 | uint64_t policy_generation; /* 32 floor this image raises on promotion (<= generation) */ |
| 132 | uint64_t min_rollback_generation; /* 40 oldest generation this image may be rolled back to (<= generation) */ |
| 133 | uint64_t image_len; /* 48 bytes of the image, > 0 */ |
| 134 | uint8_t image_sha512[64]; /* 56 */ |
| 135 | uint8_t kernel_sha512[64]; /* 120 == image_sha512 for payload_kind 1 */ |
| 136 | uint8_t signer_id[32]; /* 184 SHA-256 of the signer's Ed25519 public key */ |
| 137 | uint8_t commit[40]; /* 216 lower-case hex of the source commit, no terminator */ |
| 138 | uint8_t provenance[64]; /* 256 printable ASCII, zero padded */ |
| 139 | uint64_t timestamp; /* 320 unix seconds, > 0 */ |
| 140 | uint32_t flags; /* 328 0 */ |
| 141 | uint32_t reserved0; /* 332 0 */ |
| 142 | uint8_t reserved1[112]; /* 336 zero */ |
| 143 | uint8_t signature[64]; /* 448 Ed25519 over bytes [0, 448) */ |
| 144 | uint8_t pad[CK_SLOT_UNIT - 512u]; /* 512 zero */ |
| 145 | }; |
| 146 | _Static_assert(sizeof(struct ck_slot_manifest) == CK_SLOT_UNIT, "ck_slot_manifest size"); |
| 147 | _Static_assert(offsetof(struct ck_slot_manifest, image_sha512) == 56, "manifest image_sha512"); |
| 148 | _Static_assert(offsetof(struct ck_slot_manifest, signer_id) == 184, "manifest signer_id"); |
| 149 | _Static_assert(offsetof(struct ck_slot_manifest, timestamp) == 320, "manifest timestamp"); |
| 150 | _Static_assert(offsetof(struct ck_slot_manifest, signature) == CK_SLOT_SIG_OFFSET, "manifest signature"); |
| 151 | |
| 152 | /* ---- control record ------------------------------------------------------ */ |
| 153 | #define CK_SLOT_CONTROL_MAGIC "AIENSLC1" |
| 154 | #define CK_SLOT_CONTROL_VERSION 1u |
| 155 | #define CK_SLOT_CTL_FLAG_CANDIDATE_FAILED 1u /* the last candidate was abandoned */ |
| 156 | |
| 157 | struct ck_slot_control { |
| 158 | uint8_t magic[8]; /* 0 "AIENSLC1" */ |
| 159 | uint32_t version; /* 8 1 */ |
| 160 | uint8_t active_slot; /* 12 0 or 1: the known-good slot */ |
| 161 | uint8_t candidate_slot; /* 13 0, 1 or CK_SLOT_NONE */ |
| 162 | uint8_t tries_remaining; /* 14 0..CK_SLOT_MAX_TRIES; 0 when no candidate */ |
| 163 | uint8_t flags; /* 15 */ |
| 164 | uint64_t generation_floor; /* 16 monotonic security floor */ |
| 165 | uint64_t last_good_generation; /* 24 generation of the active slot when promoted (0 = unknown) */ |
| 166 | uint8_t reserved[CK_SLOT_UNIT - 32u]; /* 32 zero */ |
| 167 | }; |
| 168 | _Static_assert(sizeof(struct ck_slot_control) == CK_SLOT_UNIT, "ck_slot_control size"); |
| 169 | |
| 170 | /* ---- decisions ----------------------------------------------------------- */ |
| 171 | /* What the verifier learned about one slot (slot_select.c ck_slot_verify). */ |
| 172 | struct ck_slot_eval { |
| 173 | int valid; /* 1 = manifest well formed, signed by a trusted key, image digest matches */ |
| 174 | uint64_t generation; /* from the manifest when valid */ |
| 175 | uint64_t policy_generation; /* from the manifest when valid */ |
| 176 | const char *why; /* refusal reason when !valid (static string) */ |
| 177 | }; |
| 178 | |
| 179 | enum ck_slot_reason { |
| 180 | CK_SLOT_R_NONE = 0, |
| 181 | CK_SLOT_R_CANDIDATE_TRY, /* booting the candidate; tries decremented (loader must persist ctl_out first) */ |
| 182 | CK_SLOT_R_ACTIVE, /* booting the active slot */ |
| 183 | CK_SLOT_R_CANDIDATE_EXHAUSTED, /* candidate had no tries left: booting active */ |
| 184 | CK_SLOT_R_CANDIDATE_INVALID, /* candidate failed verification: booting active */ |
| 185 | CK_SLOT_R_ACTIVE_INVALID_FALLBACK,/* active failed verification: booting the other valid slot */ |
| 186 | CK_SLOT_R_NO_CONTROL, /* reserved legacy boot-record reason; never selected */ |
| 187 | CK_SLOT_R_RECOVERY_REQUIRED, /* no slot may boot */ |
| 188 | }; |
| 189 | |
| 190 | struct ck_slot_decision { |
| 191 | int boot_slot; /* 0, 1 or -1 (recovery required) */ |
| 192 | enum ck_slot_reason reason; |
| 193 | int loader_write; /* 1 = ctl_out must be committed before booting boot_slot */ |
| 194 | struct ck_slot_control ctl_out; /* valid when loader_write */ |
| 195 | }; |
| 196 | |
| 197 | /* Structural checks, no I/O, no crypto. 0 = well formed, -1 with *why. */ |
| 198 | int ck_slot_manifest_check(const struct ck_slot_manifest *m, const char **why); |
| 199 | int ck_slot_control_check(const struct ck_slot_control *c, const char **why); |
| 200 | |
| 201 | /* Full verification of one slot: structure, image_len <= image_cap, SHA-512 |
| 202 | * of the image bytes, signer_id names one of nkeys trusted public keys and |
| 203 | * the signature verifies under it. Fills *ev. Returns ev->valid. */ |
| 204 | int ck_slot_verify(const struct ck_slot_manifest *m, const uint8_t *image, uint64_t image_cap, |
| 205 | const uint8_t (*keys)[32], unsigned nkeys, struct ck_slot_eval *ev); |
| 206 | |
| 207 | /* The loader's decision. ctl may be NULL (missing or corrupt control record). */ |
| 208 | void ck_slot_select(const struct ck_slot_control *ctl, const struct ck_slot_eval ev[CK_SLOT_COUNT], |
| 209 | struct ck_slot_decision *d); |
| 210 | |
| 211 | /* Kernel-side transitions on a control record (pure; the caller commits). |
| 212 | * 0 = ok, -1 refused with *why. */ |
| 213 | void ck_slot_control_init(struct ck_slot_control *c, unsigned active_slot); |
| 214 | int ck_slot_stage_candidate(struct ck_slot_control *c, unsigned slot, uint64_t generation, const char **why); |
| 215 | int ck_slot_promote(struct ck_slot_control *c, unsigned booted_slot, uint64_t generation, |
| 216 | uint64_t policy_generation, const char **why); |
| 217 | int ck_slot_abandon_candidate(struct ck_slot_control *c, const char **why); |
| 218 | |
| 219 | |
| 220 | /* ---- boot record: what SLOTLDR.EFI hands the kernel (P11 cut 2b) ---------- */ |
| 221 | /* The control record alone cannot tell a kernel which slot it is: "candidate |
| 222 | * on its last try" and "candidate out of tries, active booted" both read |
| 223 | * tries=0. Before StartImage the loader therefore writes this record into one |
| 224 | * page it allocates with the memory type below, a UEFI OS-loader type (UEFI |
| 225 | * 2.10 section 7.2 AllocatePages: 0x80000000..0xFFFFFFFF are reserved for OS |
| 226 | * loaders). That page survives ExitBootServices and appears in the final |
| 227 | * memory map the unchanged stub already copies into the CHANDOF3 handoff, so |
| 228 | * neither the stub nor the handoff changes. The kernel's frame allocator only |
| 229 | * hands out EfiConventionalMemory, so the page is never reused. |
| 230 | * |
| 231 | * Trust: the record is written by the loader that verified the image, before |
| 232 | * ExitBootServices, i.e. inside the same firmware + loader trust boundary as |
| 233 | * the handoff itself. Its digest catches a torn or stray page, not an |
| 234 | * attacker inside that boundary. The kernel still cross-checks the slot's |
| 235 | * manifest on disk (generation, image_sha512) before it writes anything. */ |
| 236 | #define CK_SLOT_BOOT_MEMTYPE 0x80000A1Eu /* OS-loader memory type of the boot record page */ |
| 237 | #define CK_SLOT_BOOT_MAGIC "AIENSLB1" |
| 238 | #define CK_SLOT_BOOT_VERSION 1u |
| 239 | #define CK_SLOT_BOOT_BYTES 256u |
| 240 | #define CK_SLOT_BOOT_SIGNED 224u /* digest = SHA-256 over bytes [0, 224) */ |
| 241 | |
| 242 | struct ck_slot_boot_record { |
| 243 | uint8_t magic[8]; /* 0 "AIENSLB1" */ |
| 244 | uint32_t version; /* 8 1 */ |
| 245 | uint8_t boot_slot; /* 12 0 or 1: the slot whose image the loader started */ |
| 246 | uint8_t reason; /* 13 enum ck_slot_reason of the loader's decision */ |
| 247 | uint8_t tries; /* 14 tries_remaining on disk when the image started */ |
| 248 | uint8_t control_present; /* 15 1 = a valid control record was read (and committed when asked) */ |
| 249 | uint64_t control_seq; /* 16 torn-slot sequence of that record (0 without one) */ |
| 250 | uint64_t generation; /* 24 manifest generation of the started image */ |
| 251 | uint64_t policy_generation; /* 32 manifest policy_generation of the started image */ |
| 252 | uint8_t image_sha512[64]; /* 40 manifest image_sha512 of the started image */ |
| 253 | uint8_t loader_commit[40]; /* 104 loader build commit, ASCII, zero padded */ |
| 254 | uint8_t reserved[80]; /* 144 zero */ |
| 255 | uint8_t digest[32]; /* 224 SHA-256 over bytes [0, 224) */ |
| 256 | }; |
| 257 | _Static_assert(sizeof(struct ck_slot_boot_record) == CK_SLOT_BOOT_BYTES, "ck_slot_boot_record size"); |
| 258 | _Static_assert(offsetof(struct ck_slot_boot_record, digest) == CK_SLOT_BOOT_SIGNED, "boot record digest"); |
| 259 | |
| 260 | /* Fill the digest (loader side). */ |
| 261 | void ck_slot_boot_seal(struct ck_slot_boot_record *b); |
| 262 | /* Structure + digest + ranges (kernel side). 0 = ok, -1 with *why. */ |
| 263 | int ck_slot_boot_check(const struct ck_slot_boot_record *b, const char **why); |
| 264 | |
| 265 | /* ---- what the kernel does with its slot (P11 cut 2b) ---------------------- */ |
| 266 | /* Pure decision, shared by the kernel (svc/slot_service.c) and the host |
| 267 | * self-test. Inputs: the boot record (valid), the control record recovered |
| 268 | * from disk (NULL when absent or corrupt), the started slot's manifest from |
| 269 | * disk (structurally checked by the caller, NULL when unreadable) and whether |
| 270 | * the service reached its health milestone. Rules, in order: |
| 271 | * - no control record, or the on-disk manifest of the started slot does |
| 272 | * not match the boot record (generation, image_sha512): REFUSE, write |
| 273 | * nothing (the disk changed under us or the record lies); |
| 274 | * - reason CANDIDATE_TRY and control still names that slot as candidate: |
| 275 | * healthy -> PROMOTE (ck_slot_promote with the manifest's generation and |
| 276 | * policy_generation); not healthy -> WAIT (nothing written; tries keep |
| 277 | * counting down in the loader); |
| 278 | * - reason CANDIDATE_EXHAUSTED or CANDIDATE_INVALID, control has a |
| 279 | * candidate and its active slot is the started slot -> ABANDON |
| 280 | * (ck_slot_abandon_candidate: candidate cleared, FAILED flag set; the |
| 281 | * active slot stays). This is slot_select.c rule 3's "abandoned by the |
| 282 | * kernel later". Not gated on health: the loader already decided. |
| 283 | * - anything else -> NONE. |
| 284 | * *out holds the record to commit for PROMOTE and ABANDON. */ |
| 285 | enum ck_slot_action { |
| 286 | CK_SLOT_A_NONE = 0, |
| 287 | CK_SLOT_A_PROMOTE, |
| 288 | CK_SLOT_A_ABANDON, |
| 289 | CK_SLOT_A_WAIT, |
| 290 | CK_SLOT_A_REFUSE, |
| 291 | }; |
| 292 | enum ck_slot_action ck_slot_kernel_action(const struct ck_slot_boot_record *b, const struct ck_slot_control *ctl, |
| 293 | const struct ck_slot_manifest *booted, int healthy, |
| 294 | struct ck_slot_control *out, const char **why); |
| 295 | const char *ck_slot_action_text(enum ck_slot_action a); |
| 296 | |
| 297 | /* Text for a reason code (static string). */ |
| 298 | const char *ck_slot_reason_text(enum ck_slot_reason r); |
| 299 | |
| 300 | #endif /* AIENOS_CK_SLOT_FORMAT_H */ |