]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/AcpiPlatformDxe/AcpiPlatform.c
ShellPkg/for: Fix potential null pointer deference
[mirror_edk2.git] / OvmfPkg / AcpiPlatformDxe / AcpiPlatform.c
CommitLineData
7d2bd150 1/** @file\r
522203de 2 OVMF ACPI Platform Driver\r
7d2bd150 3\r
522203de 4 Copyright (c) 2008 - 2012, Intel Corporation. All rights reserved.<BR>\r
7d2bd150 5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
522203de 13**/\r
7d2bd150 14\r
522203de 15#include "AcpiPlatform.h"\r
7d2bd150 16\r
522203de 17EFI_STATUS\r
18EFIAPI\r
19InstallAcpiTable (\r
20 IN EFI_ACPI_TABLE_PROTOCOL *AcpiProtocol,\r
21 IN VOID *AcpiTableBuffer,\r
22 IN UINTN AcpiTableBufferSize,\r
23 OUT UINTN *TableKey\r
24 )\r
25{\r
26 return AcpiProtocol->InstallAcpiTable (\r
27 AcpiProtocol,\r
28 AcpiTableBuffer,\r
29 AcpiTableBufferSize,\r
30 TableKey\r
31 );\r
32}\r
7d2bd150 33\r
7d2bd150 34\r
35/**\r
36 Locate the first instance of a protocol. If the protocol requested is an\r
37 FV protocol, then it will return the first FV that contains the ACPI table\r
38 storage file.\r
39\r
40 @param Instance Return pointer to the first instance of the protocol\r
41\r
42 @return EFI_SUCCESS The function completed successfully.\r
43 @return EFI_NOT_FOUND The protocol could not be located.\r
44 @return EFI_OUT_OF_RESOURCES There are not enough resources to find the protocol.\r
45\r
46**/\r
47EFI_STATUS\r
48LocateFvInstanceWithTables (\r
49 OUT EFI_FIRMWARE_VOLUME2_PROTOCOL **Instance\r
50 )\r
51{\r
52 EFI_STATUS Status;\r
53 EFI_HANDLE *HandleBuffer;\r
54 UINTN NumberOfHandles;\r
55 EFI_FV_FILETYPE FileType;\r
56 UINT32 FvStatus;\r
57 EFI_FV_FILE_ATTRIBUTES Attributes;\r
58 UINTN Size;\r
59 UINTN Index;\r
60 EFI_FIRMWARE_VOLUME2_PROTOCOL *FvInstance;\r
61\r
62 FvStatus = 0;\r
63\r
64 //\r
65 // Locate protocol.\r
66 //\r
67 Status = gBS->LocateHandleBuffer (\r
68 ByProtocol,\r
69 &gEfiFirmwareVolume2ProtocolGuid,\r
70 NULL,\r
71 &NumberOfHandles,\r
72 &HandleBuffer\r
73 );\r
74 if (EFI_ERROR (Status)) {\r
75 //\r
76 // Defined errors at this time are not found and out of resources.\r
77 //\r
78 return Status;\r
79 }\r
80\r
7d2bd150 81 //\r
82 // Looking for FV with ACPI storage file\r
83 //\r
7d2bd150 84 for (Index = 0; Index < NumberOfHandles; Index++) {\r
85 //\r
86 // Get the protocol on this handle\r
87 // This should not fail because of LocateHandleBuffer\r
88 //\r
89 Status = gBS->HandleProtocol (\r
90 HandleBuffer[Index],\r
91 &gEfiFirmwareVolume2ProtocolGuid,\r
92 (VOID**) &FvInstance\r
93 );\r
94 ASSERT_EFI_ERROR (Status);\r
95\r
96 //\r
97 // See if it has the ACPI storage file\r
98 //\r
99 Status = FvInstance->ReadFile (\r
100 FvInstance,\r
101 (EFI_GUID*)PcdGetPtr (PcdAcpiTableStorageFile),\r
102 NULL,\r
103 &Size,\r
104 &FileType,\r
105 &Attributes,\r
106 &FvStatus\r
107 );\r
108\r
109 //\r
110 // If we found it, then we are done\r
111 //\r
112 if (Status == EFI_SUCCESS) {\r
113 *Instance = FvInstance;\r
114 break;\r
115 }\r
116 }\r
117\r
118 //\r
119 // Our exit status is determined by the success of the previous operations\r
120 // If the protocol was found, Instance already points to it.\r
121 //\r
122\r
123 //\r
124 // Free any allocated buffers\r
125 //\r
126 gBS->FreePool (HandleBuffer);\r
127\r
128 return Status;\r
129}\r
130\r
131\r
7d2bd150 132/**\r
b70c4d07
JJ
133 Find ACPI tables in an FV and install them. \r
134\r
135 This is now a fall-back path. Normally, we will search for tables provided\r
136 by the VMM first.\r
137\r
138 If that fails, we use this function to load the ACPI tables from an FV. The\r
139 sources for the FV based tables is located under OvmfPkg/AcpiTables.\r
7d2bd150 140\r
377e758c 141 @param AcpiTable Protocol instance pointer \r
7d2bd150 142\r
143**/\r
144EFI_STATUS\r
145EFIAPI\r
b70c4d07 146InstallOvmfFvTables (\r
377e758c 147 IN EFI_ACPI_TABLE_PROTOCOL *AcpiTable\r
7d2bd150 148 )\r
149{\r
377e758c 150 EFI_STATUS Status;\r
151 EFI_FIRMWARE_VOLUME2_PROTOCOL *FwVol;\r
152 INTN Instance;\r
153 EFI_ACPI_COMMON_HEADER *CurrentTable;\r
154 UINTN TableHandle;\r
155 UINT32 FvStatus;\r
156 UINTN TableSize;\r
157 UINTN Size;\r
158 EFI_ACPI_TABLE_INSTALL_ACPI_TABLE TableInstallFunction;\r
7d2bd150 159\r
160 Instance = 0;\r
161 CurrentTable = NULL;\r
162 TableHandle = 0;\r
163\r
522203de 164 if (QemuDetected ()) {\r
165 TableInstallFunction = QemuInstallAcpiTable;\r
522203de 166 } else {\r
167 TableInstallFunction = InstallAcpiTable;\r
168 }\r
169\r
7d2bd150 170 //\r
171 // Locate the firmware volume protocol\r
172 //\r
173 Status = LocateFvInstanceWithTables (&FwVol);\r
174 if (EFI_ERROR (Status)) {\r
175 return EFI_ABORTED;\r
176 }\r
177 //\r
178 // Read tables from the storage file.\r
179 //\r
180 while (Status == EFI_SUCCESS) {\r
181\r
182 Status = FwVol->ReadSection (\r
183 FwVol,\r
184 (EFI_GUID*)PcdGetPtr (PcdAcpiTableStorageFile),\r
185 EFI_SECTION_RAW,\r
186 Instance,\r
187 (VOID**) &CurrentTable,\r
188 &Size,\r
189 &FvStatus\r
190 );\r
377e758c 191 if (!EFI_ERROR (Status)) {\r
7d2bd150 192 //\r
193 // Add the table\r
194 //\r
195 TableHandle = 0;\r
196\r
197 TableSize = ((EFI_ACPI_DESCRIPTION_HEADER *) CurrentTable)->Length;\r
198 ASSERT (Size >= TableSize);\r
199\r
7d2bd150 200 //\r
201 // Install ACPI table\r
202 //\r
522203de 203 Status = TableInstallFunction (\r
204 AcpiTable,\r
205 CurrentTable,\r
206 TableSize,\r
207 &TableHandle\r
208 );\r
7d2bd150 209\r
210 //\r
211 // Free memory allocated by ReadSection\r
212 //\r
213 gBS->FreePool (CurrentTable);\r
214\r
522203de 215 if (EFI_ERROR (Status)) {\r
7d2bd150 216 return EFI_ABORTED;\r
217 }\r
218\r
219 //\r
220 // Increment the instance\r
221 //\r
222 Instance++;\r
223 CurrentTable = NULL;\r
224 }\r
225 }\r
226\r
227 return EFI_SUCCESS;\r
228}\r
229\r
377e758c 230/**\r
04951644 231 Effective entrypoint of Acpi Platform driver.\r
377e758c 232\r
233 @param ImageHandle\r
234 @param SystemTable\r
235\r
236 @return EFI_SUCCESS\r
237 @return EFI_LOAD_ERROR\r
238 @return EFI_OUT_OF_RESOURCES\r
239\r
240**/\r
241EFI_STATUS\r
242EFIAPI\r
04951644
LE
243InstallAcpiTables (\r
244 IN EFI_ACPI_TABLE_PROTOCOL *AcpiTable\r
377e758c 245 )\r
246{\r
247 EFI_STATUS Status;\r
377e758c 248\r
249 if (XenDetected ()) {\r
250 Status = InstallXenTables (AcpiTable);\r
377e758c 251 } else {\r
f186536b 252 Status = InstallQemuFwCfgTables (AcpiTable);\r
377e758c 253 }\r
96bbdbc8 254\r
377e758c 255 if (EFI_ERROR (Status)) {\r
b70c4d07 256 Status = InstallOvmfFvTables (AcpiTable);\r
377e758c 257 }\r
258\r
96bbdbc8 259 return Status;\r
377e758c 260}\r
261\r