199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2013 The FreeBSD Foundation
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This software was developed by Benno Rice under sponsorship from
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the FreeBSD Foundation.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <bootstrap.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/endian.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stand.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <efi.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <efilib.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <efiuga.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <efipciio.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <machine/metadata.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "framebuffer.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic EFI_GUID gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic EFI_GUID pciio_guid = EFI_PCI_IO_PROTOCOL_GUID;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic EFI_GUID uga_guid = EFI_UGA_DRAW_PROTOCOL_GUID;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic u_int
199767f8919635c4928607450d9e0abb932109ceToomas Soomeefifb_color_depth(struct efi_fb *efifb)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t mask;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int depth;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome mask = efifb->fb_mask_red | efifb->fb_mask_green |
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_mask_blue | efifb->fb_mask_reserved;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (mask == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (depth = 1; mask != 1; depth++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome mask >>= 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (depth);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomeefifb_mask_from_pixfmt(struct efi_fb *efifb, EFI_GRAPHICS_PIXEL_FORMAT pixfmt,
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_PIXEL_BITMASK *pixinfo)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int result;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome result = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch (pixfmt) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case PixelRedGreenBlueReserved8BitPerColor:
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_mask_red = 0x000000ff;
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_mask_green = 0x0000ff00;
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_mask_blue = 0x00ff0000;
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_mask_reserved = 0xff000000;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case PixelBlueGreenRedReserved8BitPerColor:
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_mask_red = 0x00ff0000;
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_mask_green = 0x0000ff00;
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_mask_blue = 0x000000ff;
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_mask_reserved = 0xff000000;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case PixelBitMask:
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_mask_red = pixinfo->RedMask;
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_mask_green = pixinfo->GreenMask;
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_mask_blue = pixinfo->BlueMask;
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_mask_reserved = pixinfo->ReservedMask;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome default:
199767f8919635c4928607450d9e0abb932109ceToomas Soome result = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (result);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomeefifb_from_gop(struct efi_fb *efifb, EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE *mode,
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int result;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_addr = mode->FrameBufferBase;
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_size = mode->FrameBufferSize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_height = info->VerticalResolution;
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_width = info->HorizontalResolution;
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_stride = info->PixelsPerScanLine;
199767f8919635c4928607450d9e0abb932109ceToomas Soome result = efifb_mask_from_pixfmt(efifb, info->PixelFormat,
199767f8919635c4928607450d9e0abb932109ceToomas Soome &info->PixelInformation);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (result);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic ssize_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomeefifb_uga_find_pixel(EFI_UGA_DRAW_PROTOCOL *uga, u_int line,
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_PCI_IO_PROTOCOL *pciio, uint64_t addr, uint64_t size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_UGA_PIXEL pix0, pix1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint8_t *data1, *data2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t count, maxcount = 1024;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssize_t ofs;
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_STATUS status;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int idx;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = uga->Blt(uga, &pix0, EfiUgaVideoToBltBuffer,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 0, line, 0, 0, 1, 1, 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (EFI_ERROR(status)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("UGA BLT operation failed (video->buffer)");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome pix1.Red = ~pix0.Red;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pix1.Green = ~pix0.Green;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pix1.Blue = ~pix0.Blue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pix1.Reserved = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome data1 = calloc(maxcount, 2);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (data1 == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Unable to allocate memory");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome data2 = data1 + maxcount;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome ofs = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (size > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome count = min(size, maxcount);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = pciio->Mem.Read(pciio, EfiPciIoWidthUint32,
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_PCI_IO_PASS_THROUGH_BAR, addr + ofs, count >> 2,
199767f8919635c4928607450d9e0abb932109ceToomas Soome data1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (EFI_ERROR(status)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Error reading frame buffer (before)");
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto fail;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = uga->Blt(uga, &pix1, EfiUgaBltBufferToVideo,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 0, 0, 0, line, 1, 1, 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (EFI_ERROR(status)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("UGA BLT operation failed (modify)");
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto fail;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = pciio->Mem.Read(pciio, EfiPciIoWidthUint32,
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_PCI_IO_PASS_THROUGH_BAR, addr + ofs, count >> 2,
199767f8919635c4928607450d9e0abb932109ceToomas Soome data2);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (EFI_ERROR(status)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Error reading frame buffer (after)");
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto fail;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = uga->Blt(uga, &pix0, EfiUgaBltBufferToVideo,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 0, 0, 0, line, 1, 1, 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (EFI_ERROR(status)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("UGA BLT operation failed (restore)");
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto fail;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (idx = 0; idx < count; idx++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (data1[idx] != data2[idx]) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(data1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ofs + (idx & ~3));
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome ofs += count;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size -= count;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("No change detected in frame buffer");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fail:
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" -- error %lu\n", EFI_ERROR_CODE(status));
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(data1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic EFI_PCI_IO_PROTOCOL *
199767f8919635c4928607450d9e0abb932109ceToomas Soomeefifb_uga_get_pciio(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_PCI_IO_PROTOCOL *pciio;
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_HANDLE *buf, *hp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_STATUS status;
199767f8919635c4928607450d9e0abb932109ceToomas Soome UINTN bufsz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Get all handles that support the UGA protocol. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome bufsz = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = BS->LocateHandle(ByProtocol, &uga_guid, NULL, &bufsz, NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (status != EFI_BUFFER_TOO_SMALL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf = malloc(bufsz);
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = BS->LocateHandle(ByProtocol, &uga_guid, NULL, &bufsz, buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (status != EFI_SUCCESS) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome bufsz /= sizeof(EFI_HANDLE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Get the PCI I/O interface of the first handle that supports it. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome pciio = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (hp = buf; hp < buf + bufsz; hp++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = BS->HandleProtocol(*hp, &pciio_guid, (void **)&pciio);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (status == EFI_SUCCESS) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (pciio);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic EFI_STATUS
199767f8919635c4928607450d9e0abb932109ceToomas Soomeefifb_uga_locate_framebuffer(EFI_PCI_IO_PROTOCOL *pciio, uint64_t *addrp,
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t *sizep)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint8_t *resattr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t addr, size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_STATUS status;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int bar;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pciio == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EFI_DEVICE_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Attempt to get the frame buffer address (imprecise). */
199767f8919635c4928607450d9e0abb932109ceToomas Soome *addrp = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *sizep = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (bar = 0; bar < 6; bar++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = pciio->GetBarAttributes(pciio, bar, NULL,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (void **)&resattr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (status != EFI_SUCCESS)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* XXX magic offsets and constants. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (resattr[0] == 0x87 && resattr[3] == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* 32-bit address space descriptor (MEMIO) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr = le32dec(resattr + 10);
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = le32dec(resattr + 22);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (resattr[0] == 0x8a && resattr[3] == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* 64-bit address space descriptor (MEMIO) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr = le64dec(resattr + 14);
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = le64dec(resattr + 38);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome BS->FreePool(resattr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (addr == 0 || size == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* We assume the largest BAR is the frame buffer. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (size > *sizep) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome *addrp = addr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *sizep = size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return ((*addrp == 0 || *sizep == 0) ? EFI_DEVICE_ERROR : 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomeefifb_from_uga(struct efi_fb *efifb, EFI_UGA_DRAW_PROTOCOL *uga)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_PCI_IO_PROTOCOL *pciio;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *ev, *p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_STATUS status;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssize_t offset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t fbaddr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t horiz, vert, stride;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t np, depth, refresh;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = uga->GetMode(uga, &horiz, &vert, &depth, &refresh);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (EFI_ERROR(status))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_height = vert;
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_width = horiz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Paranoia... */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (efifb->fb_height == 0 || efifb->fb_width == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* The color masks are fixed AFAICT. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb_mask_from_pixfmt(efifb, PixelBlueGreenRedReserved8BitPerColor,
199767f8919635c4928607450d9e0abb932109ceToomas Soome NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* pciio can be NULL on return! */
199767f8919635c4928607450d9e0abb932109ceToomas Soome pciio = efifb_uga_get_pciio();
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Try to find the frame buffer. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = efifb_uga_locate_framebuffer(pciio, &efifb->fb_addr,
199767f8919635c4928607450d9e0abb932109ceToomas Soome &efifb->fb_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (EFI_ERROR(status)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_addr = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_size = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * There's no reliable way to detect the frame buffer or the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * offset within the frame buffer of the visible region, nor
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the stride. Our only option is to look at the system and
199767f8919635c4928607450d9e0abb932109ceToomas Soome * fill in the blanks based on that. Luckily, UGA was mostly
199767f8919635c4928607450d9e0abb932109ceToomas Soome * only used on Apple hardware.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome offset = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ev = getenv("smbios.system.maker");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ev != NULL && !strcmp(ev, "Apple Inc.")) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome ev = getenv("smbios.system.product");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ev != NULL && !strcmp(ev, "iMac7,1")) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* These are the expected values we should have. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome horiz = 1680;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vert = 1050;
199767f8919635c4928607450d9e0abb932109ceToomas Soome fbaddr = 0xc0000000;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* These are the missing bits. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome offset = 0x10000;
199767f8919635c4928607450d9e0abb932109ceToomas Soome stride = 1728;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (ev != NULL && !strcmp(ev, "MacBook3,1")) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* These are the expected values we should have. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome horiz = 1280;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vert = 800;
199767f8919635c4928607450d9e0abb932109ceToomas Soome fbaddr = 0xc0000000;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* These are the missing bits. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome offset = 0x0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome stride = 2048;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If this is hardware we know, make sure that it looks familiar
199767f8919635c4928607450d9e0abb932109ceToomas Soome * before we accept our hardcoded values.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (offset >= 0 && efifb->fb_width == horiz &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_height == vert && efifb->fb_addr == fbaddr) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_addr += offset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_size -= offset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_stride = stride;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (offset >= 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Hardware make/model known, but graphics not "
199767f8919635c4928607450d9e0abb932109ceToomas Soome "as expected.\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Console may not work!\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The stride is equal or larger to the width. Often it's the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * next larger power of two. We'll start with that...
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_stride = efifb->fb_width;
199767f8919635c4928607450d9e0abb932109ceToomas Soome do {
199767f8919635c4928607450d9e0abb932109ceToomas Soome np = efifb->fb_stride & (efifb->fb_stride - 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (np) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_stride |= (np - 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_stride++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome } while (np);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome ev = getenv("hw.efifb.address");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ev == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (efifb->fb_addr == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Please set hw.efifb.address and "
199767f8919635c4928607450d9e0abb932109ceToomas Soome "hw.efifb.stride.\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The visible part of the frame buffer may not start at
199767f8919635c4928607450d9e0abb932109ceToomas Soome * offset 0, so try to detect it. Note that we may not
199767f8919635c4928607450d9e0abb932109ceToomas Soome * always be able to read from the frame buffer, which
199767f8919635c4928607450d9e0abb932109ceToomas Soome * means that we may not be able to detect anything. In
199767f8919635c4928607450d9e0abb932109ceToomas Soome * that case, we would take a long time scanning for a
199767f8919635c4928607450d9e0abb932109ceToomas Soome * pixel change in the frame buffer, which would have it
199767f8919635c4928607450d9e0abb932109ceToomas Soome * appear that we're hanging, so we limit the scan to
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1/256th of the frame buffer. This number is mostly
199767f8919635c4928607450d9e0abb932109ceToomas Soome * based on PR 202730 and the fact that on a MacBoook,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * where we can't read from the frame buffer the offset
199767f8919635c4928607450d9e0abb932109ceToomas Soome * of the visible region is 0. In short: we want to scan
199767f8919635c4928607450d9e0abb932109ceToomas Soome * enough to handle all adapters that have an offset
199767f8919635c4928607450d9e0abb932109ceToomas Soome * larger than 0 and we want to scan as little as we can
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to not appear to hang when we can't read from the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * frame buffer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome offset = efifb_uga_find_pixel(uga, 0, pciio, efifb->fb_addr,
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_size >> 8);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (offset == -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Unable to reliably detect frame buffer.\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (offset > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_addr += offset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_size -= offset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome offset = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_size = efifb->fb_height * efifb->fb_stride * 4;
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_addr = strtoul(ev, &p, 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p != '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome ev = getenv("hw.efifb.stride");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ev == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pciio != NULL && offset != -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Determine the stride. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome offset = efifb_uga_find_pixel(uga, 1, pciio,
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_addr, horiz * 8);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (offset != -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_stride = offset >> 2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Unable to reliably detect the stride.\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_stride = strtoul(ev, &p, 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p != '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * We finalized on the stride, so recalculate the size of the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * frame buffer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_size = efifb->fb_height * efifb->fb_stride * 4;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomeefi_find_framebuffer(struct efi_fb *efifb)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_GRAPHICS_OUTPUT *gop;
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_UGA_DRAW_PROTOCOL *uga;
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_STATUS status;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = BS->LocateProtocol(&gop_guid, NULL, (VOID **)&gop);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (status == EFI_SUCCESS)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (efifb_from_gop(efifb, gop->Mode, gop->Mode->Info));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = BS->LocateProtocol(&uga_guid, NULL, (VOID **)&uga);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (status == EFI_SUCCESS)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (efifb_from_uga(efifb, uga));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomeprint_efifb(int mode, struct efi_fb *efifb, int verbose)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int depth;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (mode >= 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("mode %d: ", mode);
199767f8919635c4928607450d9e0abb932109ceToomas Soome depth = efifb_color_depth(efifb);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%ux%ux%u, stride=%u", efifb->fb_width, efifb->fb_height,
199767f8919635c4928607450d9e0abb932109ceToomas Soome depth, efifb->fb_stride);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (verbose) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\n frame buffer: address=%jx, size=%jx",
199767f8919635c4928607450d9e0abb932109ceToomas Soome (uintmax_t)efifb->fb_addr, (uintmax_t)efifb->fb_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\n color mask: R=%08x, G=%08x, B=%08x\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_mask_red, efifb->fb_mask_green,
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_mask_blue);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(gop, "gop", "graphics output protocol", command_gop);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomecommand_gop(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct efi_fb efifb;
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_GRAPHICS_OUTPUT *gop;
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_STATUS status;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int mode;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = BS->LocateProtocol(&gop_guid, NULL, (VOID **)&gop);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (EFI_ERROR(status)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "%s: Graphics Output Protocol not present (error=%lu)",
199767f8919635c4928607450d9e0abb932109ceToomas Soome argv[0], EFI_ERROR_CODE(status));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (argc < 2)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto usage;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!strcmp(argv[1], "set")) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *cp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (argc != 3)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto usage;
199767f8919635c4928607450d9e0abb932109ceToomas Soome mode = strtol(argv[2], &cp, 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cp[0] != '\0') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(command_errbuf, "mode is an integer");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = gop->SetMode(gop, mode);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (EFI_ERROR(status)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "%s: Unable to set mode to %u (error=%lu)",
199767f8919635c4928607450d9e0abb932109ceToomas Soome argv[0], mode, EFI_ERROR_CODE(status));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (!strcmp(argv[1], "get")) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (argc != 2)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto usage;
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb_from_gop(&efifb, gop->Mode, gop->Mode->Info);
199767f8919635c4928607450d9e0abb932109ceToomas Soome print_efifb(gop->Mode->Mode, &efifb, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (!strcmp(argv[1], "list")) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
199767f8919635c4928607450d9e0abb932109ceToomas Soome UINTN infosz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (argc != 2)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto usage;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_open();
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (mode = 0; mode < gop->Mode->MaxMode; mode++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = gop->QueryMode(gop, mode, &infosz, &info);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (EFI_ERROR(status))
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb_from_gop(&efifb, gop->Mode, info);
199767f8919635c4928607450d9e0abb932109ceToomas Soome print_efifb(mode, &efifb, 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pager_output("\n"))
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_close();
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome usage:
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "usage: %s [list | get | set <mode>]", argv[0]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(uga, "uga", "universal graphics adapter", command_uga);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomecommand_uga(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct efi_fb efifb;
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_UGA_DRAW_PROTOCOL *uga;
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_STATUS status;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = BS->LocateProtocol(&uga_guid, NULL, (VOID **)&uga);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (EFI_ERROR(status)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "%s: UGA Protocol not present (error=%lu)",
199767f8919635c4928607450d9e0abb932109ceToomas Soome argv[0], EFI_ERROR_CODE(status));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (argc != 1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto usage;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (efifb_from_uga(&efifb, uga) != CMD_OK) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "%s: Unable to get UGA information", argv[0]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome print_efifb(-1, &efifb, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome usage:
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf), "usage: %s", argv[0]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}