]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Acpi/BootGraphicsResourceTableDxe/BootGraphicsResourceTableDxe.c
MdeModulePkg/BootGraphicsResourceDxe: Add Boot Logo 2 Protocol
[mirror_edk2.git] / MdeModulePkg / Universal / Acpi / BootGraphicsResourceTableDxe / BootGraphicsResourceTableDxe.c
1 /** @file
2 This module install ACPI Boot Graphics Resource Table (BGRT).
3
4 Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2016, Microsoft Corporation<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 **/
14
15 #include <Uefi.h>
16
17 #include <IndustryStandard/Acpi.h>
18
19 #include <Protocol/AcpiTable.h>
20 #include <Protocol/GraphicsOutput.h>
21 #include <Protocol/BootLogo.h>
22 #include <Protocol/BootLogo2.h>
23
24 #include <Guid/EventGroup.h>
25
26 #include <Library/BaseLib.h>
27 #include <Library/BaseMemoryLib.h>
28 #include <Library/MemoryAllocationLib.h>
29 #include <Library/UefiBootServicesTableLib.h>
30 #include <Library/DebugLib.h>
31 #include <Library/PcdLib.h>
32 #include <Library/SafeIntLib.h>
33 #include <Library/BmpSupportLib.h>
34
35 /**
36 Update information of logo image drawn on screen.
37
38 @param[in] This The pointer to the Boot Logo protocol 2 instance.
39 @param[in] BltBuffer The BLT buffer for logo drawn on screen. If BltBuffer
40 is set to NULL, it indicates that logo image is no
41 longer on the screen.
42 @param[in] DestinationX X coordinate of destination for the BltBuffer.
43 @param[in] DestinationY Y coordinate of destination for the BltBuffer.
44 @param[in] Width Width of rectangle in BltBuffer in pixels.
45 @param[in] Height Hight of rectangle in BltBuffer in pixels.
46
47 @retval EFI_SUCCESS The boot logo information was updated.
48 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
49 @retval EFI_OUT_OF_RESOURCES The logo information was not updated due to
50 insufficient memory resources.
51 **/
52 EFI_STATUS
53 EFIAPI
54 SetBootLogo (
55 IN EFI_BOOT_LOGO_PROTOCOL *This,
56 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer OPTIONAL,
57 IN UINTN DestinationX,
58 IN UINTN DestinationY,
59 IN UINTN Width,
60 IN UINTN Height
61 );
62
63 /**
64 Update information of logo image drawn on screen.
65
66 @param[in] This The pointer to the Boot Logo protocol 2 instance.
67 @param[in] BltBuffer The BLT buffer for logo drawn on screen. If BltBuffer
68 is set to NULL, it indicates that logo image is no
69 longer on the screen.
70 @param[in] DestinationX X coordinate of destination for the BltBuffer.
71 @param[in] DestinationY Y coordinate of destination for the BltBuffer.
72 @param[in] Width Width of rectangle in BltBuffer in pixels.
73 @param[in] Height Hight of rectangle in BltBuffer in pixels.
74
75 @retval EFI_SUCCESS The boot logo information was updated.
76 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
77 @retval EFI_OUT_OF_RESOURCES The logo information was not updated due to
78 insufficient memory resources.
79 **/
80 EFI_STATUS
81 EFIAPI
82 SetBootLogo2 (
83 IN EDKII_BOOT_LOGO2_PROTOCOL *This,
84 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer OPTIONAL,
85 IN UINTN DestinationX,
86 IN UINTN DestinationY,
87 IN UINTN Width,
88 IN UINTN Height
89 );
90
91 /**
92 Get the location of the boot logo on the screen.
93
94 @param[in] This The pointer to the Boot Logo Protocol 2 instance
95 @param[out] BltBuffer Returns pointer to the GOP BLT buffer that was
96 previously registered with SetBootLogo2(). The
97 buffer returned must not be modified or freed.
98 @param[out] DestinationX Returns the X start position of the GOP BLT buffer
99 that was previously registered with SetBootLogo2().
100 @param[out] DestinationY Returns the Y start position of the GOP BLT buffer
101 that was previously registered with SetBootLogo2().
102 @param[out] Width Returns the width of the GOP BLT buffer
103 that was previously registered with SetBootLogo2().
104 @param[out] Height Returns the height of the GOP BLT buffer
105 that was previously registered with SetBootLogo2().
106
107 @retval EFI_SUCCESS The location of the boot logo was returned.
108 @retval EFI_NOT_READY The boot logo has not been set.
109 @retval EFI_INVALID_PARAMETER BltBuffer is NULL.
110 @retval EFI_INVALID_PARAMETER DestinationX is NULL.
111 @retval EFI_INVALID_PARAMETER DestinationY is NULL.
112 @retval EFI_INVALID_PARAMETER Width is NULL.
113 @retval EFI_INVALID_PARAMETER Height is NULL.
114 **/
115 EFI_STATUS
116 EFIAPI
117 GetBootLogo2 (
118 IN EDKII_BOOT_LOGO2_PROTOCOL *This,
119 OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL **BltBuffer,
120 OUT UINTN *DestinationX,
121 OUT UINTN *DestinationY,
122 OUT UINTN *Width,
123 OUT UINTN *Height
124 );
125
126 //
127 // Boot Logo Protocol Handle
128 //
129 EFI_HANDLE mBootLogoHandle = NULL;
130
131 //
132 // Boot Logo Protocol Instance
133 //
134 EFI_BOOT_LOGO_PROTOCOL mBootLogoProtocolTemplate = {
135 SetBootLogo
136 };
137
138 ///
139 /// Boot Logo 2 Protocol instance
140 ///
141 EDKII_BOOT_LOGO2_PROTOCOL mBootLogo2ProtocolTemplate = {
142 SetBootLogo2,
143 GetBootLogo2
144 };
145
146 EFI_EVENT mBootGraphicsReadyToBootEvent;
147 UINTN mBootGraphicsResourceTableKey = 0;
148 BOOLEAN mIsLogoValid = FALSE;
149 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *mLogoBltBuffer = NULL;
150 UINTN mLogoDestX = 0;
151 UINTN mLogoDestY = 0;
152 UINTN mLogoWidth = 0;
153 UINTN mLogoHeight = 0;
154 BOOLEAN mAcpiBgrtInstalled = FALSE;
155 BOOLEAN mAcpiBgrtStatusChanged = FALSE;
156 BOOLEAN mAcpiBgrtBufferChanged = FALSE;
157
158 //
159 // ACPI Boot Graphics Resource Table template
160 //
161 EFI_ACPI_5_0_BOOT_GRAPHICS_RESOURCE_TABLE mBootGraphicsResourceTableTemplate = {
162 {
163 EFI_ACPI_5_0_BOOT_GRAPHICS_RESOURCE_TABLE_SIGNATURE,
164 sizeof (EFI_ACPI_5_0_BOOT_GRAPHICS_RESOURCE_TABLE),
165 EFI_ACPI_5_0_BOOT_GRAPHICS_RESOURCE_TABLE_REVISION, // Revision
166 0x00, // Checksum will be updated at runtime
167 //
168 // It is expected that these values will be updated at EntryPoint.
169 //
170 {0x00}, // OEM ID is a 6 bytes long field
171 0x00, // OEM Table ID(8 bytes long)
172 0x00, // OEM Revision
173 0x00, // Creator ID
174 0x00, // Creator Revision
175 },
176 EFI_ACPI_5_0_BGRT_VERSION, // Version
177 EFI_ACPI_5_0_BGRT_STATUS_VALID, // Status
178 EFI_ACPI_5_0_BGRT_IMAGE_TYPE_BMP, // Image Type
179 0, // Image Address
180 0, // Image Offset X
181 0 // Image Offset Y
182 };
183
184 /**
185 Update information of logo image drawn on screen.
186
187 @param This The pointer to the Boot Logo protocol instance.
188 @param BltBuffer The BLT buffer for logo drawn on screen. If BltBuffer
189 is set to NULL, it indicates that logo image is no
190 longer on the screen.
191 @param DestinationX X coordinate of destination for the BltBuffer.
192 @param DestinationY Y coordinate of destination for the BltBuffer.
193 @param Width Width of rectangle in BltBuffer in pixels.
194 @param Height Hight of rectangle in BltBuffer in pixels.
195
196 @retval EFI_SUCCESS The boot logo information was updated.
197 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
198 @retval EFI_OUT_OF_RESOURCES The logo information was not updated due to
199 insufficient memory resources.
200
201 **/
202 EFI_STATUS
203 EFIAPI
204 SetBootLogo (
205 IN EFI_BOOT_LOGO_PROTOCOL *This,
206 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer OPTIONAL,
207 IN UINTN DestinationX,
208 IN UINTN DestinationY,
209 IN UINTN Width,
210 IN UINTN Height
211 )
212 {
213 //
214 // Call same service in Boot Logo 2 Protocol
215 //
216 return SetBootLogo2 (
217 &mBootLogo2ProtocolTemplate,
218 BltBuffer,
219 DestinationX,
220 DestinationY,
221 Width,
222 Height
223 );
224 }
225
226 /**
227 Update information of logo image drawn on screen.
228
229 @param[in] This The pointer to the Boot Logo protocol 2 instance.
230 @param[in] BltBuffer The BLT buffer for logo drawn on screen. If BltBuffer
231 is set to NULL, it indicates that logo image is no
232 longer on the screen.
233 @param[in] DestinationX X coordinate of destination for the BltBuffer.
234 @param[in] DestinationY Y coordinate of destination for the BltBuffer.
235 @param[in] Width Width of rectangle in BltBuffer in pixels.
236 @param[in] Height Hight of rectangle in BltBuffer in pixels.
237
238 @retval EFI_SUCCESS The boot logo information was updated.
239 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
240 @retval EFI_OUT_OF_RESOURCES The logo information was not updated due to
241 insufficient memory resources.
242 **/
243 EFI_STATUS
244 EFIAPI
245 SetBootLogo2 (
246 IN EDKII_BOOT_LOGO2_PROTOCOL *This,
247 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer OPTIONAL,
248 IN UINTN DestinationX,
249 IN UINTN DestinationY,
250 IN UINTN Width,
251 IN UINTN Height
252 )
253 {
254 EFI_STATUS Status;
255 UINTN BufferSize;
256 UINT32 Result32;
257
258 if (BltBuffer == NULL) {
259 mIsLogoValid = FALSE;
260 mAcpiBgrtStatusChanged = TRUE;
261 return EFI_SUCCESS;
262 }
263
264 //
265 // Width and height are not allowed to be zero.
266 //
267 if (Width == 0 || Height == 0) {
268 return EFI_INVALID_PARAMETER;
269 }
270
271 //
272 // Verify destination, width, and height do not overflow 32-bit values.
273 // The Boot Graphics Resource Table only has 32-bit fields for these values.
274 //
275 Status = SafeUintnToUint32 (DestinationX, &Result32);
276 if (EFI_ERROR (Status)) {
277 return EFI_INVALID_PARAMETER;
278 }
279 Status = SafeUintnToUint32 (DestinationY, &Result32);
280 if (EFI_ERROR (Status)) {
281 return EFI_INVALID_PARAMETER;
282 }
283 Status = SafeUintnToUint32 (Width, &Result32);
284 if (EFI_ERROR (Status)) {
285 return EFI_INVALID_PARAMETER;
286 }
287 Status = SafeUintnToUint32 (Height, &Result32);
288 if (EFI_ERROR (Status)) {
289 return EFI_INVALID_PARAMETER;
290 }
291
292 //
293 // Ensure the Height * Width * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL) does
294 // not overflow UINTN
295 //
296 Status = SafeUintnMult (
297 Width,
298 Height,
299 &BufferSize
300 );
301 if (EFI_ERROR (Status)) {
302 return EFI_UNSUPPORTED;
303 }
304 Status = SafeUintnMult (
305 BufferSize,
306 sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL),
307 &BufferSize
308 );
309 if (EFI_ERROR (Status)) {
310 return EFI_UNSUPPORTED;
311 }
312
313 //
314 // Update state
315 //
316 mAcpiBgrtBufferChanged = TRUE;
317
318 //
319 // Free old logo buffer
320 //
321 if (mLogoBltBuffer != NULL) {
322 FreePool (mLogoBltBuffer);
323 mLogoBltBuffer = NULL;
324 }
325
326 //
327 // Allocate new logo buffer
328 //
329 mLogoBltBuffer = AllocateCopyPool (BufferSize, BltBuffer);
330 if (mLogoBltBuffer == NULL) {
331 return EFI_OUT_OF_RESOURCES;
332 }
333
334 mLogoDestX = DestinationX;
335 mLogoDestY = DestinationY;
336 mLogoWidth = Width;
337 mLogoHeight = Height;
338 mIsLogoValid = TRUE;
339
340 return EFI_SUCCESS;
341 }
342
343 /**
344 Get the location of the boot logo on the screen.
345
346 @param[in] This The pointer to the Boot Logo Protocol 2 instance
347 @param[out] BltBuffer Returns pointer to the GOP BLT buffer that was
348 previously registered with SetBootLogo2(). The
349 buffer returned must not be modified or freed.
350 @param[out] DestinationX Returns the X start position of the GOP BLT buffer
351 that was previously registered with SetBootLogo2().
352 @param[out] DestinationY Returns the Y start position of the GOP BLT buffer
353 that was previously registered with SetBootLogo2().
354 @param[out] Width Returns the width of the GOP BLT buffer
355 that was previously registered with SetBootLogo2().
356 @param[out] Height Returns the height of the GOP BLT buffer
357 that was previously registered with SetBootLogo2().
358
359 @retval EFI_SUCCESS The location of the boot logo was returned.
360 @retval EFI_NOT_READY The boot logo has not been set.
361 @retval EFI_INVALID_PARAMETER BltBuffer is NULL.
362 @retval EFI_INVALID_PARAMETER DestinationX is NULL.
363 @retval EFI_INVALID_PARAMETER DestinationY is NULL.
364 @retval EFI_INVALID_PARAMETER Width is NULL.
365 @retval EFI_INVALID_PARAMETER Height is NULL.
366 **/
367 EFI_STATUS
368 EFIAPI
369 GetBootLogo2 (
370 IN EDKII_BOOT_LOGO2_PROTOCOL *This,
371 OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL **BltBuffer,
372 OUT UINTN *DestinationX,
373 OUT UINTN *DestinationY,
374 OUT UINTN *Width,
375 OUT UINTN *Height
376 )
377 {
378 //
379 // If the boot logo has not been set with SetBootLogo() or SetBootLogo() was
380 // called with a NULL BltBuffer then the boot logo is not valid and
381 // EFI_NOT_READY is returned.
382 //
383 if (mLogoBltBuffer == NULL) {
384 DEBUG ((DEBUG_ERROR, "Request to get boot logo location before boot logo has been set.\n"));
385 return EFI_NOT_READY;
386 }
387
388 //
389 // Make sure none of the boot logo location parameters are NULL.
390 //
391 if (BltBuffer == NULL || DestinationX == NULL || DestinationY == NULL ||
392 Width == NULL || Height == NULL) {
393 return EFI_INVALID_PARAMETER;
394 }
395
396 //
397 // Boot logo is valid. Return values from module globals.
398 //
399 *BltBuffer = mLogoBltBuffer;
400 *DestinationX = mLogoDestX;
401 *DestinationY = mLogoDestY;
402 *Width = mLogoWidth;
403 *Height = mLogoHeight;
404
405 return EFI_SUCCESS;
406 }
407
408 /**
409 Notify function for event group EFI_EVENT_GROUP_READY_TO_BOOT. This is used to
410 install the Boot Graphics Resource Table.
411
412 @param[in] Event The Event that is being processed.
413 @param[in] Context The Event Context.
414
415 **/
416 VOID
417 EFIAPI
418 BgrtReadyToBootEventNotify (
419 IN EFI_EVENT Event,
420 IN VOID *Context
421 )
422 {
423 EFI_STATUS Status;
424 EFI_ACPI_TABLE_PROTOCOL *AcpiTableProtocol;
425 VOID *ImageBuffer;
426 UINT32 BmpSize;
427
428 //
429 // Get ACPI Table protocol.
430 //
431 Status = gBS->LocateProtocol (
432 &gEfiAcpiTableProtocolGuid,
433 NULL,
434 (VOID **) &AcpiTableProtocol
435 );
436 if (EFI_ERROR (Status)) {
437 return;
438 }
439
440 //
441 // Check whether Boot Graphics Resource Table is already installed.
442 //
443 if (mAcpiBgrtInstalled) {
444 if (!mAcpiBgrtStatusChanged && !mAcpiBgrtBufferChanged) {
445 //
446 // Nothing has changed
447 //
448 return;
449 } else {
450 //
451 // If BGRT data change happens, then uninstall orignal AcpiTable first
452 //
453 Status = AcpiTableProtocol->UninstallAcpiTable (
454 AcpiTableProtocol,
455 mBootGraphicsResourceTableKey
456 );
457 if (EFI_ERROR (Status)) {
458 return;
459 }
460 }
461 } else {
462 //
463 // Check whether Logo exists
464 //
465 if (mLogoBltBuffer == NULL) {
466 return;
467 }
468 }
469
470 if (mAcpiBgrtBufferChanged) {
471 //
472 // Free the old BMP image buffer
473 //
474 ImageBuffer = (UINT8 *)(UINTN)mBootGraphicsResourceTableTemplate.ImageAddress;
475 if (ImageBuffer != NULL) {
476 FreePool (ImageBuffer);
477 }
478
479 //
480 // Convert GOP Blt buffer to BMP image. Pass in ImageBuffer set to NULL
481 // so the BMP image is allocated by TranslateGopBltToBmp().
482 //
483 ImageBuffer = NULL;
484 Status = TranslateGopBltToBmp (
485 mLogoBltBuffer,
486 (UINT32)mLogoHeight,
487 (UINT32)mLogoWidth,
488 &ImageBuffer,
489 &BmpSize
490 );
491 if (EFI_ERROR (Status)) {
492 return;
493 }
494
495 //
496 // Free the logo buffer
497 //
498 FreePool (mLogoBltBuffer);
499 mLogoBltBuffer = NULL;
500
501 //
502 // Update BMP image fields of the Boot Graphics Resource Table
503 //
504 mBootGraphicsResourceTableTemplate.ImageAddress = (UINT64)(UINTN)ImageBuffer;
505 mBootGraphicsResourceTableTemplate.ImageOffsetX = (UINT32)mLogoDestX;
506 mBootGraphicsResourceTableTemplate.ImageOffsetY = (UINT32)mLogoDestY;
507 }
508
509 //
510 // Update Status field of Boot Graphics Resource Table
511 //
512 if (mIsLogoValid) {
513 mBootGraphicsResourceTableTemplate.Status = EFI_ACPI_5_0_BGRT_STATUS_VALID;
514 } else {
515 mBootGraphicsResourceTableTemplate.Status = EFI_ACPI_5_0_BGRT_STATUS_INVALID;
516 }
517
518 //
519 // Update Checksum of Boot Graphics Resource Table
520 //
521 mBootGraphicsResourceTableTemplate.Header.Checksum = 0;
522 mBootGraphicsResourceTableTemplate.Header.Checksum =
523 CalculateCheckSum8 (
524 (UINT8 *)&mBootGraphicsResourceTableTemplate,
525 sizeof (EFI_ACPI_5_0_BOOT_GRAPHICS_RESOURCE_TABLE)
526 );
527
528 //
529 // Publish Boot Graphics Resource Table.
530 //
531 Status = AcpiTableProtocol->InstallAcpiTable (
532 AcpiTableProtocol,
533 &mBootGraphicsResourceTableTemplate,
534 sizeof (EFI_ACPI_5_0_BOOT_GRAPHICS_RESOURCE_TABLE),
535 &mBootGraphicsResourceTableKey
536 );
537 if (EFI_ERROR (Status)) {
538 return;
539 }
540
541 mAcpiBgrtInstalled = TRUE;
542 mAcpiBgrtStatusChanged = FALSE;
543 mAcpiBgrtBufferChanged = FALSE;
544 }
545
546 /**
547 The module Entry Point of the Boot Graphics Resource Table DXE driver.
548
549 @param[in] ImageHandle The firmware allocated handle for the EFI image.
550 @param[in] SystemTable A pointer to the EFI System Table.
551
552 @retval EFI_SUCCESS The entry point is executed successfully.
553 @retval Other Some error occurs when executing this entry point.
554
555 **/
556 EFI_STATUS
557 EFIAPI
558 BootGraphicsDxeEntryPoint (
559 IN EFI_HANDLE ImageHandle,
560 IN EFI_SYSTEM_TABLE *SystemTable
561 )
562 {
563 EFI_STATUS Status;
564 EFI_ACPI_DESCRIPTION_HEADER *Header;
565
566 //
567 // Update Header fields of Boot Graphics Resource Table from PCDs
568 //
569 Header = &mBootGraphicsResourceTableTemplate.Header;
570 ZeroMem (Header->OemId, sizeof (Header->OemId));
571 CopyMem (
572 Header->OemId,
573 PcdGetPtr (PcdAcpiDefaultOemId),
574 MIN (PcdGetSize (PcdAcpiDefaultOemId), sizeof (Header->OemId))
575 );
576 WriteUnaligned64 (&Header->OemTableId, PcdGet64 (PcdAcpiDefaultOemTableId));
577 Header->OemRevision = PcdGet32 (PcdAcpiDefaultOemRevision);
578 Header->CreatorId = PcdGet32 (PcdAcpiDefaultCreatorId);
579 Header->CreatorRevision = PcdGet32 (PcdAcpiDefaultCreatorRevision);
580
581 //
582 // Install Boot Logo and Boot Logo 2 Protocols.
583 //
584 Status = gBS->InstallMultipleProtocolInterfaces (
585 &mBootLogoHandle,
586 &gEfiBootLogoProtocolGuid,
587 &mBootLogoProtocolTemplate,
588 &gEdkiiBootLogo2ProtocolGuid,
589 &mBootLogo2ProtocolTemplate,
590 NULL
591 );
592 ASSERT_EFI_ERROR (Status);
593
594 //
595 // Register notify function to install BGRT on ReadyToBoot Event.
596 //
597 Status = gBS->CreateEventEx (
598 EVT_NOTIFY_SIGNAL,
599 TPL_CALLBACK,
600 BgrtReadyToBootEventNotify,
601 NULL,
602 &gEfiEventReadyToBootGuid,
603 &mBootGraphicsReadyToBootEvent
604 );
605 ASSERT_EFI_ERROR (Status);
606
607 return Status;
608 }