3 * Screen handling functions
5 * Copyright (c) 2006 Christoph Pfisterer
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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.
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.
37 * Modifications copyright (c) 2012 Roderick W. Smith
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.
46 #include "../refind/screen.h"
47 #include "../refind/lib.h"
48 #include "../include/refit_call_wrapper.h"
50 #include <efiUgaDraw.h>
51 #include <efiConsoleControl.h>
53 #ifndef __MAKEWITH_GNUEFI
54 #define LibLocateProtocol EfiLibLocateProtocol
57 // Console defines and variables
59 static EFI_GUID ConsoleControlProtocolGuid
= EFI_CONSOLE_CONTROL_PROTOCOL_GUID
;
60 static EFI_CONSOLE_CONTROL_PROTOCOL
*ConsoleControl
= NULL
;
62 static EFI_GUID UgaDrawProtocolGuid
= EFI_UGA_DRAW_PROTOCOL_GUID
;
63 static EFI_UGA_DRAW_PROTOCOL
*UgaDraw
= NULL
;
65 static EFI_GUID GraphicsOutputProtocolGuid
= EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID
;
66 static EFI_GRAPHICS_OUTPUT_PROTOCOL
*GraphicsOutput
= NULL
;
68 static BOOLEAN egHasGraphics
= FALSE
;
69 static UINTN egScreenWidth
= 800;
70 static UINTN egScreenHeight
= 600;
76 static VOID
egDetermineScreenSize(VOID
) {
77 EFI_STATUS Status
= EFI_SUCCESS
;
78 UINT32 UGAWidth
, UGAHeight
, UGADepth
, UGARefreshRate
;
81 egHasGraphics
= FALSE
;
82 if (GraphicsOutput
!= NULL
) {
83 egScreenWidth
= GraphicsOutput
->Mode
->Info
->HorizontalResolution
;
84 egScreenHeight
= GraphicsOutput
->Mode
->Info
->VerticalResolution
;
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
91 egScreenWidth
= UGAWidth
;
92 egScreenHeight
= UGAHeight
;
98 VOID
egInitScreen(VOID
)
100 EFI_STATUS Status
= EFI_SUCCESS
;
103 Status
= LibLocateProtocol(&ConsoleControlProtocolGuid
, (VOID
**) &ConsoleControl
);
104 if (EFI_ERROR(Status
))
105 ConsoleControl
= NULL
;
107 Status
= LibLocateProtocol(&UgaDrawProtocolGuid
, (VOID
**) &UgaDraw
);
108 if (EFI_ERROR(Status
))
111 Status
= LibLocateProtocol(&GraphicsOutputProtocolGuid
, (VOID
**) &GraphicsOutput
);
112 if (EFI_ERROR(Status
))
113 GraphicsOutput
= NULL
;
115 egDetermineScreenSize();
118 // Sets the screen resolution to the specified value, if possible. If *ScreenHeight
119 // is 0 and GOP mode is detected, assume that *ScreenWidth contains a GOP mode
120 // number rather than a horizontal resolution. If the specified resolution is not
121 // valid, displays a warning with the valid modes on GOP (UEFI) systems, or silently
122 // fails on UGA (EFI 1.x) systems. Note that this function attempts to set ANY screen
123 // resolution, even 0x0 or ridiculously large values.
124 // Upon success, returns actual screen resolution in *ScreenWidth and *ScreenHeight.
125 // These values are unchanged upon failure.
126 // Returns TRUE if successful, FALSE if not.
127 BOOLEAN
egSetScreenSize(IN OUT UINTN
*ScreenWidth
, IN OUT UINTN
*ScreenHeight
) {
128 EFI_STATUS Status
= EFI_SUCCESS
;
129 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
*Info
;
132 BOOLEAN ModeSet
= FALSE
;
133 UINT32 UGAWidth
, UGAHeight
, UGADepth
, UGARefreshRate
;
135 if (GraphicsOutput
!= NULL
) { // GOP mode (UEFI)
136 if (*ScreenHeight
== 0) { // User specified a mode number (stored in *ScreenWidth); use it directly
137 // if ((*ScreenWidth == GraphicsOutput->Mode->Mode)) { // user requested current mode; do nothing
139 // *ScreenWidth = Info->HorizontalResolution;
140 // *ScreenHeight = Info->VerticalResolution;
142 ModeNum
= (UINT32
) *ScreenWidth
;
143 Status
= refit_call4_wrapper(GraphicsOutput
->QueryMode
, GraphicsOutput
, ModeNum
, &Size
, &Info
);
144 if (Status
== EFI_SUCCESS
) {
145 Status
= refit_call2_wrapper(GraphicsOutput
->SetMode
, GraphicsOutput
, ModeNum
);
146 if (Status
== EFI_SUCCESS
) {
148 *ScreenWidth
= Info
->HorizontalResolution
;
149 *ScreenHeight
= Info
->VerticalResolution
;
151 } // if queried mode OK
154 // User specified width & height; must find mode -- but only if change is required....
156 Status
= refit_call4_wrapper(GraphicsOutput
->QueryMode
, GraphicsOutput
, GraphicsOutput
->Mode
->Mode
, &Size
, &Info
);
157 // if ((Status == EFI_SUCCESS) && (Info->HorizontalResolution == *ScreenWidth) &&
158 // (Info->VerticalResolution == *ScreenHeight) && !AlwaysSet) {
159 // ModeSet = TRUE; // user requested current mode; do nothing
161 // Do a loop through the modes to see if the specified one is available;
162 // and if so, switch to it....
164 Status
= refit_call4_wrapper(GraphicsOutput
->QueryMode
, GraphicsOutput
, ModeNum
, &Size
, &Info
);
165 if ((Status
== EFI_SUCCESS
) && (Size
>= sizeof(*Info
)) &&
166 (Info
->HorizontalResolution
== *ScreenWidth
) && (Info
->VerticalResolution
== *ScreenHeight
)) {
167 Status
= refit_call2_wrapper(GraphicsOutput
->SetMode
, GraphicsOutput
, ModeNum
);
168 ModeSet
= (Status
== EFI_SUCCESS
);
170 } while ((++ModeNum
< GraphicsOutput
->Mode
->MaxMode
) && !ModeSet
);
175 egScreenWidth
= *ScreenWidth
;
176 egScreenHeight
= *ScreenHeight
;
177 } else {// If unsuccessful, display an error message for the user....
179 Print(L
"Error setting graphics mode %d x %d; using default mode!\nAvailable modes are:\n", *ScreenWidth
, *ScreenHeight
);
182 Status
= refit_call4_wrapper(GraphicsOutput
->QueryMode
, GraphicsOutput
, ModeNum
, &Size
, &Info
);
183 if ((Status
== EFI_SUCCESS
) && (Info
!= NULL
)) {
184 Print(L
"Mode %d: %d x %d\n", ModeNum
, Info
->HorizontalResolution
, Info
->VerticalResolution
);
186 } while (++ModeNum
< GraphicsOutput
->Mode
->MaxMode
);
191 } else if (UgaDraw
!= NULL
) { // UGA mode (EFI 1.x)
192 // Try to use current color depth & refresh rate for new mode. Maybe not the best choice
193 // in all cases, but I don't know how to probe for alternatives....
194 Status
= refit_call5_wrapper(UgaDraw
->GetMode
, UgaDraw
, &UGAWidth
, &UGAHeight
, &UGADepth
, &UGARefreshRate
);
195 Status
= refit_call5_wrapper(UgaDraw
->SetMode
, UgaDraw
, *ScreenWidth
, *ScreenHeight
, UGADepth
, UGARefreshRate
);
196 if (Status
== EFI_SUCCESS
) {
197 egScreenWidth
= *ScreenWidth
;
198 egScreenHeight
= *ScreenHeight
;
201 // TODO: Find a list of supported modes and display it.
202 // NOTE: Below doesn't actually appear unless we explicitly switch to text mode.
203 // This is just a placeholder until something better can be done....
204 Print(L
"Error setting graphics mode %d x %d; unsupported mode!\n");
208 } // BOOLEAN egSetScreenSize()
210 VOID
egGetScreenSize(OUT UINTN
*ScreenWidth
, OUT UINTN
*ScreenHeight
)
212 egDetermineScreenSize();
214 if (ScreenWidth
!= NULL
)
215 *ScreenWidth
= egScreenWidth
;
216 if (ScreenHeight
!= NULL
)
217 *ScreenHeight
= egScreenHeight
;
221 // Returns TRUE if the mode actually changed, FALSE otherwise.
222 // Note that a FALSE return value can mean either an error or no change
224 BOOLEAN
egSetTextMode(UINT32 RequestedMode
) {
225 UINTN i
= 0, Width
, Height
;
227 BOOLEAN ChangedIt
= FALSE
;
229 if (RequestedMode
!= ST
->ConOut
->Mode
->Mode
) {
230 // SwitchToGraphics();
231 Status
= refit_call2_wrapper(ST
->ConOut
->SetMode
, ST
->ConOut
, RequestedMode
);
232 if (Status
== EFI_SUCCESS
) {
236 Print(L
"Error setting text mode %d; available modes are:\n", RequestedMode
);
238 Status
= refit_call4_wrapper(ST
->ConOut
->QueryMode
, ST
->ConOut
, i
, &Width
, &Height
);
239 if (Status
== EFI_SUCCESS
)
240 Print(L
"Mode %d: %d x %d\n", i
, Width
, Height
);
241 } while (++i
< ST
->ConOut
->Mode
->MaxMode
);
245 } // if/else successful change
246 } // if need to change mode
248 } // BOOLEAN egSetTextMode()
250 CHAR16
* egScreenDescription(VOID
)
252 CHAR16
*GraphicsInfo
, *TextInfo
= NULL
;
254 GraphicsInfo
= AllocateZeroPool(256 * sizeof(CHAR16
));
255 if (GraphicsInfo
== NULL
)
256 return L
"memory allocation error";
259 if (GraphicsOutput
!= NULL
) {
260 SPrint(GraphicsInfo
, 255, L
"Graphics Output (UEFI), %dx%d", egScreenWidth
, egScreenHeight
);
261 } else if (UgaDraw
!= NULL
) {
262 GraphicsInfo
= AllocateZeroPool(256 * sizeof(CHAR16
));
263 SPrint(GraphicsInfo
, 255, L
"UGA Draw (EFI 1.10), %dx%d", egScreenWidth
, egScreenHeight
);
265 MyFreePool(GraphicsInfo
);
266 MyFreePool(TextInfo
);
267 return L
"Internal Error";
269 if (!AllowGraphicsMode
) { // graphics-capable HW, but in text mode
270 TextInfo
= AllocateZeroPool(256 * sizeof(CHAR16
));
271 SPrint(TextInfo
, 255, L
"(in %dx%d text mode)", ConWidth
, ConHeight
);
272 MergeStrings(&GraphicsInfo
, TextInfo
, L
' ');
275 SPrint(GraphicsInfo
, 255, L
"Text-foo console, %dx%d", ConWidth
, ConHeight
);
277 MyFreePool(TextInfo
);
281 BOOLEAN
egHasGraphicsMode(VOID
)
283 return egHasGraphics
;
286 BOOLEAN
egIsGraphicsModeEnabled(VOID
)
288 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode
;
290 if (ConsoleControl
!= NULL
) {
291 refit_call4_wrapper(ConsoleControl
->GetMode
, ConsoleControl
, &CurrentMode
, NULL
, NULL
);
292 return (CurrentMode
== EfiConsoleControlScreenGraphics
) ? TRUE
: FALSE
;
298 VOID
egSetGraphicsModeEnabled(IN BOOLEAN Enable
)
300 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode
;
301 EFI_CONSOLE_CONTROL_SCREEN_MODE NewMode
;
303 egSetTextMode(GlobalConfig
.RequestedTextMode
);
304 if (ConsoleControl
!= NULL
) {
305 refit_call4_wrapper(ConsoleControl
->GetMode
, ConsoleControl
, &CurrentMode
, NULL
, NULL
);
307 NewMode
= Enable
? EfiConsoleControlScreenGraphics
308 : EfiConsoleControlScreenText
;
309 if (CurrentMode
!= NewMode
)
310 refit_call2_wrapper(ConsoleControl
->SetMode
, ConsoleControl
, NewMode
);
315 // Drawing to the screen
318 VOID
egClearScreen(IN EG_PIXEL
*Color
)
320 EFI_UGA_PIXEL FillColor
;
326 FillColor
.Red
= Color
->r
;
327 FillColor
.Green
= Color
->g
;
328 FillColor
.Blue
= Color
->b
;
331 FillColor
.Green
= 0x0;
332 FillColor
.Blue
= 0x0;
334 FillColor
.Reserved
= 0;
336 if (GraphicsOutput
!= NULL
) {
337 // EFI_GRAPHICS_OUTPUT_BLT_PIXEL and EFI_UGA_PIXEL have the same
338 // layout, and the header from TianoCore actually defines them
339 // to be the same type.
340 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)&FillColor
, EfiBltVideoFill
,
341 0, 0, 0, 0, egScreenWidth
, egScreenHeight
, 0);
342 } else if (UgaDraw
!= NULL
) {
343 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, &FillColor
, EfiUgaVideoFill
, 0, 0, 0, 0, egScreenWidth
, egScreenHeight
, 0);
347 VOID
egDrawImage(IN EG_IMAGE
*Image
, IN UINTN ScreenPosX
, IN UINTN ScreenPosY
)
352 if (Image
->HasAlpha
) {
353 Image
->HasAlpha
= FALSE
;
354 egSetPlane(PLPTR(Image
, a
), 0, Image
->Width
* Image
->Height
);
357 if (GraphicsOutput
!= NULL
) {
358 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)Image
->PixelData
, EfiBltBufferToVideo
,
359 0, 0, ScreenPosX
, ScreenPosY
, Image
->Width
, Image
->Height
, 0);
360 } else if (UgaDraw
!= NULL
) {
361 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, (EFI_UGA_PIXEL
*)Image
->PixelData
, EfiUgaBltBufferToVideo
,
362 0, 0, ScreenPosX
, ScreenPosY
, Image
->Width
, Image
->Height
, 0);
366 VOID
egDrawImageArea(IN EG_IMAGE
*Image
,
367 IN UINTN AreaPosX
, IN UINTN AreaPosY
,
368 IN UINTN AreaWidth
, IN UINTN AreaHeight
,
369 IN UINTN ScreenPosX
, IN UINTN ScreenPosY
)
374 egRestrictImageArea(Image
, AreaPosX
, AreaPosY
, &AreaWidth
, &AreaHeight
);
378 if (Image
->HasAlpha
) {
379 Image
->HasAlpha
= FALSE
;
380 egSetPlane(PLPTR(Image
, a
), 0, Image
->Width
* Image
->Height
);
383 if (GraphicsOutput
!= NULL
) {
384 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)Image
->PixelData
, EfiBltBufferToVideo
,
385 AreaPosX
, AreaPosY
, ScreenPosX
, ScreenPosY
, AreaWidth
, AreaHeight
, Image
->Width
* 4);
386 } else if (UgaDraw
!= NULL
) {
387 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, (EFI_UGA_PIXEL
*)Image
->PixelData
, EfiUgaBltBufferToVideo
,
388 AreaPosX
, AreaPosY
, ScreenPosX
, ScreenPosY
, AreaWidth
, AreaHeight
, Image
->Width
* 4);
392 // Display a message in the center of the screen, surrounded by a box of the
393 // specified color. For the moment, uses graphics calls only. (It still works
394 // in text mode on GOP/UEFI systems, but not on UGA/EFI 1.x systems.)
395 VOID
egDisplayMessage(IN CHAR16
*Text
, EG_PIXEL
*BGColor
) {
396 UINTN BoxWidth
, BoxHeight
;
399 if ((Text
!= NULL
) && (BGColor
!= NULL
)) {
400 BoxWidth
= (StrLen(Text
) + 2) * FONT_CELL_WIDTH
;
401 if (BoxWidth
> egScreenWidth
)
402 BoxWidth
= egScreenWidth
;
403 BoxHeight
= 2 * FONT_CELL_HEIGHT
;
404 Box
= egCreateFilledImage(BoxWidth
, BoxHeight
, FALSE
, BGColor
);
405 egRenderText(Text
, Box
, FONT_CELL_WIDTH
, FONT_CELL_HEIGHT
/ 2);
406 egDrawImage(Box
, (egScreenWidth
- BoxWidth
) / 2, (egScreenHeight
- BoxHeight
) / 2);
407 } // if non-NULL inputs
408 } // VOID egDisplayMessage()
414 VOID
egScreenShot(VOID
)
419 UINTN FileDataLength
;
425 // allocate a buffer for the whole screen
426 Image
= egCreateImage(egScreenWidth
, egScreenHeight
, FALSE
);
428 Print(L
"Error egCreateImage returned NULL\n");
432 // get full screen image
433 if (GraphicsOutput
!= NULL
) {
434 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)Image
->PixelData
, EfiBltVideoToBltBuffer
,
435 0, 0, 0, 0, Image
->Width
, Image
->Height
, 0);
436 } else if (UgaDraw
!= NULL
) {
437 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, (EFI_UGA_PIXEL
*)Image
->PixelData
, EfiUgaVideoToBltBuffer
,
438 0, 0, 0, 0, Image
->Width
, Image
->Height
, 0);
442 egEncodeBMP(Image
, &FileData
, &FileDataLength
);
444 if (FileData
== NULL
) {
445 Print(L
"Error egEncodeBMP returned NULL\n");
449 // save to file on the ESP
450 Status
= egSaveFile(NULL
, L
"screenshot.bmp", FileData
, FileDataLength
);
452 if (EFI_ERROR(Status
)) {
453 Print(L
"Error egSaveFile: %x\n", Status
);
459 // DEBUG: switch to text mode
461 egSetGraphicsModeEnabled(FALSE
);
462 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &Index
);