2 Provide generic extract guided section functions for PEI 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/PcdLib.h>
13 #include <Library/BaseMemoryLib.h>
14 #include <Library/HobLib.h>
15 #include <Library/ExtractGuidedSectionLib.h>
17 #define PEI_EXTRACT_HANDLER_INFO_SIGNATURE SIGNATURE_32 ('P', 'E', 'H', 'I')
21 UINT32 NumberOfExtractHandler
;
22 GUID
*ExtractHandlerGuidTable
;
23 EXTRACT_GUIDED_SECTION_DECODE_HANDLER
*ExtractDecodeHandlerTable
;
24 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
*ExtractGetInfoHandlerTable
;
25 } PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
;
28 Build guid hob for the global memory to store the registered guid and Handler list.
29 If GuidHob exists, HandlerInfo will be directly got from Guid hob data.
31 @param[in, out] InfoPointer The pointer to pei handler information structure.
33 @retval RETURN_SUCCESS Build Guid hob for the global memory space to store guid and function tables.
34 @retval RETURN_OUT_OF_RESOURCES No enough memory to allocated.
37 PeiGetExtractGuidedSectionHandlerInfo (
38 IN OUT PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
**InfoPointer
41 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
42 EFI_PEI_HOB_POINTERS Hob
;
45 // First try to get handler information from guid hob specified by CallerId.
47 Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION
, GetHobList ());
48 while (Hob
.Raw
!= NULL
) {
49 if (CompareGuid (&(Hob
.Guid
->Name
), &gEfiCallerIdGuid
)) {
50 HandlerInfo
= (PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
*)GET_GUID_HOB_DATA (Hob
.Guid
);
51 if (HandlerInfo
->Signature
== PEI_EXTRACT_HANDLER_INFO_SIGNATURE
) {
53 // Update Table Pointer when hob start address is changed.
55 if (HandlerInfo
->ExtractHandlerGuidTable
!= (GUID
*)(HandlerInfo
+ 1)) {
56 HandlerInfo
->ExtractHandlerGuidTable
= (GUID
*)(HandlerInfo
+ 1);
57 HandlerInfo
->ExtractDecodeHandlerTable
= (EXTRACT_GUIDED_SECTION_DECODE_HANDLER
*)(
58 (UINT8
*)HandlerInfo
->ExtractHandlerGuidTable
+
59 PcdGet32 (PcdMaximumGuidedExtractHandler
) * sizeof (GUID
)
61 HandlerInfo
->ExtractGetInfoHandlerTable
= (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
*)(
62 (UINT8
*)HandlerInfo
->ExtractDecodeHandlerTable
+
63 PcdGet32 (PcdMaximumGuidedExtractHandler
) *
64 sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER
)
69 // Return HandlerInfo pointer.
71 *InfoPointer
= HandlerInfo
;
76 Hob
.Raw
= GET_NEXT_HOB (Hob
);
77 Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION
, Hob
.Raw
);
81 // If Guid Hob is not found, Build CallerId Guid hob to store Handler Info
83 HandlerInfo
= BuildGuidHob (
85 sizeof (PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
) +
86 PcdGet32 (PcdMaximumGuidedExtractHandler
) *
87 (sizeof (GUID
) + sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER
) + sizeof (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
))
89 if (HandlerInfo
== NULL
) {
91 // No enough resource to build guid hob.
94 return EFI_OUT_OF_RESOURCES
;
98 // Init HandlerInfo structure
100 HandlerInfo
->Signature
= PEI_EXTRACT_HANDLER_INFO_SIGNATURE
;
101 HandlerInfo
->NumberOfExtractHandler
= 0;
102 HandlerInfo
->ExtractHandlerGuidTable
= (GUID
*)(HandlerInfo
+ 1);
103 HandlerInfo
->ExtractDecodeHandlerTable
= (EXTRACT_GUIDED_SECTION_DECODE_HANDLER
*)(
104 (UINT8
*)HandlerInfo
->ExtractHandlerGuidTable
+
105 PcdGet32 (PcdMaximumGuidedExtractHandler
) * sizeof (GUID
)
107 HandlerInfo
->ExtractGetInfoHandlerTable
= (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
*)(
108 (UINT8
*)HandlerInfo
->ExtractDecodeHandlerTable
+
109 PcdGet32 (PcdMaximumGuidedExtractHandler
) *
110 sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER
)
113 // return the created HandlerInfo.
115 *InfoPointer
= HandlerInfo
;
120 Retrieve the list GUIDs that have been registered through ExtractGuidedSectionRegisterHandlers().
122 Sets ExtractHandlerGuidTable so it points at a callee allocated array of registered GUIDs.
123 The total number of GUIDs in the array are returned. Since the array of GUIDs is callee allocated
124 and caller must treat this array of GUIDs as read-only data.
125 If ExtractHandlerGuidTable is NULL, then ASSERT().
127 @param[out] ExtractHandlerGuidTable A pointer to the array of GUIDs that have been registered through
128 ExtractGuidedSectionRegisterHandlers().
130 @return the number of the supported extract guided Handler.
135 ExtractGuidedSectionGetGuidList (
136 OUT GUID
**ExtractHandlerGuidTable
140 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
142 ASSERT (ExtractHandlerGuidTable
!= NULL
);
145 // Get all registered handler information
147 Status
= PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo
);
148 if (EFI_ERROR (Status
)) {
149 *ExtractHandlerGuidTable
= NULL
;
154 // Get GuidTable and Table Number
156 ASSERT (HandlerInfo
!= NULL
);
157 *ExtractHandlerGuidTable
= HandlerInfo
->ExtractHandlerGuidTable
;
158 return HandlerInfo
->NumberOfExtractHandler
;
162 Registers handlers of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER and EXTRACT_GUIDED_SECTION_DECODE_HANDLER
163 for a specific GUID section type.
165 Registers the handlers specified by GetInfoHandler and DecodeHandler with the GUID specified by SectionGuid.
166 If the GUID value specified by SectionGuid has already been registered, then return RETURN_ALREADY_STARTED.
167 If there are not enough resources available to register the handlers then RETURN_OUT_OF_RESOURCES is returned.
169 If SectionGuid is NULL, then ASSERT().
170 If GetInfoHandler is NULL, then ASSERT().
171 If DecodeHandler is NULL, then ASSERT().
173 @param[in] SectionGuid A pointer to the GUID associated with the the handlers
174 of the GUIDed section type being registered.
175 @param[in] GetInfoHandler The pointer to a function that examines a GUIDed section and returns the
176 size of the decoded buffer and the size of an optional scratch buffer
177 required to actually decode the data in a GUIDed section.
178 @param[in] DecodeHandler The pointer to a function that decodes a GUIDed section into a caller
179 allocated output buffer.
181 @retval RETURN_SUCCESS The handlers were registered.
182 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to register the handlers.
187 ExtractGuidedSectionRegisterHandlers (
188 IN CONST GUID
*SectionGuid
,
189 IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER GetInfoHandler
,
190 IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER DecodeHandler
195 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
198 // Check input parameter
200 ASSERT (SectionGuid
!= NULL
);
201 ASSERT (GetInfoHandler
!= NULL
);
202 ASSERT (DecodeHandler
!= NULL
);
205 // Get the registered handler information
207 Status
= PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo
);
208 if (EFI_ERROR (Status
)) {
213 // Search the match registered GetInfo handler for the input guided section.
215 ASSERT (HandlerInfo
!= NULL
);
216 for (Index
= 0; Index
< HandlerInfo
->NumberOfExtractHandler
; Index
++) {
217 if (CompareGuid (HandlerInfo
->ExtractHandlerGuidTable
+ Index
, SectionGuid
)) {
219 // If the guided handler has been registered before, only update its handler.
221 HandlerInfo
->ExtractDecodeHandlerTable
[Index
] = DecodeHandler
;
222 HandlerInfo
->ExtractGetInfoHandlerTable
[Index
] = GetInfoHandler
;
223 return RETURN_SUCCESS
;
228 // Check the global table is enough to contain new Handler.
230 if (HandlerInfo
->NumberOfExtractHandler
>= PcdGet32 (PcdMaximumGuidedExtractHandler
)) {
231 return RETURN_OUT_OF_RESOURCES
;
235 // Register new Handler and guid value.
237 CopyGuid (HandlerInfo
->ExtractHandlerGuidTable
+ HandlerInfo
->NumberOfExtractHandler
, SectionGuid
);
238 HandlerInfo
->ExtractDecodeHandlerTable
[HandlerInfo
->NumberOfExtractHandler
] = DecodeHandler
;
239 HandlerInfo
->ExtractGetInfoHandlerTable
[HandlerInfo
->NumberOfExtractHandler
++] = GetInfoHandler
;
242 // Build the Guided Section GUID HOB to record the GUID itself.
243 // Then the content of the GUIDed HOB will be the same as the GUID value itself.
246 (EFI_GUID
*)SectionGuid
,
251 return RETURN_SUCCESS
;
255 Retrieves a GUID from a GUIDed section and uses that GUID to select an associated handler of type
256 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().
257 The selected handler is used to retrieve and return the size of the decoded buffer and the size of an
258 optional scratch buffer required to actually decode the data in a GUIDed section.
260 Examines a GUIDed section specified by InputSection.
261 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),
262 then RETURN_UNSUPPORTED is returned.
263 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler
264 of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()
265 is used to retrieve the OututBufferSize, ScratchSize, and Attributes values. The return status from the handler of
266 type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER is returned.
268 If InputSection is NULL, then ASSERT().
269 If OutputBufferSize is NULL, then ASSERT().
270 If ScratchBufferSize is NULL, then ASSERT().
271 If SectionAttribute is NULL, then ASSERT().
273 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
274 @param[out] OutputBufferSize A pointer to the size, in bytes, of an output buffer required if the buffer
275 specified by InputSection were decoded.
276 @param[out] ScratchBufferSize A pointer to the size, in bytes, required as scratch space if the buffer specified by
277 InputSection were decoded.
278 @param[out] SectionAttribute A pointer to the attributes of the GUIDed section. See the Attributes field of
279 EFI_GUID_DEFINED_SECTION in the PI Specification.
281 @retval RETURN_SUCCESS Get the required information successfully.
282 @retval RETURN_UNSUPPORTED The GUID from the section specified by InputSection does not match any of
283 the GUIDs registered with ExtractGuidedSectionRegisterHandlers().
284 @retval Others The return status from the handler associated with the GUID retrieved from
285 the section specified by InputSection.
290 ExtractGuidedSectionGetInfo (
291 IN CONST VOID
*InputSection
,
292 OUT UINT32
*OutputBufferSize
,
293 OUT UINT32
*ScratchBufferSize
,
294 OUT UINT16
*SectionAttribute
299 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
300 EFI_GUID
*SectionDefinitionGuid
;
303 // Check input parameter
305 ASSERT (InputSection
!= NULL
);
306 ASSERT (OutputBufferSize
!= NULL
);
307 ASSERT (ScratchBufferSize
!= NULL
);
308 ASSERT (SectionAttribute
!= NULL
);
311 // Get all registered handler information.
313 Status
= PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo
);
314 if (EFI_ERROR (Status
)) {
318 if (IS_SECTION2 (InputSection
)) {
319 SectionDefinitionGuid
= &(((EFI_GUID_DEFINED_SECTION2
*)InputSection
)->SectionDefinitionGuid
);
321 SectionDefinitionGuid
= &(((EFI_GUID_DEFINED_SECTION
*)InputSection
)->SectionDefinitionGuid
);
325 // Search the match registered GetInfo handler for the input guided section.
327 ASSERT (HandlerInfo
!= NULL
);
328 for (Index
= 0; Index
< HandlerInfo
->NumberOfExtractHandler
; Index
++) {
329 if (CompareGuid (HandlerInfo
->ExtractHandlerGuidTable
+ Index
, SectionDefinitionGuid
)) {
331 // Call the match handler to get information for the input section data.
333 return HandlerInfo
->ExtractGetInfoHandlerTable
[Index
](
343 // Not found, the input guided section is not supported.
345 return RETURN_UNSUPPORTED
;
349 Retrieves the GUID from a GUIDed section and uses that GUID to select an associated handler of type
350 EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().
351 The selected handler is used to decode the data in a GUIDed section and return the result in a caller
352 allocated output buffer.
354 Decodes the GUIDed section specified by InputSection.
355 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),
356 then RETURN_UNSUPPORTED is returned.
357 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler
358 of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()
359 is used to decode InputSection into the buffer specified by OutputBuffer and the authentication status of this
360 decode operation is returned in AuthenticationStatus. If the decoded buffer is identical to the data in InputSection,
361 then OutputBuffer is set to point at the data in InputSection. Otherwise, the decoded data will be placed in caller
362 allocated buffer specified by OutputBuffer. This function is responsible for computing the EFI_AUTH_STATUS_PLATFORM_OVERRIDE
363 bit of in AuthenticationStatus. The return status from the handler of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER is returned.
365 If InputSection is NULL, then ASSERT().
366 If OutputBuffer is NULL, then ASSERT().
367 If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().
368 If AuthenticationStatus is NULL, then ASSERT().
370 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
371 @param[out] OutputBuffer A pointer to a buffer that contains the result of a decode operation.
372 @param[in] ScratchBuffer A caller allocated buffer that may be required by this function as a scratch buffer to perform the decode operation.
373 @param[out] AuthenticationStatus
374 A pointer to the authentication status of the decoded output buffer. See the definition
375 of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI section of the PI
378 @retval RETURN_SUCCESS The buffer specified by InputSection was decoded.
379 @retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports.
380 @retval RETURN_INVALID_PARAMETER The section specified by InputSection can not be decoded.
385 ExtractGuidedSectionDecode (
386 IN CONST VOID
*InputSection
,
387 OUT VOID
**OutputBuffer
,
388 IN VOID
*ScratchBuffer OPTIONAL
,
389 OUT UINT32
*AuthenticationStatus
394 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
395 EFI_GUID
*SectionDefinitionGuid
;
398 // Check input parameter
400 ASSERT (InputSection
!= NULL
);
401 ASSERT (OutputBuffer
!= NULL
);
402 ASSERT (AuthenticationStatus
!= NULL
);
405 // Get all registered handler information.
407 Status
= PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo
);
408 if (EFI_ERROR (Status
)) {
412 if (IS_SECTION2 (InputSection
)) {
413 SectionDefinitionGuid
= &(((EFI_GUID_DEFINED_SECTION2
*)InputSection
)->SectionDefinitionGuid
);
415 SectionDefinitionGuid
= &(((EFI_GUID_DEFINED_SECTION
*)InputSection
)->SectionDefinitionGuid
);
419 // Search the match registered Extract handler for the input guided section.
421 ASSERT (HandlerInfo
!= NULL
);
422 for (Index
= 0; Index
< HandlerInfo
->NumberOfExtractHandler
; Index
++) {
423 if (CompareGuid (HandlerInfo
->ExtractHandlerGuidTable
+ Index
, SectionDefinitionGuid
)) {
425 // Call the match handler to extract raw data for the input guided section.
427 return HandlerInfo
->ExtractDecodeHandlerTable
[Index
](
437 // Not found, the input guided section is not supported.
439 return RETURN_UNSUPPORTED
;
443 Retrieves handlers of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER and
444 EXTRACT_GUIDED_SECTION_DECODE_HANDLER for a specific GUID section type.
446 Retrieves the handlers associated with SectionGuid and returns them in
447 GetInfoHandler and DecodeHandler.
449 If the GUID value specified by SectionGuid has not been registered, then
450 return RETURN_NOT_FOUND.
452 If SectionGuid is NULL, then ASSERT().
454 @param[in] SectionGuid A pointer to the GUID associated with the handlersof the GUIDed
455 section type being retrieved.
456 @param[out] GetInfoHandler Pointer to a function that examines a GUIDed section and returns
457 the size of the decoded buffer and the size of an optional scratch
458 buffer required to actually decode the data in a GUIDed section.
459 This is an optional parameter that may be NULL. If it is NULL, then
460 the previously registered handler is not returned.
461 @param[out] DecodeHandler Pointer to a function that decodes a GUIDed section into a caller
462 allocated output buffer. This is an optional parameter that may be NULL.
463 If it is NULL, then the previously registered handler is not returned.
465 @retval RETURN_SUCCESS The handlers were retrieved.
466 @retval RETURN_NOT_FOUND No handlers have been registered with the specified GUID.
471 ExtractGuidedSectionGetHandlers (
472 IN CONST GUID
*SectionGuid
,
473 OUT EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
*GetInfoHandler OPTIONAL
,
474 OUT EXTRACT_GUIDED_SECTION_DECODE_HANDLER
*DecodeHandler OPTIONAL
479 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
482 // Check input parameter
484 ASSERT (SectionGuid
!= NULL
);
487 // Get the registered handler information
489 Status
= PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo
);
490 if (EFI_ERROR (Status
)) {
495 // Search the match registered GetInfo handler for the input guided section.
497 ASSERT (HandlerInfo
!= NULL
);
498 for (Index
= 0; Index
< HandlerInfo
->NumberOfExtractHandler
; Index
++) {
499 if (CompareGuid (HandlerInfo
->ExtractHandlerGuidTable
+ Index
, SectionGuid
)) {
501 // If the guided handler has been registered before, then return the registered handlers.
503 if (GetInfoHandler
!= NULL
) {
504 *GetInfoHandler
= HandlerInfo
->ExtractGetInfoHandlerTable
[Index
];
507 if (DecodeHandler
!= NULL
) {
508 *DecodeHandler
= HandlerInfo
->ExtractDecodeHandlerTable
[Index
];
511 return RETURN_SUCCESS
;
515 return RETURN_NOT_FOUND
;