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
)
68 // Return HandlerInfo pointer.
70 *InfoPointer
= HandlerInfo
;
74 Hob
.Raw
= GET_NEXT_HOB (Hob
);
75 Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION
, Hob
.Raw
);
79 // If Guid Hob is not found, Build CallerId Guid hob to store Handler Info
81 HandlerInfo
= BuildGuidHob (
83 sizeof (PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
) +
84 PcdGet32 (PcdMaximumGuidedExtractHandler
) *
85 (sizeof (GUID
) + sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER
) + sizeof (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
))
87 if (HandlerInfo
== NULL
) {
89 // No enough resource to build guid hob.
92 return EFI_OUT_OF_RESOURCES
;
95 // Init HandlerInfo structure
97 HandlerInfo
->Signature
= PEI_EXTRACT_HANDLER_INFO_SIGNATURE
;
98 HandlerInfo
->NumberOfExtractHandler
= 0;
99 HandlerInfo
->ExtractHandlerGuidTable
= (GUID
*) (HandlerInfo
+ 1);
100 HandlerInfo
->ExtractDecodeHandlerTable
= (EXTRACT_GUIDED_SECTION_DECODE_HANDLER
*) (
101 (UINT8
*)HandlerInfo
->ExtractHandlerGuidTable
+
102 PcdGet32 (PcdMaximumGuidedExtractHandler
) * sizeof (GUID
)
104 HandlerInfo
->ExtractGetInfoHandlerTable
= (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
*) (
105 (UINT8
*)HandlerInfo
->ExtractDecodeHandlerTable
+
106 PcdGet32 (PcdMaximumGuidedExtractHandler
) *
107 sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER
)
110 // return the created HandlerInfo.
112 *InfoPointer
= HandlerInfo
;
117 Retrieve the list GUIDs that have been registered through ExtractGuidedSectionRegisterHandlers().
119 Sets ExtractHandlerGuidTable so it points at a callee allocated array of registered GUIDs.
120 The total number of GUIDs in the array are returned. Since the array of GUIDs is callee allocated
121 and caller must treat this array of GUIDs as read-only data.
122 If ExtractHandlerGuidTable is NULL, then ASSERT().
124 @param[out] ExtractHandlerGuidTable A pointer to the array of GUIDs that have been registered through
125 ExtractGuidedSectionRegisterHandlers().
127 @return the number of the supported extract guided Handler.
132 ExtractGuidedSectionGetGuidList (
133 OUT GUID
**ExtractHandlerGuidTable
137 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
139 ASSERT (ExtractHandlerGuidTable
!= NULL
);
142 // Get all registered handler information
144 Status
= PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo
);
145 if (EFI_ERROR (Status
)) {
146 *ExtractHandlerGuidTable
= NULL
;
151 // Get GuidTable and Table Number
153 ASSERT (HandlerInfo
!= NULL
);
154 *ExtractHandlerGuidTable
= HandlerInfo
->ExtractHandlerGuidTable
;
155 return HandlerInfo
->NumberOfExtractHandler
;
159 Registers handlers of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER and EXTRACT_GUIDED_SECTION_DECODE_HANDLER
160 for a specific GUID section type.
162 Registers the handlers specified by GetInfoHandler and DecodeHandler with the GUID specified by SectionGuid.
163 If the GUID value specified by SectionGuid has already been registered, then return RETURN_ALREADY_STARTED.
164 If there are not enough resources available to register the handlers then RETURN_OUT_OF_RESOURCES is returned.
166 If SectionGuid is NULL, then ASSERT().
167 If GetInfoHandler is NULL, then ASSERT().
168 If DecodeHandler is NULL, then ASSERT().
170 @param[in] SectionGuid A pointer to the GUID associated with the the handlers
171 of the GUIDed section type being registered.
172 @param[in] GetInfoHandler The pointer to a function that examines a GUIDed section and returns the
173 size of the decoded buffer and the size of an optional scratch buffer
174 required to actually decode the data in a GUIDed section.
175 @param[in] DecodeHandler The pointer to a function that decodes a GUIDed section into a caller
176 allocated output buffer.
178 @retval RETURN_SUCCESS The handlers were registered.
179 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to register the handlers.
184 ExtractGuidedSectionRegisterHandlers (
185 IN CONST GUID
*SectionGuid
,
186 IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER GetInfoHandler
,
187 IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER DecodeHandler
192 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
195 // Check input parameter
197 ASSERT (SectionGuid
!= NULL
);
198 ASSERT (GetInfoHandler
!= NULL
);
199 ASSERT (DecodeHandler
!= NULL
);
204 // Get the registered handler information
206 Status
= PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo
);
207 if (EFI_ERROR (Status
)) {
212 // Search the match registered GetInfo handler for the input guided section.
214 ASSERT (HandlerInfo
!= NULL
);
215 for (Index
= 0; Index
< HandlerInfo
->NumberOfExtractHandler
; Index
++) {
216 if (CompareGuid (HandlerInfo
->ExtractHandlerGuidTable
+ Index
, SectionGuid
)) {
218 // If the guided handler has been registered before, only update its handler.
220 HandlerInfo
->ExtractDecodeHandlerTable
[Index
] = DecodeHandler
;
221 HandlerInfo
->ExtractGetInfoHandlerTable
[Index
] = GetInfoHandler
;
222 return RETURN_SUCCESS
;
227 // Check the global table is enough to contain new Handler.
229 if (HandlerInfo
->NumberOfExtractHandler
>= PcdGet32 (PcdMaximumGuidedExtractHandler
)) {
230 return RETURN_OUT_OF_RESOURCES
;
234 // Register new Handler and guid value.
236 CopyGuid (HandlerInfo
->ExtractHandlerGuidTable
+ HandlerInfo
->NumberOfExtractHandler
, SectionGuid
);
237 HandlerInfo
->ExtractDecodeHandlerTable
[HandlerInfo
->NumberOfExtractHandler
] = DecodeHandler
;
238 HandlerInfo
->ExtractGetInfoHandlerTable
[HandlerInfo
->NumberOfExtractHandler
++] = GetInfoHandler
;
241 // Build the Guided Section GUID HOB to record the GUID itself.
242 // Then the content of the GUIDed HOB will be the same as the GUID value itself.
245 (EFI_GUID
*) SectionGuid
,
246 (VOID
*) SectionGuid
,
250 return RETURN_SUCCESS
;
254 Retrieves a GUID from a GUIDed section and uses that GUID to select an associated handler of type
255 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().
256 The selected handler is used to retrieve and return the size of the decoded buffer and the size of an
257 optional scratch buffer required to actually decode the data in a GUIDed section.
259 Examines a GUIDed section specified by InputSection.
260 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),
261 then RETURN_UNSUPPORTED is returned.
262 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler
263 of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()
264 is used to retrieve the OututBufferSize, ScratchSize, and Attributes values. The return status from the handler of
265 type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER is returned.
267 If InputSection is NULL, then ASSERT().
268 If OutputBufferSize is NULL, then ASSERT().
269 If ScratchBufferSize is NULL, then ASSERT().
270 If SectionAttribute is NULL, then ASSERT().
272 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
273 @param[out] OutputBufferSize A pointer to the size, in bytes, of an output buffer required if the buffer
274 specified by InputSection were decoded.
275 @param[out] ScratchBufferSize A pointer to the size, in bytes, required as scratch space if the buffer specified by
276 InputSection were decoded.
277 @param[out] SectionAttribute A pointer to the attributes of the GUIDed section. See the Attributes field of
278 EFI_GUID_DEFINED_SECTION in the PI Specification.
280 @retval RETURN_SUCCESS Get the required information successfully.
281 @retval RETURN_UNSUPPORTED The GUID from the section specified by InputSection does not match any of
282 the GUIDs registered with ExtractGuidedSectionRegisterHandlers().
283 @retval Others The return status from the handler associated with the GUID retrieved from
284 the section specified by InputSection.
289 ExtractGuidedSectionGetInfo (
290 IN CONST VOID
*InputSection
,
291 OUT UINT32
*OutputBufferSize
,
292 OUT UINT32
*ScratchBufferSize
,
293 OUT UINT16
*SectionAttribute
298 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
299 EFI_GUID
*SectionDefinitionGuid
;
302 // Check input parameter
304 ASSERT (InputSection
!= NULL
);
305 ASSERT (OutputBufferSize
!= NULL
);
306 ASSERT (ScratchBufferSize
!= NULL
);
307 ASSERT (SectionAttribute
!= NULL
);
310 // Get all registered handler information.
312 Status
= PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo
);
313 if (EFI_ERROR (Status
)) {
317 if (IS_SECTION2 (InputSection
)) {
318 SectionDefinitionGuid
= &(((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->SectionDefinitionGuid
);
320 SectionDefinitionGuid
= &(((EFI_GUID_DEFINED_SECTION
*) InputSection
)->SectionDefinitionGuid
);
324 // Search the match registered GetInfo handler for the input guided section.
326 ASSERT (HandlerInfo
!= NULL
);
327 for (Index
= 0; Index
< HandlerInfo
->NumberOfExtractHandler
; Index
++) {
328 if (CompareGuid (HandlerInfo
->ExtractHandlerGuidTable
+ Index
, SectionDefinitionGuid
)) {
330 // Call the match handler to get information for the input section data.
332 return HandlerInfo
->ExtractGetInfoHandlerTable
[Index
] (
342 // Not found, the input guided section is not supported.
344 return RETURN_UNSUPPORTED
;
348 Retrieves the GUID from a GUIDed section and uses that GUID to select an associated handler of type
349 EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().
350 The selected handler is used to decode the data in a GUIDed section and return the result in a caller
351 allocated output buffer.
353 Decodes the GUIDed section specified by InputSection.
354 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),
355 then RETURN_UNSUPPORTED is returned.
356 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler
357 of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()
358 is used to decode InputSection into the buffer specified by OutputBuffer and the authentication status of this
359 decode operation is returned in AuthenticationStatus. If the decoded buffer is identical to the data in InputSection,
360 then OutputBuffer is set to point at the data in InputSection. Otherwise, the decoded data will be placed in caller
361 allocated buffer specified by OutputBuffer. This function is responsible for computing the EFI_AUTH_STATUS_PLATFORM_OVERRIDE
362 bit of in AuthenticationStatus. The return status from the handler of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER is returned.
364 If InputSection is NULL, then ASSERT().
365 If OutputBuffer is NULL, then ASSERT().
366 If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().
367 If AuthenticationStatus is NULL, then ASSERT().
369 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
370 @param[out] OutputBuffer A pointer to a buffer that contains the result of a decode operation.
371 @param[in] ScratchBuffer A caller allocated buffer that may be required by this function as a scratch buffer to perform the decode operation.
372 @param[out] AuthenticationStatus
373 A pointer to the authentication status of the decoded output buffer. See the definition
374 of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI section of the PI
377 @retval RETURN_SUCCESS The buffer specified by InputSection was decoded.
378 @retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports.
379 @retval RETURN_INVALID_PARAMETER The section specified by InputSection can not be decoded.
384 ExtractGuidedSectionDecode (
385 IN CONST VOID
*InputSection
,
386 OUT VOID
**OutputBuffer
,
387 IN VOID
*ScratchBuffer
, OPTIONAL
388 OUT UINT32
*AuthenticationStatus
393 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
394 EFI_GUID
*SectionDefinitionGuid
;
397 // Check input parameter
399 ASSERT (InputSection
!= NULL
);
400 ASSERT (OutputBuffer
!= NULL
);
401 ASSERT (AuthenticationStatus
!= NULL
);
404 // Get all registered handler information.
406 Status
= PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo
);
407 if (EFI_ERROR (Status
)) {
411 if (IS_SECTION2 (InputSection
)) {
412 SectionDefinitionGuid
= &(((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->SectionDefinitionGuid
);
414 SectionDefinitionGuid
= &(((EFI_GUID_DEFINED_SECTION
*) InputSection
)->SectionDefinitionGuid
);
418 // Search the match registered Extract handler for the input guided section.
420 ASSERT (HandlerInfo
!= NULL
);
421 for (Index
= 0; Index
< HandlerInfo
->NumberOfExtractHandler
; Index
++) {
422 if (CompareGuid (HandlerInfo
->ExtractHandlerGuidTable
+ Index
, SectionDefinitionGuid
)) {
424 // Call the match handler to extract raw data for the input guided section.
426 return HandlerInfo
->ExtractDecodeHandlerTable
[Index
] (
436 // Not found, the input guided section is not supported.
438 return RETURN_UNSUPPORTED
;
442 Retrieves handlers of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER and
443 EXTRACT_GUIDED_SECTION_DECODE_HANDLER for a specific GUID section type.
445 Retrieves the handlers associated with SectionGuid and returns them in
446 GetInfoHandler and DecodeHandler.
448 If the GUID value specified by SectionGuid has not been registered, then
449 return RETURN_NOT_FOUND.
451 If SectionGuid is NULL, then ASSERT().
453 @param[in] SectionGuid A pointer to the GUID associated with the handlersof the GUIDed
454 section type being retrieved.
455 @param[out] GetInfoHandler Pointer to a function that examines a GUIDed section and returns
456 the size of the decoded buffer and the size of an optional scratch
457 buffer required to actually decode the data in a GUIDed section.
458 This is an optional parameter that may be NULL. If it is NULL, then
459 the previously registered handler is not returned.
460 @param[out] DecodeHandler Pointer to a function that decodes a GUIDed section into a caller
461 allocated output buffer. This is an optional parameter that may be NULL.
462 If it is NULL, then the previously registered handler is not returned.
464 @retval RETURN_SUCCESS The handlers were retrieved.
465 @retval RETURN_NOT_FOUND No handlers have been registered with the specified GUID.
470 ExtractGuidedSectionGetHandlers (
471 IN CONST GUID
*SectionGuid
,
472 OUT EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
*GetInfoHandler
, OPTIONAL
473 OUT EXTRACT_GUIDED_SECTION_DECODE_HANDLER
*DecodeHandler OPTIONAL
478 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
481 // Check input parameter
483 ASSERT (SectionGuid
!= NULL
);
486 // Get the registered handler information
488 Status
= PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo
);
489 if (EFI_ERROR (Status
)) {
494 // Search the match registered GetInfo handler for the input guided section.
496 ASSERT (HandlerInfo
!= NULL
);
497 for (Index
= 0; Index
< HandlerInfo
->NumberOfExtractHandler
; Index
++) {
498 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
];
506 if (DecodeHandler
!= NULL
) {
507 *DecodeHandler
= HandlerInfo
->ExtractDecodeHandlerTable
[Index
];
509 return RETURN_SUCCESS
;
512 return RETURN_NOT_FOUND
;