1ca987d46SWarner Losh /*-
2ca987d46SWarner Losh * Copyright (c) 2013 The FreeBSD Foundation
3ca987d46SWarner Losh *
4ca987d46SWarner Losh * This software was developed by Benno Rice under sponsorship from
5ca987d46SWarner Losh * the FreeBSD Foundation.
6ca987d46SWarner Losh * Redistribution and use in source and binary forms, with or without
7ca987d46SWarner Losh * modification, are permitted provided that the following conditions
8ca987d46SWarner Losh * are met:
9ca987d46SWarner Losh * 1. Redistributions of source code must retain the above copyright
10ca987d46SWarner Losh * notice, this list of conditions and the following disclaimer.
11ca987d46SWarner Losh * 2. Redistributions in binary form must reproduce the above copyright
12ca987d46SWarner Losh * notice, this list of conditions and the following disclaimer in the
13ca987d46SWarner Losh * documentation and/or other materials provided with the distribution.
14ca987d46SWarner Losh *
15ca987d46SWarner Losh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16ca987d46SWarner Losh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17ca987d46SWarner Losh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18ca987d46SWarner Losh * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19ca987d46SWarner Losh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20ca987d46SWarner Losh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21ca987d46SWarner Losh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22ca987d46SWarner Losh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23ca987d46SWarner Losh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24ca987d46SWarner Losh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25ca987d46SWarner Losh * SUCH DAMAGE.
26ca987d46SWarner Losh */
27ca987d46SWarner Losh
28ca987d46SWarner Losh #include <bootstrap.h>
29ca987d46SWarner Losh #include <sys/endian.h>
3005e8899dSKyle Evans #include <sys/param.h>
31ca987d46SWarner Losh #include <stand.h>
32ca987d46SWarner Losh
33ca987d46SWarner Losh #include <efi.h>
34ca987d46SWarner Losh #include <efilib.h>
35ca987d46SWarner Losh #include <efiuga.h>
36ca987d46SWarner Losh #include <efipciio.h>
37becaac39SToomas Soome #include <Protocol/EdidActive.h>
38becaac39SToomas Soome #include <Protocol/EdidDiscovered.h>
39ca987d46SWarner Losh #include <machine/metadata.h>
40ca987d46SWarner Losh
413630506bSToomas Soome #include "bootstrap.h"
42ca987d46SWarner Losh #include "framebuffer.h"
43ca987d46SWarner Losh
4452ec89a9SToomas Soome static EFI_GUID conout_guid = EFI_CONSOLE_OUT_DEVICE_GUID;
453630506bSToomas Soome EFI_GUID gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
46ca987d46SWarner Losh static EFI_GUID pciio_guid = EFI_PCI_IO_PROTOCOL_GUID;
47ca987d46SWarner Losh static EFI_GUID uga_guid = EFI_UGA_DRAW_PROTOCOL_GUID;
48becaac39SToomas Soome static EFI_GUID active_edid_guid = EFI_EDID_ACTIVE_PROTOCOL_GUID;
49becaac39SToomas Soome static EFI_GUID discovered_edid_guid = EFI_EDID_DISCOVERED_PROTOCOL_GUID;
50becaac39SToomas Soome static EFI_HANDLE gop_handle;
51becaac39SToomas Soome
52becaac39SToomas Soome /* Cached EDID. */
53becaac39SToomas Soome struct vesa_edid_info *edid_info = NULL;
54ca987d46SWarner Losh
5552ec89a9SToomas Soome static EFI_GRAPHICS_OUTPUT *gop;
5652ec89a9SToomas Soome static EFI_UGA_DRAW_PROTOCOL *uga;
5752ec89a9SToomas Soome
5805e8899dSKyle Evans static struct named_resolution {
5905e8899dSKyle Evans const char *name;
6005e8899dSKyle Evans const char *alias;
6105e8899dSKyle Evans unsigned int width;
6205e8899dSKyle Evans unsigned int height;
6305e8899dSKyle Evans } resolutions[] = {
6405e8899dSKyle Evans {
6505e8899dSKyle Evans .name = "480p",
6605e8899dSKyle Evans .width = 640,
6705e8899dSKyle Evans .height = 480,
6805e8899dSKyle Evans },
6905e8899dSKyle Evans {
7005e8899dSKyle Evans .name = "720p",
7105e8899dSKyle Evans .width = 1280,
7205e8899dSKyle Evans .height = 720,
7305e8899dSKyle Evans },
7405e8899dSKyle Evans {
7505e8899dSKyle Evans .name = "1080p",
7605e8899dSKyle Evans .width = 1920,
7705e8899dSKyle Evans .height = 1080,
7805e8899dSKyle Evans },
7905e8899dSKyle Evans {
807edbf69bSDag-Erling Smørgrav .name = "1440p",
817edbf69bSDag-Erling Smørgrav .width = 2560,
827edbf69bSDag-Erling Smørgrav .height = 1440,
837edbf69bSDag-Erling Smørgrav },
847edbf69bSDag-Erling Smørgrav {
8505e8899dSKyle Evans .name = "2160p",
8605e8899dSKyle Evans .alias = "4k",
8705e8899dSKyle Evans .width = 3840,
8805e8899dSKyle Evans .height = 2160,
8905e8899dSKyle Evans },
9005e8899dSKyle Evans {
9105e8899dSKyle Evans .name = "5k",
9205e8899dSKyle Evans .width = 5120,
9305e8899dSKyle Evans .height = 2880,
9405e8899dSKyle Evans }
9505e8899dSKyle Evans };
9605e8899dSKyle Evans
97ca987d46SWarner Losh static u_int
efifb_color_depth(struct efi_fb * efifb)98ca987d46SWarner Losh efifb_color_depth(struct efi_fb *efifb)
99ca987d46SWarner Losh {
100ca987d46SWarner Losh uint32_t mask;
101ca987d46SWarner Losh u_int depth;
102ca987d46SWarner Losh
103ca987d46SWarner Losh mask = efifb->fb_mask_red | efifb->fb_mask_green |
104ca987d46SWarner Losh efifb->fb_mask_blue | efifb->fb_mask_reserved;
105ca987d46SWarner Losh if (mask == 0)
106ca987d46SWarner Losh return (0);
107ca987d46SWarner Losh for (depth = 1; mask != 1; depth++)
108ca987d46SWarner Losh mask >>= 1;
109ca987d46SWarner Losh return (depth);
110ca987d46SWarner Losh }
111ca987d46SWarner Losh
112ca987d46SWarner Losh static int
efifb_mask_from_pixfmt(struct efi_fb * efifb,EFI_GRAPHICS_PIXEL_FORMAT pixfmt,EFI_PIXEL_BITMASK * pixinfo)113ca987d46SWarner Losh efifb_mask_from_pixfmt(struct efi_fb *efifb, EFI_GRAPHICS_PIXEL_FORMAT pixfmt,
114ca987d46SWarner Losh EFI_PIXEL_BITMASK *pixinfo)
115ca987d46SWarner Losh {
116ca987d46SWarner Losh int result;
117ca987d46SWarner Losh
118ca987d46SWarner Losh result = 0;
119ca987d46SWarner Losh switch (pixfmt) {
120ca987d46SWarner Losh case PixelRedGreenBlueReserved8BitPerColor:
1211a816c75SToomas Soome case PixelBltOnly:
122ca987d46SWarner Losh efifb->fb_mask_red = 0x000000ff;
123ca987d46SWarner Losh efifb->fb_mask_green = 0x0000ff00;
124ca987d46SWarner Losh efifb->fb_mask_blue = 0x00ff0000;
125ca987d46SWarner Losh efifb->fb_mask_reserved = 0xff000000;
126ca987d46SWarner Losh break;
127ca987d46SWarner Losh case PixelBlueGreenRedReserved8BitPerColor:
128ca987d46SWarner Losh efifb->fb_mask_red = 0x00ff0000;
129ca987d46SWarner Losh efifb->fb_mask_green = 0x0000ff00;
130ca987d46SWarner Losh efifb->fb_mask_blue = 0x000000ff;
131ca987d46SWarner Losh efifb->fb_mask_reserved = 0xff000000;
132ca987d46SWarner Losh break;
133ca987d46SWarner Losh case PixelBitMask:
134ca987d46SWarner Losh efifb->fb_mask_red = pixinfo->RedMask;
135ca987d46SWarner Losh efifb->fb_mask_green = pixinfo->GreenMask;
136ca987d46SWarner Losh efifb->fb_mask_blue = pixinfo->BlueMask;
137ca987d46SWarner Losh efifb->fb_mask_reserved = pixinfo->ReservedMask;
138ca987d46SWarner Losh break;
139ca987d46SWarner Losh default:
140ca987d46SWarner Losh result = 1;
141ca987d46SWarner Losh break;
142ca987d46SWarner Losh }
143ca987d46SWarner Losh return (result);
144ca987d46SWarner Losh }
145ca987d46SWarner Losh
146ca987d46SWarner Losh static int
efifb_from_gop(struct efi_fb * efifb,EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE * mode,EFI_GRAPHICS_OUTPUT_MODE_INFORMATION * info)147ca987d46SWarner Losh efifb_from_gop(struct efi_fb *efifb, EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE *mode,
148ca987d46SWarner Losh EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info)
149ca987d46SWarner Losh {
150ca987d46SWarner Losh int result;
151ca987d46SWarner Losh
152*6818ff77SAhmad Khalifa /*
153*6818ff77SAhmad Khalifa * The Asus EEEPC 1025C, and possibly others,
154*6818ff77SAhmad Khalifa * require the address to be masked.
155*6818ff77SAhmad Khalifa */
156*6818ff77SAhmad Khalifa efifb->fb_addr =
157*6818ff77SAhmad Khalifa #ifdef __i386__
158*6818ff77SAhmad Khalifa mode->FrameBufferBase & 0xffffffff;
159*6818ff77SAhmad Khalifa #else
160*6818ff77SAhmad Khalifa mode->FrameBufferBase;
161*6818ff77SAhmad Khalifa #endif
162ca987d46SWarner Losh efifb->fb_size = mode->FrameBufferSize;
163ca987d46SWarner Losh efifb->fb_height = info->VerticalResolution;
164ca987d46SWarner Losh efifb->fb_width = info->HorizontalResolution;
165ca987d46SWarner Losh efifb->fb_stride = info->PixelsPerScanLine;
166ca987d46SWarner Losh result = efifb_mask_from_pixfmt(efifb, info->PixelFormat,
167ca987d46SWarner Losh &info->PixelInformation);
168ca987d46SWarner Losh return (result);
169ca987d46SWarner Losh }
170ca987d46SWarner Losh
171ca987d46SWarner Losh static ssize_t
efifb_uga_find_pixel(EFI_UGA_DRAW_PROTOCOL * uga,u_int line,EFI_PCI_IO_PROTOCOL * pciio,uint64_t addr,uint64_t size)172ca987d46SWarner Losh efifb_uga_find_pixel(EFI_UGA_DRAW_PROTOCOL *uga, u_int line,
173ca987d46SWarner Losh EFI_PCI_IO_PROTOCOL *pciio, uint64_t addr, uint64_t size)
174ca987d46SWarner Losh {
175ca987d46SWarner Losh EFI_UGA_PIXEL pix0, pix1;
176ca987d46SWarner Losh uint8_t *data1, *data2;
177ca987d46SWarner Losh size_t count, maxcount = 1024;
178ca987d46SWarner Losh ssize_t ofs;
179ca987d46SWarner Losh EFI_STATUS status;
180ca987d46SWarner Losh u_int idx;
181ca987d46SWarner Losh
182ca987d46SWarner Losh status = uga->Blt(uga, &pix0, EfiUgaVideoToBltBuffer,
183ca987d46SWarner Losh 0, line, 0, 0, 1, 1, 0);
184ca987d46SWarner Losh if (EFI_ERROR(status)) {
185ca987d46SWarner Losh printf("UGA BLT operation failed (video->buffer)");
186ca987d46SWarner Losh return (-1);
187ca987d46SWarner Losh }
188ca987d46SWarner Losh pix1.Red = ~pix0.Red;
189ca987d46SWarner Losh pix1.Green = ~pix0.Green;
190ca987d46SWarner Losh pix1.Blue = ~pix0.Blue;
191ca987d46SWarner Losh pix1.Reserved = 0;
192ca987d46SWarner Losh
193ca987d46SWarner Losh data1 = calloc(maxcount, 2);
194ca987d46SWarner Losh if (data1 == NULL) {
195ca987d46SWarner Losh printf("Unable to allocate memory");
196ca987d46SWarner Losh return (-1);
197ca987d46SWarner Losh }
198ca987d46SWarner Losh data2 = data1 + maxcount;
199ca987d46SWarner Losh
200ca987d46SWarner Losh ofs = 0;
201ca987d46SWarner Losh while (size > 0) {
202ca987d46SWarner Losh count = min(size, maxcount);
203ca987d46SWarner Losh
204ca987d46SWarner Losh status = pciio->Mem.Read(pciio, EfiPciIoWidthUint32,
205ca987d46SWarner Losh EFI_PCI_IO_PASS_THROUGH_BAR, addr + ofs, count >> 2,
206ca987d46SWarner Losh data1);
207ca987d46SWarner Losh if (EFI_ERROR(status)) {
208ca987d46SWarner Losh printf("Error reading frame buffer (before)");
209ca987d46SWarner Losh goto fail;
210ca987d46SWarner Losh }
211ca987d46SWarner Losh status = uga->Blt(uga, &pix1, EfiUgaBltBufferToVideo,
212ca987d46SWarner Losh 0, 0, 0, line, 1, 1, 0);
213ca987d46SWarner Losh if (EFI_ERROR(status)) {
214ca987d46SWarner Losh printf("UGA BLT operation failed (modify)");
215ca987d46SWarner Losh goto fail;
216ca987d46SWarner Losh }
217ca987d46SWarner Losh status = pciio->Mem.Read(pciio, EfiPciIoWidthUint32,
218ca987d46SWarner Losh EFI_PCI_IO_PASS_THROUGH_BAR, addr + ofs, count >> 2,
219ca987d46SWarner Losh data2);
220ca987d46SWarner Losh if (EFI_ERROR(status)) {
221ca987d46SWarner Losh printf("Error reading frame buffer (after)");
222ca987d46SWarner Losh goto fail;
223ca987d46SWarner Losh }
224ca987d46SWarner Losh status = uga->Blt(uga, &pix0, EfiUgaBltBufferToVideo,
225ca987d46SWarner Losh 0, 0, 0, line, 1, 1, 0);
226ca987d46SWarner Losh if (EFI_ERROR(status)) {
227ca987d46SWarner Losh printf("UGA BLT operation failed (restore)");
228ca987d46SWarner Losh goto fail;
229ca987d46SWarner Losh }
230ca987d46SWarner Losh for (idx = 0; idx < count; idx++) {
231ca987d46SWarner Losh if (data1[idx] != data2[idx]) {
232ca987d46SWarner Losh free(data1);
233ca987d46SWarner Losh return (ofs + (idx & ~3));
234ca987d46SWarner Losh }
235ca987d46SWarner Losh }
236ca987d46SWarner Losh ofs += count;
237ca987d46SWarner Losh size -= count;
238ca987d46SWarner Losh }
239ca987d46SWarner Losh printf("No change detected in frame buffer");
240ca987d46SWarner Losh
241ca987d46SWarner Losh fail:
242ca987d46SWarner Losh printf(" -- error %lu\n", EFI_ERROR_CODE(status));
243ca987d46SWarner Losh free(data1);
244ca987d46SWarner Losh return (-1);
245ca987d46SWarner Losh }
246ca987d46SWarner Losh
247ca987d46SWarner Losh static EFI_PCI_IO_PROTOCOL *
efifb_uga_get_pciio(void)248ca987d46SWarner Losh efifb_uga_get_pciio(void)
249ca987d46SWarner Losh {
250ca987d46SWarner Losh EFI_PCI_IO_PROTOCOL *pciio;
251ca987d46SWarner Losh EFI_HANDLE *buf, *hp;
252ca987d46SWarner Losh EFI_STATUS status;
253ca987d46SWarner Losh UINTN bufsz;
254ca987d46SWarner Losh
255ca987d46SWarner Losh /* Get all handles that support the UGA protocol. */
256ca987d46SWarner Losh bufsz = 0;
257ca987d46SWarner Losh status = BS->LocateHandle(ByProtocol, &uga_guid, NULL, &bufsz, NULL);
258ca987d46SWarner Losh if (status != EFI_BUFFER_TOO_SMALL)
259ca987d46SWarner Losh return (NULL);
260ca987d46SWarner Losh buf = malloc(bufsz);
261ca987d46SWarner Losh status = BS->LocateHandle(ByProtocol, &uga_guid, NULL, &bufsz, buf);
262ca987d46SWarner Losh if (status != EFI_SUCCESS) {
263ca987d46SWarner Losh free(buf);
264ca987d46SWarner Losh return (NULL);
265ca987d46SWarner Losh }
266ca987d46SWarner Losh bufsz /= sizeof(EFI_HANDLE);
267ca987d46SWarner Losh
268ca987d46SWarner Losh /* Get the PCI I/O interface of the first handle that supports it. */
269ca987d46SWarner Losh pciio = NULL;
270ca987d46SWarner Losh for (hp = buf; hp < buf + bufsz; hp++) {
271110d56cbSToomas Soome status = OpenProtocolByHandle(*hp, &pciio_guid,
272110d56cbSToomas Soome (void **)&pciio);
273ca987d46SWarner Losh if (status == EFI_SUCCESS) {
274ca987d46SWarner Losh free(buf);
275ca987d46SWarner Losh return (pciio);
276ca987d46SWarner Losh }
277ca987d46SWarner Losh }
278ca987d46SWarner Losh free(buf);
279ca987d46SWarner Losh return (NULL);
280ca987d46SWarner Losh }
281ca987d46SWarner Losh
282ca987d46SWarner Losh static EFI_STATUS
efifb_uga_locate_framebuffer(EFI_PCI_IO_PROTOCOL * pciio,uint64_t * addrp,uint64_t * sizep)283ca987d46SWarner Losh efifb_uga_locate_framebuffer(EFI_PCI_IO_PROTOCOL *pciio, uint64_t *addrp,
284ca987d46SWarner Losh uint64_t *sizep)
285ca987d46SWarner Losh {
286ca987d46SWarner Losh uint8_t *resattr;
287ca987d46SWarner Losh uint64_t addr, size;
288ca987d46SWarner Losh EFI_STATUS status;
289ca987d46SWarner Losh u_int bar;
290ca987d46SWarner Losh
291ca987d46SWarner Losh if (pciio == NULL)
292ca987d46SWarner Losh return (EFI_DEVICE_ERROR);
293ca987d46SWarner Losh
294ca987d46SWarner Losh /* Attempt to get the frame buffer address (imprecise). */
295ca987d46SWarner Losh *addrp = 0;
296ca987d46SWarner Losh *sizep = 0;
297ca987d46SWarner Losh for (bar = 0; bar < 6; bar++) {
298ca987d46SWarner Losh status = pciio->GetBarAttributes(pciio, bar, NULL,
299ca987d46SWarner Losh (void **)&resattr);
300ca987d46SWarner Losh if (status != EFI_SUCCESS)
301ca987d46SWarner Losh continue;
302ca987d46SWarner Losh /* XXX magic offsets and constants. */
303ca987d46SWarner Losh if (resattr[0] == 0x87 && resattr[3] == 0) {
304ca987d46SWarner Losh /* 32-bit address space descriptor (MEMIO) */
305ca987d46SWarner Losh addr = le32dec(resattr + 10);
306ca987d46SWarner Losh size = le32dec(resattr + 22);
307ca987d46SWarner Losh } else if (resattr[0] == 0x8a && resattr[3] == 0) {
308ca987d46SWarner Losh /* 64-bit address space descriptor (MEMIO) */
309ca987d46SWarner Losh addr = le64dec(resattr + 14);
310ca987d46SWarner Losh size = le64dec(resattr + 38);
311ca987d46SWarner Losh } else {
312ca987d46SWarner Losh addr = 0;
313ca987d46SWarner Losh size = 0;
314ca987d46SWarner Losh }
315ca987d46SWarner Losh BS->FreePool(resattr);
316ca987d46SWarner Losh if (addr == 0 || size == 0)
317ca987d46SWarner Losh continue;
318ca987d46SWarner Losh
319ca987d46SWarner Losh /* We assume the largest BAR is the frame buffer. */
320ca987d46SWarner Losh if (size > *sizep) {
321ca987d46SWarner Losh *addrp = addr;
322ca987d46SWarner Losh *sizep = size;
323ca987d46SWarner Losh }
324ca987d46SWarner Losh }
325ca987d46SWarner Losh return ((*addrp == 0 || *sizep == 0) ? EFI_DEVICE_ERROR : 0);
326ca987d46SWarner Losh }
327ca987d46SWarner Losh
328ca987d46SWarner Losh static int
efifb_from_uga(struct efi_fb * efifb)32952ec89a9SToomas Soome efifb_from_uga(struct efi_fb *efifb)
330ca987d46SWarner Losh {
331ca987d46SWarner Losh EFI_PCI_IO_PROTOCOL *pciio;
332ca987d46SWarner Losh char *ev, *p;
333ca987d46SWarner Losh EFI_STATUS status;
334ca987d46SWarner Losh ssize_t offset;
335ca987d46SWarner Losh uint64_t fbaddr;
336ca987d46SWarner Losh uint32_t horiz, vert, stride;
337ca987d46SWarner Losh uint32_t np, depth, refresh;
338ca987d46SWarner Losh
339ca987d46SWarner Losh status = uga->GetMode(uga, &horiz, &vert, &depth, &refresh);
340ca987d46SWarner Losh if (EFI_ERROR(status))
341ca987d46SWarner Losh return (1);
342ca987d46SWarner Losh efifb->fb_height = vert;
343ca987d46SWarner Losh efifb->fb_width = horiz;
344ca987d46SWarner Losh /* Paranoia... */
345ca987d46SWarner Losh if (efifb->fb_height == 0 || efifb->fb_width == 0)
346ca987d46SWarner Losh return (1);
347ca987d46SWarner Losh
348ca987d46SWarner Losh /* The color masks are fixed AFAICT. */
349ca987d46SWarner Losh efifb_mask_from_pixfmt(efifb, PixelBlueGreenRedReserved8BitPerColor,
350ca987d46SWarner Losh NULL);
351ca987d46SWarner Losh
352ca987d46SWarner Losh /* pciio can be NULL on return! */
353ca987d46SWarner Losh pciio = efifb_uga_get_pciio();
354ca987d46SWarner Losh
355ca987d46SWarner Losh /* Try to find the frame buffer. */
356ca987d46SWarner Losh status = efifb_uga_locate_framebuffer(pciio, &efifb->fb_addr,
357ca987d46SWarner Losh &efifb->fb_size);
358ca987d46SWarner Losh if (EFI_ERROR(status)) {
359ca987d46SWarner Losh efifb->fb_addr = 0;
360ca987d46SWarner Losh efifb->fb_size = 0;
361ca987d46SWarner Losh }
362ca987d46SWarner Losh
363ca987d46SWarner Losh /*
364ca987d46SWarner Losh * There's no reliable way to detect the frame buffer or the
365ca987d46SWarner Losh * offset within the frame buffer of the visible region, nor
366ca987d46SWarner Losh * the stride. Our only option is to look at the system and
367ca987d46SWarner Losh * fill in the blanks based on that. Luckily, UGA was mostly
368ca987d46SWarner Losh * only used on Apple hardware.
369ca987d46SWarner Losh */
370ca987d46SWarner Losh offset = -1;
371ca987d46SWarner Losh ev = getenv("smbios.system.maker");
372ca987d46SWarner Losh if (ev != NULL && !strcmp(ev, "Apple Inc.")) {
373ca987d46SWarner Losh ev = getenv("smbios.system.product");
374ca987d46SWarner Losh if (ev != NULL && !strcmp(ev, "iMac7,1")) {
375ca987d46SWarner Losh /* These are the expected values we should have. */
376ca987d46SWarner Losh horiz = 1680;
377ca987d46SWarner Losh vert = 1050;
378ca987d46SWarner Losh fbaddr = 0xc0000000;
379ca987d46SWarner Losh /* These are the missing bits. */
380ca987d46SWarner Losh offset = 0x10000;
381ca987d46SWarner Losh stride = 1728;
382ca987d46SWarner Losh } else if (ev != NULL && !strcmp(ev, "MacBook3,1")) {
383ca987d46SWarner Losh /* These are the expected values we should have. */
384ca987d46SWarner Losh horiz = 1280;
385ca987d46SWarner Losh vert = 800;
386ca987d46SWarner Losh fbaddr = 0xc0000000;
387ca987d46SWarner Losh /* These are the missing bits. */
388ca987d46SWarner Losh offset = 0x0;
389ca987d46SWarner Losh stride = 2048;
390ca987d46SWarner Losh }
391ca987d46SWarner Losh }
392ca987d46SWarner Losh
393ca987d46SWarner Losh /*
394ca987d46SWarner Losh * If this is hardware we know, make sure that it looks familiar
395ca987d46SWarner Losh * before we accept our hardcoded values.
396ca987d46SWarner Losh */
397ca987d46SWarner Losh if (offset >= 0 && efifb->fb_width == horiz &&
398ca987d46SWarner Losh efifb->fb_height == vert && efifb->fb_addr == fbaddr) {
399ca987d46SWarner Losh efifb->fb_addr += offset;
400ca987d46SWarner Losh efifb->fb_size -= offset;
401ca987d46SWarner Losh efifb->fb_stride = stride;
402ca987d46SWarner Losh return (0);
403ca987d46SWarner Losh } else if (offset >= 0) {
404ca987d46SWarner Losh printf("Hardware make/model known, but graphics not "
405ca987d46SWarner Losh "as expected.\n");
406ca987d46SWarner Losh printf("Console may not work!\n");
407ca987d46SWarner Losh }
408ca987d46SWarner Losh
409ca987d46SWarner Losh /*
410ca987d46SWarner Losh * The stride is equal or larger to the width. Often it's the
411ca987d46SWarner Losh * next larger power of two. We'll start with that...
412ca987d46SWarner Losh */
413ca987d46SWarner Losh efifb->fb_stride = efifb->fb_width;
414ca987d46SWarner Losh do {
415ca987d46SWarner Losh np = efifb->fb_stride & (efifb->fb_stride - 1);
416ca987d46SWarner Losh if (np) {
417ca987d46SWarner Losh efifb->fb_stride |= (np - 1);
418ca987d46SWarner Losh efifb->fb_stride++;
419ca987d46SWarner Losh }
420ca987d46SWarner Losh } while (np);
421ca987d46SWarner Losh
422ca987d46SWarner Losh ev = getenv("hw.efifb.address");
423ca987d46SWarner Losh if (ev == NULL) {
424ca987d46SWarner Losh if (efifb->fb_addr == 0) {
425ca987d46SWarner Losh printf("Please set hw.efifb.address and "
426ca987d46SWarner Losh "hw.efifb.stride.\n");
427ca987d46SWarner Losh return (1);
428ca987d46SWarner Losh }
429ca987d46SWarner Losh
430ca987d46SWarner Losh /*
431ca987d46SWarner Losh * The visible part of the frame buffer may not start at
432ca987d46SWarner Losh * offset 0, so try to detect it. Note that we may not
433ca987d46SWarner Losh * always be able to read from the frame buffer, which
434ca987d46SWarner Losh * means that we may not be able to detect anything. In
435ca987d46SWarner Losh * that case, we would take a long time scanning for a
436ca987d46SWarner Losh * pixel change in the frame buffer, which would have it
437ca987d46SWarner Losh * appear that we're hanging, so we limit the scan to
438ca987d46SWarner Losh * 1/256th of the frame buffer. This number is mostly
439ca987d46SWarner Losh * based on PR 202730 and the fact that on a MacBoook,
440ca987d46SWarner Losh * where we can't read from the frame buffer the offset
441ca987d46SWarner Losh * of the visible region is 0. In short: we want to scan
442ca987d46SWarner Losh * enough to handle all adapters that have an offset
443ca987d46SWarner Losh * larger than 0 and we want to scan as little as we can
444ca987d46SWarner Losh * to not appear to hang when we can't read from the
445ca987d46SWarner Losh * frame buffer.
446ca987d46SWarner Losh */
447ca987d46SWarner Losh offset = efifb_uga_find_pixel(uga, 0, pciio, efifb->fb_addr,
448ca987d46SWarner Losh efifb->fb_size >> 8);
449ca987d46SWarner Losh if (offset == -1) {
450ca987d46SWarner Losh printf("Unable to reliably detect frame buffer.\n");
451ca987d46SWarner Losh } else if (offset > 0) {
452ca987d46SWarner Losh efifb->fb_addr += offset;
453ca987d46SWarner Losh efifb->fb_size -= offset;
454ca987d46SWarner Losh }
455ca987d46SWarner Losh } else {
456ca987d46SWarner Losh offset = 0;
457ca987d46SWarner Losh efifb->fb_size = efifb->fb_height * efifb->fb_stride * 4;
458ca987d46SWarner Losh efifb->fb_addr = strtoul(ev, &p, 0);
459ca987d46SWarner Losh if (*p != '\0')
460ca987d46SWarner Losh return (1);
461ca987d46SWarner Losh }
462ca987d46SWarner Losh
463ca987d46SWarner Losh ev = getenv("hw.efifb.stride");
464ca987d46SWarner Losh if (ev == NULL) {
465ca987d46SWarner Losh if (pciio != NULL && offset != -1) {
466ca987d46SWarner Losh /* Determine the stride. */
467ca987d46SWarner Losh offset = efifb_uga_find_pixel(uga, 1, pciio,
468ca987d46SWarner Losh efifb->fb_addr, horiz * 8);
469ca987d46SWarner Losh if (offset != -1)
470ca987d46SWarner Losh efifb->fb_stride = offset >> 2;
471ca987d46SWarner Losh } else {
472ca987d46SWarner Losh printf("Unable to reliably detect the stride.\n");
473ca987d46SWarner Losh }
474ca987d46SWarner Losh } else {
475ca987d46SWarner Losh efifb->fb_stride = strtoul(ev, &p, 0);
476ca987d46SWarner Losh if (*p != '\0')
477ca987d46SWarner Losh return (1);
478ca987d46SWarner Losh }
479ca987d46SWarner Losh
480ca987d46SWarner Losh /*
481ca987d46SWarner Losh * We finalized on the stride, so recalculate the size of the
482ca987d46SWarner Losh * frame buffer.
483ca987d46SWarner Losh */
484ca987d46SWarner Losh efifb->fb_size = efifb->fb_height * efifb->fb_stride * 4;
485ca987d46SWarner Losh return (0);
486ca987d46SWarner Losh }
487ca987d46SWarner Losh
488becaac39SToomas Soome /*
489becaac39SToomas Soome * Fetch EDID info. Caller must free the buffer.
490becaac39SToomas Soome */
491becaac39SToomas Soome static struct vesa_edid_info *
efifb_gop_get_edid(EFI_HANDLE h)492becaac39SToomas Soome efifb_gop_get_edid(EFI_HANDLE h)
493becaac39SToomas Soome {
494becaac39SToomas Soome const uint8_t magic[] = EDID_MAGIC;
495becaac39SToomas Soome EFI_EDID_ACTIVE_PROTOCOL *edid;
496becaac39SToomas Soome struct vesa_edid_info *edid_infop;
497becaac39SToomas Soome EFI_GUID *guid;
498becaac39SToomas Soome EFI_STATUS status;
499becaac39SToomas Soome size_t size;
500becaac39SToomas Soome
501becaac39SToomas Soome guid = &active_edid_guid;
502becaac39SToomas Soome status = BS->OpenProtocol(h, guid, (void **)&edid, IH, NULL,
503becaac39SToomas Soome EFI_OPEN_PROTOCOL_GET_PROTOCOL);
504becaac39SToomas Soome if (status != EFI_SUCCESS ||
505becaac39SToomas Soome edid->SizeOfEdid == 0) {
506becaac39SToomas Soome guid = &discovered_edid_guid;
507becaac39SToomas Soome status = BS->OpenProtocol(h, guid, (void **)&edid, IH, NULL,
508becaac39SToomas Soome EFI_OPEN_PROTOCOL_GET_PROTOCOL);
509becaac39SToomas Soome if (status != EFI_SUCCESS ||
510becaac39SToomas Soome edid->SizeOfEdid == 0)
511becaac39SToomas Soome return (NULL);
512becaac39SToomas Soome }
513becaac39SToomas Soome
514becaac39SToomas Soome size = MAX(sizeof(*edid_infop), edid->SizeOfEdid);
515becaac39SToomas Soome
516becaac39SToomas Soome edid_infop = calloc(1, size);
517becaac39SToomas Soome if (edid_infop == NULL)
518becaac39SToomas Soome return (NULL);
519becaac39SToomas Soome
520becaac39SToomas Soome memcpy(edid_infop, edid->Edid, edid->SizeOfEdid);
521becaac39SToomas Soome
522becaac39SToomas Soome /* Validate EDID */
523becaac39SToomas Soome if (memcmp(edid_infop, magic, sizeof (magic)) != 0)
524becaac39SToomas Soome goto error;
525becaac39SToomas Soome
526becaac39SToomas Soome if (edid_infop->header.version != 1)
527becaac39SToomas Soome goto error;
528becaac39SToomas Soome
529becaac39SToomas Soome return (edid_infop);
530becaac39SToomas Soome error:
531becaac39SToomas Soome free(edid_infop);
532becaac39SToomas Soome return (NULL);
533becaac39SToomas Soome }
534becaac39SToomas Soome
535becaac39SToomas Soome static bool
efifb_get_edid(edid_res_list_t * res)536becaac39SToomas Soome efifb_get_edid(edid_res_list_t *res)
537becaac39SToomas Soome {
538becaac39SToomas Soome bool rv = false;
539becaac39SToomas Soome
540becaac39SToomas Soome if (edid_info == NULL)
541becaac39SToomas Soome edid_info = efifb_gop_get_edid(gop_handle);
542becaac39SToomas Soome
543becaac39SToomas Soome if (edid_info != NULL)
544becaac39SToomas Soome rv = gfx_get_edid_resolution(edid_info, res);
545becaac39SToomas Soome
546becaac39SToomas Soome return (rv);
547becaac39SToomas Soome }
548becaac39SToomas Soome
549df065f69SWarner Losh bool
efi_has_gop(void)550df065f69SWarner Losh efi_has_gop(void)
551df065f69SWarner Losh {
552df065f69SWarner Losh EFI_STATUS status;
553df065f69SWarner Losh EFI_HANDLE *hlist;
554df065f69SWarner Losh UINTN hsize;
555df065f69SWarner Losh
556df065f69SWarner Losh hsize = 0;
557df065f69SWarner Losh hlist = NULL;
558df065f69SWarner Losh status = BS->LocateHandle(ByProtocol, &gop_guid, NULL, &hsize, hlist);
559df065f69SWarner Losh
560df065f69SWarner Losh return (status == EFI_BUFFER_TOO_SMALL);
561df065f69SWarner Losh }
562df065f69SWarner Losh
563df065f69SWarner Losh
564ca987d46SWarner Losh int
efi_find_framebuffer(teken_gfx_t * gfx_state)56550180d2bSToomas Soome efi_find_framebuffer(teken_gfx_t *gfx_state)
566ca987d46SWarner Losh {
567*6818ff77SAhmad Khalifa EFI_PHYSICAL_ADDRESS ptr;
568becaac39SToomas Soome EFI_HANDLE *hlist;
569ef698fabSToomas Soome UINTN nhandles, i, hsize;
57050180d2bSToomas Soome struct efi_fb efifb;
571ca987d46SWarner Losh EFI_STATUS status;
57250180d2bSToomas Soome int rv;
57350180d2bSToomas Soome
57450180d2bSToomas Soome gfx_state->tg_fb_type = FB_TEXT;
575ca987d46SWarner Losh
576ef698fabSToomas Soome hsize = 0;
5779adce1aeSToomas Soome hlist = NULL;
578ef698fabSToomas Soome status = BS->LocateHandle(ByProtocol, &gop_guid, NULL, &hsize, hlist);
579ef698fabSToomas Soome if (status == EFI_BUFFER_TOO_SMALL) {
580ef698fabSToomas Soome hlist = malloc(hsize);
581ef698fabSToomas Soome if (hlist == NULL)
582ef698fabSToomas Soome return (ENOMEM);
583ef698fabSToomas Soome status = BS->LocateHandle(ByProtocol, &gop_guid, NULL, &hsize,
584ef698fabSToomas Soome hlist);
585ef698fabSToomas Soome if (EFI_ERROR(status))
586ef698fabSToomas Soome free(hlist);
587ef698fabSToomas Soome }
588ef698fabSToomas Soome if (EFI_ERROR(status))
589ef698fabSToomas Soome return (efi_status_to_errno(status));
590ef698fabSToomas Soome
591ef698fabSToomas Soome nhandles = hsize / sizeof(*hlist);
592ef698fabSToomas Soome
593ef698fabSToomas Soome /*
594ef698fabSToomas Soome * Search for ConOut protocol, if not found, use first handle.
595ef698fabSToomas Soome */
59602732f94SAlexander Motin gop_handle = NULL;
597ef698fabSToomas Soome for (i = 0; i < nhandles; i++) {
59802732f94SAlexander Motin EFI_GRAPHICS_OUTPUT *tgop;
59902732f94SAlexander Motin void *dummy;
60002732f94SAlexander Motin
60102732f94SAlexander Motin status = OpenProtocolByHandle(hlist[i], &gop_guid, (void **)&tgop);
60202732f94SAlexander Motin if (status != EFI_SUCCESS)
60302732f94SAlexander Motin continue;
60402732f94SAlexander Motin
60502732f94SAlexander Motin if (tgop->Mode->Info->PixelFormat == PixelBltOnly ||
60602732f94SAlexander Motin tgop->Mode->Info->PixelFormat >= PixelFormatMax)
60702732f94SAlexander Motin continue;
608ef698fabSToomas Soome
609ef698fabSToomas Soome status = OpenProtocolByHandle(hlist[i], &conout_guid, &dummy);
610ef698fabSToomas Soome if (status == EFI_SUCCESS) {
611becaac39SToomas Soome gop_handle = hlist[i];
61202732f94SAlexander Motin gop = tgop;
613ef698fabSToomas Soome break;
61402732f94SAlexander Motin } else if (gop_handle == NULL) {
61502732f94SAlexander Motin gop_handle = hlist[i];
61602732f94SAlexander Motin gop = tgop;
617ef698fabSToomas Soome }
618ef698fabSToomas Soome }
619ef698fabSToomas Soome
620ef698fabSToomas Soome free(hlist);
621ef698fabSToomas Soome
62202732f94SAlexander Motin if (gop_handle != NULL) {
62350180d2bSToomas Soome gfx_state->tg_fb_type = FB_GOP;
62450180d2bSToomas Soome gfx_state->tg_private = gop;
625becaac39SToomas Soome if (edid_info == NULL)
626becaac39SToomas Soome edid_info = efifb_gop_get_edid(gop_handle);
62750180d2bSToomas Soome } else {
628ca987d46SWarner Losh status = BS->LocateProtocol(&uga_guid, NULL, (VOID **)&uga);
62950180d2bSToomas Soome if (status == EFI_SUCCESS) {
63050180d2bSToomas Soome gfx_state->tg_fb_type = FB_UGA;
63150180d2bSToomas Soome gfx_state->tg_private = uga;
63250180d2bSToomas Soome } else {
633ca987d46SWarner Losh return (1);
634ca987d46SWarner Losh }
63550180d2bSToomas Soome }
63650180d2bSToomas Soome
63750180d2bSToomas Soome switch (gfx_state->tg_fb_type) {
63850180d2bSToomas Soome case FB_GOP:
63950180d2bSToomas Soome rv = efifb_from_gop(&efifb, gop->Mode, gop->Mode->Info);
64050180d2bSToomas Soome break;
64150180d2bSToomas Soome
64250180d2bSToomas Soome case FB_UGA:
64352ec89a9SToomas Soome rv = efifb_from_uga(&efifb);
64450180d2bSToomas Soome break;
64550180d2bSToomas Soome
64650180d2bSToomas Soome default:
64750180d2bSToomas Soome return (1);
64850180d2bSToomas Soome }
64950180d2bSToomas Soome
65050180d2bSToomas Soome gfx_state->tg_fb.fb_addr = efifb.fb_addr;
65150180d2bSToomas Soome gfx_state->tg_fb.fb_size = efifb.fb_size;
65250180d2bSToomas Soome gfx_state->tg_fb.fb_height = efifb.fb_height;
65350180d2bSToomas Soome gfx_state->tg_fb.fb_width = efifb.fb_width;
65450180d2bSToomas Soome gfx_state->tg_fb.fb_stride = efifb.fb_stride;
65550180d2bSToomas Soome gfx_state->tg_fb.fb_mask_red = efifb.fb_mask_red;
65650180d2bSToomas Soome gfx_state->tg_fb.fb_mask_green = efifb.fb_mask_green;
65750180d2bSToomas Soome gfx_state->tg_fb.fb_mask_blue = efifb.fb_mask_blue;
65850180d2bSToomas Soome gfx_state->tg_fb.fb_mask_reserved = efifb.fb_mask_reserved;
65950180d2bSToomas Soome
66050180d2bSToomas Soome gfx_state->tg_fb.fb_bpp = fls(efifb.fb_mask_red | efifb.fb_mask_green |
66150180d2bSToomas Soome efifb.fb_mask_blue | efifb.fb_mask_reserved);
66250180d2bSToomas Soome
663221376dbSToomas Soome if (gfx_state->tg_shadow_fb != NULL)
664*6818ff77SAhmad Khalifa BS->FreePages((uintptr_t)gfx_state->tg_shadow_fb,
665221376dbSToomas Soome gfx_state->tg_shadow_sz);
666221376dbSToomas Soome gfx_state->tg_shadow_sz =
667221376dbSToomas Soome EFI_SIZE_TO_PAGES(efifb.fb_height * efifb.fb_width *
6686102f43cSToomas Soome sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
66995f75b0eSAhmad Khalifa status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
670*6818ff77SAhmad Khalifa gfx_state->tg_shadow_sz, &ptr);
671*6818ff77SAhmad Khalifa gfx_state->tg_shadow_fb = status == EFI_SUCCESS ?
672*6818ff77SAhmad Khalifa (uint32_t *)(uintptr_t)ptr : NULL;
673221376dbSToomas Soome
67450180d2bSToomas Soome return (0);
67550180d2bSToomas Soome }
676ca987d46SWarner Losh
677ca987d46SWarner Losh static void
print_efifb(int mode,struct efi_fb * efifb,int verbose)678ca987d46SWarner Losh print_efifb(int mode, struct efi_fb *efifb, int verbose)
679ca987d46SWarner Losh {
680ca987d46SWarner Losh u_int depth;
681ca987d46SWarner Losh
682ca987d46SWarner Losh if (mode >= 0)
683ca987d46SWarner Losh printf("mode %d: ", mode);
684ca987d46SWarner Losh depth = efifb_color_depth(efifb);
685ca987d46SWarner Losh printf("%ux%ux%u, stride=%u", efifb->fb_width, efifb->fb_height,
686ca987d46SWarner Losh depth, efifb->fb_stride);
687ca987d46SWarner Losh if (verbose) {
688ca987d46SWarner Losh printf("\n frame buffer: address=%jx, size=%jx",
689ca987d46SWarner Losh (uintmax_t)efifb->fb_addr, (uintmax_t)efifb->fb_size);
690ca987d46SWarner Losh printf("\n color mask: R=%08x, G=%08x, B=%08x\n",
691ca987d46SWarner Losh efifb->fb_mask_red, efifb->fb_mask_green,
692ca987d46SWarner Losh efifb->fb_mask_blue);
693ca987d46SWarner Losh }
694ca987d46SWarner Losh }
695ca987d46SWarner Losh
69605e8899dSKyle Evans static bool
efi_resolution_compare(struct named_resolution * res,const char * cmp)69705e8899dSKyle Evans efi_resolution_compare(struct named_resolution *res, const char *cmp)
69805e8899dSKyle Evans {
69905e8899dSKyle Evans
70005e8899dSKyle Evans if (strcasecmp(res->name, cmp) == 0)
70105e8899dSKyle Evans return (true);
70205e8899dSKyle Evans if (res->alias != NULL && strcasecmp(res->alias, cmp) == 0)
70305e8899dSKyle Evans return (true);
70405e8899dSKyle Evans return (false);
70505e8899dSKyle Evans }
70605e8899dSKyle Evans
70705e8899dSKyle Evans
70805e8899dSKyle Evans static void
efi_get_max_resolution(int * width,int * height)70905e8899dSKyle Evans efi_get_max_resolution(int *width, int *height)
71005e8899dSKyle Evans {
71105e8899dSKyle Evans struct named_resolution *res;
71205e8899dSKyle Evans char *maxres;
71305e8899dSKyle Evans char *height_start, *width_start;
71405e8899dSKyle Evans int idx;
71505e8899dSKyle Evans
71605e8899dSKyle Evans *width = *height = 0;
71705e8899dSKyle Evans maxres = getenv("efi_max_resolution");
71805e8899dSKyle Evans /* No max_resolution set? Bail out; choose highest resolution */
71905e8899dSKyle Evans if (maxres == NULL)
72005e8899dSKyle Evans return;
72105e8899dSKyle Evans /* See if it matches one of our known resolutions */
72205e8899dSKyle Evans for (idx = 0; idx < nitems(resolutions); ++idx) {
72305e8899dSKyle Evans res = &resolutions[idx];
72405e8899dSKyle Evans if (efi_resolution_compare(res, maxres)) {
72505e8899dSKyle Evans *width = res->width;
72605e8899dSKyle Evans *height = res->height;
72705e8899dSKyle Evans return;
72805e8899dSKyle Evans }
72905e8899dSKyle Evans }
73005e8899dSKyle Evans /* Not a known resolution, try to parse it; make a copy we can modify */
73105e8899dSKyle Evans maxres = strdup(maxres);
73205e8899dSKyle Evans if (maxres == NULL)
73305e8899dSKyle Evans return;
73405e8899dSKyle Evans height_start = strchr(maxres, 'x');
73505e8899dSKyle Evans if (height_start == NULL) {
73605e8899dSKyle Evans free(maxres);
73705e8899dSKyle Evans return;
73805e8899dSKyle Evans }
73905e8899dSKyle Evans width_start = maxres;
74005e8899dSKyle Evans *height_start++ = 0;
74105e8899dSKyle Evans /* Errors from this will effectively mean "no max" */
74205e8899dSKyle Evans *width = (int)strtol(width_start, NULL, 0);
74305e8899dSKyle Evans *height = (int)strtol(height_start, NULL, 0);
74405e8899dSKyle Evans free(maxres);
74505e8899dSKyle Evans }
74605e8899dSKyle Evans
7475f8cfbe1SKyle Evans static int
gop_autoresize(void)74852ec89a9SToomas Soome gop_autoresize(void)
7495f8cfbe1SKyle Evans {
7505f8cfbe1SKyle Evans struct efi_fb efifb;
7515f8cfbe1SKyle Evans EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
7525f8cfbe1SKyle Evans EFI_STATUS status;
7535f8cfbe1SKyle Evans UINTN infosz;
7545f8cfbe1SKyle Evans UINT32 best_mode, currdim, maxdim, mode;
75505e8899dSKyle Evans int height, max_height, max_width, width;
7565f8cfbe1SKyle Evans
7575f8cfbe1SKyle Evans best_mode = maxdim = 0;
75805e8899dSKyle Evans efi_get_max_resolution(&max_width, &max_height);
7595f8cfbe1SKyle Evans for (mode = 0; mode < gop->Mode->MaxMode; mode++) {
7605f8cfbe1SKyle Evans status = gop->QueryMode(gop, mode, &infosz, &info);
7615f8cfbe1SKyle Evans if (EFI_ERROR(status))
7625f8cfbe1SKyle Evans continue;
7635f8cfbe1SKyle Evans efifb_from_gop(&efifb, gop->Mode, info);
76405e8899dSKyle Evans width = info->HorizontalResolution;
76505e8899dSKyle Evans height = info->VerticalResolution;
76605e8899dSKyle Evans currdim = width * height;
7675f8cfbe1SKyle Evans if (currdim > maxdim) {
76805e8899dSKyle Evans if ((max_width != 0 && width > max_width) ||
76905e8899dSKyle Evans (max_height != 0 && height > max_height))
77005e8899dSKyle Evans continue;
7715f8cfbe1SKyle Evans maxdim = currdim;
7725f8cfbe1SKyle Evans best_mode = mode;
7735f8cfbe1SKyle Evans }
7745f8cfbe1SKyle Evans }
7755f8cfbe1SKyle Evans
776807dbf2bSKyle Evans if (maxdim != 0) {
7775f8cfbe1SKyle Evans status = gop->SetMode(gop, best_mode);
7785f8cfbe1SKyle Evans if (EFI_ERROR(status)) {
7795f8cfbe1SKyle Evans snprintf(command_errbuf, sizeof(command_errbuf),
7805f8cfbe1SKyle Evans "gop_autoresize: Unable to set mode to %u (error=%lu)",
7815f8cfbe1SKyle Evans mode, EFI_ERROR_CODE(status));
7825f8cfbe1SKyle Evans return (CMD_ERROR);
7835f8cfbe1SKyle Evans }
7843630506bSToomas Soome (void) cons_update_mode(true);
785807dbf2bSKyle Evans }
7865f8cfbe1SKyle Evans return (CMD_OK);
7875f8cfbe1SKyle Evans }
7885f8cfbe1SKyle Evans
7895f8cfbe1SKyle Evans static int
text_autoresize()7901c169279SKyle Evans text_autoresize()
7911c169279SKyle Evans {
7921c169279SKyle Evans SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
7931c169279SKyle Evans EFI_STATUS status;
7941c169279SKyle Evans UINTN i, max_dim, best_mode, cols, rows;
7951c169279SKyle Evans
7961c169279SKyle Evans conout = ST->ConOut;
7971c169279SKyle Evans max_dim = best_mode = 0;
7981c169279SKyle Evans for (i = 0; i < conout->Mode->MaxMode; i++) {
7991c169279SKyle Evans status = conout->QueryMode(conout, i, &cols, &rows);
8001c169279SKyle Evans if (EFI_ERROR(status))
8011c169279SKyle Evans continue;
8021c169279SKyle Evans if (cols * rows > max_dim) {
8031c169279SKyle Evans max_dim = cols * rows;
8041c169279SKyle Evans best_mode = i;
8051c169279SKyle Evans }
8061c169279SKyle Evans }
8071c169279SKyle Evans if (max_dim > 0)
8081c169279SKyle Evans conout->SetMode(conout, best_mode);
8093630506bSToomas Soome (void) cons_update_mode(true);
8101c169279SKyle Evans return (CMD_OK);
8111c169279SKyle Evans }
8121c169279SKyle Evans
8131c169279SKyle Evans static int
uga_autoresize(void)81452ec89a9SToomas Soome uga_autoresize(void)
8155f8cfbe1SKyle Evans {
8165f8cfbe1SKyle Evans
81776e00374SKyle Evans return (text_autoresize());
8185f8cfbe1SKyle Evans }
8195f8cfbe1SKyle Evans
8205f8cfbe1SKyle Evans COMMAND_SET(efi_autoresize, "efi-autoresizecons", "EFI Auto-resize Console", command_autoresize);
8215f8cfbe1SKyle Evans
8225f8cfbe1SKyle Evans static int
command_autoresize(int argc,char * argv[])8235f8cfbe1SKyle Evans command_autoresize(int argc, char *argv[])
8245f8cfbe1SKyle Evans {
8251c169279SKyle Evans char *textmode;
8265f8cfbe1SKyle Evans
8271c169279SKyle Evans textmode = getenv("hw.vga.textmode");
8281c169279SKyle Evans /* If it's set and non-zero, we'll select a console mode instead */
8291c169279SKyle Evans if (textmode != NULL && strcmp(textmode, "0") != 0)
8301c169279SKyle Evans return (text_autoresize());
8311c169279SKyle Evans
83252ec89a9SToomas Soome if (gop != NULL)
83352ec89a9SToomas Soome return (gop_autoresize());
8345f8cfbe1SKyle Evans
83552ec89a9SToomas Soome if (uga != NULL)
83652ec89a9SToomas Soome return (uga_autoresize());
8375f8cfbe1SKyle Evans
8385f8cfbe1SKyle Evans snprintf(command_errbuf, sizeof(command_errbuf),
8395f8cfbe1SKyle Evans "%s: Neither Graphics Output Protocol nor Universal Graphics Adapter present",
8405f8cfbe1SKyle Evans argv[0]);
84137528fb4SKyle Evans
84237528fb4SKyle Evans /*
84337528fb4SKyle Evans * Default to text_autoresize if we have neither GOP or UGA. This won't
84437528fb4SKyle Evans * give us the most ideal resolution, but it will at least leave us
84537528fb4SKyle Evans * functional rather than failing the boot for an objectively bad
84637528fb4SKyle Evans * reason.
84737528fb4SKyle Evans */
84837528fb4SKyle Evans return (text_autoresize());
8495f8cfbe1SKyle Evans }
8505f8cfbe1SKyle Evans
851ca987d46SWarner Losh COMMAND_SET(gop, "gop", "graphics output protocol", command_gop);
852ca987d46SWarner Losh
853ca987d46SWarner Losh static int
command_gop(int argc,char * argv[])854ca987d46SWarner Losh command_gop(int argc, char *argv[])
855ca987d46SWarner Losh {
856ca987d46SWarner Losh struct efi_fb efifb;
857ca987d46SWarner Losh EFI_STATUS status;
858ca987d46SWarner Losh u_int mode;
859ca987d46SWarner Losh
86052ec89a9SToomas Soome if (gop == NULL) {
861ca987d46SWarner Losh snprintf(command_errbuf, sizeof(command_errbuf),
86252ec89a9SToomas Soome "%s: Graphics Output Protocol not present", argv[0]);
863ca987d46SWarner Losh return (CMD_ERROR);
864ca987d46SWarner Losh }
865ca987d46SWarner Losh
866ca987d46SWarner Losh if (argc < 2)
867ca987d46SWarner Losh goto usage;
868ca987d46SWarner Losh
869ca987d46SWarner Losh if (!strcmp(argv[1], "set")) {
870ca987d46SWarner Losh char *cp;
871ca987d46SWarner Losh
872ca987d46SWarner Losh if (argc != 3)
873ca987d46SWarner Losh goto usage;
874ca987d46SWarner Losh mode = strtol(argv[2], &cp, 0);
875ca987d46SWarner Losh if (cp[0] != '\0') {
876ca987d46SWarner Losh sprintf(command_errbuf, "mode is an integer");
877ca987d46SWarner Losh return (CMD_ERROR);
878ca987d46SWarner Losh }
879ca987d46SWarner Losh status = gop->SetMode(gop, mode);
880ca987d46SWarner Losh if (EFI_ERROR(status)) {
881ca987d46SWarner Losh snprintf(command_errbuf, sizeof(command_errbuf),
882ca987d46SWarner Losh "%s: Unable to set mode to %u (error=%lu)",
883ca987d46SWarner Losh argv[0], mode, EFI_ERROR_CODE(status));
884ca987d46SWarner Losh return (CMD_ERROR);
885ca987d46SWarner Losh }
8863630506bSToomas Soome (void) cons_update_mode(true);
8873630506bSToomas Soome } else if (strcmp(argv[1], "off") == 0) {
8883630506bSToomas Soome (void) cons_update_mode(false);
8893630506bSToomas Soome } else if (strcmp(argv[1], "get") == 0) {
890becaac39SToomas Soome edid_res_list_t res;
891becaac39SToomas Soome
892ca987d46SWarner Losh if (argc != 2)
893ca987d46SWarner Losh goto usage;
894becaac39SToomas Soome TAILQ_INIT(&res);
895ca987d46SWarner Losh efifb_from_gop(&efifb, gop->Mode, gop->Mode->Info);
896becaac39SToomas Soome if (efifb_get_edid(&res)) {
897becaac39SToomas Soome struct resolution *rp;
898becaac39SToomas Soome
899becaac39SToomas Soome printf("EDID");
900becaac39SToomas Soome while ((rp = TAILQ_FIRST(&res)) != NULL) {
901becaac39SToomas Soome printf(" %dx%d", rp->width, rp->height);
902becaac39SToomas Soome TAILQ_REMOVE(&res, rp, next);
903becaac39SToomas Soome free(rp);
904becaac39SToomas Soome }
905becaac39SToomas Soome printf("\n");
906becaac39SToomas Soome } else {
907becaac39SToomas Soome printf("no EDID information\n");
908becaac39SToomas Soome }
909ca987d46SWarner Losh print_efifb(gop->Mode->Mode, &efifb, 1);
910ca987d46SWarner Losh printf("\n");
911ca987d46SWarner Losh } else if (!strcmp(argv[1], "list")) {
912ca987d46SWarner Losh EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
913ca987d46SWarner Losh UINTN infosz;
914ca987d46SWarner Losh
915ca987d46SWarner Losh if (argc != 2)
916ca987d46SWarner Losh goto usage;
917becaac39SToomas Soome
918ca987d46SWarner Losh pager_open();
919ca987d46SWarner Losh for (mode = 0; mode < gop->Mode->MaxMode; mode++) {
920ca987d46SWarner Losh status = gop->QueryMode(gop, mode, &infosz, &info);
921ca987d46SWarner Losh if (EFI_ERROR(status))
922ca987d46SWarner Losh continue;
923ca987d46SWarner Losh efifb_from_gop(&efifb, gop->Mode, info);
924ca987d46SWarner Losh print_efifb(mode, &efifb, 0);
925ca987d46SWarner Losh if (pager_output("\n"))
926ca987d46SWarner Losh break;
927ca987d46SWarner Losh }
928ca987d46SWarner Losh pager_close();
929ca987d46SWarner Losh }
930ca987d46SWarner Losh return (CMD_OK);
931ca987d46SWarner Losh
932ca987d46SWarner Losh usage:
933ca987d46SWarner Losh snprintf(command_errbuf, sizeof(command_errbuf),
9343630506bSToomas Soome "usage: %s [list | get | set <mode> | off]", argv[0]);
935ca987d46SWarner Losh return (CMD_ERROR);
936ca987d46SWarner Losh }
937ca987d46SWarner Losh
938ca987d46SWarner Losh COMMAND_SET(uga, "uga", "universal graphics adapter", command_uga);
939ca987d46SWarner Losh
940ca987d46SWarner Losh static int
command_uga(int argc,char * argv[])941ca987d46SWarner Losh command_uga(int argc, char *argv[])
942ca987d46SWarner Losh {
943ca987d46SWarner Losh struct efi_fb efifb;
944ca987d46SWarner Losh
94552ec89a9SToomas Soome if (uga == NULL) {
946ca987d46SWarner Losh snprintf(command_errbuf, sizeof(command_errbuf),
94752ec89a9SToomas Soome "%s: UGA Protocol not present", argv[0]);
948ca987d46SWarner Losh return (CMD_ERROR);
949ca987d46SWarner Losh }
950ca987d46SWarner Losh
951ca987d46SWarner Losh if (argc != 1)
952ca987d46SWarner Losh goto usage;
953ca987d46SWarner Losh
95452ec89a9SToomas Soome if (efifb_from_uga(&efifb) != CMD_OK) {
955ca987d46SWarner Losh snprintf(command_errbuf, sizeof(command_errbuf),
956ca987d46SWarner Losh "%s: Unable to get UGA information", argv[0]);
957ca987d46SWarner Losh return (CMD_ERROR);
958ca987d46SWarner Losh }
959ca987d46SWarner Losh
960ca987d46SWarner Losh print_efifb(-1, &efifb, 1);
961ca987d46SWarner Losh printf("\n");
962ca987d46SWarner Losh return (CMD_OK);
963ca987d46SWarner Losh
964ca987d46SWarner Losh usage:
965ca987d46SWarner Losh snprintf(command_errbuf, sizeof(command_errbuf), "usage: %s", argv[0]);
966ca987d46SWarner Losh return (CMD_ERROR);
967ca987d46SWarner Losh }
968