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 / kernel / tools / ck_uefi_vars.c
| 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 | /* ck_uefi_vars -- host reader for an edk2 (AAVMF/OVMF) variable store file, |
| 18 | * used by scripts/qemu_ck_rollback_test.sh (CK gate M0_ROLLBACK). It reads |
| 19 | * the vars.fd a QEMU run left behind and prints the boot variables the |
| 20 | * firmware itself holds, so the rollback checks do not depend on what a guest |
| 21 | * program printed. Hosted C, test tooling only, never part of an image. |
| 22 | * Read-only: the file is opened "rb" and never written. |
| 23 | * |
| 24 | * ck_uefi_vars VARS.fd print the live boot variables (below) |
| 25 | * ck_uefi_vars --self-test parse synthetic stores; exit 0 only if every |
| 26 | * case gives the expected answer |
| 27 | * |
| 28 | * Output (one line each, stable order, for diffing between boots): |
| 29 | * uefi_vars: store=authenticated|plain live=<n> obsolete=<n> |
| 30 | * BootOrder=<hex16,hex16,...>|absent |
| 31 | * BootNext=<hex16>|absent |
| 32 | * Boot<XXXX>=<description> one per live Boot#### option, ascending |
| 33 | * Exit 0 on success, 2 when the file is not a healthy edk2 variable store. |
| 34 | * |
| 35 | * Layout source (read, not recalled): edk2 tag edk2-stable202402 (the |
| 36 | * firmware here is Ubuntu qemu-efi-aarch64 2024.02-2ubuntu0.9), |
| 37 | * MdeModulePkg/Include/Guid/VariableFormat.h: VARIABLE_STORE_HEADER (GUID |
| 38 | * signature, UINT32 Size, UINT8 Format 0x5a, UINT8 State 0xfe, UINT16, UINT32 |
| 39 | * = 28 bytes), VARIABLE_HEADER (32 bytes) and AUTHENTICATED_VARIABLE_HEADER |
| 40 | * (60 bytes), StartId 0x55AA, State VAR_ADDED 0x3f / VAR_IN_DELETED_TRANSITION |
| 41 | * 0xfe / VAR_DELETED 0xfd / VAR_HEADER_VALID_ONLY 0x7f, HEADER_ALIGNMENT 4, |
| 42 | * name and data unpadded (ALIGNMENT 1). Store start: the firmware volume |
| 43 | * header's HeaderLength (MdePkg/Include/Pi/PiFirmwareVolume.h, UINT16 at |
| 44 | * offset 48, signature "_FVH" at offset 40). Which copy is live: |
| 45 | * MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c FindVariableEx |
| 46 | * (State VAR_ADDED wins; a State VAR_ADDED & VAR_IN_DELETED_TRANSITION copy is |
| 47 | * used only when no VAR_ADDED copy exists). */ |
| 48 | #include <stdint.h> |
| 49 | #include <stdio.h> |
| 50 | #include <stdlib.h> |
| 51 | #include <string.h> |
| 52 | |
| 53 | #define VARIABLE_DATA 0x55AAu |
| 54 | #define VAR_ADDED 0x3fu |
| 55 | #define VAR_IN_DELETED_TRANSITION 0xfeu |
| 56 | #define VAR_HEADER_VALID_ONLY 0x7fu |
| 57 | #define VAR_LIVE_IN_TRANSITION (VAR_ADDED & VAR_IN_DELETED_TRANSITION) /* 0x3e */ |
| 58 | #define STORE_FORMATTED 0x5au |
| 59 | #define STORE_HEALTHY 0xfeu |
| 60 | #define STORE_HDR 28u |
| 61 | #define PLAIN_HDR 32u |
| 62 | #define AUTH_HDR 60u |
| 63 | #define MAX_VARS 4096 |
| 64 | |
| 65 | /* EFI_VARIABLE_GUID ddcf3616-3275-4164-98b6-fe85707ffe7d and |
| 66 | * EFI_AUTHENTICATED_VARIABLE_GUID aaf32c78-947b-439a-a180-2e144ec37792, as |
| 67 | * the bytes stored on flash (first three fields little-endian). */ |
| 68 | static const uint8_t plain_sig[16] = {0x16, 0x36, 0xcf, 0xdd, 0x75, 0x32, 0x64, 0x41, |
| 69 | 0x98, 0xb6, 0xfe, 0x85, 0x70, 0x7f, 0xfe, 0x7d}; |
| 70 | static const uint8_t auth_sig[16] = {0x78, 0x2c, 0xf3, 0xaa, 0x7b, 0x94, 0x9a, 0x43, |
| 71 | 0xa1, 0x80, 0x2e, 0x14, 0x4e, 0xc3, 0x77, 0x92}; |
| 72 | /* EFI_GLOBAL_VARIABLE 8be4df61-93ca-11d2-aa0d-00e098032b8c (UEFI spec 3.3). */ |
| 73 | static const uint8_t global_guid[16] = {0x61, 0xdf, 0xe4, 0x8b, 0xca, 0x93, 0xd2, 0x11, |
| 74 | 0xaa, 0x0d, 0x00, 0xe0, 0x98, 0x03, 0x2b, 0x8c}; |
| 75 | |
| 76 | struct var { |
| 77 | uint8_t state; |
| 78 | uint8_t guid[16]; |
| 79 | char name[64]; /* ASCII copy of the UCS-2 name; '?' for non-ASCII */ |
| 80 | const uint8_t *data; |
| 81 | uint32_t data_size; |
| 82 | int superseded; |
| 83 | }; |
| 84 | |
| 85 | struct store { |
| 86 | int authenticated; |
| 87 | unsigned live, obsolete, nvars; |
| 88 | struct var v[MAX_VARS]; |
| 89 | }; |
| 90 | |
| 91 | static uint16_t rd16(const uint8_t *p) { return (uint16_t)(p[0] | p[1] << 8); } |
| 92 | static uint32_t rd32(const uint8_t *p) { return (uint32_t)rd16(p) | (uint32_t)rd16(p + 2) << 16; } |
| 93 | |
| 94 | /* Returns 0 and fills *s, or -1 with a reason on stderr. */ |
| 95 | static int parse(const uint8_t *img, size_t len, struct store *s) |
| 96 | { |
| 97 | memset(s, 0, sizeof *s); |
| 98 | if (len < 64 || memcmp(img + 40, "_FVH", 4) != 0) { |
| 99 | fprintf(stderr, "ck_uefi_vars: no firmware volume header (_FVH at offset 40)\n"); |
| 100 | return -1; |
| 101 | } |
| 102 | size_t base = rd16(img + 48); |
| 103 | if (base < 56 || base + STORE_HDR > len) { |
| 104 | fprintf(stderr, "ck_uefi_vars: bad FV HeaderLength %zu\n", base); |
| 105 | return -1; |
| 106 | } |
| 107 | const uint8_t *sh = img + base; |
| 108 | if (memcmp(sh, auth_sig, 16) == 0) |
| 109 | s->authenticated = 1; |
| 110 | else if (memcmp(sh, plain_sig, 16) != 0) { |
| 111 | fprintf(stderr, "ck_uefi_vars: unknown variable store signature\n"); |
| 112 | return -1; |
| 113 | } |
| 114 | uint32_t size = rd32(sh + 16); |
| 115 | if (sh[20] != STORE_FORMATTED || sh[21] != STORE_HEALTHY) { |
| 116 | fprintf(stderr, "ck_uefi_vars: store not formatted/healthy (format=0x%02x state=0x%02x)\n", |
| 117 | sh[20], sh[21]); |
| 118 | return -1; |
| 119 | } |
| 120 | if (size < STORE_HDR || base + size > len) { |
| 121 | fprintf(stderr, "ck_uefi_vars: store size %u exceeds file\n", size); |
| 122 | return -1; |
| 123 | } |
| 124 | size_t end = base + size, hdr = s->authenticated ? AUTH_HDR : PLAIN_HDR; |
| 125 | size_t off = (base + STORE_HDR + 3) & ~(size_t)3; |
| 126 | while (off + hdr <= end && rd16(img + off) == VARIABLE_DATA) { |
| 127 | const uint8_t *h = img + off; |
| 128 | uint32_t name_size = rd32(h + hdr - 24), data_size = rd32(h + hdr - 20); |
| 129 | const uint8_t *guid = h + hdr - 16; |
| 130 | if (name_size > end - off - hdr || data_size > end - off - hdr - name_size) { |
| 131 | fprintf(stderr, "ck_uefi_vars: variable at 0x%zx runs past the store\n", off); |
| 132 | return -1; |
| 133 | } |
| 134 | if (s->nvars == MAX_VARS) { |
| 135 | fprintf(stderr, "ck_uefi_vars: more than %d variables\n", MAX_VARS); |
| 136 | return -1; |
| 137 | } |
| 138 | struct var *v = &s->v[s->nvars++]; |
| 139 | v->state = h[2]; |
| 140 | memcpy(v->guid, guid, 16); |
| 141 | size_t n = 0; |
| 142 | for (uint32_t i = 0; i + 1 < name_size && n < sizeof v->name - 1; i += 2) { |
| 143 | uint16_t c = rd16(h + hdr + i); |
| 144 | if (!c) |
| 145 | break; |
| 146 | v->name[n++] = c < 0x80 ? (char)c : '?'; |
| 147 | } |
| 148 | v->name[n] = 0; |
| 149 | v->data = h + hdr + name_size; |
| 150 | v->data_size = data_size; |
| 151 | off = (off + hdr + name_size + data_size + 3) & ~(size_t)3; |
| 152 | } |
| 153 | /* FindVariableEx: VAR_ADDED wins over an in-deleted-transition copy. */ |
| 154 | for (unsigned i = 0; i < s->nvars; i++) { |
| 155 | struct var *a = &s->v[i]; |
| 156 | if (a->state != VAR_LIVE_IN_TRANSITION) |
| 157 | continue; |
| 158 | for (unsigned j = 0; j < s->nvars; j++) |
| 159 | if (s->v[j].state == VAR_ADDED && !strcmp(s->v[j].name, a->name) && |
| 160 | !memcmp(s->v[j].guid, a->guid, 16)) |
| 161 | a->superseded = 1; |
| 162 | } |
| 163 | for (unsigned i = 0; i < s->nvars; i++) { |
| 164 | struct var *a = &s->v[i]; |
| 165 | int live = a->state == VAR_ADDED || (a->state == VAR_LIVE_IN_TRANSITION && !a->superseded); |
| 166 | if (live) |
| 167 | s->live++; |
| 168 | else |
| 169 | s->obsolete++; |
| 170 | a->superseded = !live; /* reuse: 1 = not live */ |
| 171 | } |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static const struct var *find_global(const struct store *s, const char *name) |
| 176 | { |
| 177 | for (unsigned i = 0; i < s->nvars; i++) |
| 178 | if (!s->v[i].superseded && !memcmp(s->v[i].guid, global_guid, 16) && |
| 179 | !strcmp(s->v[i].name, name)) |
| 180 | return &s->v[i]; |
| 181 | return NULL; |
| 182 | } |
| 183 | |
| 184 | static int is_boot_option(const char *n) |
| 185 | { |
| 186 | if (strncmp(n, "Boot", 4) || strlen(n) != 8) |
| 187 | return 0; |
| 188 | for (int i = 4; i < 8; i++) |
| 189 | if (!((n[i] >= '0' && n[i] <= '9') || (n[i] >= 'A' && n[i] <= 'F'))) |
| 190 | return 0; |
| 191 | return 1; |
| 192 | } |
| 193 | |
| 194 | static void report(const struct store *s, FILE *o) |
| 195 | { |
| 196 | fprintf(o, "uefi_vars: store=%s live=%u obsolete=%u\n", s->authenticated ? "authenticated" : "plain", |
| 197 | s->live, s->obsolete); |
| 198 | const struct var *bo = find_global(s, "BootOrder"); |
| 199 | if (!bo) |
| 200 | fprintf(o, "BootOrder=absent\n"); |
| 201 | else { |
| 202 | fprintf(o, "BootOrder="); |
| 203 | for (uint32_t i = 0; i + 1 < bo->data_size; i += 2) |
| 204 | fprintf(o, "%s%04X", i ? "," : "", rd16(bo->data + i)); |
| 205 | fprintf(o, "%s\n", bo->data_size % 2 ? " (odd size)" : ""); |
| 206 | } |
| 207 | const struct var *bn = find_global(s, "BootNext"); |
| 208 | if (!bn) |
| 209 | fprintf(o, "BootNext=absent\n"); |
| 210 | else if (bn->data_size != 2) |
| 211 | fprintf(o, "BootNext=bad-size-%u\n", bn->data_size); |
| 212 | else |
| 213 | fprintf(o, "BootNext=%04X\n", rd16(bn->data)); |
| 214 | /* Boot#### options in ascending name order. EFI_LOAD_OPTION: UINT32 |
| 215 | * Attributes, UINT16 FilePathListLength, CHAR16 Description[] (UEFI 3.1.3). */ |
| 216 | const char *prev = ""; |
| 217 | for (;;) { |
| 218 | const struct var *next = NULL; |
| 219 | for (unsigned i = 0; i < s->nvars; i++) { |
| 220 | const struct var *v = &s->v[i]; |
| 221 | if (v->superseded || memcmp(v->guid, global_guid, 16) || !is_boot_option(v->name)) |
| 222 | continue; |
| 223 | if (strcmp(v->name, prev) > 0 && (!next || strcmp(v->name, next->name) < 0)) |
| 224 | next = v; |
| 225 | } |
| 226 | if (!next) |
| 227 | break; |
| 228 | char desc[80]; |
| 229 | size_t n = 0; |
| 230 | for (uint32_t i = 6; i + 1 < next->data_size && n < sizeof desc - 1; i += 2) { |
| 231 | uint16_t c = rd16(next->data + i); |
| 232 | if (!c) |
| 233 | break; |
| 234 | desc[n++] = c >= 0x20 && c < 0x7f ? (char)c : '?'; |
| 235 | } |
| 236 | desc[n] = 0; |
| 237 | fprintf(o, "%s=%s\n", next->name, desc); |
| 238 | prev = next->name; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /* ---- self-test: synthetic stores ---- */ |
| 243 | |
| 244 | static uint8_t *t_img; |
| 245 | static size_t t_off; |
| 246 | |
| 247 | static void t_begin(int auth) |
| 248 | { |
| 249 | memset(t_img, 0xff, 8192); |
| 250 | memset(t_img, 0, 72); |
| 251 | memcpy(t_img + 40, "_FVH", 4); |
| 252 | t_img[48] = 72; |
| 253 | memcpy(t_img + 72, auth ? auth_sig : plain_sig, 16); |
| 254 | uint32_t size = 8192 - 72; |
| 255 | memcpy(t_img + 88, &size, 4); |
| 256 | t_img[92] = STORE_FORMATTED; |
| 257 | t_img[93] = STORE_HEALTHY; |
| 258 | memset(t_img + 94, 0, 6); |
| 259 | t_off = 100; |
| 260 | } |
| 261 | |
| 262 | static void t_var(int auth, uint8_t state, const char *name, const void *data, uint32_t dsize) |
| 263 | { |
| 264 | size_t hdr = auth ? AUTH_HDR : PLAIN_HDR; |
| 265 | uint8_t *h = t_img + t_off; |
| 266 | memset(h, 0, hdr); |
| 267 | h[0] = 0xaa; |
| 268 | h[1] = 0x55; |
| 269 | h[2] = state; |
| 270 | h[4] = 7; |
| 271 | uint32_t nsize = (uint32_t)(strlen(name) + 1) * 2; |
| 272 | memcpy(h + hdr - 24, &nsize, 4); |
| 273 | memcpy(h + hdr - 20, &dsize, 4); |
| 274 | memcpy(h + hdr - 16, global_guid, 16); |
| 275 | for (size_t i = 0; i <= strlen(name); i++) { |
| 276 | h[hdr + 2 * i] = (uint8_t)name[i]; |
| 277 | h[hdr + 2 * i + 1] = 0; |
| 278 | } |
| 279 | memcpy(h + hdr + nsize, data, dsize); |
| 280 | t_off = (t_off + hdr + nsize + dsize + 3) & ~(size_t)3; |
| 281 | } |
| 282 | |
| 283 | static int t_expect(const char *label, const char *want_line, int want_present) |
| 284 | { |
| 285 | static struct store s; |
| 286 | char buf[4096]; |
| 287 | FILE *m = fmemopen(buf, sizeof buf, "w"); |
| 288 | int rc = parse(t_img, 8192, &s); |
| 289 | if (rc == 0) |
| 290 | report(&s, m); |
| 291 | fclose(m); |
| 292 | if (rc != 0) { |
| 293 | printf("%s %s (parse refused)\n", want_line ? "FAIL" : "PASS", label); |
| 294 | return want_line ? 1 : 0; |
| 295 | } |
| 296 | if (!want_line) { |
| 297 | printf("FAIL %s (parse accepted a bad store)\n", label); |
| 298 | return 1; |
| 299 | } |
| 300 | char needle[128]; |
| 301 | snprintf(needle, sizeof needle, "%s\n", want_line); |
| 302 | int got = strstr(buf, needle) != NULL; |
| 303 | printf("%s %s\n", got == want_present ? "PASS" : "FAIL", label); |
| 304 | return got != want_present; |
| 305 | } |
| 306 | |
| 307 | static int self_test(void) |
| 308 | { |
| 309 | static uint8_t img[8192]; |
| 310 | t_img = img; |
| 311 | int bad = 0; |
| 312 | const uint8_t order1[2] = {0x01, 0x00}, next0[2] = {0x00, 0x00}, order21[4] = {2, 0, 1, 0}; |
| 313 | uint8_t opt[6 + 2 * 17] = {1, 0, 0, 0, 0, 0}; |
| 314 | const char *d = "AIENOS Candidate"; |
| 315 | for (size_t i = 0; i <= strlen(d); i++) |
| 316 | opt[6 + 2 * i] = (uint8_t)d[i]; |
| 317 | |
| 318 | for (int auth = 0; auth <= 1; auth++) { |
| 319 | t_begin(auth); |
| 320 | t_var(auth, VAR_ADDED, "BootOrder", order1, 2); |
| 321 | t_var(auth, VAR_ADDED, "BootNext", next0, 2); |
| 322 | t_var(auth, VAR_ADDED, "Boot0000", opt, sizeof opt); |
| 323 | bad |= t_expect(auth ? "auth: staged BootNext read" : "plain: staged BootNext read", "BootNext=0000", 1); |
| 324 | bad |= t_expect("staged BootOrder read", "BootOrder=0001", 1); |
| 325 | bad |= t_expect("Boot0000 description read", "Boot0000=AIENOS Candidate", 1); |
| 326 | } |
| 327 | /* The firmware deletes BootNext: the old header goes to VAR_DELETED. */ |
| 328 | t_begin(1); |
| 329 | t_var(1, VAR_ADDED, "BootOrder", order1, 2); |
| 330 | t_var(1, 0xfd, "BootNext", next0, 2); |
| 331 | bad |= t_expect("deleted BootNext is absent", "BootNext=absent", 1); |
| 332 | bad |= t_expect("deleted BootNext is not reported present", "BootNext=0000", 0); |
| 333 | /* A header-only (interrupted add) BootNext is not live. */ |
| 334 | t_begin(1); |
| 335 | t_var(1, VAR_HEADER_VALID_ONLY, "BootNext", next0, 2); |
| 336 | bad |= t_expect("header-valid-only BootNext is absent", "BootNext=absent", 1); |
| 337 | /* An in-deleted-transition copy is live while no newer copy exists. */ |
| 338 | t_begin(1); |
| 339 | t_var(1, VAR_LIVE_IN_TRANSITION, "BootNext", next0, 2); |
| 340 | bad |= t_expect("in-transition BootNext with no newer copy is live", "BootNext=0000", 1); |
| 341 | /* BootOrder rewritten: the old copy in transition, the new one added. */ |
| 342 | t_begin(1); |
| 343 | t_var(1, VAR_LIVE_IN_TRANSITION, "BootOrder", order1, 2); |
| 344 | t_var(1, VAR_ADDED, "BootOrder", order21, 4); |
| 345 | bad |= t_expect("newer BootOrder wins over the in-transition copy", "BootOrder=0002,0001", 1); |
| 346 | bad |= t_expect("old BootOrder copy is not reported", "BootOrder=0001", 0); |
| 347 | /* Nothing written yet. */ |
| 348 | t_begin(1); |
| 349 | bad |= t_expect("empty store: BootOrder absent", "BootOrder=absent", 1); |
| 350 | bad |= t_expect("empty store: BootNext absent", "BootNext=absent", 1); |
| 351 | /* Refusals. */ |
| 352 | t_begin(1); |
| 353 | t_img[93] = 0x00; |
| 354 | bad |= t_expect("unhealthy store refused", NULL, 0); |
| 355 | t_begin(1); |
| 356 | t_img[72] ^= 1; |
| 357 | bad |= t_expect("unknown store signature refused", NULL, 0); |
| 358 | t_begin(1); |
| 359 | memcpy(t_img + 40, "_FVX", 4); |
| 360 | bad |= t_expect("missing _FVH refused", NULL, 0); |
| 361 | t_begin(1); |
| 362 | t_var(1, VAR_ADDED, "BootNext", next0, 2); |
| 363 | uint32_t huge = 0x10000; |
| 364 | memcpy(t_img + 100 + AUTH_HDR - 20, &huge, 4); |
| 365 | bad |= t_expect("variable running past the store refused", NULL, 0); |
| 366 | printf("CK_UEFI_VARS_SELF_TEST: %s\n", bad ? "FAIL" : "PASS"); |
| 367 | return bad ? 1 : 0; |
| 368 | } |
| 369 | |
| 370 | int main(int argc, char **argv) |
| 371 | { |
| 372 | if (argc == 2 && !strcmp(argv[1], "--self-test")) |
| 373 | return self_test(); |
| 374 | if (argc != 2 || argv[1][0] == '-') { |
| 375 | fprintf(stderr, "usage: ck_uefi_vars VARS.fd | --self-test\n"); |
| 376 | return 2; |
| 377 | } |
| 378 | FILE *f = fopen(argv[1], "rb"); |
| 379 | if (!f) { |
| 380 | perror(argv[1]); |
| 381 | return 2; |
| 382 | } |
| 383 | size_t cap = 1u << 20, len = 0; |
| 384 | uint8_t *img = malloc(cap); |
| 385 | size_t n; |
| 386 | while (img && (n = fread(img + len, 1, cap - len, f)) > 0) { |
| 387 | len += n; |
| 388 | if (len == cap) { |
| 389 | uint8_t *bigger = realloc(img, cap * 2); |
| 390 | if (!bigger) { |
| 391 | free(img); |
| 392 | img = NULL; |
| 393 | break; |
| 394 | } |
| 395 | img = bigger; |
| 396 | cap *= 2; |
| 397 | } |
| 398 | } |
| 399 | fclose(f); |
| 400 | if (!img) { |
| 401 | fprintf(stderr, "ck_uefi_vars: out of memory\n"); |
| 402 | return 2; |
| 403 | } |
| 404 | static struct store s; |
| 405 | int rc = parse(img, len, &s); |
| 406 | if (rc == 0) |
| 407 | report(&s, stdout); |
| 408 | free(img); |
| 409 | return rc == 0 ? 0 : 2; |
| 410 | } |