]> code.delx.au - refind/blob - libeg/screen.c
Tweaks to graphics mode-setting code; added --root option to install.sh
[refind] / libeg / screen.c
1 /*
2 * libeg/screen.c
3 * Screen handling functions
4 *
5 * Copyright (c) 2006 Christoph Pfisterer
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * Neither the name of Christoph Pfisterer nor the names of the
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36 /*
37 * Modifications copyright (c) 2012 Roderick W. Smith
38 *
39 * Modifications distributed under the terms of the GNU General Public
40 * License (GPL) version 3 (GPLv3), a copy of which must be distributed
41 * with this source code or binaries made from it.
42 *
43 */
44
45 #include "libegint.h"
46 #include "../refind/screen.h"
47 #include "../refind/lib.h"
48 #include "../include/refit_call_wrapper.h"
49
50 #include <efiUgaDraw.h>
51 #include <efiConsoleControl.h>
52
53 #ifndef __MAKEWITH_GNUEFI
54 #define LibLocateProtocol EfiLibLocateProtocol
55 #endif
56
57 // Console defines and variables
58
59 static EFI_GUID ConsoleControlProtocolGuid = EFI_CONSOLE_CONTROL_PROTOCOL_GUID;
60 static EFI_CONSOLE_CONTROL_PROTOCOL *ConsoleControl = NULL;
61
62 static EFI_GUID UgaDrawProtocolGuid = EFI_UGA_DRAW_PROTOCOL_GUID;
63 static EFI_UGA_DRAW_PROTOCOL *UgaDraw = NULL;
64
65 static EFI_GUID GraphicsOutputProtocolGuid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
66 static EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput = NULL;
67
68 static BOOLEAN egHasGraphics = FALSE;
69 static UINTN egScreenWidth = 800;
70 static UINTN egScreenHeight = 600;
71
72 //
73 // Screen handling
74 //
75
76 static VOID egDetermineScreenSize(VOID) {
77 EFI_STATUS Status = EFI_SUCCESS;
78 UINT32 UGAWidth, UGAHeight, UGADepth, UGARefreshRate;
79
80 // get screen size
81 egHasGraphics = FALSE;
82 if (GraphicsOutput != NULL) {
83 egScreenWidth = GraphicsOutput->Mode->Info->HorizontalResolution;
84 egScreenHeight = GraphicsOutput->Mode->Info->VerticalResolution;
85 egHasGraphics = TRUE;
86 } else if (UgaDraw != NULL) {
87 Status = refit_call5_wrapper(UgaDraw->GetMode, UgaDraw, &UGAWidth, &UGAHeight, &UGADepth, &UGARefreshRate);
88 if (EFI_ERROR(Status)) {
89 UgaDraw = NULL; // graphics not available
90 } else {
91 egScreenWidth = UGAWidth;
92 egScreenHeight = UGAHeight;
93 egHasGraphics = TRUE;
94 }
95 }
96 }
97
98 VOID egInitScreen(VOID)
99 {
100 EFI_STATUS Status = EFI_SUCCESS;
101
102 // get protocols
103 Status = LibLocateProtocol(&ConsoleControlProtocolGuid, (VOID **) &ConsoleControl);
104 if (EFI_ERROR(Status))
105 ConsoleControl = NULL;
106
107 Status = LibLocateProtocol(&UgaDrawProtocolGuid, (VOID **) &UgaDraw);
108 if (EFI_ERROR(Status))
109 UgaDraw = NULL;
110
111 Status = LibLocateProtocol(&GraphicsOutputProtocolGuid, (VOID **) &GraphicsOutput);
112 if (EFI_ERROR(Status))
113 GraphicsOutput = NULL;
114
115 egDetermineScreenSize();
116 }
117
118 // Convert a graphics mode (in *ModeWidth) to a width and height (returned in
119 // *ModeWidth and *Height, respectively).
120 // Returns TRUE if successful, FALSE if not (invalid mode)
121 BOOLEAN egGetResFromMode(UINTN *ModeWidth, UINTN *Height) {
122 UINTN Size;
123 EFI_STATUS Status;
124 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info = NULL;
125
126 Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, *ModeWidth, &Size, &Info);
127 if ((Status == EFI_SUCCESS) && (Info != NULL)) {
128 *ModeWidth = Info->HorizontalResolution;
129 *Height = Info->VerticalResolution;
130 return TRUE;
131 }
132 return FALSE;
133 } // BOOLEAN egGetResFromMode()
134
135 // Sets the screen resolution to the specified value, if possible. If *ScreenHeight
136 // is 0 and GOP mode is detected, assume that *ScreenWidth contains a GOP mode
137 // number rather than a horizontal resolution. If the specified resolution is not
138 // valid, displays a warning with the valid modes on GOP (UEFI) systems, or silently
139 // fails on UGA (EFI 1.x) systems. Note that this function attempts to set ANY screen
140 // resolution, even 0x0 or ridiculously large values.
141 // Upon success, returns actual screen resolution in *ScreenWidth and *ScreenHeight.
142 // These values are unchanged upon failure.
143 // Returns TRUE if successful, FALSE if not.
144 BOOLEAN egSetScreenSize(IN OUT UINTN *ScreenWidth, IN OUT UINTN *ScreenHeight) {
145 EFI_STATUS Status = EFI_SUCCESS;
146 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info;
147 UINT32 ModeNum = 0;
148 UINTN Size;
149 BOOLEAN ModeSet = FALSE;
150 UINT32 UGAWidth, UGAHeight, UGADepth, UGARefreshRate;
151
152 if (GraphicsOutput != NULL) { // GOP mode (UEFI)
153 if (*ScreenHeight == 0) { // User specified a mode number (stored in *ScreenWidth); use it directly
154 ModeNum = (UINT32) *ScreenWidth;
155 if (egGetResFromMode(ScreenWidth, ScreenHeight) && (
156 refit_call2_wrapper(GraphicsOutput->SetMode, GraphicsOutput, ModeNum) == EFI_SUCCESS)) {
157 ModeSet = TRUE;
158 }
159 // if ((*ScreenWidth == GraphicsOutput->Mode->Mode)) { // user requested current mode; do nothing
160 // ModeSet = TRUE;
161 // *ScreenWidth = Info->HorizontalResolution;
162 // *ScreenHeight = Info->VerticalResolution;
163 // } else {
164 /* ModeNum = (UINT32) *ScreenWidth;
165 Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, ModeNum, &Size, &Info);
166 if (Status == EFI_SUCCESS) {
167 Status = refit_call2_wrapper(GraphicsOutput->SetMode, GraphicsOutput, ModeNum);
168 if (Status == EFI_SUCCESS) {
169 ModeSet = TRUE;
170 *ScreenWidth = Info->HorizontalResolution;
171 *ScreenHeight = Info->VerticalResolution;
172 } // if set mode OK
173 } // if queried mode OK */
174 // } // if/else
175
176 // User specified width & height; must find mode -- but only if change is required....
177 } else {
178 Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, GraphicsOutput->Mode->Mode, &Size, &Info);
179 // if ((Status == EFI_SUCCESS) && (Info->HorizontalResolution == *ScreenWidth) &&
180 // (Info->VerticalResolution == *ScreenHeight) && !AlwaysSet) {
181 // ModeSet = TRUE; // user requested current mode; do nothing
182 // } else {
183 // Do a loop through the modes to see if the specified one is available;
184 // and if so, switch to it....
185 do {
186 Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, ModeNum, &Size, &Info);
187 if ((Status == EFI_SUCCESS) && (Size >= sizeof(*Info)) &&
188 (Info->HorizontalResolution == *ScreenWidth) && (Info->VerticalResolution == *ScreenHeight)) {
189 Status = refit_call2_wrapper(GraphicsOutput->SetMode, GraphicsOutput, ModeNum);
190 ModeSet = (Status == EFI_SUCCESS);
191 } // if
192 } while ((++ModeNum < GraphicsOutput->Mode->MaxMode) && !ModeSet);
193 // } // if/else
194 } // if/else
195
196 if (ModeSet) {
197 egScreenWidth = *ScreenWidth;
198 egScreenHeight = *ScreenHeight;
199 } else {// If unsuccessful, display an error message for the user....
200 SwitchToText(FALSE);
201 Print(L"Error setting graphics mode %d x %d; using default mode!\nAvailable modes are:\n", *ScreenWidth, *ScreenHeight);
202 ModeNum = 0;
203 do {
204 Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, ModeNum, &Size, &Info);
205 if ((Status == EFI_SUCCESS) && (Info != NULL)) {
206 Print(L"Mode %d: %d x %d\n", ModeNum, Info->HorizontalResolution, Info->VerticalResolution);
207 } // else
208 } while (++ModeNum < GraphicsOutput->Mode->MaxMode);
209 PauseForKey();
210 SwitchToGraphics();
211 } // if()
212
213 } else if (UgaDraw != NULL) { // UGA mode (EFI 1.x)
214 // Try to use current color depth & refresh rate for new mode. Maybe not the best choice
215 // in all cases, but I don't know how to probe for alternatives....
216 Status = refit_call5_wrapper(UgaDraw->GetMode, UgaDraw, &UGAWidth, &UGAHeight, &UGADepth, &UGARefreshRate);
217 Status = refit_call5_wrapper(UgaDraw->SetMode, UgaDraw, *ScreenWidth, *ScreenHeight, UGADepth, UGARefreshRate);
218 if (Status == EFI_SUCCESS) {
219 egScreenWidth = *ScreenWidth;
220 egScreenHeight = *ScreenHeight;
221 ModeSet = TRUE;
222 } else {
223 // TODO: Find a list of supported modes and display it.
224 // NOTE: Below doesn't actually appear unless we explicitly switch to text mode.
225 // This is just a placeholder until something better can be done....
226 Print(L"Error setting graphics mode %d x %d; unsupported mode!\n");
227 } // if/else
228 } // if/else if
229 return (ModeSet);
230 } // BOOLEAN egSetScreenSize()
231
232 VOID egGetScreenSize(OUT UINTN *ScreenWidth, OUT UINTN *ScreenHeight)
233 {
234 egDetermineScreenSize();
235
236 if (ScreenWidth != NULL)
237 *ScreenWidth = egScreenWidth;
238 if (ScreenHeight != NULL)
239 *ScreenHeight = egScreenHeight;
240 }
241
242 // Set a text mode.
243 // Returns TRUE if the mode actually changed, FALSE otherwise.
244 // Note that a FALSE return value can mean either an error or no change
245 // necessary.
246 BOOLEAN egSetTextMode(UINT32 RequestedMode) {
247 UINTN i = 0, Width, Height;
248 EFI_STATUS Status;
249 BOOLEAN ChangedIt = FALSE;
250
251 if (RequestedMode != ST->ConOut->Mode->Mode) {
252 // SwitchToGraphics();
253 Status = refit_call2_wrapper(ST->ConOut->SetMode, ST->ConOut, RequestedMode);
254 if (Status == EFI_SUCCESS) {
255 ChangedIt = TRUE;
256 } else {
257 SwitchToText(FALSE);
258 Print(L"Error setting text mode %d; available modes are:\n", RequestedMode);
259 do {
260 Status = refit_call4_wrapper(ST->ConOut->QueryMode, ST->ConOut, i, &Width, &Height);
261 if (Status == EFI_SUCCESS)
262 Print(L"Mode %d: %d x %d\n", i, Width, Height);
263 } while (++i < ST->ConOut->Mode->MaxMode);
264
265 PauseForKey();
266 SwitchToGraphics();
267 } // if/else successful change
268 } // if need to change mode
269 return ChangedIt;
270 } // BOOLEAN egSetTextMode()
271
272 CHAR16 * egScreenDescription(VOID)
273 {
274 CHAR16 *GraphicsInfo, *TextInfo = NULL;
275
276 GraphicsInfo = AllocateZeroPool(256 * sizeof(CHAR16));
277 if (GraphicsInfo == NULL)
278 return L"memory allocation error";
279
280 if (egHasGraphics) {
281 if (GraphicsOutput != NULL) {
282 SPrint(GraphicsInfo, 255, L"Graphics Output (UEFI), %dx%d", egScreenWidth, egScreenHeight);
283 } else if (UgaDraw != NULL) {
284 GraphicsInfo = AllocateZeroPool(256 * sizeof(CHAR16));
285 SPrint(GraphicsInfo, 255, L"UGA Draw (EFI 1.10), %dx%d", egScreenWidth, egScreenHeight);
286 } else {
287 MyFreePool(GraphicsInfo);
288 MyFreePool(TextInfo);
289 return L"Internal Error";
290 }
291 if (!AllowGraphicsMode) { // graphics-capable HW, but in text mode
292 TextInfo = AllocateZeroPool(256 * sizeof(CHAR16));
293 SPrint(TextInfo, 255, L"(in %dx%d text mode)", ConWidth, ConHeight);
294 MergeStrings(&GraphicsInfo, TextInfo, L' ');
295 }
296 } else {
297 SPrint(GraphicsInfo, 255, L"Text-foo console, %dx%d", ConWidth, ConHeight);
298 }
299 MyFreePool(TextInfo);
300 return GraphicsInfo;
301 }
302
303 BOOLEAN egHasGraphicsMode(VOID)
304 {
305 return egHasGraphics;
306 }
307
308 BOOLEAN egIsGraphicsModeEnabled(VOID)
309 {
310 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode;
311
312 if (ConsoleControl != NULL) {
313 refit_call4_wrapper(ConsoleControl->GetMode, ConsoleControl, &CurrentMode, NULL, NULL);
314 return (CurrentMode == EfiConsoleControlScreenGraphics) ? TRUE : FALSE;
315 }
316
317 return FALSE;
318 }
319
320 VOID egSetGraphicsModeEnabled(IN BOOLEAN Enable)
321 {
322 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode;
323 EFI_CONSOLE_CONTROL_SCREEN_MODE NewMode;
324
325 if (ConsoleControl != NULL) {
326 refit_call4_wrapper(ConsoleControl->GetMode, ConsoleControl, &CurrentMode, NULL, NULL);
327
328 NewMode = Enable ? EfiConsoleControlScreenGraphics
329 : EfiConsoleControlScreenText;
330 if (CurrentMode != NewMode)
331 refit_call2_wrapper(ConsoleControl->SetMode, ConsoleControl, NewMode);
332 }
333 }
334
335 //
336 // Drawing to the screen
337 //
338
339 VOID egClearScreen(IN EG_PIXEL *Color)
340 {
341 EFI_UGA_PIXEL FillColor;
342
343 if (!egHasGraphics)
344 return;
345
346 if (Color != NULL) {
347 FillColor.Red = Color->r;
348 FillColor.Green = Color->g;
349 FillColor.Blue = Color->b;
350 } else {
351 FillColor.Red = 0x0;
352 FillColor.Green = 0x0;
353 FillColor.Blue = 0x0;
354 }
355 FillColor.Reserved = 0;
356
357 if (GraphicsOutput != NULL) {
358 // EFI_GRAPHICS_OUTPUT_BLT_PIXEL and EFI_UGA_PIXEL have the same
359 // layout, and the header from TianoCore actually defines them
360 // to be the same type.
361 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)&FillColor, EfiBltVideoFill,
362 0, 0, 0, 0, egScreenWidth, egScreenHeight, 0);
363 } else if (UgaDraw != NULL) {
364 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, &FillColor, EfiUgaVideoFill, 0, 0, 0, 0, egScreenWidth, egScreenHeight, 0);
365 }
366 }
367
368 VOID egDrawImage(IN EG_IMAGE *Image, IN UINTN ScreenPosX, IN UINTN ScreenPosY)
369 {
370 if (!egHasGraphics)
371 return;
372
373 if (Image->HasAlpha) {
374 Image->HasAlpha = FALSE;
375 egSetPlane(PLPTR(Image, a), 0, Image->Width * Image->Height);
376 }
377
378 if (GraphicsOutput != NULL) {
379 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)Image->PixelData, EfiBltBufferToVideo,
380 0, 0, ScreenPosX, ScreenPosY, Image->Width, Image->Height, 0);
381 } else if (UgaDraw != NULL) {
382 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, (EFI_UGA_PIXEL *)Image->PixelData, EfiUgaBltBufferToVideo,
383 0, 0, ScreenPosX, ScreenPosY, Image->Width, Image->Height, 0);
384 }
385 }
386
387 VOID egDrawImageArea(IN EG_IMAGE *Image,
388 IN UINTN AreaPosX, IN UINTN AreaPosY,
389 IN UINTN AreaWidth, IN UINTN AreaHeight,
390 IN UINTN ScreenPosX, IN UINTN ScreenPosY)
391 {
392 if (!egHasGraphics)
393 return;
394
395 egRestrictImageArea(Image, AreaPosX, AreaPosY, &AreaWidth, &AreaHeight);
396 if (AreaWidth == 0)
397 return;
398
399 if (Image->HasAlpha) {
400 Image->HasAlpha = FALSE;
401 egSetPlane(PLPTR(Image, a), 0, Image->Width * Image->Height);
402 }
403
404 if (GraphicsOutput != NULL) {
405 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)Image->PixelData, EfiBltBufferToVideo,
406 AreaPosX, AreaPosY, ScreenPosX, ScreenPosY, AreaWidth, AreaHeight, Image->Width * 4);
407 } else if (UgaDraw != NULL) {
408 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, (EFI_UGA_PIXEL *)Image->PixelData, EfiUgaBltBufferToVideo,
409 AreaPosX, AreaPosY, ScreenPosX, ScreenPosY, AreaWidth, AreaHeight, Image->Width * 4);
410 }
411 }
412
413 // Display a message in the center of the screen, surrounded by a box of the
414 // specified color. For the moment, uses graphics calls only. (It still works
415 // in text mode on GOP/UEFI systems, but not on UGA/EFI 1.x systems.)
416 VOID egDisplayMessage(IN CHAR16 *Text, EG_PIXEL *BGColor) {
417 UINTN BoxWidth, BoxHeight;
418 EG_IMAGE *Box;
419
420 if ((Text != NULL) && (BGColor != NULL)) {
421 BoxWidth = (StrLen(Text) + 2) * FONT_CELL_WIDTH;
422 if (BoxWidth > egScreenWidth)
423 BoxWidth = egScreenWidth;
424 BoxHeight = 2 * FONT_CELL_HEIGHT;
425 Box = egCreateFilledImage(BoxWidth, BoxHeight, FALSE, BGColor);
426 egRenderText(Text, Box, FONT_CELL_WIDTH, FONT_CELL_HEIGHT / 2);
427 egDrawImage(Box, (egScreenWidth - BoxWidth) / 2, (egScreenHeight - BoxHeight) / 2);
428 } // if non-NULL inputs
429 } // VOID egDisplayMessage()
430
431 //
432 // Make a screenshot
433 //
434
435 VOID egScreenShot(VOID)
436 {
437 EFI_STATUS Status;
438 EG_IMAGE *Image;
439 UINT8 *FileData;
440 UINTN FileDataLength;
441 UINTN Index;
442
443 if (!egHasGraphics)
444 return;
445
446 // allocate a buffer for the whole screen
447 Image = egCreateImage(egScreenWidth, egScreenHeight, FALSE);
448 if (Image == NULL) {
449 Print(L"Error egCreateImage returned NULL\n");
450 goto bailout_wait;
451 }
452
453 // get full screen image
454 if (GraphicsOutput != NULL) {
455 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)Image->PixelData, EfiBltVideoToBltBuffer,
456 0, 0, 0, 0, Image->Width, Image->Height, 0);
457 } else if (UgaDraw != NULL) {
458 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, (EFI_UGA_PIXEL *)Image->PixelData, EfiUgaVideoToBltBuffer,
459 0, 0, 0, 0, Image->Width, Image->Height, 0);
460 }
461
462 // encode as BMP
463 egEncodeBMP(Image, &FileData, &FileDataLength);
464 egFreeImage(Image);
465 if (FileData == NULL) {
466 Print(L"Error egEncodeBMP returned NULL\n");
467 goto bailout_wait;
468 }
469
470 // save to file on the ESP
471 Status = egSaveFile(NULL, L"screenshot.bmp", FileData, FileDataLength);
472 FreePool(FileData);
473 if (EFI_ERROR(Status)) {
474 Print(L"Error egSaveFile: %x\n", Status);
475 goto bailout_wait;
476 }
477
478 return;
479
480 // DEBUG: switch to text mode
481 bailout_wait:
482 egSetGraphicsModeEnabled(FALSE);
483 refit_call3_wrapper(BS->WaitForEvent, 1, &ST->ConIn->WaitForKey, &Index);
484 }
485
486 /* EOF */