2 Provide generic extract guided section functions for Dxe phase.
4 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Library/DebugLib.h>
12 #include <Library/BaseMemoryLib.h>
13 #include <Library/MemoryAllocationLib.h>
14 #include <Library/ExtractGuidedSectionLib.h>
15 #include <Library/UefiBootServicesTableLib.h>
17 #define EXTRACT_HANDLER_TABLE_SIZE 0x10
19 UINT32 mNumberOfExtractHandler
= 0;
20 UINT32 mMaxNumberOfExtractHandler
= 0;
22 GUID
*mExtractHandlerGuidTable
= NULL
;
23 EXTRACT_GUIDED_SECTION_DECODE_HANDLER
*mExtractDecodeHandlerTable
= NULL
;
24 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
*mExtractGetInfoHandlerTable
= NULL
;
27 Reallocates more global memory to store the registered guid and Handler list.
29 @retval RETURN_SUCCESS Reallocated more global memory space to store guid and function tables.
30 @retval RETURN_OUT_OF_RESOURCES Not enough memory to allocate.
34 ReallocateExtractHandlerTable (
39 // Reallocate memory for GuidTable
41 mExtractHandlerGuidTable
= ReallocatePool (
42 mMaxNumberOfExtractHandler
* sizeof (GUID
),
43 (mMaxNumberOfExtractHandler
+ EXTRACT_HANDLER_TABLE_SIZE
) * sizeof (GUID
),
44 mExtractHandlerGuidTable
47 if (mExtractHandlerGuidTable
== NULL
) {
52 // Reallocate memory for Decode handler Table
54 mExtractDecodeHandlerTable
= ReallocatePool (
55 mMaxNumberOfExtractHandler
* sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER
),
56 (mMaxNumberOfExtractHandler
+ EXTRACT_HANDLER_TABLE_SIZE
) * sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER
),
57 mExtractDecodeHandlerTable
60 if (mExtractDecodeHandlerTable
== NULL
) {
65 // Reallocate memory for GetInfo handler Table
67 mExtractGetInfoHandlerTable
= ReallocatePool (
68 mMaxNumberOfExtractHandler
* sizeof (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
),
69 (mMaxNumberOfExtractHandler
+ EXTRACT_HANDLER_TABLE_SIZE
) * sizeof (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
),
70 mExtractGetInfoHandlerTable
73 if (mExtractGetInfoHandlerTable
== NULL
) {
78 // Increase max handler number
80 mMaxNumberOfExtractHandler
= mMaxNumberOfExtractHandler
+ EXTRACT_HANDLER_TABLE_SIZE
;
81 return RETURN_SUCCESS
;
84 if (mExtractHandlerGuidTable
!= NULL
) {
85 FreePool (mExtractHandlerGuidTable
);
87 if (mExtractDecodeHandlerTable
!= NULL
) {
88 FreePool (mExtractDecodeHandlerTable
);
90 if (mExtractGetInfoHandlerTable
!= NULL
) {
91 FreePool (mExtractGetInfoHandlerTable
);
94 return RETURN_OUT_OF_RESOURCES
;
97 Constructor allocates the global memory to store the registered guid and Handler list.
99 @param ImageHandle The firmware allocated handle for the EFI image.
100 @param SystemTable A pointer to the EFI System Table.
102 @retval RETURN_SUCCESS Allocated the global memory space to store guid and function tables.
103 @retval RETURN_OUT_OF_RESOURCES Not enough memory to allocate.
107 DxeExtractGuidedSectionLibConstructor (
108 IN EFI_HANDLE ImageHandle
,
109 IN EFI_SYSTEM_TABLE
*SystemTable
112 return ReallocateExtractHandlerTable ();
116 Retrieve the list GUIDs that have been registered through ExtractGuidedSectionRegisterHandlers().
118 Sets ExtractHandlerGuidTable so it points at a callee allocated array of registered GUIDs.
119 The total number of GUIDs in the array are returned. Since the array of GUIDs is callee allocated
120 and caller must treat this array of GUIDs as read-only data.
121 If ExtractHandlerGuidTable is NULL, then ASSERT().
123 @param[out] ExtractHandlerGuidTable A pointer to the array of GUIDs that have been registered through
124 ExtractGuidedSectionRegisterHandlers().
126 @return The number of the supported extract guided Handler.
131 ExtractGuidedSectionGetGuidList (
132 OUT GUID
**ExtractHandlerGuidTable
135 ASSERT (ExtractHandlerGuidTable
!= NULL
);
137 *ExtractHandlerGuidTable
= mExtractHandlerGuidTable
;
138 return mNumberOfExtractHandler
;
142 Registers handlers of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER and EXTRACT_GUIDED_SECTION_DECODE_HANDLER
143 for a specific GUID section type.
145 Registers the handlers specified by GetInfoHandler and DecodeHandler with the GUID specified by SectionGuid.
146 If the GUID value specified by SectionGuid has already been registered, then return RETURN_ALREADY_STARTED.
147 If there are not enough resources available to register the handlers then RETURN_OUT_OF_RESOURCES is returned.
149 If SectionGuid is NULL, then ASSERT().
150 If GetInfoHandler is NULL, then ASSERT().
151 If DecodeHandler is NULL, then ASSERT().
153 @param[in] SectionGuid A pointer to the GUID associated with the handlers
154 of the GUIDed section type being registered.
155 @param[in] GetInfoHandler The pointer to a function that examines a GUIDed section and returns the
156 size of the decoded buffer and the size of an optional scratch buffer
157 required to actually decode the data in a GUIDed section.
158 @param[in] DecodeHandler The pointer to a function that decodes a GUIDed section into a caller
159 allocated output buffer.
161 @retval RETURN_SUCCESS The handlers were registered.
162 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to register the handlers.
167 ExtractGuidedSectionRegisterHandlers (
168 IN CONST GUID
*SectionGuid
,
169 IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER GetInfoHandler
,
170 IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER DecodeHandler
177 // Check input parameter.
179 ASSERT (SectionGuid
!= NULL
);
180 ASSERT (GetInfoHandler
!= NULL
);
181 ASSERT (DecodeHandler
!= NULL
);
184 // Search the match registered GetInfo handler for the input guided section.
186 for (Index
= 0; Index
< mNumberOfExtractHandler
; Index
++) {
187 if (CompareGuid (&mExtractHandlerGuidTable
[Index
], SectionGuid
)) {
189 // If the guided handler has been registered before, only update its handler.
191 mExtractDecodeHandlerTable
[Index
] = DecodeHandler
;
192 mExtractGetInfoHandlerTable
[Index
] = GetInfoHandler
;
193 return RETURN_SUCCESS
;
198 // Check the global table is enough to contain new Handler.
200 if (mNumberOfExtractHandler
>= mMaxNumberOfExtractHandler
) {
201 if (ReallocateExtractHandlerTable () != RETURN_SUCCESS
) {
202 return RETURN_OUT_OF_RESOURCES
;
207 // Register new Handler and guid value.
209 CopyGuid (&mExtractHandlerGuidTable
[mNumberOfExtractHandler
], SectionGuid
);
210 mExtractDecodeHandlerTable
[mNumberOfExtractHandler
] = DecodeHandler
;
211 mExtractGetInfoHandlerTable
[mNumberOfExtractHandler
++] = GetInfoHandler
;
214 // Install the Guided Section GUID configuration table to record the GUID itself.
215 // Then the content of the configuration table buffer will be the same as the GUID value itself.
217 GuidData
= AllocateCopyPool (sizeof (GUID
), (VOID
*) SectionGuid
);
218 if (GuidData
!= NULL
) {
219 gBS
->InstallConfigurationTable ((EFI_GUID
*) SectionGuid
, GuidData
);
222 return RETURN_SUCCESS
;
226 Retrieves a GUID from a GUIDed section and uses that GUID to select an associated handler of type
227 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().
228 The selected handler is used to retrieve and return the size of the decoded buffer and the size of an
229 optional scratch buffer required to actually decode the data in a GUIDed section.
231 Examines a GUIDed section specified by InputSection.
232 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),
233 then RETURN_UNSUPPORTED is returned.
234 If the GUID of InputSection does match the GUID that this handler supports, then the associated handler
235 of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()
236 is used to retrieve the OutputBufferSize, ScratchSize, and Attributes values. The return status from the handler of
237 type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER is returned.
239 If InputSection is NULL, then ASSERT().
240 If OutputBufferSize is NULL, then ASSERT().
241 If ScratchBufferSize is NULL, then ASSERT().
242 If SectionAttribute is NULL, then ASSERT().
244 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
245 @param[out] OutputBufferSize A pointer to the size, in bytes, of an output buffer required if the buffer
246 specified by InputSection were decoded.
247 @param[out] ScratchBufferSize A pointer to the size, in bytes, required as scratch space if the buffer specified by
248 InputSection were decoded.
249 @param[out] SectionAttribute A pointer to the attributes of the GUIDed section. See the Attributes field of
250 EFI_GUID_DEFINED_SECTION in the PI Specification.
252 @retval RETURN_SUCCESS Successfully obtained the required information.
253 @retval RETURN_UNSUPPORTED The GUID from the section specified by InputSection does not match any of
254 the GUIDs registered with ExtractGuidedSectionRegisterHandlers().
255 @retval Others The return status from the handler associated with the GUID retrieved from
256 the section specified by InputSection.
261 ExtractGuidedSectionGetInfo (
262 IN CONST VOID
*InputSection
,
263 OUT UINT32
*OutputBufferSize
,
264 OUT UINT32
*ScratchBufferSize
,
265 OUT UINT16
*SectionAttribute
269 EFI_GUID
*SectionDefinitionGuid
;
271 ASSERT (InputSection
!= NULL
);
272 ASSERT (OutputBufferSize
!= NULL
);
273 ASSERT (ScratchBufferSize
!= NULL
);
274 ASSERT (SectionAttribute
!= NULL
);
276 if (IS_SECTION2 (InputSection
)) {
277 SectionDefinitionGuid
= &(((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->SectionDefinitionGuid
);
279 SectionDefinitionGuid
= &(((EFI_GUID_DEFINED_SECTION
*) InputSection
)->SectionDefinitionGuid
);
283 // Search the match registered GetInfo handler for the input guided section.
285 for (Index
= 0; Index
< mNumberOfExtractHandler
; Index
++) {
286 if (CompareGuid (&mExtractHandlerGuidTable
[Index
], SectionDefinitionGuid
)) {
288 // Call the match handler to getinfo for the input section data.
290 return mExtractGetInfoHandlerTable
[Index
] (
300 // Not found, the input guided section is not supported.
302 return RETURN_UNSUPPORTED
;
306 Retrieves the GUID from a GUIDed section and uses that GUID to select an associated handler of type
307 EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().
308 The selected handler is used to decode the data in a GUIDed section and return the result in a caller
309 allocated output buffer.
311 Decodes the GUIDed section specified by InputSection.
312 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),
313 then RETURN_UNSUPPORTED is returned.
314 If the GUID of InputSection does match the GUID that this handler supports, then the associated handler
315 of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()
316 is used to decode InputSection into the buffer specified by OutputBuffer and the authentication status of this
317 decode operation is returned in AuthenticationStatus. If the decoded buffer is identical to the data in InputSection,
318 then OutputBuffer is set to point at the data in InputSection. Otherwise, the decoded data will be placed in caller
319 allocated buffer specified by OutputBuffer. This function is responsible for computing the EFI_AUTH_STATUS_PLATFORM_OVERRIDE
320 bit of in AuthenticationStatus. The return status from the handler of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER is returned.
322 If InputSection is NULL, then ASSERT().
323 If OutputBuffer is NULL, then ASSERT().
324 If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().
325 If AuthenticationStatus is NULL, then ASSERT().
327 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
328 @param[out] OutputBuffer A pointer to a buffer that contains the result of a decode operation.
329 @param[in] ScratchBuffer A caller allocated buffer that may be required by this function as a scratch buffer to perform the decode operation.
330 @param[out] AuthenticationStatus
331 A pointer to the authentication status of the decoded output buffer. See the definition
332 of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI section of the PI
335 @retval RETURN_SUCCESS The buffer specified by InputSection was decoded.
336 @retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports.
337 @retval RETURN_INVALID_PARAMETER The section specified by InputSection can not be decoded.
342 ExtractGuidedSectionDecode (
343 IN CONST VOID
*InputSection
,
344 OUT VOID
**OutputBuffer
,
345 IN VOID
*ScratchBuffer
, OPTIONAL
346 OUT UINT32
*AuthenticationStatus
350 EFI_GUID
*SectionDefinitionGuid
;
353 // Check the input parameters
355 ASSERT (InputSection
!= NULL
);
356 ASSERT (OutputBuffer
!= NULL
);
357 ASSERT (AuthenticationStatus
!= NULL
);
359 if (IS_SECTION2 (InputSection
)) {
360 SectionDefinitionGuid
= &(((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->SectionDefinitionGuid
);
362 SectionDefinitionGuid
= &(((EFI_GUID_DEFINED_SECTION
*) InputSection
)->SectionDefinitionGuid
);
366 // Search the match registered extract handler for the input guided section.
368 for (Index
= 0; Index
< mNumberOfExtractHandler
; Index
++) {
369 if (CompareGuid (&mExtractHandlerGuidTable
[Index
], SectionDefinitionGuid
)) {
371 // Call the match handler to extract raw data for the input section data.
373 return mExtractDecodeHandlerTable
[Index
] (
383 // Not found, the input guided section is not supported.
385 return RETURN_UNSUPPORTED
;
389 Retrieves handlers of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER and
390 EXTRACT_GUIDED_SECTION_DECODE_HANDLER for a specific GUID section type.
392 Retrieves the handlers associated with SectionGuid and returns them in
393 GetInfoHandler and DecodeHandler.
395 If the GUID value specified by SectionGuid has not been registered, then
396 return RETURN_NOT_FOUND.
398 If SectionGuid is NULL, then ASSERT().
400 @param[in] SectionGuid A pointer to the GUID associated with the handlers of the GUIDed
401 section type being retrieved.
402 @param[out] GetInfoHandler Pointer to a function that examines a GUIDed section and returns
403 the size of the decoded buffer and the size of an optional scratch
404 buffer required to actually decode the data in a GUIDed section.
405 This is an optional parameter that may be NULL. If it is NULL, then
406 the previously registered handler is not returned.
407 @param[out] DecodeHandler Pointer to a function that decodes a GUIDed section into a caller
408 allocated output buffer. This is an optional parameter that may be NULL.
409 If it is NULL, then the previously registered handler is not returned.
411 @retval RETURN_SUCCESS The handlers were retrieved.
412 @retval RETURN_NOT_FOUND No handlers have been registered with the specified GUID.
417 ExtractGuidedSectionGetHandlers (
418 IN CONST GUID
*SectionGuid
,
419 OUT EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
*GetInfoHandler
, OPTIONAL
420 OUT EXTRACT_GUIDED_SECTION_DECODE_HANDLER
*DecodeHandler OPTIONAL
426 // Check input parameter.
428 ASSERT (SectionGuid
!= NULL
);
431 // Search the match registered GetInfo handler for the input guided section.
433 for (Index
= 0; Index
< mNumberOfExtractHandler
; Index
++) {
434 if (CompareGuid (&mExtractHandlerGuidTable
[Index
], SectionGuid
)) {
437 // If the guided handler has been registered before, then return the registered handlers.
439 if (GetInfoHandler
!= NULL
) {
440 *GetInfoHandler
= mExtractGetInfoHandlerTable
[Index
];
442 if (DecodeHandler
!= NULL
) {
443 *DecodeHandler
= mExtractDecodeHandlerTable
[Index
];
445 return RETURN_SUCCESS
;
448 return RETURN_NOT_FOUND
;