VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/StandaloneMmPkg/Library/StandaloneMmExtractGuidedSectionLib/StandaloneMmExtractGuidedSectionLib.c@ 108794

Last change on this file since 108794 was 108794, checked in by vboxsync, 2 weeks ago

Devices/EFI/FirmwareNew: Merge edk2-stable202502 from the vendor branch and make it build for the important platforms, bugref:4643

  • Property svn:eol-style set to native
File size: 15.9 KB
Line 
1/** @file
2
3 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
4 Copyright (c) 2024, Arm Ltd. All rights reserved.<BR>
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8**/
9
10#include <PiMm.h>
11#include <Pi/PiFirmwareFile.h>
12
13#include <Library/BaseLib.h>
14#include <Library/BaseMemoryLib.h>
15#include <Library/DebugLib.h>
16#include <Library/ExtractGuidedSectionLib.h>
17#include <Library/MemoryAllocationLib.h>
18#include <Library/PcdLib.h>
19
20/**
21 Extract guided section handler info.
22 */
23typedef struct {
24 /// Number of handlers.
25 UINT32 NumberOfExtractHandler;
26
27 /// Padding.
28 UINT32 Padding;
29
30 /// Guid table of handlers
31 GUID *ExtractHandlerGuidTable;
32
33 /// Handler table for decoding correspond guided section.
34 EXTRACT_GUIDED_SECTION_DECODE_HANDLER *ExtractDecodeHandlerTable;
35
36 /// Handler table for getting information about correspond guided section.
37 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *ExtractGetInfoHandlerTable;
38} EXTRACT_GUIDED_SECTION_HANDLER_INFO;
39
40STATIC EXTRACT_GUIDED_SECTION_HANDLER_INFO *mHandlerInfo;
41
42/**
43 Registers handlers of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER and EXTRACT_GUIDED_SECTION_DECODE_HANDLER
44 for a specific GUID section type.
45
46 Registers the handlers specified by GetInfoHandler and DecodeHandler with the GUID specified by SectionGuid.
47 If the GUID value specified by SectionGuid has already been registered, then return RETURN_ALREADY_STARTED.
48 If there are not enough resources available to register the handlers then RETURN_OUT_OF_RESOURCES is returned.
49
50 If SectionGuid is NULL, then ASSERT().
51 If GetInfoHandler is NULL, then ASSERT().
52 If DecodeHandler is NULL, then ASSERT().
53
54 @param[in] SectionGuid A pointer to the GUID associated with the the handlers
55 of the GUIDed section type being registered.
56 @param[in] GetInfoHandler Pointer to a function that examines a GUIDed section and returns the
57 size of the decoded buffer and the size of an optional scratch buffer
58 required to actually decode the data in a GUIDed section.
59 @param[in] DecodeHandler Pointer to a function that decodes a GUIDed section into a caller
60 allocated output buffer.
61
62 @retval RETURN_SUCCESS The handlers were registered.
63 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to register the handlers.
64
65**/
66RETURN_STATUS
67EFIAPI
68ExtractGuidedSectionRegisterHandlers (
69 IN CONST GUID *SectionGuid,
70 IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER GetInfoHandler,
71 IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER DecodeHandler
72 )
73{
74 UINT32 Index;
75
76 //
77 // Check input parameter.
78 //
79 if (SectionGuid == NULL) {
80 return RETURN_INVALID_PARAMETER;
81 }
82
83 if (mHandlerInfo == NULL) {
84 return RETURN_NOT_READY;
85 }
86
87 //
88 // Search the match registered GetInfo handler for the input guided section.
89 //
90 for (Index = 0; Index < mHandlerInfo->NumberOfExtractHandler; Index++) {
91 if (CompareGuid (&mHandlerInfo->ExtractHandlerGuidTable[Index], SectionGuid)) {
92 break;
93 }
94 }
95
96 //
97 // If the guided handler has been registered before, only update its handler.
98 //
99 if (Index < mHandlerInfo->NumberOfExtractHandler) {
100 mHandlerInfo->ExtractDecodeHandlerTable[Index] = DecodeHandler;
101 mHandlerInfo->ExtractGetInfoHandlerTable[Index] = GetInfoHandler;
102 return RETURN_SUCCESS;
103 }
104
105 //
106 // Check the global table is enough to contain new Handler.
107 //
108 if (mHandlerInfo->NumberOfExtractHandler >= PcdGet32 (PcdMaximumGuidedExtractHandler)) {
109 return RETURN_OUT_OF_RESOURCES;
110 }
111
112 //
113 // Register new Handler and guid value.
114 //
115 CopyGuid (&mHandlerInfo->ExtractHandlerGuidTable[mHandlerInfo->NumberOfExtractHandler], SectionGuid);
116 mHandlerInfo->ExtractDecodeHandlerTable[mHandlerInfo->NumberOfExtractHandler] = DecodeHandler;
117 mHandlerInfo->ExtractGetInfoHandlerTable[mHandlerInfo->NumberOfExtractHandler++] = GetInfoHandler;
118
119 return RETURN_SUCCESS;
120}
121
122/**
123 Retrieve the list GUIDs that have been registered through ExtractGuidedSectionRegisterHandlers().
124
125 Sets ExtractHandlerGuidTable so it points at a callee allocated array of registered GUIDs.
126 The total number of GUIDs in the array are returned. Since the array of GUIDs is callee allocated
127 and caller must treat this array of GUIDs as read-only data.
128 If ExtractHandlerGuidTable is NULL, then ASSERT().
129
130 @param[in, out] ExtractHandlerGuidTable A pointer to the array of GUIDs that have been registered through
131 ExtractGuidedSectionRegisterHandlers().
132
133 @return the number of the supported extract guided Handler.
134
135**/
136UINTN
137EFIAPI
138ExtractGuidedSectionGetGuidList (
139 IN OUT GUID **ExtractHandlerGuidTable
140 )
141{
142 ASSERT (ExtractHandlerGuidTable != NULL);
143 ASSERT (mHandlerInfo != NULL);
144
145 *ExtractHandlerGuidTable = mHandlerInfo->ExtractHandlerGuidTable;
146
147 return mHandlerInfo->NumberOfExtractHandler;
148}
149
150/**
151 Retrieves a GUID from a GUIDed section and uses that GUID to select an associated handler of type
152 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().
153 The selected handler is used to retrieve and return the size of the decoded buffer and the size of an
154 optional scratch buffer required to actually decode the data in a GUIDed section.
155
156 Examines a GUIDed section specified by InputSection.
157 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),
158 then RETURN_UNSUPPORTED is returned.
159 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler
160 of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()
161 is used to retrieve the OututBufferSize, ScratchSize, and Attributes values. The return status from the handler of
162 type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER is returned.
163
164 If InputSection is NULL, then ASSERT().
165 If OutputBufferSize is NULL, then ASSERT().
166 If ScratchBufferSize is NULL, then ASSERT().
167 If SectionAttribute is NULL, then ASSERT().
168
169 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
170 @param[out] OutputBufferSize A pointer to the size, in bytes, of an output buffer required if the buffer
171 specified by InputSection were decoded.
172 @param[out] ScratchBufferSize A pointer to the size, in bytes, required as scratch space if the buffer specified by
173 InputSection were decoded.
174 @param[out] SectionAttribute A pointer to the attributes of the GUIDed section. See the Attributes field of
175 EFI_GUID_DEFINED_SECTION in the PI Specification.
176
177 @retval RETURN_SUCCESS Get the required information successfully.
178 @retval RETURN_UNSUPPORTED The GUID from the section specified by InputSection does not match any of
179 the GUIDs registered with ExtractGuidedSectionRegisterHandlers().
180 @retval Others The return status from the handler associated with the GUID retrieved from
181 the section specified by InputSection.
182
183**/
184RETURN_STATUS
185EFIAPI
186ExtractGuidedSectionGetInfo (
187 IN CONST VOID *InputSection,
188 OUT UINT32 *OutputBufferSize,
189 OUT UINT32 *ScratchBufferSize,
190 OUT UINT16 *SectionAttribute
191 )
192{
193 UINT32 Index;
194 EFI_GUID *SectionDefinitionGuid;
195
196 if (InputSection == NULL) {
197 return RETURN_INVALID_PARAMETER;
198 }
199
200 ASSERT (OutputBufferSize != NULL);
201 ASSERT (ScratchBufferSize != NULL);
202 ASSERT (SectionAttribute != NULL);
203
204 if (IS_SECTION2 (InputSection)) {
205 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION2 *)InputSection)->SectionDefinitionGuid);
206 } else {
207 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION *)InputSection)->SectionDefinitionGuid);
208 }
209
210 //
211 // Search the match registered GetInfo handler for the input guided section.
212 //
213 for (Index = 0; Index < mHandlerInfo->NumberOfExtractHandler; Index++) {
214 if (CompareGuid (&mHandlerInfo->ExtractHandlerGuidTable[Index], SectionDefinitionGuid)) {
215 break;
216 }
217 }
218
219 //
220 // Not found, the input guided section is not supported.
221 //
222 if (Index == mHandlerInfo->NumberOfExtractHandler) {
223 return RETURN_INVALID_PARAMETER;
224 }
225
226 //
227 // Call the match handler to getinfo for the input section data.
228 //
229 return mHandlerInfo->ExtractGetInfoHandlerTable[Index](
230 InputSection,
231 OutputBufferSize,
232 ScratchBufferSize,
233 SectionAttribute
234 );
235}
236
237/**
238 Retrieves the GUID from a GUIDed section and uses that GUID to select an associated handler of type
239 EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().
240 The selected handler is used to decode the data in a GUIDed section and return the result in a caller
241 allocated output buffer.
242
243 Decodes the GUIDed section specified by InputSection.
244 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),
245 then RETURN_UNSUPPORTED is returned.
246 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler
247 of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()
248 is used to decode InputSection into the buffer specified by OutputBuffer and the authentication status of this
249 decode operation is returned in AuthenticationStatus. If the decoded buffer is identical to the data in InputSection,
250 then OutputBuffer is set to point at the data in InputSection. Otherwise, the decoded data will be placed in caller
251 allocated buffer specified by OutputBuffer. This function is responsible for computing the EFI_AUTH_STATUS_PLATFORM_OVERRIDE
252 bit of in AuthenticationStatus. The return status from the handler of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER is returned.
253
254 If InputSection is NULL, then ASSERT().
255 If OutputBuffer is NULL, then ASSERT().
256 If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().
257 If AuthenticationStatus is NULL, then ASSERT().
258
259 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
260 @param[out] OutputBuffer A pointer to a buffer that contains the result of a decode operation.
261 @param[out] ScratchBuffer A caller allocated buffer that may be required by this function as a scratch buffer to perform the decode operation.
262 @param[out] AuthenticationStatus
263 A pointer to the authentication status of the decoded output buffer. See the definition
264 of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI section of the PI
265 Specification.
266
267 @retval RETURN_SUCCESS The buffer specified by InputSection was decoded.
268 @retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports.
269 @retval RETURN_INVALID_PARAMETER The section specified by InputSection can not be decoded.
270
271**/
272RETURN_STATUS
273EFIAPI
274ExtractGuidedSectionDecode (
275 IN CONST VOID *InputSection,
276 OUT VOID **OutputBuffer,
277 OUT VOID *ScratchBuffer OPTIONAL,
278 OUT UINT32 *AuthenticationStatus
279 )
280{
281 UINT32 Index;
282 EFI_GUID *SectionDefinitionGuid;
283
284 if (InputSection == NULL) {
285 return RETURN_INVALID_PARAMETER;
286 }
287
288 ASSERT (OutputBuffer != NULL);
289 ASSERT (AuthenticationStatus != NULL);
290
291 if (IS_SECTION2 (InputSection)) {
292 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION2 *)InputSection)->SectionDefinitionGuid);
293 } else {
294 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION *)InputSection)->SectionDefinitionGuid);
295 }
296
297 //
298 // Search the match registered GetInfo handler for the input guided section.
299 //
300 for (Index = 0; Index < mHandlerInfo->NumberOfExtractHandler; Index++) {
301 if (CompareGuid (&mHandlerInfo->ExtractHandlerGuidTable[Index], SectionDefinitionGuid)) {
302 break;
303 }
304 }
305
306 //
307 // Not found, the input guided section is not supported.
308 //
309 if (Index == mHandlerInfo->NumberOfExtractHandler) {
310 return RETURN_INVALID_PARAMETER;
311 }
312
313 //
314 // Call the match handler to getinfo for the input section data.
315 //
316 return mHandlerInfo->ExtractDecodeHandlerTable[Index](
317 InputSection,
318 OutputBuffer,
319 ScratchBuffer,
320 AuthenticationStatus
321 );
322}
323
324/**
325 ExtraGuidSectionLib constructor for StandaloneMmCore.
326
327 @param [in] ImageHandle The firmware allocated handle for the EFI image.
328 @param [in] MmSystemTable A pointer to the Management mode System Table.
329
330 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
331
332**/
333RETURN_STATUS
334EFIAPI
335StandaloneMmExtractGuidedSectionLibConstructor (
336 IN EFI_HANDLE ImageHandle,
337 IN EFI_MM_SYSTEM_TABLE *MmSystemTable
338 )
339{
340 RETURN_STATUS Status;
341 UINTN Idx;
342 EFI_CONFIGURATION_TABLE *ConfigurationTable;
343 UINT8 *Buffer;
344 UINTN HandlerInfoSize;
345
346 Status = RETURN_NOT_FOUND;
347
348 // Find ExtraGuidedSectionHandlerInfo in ConfigurationTable.
349 ConfigurationTable = MmSystemTable->MmConfigurationTable;
350 for (Idx = 0; Idx < MmSystemTable->NumberOfTableEntries; Idx++) {
351 if (CompareGuid (&gExtraGuidedSectionHandlerInfoGuid, &ConfigurationTable[Idx].VendorGuid)) {
352 Status = RETURN_SUCCESS;
353 break;
354 }
355 }
356
357 if (!RETURN_ERROR (Status)) {
358 mHandlerInfo = ConfigurationTable[Idx].VendorTable;
359 return RETURN_SUCCESS;
360 }
361
362 /// Allocate new ExtraGuidedSectionHandlerInfo and install in ConfigurationTable.
363 HandlerInfoSize = (sizeof (EXTRACT_GUIDED_SECTION_HANDLER_INFO) +
364 (PcdGet32 (PcdMaximumGuidedExtractHandler) *
365 ((sizeof (GUID) +
366 sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER) +
367 sizeof (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER)))
368 ));
369
370 Buffer = AllocateZeroPool (HandlerInfoSize);
371 if (Buffer == NULL) {
372 DEBUG ((DEBUG_ERROR, "%a: Failed to allocate HandlerInfo...\n", __func__));
373 return RETURN_OUT_OF_RESOURCES;
374 }
375
376 mHandlerInfo = (EXTRACT_GUIDED_SECTION_HANDLER_INFO *)Buffer;
377
378 Buffer += sizeof (EXTRACT_GUIDED_SECTION_HANDLER_INFO);
379 mHandlerInfo->ExtractHandlerGuidTable = (GUID *)Buffer;
380
381 Buffer += (sizeof (GUID) * PcdGet32 (PcdMaximumGuidedExtractHandler));
382 mHandlerInfo->ExtractDecodeHandlerTable = (EXTRACT_GUIDED_SECTION_DECODE_HANDLER *)Buffer;
383
384 Buffer += (sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER) * PcdGet32 (PcdMaximumGuidedExtractHandler));
385 mHandlerInfo->ExtractGetInfoHandlerTable = (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *)Buffer;
386
387 Status = MmSystemTable->MmInstallConfigurationTable (
388 MmSystemTable,
389 &gExtraGuidedSectionHandlerInfoGuid,
390 mHandlerInfo,
391 HandlerInfoSize
392 );
393 if (RETURN_ERROR (Status)) {
394 DEBUG ((
395 DEBUG_ERROR,
396 "%a: Failed to install ExtraGuidedSection Handler Info... (%r)\n",
397 __func__,
398 Status
399 ));
400 FreePool (mHandlerInfo);
401 mHandlerInfo = NULL;
402 return Status;
403 }
404
405 return RETURN_SUCCESS;
406}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette