1113596Snectar/*-
2113596Snectar * Copyright (c) 2013 The FreeBSD Foundation
3113596Snectar * All rights reserved.
41573Srgrimes *
5113596Snectar * This software was developed by Benno Rice under sponsorship from
6113596Snectar * the FreeBSD Foundation.
7113596Snectar * Redistribution and use in source and binary forms, with or without
8113596Snectar * modification, are permitted provided that the following conditions
9113596Snectar * are met:
10113596Snectar * 1. Redistributions of source code must retain the above copyright
111573Srgrimes *    notice, this list of conditions and the following disclaimer.
121573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
131573Srgrimes *    notice, this list of conditions and the following disclaimer in the
141573Srgrimes *    documentation and/or other materials provided with the distribution.
151573Srgrimes *
161573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
171573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
181573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
191573Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20113596Snectar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
211573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
221573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23113596Snectar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
241573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261573Srgrimes * SUCH DAMAGE.
271573Srgrimes */
281573Srgrimes
291573Srgrimes#include <sys/cdefs.h>
301573Srgrimes__FBSDID("$FreeBSD$");
31113596Snectar
321573Srgrimes#include <stand.h>
3390016Sbde
3489999Sobrien#include <efi.h>
351573Srgrimes#include <efilib.h>
36113596Snectar#include <machine/metadata.h>
371573Srgrimes
38113596Snectarstatic EFI_GUID gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
39113596Snectar
40113596Snectarint
41113596Snectarefi_find_framebuffer(struct efi_fb *efifb)
42113596Snectar{
43113596Snectar	EFI_GRAPHICS_OUTPUT			*gop;
44113596Snectar	EFI_STATUS				status;
451573Srgrimes	EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE	*mode;
4665532Snectar	EFI_GRAPHICS_OUTPUT_MODE_INFORMATION	*info;
4765532Snectar
4865532Snectar	status = BS->LocateProtocol(&gop_guid, NULL, (VOID **)&gop);
49113596Snectar	if (EFI_ERROR(status))
50113596Snectar		return (1);
51113596Snectar
52113596Snectar	mode = gop->Mode;
53113596Snectar	info = gop->Mode->Info;
54113596Snectar
5565532Snectar	efifb->fb_addr = mode->FrameBufferBase;
56113596Snectar	efifb->fb_size = mode->FrameBufferSize;
57113596Snectar	efifb->fb_height = info->VerticalResolution;
58113596Snectar	efifb->fb_width = info->HorizontalResolution;
5971579Sdeischen	efifb->fb_stride = info->PixelsPerScanLine;
60113596Snectar
61111618Snectar	switch (info->PixelFormat) {
6265532Snectar	case PixelRedGreenBlueReserved8BitPerColor:
63113596Snectar		efifb->fb_mask_red = 0x000000ff;
64158115Sume		efifb->fb_mask_green = 0x0000ff00;
65158115Sume		efifb->fb_mask_blue = 0x00ff0000;
66158115Sume		efifb->fb_mask_reserved = 0xff000000;
6765532Snectar		break;
68113596Snectar	case PixelBlueGreenRedReserved8BitPerColor:
69113596Snectar		efifb->fb_mask_red = 0x00ff0000;
70113596Snectar		efifb->fb_mask_green = 0x0000ff00;
71113596Snectar		efifb->fb_mask_blue = 0x000000ff;
7265532Snectar		efifb->fb_mask_reserved = 0xff000000;
7365532Snectar		break;
74113596Snectar	case PixelBitMask:
75113596Snectar		efifb->fb_mask_red = info->PixelInformation.RedMask;
7623668Speter		efifb->fb_mask_green = info->PixelInformation.GreenMask;
77113596Snectar		efifb->fb_mask_blue = info->PixelInformation.BlueMask;
78113596Snectar		efifb->fb_mask_reserved =
7965532Snectar		    info->PixelInformation.ReservedMask;
80113596Snectar		break;
81113596Snectar	default:
82113596Snectar		return (1);
83113596Snectar	}
84113596Snectar	return (0);
85113596Snectar}
86113596Snectar