efigop.c revision 1.2 1 /* $NetBSD: efigop.c,v 1.2 2021/10/06 10:13:19 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2021 Jared McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include "efiboot.h"
30
31 static EFI_GRAPHICS_OUTPUT_PROTOCOL *gop = NULL;
32
33 void
34 efi_gop_probe(void)
35 {
36 EFI_HANDLE *gop_handle;
37 UINTN ngop_handle;
38 EFI_STATUS status;
39
40 status = LibLocateHandle(ByProtocol, &GraphicsOutputProtocol, NULL,
41 &ngop_handle, &gop_handle);
42 if (EFI_ERROR(status) || ngop_handle == 0) {
43 return;
44 }
45
46 for (size_t n = 0; n < ngop_handle; n++) {
47 status = uefi_call_wrapper(BS->HandleProtocol, 3,
48 gop_handle[n], &GraphicsOutputProtocol, (void **)&gop);
49 if (EFI_ERROR(status) || gop->Mode == NULL) {
50 gop = NULL;
51 continue;
52 } else {
53 break;
54 }
55 }
56 }
57
58 static uint32_t
59 efi_gop_bpp(EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info)
60 {
61 if (info->PixelFormat == PixelRedGreenBlueReserved8BitPerColor ||
62 info->PixelFormat == PixelBlueGreenRedReserved8BitPerColor) {
63 return 24;
64 }
65
66 return popcount32(info->PixelInformation.RedMask) +
67 popcount32(info->PixelInformation.GreenMask) +
68 popcount32(info->PixelInformation.BlueMask);
69 }
70
71 static void
72 efi_gop_printmode(UINT32 mode, EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info)
73 {
74 char buf[sizeof("NNNNN: HHHHHxVVVVV BB")];
75
76 snprintf(buf, sizeof(buf), "%-5u: %ux%u %u", mode,
77 info->HorizontalResolution, info->VerticalResolution,
78 efi_gop_bpp(info));
79
80 printf("%-21s", buf);
81 }
82
83 void
84 efi_gop_show(void)
85 {
86 if (gop == NULL) {
87 return;
88 }
89
90 printf("GOP: ");
91 efi_gop_printmode(gop->Mode->Mode, gop->Mode->Info);
92 printf("\n");
93 }
94
95 void
96 efi_gop_dump(void)
97 {
98 EFI_STATUS status;
99 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
100 UINTN size;
101
102 if (gop == NULL) {
103 return;
104 }
105
106 for (UINT32 mode = 0; mode < gop->Mode->MaxMode; mode++) {
107 status = uefi_call_wrapper(gop->QueryMode, 4, gop, mode,
108 &size, &info);
109 if (EFI_ERROR(status)) {
110 continue;
111 }
112 if (mode == gop->Mode->Mode) {
113 printf(" -> ");
114 } else {
115 printf(" ");
116 }
117 efi_gop_printmode(mode, info);
118 if (mode != gop->Mode->MaxMode - 1 &&
119 mode % 3 == 2) {
120 printf("\n");
121 }
122 }
123 printf("\n");
124 }
125
126 void
127 efi_gop_setmode(UINT32 mode)
128 {
129 EFI_STATUS status;
130
131 if (gop == NULL) {
132 return;
133 }
134
135 status = uefi_call_wrapper(gop->SetMode, 2, gop, mode);
136 if (EFI_ERROR(status)) {
137 printf("Failed to set video mode: %ld\n", (long)status);
138 }
139 }
140