forked from microsoft/mu_plus
/
AcpiRgrt.c
186 lines (150 loc) · 6.03 KB
/
AcpiRgrt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/** @file AcpiRgrt.c
*
*This driver provides an entry in the ACPI table that provides a PNG graphic
for regulatory purposes.
Copyright (C) Microsoft Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <PiDxe.h>
#include <IndustryStandard/Acpi.h>
#include <Library/SafeIntLib.h>
#include <AcpiRgrt.h>
#include <Protocol/AcpiTable.h>
#include <Library/DxeServicesLib.h>
#include <Library/DebugLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/UefiDriverEntryPoint.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/BaseLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiLib.h>
UINTN mRgImageSize;
UINT8 *mRgImageData;
/**
Install RGRT ACPI Table when ACPI Table Protocol is available.
The Regulatory GraphicResource Table (RGRT) is an optional ACPI
table that enables boot firmware to provide the operating
system with color and contrast used to paint the screen
background during boot.
@param[in] Event Event whose notification function is being invoked
@param[in] Context Pointer to the notification function's context
**/
VOID
EFIAPI
InstallAcpiTable (
IN EFI_EVENT Event,
IN VOID *Context
)
{
UINTN TableKey;
EFI_STATUS Status;
EFI_ACPI_TABLE_PROTOCOL *AcpiTable;
UINT8 Checksum;
MSFT_RGRT_ACPI_TABLE *RgrtAcpiTable;
UINT32 RgrtTableSize;
UINT64 OemTableId;
RgrtAcpiTable = NULL;
Status = gBS->LocateProtocol (&gEfiAcpiTableProtocolGuid, NULL, (VOID **)&AcpiTable);
if (EFI_ERROR (Status)) {
// we are expected to be called at least once before ACPI is installed
return;
}
if ((mRgImageSize == 0) || (mRgImageData == NULL)) {
DEBUG ((DEBUG_ERROR, "ACPI RGRT graphic not found\n"));
goto cleanup;
}
// Figure out the size we need, which is struct + image
Status = SafeUint32Add (sizeof (MSFT_RGRT_ACPI_TABLE), (UINT32)mRgImageSize, &RgrtTableSize);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "ACPI RGRT table- image is too large\n"));
goto cleanup;
}
// Now that we know the size, allocate the struct
RgrtAcpiTable = (MSFT_RGRT_ACPI_TABLE *)AllocateZeroPool (RgrtTableSize);
// Check to make sure we actually have the memory
if (RgrtAcpiTable == NULL) {
DEBUG ((DEBUG_ERROR, "ACPI RGRT table failed to allocate %x of memory for table\n", RgrtTableSize));
goto cleanup;
}
// Add the signature, length, and revision
RgrtAcpiTable->Header.Signature = MSFT_ACPI_REGULATORY_GRAPHIC_RESOURCE_TABLE_SIGNATURE;
RgrtAcpiTable->Header.Length = RgrtTableSize;
RgrtAcpiTable->Header.Revision = MSFT_ACPI_REGULATORY_GRAPHIC_RESOURCE_TABLE_IMAGE_REVISION;
// Copy the default parameters into the table headers
CopyMem (&RgrtAcpiTable->Header.OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (RgrtAcpiTable->Header.OemId));
OemTableId = PcdGet64 (PcdAcpiDefaultOemTableId);
CopyMem (&RgrtAcpiTable->Header.OemTableId, &OemTableId, sizeof (OemTableId));
RgrtAcpiTable->Header.OemRevision = PcdGet32 (PcdAcpiDefaultOemRevision);
RgrtAcpiTable->Header.CreatorId = PcdGet32 (PcdAcpiDefaultCreatorId);
RgrtAcpiTable->Header.CreatorRevision = PcdGet32 (PcdAcpiDefaultCreatorRevision);
// Set the version
RgrtAcpiTable->Version = MSFT_ACPI_REGULATORY_GRAPHIC_RESOURCE_TABLE_IMAGE_VERSION;
// Set the image type
RgrtAcpiTable->ImageType = MSFT_ACPI_REGULATORY_GRAPHIC_RESOURCE_TABLE_IMAGE_TYPE_PNG;
// Copy the image into the table
CopyMem (&RgrtAcpiTable->Image, mRgImageData, mRgImageSize);
//
// The ACPI table must be checksumed before calling the InstallAcpiTable()
// service of the ACPI table protocol to install it.
//
Checksum = CalculateCheckSum8 ((UINT8 *)RgrtAcpiTable, RgrtTableSize);
RgrtAcpiTable->Header.Checksum = Checksum;
// Install the table
Status = AcpiTable->InstallAcpiTable (
AcpiTable,
RgrtAcpiTable,
RgrtTableSize,
&TableKey
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "ACPI RGRT table failed to install: %r\n", Status));
}
cleanup:
// If we got here by an event, make sure we close it
if (Event != NULL) {
gBS->CloseEvent (Event);
}
// Make sure to free the memory we allocated
if (RgrtAcpiTable != NULL) {
FreePool (RgrtAcpiTable);
}
if (mRgImageData != NULL) {
FreePool (mRgImageData);
mRgImageData = NULL;
mRgImageSize = 0;
}
}
/**
The driver's entry point.
It produces ACPI Regulatory GraphicResource Table (RGRT)
@param[in] ImageHandle The firmware allocated handle for the EFI image.
@param[in] SystemTable A pointer to the EFI System Table.
@retval EFI_SUCCESS The entry point is executed successfully.
@retval other Some error occurs when executing this entry point.
**/
EFI_STATUS
EFIAPI
DriverEntry (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
VOID *Registration;
EFI_STATUS Status;
EFI_GUID *ImageLocationGuid;
ImageLocationGuid = PcdGetPtr (PcdRegulatoryGraphicFileGuid); // Get the guid of the regulatory graphic
if (ImageLocationGuid == NULL) {
DEBUG ((DEBUG_ERROR, "ACPI RGRT failed to find Graphic Image GUID\n"));
return EFI_ABORTED;
}
// Get the image out of the FV
Status = GetSectionFromAnyFv (ImageLocationGuid, EFI_SECTION_RAW, 0, (VOID **)&mRgImageData, &mRgImageSize);
// if we didn't find it
if (EFI_ERROR (Status) || (mRgImageSize == 0)) {
DEBUG ((DEBUG_ERROR, "ACPI RGRT table failed to find Graphic Image location\n"));
return EFI_ABORTED;
}
// register for a call back (we will be called immediately as well)
EfiCreateProtocolNotifyEvent (&gEfiAcpiTableProtocolGuid, TPL_CALLBACK, InstallAcpiTable, NULL, &Registration);
return EFI_SUCCESS;
}