interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / boot / slot_loader.c

539 lines · 22981 bytes Raw Blame History
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_loader.c -- SLOTLDR.EFI: the AIENOS signed A/B image slot loader
18 * (native v1 plan P11 cut 2a, gate N7; docs/NATIVE_IMAGE_SLOTS.md).
19 *
20 * A separate UEFI application that runs IN FRONT of the unchanged native
21 * image (stub native/boot/efi_main.c + kernel, one PE). The owner approved a
22 * narrowly scoped slot loader on 2026-10-12: signed A/B image selection,
23 * boot-success tracking (bounded candidate tries) and rollback, nothing else.
24 * Putting it in its own PE keeps the qualified stub + kernel byte-identical
25 * (BOOT_HANDOFF_CONTRACT.md section 7) and avoids recursion: a stub that
26 * chain-loaded itself would select slots again inside the chained copy.
27 *
28 * Flow (UEFI 2.10 boot services only; edk2 BlockIo.h, LoadedImage.h):
29 * 1. LocateHandleBuffer(ByProtocol, BlockIo): on every whole disk
30 * (LogicalPartition == 0, block size 512 or 4096) read the primary GPT
31 * (header CRC32, entry array CRC32, usable range) and find entries with
32 * the slots partition type CK_SLOT_PART_TYPE_BYTES. Exactly one across
33 * all disks, or recovery is required (no fallback guess).
34 * 2. Control record: torn-slot recover over partition units 0..3
35 * (native/store/torn_slot.c, the same object the host tool links).
36 * 3. Each slot: read the manifest unit, read image_len bytes of the image
37 * (bounded by the slot capacity, <= 8 MiB ReadBlocks pieces), then
38 * ck_slot_verify against the owner keys baked in at build time
39 * (CK_SLOT_KEYS, or the TEST 1 key with CK_TEST_SLOT_KEY=1, which the
40 * Makefile refuses with CK_HARDWARE_STAGING).
41 * 4. ck_slot_select (slot_select.c, mutation-tested in make slot-test).
42 * CK_SLOT_R_CANDIDATE_TRY: commit ctl_out (tries - 1) with ts_commit,
43 * FlushBlocks, read it back, and only then boot the candidate. That is
44 * what bounds attempts when a candidate hangs or never promotes. If the
45 * commit fails the candidate is treated as invalid and selection reruns.
46 * 4b. (cut 2b) Hand the kernel one boot record (slot_format.h struct
47 * ck_slot_boot_record: slot, reason, tries, control sequence, the
48 * manifest's generation and digest) in one page of OS-loader memory type
49 * CK_SLOT_BOOT_MEMTYPE. The page survives ExitBootServices and appears in
50 * the memory map the unchanged stub hands over; the service kernel
51 * (svc/slot_service.c) promotes or abandons from it. No page: the kernel
52 * writes nothing. The control record is committed through
53 * ck_slot_control_commit (slot_control.c), the routine the kernel uses.
54 * 5. One decision line, then LoadImage(SourceBuffer = the verified bytes,
55 * DevicePath = this loader's own LoadedImageDevicePath, so the chained
56 * stub's LoadedImage->DeviceHandle is still the boot volume and its
57 * MODEL.MAP still loads) and StartImage.
58 * Recovery required: print why for both slots and return EFI_LOAD_ERROR to
59 * the firmware, which goes on to its next boot option.
60 *
61 * Never: UEFI variables, runtime services, ExitBootServices (the chained
62 * stub leaves boot services itself), any disk write other than the control
63 * region, any write when the control record is missing or corrupt. Missing
64 * control requires recovery, not a zero-floor signed-image boot.
65 * Freestanding C with the kernel's flags; no kernel objects are linked. */
66#include <stdarg.h>
67#include "efi.h"
68#include "slot_format.h"
69#include "slot_control.h"
70#include "../kernel/core/fmt.h"
71
72#ifndef AIENOS_COMMIT
73#define AIENOS_COMMIT "unknown"
74#endif
75
76void *memcpy(void *dst, const void *src, size_t n);
77void *memset(void *dst, int c, size_t n);
78int memcmp(const void *a, const void *b, size_t n);
79
80/* Generated at build time (native/kernel/Makefile, ck_slot_tool keys-c). */
81extern const uint8_t ck_slot_loader_keys[][32];
82extern const unsigned ck_slot_loader_nkeys;
83extern const char ck_slot_loader_key_note[];
84
85#define EFI_LOAD_ERROR EFI_ERR(1)
86#define READ_CHUNK (8ull << 20)
87#define PAGE 4096ull
88#define GPT_ENT_MAX (128u * 1024u)
89
90/* Boot-services members efi.h keeps as placeholders (UEFI 2.10 sections 7.4
91 * and 13.9; r-efi system.rs BootLoadImage/BootStartImage, block_io.rs). The
92 * shared efi.h is left untouched so the stub's sources do not change. */
93typedef EFI_STATUS (*load_image_fn)(uint8_t boot_policy, EFI_HANDLE parent, void *device_path,
94 void *source, uint64_t source_size, EFI_HANDLE *out);
95typedef EFI_STATUS (*start_image_fn)(EFI_HANDLE image, uint64_t *exit_data_size, CHAR16 **exit_data);
96typedef EFI_STATUS (*write_blocks_fn)(EFI_BLOCK_IO_PROTOCOL *bio, uint32_t media_id, uint64_t lba,
97 uint64_t size, void *buf);
98typedef EFI_STATUS (*flush_blocks_fn)(EFI_BLOCK_IO_PROTOCOL *bio);
99
100/* EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL_GUID (UEFI 2.10 section 9.2;
101 * vendor/uefi/src/proto/device_path/mod.rs LoadedImageDevicePath). */
102#define LOADED_IMAGE_DEVICE_PATH_GUID_INIT \
103 { 0xbc62157e, 0x3e33, 0x4fec, { 0x99, 0x20, 0x2d, 0x3b, 0x36, 0xd7, 0x50, 0xdf } }
104
105static EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout;
106static EFI_BOOT_SERVICES *bs;
107
108static uint8_t blk[4096] __attribute__((aligned(4096)));
109static uint8_t hdr[4096];
110static uint8_t ents[GPT_ENT_MAX + 4096] __attribute__((aligned(4096)));
111static uint8_t bounce[CK_SLOT_UNIT] __attribute__((aligned(4096)));
112static struct ck_slot_manifest man[CK_SLOT_COUNT] __attribute__((aligned(4096)));
113static struct ck_slot_control ctl_rec;
114
115/* ---- console -------------------------------------------------------------- */
116struct line {
117 char b[256];
118 size_t n;
119};
120
121static void line_emit(void *ctx, char c)
122{
123 struct line *l = ctx;
124 if (l->n < sizeof l->b - 1)
125 l->b[l->n++] = c;
126}
127
128static void say(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
129static void say(const char *fmt, ...)
130{
131 struct line l;
132 CHAR16 w[2 * sizeof l.b + 1];
133 size_t k = 0;
134 va_list ap;
135 l.n = 0;
136 va_start(ap, fmt);
137 ck_vformat(line_emit, &l, fmt, ap);
138 va_end(ap);
139 for (size_t i = 0; i < l.n; i++) {
140 if (l.b[i] == '\n')
141 w[k++] = '\r';
142 w[k++] = (uint8_t)l.b[i];
143 }
144 w[k] = 0;
145 if (conout)
146 conout->OutputString(conout, w);
147}
148
149/* ---- primary GPT ------------------------------------------------------------ */
150static uint32_t crc32(const uint8_t *p, size_t n)
151{
152 uint32_t c = 0xFFFFFFFFu;
153 for (size_t i = 0; i < n; i++) {
154 c ^= p[i];
155 for (int k = 0; k < 8; k++)
156 c = (c >> 1) ^ (0xEDB88320u & (0u - (c & 1u)));
157 }
158 return ~c;
159}
160
161static uint32_t le32(const uint8_t *p)
162{
163 return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24;
164}
165
166static uint64_t le64(const uint8_t *p)
167{
168 return (uint64_t)le32(p) | (uint64_t)le32(p + 4) << 32;
169}
170
171struct slots_part {
172 EFI_BLOCK_IO_PROTOCOL *bio;
173 uint32_t bs;
174 uint64_t first, last;
175};
176
177/* Number of slots-type entries in this disk's primary GPT (0: no GPT or none),
178 * the first one in *out; -1 with *why when the GPT is present but refused. */
179static int gpt_scan(EFI_BLOCK_IO_PROTOCOL *bio, struct slots_part *out, const char **why)
180{
181 static const uint8_t type[16] = CK_SLOT_PART_TYPE_BYTES;
182 const EFI_BLOCK_IO_MEDIA *m = bio->Media;
183 uint32_t bsz = m->BlockSize;
184 int count = 0;
185 if (bio->ReadBlocks(bio, m->MediaId, 1, bsz, blk) != EFI_SUCCESS) {
186 *why = "LBA 1 read failed";
187 return -1;
188 }
189 if (memcmp(blk, "EFI PART", 8) != 0)
190 return 0;
191 uint32_t hsz = le32(blk + 12);
192 if (le32(blk + 8) != 0x00010000u || hsz < 92 || hsz > bsz) {
193 *why = "GPT header revision or size";
194 return -1;
195 }
196 memcpy(hdr, blk, hsz);
197 memset(hdr + 16, 0, 4);
198 if (crc32(hdr, hsz) != le32(blk + 16)) {
199 *why = "GPT header CRC32 mismatch";
200 return -1;
201 }
202 uint64_t fu = le64(blk + 40), lu = le64(blk + 48), elba = le64(blk + 72);
203 uint32_t nent = le32(blk + 80), esz = le32(blk + 84), ecrc = le32(blk + 88);
204 if (le64(blk + 24) != 1 || fu > lu || lu > m->LastBlock) {
205 *why = "GPT header LBAs";
206 return -1;
207 }
208 if (esz < 128 || esz % 8 || nent == 0 || nent > 1024 || (uint64_t)nent * esz > GPT_ENT_MAX) {
209 *why = "GPT entry geometry";
210 return -1;
211 }
212 uint64_t ebytes = (uint64_t)nent * esz, eblocks = (ebytes + bsz - 1) / bsz;
213 if (elba < 2 || elba + eblocks - 1 > m->LastBlock) {
214 *why = "GPT entry array LBA";
215 return -1;
216 }
217 if (bio->ReadBlocks(bio, m->MediaId, elba, eblocks * bsz, ents) != EFI_SUCCESS) {
218 *why = "GPT entry array read failed";
219 return -1;
220 }
221 if (crc32(ents, (size_t)ebytes) != ecrc) {
222 *why = "GPT entry array CRC32 mismatch";
223 return -1;
224 }
225 for (uint32_t i = 0; i < nent; i++) {
226 const uint8_t *e = ents + (uint64_t)i * esz;
227 if (memcmp(e, type, 16) != 0)
228 continue;
229 uint64_t f = le64(e + 32), l = le64(e + 40);
230 if (f > l || f < fu || l > lu) {
231 *why = "slots partition outside the GPT usable range";
232 return -1;
233 }
234 if (count++ == 0) {
235 out->bio = bio;
236 out->bs = bsz;
237 out->first = f;
238 out->last = l;
239 }
240 }
241 return count;
242}
243
244/* Exactly one slots partition across every whole disk, or -1 with *why. */
245static int find_slots(struct slots_part *sp, const char **why)
246{
247 static const EFI_GUID bio_guid = EFI_BLOCK_IO_PROTOCOL_GUID_INIT;
248 EFI_HANDLE *handles = 0;
249 uint64_t n = 0;
250 int total = 0;
251 if (bs->LocateHandleBuffer(ByProtocol, &bio_guid, 0, &n, &handles) != EFI_SUCCESS || !handles) {
252 *why = "no block devices";
253 return -1;
254 }
255 for (uint64_t i = 0; i < n; i++) {
256 EFI_BLOCK_IO_PROTOCOL *bio = 0;
257 struct slots_part c;
258 const char *w = 0;
259 if (bs->HandleProtocol(handles[i], &bio_guid, (void **)&bio) != EFI_SUCCESS || !bio || !bio->Media)
260 continue;
261 const EFI_BLOCK_IO_MEDIA *m = bio->Media;
262 if (!m->MediaPresent || m->LogicalPartition || (m->BlockSize != 512 && m->BlockSize != 4096) ||
263 m->LastBlock < 34 || m->IoAlign > PAGE)
264 continue;
265 int rc = gpt_scan(bio, &c, &w);
266 if (rc < 0) {
267 say("slot_loader: disk %llu: GPT refused (%s)\n", (unsigned long long)i, w);
268 continue;
269 }
270 if (rc > 0 && total == 0)
271 *sp = c;
272 total += rc;
273 }
274 bs->FreePool(handles);
275 if (total == 0) {
276 *why = "no slots partition (type " CK_SLOT_PART_TYPE_TEXT ")";
277 return -1;
278 }
279 if (total > 1) {
280 *why = "more than one slots partition";
281 return -1;
282 }
283 return 0;
284}
285
286/* ---- torn-slot device over the whole disk (absolute LBAs) ----------------- */
287static int dev_read(void *ctx, uint64_t lba, uint32_t count, uint8_t *buf)
288{
289 struct slots_part *sp = ctx;
290 uint64_t n = (uint64_t)count * sp->bs;
291 if (n == 0 || n > sizeof bounce)
292 return -1;
293 if (sp->bio->ReadBlocks(sp->bio, sp->bio->Media->MediaId, lba, n, bounce) != EFI_SUCCESS)
294 return -1;
295 memcpy(buf, bounce, (size_t)n);
296 return 0;
297}
298
299static int dev_write(void *ctx, uint64_t lba, uint32_t count, const uint8_t *buf)
300{
301 struct slots_part *sp = ctx;
302 uint64_t n = (uint64_t)count * sp->bs;
303 write_blocks_fn wr = (write_blocks_fn)sp->bio->WriteBlocks;
304 if (n == 0 || n > sizeof bounce || !wr || sp->bio->Media->ReadOnly)
305 return -1;
306 /* Only the control region (units 0..3) is ever written. */
307 if (lba < sp->first || lba + count > sp->first + CK_SLOT_CONTROL_UNITS * (CK_SLOT_UNIT / sp->bs))
308 return -1;
309 memcpy(bounce, buf, (size_t)n);
310 return wr(sp->bio, sp->bio->Media->MediaId, lba, n, bounce) == EFI_SUCCESS ? 0 : -1;
311}
312
313static int dev_flush(void *ctx)
314{
315 struct slots_part *sp = ctx;
316 flush_blocks_fn fl = (flush_blocks_fn)sp->bio->FlushBlocks;
317 return fl && fl(sp->bio) == EFI_SUCCESS ? 0 : -1;
318}
319
320/* ---- slots -------------------------------------------------------------------- */
321/* Read image_len bytes of slot s into fresh pages; 0 when the manifest is not
322 * structurally readable or the image does not fit (ck_slot_verify says why). */
323static uint8_t *read_image(const struct slots_part *sp, uint64_t lba, const struct ck_slot_manifest *m,
324 uint64_t cap, uint64_t *pages_out, const char **why)
325{
326 const EFI_BLOCK_IO_MEDIA *md = sp->bio->Media;
327 uint64_t bytes = (m->image_len + sp->bs - 1) / sp->bs * sp->bs;
328 uint64_t pages = (bytes + PAGE - 1) / PAGE, base = 0, off = 0;
329 *pages_out = 0;
330 if (ck_slot_manifest_check(m, why) != 0 || m->image_len > cap)
331 return 0;
332 if (bs->AllocatePages(AllocateAnyPages, EfiLoaderData, pages, &base) != EFI_SUCCESS || !base) {
333 *why = "AllocatePages failed";
334 return 0;
335 }
336 while (off < bytes) {
337 uint64_t n = bytes - off > READ_CHUNK ? READ_CHUNK : bytes - off;
338 if (sp->bio->ReadBlocks(sp->bio, md->MediaId, lba + off / sp->bs, n, (void *)(uintptr_t)(base + off)) !=
339 EFI_SUCCESS) {
340 bs->FreePages(base, pages);
341 *why = "image read failed";
342 return 0;
343 }
344 off += n;
345 }
346 *pages_out = pages;
347 return (uint8_t *)(uintptr_t)base;
348}
349
350static const char *unusable_why(const struct ck_slot_eval *e, const struct ck_slot_control *ctl, unsigned s)
351{
352 if (!e->valid)
353 return e->why ? e->why : "invalid";
354 if (ctl && e->generation < ctl->generation_floor)
355 return "below the generation floor";
356 if (ctl && ctl->candidate_slot == s && ctl->tries_remaining == 0)
357 return "candidate out of tries";
358 if (ctl && ctl->candidate_slot == s)
359 return "candidate, active unusable: never boots on its own authority";
360 return "not selectable";
361}
362
363/* ---- boot record for the kernel (P11 cut 2b, slot_format.h) ------------------ */
364/* One page of the OS-loader memory type CK_SLOT_BOOT_MEMTYPE: it survives
365 * ExitBootServices and shows up in the final memory map the unchanged stub
366 * copies into the handoff. If the allocation fails the image still boots;
367 * its kernel then finds no record and writes nothing (no promotion, no
368 * abandon): fail closed, the tries counter still bounds a candidate. */
369static void hand_over(unsigned b, const struct ck_slot_decision *d, const struct ck_slot_control *ctl,
370 uint64_t seq, const struct ck_slot_manifest *m)
371{
372 uint64_t pa = 0;
373 struct ck_slot_boot_record *br;
374 const char *c = AIENOS_COMMIT;
375 EFI_STATUS s = bs->AllocatePages(AllocateAnyPages, CK_SLOT_BOOT_MEMTYPE, 1, &pa);
376 if (s != EFI_SUCCESS || !pa) {
377 say("slot_loader: boot record NOT handed over (AllocatePages status=0x%llx): the kernel will write nothing\n",
378 (unsigned long long)s);
379 return;
380 }
381 br = (struct ck_slot_boot_record *)(uintptr_t)pa;
382 memset(br, 0, PAGE);
383 memcpy(br->magic, CK_SLOT_BOOT_MAGIC, 8);
384 br->version = CK_SLOT_BOOT_VERSION;
385 br->boot_slot = (uint8_t)b;
386 br->reason = (uint8_t)d->reason;
387 br->tries = d->loader_write ? d->ctl_out.tries_remaining : (ctl ? ctl->tries_remaining : 0);
388 br->control_present = ctl ? 1 : 0;
389 br->control_seq = ctl ? seq : 0;
390 br->generation = m->generation;
391 br->policy_generation = m->policy_generation;
392 memcpy(br->image_sha512, m->image_sha512, 64);
393 for (unsigned i = 0; i < sizeof br->loader_commit && c[i]; i++)
394 br->loader_commit[i] = (uint8_t)c[i];
395 ck_slot_boot_seal(br);
396 say("slot_loader: boot record handed over type=0x%x slot=%c reason=%s seq=%llu\n", CK_SLOT_BOOT_MEMTYPE, 'A' + b,
397 ck_slot_reason_text(d->reason), (unsigned long long)br->control_seq);
398}
399
400EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *st)
401{
402 static const EFI_GUID lidp_guid = LOADED_IMAGE_DEVICE_PATH_GUID_INIT;
403 struct slots_part sp;
404 struct ck_slot_eval ev[CK_SLOT_COUNT];
405 struct ck_slot_decision d;
406 const struct ck_slot_control *ctl = 0;
407 uint8_t *img[CK_SLOT_COUNT] = {0, 0};
408 uint64_t pages[CK_SLOT_COUNT] = {0, 0};
409 const char *why = 0;
410 ts_device dev;
411 ts_region r;
412 ts_slot_state tss[2];
413 uint64_t seq = 0;
414 int trc;
415
416 conout = st->ConOut;
417 bs = st->BootServices;
418 say("slot_loader: AIENOS SLOTLDR v1 commit=%s keys=%u\n", AIENOS_COMMIT, ck_slot_loader_nkeys);
419 say("slot_loader: %s\n", ck_slot_loader_key_note);
420 if (ck_slot_loader_nkeys == 0) {
421 say("slot_loader: recovery_required (no trusted keys built in)\n");
422 return EFI_LOAD_ERROR;
423 }
424 if (find_slots(&sp, &why) != 0) {
425 say("slot_loader: recovery_required (%s)\n", why);
426 return EFI_LOAD_ERROR;
427 }
428 uint64_t units = (sp.last - sp.first + 1) * sp.bs / CK_SLOT_UNIT;
429 uint64_t cap_units = ck_slot_cap_units(units), bpu = CK_SLOT_UNIT / sp.bs;
430 uint64_t image_cap = cap_units >= CK_SLOT_MIN_CAP_UNITS ? (cap_units - 1) * CK_SLOT_UNIT : 0;
431 say("slot_loader: slots partition first_lba=%llu last_lba=%llu block=%u units=%llu cap_units=%llu\n",
432 (unsigned long long)sp.first, (unsigned long long)sp.last, sp.bs, (unsigned long long)units,
433 (unsigned long long)cap_units);
434 if (cap_units < CK_SLOT_MIN_CAP_UNITS) {
435 say("slot_loader: recovery_required (slots partition too small)\n");
436 return EFI_LOAD_ERROR;
437 }
438
439 /* Control record (torn-slot region, units 0..3). */
440 dev.ctx = &sp;
441 dev.block_size = sp.bs;
442 dev.block_count = sp.bio->Media->LastBlock + 1;
443 dev.read = dev_read;
444 dev.write = dev_write;
445 dev.flush = dev_flush;
446 trc = ts_open(&r, &dev, sp.first);
447 if (trc == TS_OK)
448 trc = ts_recover(&r, (uint8_t *)&ctl_rec, &seq, tss);
449 if (trc == TS_OK) {
450 if (ck_slot_control_check(&ctl_rec, &why) == 0) {
451 ctl = &ctl_rec;
452 say("slot_loader: control seq=%llu active=%c candidate=%c tries=%u flags=0x%x floor=%llu last_good=%llu\n",
453 (unsigned long long)seq, 'A' + ctl->active_slot,
454 ctl->candidate_slot == CK_SLOT_NONE ? '-' : 'A' + ctl->candidate_slot, ctl->tries_remaining,
455 ctl->flags, (unsigned long long)ctl->generation_floor,
456 (unsigned long long)ctl->last_good_generation);
457 } else {
458 say("slot_loader: control record refused (%s): recovery required, nothing will be written\n", why);
459 }
460 } else if (trc == TS_EMPTY) {
461 say("slot_loader: control none committed: recovery required, nothing will be written\n");
462 } else {
463 say("slot_loader: control region unreadable or corrupt (rc=%d): recovery required, nothing will be written\n", trc);
464 }
465
466 /* Both slots: manifest, image, verification. */
467 for (unsigned s = 0; s < CK_SLOT_COUNT; s++) {
468 uint64_t lba = sp.first + ck_slot_manifest_unit(s, cap_units) * bpu;
469 const char *rw = 0;
470 ev[s].valid = 0;
471 ev[s].generation = ev[s].policy_generation = 0;
472 ev[s].why = 0;
473 if (sp.bio->ReadBlocks(sp.bio, sp.bio->Media->MediaId, lba, CK_SLOT_UNIT, &man[s]) != EFI_SUCCESS) {
474 ev[s].why = "manifest read failed";
475 } else {
476 img[s] = read_image(&sp, lba + bpu, &man[s], image_cap, &pages[s], &rw);
477 if (!img[s] && rw && ck_slot_manifest_check(&man[s], &why) == 0 && man[s].image_len <= image_cap)
478 ev[s].why = rw; /* allocation or read failure */
479 else
480 ck_slot_verify(&man[s], img[s], image_cap, ck_slot_loader_keys, ck_slot_loader_nkeys, &ev[s]);
481 }
482 if (ev[s].valid)
483 say("slot_loader: slot %c valid gen=%llu policy=%llu len=%llu%s\n", 'A' + s,
484 (unsigned long long)ev[s].generation, (unsigned long long)ev[s].policy_generation,
485 (unsigned long long)man[s].image_len,
486 ctl && ev[s].generation < ctl->generation_floor ? " (below the generation floor)" : "");
487 else
488 say("slot_loader: slot %c refused (%s)\n", 'A' + s, ev[s].why ? ev[s].why : "invalid");
489 }
490
491 ck_slot_select(ctl, ev, &d);
492 if (d.loader_write) {
493 uint64_t bseq = 0;
494 int crc = ck_slot_control_commit(&r, &d.ctl_out, &bseq);
495 if (crc == TS_OK)
496 seq = bseq;
497 if (crc != TS_OK) {
498 unsigned c = (unsigned)d.boot_slot;
499 say("slot_loader: control commit failed (rc=%d): the candidate is not booted\n", crc);
500 ev[c].valid = 0;
501 ev[c].why = "control commit failed";
502 ck_slot_select(ctl, ev, &d);
503 } else {
504 say("slot_loader: control committed seq=%llu tries=%u\n", (unsigned long long)bseq,
505 d.ctl_out.tries_remaining);
506 }
507 }
508 if (d.boot_slot < 0) {
509 say("slot_loader: recovery_required (A: %s; B: %s)\n", unusable_why(&ev[0], ctl, 0),
510 unusable_why(&ev[1], ctl, 1));
511 for (unsigned s = 0; s < CK_SLOT_COUNT; s++)
512 if (img[s])
513 bs->FreePages((uint64_t)(uintptr_t)img[s], pages[s]);
514 return EFI_LOAD_ERROR;
515 }
516
517 unsigned b = (unsigned)d.boot_slot;
518 unsigned tries = d.loader_write ? d.ctl_out.tries_remaining : (ctl ? ctl->tries_remaining : 0);
519 say("slot_loader: boot=%c reason=%s gen=%llu tries=%u floor=%llu\n", 'A' + b, ck_slot_reason_text(d.reason),
520 (unsigned long long)ev[b].generation, tries, (unsigned long long)(ctl ? ctl->generation_floor : 0));
521 if (img[b ^ 1u])
522 bs->FreePages((uint64_t)(uintptr_t)img[b ^ 1u], pages[b ^ 1u]);
523
524 hand_over(b, &d, ctl, seq, &man[b]);
525 void *dp = 0;
526 if (bs->HandleProtocol(image, &lidp_guid, &dp) != EFI_SUCCESS)
527 dp = 0;
528 EFI_HANDLE child = 0;
529 EFI_STATUS s = ((load_image_fn)bs->LoadImage)(0, image, dp, img[b], man[b].image_len, &child);
530 bs->FreePages((uint64_t)(uintptr_t)img[b], pages[b]);
531 if (s != EFI_SUCCESS || !child) {
532 say("slot_loader: LoadImage of slot %c status=0x%llx: not booted\n", 'A' + b, (unsigned long long)s);
533 return EFI_LOAD_ERROR;
534 }
535 say("slot_loader: starting slot %c\n", 'A' + b);
536 s = ((start_image_fn)bs->StartImage)(child, 0, 0);
537 say("slot_loader: slot %c returned status=0x%llx\n", 'A' + b, (unsigned long long)s);
538 return s;
539}