199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2013 The FreeBSD Foundation
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
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 * 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 * 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 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 mask = efifb->fb_mask_red | efifb->fb_mask_green |
199767f8919635c4928607450d9e0abb932109ceToomas Soomeefifb_mask_from_pixfmt(struct efi_fb *efifb, EFI_GRAPHICS_PIXEL_FORMAT pixfmt,
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_mask_reserved = pixinfo->ReservedMask;
199767f8919635c4928607450d9e0abb932109ceToomas Soomeefifb_from_gop(struct efi_fb *efifb, EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE *mode,
199767f8919635c4928607450d9e0abb932109ceToomas Soome result = efifb_mask_from_pixfmt(efifb, info->PixelFormat,
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 status = uga->Blt(uga, &pix0, EfiUgaVideoToBltBuffer,
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("UGA BLT operation failed (video->buffer)");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (size > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = pciio->Mem.Read(pciio, EfiPciIoWidthUint32,
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_PCI_IO_PASS_THROUGH_BAR, addr + ofs, count >> 2,
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = uga->Blt(uga, &pix1, EfiUgaBltBufferToVideo,
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = pciio->Mem.Read(pciio, EfiPciIoWidthUint32,
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_PCI_IO_PASS_THROUGH_BAR, addr + ofs, count >> 2,
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = uga->Blt(uga, &pix0, EfiUgaBltBufferToVideo,
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" -- error %lu\n", EFI_ERROR_CODE(status));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Get all handles that support the UGA protocol. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = BS->LocateHandle(ByProtocol, &uga_guid, NULL, &bufsz, NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = BS->LocateHandle(ByProtocol, &uga_guid, NULL, &bufsz, buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Get the PCI I/O interface of the first handle that supports it. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = BS->HandleProtocol(*hp, &pciio_guid, (void **)&pciio);
199767f8919635c4928607450d9e0abb932109ceToomas Soomeefifb_uga_locate_framebuffer(EFI_PCI_IO_PROTOCOL *pciio, uint64_t *addrp,
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Attempt to get the frame buffer address (imprecise). */
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = pciio->GetBarAttributes(pciio, bar, NULL,
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* XXX magic offsets and constants. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* 32-bit address space descriptor (MEMIO) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (resattr[0] == 0x8a && resattr[3] == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* 64-bit address space descriptor (MEMIO) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* We assume the largest BAR is the frame buffer. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return ((*addrp == 0 || *sizep == 0) ? EFI_DEVICE_ERROR : 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soomeefifb_from_uga(struct efi_fb *efifb, EFI_UGA_DRAW_PROTOCOL *uga)
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = uga->GetMode(uga, &horiz, &vert, &depth, &refresh);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Paranoia... */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (efifb->fb_height == 0 || efifb->fb_width == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* The color masks are fixed AFAICT. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb_mask_from_pixfmt(efifb, PixelBlueGreenRedReserved8BitPerColor,
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* pciio can be NULL on return! */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Try to find the frame buffer. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = efifb_uga_locate_framebuffer(pciio, &efifb->fb_addr,
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 /* These are the expected values we should have. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* These are the missing bits. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (ev != NULL && !strcmp(ev, "MacBook3,1")) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* These are the expected values we should have. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* These are the missing bits. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If this is hardware we know, make sure that it looks familiar
199767f8919635c4928607450d9e0abb932109ceToomas Soome * before we accept our hardcoded values.
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_height == vert && efifb->fb_addr == fbaddr) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (offset >= 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Hardware make/model known, but graphics not "
199767f8919635c4928607450d9e0abb932109ceToomas Soome "as expected.\n");
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 np = efifb->fb_stride & (efifb->fb_stride - 1);
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 offset = efifb_uga_find_pixel(uga, 0, pciio, efifb->fb_addr,
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Unable to reliably detect frame buffer.\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (offset > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_size = efifb->fb_height * efifb->fb_stride * 4;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p != '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Determine the stride. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Unable to reliably detect the stride.\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p != '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome * We finalized on the stride, so recalculate the size of the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * frame buffer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb->fb_size = efifb->fb_height * efifb->fb_stride * 4;
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = BS->LocateProtocol(&gop_guid, NULL, (VOID **)&gop);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (efifb_from_gop(efifb, gop->Mode, gop->Mode->Info));
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = BS->LocateProtocol(&uga_guid, NULL, (VOID **)&uga);
199767f8919635c4928607450d9e0abb932109ceToomas Soomeprint_efifb(int mode, struct efi_fb *efifb, int verbose)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%ux%ux%u, stride=%u", efifb->fb_width, efifb->fb_height,
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 SoomeCOMMAND_SET(gop, "gop", "graphics output protocol", command_gop);
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = BS->LocateProtocol(&gop_guid, NULL, (VOID **)&gop);
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "%s: Graphics Output Protocol not present (error=%lu)",
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "%s: Unable to set mode to %u (error=%lu)",
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb_from_gop(&efifb, gop->Mode, gop->Mode->Info);
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (mode = 0; mode < gop->Mode->MaxMode; mode++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = gop->QueryMode(gop, mode, &infosz, &info);
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "usage: %s [list | get | set <mode>]", argv[0]);
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(uga, "uga", "universal graphics adapter", command_uga);
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = BS->LocateProtocol(&uga_guid, NULL, (VOID **)&uga);
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "%s: UGA Protocol not present (error=%lu)",
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf), "usage: %s", argv[0]);