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.
38 #include "../refind/screen.h"
39 #include "../refind/lib.h"
40 #include "../include/refit_call_wrapper.h"
42 #include <efiUgaDraw.h>
43 #include <efiConsoleControl.h>
45 #ifndef __MAKEWITH_GNUEFI
46 #define LibLocateProtocol EfiLibLocateProtocol
49 // Scan for at least this many text-mode and graphics-mode display modes, even
50 // if the first modes are invalid. The number of text modes scanned must be at
51 // least 3 on some computers to properly list all the available modes when the
52 // user specifies an invalid value, since mode 1 (numbered from 0) is often
53 // invalid, but mode 2 (the third one) is usually valid. AFAIK, gaps never
54 // occur in graphics modes, although I've heard of one case that makes me think
55 // some exceptions may occur.
56 #define MIN_SCAN_TEXT_MODES 3
57 #define MIN_SCAN_GRAPHICS_MODES 1
59 // Console defines and variables
61 static EFI_GUID ConsoleControlProtocolGuid
= EFI_CONSOLE_CONTROL_PROTOCOL_GUID
;
62 static EFI_CONSOLE_CONTROL_PROTOCOL
*ConsoleControl
= NULL
;
64 static EFI_GUID UgaDrawProtocolGuid
= EFI_UGA_DRAW_PROTOCOL_GUID
;
65 static EFI_UGA_DRAW_PROTOCOL
*UgaDraw
= NULL
;
67 static EFI_GUID GraphicsOutputProtocolGuid
= EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID
;
68 static EFI_GRAPHICS_OUTPUT_PROTOCOL
*GraphicsOutput
= NULL
;
70 static BOOLEAN egHasGraphics
= FALSE
;
71 static UINTN egScreenWidth
= 800;
72 static UINTN egScreenHeight
= 600;
78 VOID
egInitScreen(VOID
)
80 EFI_STATUS Status
= EFI_SUCCESS
;
81 UINT32 UGAWidth
, UGAHeight
, UGADepth
, UGARefreshRate
;
84 Status
= LibLocateProtocol(&ConsoleControlProtocolGuid
, (VOID
**) &ConsoleControl
);
85 if (EFI_ERROR(Status
))
86 ConsoleControl
= NULL
;
88 Status
= LibLocateProtocol(&UgaDrawProtocolGuid
, (VOID
**) &UgaDraw
);
89 if (EFI_ERROR(Status
))
92 Status
= LibLocateProtocol(&GraphicsOutputProtocolGuid
, (VOID
**) &GraphicsOutput
);
93 if (EFI_ERROR(Status
))
94 GraphicsOutput
= NULL
;
97 egHasGraphics
= FALSE
;
98 if (GraphicsOutput
!= NULL
) {
99 egScreenWidth
= GraphicsOutput
->Mode
->Info
->HorizontalResolution
;
100 egScreenHeight
= GraphicsOutput
->Mode
->Info
->VerticalResolution
;
101 egHasGraphics
= TRUE
;
102 } else if (UgaDraw
!= NULL
) {
103 Status
= refit_call5_wrapper(UgaDraw
->GetMode
, UgaDraw
, &UGAWidth
, &UGAHeight
, &UGADepth
, &UGARefreshRate
);
104 if (EFI_ERROR(Status
)) {
105 UgaDraw
= NULL
; // graphics not available
107 egScreenWidth
= UGAWidth
;
108 egScreenHeight
= UGAHeight
;
109 egHasGraphics
= TRUE
;
114 // Sets the screen resolution to the specified value, if possible. If *ScreenHeight
115 // is 0 and GOP mode is detected, assume that *ScreenWidth contains a GOP mode
116 // number rather than a horizontal resolution. If the specified resolution is not
117 // valid, displays a warning with the valid modes on GOP (UEFI) systems, or silently
118 // fails on UGA (EFI 1.x) systems. Note that this function attempts to set ANY screen
119 // resolution, even 0x0 or ridiculously large values.
120 // Upon success, returns actual screen resolution in *ScreenWidth and *ScreenHeight.
121 // These values are unchanged upon failure.
122 // Returns TRUE if successful, FALSE if not.
123 BOOLEAN
egSetScreenSize(IN OUT UINTN
*ScreenWidth
, IN OUT UINTN
*ScreenHeight
) {
124 EFI_STATUS Status
= EFI_SUCCESS
;
125 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
*Info
;
128 BOOLEAN ModeSet
= FALSE
;
129 UINT32 UGAWidth
, UGAHeight
, UGADepth
, UGARefreshRate
;
131 if (GraphicsOutput
!= NULL
) { // GOP mode (UEFI)
132 if (*ScreenHeight
== 0) { // User specified a mode number (stored in *ScreenWidth); use it directly
133 ModeNum
= (UINT32
) *ScreenWidth
;
134 Status
= refit_call4_wrapper(GraphicsOutput
->QueryMode
, GraphicsOutput
, ModeNum
, &Size
, &Info
);
135 if (Status
== EFI_SUCCESS
) {
136 Status
= refit_call2_wrapper(GraphicsOutput
->SetMode
, GraphicsOutput
, ModeNum
);
137 if (Status
== EFI_SUCCESS
) {
139 *ScreenWidth
= Info
->HorizontalResolution
;
140 *ScreenHeight
= Info
->VerticalResolution
;
143 } else { // User specified width & height; must find mode
144 // Do a loop through the modes to see if the specified one is available;
145 // and if so, switch to it....
147 Status
= refit_call4_wrapper(GraphicsOutput
->QueryMode
, GraphicsOutput
, ModeNum
, &Size
, &Info
);
148 if ((Status
== EFI_SUCCESS
) && (Size
>= sizeof(*Info
)) &&
149 (Info
->HorizontalResolution
== *ScreenWidth
) && (Info
->VerticalResolution
== *ScreenHeight
)) {
150 Status
= refit_call2_wrapper(GraphicsOutput
->SetMode
, GraphicsOutput
, ModeNum
);
151 ModeSet
= (Status
== EFI_SUCCESS
);
153 } while (((++ModeNum
< MIN_SCAN_GRAPHICS_MODES
) || (Status
== EFI_SUCCESS
)) && !ModeSet
);
154 // while ((Status == EFI_SUCCESS) && (!ModeSet)) {
155 // Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, ModeNum, &Size, &Info);
156 // if ((Status == EFI_SUCCESS) && (Size >= sizeof(*Info)) &&
157 // (Info->HorizontalResolution == *ScreenWidth) && (Info->VerticalResolution == *ScreenHeight)) {
158 // Status = refit_call2_wrapper(GraphicsOutput->SetMode, GraphicsOutput, ModeNum);
159 // ModeSet = (Status == EFI_SUCCESS);
166 egScreenWidth
= *ScreenWidth
;
167 egScreenHeight
= *ScreenHeight
;
168 } else {// If unsuccessful, display an error message for the user....
170 Print(L
"Error setting graphics mode %d x %d; using default mode!\nAvailable modes are:\n", *ScreenWidth
, *ScreenHeight
);
173 Status
= refit_call4_wrapper(GraphicsOutput
->QueryMode
, GraphicsOutput
, ModeNum
, &Size
, &Info
);
174 if ((Status
== EFI_SUCCESS
) && (Info
!= NULL
)) {
175 Print(L
"Mode %d: %d x %d\n", ModeNum
, Info
->HorizontalResolution
, Info
->VerticalResolution
);
177 } while ((++ModeNum
< MIN_SCAN_GRAPHICS_MODES
) || (Status
== EFI_SUCCESS
));
178 // Status = EFI_SUCCESS;
179 // while (Status == EFI_SUCCESS) {
180 // Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, ModeNum, &Size, &Info);
181 // if ((Status == EFI_SUCCESS) && (Size >= sizeof(*Info))) {
182 // Print(L"Mode %d: %d x %d\n", ModeNum, Info->HorizontalResolution, Info->VerticalResolution);
190 } else if (UgaDraw
!= NULL
) { // UGA mode (EFI 1.x)
191 // Try to use current color depth & refresh rate for new mode. Maybe not the best choice
192 // in all cases, but I don't know how to probe for alternatives....
193 Status
= refit_call5_wrapper(UgaDraw
->GetMode
, UgaDraw
, &UGAWidth
, &UGAHeight
, &UGADepth
, &UGARefreshRate
);
194 Status
= refit_call5_wrapper(UgaDraw
->SetMode
, UgaDraw
, *ScreenWidth
, *ScreenHeight
, UGADepth
, UGARefreshRate
);
195 if (Status
== EFI_SUCCESS
) {
196 egScreenWidth
= *ScreenWidth
;
197 egScreenHeight
= *ScreenHeight
;
200 // TODO: Find a list of supported modes and display it.
201 // NOTE: Below doesn't actually appear unless we explicitly switch to text mode.
202 // This is just a placeholder until something better can be done....
203 Print(L
"Error setting graphics mode %d x %d; unsupported mode!\n");
207 } // BOOLEAN egSetScreenSize()
209 VOID
egGetScreenSize(OUT UINTN
*ScreenWidth
, OUT UINTN
*ScreenHeight
)
211 if (ScreenWidth
!= NULL
)
212 *ScreenWidth
= egScreenWidth
;
213 if (ScreenHeight
!= NULL
)
214 *ScreenHeight
= egScreenHeight
;
218 // Returns the mode that was actually set.
219 UINT32
egSetTextMode(UINT32 RequestedMode
) {
220 UINTN i
= 0, Width
, Height
;
221 UINT32 UsedMode
= ST
->ConOut
->Mode
->Mode
;
224 if (RequestedMode
!= ST
->ConOut
->Mode
->Mode
) {
225 Status
= refit_call2_wrapper(ST
->ConOut
->SetMode
, ST
->ConOut
, RequestedMode
);
226 if (Status
== EFI_SUCCESS
) {
227 UsedMode
= RequestedMode
;
230 Print(L
"Error setting text mode %d; available modes are:\n", RequestedMode
);
232 Status
= refit_call4_wrapper(ST
->ConOut
->QueryMode
, ST
->ConOut
, i
, &Width
, &Height
);
233 if (Status
== EFI_SUCCESS
)
234 Print(L
"Mode: %d: %d x %d\n", i
, Width
, Height
);
235 } while ((++i
< MIN_SCAN_TEXT_MODES
) || (Status
== EFI_SUCCESS
));
239 } // if/else successful change
240 } // if need to change mode
242 } // UINT32 egSetTextMode()
244 CHAR16
* egScreenDescription(VOID
)
246 CHAR16
*GraphicsInfo
, *TextInfo
= NULL
;
248 GraphicsInfo
= AllocateZeroPool(256 * sizeof(CHAR16
));
249 if (GraphicsInfo
== NULL
)
250 return L
"memory allocation error";
253 if (GraphicsOutput
!= NULL
) {
254 SPrint(GraphicsInfo
, 255, L
"Graphics Output (UEFI), %dx%d", egScreenWidth
, egScreenHeight
);
255 } else if (UgaDraw
!= NULL
) {
256 GraphicsInfo
= AllocateZeroPool(256 * sizeof(CHAR16
));
257 SPrint(GraphicsInfo
, 255, L
"UGA Draw (EFI 1.10), %dx%d", egScreenWidth
, egScreenHeight
);
259 MyFreePool(GraphicsInfo
);
260 MyFreePool(TextInfo
);
261 return L
"Internal Error";
263 if (!AllowGraphicsMode
) { // graphics-capable HW, but in text mode
264 TextInfo
= AllocateZeroPool(256 * sizeof(CHAR16
));
265 SPrint(TextInfo
, 255, L
"(in %dx%d text mode)", ConWidth
, ConHeight
);
266 MergeStrings(&GraphicsInfo
, TextInfo
, L
' ');
269 SPrint(GraphicsInfo
, 255, L
"Text-foo console, %dx%d", ConWidth
, ConHeight
);
271 MyFreePool(TextInfo
);
275 BOOLEAN
egHasGraphicsMode(VOID
)
277 return egHasGraphics
;
280 BOOLEAN
egIsGraphicsModeEnabled(VOID
)
282 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode
;
284 if (ConsoleControl
!= NULL
) {
285 refit_call4_wrapper(ConsoleControl
->GetMode
, ConsoleControl
, &CurrentMode
, NULL
, NULL
);
286 return (CurrentMode
== EfiConsoleControlScreenGraphics
) ? TRUE
: FALSE
;
292 VOID
egSetGraphicsModeEnabled(IN BOOLEAN Enable
)
294 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode
;
295 EFI_CONSOLE_CONTROL_SCREEN_MODE NewMode
;
297 if (ConsoleControl
!= NULL
) {
298 refit_call4_wrapper(ConsoleControl
->GetMode
, ConsoleControl
, &CurrentMode
, NULL
, NULL
);
300 NewMode
= Enable
? EfiConsoleControlScreenGraphics
301 : EfiConsoleControlScreenText
;
302 if (CurrentMode
!= NewMode
)
303 refit_call2_wrapper(ConsoleControl
->SetMode
, ConsoleControl
, NewMode
);
308 // Drawing to the screen
311 VOID
egClearScreen(IN EG_PIXEL
*Color
)
313 EFI_UGA_PIXEL FillColor
;
319 FillColor
.Red
= Color
->r
;
320 FillColor
.Green
= Color
->g
;
321 FillColor
.Blue
= Color
->b
;
324 FillColor
.Green
= 0x0;
325 FillColor
.Blue
= 0x0;
327 FillColor
.Reserved
= 0;
329 if (GraphicsOutput
!= NULL
) {
330 // EFI_GRAPHICS_OUTPUT_BLT_PIXEL and EFI_UGA_PIXEL have the same
331 // layout, and the header from TianoCore actually defines them
332 // to be the same type.
333 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)&FillColor
, EfiBltVideoFill
,
334 0, 0, 0, 0, egScreenWidth
, egScreenHeight
, 0);
335 } else if (UgaDraw
!= NULL
) {
336 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, &FillColor
, EfiUgaVideoFill
, 0, 0, 0, 0, egScreenWidth
, egScreenHeight
, 0);
340 VOID
egDrawImage(IN EG_IMAGE
*Image
, IN UINTN ScreenPosX
, IN UINTN ScreenPosY
)
345 if (Image
->HasAlpha
) {
346 Image
->HasAlpha
= FALSE
;
347 egSetPlane(PLPTR(Image
, a
), 0, Image
->Width
* Image
->Height
);
350 if (GraphicsOutput
!= NULL
) {
351 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)Image
->PixelData
, EfiBltBufferToVideo
,
352 0, 0, ScreenPosX
, ScreenPosY
, Image
->Width
, Image
->Height
, 0);
353 } else if (UgaDraw
!= NULL
) {
354 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, (EFI_UGA_PIXEL
*)Image
->PixelData
, EfiUgaBltBufferToVideo
,
355 0, 0, ScreenPosX
, ScreenPosY
, Image
->Width
, Image
->Height
, 0);
359 VOID
egDrawImageArea(IN EG_IMAGE
*Image
,
360 IN UINTN AreaPosX
, IN UINTN AreaPosY
,
361 IN UINTN AreaWidth
, IN UINTN AreaHeight
,
362 IN UINTN ScreenPosX
, IN UINTN ScreenPosY
)
367 egRestrictImageArea(Image
, AreaPosX
, AreaPosY
, &AreaWidth
, &AreaHeight
);
371 if (Image
->HasAlpha
) {
372 Image
->HasAlpha
= FALSE
;
373 egSetPlane(PLPTR(Image
, a
), 0, Image
->Width
* Image
->Height
);
376 if (GraphicsOutput
!= NULL
) {
377 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)Image
->PixelData
, EfiBltBufferToVideo
,
378 AreaPosX
, AreaPosY
, ScreenPosX
, ScreenPosY
, AreaWidth
, AreaHeight
, Image
->Width
* 4);
379 } else if (UgaDraw
!= NULL
) {
380 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, (EFI_UGA_PIXEL
*)Image
->PixelData
, EfiUgaBltBufferToVideo
,
381 AreaPosX
, AreaPosY
, ScreenPosX
, ScreenPosY
, AreaWidth
, AreaHeight
, Image
->Width
* 4);
385 // Display a message in the center of the screen, surrounded by a box of the
386 // specified color. For the moment, uses graphics calls only. (It still works
387 // in text mode on GOP/UEFI systems, but not on UGA/EFI 1.x systems.)
388 VOID
egDisplayMessage(IN CHAR16
*Text
, EG_PIXEL
*BGColor
) {
389 UINTN BoxWidth
, BoxHeight
;
392 if ((Text
!= NULL
) && (BGColor
!= NULL
)) {
393 BoxWidth
= (StrLen(Text
) + 2) * FONT_CELL_WIDTH
;
394 if (BoxWidth
> egScreenWidth
)
395 BoxWidth
= egScreenWidth
;
396 BoxHeight
= 2 * FONT_CELL_HEIGHT
;
397 Box
= egCreateFilledImage(BoxWidth
, BoxHeight
, FALSE
, BGColor
);
398 egRenderText(Text
, Box
, FONT_CELL_WIDTH
, FONT_CELL_HEIGHT
/ 2);
399 egDrawImage(Box
, (egScreenWidth
- BoxWidth
) / 2, (egScreenHeight
- BoxHeight
) / 2);
400 } // if non-NULL inputs
401 } // VOID egDisplayMessage()
407 VOID
egScreenShot(VOID
)
412 UINTN FileDataLength
;
418 // allocate a buffer for the whole screen
419 Image
= egCreateImage(egScreenWidth
, egScreenHeight
, FALSE
);
421 Print(L
"Error egCreateImage returned NULL\n");
425 // get full screen image
426 if (GraphicsOutput
!= NULL
) {
427 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)Image
->PixelData
, EfiBltVideoToBltBuffer
,
428 0, 0, 0, 0, Image
->Width
, Image
->Height
, 0);
429 } else if (UgaDraw
!= NULL
) {
430 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, (EFI_UGA_PIXEL
*)Image
->PixelData
, EfiUgaVideoToBltBuffer
,
431 0, 0, 0, 0, Image
->Width
, Image
->Height
, 0);
435 egEncodeBMP(Image
, &FileData
, &FileDataLength
);
437 if (FileData
== NULL
) {
438 Print(L
"Error egEncodeBMP returned NULL\n");
442 // save to file on the ESP
443 Status
= egSaveFile(NULL
, L
"screenshot.bmp", FileData
, FileDataLength
);
445 if (EFI_ERROR(Status
)) {
446 Print(L
"Error egSaveFile: %x\n", Status
);
452 // DEBUG: switch to text mode
454 egSetGraphicsModeEnabled(FALSE
);
455 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &Index
);