1/* $NetBSD: efigop.c,v 1.3 2022/08/14 11:26:41 jmcneill Exp $ */
2
3/*-
4 * Copyright (c) 2021 Jared McNeill <jmcneill@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
31static EFI_GRAPHICS_OUTPUT_PROTOCOL *gop = NULL;
32
33void
34efi_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
58static uint32_t
59efi_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
71static void
72efi_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
83void
84efi_gop_show(void)
85{
86	if (gop == NULL) {
87		return;
88	}
89
90	command_printtab("GOP", "");
91	efi_gop_printmode(gop->Mode->Mode, gop->Mode->Info);
92	if (gop->Mode->FrameBufferBase != 0) {
93		printf(" (0x%lx,0x%lx)",
94		    (unsigned long)gop->Mode->FrameBufferBase,
95		    (unsigned long)gop->Mode->FrameBufferSize);
96	}
97	printf("\n");
98}
99
100void
101efi_gop_dump(void)
102{
103	EFI_STATUS status;
104	EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
105	UINTN size;
106
107	if (gop == NULL) {
108		return;
109	}
110
111	for (UINT32 mode = 0; mode < gop->Mode->MaxMode; mode++) {
112		status = uefi_call_wrapper(gop->QueryMode, 4, gop, mode,
113		    &size, &info);
114		if (EFI_ERROR(status)) {
115			continue;
116		}
117		if (mode == gop->Mode->Mode) {
118			printf(" -> ");
119		} else {
120			printf("    ");
121		}
122		efi_gop_printmode(mode, info);
123		if (mode != gop->Mode->MaxMode - 1 &&
124		    mode % 3 == 2) {
125			printf("\n");
126		}
127	}
128	printf("\n");
129}
130
131void
132efi_gop_setmode(UINT32 mode)
133{
134	EFI_STATUS status;
135
136	if (gop == NULL) {
137		return;
138	}
139
140	status = uefi_call_wrapper(gop->SetMode, 2, gop, mode);
141	if (EFI_ERROR(status)) {
142		printf("Failed to set video mode: %ld\n", (long)status);
143	}
144}
145