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_klog.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_klog.c -- read \EFI\AIENOS\KLOG.MAP from the boot volume before |
| 18 | * ExitBootServices and find the card that carries the matching log slot, |
| 19 | * through the firmware's own Block I/O (Raspberry Pi 5 build, CK_KLOG). |
| 20 | * The kernel later writes its console log into that slot with its own SD |
| 21 | * driver (native/kernel/core/klog_flush.c), after checking the slot header |
| 22 | * again itself. Here nothing is written: this file only reads. |
| 23 | * |
| 24 | * Result in ck_klog_boot (shared with the kernel: the stub and the kernel |
| 25 | * are one image). state CK_KLOG_BOOT_ABSENT (no map: not an error), |
| 26 | * _REFUSED (printed why), _FOUND (map checked and the slot header read back |
| 27 | * from exactly one whole disk). */ |
| 28 | #include "efi.h" |
| 29 | #include "../kernel/core/klog_slot.h" |
| 30 | #include "../kernel/core/klog_boot.h" |
| 31 | #include "../kernel/arch/arch.h" |
| 32 | #include "../kernel/core/ck_internal.h" |
| 33 | |
| 34 | struct ck_klog_boot ck_klog_boot; |
| 35 | static struct ck_klog_hdr sector __attribute__((aligned(4096))); |
| 36 | |
| 37 | static int read_map(EFI_HANDLE image, EFI_BOOT_SERVICES *bs, struct ck_klog_map *m) |
| 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 | 'K', 'L', 'O', 'G', '.', 'M', 'A', 'P', 0}; |
| 43 | EFI_LOADED_IMAGE_PROTOCOL *li = 0; |
| 44 | EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *fs = 0; |
| 45 | EFI_FILE_PROTOCOL *root = 0, *f = 0; |
| 46 | if (bs->HandleProtocol(image, &li_guid, (void **)&li) != EFI_SUCCESS || !li || !li->DeviceHandle) |
| 47 | return 1; |
| 48 | if (bs->HandleProtocol(li->DeviceHandle, &fs_guid, (void **)&fs) != EFI_SUCCESS || !fs) |
| 49 | return 1; |
| 50 | if (fs->OpenVolume(fs, &root) != EFI_SUCCESS || !root) |
| 51 | return 1; |
| 52 | if (root->Open(root, &f, path, EFI_FILE_MODE_READ, 0) != EFI_SUCCESS || !f) { |
| 53 | root->Close(root); |
| 54 | return 1; |
| 55 | } |
| 56 | uint64_t n = sizeof *m; |
| 57 | memset(m, 0, sizeof *m); |
| 58 | EFI_STATUS s = f->Read(f, &n, m); |
| 59 | f->Close(f); |
| 60 | root->Close(root); |
| 61 | if (s != EFI_SUCCESS || n != sizeof *m) { |
| 62 | ck_printf("klog_map: refused (read status=0x%llx bytes=%llu, want %u)\n", (unsigned long long)s, |
| 63 | (unsigned long long)n, (unsigned)sizeof *m); |
| 64 | return -1; |
| 65 | } |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | void ck_boot_klog_find(EFI_HANDLE image, EFI_SYSTEM_TABLE *st) |
| 70 | { |
| 71 | static const EFI_GUID bio_guid = EFI_BLOCK_IO_PROTOCOL_GUID_INIT; |
| 72 | EFI_BOOT_SERVICES *bs = st->BootServices; |
| 73 | struct ck_klog_boot *kb = &ck_klog_boot; |
| 74 | const char *why = 0; |
| 75 | memset(kb, 0, sizeof *kb); |
| 76 | int rc = read_map(image, bs, &kb->map); |
| 77 | if (rc > 0) { |
| 78 | kb->state = CK_KLOG_BOOT_ABSENT; |
| 79 | ck_puts("klog_map: absent (no " CK_KLOG_MAP_PATH "); the log stays on the console only\n"); |
| 80 | return; |
| 81 | } |
| 82 | kb->state = CK_KLOG_BOOT_REFUSED; |
| 83 | if (rc < 0) |
| 84 | return; |
| 85 | if (ck_klog_map_check(&kb->map, 0, &why)) { |
| 86 | ck_printf("klog_map: refused (%s)\n", why); |
| 87 | return; |
| 88 | } |
| 89 | EFI_HANDLE *handles = 0; |
| 90 | uint64_t n = 0; |
| 91 | if (bs->LocateHandleBuffer(ByProtocol, &bio_guid, 0, &n, &handles) != EFI_SUCCESS || !handles) { |
| 92 | ck_puts("klog_map: refused (no Block I/O handles)\n"); |
| 93 | return; |
| 94 | } |
| 95 | unsigned matches = 0; |
| 96 | uint64_t last = 0; |
| 97 | for (uint64_t i = 0; i < n; i++) { |
| 98 | EFI_BLOCK_IO_PROTOCOL *bio = 0; |
| 99 | if (bs->HandleProtocol(handles[i], &bio_guid, (void **)&bio) != EFI_SUCCESS || !bio || !bio->Media) |
| 100 | continue; |
| 101 | const EFI_BLOCK_IO_MEDIA *md = bio->Media; |
| 102 | if (!md->MediaPresent || md->LogicalPartition || md->BlockSize != 512u || |
| 103 | md->LastBlock < kb->map.lba + kb->map.count - 1u) |
| 104 | continue; |
| 105 | if (bio->ReadBlocks(bio, md->MediaId, kb->map.lba, 512u, §or) != EFI_SUCCESS) |
| 106 | continue; |
| 107 | if (ck_klog_hdr_match(§or, &kb->map, &why) == 0) { |
| 108 | matches++; |
| 109 | last = md->LastBlock; |
| 110 | } |
| 111 | } |
| 112 | bs->FreePool(handles); |
| 113 | if (matches != 1) { |
| 114 | ck_printf("klog_map: refused (%u disks carry the slot header, want exactly 1; %llu Block I/O handles)\n", |
| 115 | matches, (unsigned long long)n); |
| 116 | return; |
| 117 | } |
| 118 | kb->state = CK_KLOG_BOOT_FOUND; |
| 119 | kb->disk_last_block = last; |
| 120 | ck_printf("klog_map: slot lba=%llu sectors=%llu on a disk of %llu sectors (firmware read back the header)\n", |
| 121 | (unsigned long long)kb->map.lba, (unsigned long long)kb->map.count, |
| 122 | (unsigned long long)(last + 1u)); |
| 123 | } |