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 / efi_unlock.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 | /* efi_unlock.c -- read \EFI\AIENOS\UNLOCK.REQ from the boot volume before |
| 18 | * ExitBootServices (Raspberry Pi 5 qualification image, CK_PI5_UNLOCK_CARD). |
| 19 | * Nothing is written. The file holds a THROWAWAY recovery secret placed by the |
| 20 | * run tool (see native/kernel/core/unlock_req.h); the secret is never printed. |
| 21 | * Result in ck_unlock_req_boot (stub and kernel are one image): ABSENT (no |
| 22 | * file: not an error, the Store stays closed), REFUSED (printed why), FOUND. */ |
| 23 | #include "efi.h" |
| 24 | #include "../kernel/core/unlock_req.h" |
| 25 | #include "../kernel/arch/arch.h" |
| 26 | #include "../kernel/core/ck_internal.h" |
| 27 | |
| 28 | static uint8_t buf[CK_UNLOCK_REQ_SIZE + 1u] __attribute__((aligned(64))); |
| 29 | |
| 30 | static void wipe_buf(void) |
| 31 | { |
| 32 | volatile uint8_t *b = buf; |
| 33 | for (unsigned i = 0; i < sizeof buf; i++) |
| 34 | b[i] = 0; |
| 35 | } |
| 36 | |
| 37 | void ck_boot_unlock_req_find(EFI_HANDLE image, EFI_SYSTEM_TABLE *st) |
| 38 | { |
| 39 | static const EFI_GUID li_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID_INIT; |
| 40 | static const EFI_GUID fs_guid = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID_INIT; |
| 41 | static CHAR16 path[] = {'\\', 'E', 'F', 'I', '\\', 'A', 'I', 'E', 'N', 'O', 'S', '\\', |
| 42 | 'U', 'N', 'L', 'O', 'C', 'K', '.', 'R', 'E', 'Q', 0}; |
| 43 | EFI_BOOT_SERVICES *bs = st->BootServices; |
| 44 | EFI_LOADED_IMAGE_PROTOCOL *li = 0; |
| 45 | EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *fs = 0; |
| 46 | EFI_FILE_PROTOCOL *root = 0, *f = 0; |
| 47 | ck_unlock_req_boot.state = CK_UNLOCK_REQ_ABSENT; |
| 48 | if (bs->HandleProtocol(image, &li_guid, (void **)&li) != EFI_SUCCESS || !li || !li->DeviceHandle || |
| 49 | bs->HandleProtocol(li->DeviceHandle, &fs_guid, (void **)&fs) != EFI_SUCCESS || !fs || |
| 50 | fs->OpenVolume(fs, &root) != EFI_SUCCESS || !root) { |
| 51 | ck_puts("unlock_req: absent (boot volume not readable); the Store stays closed\n"); |
| 52 | return; |
| 53 | } |
| 54 | if (root->Open(root, &f, path, EFI_FILE_MODE_READ, 0) != EFI_SUCCESS || !f) { |
| 55 | root->Close(root); |
| 56 | ck_puts("unlock_req: absent (no " CK_UNLOCK_REQ_PATH "); the Store stays closed\n"); |
| 57 | return; |
| 58 | } |
| 59 | wipe_buf(); |
| 60 | uint64_t n = sizeof buf; /* one byte more than the format: a longer file is refused */ |
| 61 | EFI_STATUS s = f->Read(f, &n, buf); |
| 62 | f->Close(f); |
| 63 | root->Close(root); |
| 64 | ck_unlock_req_boot.state = CK_UNLOCK_REQ_REFUSED; |
| 65 | if (s != EFI_SUCCESS || n != CK_UNLOCK_REQ_SIZE) { |
| 66 | ck_printf("unlock_req: refused (read status=0x%llx bytes=%llu, want exactly %u)\n", (unsigned long long)s, |
| 67 | (unsigned long long)n, (unsigned)CK_UNLOCK_REQ_SIZE); |
| 68 | wipe_buf(); |
| 69 | return; |
| 70 | } |
| 71 | const char *why = 0; |
| 72 | if (ck_unlock_req_check((const struct ck_unlock_req *)buf, &why)) { |
| 73 | ck_printf("unlock_req: refused (%s)\n", why ? why : "malformed"); |
| 74 | wipe_buf(); |
| 75 | return; |
| 76 | } |
| 77 | ck_unlock_req_store((const struct ck_unlock_req *)buf); |
| 78 | wipe_buf(); |
| 79 | ck_puts("unlock_req: found a well-formed request file (QUALIFICATION ONLY, throwaway secret); secret not printed\n"); |
| 80 | } |