1 /** @file
2   GopBltLib - Library to perform blt using the UEFI Graphics Output Protocol.
3 
4   Copyright (c) 2007 - 2011, Intel Corporation. All rights reserved.<BR>
5   SPDX-License-Identifier: BSD-2-Clause-Patent
6 
7 **/
8 
9 #include "PiDxe.h"
10 
11 #include <Protocol/GraphicsOutput.h>
12 
13 #include <Library/BaseLib.h>
14 #include <Library/BaseMemoryLib.h>
15 #include <Library/BltLib.h>
16 #include <Library/DebugLib.h>
17 #include <Library/MemoryAllocationLib.h>
18 #include <Library/UefiBootServicesTableLib.h>
19 
20 EFI_GRAPHICS_OUTPUT_PROTOCOL         *mGop = NULL;
21 
22 
23 /**
24   Configure the FrameBufferLib instance
25 
26   @param[in] FrameBuffer      Pointer to the start of the frame buffer
27   @param[in] FrameBufferInfo  Describes the frame buffer characteristics
28 
29   @retval  EFI_INVALID_PARAMETER - Invalid parameter
30   @retval  EFI_UNSUPPORTED - The BltLib does not support this configuration
31   @retval  EFI_SUCCESS - Blt operation success
32 
33 **/
34 EFI_STATUS
35 EFIAPI
BltLibConfigure(IN VOID * FrameBuffer,IN EFI_GRAPHICS_OUTPUT_MODE_INFORMATION * FrameBufferInfo)36 BltLibConfigure (
37   IN  VOID                                 *FrameBuffer,
38   IN  EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *FrameBufferInfo
39   )
40 {
41   EFI_STATUS                      Status;
42   EFI_HANDLE                      *HandleBuffer;
43   UINTN                           HandleCount;
44   UINTN                           Index;
45   EFI_GRAPHICS_OUTPUT_PROTOCOL    *Gop;
46 
47   Status = gBS->LocateHandleBuffer (
48                   ByProtocol,
49                   &gEfiGraphicsOutputProtocolGuid,
50                   NULL,
51                   &HandleCount,
52                   &HandleBuffer
53                   );
54   if (!EFI_ERROR (Status)) {
55     for (Index = 0; Index < HandleCount; Index++) {
56       Status = gBS->HandleProtocol (
57                       HandleBuffer[Index],
58                       &gEfiGraphicsOutputProtocolGuid,
59                       (VOID*) &Gop
60                       );
61       if (!EFI_ERROR (Status) &&
62           (FrameBuffer == (VOID*)(UINTN) Gop->Mode->FrameBufferBase)) {
63         mGop = Gop;
64         FreePool (HandleBuffer);
65         return EFI_SUCCESS;
66       }
67     }
68 
69     FreePool (HandleBuffer);
70   }
71 
72   return EFI_UNSUPPORTED;
73 }
74 
75 
76 /**
77   Performs a UEFI Graphics Output Protocol Blt operation.
78 
79   @param[in,out] BltBuffer     - The data to transfer to screen
80   @param[in]     BltOperation  - The operation to perform
81   @param[in]     SourceX       - The X coordinate of the source for BltOperation
82   @param[in]     SourceY       - The Y coordinate of the source for BltOperation
83   @param[in]     DestinationX  - The X coordinate of the destination for BltOperation
84   @param[in]     DestinationY  - The Y coordinate of the destination for BltOperation
85   @param[in]     Width         - The width of a rectangle in the blt rectangle in pixels
86   @param[in]     Height        - The height of a rectangle in the blt rectangle in pixels
87   @param[in]     Delta         - Not used for EfiBltVideoFill and EfiBltVideoToVideo operation.
88                                  If a Delta of 0 is used, the entire BltBuffer will be operated on.
89                                  If a subrectangle of the BltBuffer is used, then Delta represents
90                                  the number of bytes in a row of the BltBuffer.
91 
92   @retval  EFI_DEVICE_ERROR - A hardware error occured
93   @retval  EFI_INVALID_PARAMETER - Invalid parameter passed in
94   @retval  EFI_SUCCESS - Blt operation success
95 
96 **/
97 EFI_STATUS
InternalGopBltCommon(IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL * BltBuffer,OPTIONAL IN EFI_GRAPHICS_OUTPUT_BLT_OPERATION BltOperation,IN UINTN SourceX,IN UINTN SourceY,IN UINTN DestinationX,IN UINTN DestinationY,IN UINTN Width,IN UINTN Height,IN UINTN Delta)98 InternalGopBltCommon (
99   IN  EFI_GRAPHICS_OUTPUT_BLT_PIXEL         *BltBuffer, OPTIONAL
100   IN  EFI_GRAPHICS_OUTPUT_BLT_OPERATION     BltOperation,
101   IN  UINTN                                 SourceX,
102   IN  UINTN                                 SourceY,
103   IN  UINTN                                 DestinationX,
104   IN  UINTN                                 DestinationY,
105   IN  UINTN                                 Width,
106   IN  UINTN                                 Height,
107   IN  UINTN                                 Delta
108   )
109 {
110   if (mGop == NULL) {
111     return EFI_DEVICE_ERROR;
112   }
113 
114   return mGop->Blt (
115                  mGop,
116                  BltBuffer,
117                  BltOperation,
118                  SourceX,
119                  SourceY,
120                  DestinationX,
121                  DestinationY,
122                  Width,
123                  Height,
124                  Delta
125                  );
126 }
127 
128 
129 /**
130   Performs a UEFI Graphics Output Protocol Blt operation.
131 
132   @param[in,out] BltBuffer     - The data to transfer to screen
133   @param[in]     BltOperation  - The operation to perform
134   @param[in]     SourceX       - The X coordinate of the source for BltOperation
135   @param[in]     SourceY       - The Y coordinate of the source for BltOperation
136   @param[in]     DestinationX  - The X coordinate of the destination for BltOperation
137   @param[in]     DestinationY  - The Y coordinate of the destination for BltOperation
138   @param[in]     Width         - The width of a rectangle in the blt rectangle in pixels
139   @param[in]     Height        - The height of a rectangle in the blt rectangle in pixels
140   @param[in]     Delta         - Not used for EfiBltVideoFill and EfiBltVideoToVideo operation.
141                                  If a Delta of 0 is used, the entire BltBuffer will be operated on.
142                                  If a subrectangle of the BltBuffer is used, then Delta represents
143                                  the number of bytes in a row of the BltBuffer.
144 
145   @retval  EFI_DEVICE_ERROR - A hardware error occured
146   @retval  EFI_INVALID_PARAMETER - Invalid parameter passed in
147   @retval  EFI_SUCCESS - Blt operation success
148 
149 **/
150 EFI_STATUS
151 EFIAPI
BltLibGopBlt(IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL * BltBuffer,OPTIONAL IN EFI_GRAPHICS_OUTPUT_BLT_OPERATION BltOperation,IN UINTN SourceX,IN UINTN SourceY,IN UINTN DestinationX,IN UINTN DestinationY,IN UINTN Width,IN UINTN Height,IN UINTN Delta)152 BltLibGopBlt (
153   IN  EFI_GRAPHICS_OUTPUT_BLT_PIXEL         *BltBuffer, OPTIONAL
154   IN  EFI_GRAPHICS_OUTPUT_BLT_OPERATION     BltOperation,
155   IN  UINTN                                 SourceX,
156   IN  UINTN                                 SourceY,
157   IN  UINTN                                 DestinationX,
158   IN  UINTN                                 DestinationY,
159   IN  UINTN                                 Width,
160   IN  UINTN                                 Height,
161   IN  UINTN                                 Delta
162   )
163 {
164   return InternalGopBltCommon (
165            BltBuffer,
166            BltOperation,
167            SourceX,
168            SourceY,
169            DestinationX,
170            DestinationY,
171            Width,
172            Height,
173            Delta
174            );
175 }
176 
177 
178 /**
179   Performs a UEFI Graphics Output Protocol Blt Video Fill.
180 
181   @param[in]  Color         Color to fill the region with
182   @param[in]  DestinationX  X location to start fill operation
183   @param[in]  DestinationY  Y location to start fill operation
184   @param[in]  Width         Width (in pixels) to fill
185   @param[in]  Height        Height to fill
186 
187   @retval  EFI_DEVICE_ERROR - A hardware error occured
188   @retval  EFI_INVALID_PARAMETER - Invalid parameter passed in
189   @retval  EFI_SUCCESS - The sizes were returned
190 
191 **/
192 EFI_STATUS
193 EFIAPI
BltLibVideoFill(IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL * Color,IN UINTN DestinationX,IN UINTN DestinationY,IN UINTN Width,IN UINTN Height)194 BltLibVideoFill (
195   IN  EFI_GRAPHICS_OUTPUT_BLT_PIXEL         *Color,
196   IN  UINTN                                 DestinationX,
197   IN  UINTN                                 DestinationY,
198   IN  UINTN                                 Width,
199   IN  UINTN                                 Height
200   )
201 {
202   return InternalGopBltCommon (
203            Color,
204            EfiBltVideoFill,
205            0,
206            0,
207            DestinationX,
208            DestinationY,
209            Width,
210            Height,
211            0
212            );
213 }
214 
215 
216 /**
217   Performs a UEFI Graphics Output Protocol Blt Video to Buffer operation.
218 
219   @param[out] BltBuffer     Output buffer for pixel color data
220   @param[in]  SourceX       X location within video
221   @param[in]  SourceY       Y location within video
222   @param[in]  Width         Width (in pixels)
223   @param[in]  Height        Height
224 
225   @retval  EFI_DEVICE_ERROR - A hardware error occured
226   @retval  EFI_INVALID_PARAMETER - Invalid parameter passed in
227   @retval  EFI_SUCCESS - The sizes were returned
228 
229 **/
230 EFI_STATUS
231 EFIAPI
BltLibVideoToBltBuffer(OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL * BltBuffer,IN UINTN SourceX,IN UINTN SourceY,IN UINTN Width,IN UINTN Height)232 BltLibVideoToBltBuffer (
233   OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL         *BltBuffer,
234   IN  UINTN                                 SourceX,
235   IN  UINTN                                 SourceY,
236   IN  UINTN                                 Width,
237   IN  UINTN                                 Height
238   )
239 {
240   return InternalGopBltCommon (
241            BltBuffer,
242            EfiBltVideoToBltBuffer,
243            SourceX,
244            SourceY,
245            0,
246            0,
247            Width,
248            Height,
249            0
250            );
251 }
252 
253 
254 /**
255   Performs a UEFI Graphics Output Protocol Blt Video to Buffer operation
256   with extended parameters.
257 
258   @param[out] BltBuffer     Output buffer for pixel color data
259   @param[in]  SourceX       X location within video
260   @param[in]  SourceY       Y location within video
261   @param[in]  DestinationX  X location within BltBuffer
262   @param[in]  DestinationY  Y location within BltBuffer
263   @param[in]  Width         Width (in pixels)
264   @param[in]  Height        Height
265   @param[in]  Delta         Number of bytes in a row of BltBuffer
266 
267   @retval  EFI_DEVICE_ERROR - A hardware error occured
268   @retval  EFI_INVALID_PARAMETER - Invalid parameter passed in
269   @retval  EFI_SUCCESS - The sizes were returned
270 
271 **/
272 EFI_STATUS
273 EFIAPI
BltLibVideoToBltBufferEx(OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL * BltBuffer,IN UINTN SourceX,IN UINTN SourceY,IN UINTN DestinationX,IN UINTN DestinationY,IN UINTN Width,IN UINTN Height,IN UINTN Delta)274 BltLibVideoToBltBufferEx (
275   OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL         *BltBuffer,
276   IN  UINTN                                 SourceX,
277   IN  UINTN                                 SourceY,
278   IN  UINTN                                 DestinationX,
279   IN  UINTN                                 DestinationY,
280   IN  UINTN                                 Width,
281   IN  UINTN                                 Height,
282   IN  UINTN                                 Delta
283   )
284 {
285   return InternalGopBltCommon (
286            BltBuffer,
287            EfiBltVideoToBltBuffer,
288            SourceX,
289            SourceY,
290            DestinationX,
291            DestinationY,
292            Width,
293            Height,
294            Delta
295            );
296 }
297 
298 
299 /**
300   Performs a UEFI Graphics Output Protocol Blt Buffer to Video operation.
301 
302   @param[in]  BltBuffer     Output buffer for pixel color data
303   @param[in]  DestinationX  X location within video
304   @param[in]  DestinationY  Y location within video
305   @param[in]  Width         Width (in pixels)
306   @param[in]  Height        Height
307 
308   @retval  EFI_DEVICE_ERROR - A hardware error occured
309   @retval  EFI_INVALID_PARAMETER - Invalid parameter passed in
310   @retval  EFI_SUCCESS - The sizes were returned
311 
312 **/
313 EFI_STATUS
314 EFIAPI
BltLibBufferToVideo(IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL * BltBuffer,IN UINTN DestinationX,IN UINTN DestinationY,IN UINTN Width,IN UINTN Height)315 BltLibBufferToVideo (
316   IN  EFI_GRAPHICS_OUTPUT_BLT_PIXEL         *BltBuffer,
317   IN  UINTN                                 DestinationX,
318   IN  UINTN                                 DestinationY,
319   IN  UINTN                                 Width,
320   IN  UINTN                                 Height
321   )
322 {
323   return InternalGopBltCommon (
324            BltBuffer,
325            EfiBltBufferToVideo,
326            0,
327            0,
328            DestinationX,
329            DestinationY,
330            Width,
331            Height,
332            0
333            );
334 }
335 
336 
337 /**
338   Performs a UEFI Graphics Output Protocol Blt Buffer to Video operation
339   with extended parameters.
340 
341   @param[in]  BltBuffer     Output buffer for pixel color data
342   @param[in]  SourceX       X location within BltBuffer
343   @param[in]  SourceY       Y location within BltBuffer
344   @param[in]  DestinationX  X location within video
345   @param[in]  DestinationY  Y location within video
346   @param[in]  Width         Width (in pixels)
347   @param[in]  Height        Height
348   @param[in]  Delta         Number of bytes in a row of BltBuffer
349 
350   @retval  EFI_DEVICE_ERROR - A hardware error occured
351   @retval  EFI_INVALID_PARAMETER - Invalid parameter passed in
352   @retval  EFI_SUCCESS - The sizes were returned
353 
354 **/
355 EFI_STATUS
356 EFIAPI
BltLibBufferToVideoEx(IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL * BltBuffer,IN UINTN SourceX,IN UINTN SourceY,IN UINTN DestinationX,IN UINTN DestinationY,IN UINTN Width,IN UINTN Height,IN UINTN Delta)357 BltLibBufferToVideoEx (
358   IN  EFI_GRAPHICS_OUTPUT_BLT_PIXEL         *BltBuffer,
359   IN  UINTN                                 SourceX,
360   IN  UINTN                                 SourceY,
361   IN  UINTN                                 DestinationX,
362   IN  UINTN                                 DestinationY,
363   IN  UINTN                                 Width,
364   IN  UINTN                                 Height,
365   IN  UINTN                                 Delta
366   )
367 {
368   return InternalGopBltCommon (
369            BltBuffer,
370            EfiBltBufferToVideo,
371            SourceX,
372            SourceY,
373            DestinationX,
374            DestinationY,
375            Width,
376            Height,
377            Delta
378            );
379 }
380 
381 
382 /**
383   Performs a UEFI Graphics Output Protocol Blt Video to Video operation
384 
385   @param[in]  SourceX       X location within video
386   @param[in]  SourceY       Y location within video
387   @param[in]  DestinationX  X location within video
388   @param[in]  DestinationY  Y location within video
389   @param[in]  Width         Width (in pixels)
390   @param[in]  Height        Height
391 
392   @retval  EFI_DEVICE_ERROR - A hardware error occured
393   @retval  EFI_INVALID_PARAMETER - Invalid parameter passed in
394   @retval  EFI_SUCCESS - The sizes were returned
395 
396 **/
397 EFI_STATUS
398 EFIAPI
BltLibVideoToVideo(IN UINTN SourceX,IN UINTN SourceY,IN UINTN DestinationX,IN UINTN DestinationY,IN UINTN Width,IN UINTN Height)399 BltLibVideoToVideo (
400   IN  UINTN                                 SourceX,
401   IN  UINTN                                 SourceY,
402   IN  UINTN                                 DestinationX,
403   IN  UINTN                                 DestinationY,
404   IN  UINTN                                 Width,
405   IN  UINTN                                 Height
406   )
407 {
408   return InternalGopBltCommon (
409            NULL,
410            EfiBltVideoToVideo,
411            SourceX,
412            SourceY,
413            DestinationX,
414            DestinationY,
415            Width,
416            Height,
417            0
418            );
419 }
420 
421 /**
422   Returns the sizes related to the video device
423 
424   @param[out]  Width   Width (in pixels)
425   @param[out]  Height  Height (in pixels)
426 
427   @retval  EFI_INVALID_PARAMETER - Invalid parameter passed in
428   @retval  EFI_SUCCESS - The sizes were returned
429 
430 **/
431 EFI_STATUS
432 EFIAPI
BltLibGetSizes(OUT UINTN * Width,OPTIONAL OUT UINTN * Height OPTIONAL)433 BltLibGetSizes (
434   OUT UINTN                                 *Width,  OPTIONAL
435   OUT UINTN                                 *Height  OPTIONAL
436   )
437 {
438   ASSERT (mGop != NULL);
439 
440   if (Width != NULL) {
441     *Width = mGop->Mode->Info->HorizontalResolution;
442   }
443   if (Height != NULL) {
444     *Height = mGop->Mode->Info->VerticalResolution;
445   }
446 
447   return EFI_SUCCESS;
448 }
449 
450