Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/boot/efi_main.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / boot / efi_main.c` 240 lines ยท 9362 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 /* efi_main.c -- minimal AIENOS UEFI entry stub for the C kernel core. 18 * 19 * Loads nothing and checks nothing: the image already contains the kernel. 20 * It prints the pre-exit report on ConOut, captures the firmware facts the 21 * kernel needs (EL, TTBR0/SCTLR of the firmware regime, RSDP, counter), takes 22 * the final memory map, leaves boot services (retrying when the map key goes 23 * stale) and jumps to ck_kernel_entry with a handoff in the image's BSS. 24 * There are no A/B slots (ADR 0024 Q3; the Rust aienos-boot crate has none 25 * either): rollback is the firmware's one-time BootNext with BootOrder 26 * unchanged (docs/BOOT_HANDOFF_CONTRACT.md section 7). Signature checks are 27 * parked for the C path; see README.md. */ 28 #include "efi.h" 29 #include "../kernel/arch/arch.h" 30 #include "../kernel/core/ck_internal.h" 31 32 /* TEST-ONLY (AIENOS_CK_SCREEN negative control, native/kernel/Makefile 33 * CK_TEST_STALE_HANDOFF=2): hand the kernel a CHANDOF2 magic. Never in a 34 * hardware staging image. */ 35 #if defined(CK_TEST_STALE_HANDOFF) && defined(CK_HARDWARE_STAGING) 36 #error "CK_TEST_STALE_HANDOFF is TEST-only and cannot be combined with CK_HARDWARE_STAGING" 37 #endif 38 39 /* TEST-ONLY (CK gate M0_ROLLBACK, native/kernel/Makefile CK_TEST_ROLLBACK=bad-magic): 40 * flip bit 0 of the handoff magic ("CHANDOF3" -> "BHANDOF3") after 41 * ExitBootServices, so the kernel refuses the record with "handoff: bad magic" 42 * and resets. Never in a hardware staging image. */ 43 #if defined(CK_TEST_ROLLBACK_BAD_MAGIC) && defined(CK_HARDWARE_STAGING) 44 #error "CK_TEST_ROLLBACK_BAD_MAGIC is TEST-only and cannot be combined with CK_HARDWARE_STAGING" 45 #endif 46 47 #define MAP_BYTES (64u << 10) 48 #define EXIT_TRIES 8 49 50 extern char __image_base[], __image_end[]; 51 52 static uint64_t map_buf[MAP_BYTES / 8]; 53 static struct ck_handoff handoff; 54 static EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout; 55 56 int ck_boot_model_load(EFI_HANDLE image, EFI_SYSTEM_TABLE *st, struct ck_handoff *h); 57 #ifdef CK_PI5_UNLOCK_CARD 58 void ck_boot_unlock_req_find(EFI_HANDLE image, EFI_SYSTEM_TABLE *st); 59 #endif 60 #ifdef CK_PI5_FACT 61 void ck_boot_fact_req_find(EFI_HANDLE image, EFI_SYSTEM_TABLE *st); 62 #endif 63 #ifdef CK_PI5_LIFECYCLE 64 void ck_boot_agent_cmd_find(EFI_HANDLE image, EFI_SYSTEM_TABLE *st); 65 #endif 66 #ifdef CK_ARTIFACT_CARD 67 void ck_boot_artifact_card_find(EFI_HANDLE image, EFI_SYSTEM_TABLE *st); 68 #endif 69 #ifdef CK_ALLEN 70 void ck_boot_allen_find(EFI_HANDLE image, EFI_SYSTEM_TABLE *st); 71 #endif 72 #if defined(CK_KLOG) && defined(CK_PI5) /* the card log slot is Pi 5 only; the Spark RTL8127 build keeps klog in RAM */ 73 void ck_boot_klog_find(EFI_HANDLE image, EFI_SYSTEM_TABLE *st); 74 #endif 75 void ck_boot_gop_find(EFI_SYSTEM_TABLE *st, struct ck_handoff *h); 76 77 static void efi_write(const char *s, size_t n) 78 { 79 CHAR16 buf[130]; 80 size_t k = 0; 81 for (size_t i = 0; i < n; i++) { 82 if (s[i] == '\n') 83 buf[k++] = '\r'; 84 buf[k++] = (uint8_t)s[i]; 85 if (k >= 128) { 86 buf[k] = 0; 87 conout->OutputString(conout, buf); 88 k = 0; 89 } 90 } 91 if (k) { 92 buf[k] = 0; 93 conout->OutputString(conout, buf); 94 } 95 } 96 97 static int guid_eq(const EFI_GUID *a, const EFI_GUID *b) 98 { 99 return memcmp(a, b, sizeof *a) == 0; 100 } 101 102 static void vendor_print(const CHAR16 *v) 103 { 104 char b[80]; 105 size_t i = 0; 106 for (; v && v[i] && i < sizeof b - 1; i++) 107 b[i] = v[i] < 0x80 ? (char)v[i] : '?'; 108 b[i] = 0; 109 ck_printf("firmware_vendor: %s\n", b); 110 } 111 112 EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *st) 113 { 114 struct ck_handoff *h = &handoff; 115 unsigned el = ck_current_el(); 116 h->magic = CK_HANDOFF_MAGIC; 117 h->firmware_el = el; 118 h->uefi_entry_ticks = ck_rd(cntpct_el0); 119 h->counter_freq_hz = ck_rd(cntfrq_el0); 120 if (el == 2) { 121 h->firmware_ttbr0 = ck_rd(ttbr0_el2); 122 h->firmware_sctlr = ck_rd(sctlr_el2); 123 } else { 124 h->firmware_ttbr0 = ck_rd(ttbr0_el1); 125 h->firmware_sctlr = ck_rd(sctlr_el1); 126 } 127 h->image_base = (uint64_t)(uintptr_t)__image_base; 128 h->image_end = (uint64_t)(uintptr_t)__image_end; 129 h->commit = ck_commit(); 130 h->memory_map = (uint64_t)(uintptr_t)map_buf; 131 132 conout = st->ConOut; 133 if (conout) 134 ck_console_set_efi(efi_write); 135 ck_set_stage("firmware_entry"); 136 if (st->BootServices->SetWatchdogTimer) 137 st->BootServices->SetWatchdogTimer(0, 0, 0, 0); 138 139 static const EFI_GUID acpi20 = EFI_ACPI_20_TABLE_GUID_INIT; 140 for (uint64_t i = 0; i < st->NumberOfTableEntries; i++) 141 if (guid_eq(&st->ConfigurationTable[i].VendorGuid, &acpi20)) 142 h->rsdp = (uint64_t)(uintptr_t)st->ConfigurationTable[i].VendorTable; 143 144 /* SPCR now, so a panic right after ExitBootServices can still print. */ 145 struct ck_spcr spcr = {0}; 146 int uart_ok = -1; 147 const void *t = h->rsdp ? ck_acpi_lookup(h->rsdp, "SPCR") : 0; 148 if (t && ck_spcr_parse(t, &spcr) == 0) 149 uart_ok = ck_console_set_uart(&spcr); 150 151 ck_puts("\n"); 152 ck_report_header("pre_exit"); 153 /* The GOP framebuffer for the kernel's screen console (efi_gop.c, CHANDOF3). */ 154 ck_boot_gop_find(st, h); 155 #ifdef CK_TEST_STALE_HANDOFF 156 ck_puts("handoff: TEST-ONLY stale handoff image: the kernel gets a CHANDOF2 record; never counts toward a PASS\n"); 157 #endif 158 #ifdef CK_TEST_ROLLBACK_BAD_MAGIC 159 ck_puts("rollback_test: TEST-ONLY rollback candidate (bad-magic): the kernel gets a corrupted handoff magic; never counts toward a PASS\n"); 160 #endif 161 /* The yardstick model from the boot disk, while the firmware drivers are 162 * still up (efi_model.c). Absent map: boot continues without a model. */ 163 ck_boot_model_load(image, st, h); 164 #if defined(CK_KLOG) && defined(CK_PI5) 165 /* Pi 5 build: find the card's log slot while Block I/O is still up (efi_klog.c). */ 166 ck_boot_klog_find(image, st); 167 #endif 168 #ifdef CK_PI5_UNLOCK_CARD 169 /* Pi qualification unlock image: the throwaway recovery secret request file (efi_unlock.c). */ 170 ck_boot_unlock_req_find(image, st); 171 #endif 172 #ifdef CK_PI5_FACT 173 /* PI5-5 qualification image: the one-fact request file (efi_fact.c). */ 174 ck_boot_fact_req_find(image, st); 175 #endif 176 #ifdef CK_PI5_LIFECYCLE 177 /* PI5-7 cut 2 qualification image: the lifecycle command script (efi_agentcmd.c). */ 178 ck_boot_agent_cmd_find(image, st); 179 #endif 180 #ifdef CK_ARTIFACT_CARD 181 /* PI5-7 cut 3 qualification image: the card artifact bundle (efi_artifact.c). */ 182 ck_boot_artifact_card_find(image, st); 183 #endif 184 #ifdef CK_ALLEN 185 /* PI5-8 cut K ALLEN exam image: the exam case file (efi_allen.c). */ 186 ck_boot_allen_find(image, st); 187 #endif 188 ck_puts("kernel_impl: c (native/kernel, Lane 18)\n"); 189 vendor_print(st->FirmwareVendor); 190 ck_printf("firmware_revision: 0x%x\n", st->FirmwareRevision); 191 ck_printf("firmware_el: EL%u\n", el); 192 ck_printf("counter_frequency_hz: %llu\n", (unsigned long long)h->counter_freq_hz); 193 ck_printf("acpi_rsdp: 0x%llx\n", (unsigned long long)h->rsdp); 194 if (uart_ok == 0) 195 ck_printf("uart: %s spcr type=0x%x base=0x%llx\n", ck_console_uart_name(), 196 spcr.interface_type, (unsigned long long)spcr.base); 197 else 198 ck_puts("uart: unavailable (no usable SPCR)\n"); 199 ck_printf("image: 0x%llx-0x%llx\n", (unsigned long long)h->image_base, 200 (unsigned long long)h->image_end); 201 ck_puts("exiting firmware boot services\n"); 202 ck_set_stage("exit_boot_services"); 203 204 EFI_BOOT_SERVICES *bs = st->BootServices; 205 EFI_STATUS s = EFI_INVALID_PARAMETER; 206 for (unsigned tries = 1; tries <= EXIT_TRIES; tries++) { 207 uint64_t size = sizeof map_buf, key = 0, dsize = 0; 208 uint32_t dver = 0; 209 s = bs->GetMemoryMap(&size, (EFI_MEMORY_DESCRIPTOR *)map_buf, &key, &dsize, &dver); 210 if (s != EFI_SUCCESS) { 211 ck_printf("exit_boot_services: GetMemoryMap status=0x%llx size=%llu\n", 212 (unsigned long long)s, (unsigned long long)size); 213 return s; 214 } 215 h->map_size = size; 216 h->desc_size = dsize; 217 h->desc_version = dver; 218 h->exit_attempts = tries; 219 /* No ConOut between here and ExitBootServices: printing may allocate 220 * and change the map key. */ 221 s = bs->ExitBootServices(image, key); 222 if (s == EFI_SUCCESS) 223 break; 224 } 225 if (s != EFI_SUCCESS) { 226 ck_printf("exit_boot_services: failed status=0x%llx after %u tries\n", 227 (unsigned long long)s, EXIT_TRIES); 228 return s; 229 } 230 /* Boot services are gone: ConOut too. */ 231 ck_console_set_efi(0); 232 ck_set_stage("kernel_entry"); 233 #ifdef CK_TEST_STALE_HANDOFF 234 h->magic = CK_HANDOFF_MAGIC_V2; 235 #endif 236 #ifdef CK_TEST_ROLLBACK_BAD_MAGIC 237 h->magic ^= 1ull; 238 #endif 239 ck_kernel_entry(h); 240 }