]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Acpi/AcpiPlatformDxe/AcpiPlatform.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Universal / Acpi / AcpiPlatformDxe / AcpiPlatform.c
CommitLineData
5f55c700 1/** @file\r
2 Sample ACPI Platform Driver\r
3\r
d9476a7e 4 Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>\r
9d510e61 5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
5f55c700 6\r
d1102dba 7**/\r
5f55c700 8\r
9#include <PiDxe.h>\r
10\r
11#include <Protocol/AcpiTable.h>\r
12#include <Protocol/FirmwareVolume2.h>\r
13\r
14#include <Library/BaseLib.h>\r
15#include <Library/UefiBootServicesTableLib.h>\r
16#include <Library/DebugLib.h>\r
17#include <Library/PcdLib.h>\r
18\r
19#include <IndustryStandard/Acpi.h>\r
20\r
21/**\r
22 Locate the first instance of a protocol. If the protocol requested is an\r
23 FV protocol, then it will return the first FV that contains the ACPI table\r
24 storage file.\r
25\r
5f55c700 26 @param Instance Return pointer to the first instance of the protocol\r
27\r
28 @return EFI_SUCCESS The function completed successfully.\r
29 @return EFI_NOT_FOUND The protocol could not be located.\r
30 @return EFI_OUT_OF_RESOURCES There are not enough resources to find the protocol.\r
31\r
32**/\r
33EFI_STATUS\r
34LocateFvInstanceWithTables (\r
35 OUT EFI_FIRMWARE_VOLUME2_PROTOCOL **Instance\r
36 )\r
37{\r
38 EFI_STATUS Status;\r
39 EFI_HANDLE *HandleBuffer;\r
40 UINTN NumberOfHandles;\r
41 EFI_FV_FILETYPE FileType;\r
42 UINT32 FvStatus;\r
43 EFI_FV_FILE_ATTRIBUTES Attributes;\r
44 UINTN Size;\r
45 UINTN Index;\r
46 EFI_FIRMWARE_VOLUME2_PROTOCOL *FvInstance;\r
47\r
48 FvStatus = 0;\r
49\r
50 //\r
51 // Locate protocol.\r
52 //\r
53 Status = gBS->LocateHandleBuffer (\r
54 ByProtocol,\r
55 &gEfiFirmwareVolume2ProtocolGuid,\r
56 NULL,\r
57 &NumberOfHandles,\r
58 &HandleBuffer\r
59 );\r
60 if (EFI_ERROR (Status)) {\r
61 //\r
62 // Defined errors at this time are not found and out of resources.\r
63 //\r
64 return Status;\r
65 }\r
66\r
67\r
68\r
69 //\r
70 // Looking for FV with ACPI storage file\r
71 //\r
72\r
73 for (Index = 0; Index < NumberOfHandles; Index++) {\r
74 //\r
75 // Get the protocol on this handle\r
76 // This should not fail because of LocateHandleBuffer\r
77 //\r
78 Status = gBS->HandleProtocol (\r
79 HandleBuffer[Index],\r
80 &gEfiFirmwareVolume2ProtocolGuid,\r
81 (VOID**) &FvInstance\r
82 );\r
83 ASSERT_EFI_ERROR (Status);\r
84\r
85 //\r
86 // See if it has the ACPI storage file\r
87 //\r
88 Status = FvInstance->ReadFile (\r
89 FvInstance,\r
188e4e84 90 (EFI_GUID*)PcdGetPtr (PcdAcpiTableStorageFile),\r
5f55c700 91 NULL,\r
92 &Size,\r
93 &FileType,\r
94 &Attributes,\r
95 &FvStatus\r
96 );\r
97\r
98 //\r
99 // If we found it, then we are done\r
100 //\r
101 if (Status == EFI_SUCCESS) {\r
102 *Instance = FvInstance;\r
103 break;\r
104 }\r
105 }\r
106\r
107 //\r
108 // Our exit status is determined by the success of the previous operations\r
109 // If the protocol was found, Instance already points to it.\r
110 //\r
111\r
112 //\r
113 // Free any allocated buffers\r
114 //\r
115 gBS->FreePool (HandleBuffer);\r
116\r
117 return Status;\r
118}\r
119\r
120\r
121/**\r
122 This function calculates and updates an UINT8 checksum.\r
123\r
124 @param Buffer Pointer to buffer to checksum\r
125 @param Size Number of bytes to checksum\r
126\r
127**/\r
128VOID\r
129AcpiPlatformChecksum (\r
130 IN UINT8 *Buffer,\r
131 IN UINTN Size\r
132 )\r
133{\r
134 UINTN ChecksumOffset;\r
135\r
136 ChecksumOffset = OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER, Checksum);\r
137\r
138 //\r
139 // Set checksum to 0 first\r
140 //\r
141 Buffer[ChecksumOffset] = 0;\r
142\r
143 //\r
144 // Update checksum value\r
145 //\r
146 Buffer[ChecksumOffset] = CalculateCheckSum8(Buffer, Size);\r
147}\r
148\r
149\r
150/**\r
151 Entrypoint of Acpi Platform driver.\r
152\r
153 @param ImageHandle\r
154 @param SystemTable\r
155\r
156 @return EFI_SUCCESS\r
157 @return EFI_LOAD_ERROR\r
158 @return EFI_OUT_OF_RESOURCES\r
159\r
160**/\r
161EFI_STATUS\r
6d3ea23f 162EFIAPI\r
5f55c700 163AcpiPlatformEntryPoint (\r
164 IN EFI_HANDLE ImageHandle,\r
165 IN EFI_SYSTEM_TABLE *SystemTable\r
166 )\r
167{\r
168 EFI_STATUS Status;\r
169 EFI_ACPI_TABLE_PROTOCOL *AcpiTable;\r
170 EFI_FIRMWARE_VOLUME2_PROTOCOL *FwVol;\r
171 INTN Instance;\r
172 EFI_ACPI_COMMON_HEADER *CurrentTable;\r
173 UINTN TableHandle;\r
174 UINT32 FvStatus;\r
175 UINTN TableSize;\r
176 UINTN Size;\r
177\r
178 Instance = 0;\r
179 CurrentTable = NULL;\r
180 TableHandle = 0;\r
181\r
182 //\r
183 // Find the AcpiTable protocol\r
184 //\r
185 Status = gBS->LocateProtocol (&gEfiAcpiTableProtocolGuid, NULL, (VOID**)&AcpiTable);\r
186 if (EFI_ERROR (Status)) {\r
187 return EFI_ABORTED;\r
188 }\r
189\r
190 //\r
191 // Locate the firmware volume protocol\r
192 //\r
193 Status = LocateFvInstanceWithTables (&FwVol);\r
194 if (EFI_ERROR (Status)) {\r
195 return EFI_ABORTED;\r
196 }\r
197 //\r
198 // Read tables from the storage file.\r
199 //\r
200 while (Status == EFI_SUCCESS) {\r
201\r
202 Status = FwVol->ReadSection (\r
203 FwVol,\r
188e4e84 204 (EFI_GUID*)PcdGetPtr (PcdAcpiTableStorageFile),\r
5f55c700 205 EFI_SECTION_RAW,\r
206 Instance,\r
207 (VOID**) &CurrentTable,\r
208 &Size,\r
209 &FvStatus\r
210 );\r
211 if (!EFI_ERROR(Status)) {\r
212 //\r
213 // Add the table\r
214 //\r
215 TableHandle = 0;\r
216\r
217 TableSize = ((EFI_ACPI_DESCRIPTION_HEADER *) CurrentTable)->Length;\r
218 ASSERT (Size >= TableSize);\r
219\r
220 //\r
221 // Checksum ACPI table\r
222 //\r
223 AcpiPlatformChecksum ((UINT8*)CurrentTable, TableSize);\r
224\r
225 //\r
226 // Install ACPI table\r
227 //\r
228 Status = AcpiTable->InstallAcpiTable (\r
229 AcpiTable,\r
230 CurrentTable,\r
231 TableSize,\r
232 &TableHandle\r
233 );\r
993b8cdd
SZ
234\r
235 //\r
236 // Free memory allocated by ReadSection\r
237 //\r
238 gBS->FreePool (CurrentTable);\r
239\r
5f55c700 240 if (EFI_ERROR(Status)) {\r
241 return EFI_ABORTED;\r
242 }\r
243\r
244 //\r
245 // Increment the instance\r
246 //\r
247 Instance++;\r
248 CurrentTable = NULL;\r
249 }\r
250 }\r
251\r
d9476a7e
MH
252 //\r
253 // The driver does not require to be kept loaded.\r
254 //\r
255 return EFI_REQUEST_UNLOAD_IMAGE;\r
5f55c700 256}\r
257\r