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_gop.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_gop.c -- find the UEFI Graphics Output Protocol framebuffer before |
| 18 | * ExitBootServices and record it in the CHANDOF3 handoff (fb_* fields), so |
| 19 | * the kernel can draw its report on the screen (native/kernel/core/fbcon.c). |
| 20 | * |
| 21 | * UEFI 2.10 section 12.9: "The EFI_GRAPHICS_OUTPUT_PROTOCOL also exports |
| 22 | * enough information about the current mode for operating system startup |
| 23 | * software to access the linear frame buffer directly." The stub reads the |
| 24 | * current mode only; it never calls SetMode (the screen keeps the mode the |
| 25 | * firmware chose) and never calls Blt. |
| 26 | * |
| 27 | * Choice: the GOP on ConOut's handle when it has a linear framebuffer; |
| 28 | * otherwise the first handle (LocateHandleBuffer order) whose mode is |
| 29 | * PixelRedGreenBlueReserved8BitPerColor or PixelBlueGreenRedReserved8BitPerColor |
| 30 | * with a nonzero FrameBufferBase. (edk2's console splitter may put a |
| 31 | * Blt-only GOP on the ConOut handle; that one is skipped.) PixelBitMask and |
| 32 | * PixelBltOnly modes are not used: fb_status NO_LINEAR. No GOP at all: |
| 33 | * fb_status NONE, every fb_* field zero, and the kernel boots on the UART. */ |
| 34 | #include "efi.h" |
| 35 | #include "../kernel/core/ck_internal.h" |
| 36 | |
| 37 | static int linear(const EFI_GRAPHICS_OUTPUT_PROTOCOL *g) |
| 38 | { |
| 39 | const EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE *m = g ? g->Mode : 0; |
| 40 | if (!m || !m->Info || !m->FrameBufferBase) |
| 41 | return 0; |
| 42 | return m->Info->PixelFormat == PixelRedGreenBlueReserved8BitPerColor || |
| 43 | m->Info->PixelFormat == PixelBlueGreenRedReserved8BitPerColor; |
| 44 | } |
| 45 | |
| 46 | static void record(struct ck_handoff *h, const EFI_GRAPHICS_OUTPUT_PROTOCOL *g, const char *from) |
| 47 | { |
| 48 | const EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE *m = g->Mode; |
| 49 | const EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *i = m->Info; |
| 50 | h->fb_status = CK_HANDOFF_FB_PRESENT; |
| 51 | h->fb_base = m->FrameBufferBase; |
| 52 | h->fb_size = m->FrameBufferSize; |
| 53 | h->fb_width = i->HorizontalResolution; |
| 54 | h->fb_height = i->VerticalResolution; |
| 55 | h->fb_pitch = i->PixelsPerScanLine; |
| 56 | h->fb_format = i->PixelFormat; |
| 57 | ck_printf("gop: ok from=%s handles=%u mode=%u/%u %ux%u pitch=%u format=%u base=0x%llx size=0x%llx\n", |
| 58 | from, h->fb_gop_handles, m->Mode, m->MaxMode, i->HorizontalResolution, |
| 59 | i->VerticalResolution, i->PixelsPerScanLine, i->PixelFormat, |
| 60 | (unsigned long long)m->FrameBufferBase, (unsigned long long)m->FrameBufferSize); |
| 61 | } |
| 62 | |
| 63 | void ck_boot_gop_find(EFI_SYSTEM_TABLE *st, struct ck_handoff *h) |
| 64 | { |
| 65 | static const EFI_GUID gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID_INIT; |
| 66 | EFI_BOOT_SERVICES *bs = st->BootServices; |
| 67 | EFI_HANDLE *handles = 0; |
| 68 | uint64_t n = 0; |
| 69 | EFI_STATUS s = bs->LocateHandleBuffer(ByProtocol, &gop_guid, 0, &n, &handles); |
| 70 | if (s != EFI_SUCCESS || !n || !handles) { |
| 71 | ck_printf("gop: none (LocateHandleBuffer status=0x%llx handles=%llu)\n", (unsigned long long)s, |
| 72 | (unsigned long long)n); |
| 73 | return; /* fb_status NONE, every fb_* field still zero (BSS) */ |
| 74 | } |
| 75 | h->fb_gop_handles = n > 0xffffffffull ? 0xffffffffu : (uint32_t)n; |
| 76 | |
| 77 | EFI_GRAPHICS_OUTPUT_PROTOCOL *g = 0; |
| 78 | if (st->ConsoleOutHandle && |
| 79 | bs->HandleProtocol(st->ConsoleOutHandle, &gop_guid, (void **)&g) == EFI_SUCCESS && linear(g)) { |
| 80 | bs->FreePool(handles); |
| 81 | record(h, g, "conout"); |
| 82 | return; |
| 83 | } |
| 84 | uint32_t first_format = PixelFormatMax; |
| 85 | for (uint64_t k = 0; k < n; k++) { |
| 86 | g = 0; |
| 87 | if (bs->HandleProtocol(handles[k], &gop_guid, (void **)&g) != EFI_SUCCESS || !g) |
| 88 | continue; |
| 89 | if (linear(g)) { |
| 90 | bs->FreePool(handles); |
| 91 | record(h, g, "handle"); |
| 92 | return; |
| 93 | } |
| 94 | if (first_format == PixelFormatMax && g->Mode && g->Mode->Info) |
| 95 | first_format = g->Mode->Info->PixelFormat; |
| 96 | } |
| 97 | bs->FreePool(handles); |
| 98 | h->fb_status = CK_HANDOFF_FB_NO_LINEAR; |
| 99 | ck_printf("gop: no linear framebuffer (handles=%u first_format=%u)\n", h->fb_gop_handles, first_format); |
| 100 | } |