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