Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/stand/efiboot efiboot: Add efigop.c



details:   https://anonhg.NetBSD.org/src/rev/ba11041088dd
branches:  trunk
changeset: 987491:ba11041088dd
user:      jmcneill <jmcneill%NetBSD.org@localhost>
date:      Tue Sep 28 11:38:07 2021 +0000

description:
efiboot: Add efigop.c

diffstat:

 sys/stand/efiboot/efigop.c |  141 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 141 insertions(+), 0 deletions(-)

diffs (145 lines):

diff -r b0f6d9d507c8 -r ba11041088dd sys/stand/efiboot/efigop.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/stand/efiboot/efigop.c        Tue Sep 28 11:38:07 2021 +0000
@@ -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@localhost>
+ * 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);
+       }
+}



Home | Main Index | Thread Index | Old Index