Home | History | Annotate | Line # | Download | only in efiboot
efigop.c revision 1.1
      1 /* $NetBSD: efigop.c,v 1.1 2021/09/28 11:38:07 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 #include <libfdt.h>
     32 
     33 static EFI_GRAPHICS_OUTPUT_PROTOCOL *gop = NULL;
     34 
     35 void
     36 efi_gop_probe(void)
     37 {
     38 	EFI_HANDLE *gop_handle;
     39 	UINTN ngop_handle;
     40 	EFI_STATUS status;
     41 
     42 	status = LibLocateHandle(ByProtocol, &GraphicsOutputProtocol, NULL,
     43 	    &ngop_handle, &gop_handle);
     44 	if (EFI_ERROR(status) || ngop_handle == 0) {
     45 		return;
     46 	}
     47 
     48 	for (size_t n = 0; n < ngop_handle; n++) {
     49 		status = uefi_call_wrapper(BS->HandleProtocol, 3,
     50 		    gop_handle[n], &GraphicsOutputProtocol, (void **)&gop);
     51 		if (EFI_ERROR(status) || gop->Mode == NULL) {
     52 			gop = NULL;
     53 			continue;
     54 		} else {
     55 			break;
     56 		}
     57 	}
     58 }
     59 
     60 static uint32_t
     61 efi_gop_bpp(EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info)
     62 {
     63 	if (info->PixelFormat == PixelRedGreenBlueReserved8BitPerColor ||
     64 	    info->PixelFormat == PixelBlueGreenRedReserved8BitPerColor) {
     65 		return 24;
     66 	}
     67 
     68 	return popcount32(info->PixelInformation.RedMask) +
     69 	    popcount32(info->PixelInformation.GreenMask) +
     70 	    popcount32(info->PixelInformation.BlueMask);
     71 }
     72 
     73 static void
     74 efi_gop_printmode(UINT32 mode, EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info)
     75 {
     76 	char buf[sizeof("NNNNN: HHHHHxVVVVV BB")];
     77 
     78 	snprintf(buf, sizeof(buf), "%-5u: %ux%u %u", mode,
     79 	    info->HorizontalResolution, info->VerticalResolution,
     80 	    efi_gop_bpp(info));
     81 
     82 	printf("%-21s", buf);
     83 }
     84 
     85 void
     86 efi_gop_show(void)
     87 {
     88 	if (gop == NULL) {
     89 		return;
     90 	}
     91 
     92 	printf("GOP: ");
     93 	efi_gop_printmode(gop->Mode->Mode, gop->Mode->Info);
     94 	printf("\n");
     95 }
     96 
     97 void
     98 efi_gop_dump(void)
     99 {
    100 	EFI_STATUS status;
    101 	EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
    102 	UINTN size;
    103 
    104 	if (gop == NULL) {
    105 		return;
    106 	}
    107 
    108 	for (UINT32 mode = 0; mode < gop->Mode->MaxMode; mode++) {
    109 		status = uefi_call_wrapper(gop->QueryMode, 4, gop, mode,
    110 		    &size, &info);
    111 		if (EFI_ERROR(status)) {
    112 			continue;
    113 		}
    114 		if (mode == gop->Mode->Mode) {
    115 			printf(" -> ");
    116 		} else {
    117 			printf("    ");
    118 		}
    119 		efi_gop_printmode(mode, info);
    120 		if (mode != gop->Mode->MaxMode - 1 &&
    121 		    mode % 3 == 2) {
    122 			printf("\n");
    123 		}
    124 	}
    125 	printf("\n");
    126 }
    127 
    128 void
    129 efi_gop_setmode(UINT32 mode)
    130 {
    131 	EFI_STATUS status;
    132 
    133 	if (gop == NULL) {
    134 		return;
    135 	}
    136 
    137 	status = uefi_call_wrapper(gop->SetMode, 2, gop, mode);
    138 	if (EFI_ERROR(status)) {
    139 		printf("Failed to set video mode: %ld\n", (long)status);
    140 	}
    141 }
    142