diff options
| author | jmcneill <jmcneill@NetBSD.org> | 2021-09-28 11:38:07 +0000 |
|---|---|---|
| committer | jmcneill <jmcneill@NetBSD.org> | 2021-09-28 11:38:07 +0000 |
| commit | c9764070a2f822a4916b4e0b244707da5611ac68 (patch) | |
| tree | 5d6930d99abc7c3c4f232107cb8b0a747a364854 /sys/stand | |
| parent | 2c8545097d48c00eb920d3a2b03f0b5a93b8617f (diff) | |
efiboot: Add efigop.c
Diffstat (limited to 'sys/stand')
| -rw-r--r-- | sys/stand/efiboot/efigop.c | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/sys/stand/efiboot/efigop.c b/sys/stand/efiboot/efigop.c new file mode 100644 index 00000000000..7612547ac51 --- /dev/null +++ b/sys/stand/efiboot/efigop.c @@ -0,0 +1,141 @@ +/* $NetBSD: efigop.c,v 1.1 2021/09/28 11:38:07 jmcneill Exp $ */ + +/*- + * Copyright (c) 2021 Jared McNeill <jmcneill@invisible.ca> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "efiboot.h" + +#include <libfdt.h> + +static EFI_GRAPHICS_OUTPUT_PROTOCOL *gop = NULL; + +void +efi_gop_probe(void) +{ + EFI_HANDLE *gop_handle; + UINTN ngop_handle; + EFI_STATUS status; + + status = LibLocateHandle(ByProtocol, &GraphicsOutputProtocol, NULL, + &ngop_handle, &gop_handle); + if (EFI_ERROR(status) || ngop_handle == 0) { + return; + } + + for (size_t n = 0; n < ngop_handle; n++) { + status = uefi_call_wrapper(BS->HandleProtocol, 3, + gop_handle[n], &GraphicsOutputProtocol, (void **)&gop); + if (EFI_ERROR(status) || gop->Mode == NULL) { + gop = NULL; + continue; + } else { + break; + } + } +} + +static uint32_t +efi_gop_bpp(EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info) +{ + if (info->PixelFormat == PixelRedGreenBlueReserved8BitPerColor || + info->PixelFormat == PixelBlueGreenRedReserved8BitPerColor) { + return 24; + } + + return popcount32(info->PixelInformation.RedMask) + + popcount32(info->PixelInformation.GreenMask) + + popcount32(info->PixelInformation.BlueMask); +} + +static void +efi_gop_printmode(UINT32 mode, EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info) +{ + char buf[sizeof("NNNNN: HHHHHxVVVVV BB")]; + + snprintf(buf, sizeof(buf), "%-5u: %ux%u %u", mode, + info->HorizontalResolution, info->VerticalResolution, + efi_gop_bpp(info)); + + printf("%-21s", buf); +} + +void +efi_gop_show(void) +{ + if (gop == NULL) { + return; + } + + printf("GOP: "); + efi_gop_printmode(gop->Mode->Mode, gop->Mode->Info); + printf("\n"); +} + +void +efi_gop_dump(void) +{ + EFI_STATUS status; + EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info; + UINTN size; + + if (gop == NULL) { + return; + } + + for (UINT32 mode = 0; mode < gop->Mode->MaxMode; mode++) { + status = uefi_call_wrapper(gop->QueryMode, 4, gop, mode, + &size, &info); + if (EFI_ERROR(status)) { + continue; + } + if (mode == gop->Mode->Mode) { + printf(" -> "); + } else { + printf(" "); + } + efi_gop_printmode(mode, info); + if (mode != gop->Mode->MaxMode - 1 && + mode % 3 == 2) { + printf("\n"); + } + } + printf("\n"); +} + +void +efi_gop_setmode(UINT32 mode) +{ + EFI_STATUS status; + + if (gop == NULL) { + return; + } + + status = uefi_call_wrapper(gop->SetMode, 2, gop, mode); + if (EFI_ERROR(status)) { + printf("Failed to set video mode: %ld\n", (long)status); + } +} |
