2 Provide generic extract guided section functions.
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/ExtractGuidedSectionLib.h>
16 #define EXTRACT_HANDLER_INFO_SIGNATURE SIGNATURE_32 ('E', 'G', 'S', 'I')
20 UINT32 NumberOfExtractHandler
;
21 GUID
*ExtractHandlerGuidTable
;
22 EXTRACT_GUIDED_SECTION_DECODE_HANDLER
*ExtractDecodeHandlerTable
;
23 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
*ExtractGetInfoHandlerTable
;
24 } EXTRACT_GUIDED_SECTION_HANDLER_INFO
;
27 HandlerInfo table address is set by PcdGuidedExtractHandlerTableAddress, which is used to store
28 the registered guid and Handler list. When it is initialized, it will be directly returned.
29 Or, HandlerInfo table will be initialized in this function.
31 @param[in, out] InfoPointer The pointer to the handler information structure.
33 @retval RETURN_SUCCESS HandlerInfo table can be used to store guid and function tables.
34 @retval RETURN_OUT_OF_RESOURCES HandlerInfo table address is not writable.
37 GetExtractGuidedSectionHandlerInfo (
38 IN OUT EXTRACT_GUIDED_SECTION_HANDLER_INFO
**InfoPointer
41 EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
44 // Set the available memory address to handler info.
46 HandlerInfo
= (EXTRACT_GUIDED_SECTION_HANDLER_INFO
*)(VOID
*)(UINTN
)PcdGet64 (PcdGuidedExtractHandlerTableAddress
);
47 if (HandlerInfo
== NULL
) {
49 return EFI_OUT_OF_RESOURCES
;
53 // First check whether the handler information structure is initialized.
55 if (HandlerInfo
->Signature
== EXTRACT_HANDLER_INFO_SIGNATURE
) {
57 // The handler information has been initialized and is returned.
59 *InfoPointer
= HandlerInfo
;
60 return RETURN_SUCCESS
;
64 // Try to initialize the handler information structure
66 HandlerInfo
->Signature
= EXTRACT_HANDLER_INFO_SIGNATURE
;
67 if (HandlerInfo
->Signature
!= EXTRACT_HANDLER_INFO_SIGNATURE
) {
69 // The handler information structure was not writeable because the memory is not ready.
72 return RETURN_OUT_OF_RESOURCES
;
76 // Init HandlerInfo structure
78 HandlerInfo
->NumberOfExtractHandler
= 0;
79 HandlerInfo
->ExtractHandlerGuidTable
= (GUID
*)(HandlerInfo
+ 1);
80 HandlerInfo
->ExtractDecodeHandlerTable
= (EXTRACT_GUIDED_SECTION_DECODE_HANDLER
*)(
81 (UINT8
*)HandlerInfo
->ExtractHandlerGuidTable
+
82 PcdGet32 (PcdMaximumGuidedExtractHandler
) * sizeof (GUID
)
84 HandlerInfo
->ExtractGetInfoHandlerTable
= (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
*)(
85 (UINT8
*)HandlerInfo
->ExtractDecodeHandlerTable
+
86 PcdGet32 (PcdMaximumGuidedExtractHandler
) *
87 sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER
)
89 *InfoPointer
= HandlerInfo
;
90 return RETURN_SUCCESS
;
94 Retrieve the list GUIDs that have been registered through ExtractGuidedSectionRegisterHandlers().
96 Sets ExtractHandlerGuidTable so it points at a callee allocated array of registered GUIDs.
97 The total number of GUIDs in the array are returned. Since the array of GUIDs is callee allocated
98 and caller must treat this array of GUIDs as read-only data.
99 If ExtractHandlerGuidTable is NULL, then ASSERT().
101 @param[out] ExtractHandlerGuidTable A pointer to the array of GUIDs that have been registered through
102 ExtractGuidedSectionRegisterHandlers().
104 @return The number of the supported extract guided Handler.
109 ExtractGuidedSectionGetGuidList (
110 OUT GUID
**ExtractHandlerGuidTable
113 RETURN_STATUS Status
;
114 EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
116 ASSERT (ExtractHandlerGuidTable
!= NULL
);
119 // Get all registered handler information
121 Status
= GetExtractGuidedSectionHandlerInfo (&HandlerInfo
);
122 if (RETURN_ERROR (Status
)) {
123 *ExtractHandlerGuidTable
= NULL
;
128 // Get GuidTable and Table Number
130 ASSERT (HandlerInfo
!= NULL
);
131 *ExtractHandlerGuidTable
= HandlerInfo
->ExtractHandlerGuidTable
;
132 return HandlerInfo
->NumberOfExtractHandler
;
136 Registers handlers of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER and EXTRACT_GUIDED_SECTION_DECODE_HANDLER
137 for a specific GUID section type.
139 Registers the handlers specified by GetInfoHandler and DecodeHandler with the GUID specified by SectionGuid.
140 If the GUID value specified by SectionGuid has already been registered, then return RETURN_ALREADY_STARTED.
141 If there are not enough resources available to register the handlers then RETURN_OUT_OF_RESOURCES is returned.
143 If SectionGuid is NULL, then ASSERT().
144 If GetInfoHandler is NULL, then ASSERT().
145 If DecodeHandler is NULL, then ASSERT().
147 @param[in] SectionGuid A pointer to the GUID associated with the the handlers
148 of the GUIDed section type being registered.
149 @param[in] GetInfoHandler The pointer to a function that examines a GUIDed section and returns the
150 size of the decoded buffer and the size of an optional scratch buffer
151 required to actually decode the data in a GUIDed section.
152 @param[in] DecodeHandler The pointer to a function that decodes a GUIDed section into a caller
153 allocated output buffer.
155 @retval RETURN_SUCCESS The handlers were registered.
156 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to register the handlers.
161 ExtractGuidedSectionRegisterHandlers (
162 IN CONST GUID
*SectionGuid
,
163 IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER GetInfoHandler
,
164 IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER DecodeHandler
168 RETURN_STATUS Status
;
169 EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
172 // Check input parameter
174 ASSERT (SectionGuid
!= NULL
);
175 ASSERT (GetInfoHandler
!= NULL
);
176 ASSERT (DecodeHandler
!= NULL
);
179 // Get the registered handler information
181 Status
= GetExtractGuidedSectionHandlerInfo (&HandlerInfo
);
182 if (RETURN_ERROR (Status
)) {
187 // Search the match registered GetInfo handler for the input guided section.
189 ASSERT (HandlerInfo
!= NULL
);
190 for (Index
= 0; Index
< HandlerInfo
->NumberOfExtractHandler
; Index
++) {
191 if (CompareGuid (HandlerInfo
->ExtractHandlerGuidTable
+ Index
, SectionGuid
)) {
193 // If the guided handler has been registered before, only update its handler.
195 HandlerInfo
->ExtractDecodeHandlerTable
[Index
] = DecodeHandler
;
196 HandlerInfo
->ExtractGetInfoHandlerTable
[Index
] = GetInfoHandler
;
197 return RETURN_SUCCESS
;
202 // Check the global table is enough to contain new Handler.
204 if (HandlerInfo
->NumberOfExtractHandler
>= PcdGet32 (PcdMaximumGuidedExtractHandler
)) {
205 return RETURN_OUT_OF_RESOURCES
;
209 // Register new Handler and guid value.
211 CopyGuid (HandlerInfo
->ExtractHandlerGuidTable
+ HandlerInfo
->NumberOfExtractHandler
, SectionGuid
);
212 HandlerInfo
->ExtractDecodeHandlerTable
[HandlerInfo
->NumberOfExtractHandler
] = DecodeHandler
;
213 HandlerInfo
->ExtractGetInfoHandlerTable
[HandlerInfo
->NumberOfExtractHandler
++] = GetInfoHandler
;
215 return RETURN_SUCCESS
;
219 Retrieves a GUID from a GUIDed section and uses that GUID to select an associated handler of type
220 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().
221 The selected handler is used to retrieve and return the size of the decoded buffer and the size of an
222 optional scratch buffer required to actually decode the data in a GUIDed section.
224 Examines a GUIDed section specified by InputSection.
225 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),
226 then RETURN_UNSUPPORTED is returned.
227 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler
228 of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()
229 is used to retrieve the OututBufferSize, ScratchSize, and Attributes values. The return status from the handler of
230 type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER is returned.
232 If InputSection is NULL, then ASSERT().
233 If OutputBufferSize is NULL, then ASSERT().
234 If ScratchBufferSize is NULL, then ASSERT().
235 If SectionAttribute is NULL, then ASSERT().
237 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
238 @param[out] OutputBufferSize A pointer to the size, in bytes, of an output buffer required if the buffer
239 specified by InputSection were decoded.
240 @param[out] ScratchBufferSize A pointer to the size, in bytes, required as scratch space if the buffer specified by
241 InputSection were decoded.
242 @param[out] SectionAttribute A pointer to the attributes of the GUIDed section. See the Attributes field of
243 EFI_GUID_DEFINED_SECTION in the PI Specification.
245 @retval RETURN_SUCCESS Successfully retrieved the required information.
246 @retval RETURN_UNSUPPORTED The GUID from the section specified by InputSection does not match any of
247 the GUIDs registered with ExtractGuidedSectionRegisterHandlers().
248 @retval Others The return status from the handler associated with the GUID retrieved from
249 the section specified by InputSection.
254 ExtractGuidedSectionGetInfo (
255 IN CONST VOID
*InputSection
,
256 OUT UINT32
*OutputBufferSize
,
257 OUT UINT32
*ScratchBufferSize
,
258 OUT UINT16
*SectionAttribute
262 RETURN_STATUS Status
;
263 EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
264 EFI_GUID
*SectionDefinitionGuid
;
267 // Check input parameter
269 ASSERT (InputSection
!= NULL
);
270 ASSERT (OutputBufferSize
!= NULL
);
271 ASSERT (ScratchBufferSize
!= NULL
);
272 ASSERT (SectionAttribute
!= NULL
);
275 // Get all registered handler information.
277 Status
= GetExtractGuidedSectionHandlerInfo (&HandlerInfo
);
278 if (RETURN_ERROR (Status
)) {
282 if (IS_SECTION2 (InputSection
)) {
283 SectionDefinitionGuid
= &(((EFI_GUID_DEFINED_SECTION2
*)InputSection
)->SectionDefinitionGuid
);
285 SectionDefinitionGuid
= &(((EFI_GUID_DEFINED_SECTION
*)InputSection
)->SectionDefinitionGuid
);
289 // Search the match registered GetInfo handler for the input guided section.
291 ASSERT (HandlerInfo
!= NULL
);
292 for (Index
= 0; Index
< HandlerInfo
->NumberOfExtractHandler
; Index
++) {
293 if (CompareGuid (HandlerInfo
->ExtractHandlerGuidTable
+ Index
, SectionDefinitionGuid
)) {
295 // Call the match handler to get information for the input section data.
297 return HandlerInfo
->ExtractGetInfoHandlerTable
[Index
](
307 // Not found, the input guided section is not supported.
309 return RETURN_UNSUPPORTED
;
313 Retrieves the GUID from a GUIDed section and uses that GUID to select an associated handler of type
314 EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().
315 The selected handler is used to decode the data in a GUIDed section and return the result in a caller
316 allocated output buffer.
318 Decodes the GUIDed section specified by InputSection.
319 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),
320 then RETURN_UNSUPPORTED is returned.
321 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler
322 of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()
323 is used to decode InputSection into the buffer specified by OutputBuffer and the authentication status of this
324 decode operation is returned in AuthenticationStatus. If the decoded buffer is identical to the data in InputSection,
325 then OutputBuffer is set to point at the data in InputSection. Otherwise, the decoded data will be placed in a caller
326 allocated buffer specified by OutputBuffer. This function is responsible for computing the EFI_AUTH_STATUS_PLATFORM_OVERRIDE
327 bit of in AuthenticationStatus. The return status from the handler of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER is returned.
329 If InputSection is NULL, then ASSERT().
330 If OutputBuffer is NULL, then ASSERT().
331 If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().
332 If AuthenticationStatus is NULL, then ASSERT().
334 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
335 @param[out] OutputBuffer A pointer to a buffer that contains the result of a decode operation.
336 @param[in] ScratchBuffer A caller allocated buffer that may be required by this function as a scratch buffer to perform the decode operation.
337 @param[out] AuthenticationStatus
338 A pointer to the authentication status of the decoded output buffer. See the definition
339 of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI section of the PI
342 @retval RETURN_SUCCESS The buffer specified by InputSection was decoded.
343 @retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports.
344 @retval RETURN_INVALID_PARAMETER The section specified by InputSection can not be decoded.
349 ExtractGuidedSectionDecode (
350 IN CONST VOID
*InputSection
,
351 OUT VOID
**OutputBuffer
,
352 IN VOID
*ScratchBuffer OPTIONAL
,
353 OUT UINT32
*AuthenticationStatus
357 RETURN_STATUS Status
;
358 EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
359 EFI_GUID
*SectionDefinitionGuid
;
362 // Check input parameter
364 ASSERT (InputSection
!= NULL
);
365 ASSERT (OutputBuffer
!= NULL
);
366 ASSERT (AuthenticationStatus
!= NULL
);
369 // Get all registered handler information.
371 Status
= GetExtractGuidedSectionHandlerInfo (&HandlerInfo
);
372 if (RETURN_ERROR (Status
)) {
376 if (IS_SECTION2 (InputSection
)) {
377 SectionDefinitionGuid
= &(((EFI_GUID_DEFINED_SECTION2
*)InputSection
)->SectionDefinitionGuid
);
379 SectionDefinitionGuid
= &(((EFI_GUID_DEFINED_SECTION
*)InputSection
)->SectionDefinitionGuid
);
383 // Search the match registered Extract handler for the input guided section.
385 ASSERT (HandlerInfo
!= NULL
);
386 for (Index
= 0; Index
< HandlerInfo
->NumberOfExtractHandler
; Index
++) {
387 if (CompareGuid (HandlerInfo
->ExtractHandlerGuidTable
+ Index
, SectionDefinitionGuid
)) {
389 // Call the match handler to extract raw data for the input guided section.
391 return HandlerInfo
->ExtractDecodeHandlerTable
[Index
](
401 // Not found, the input guided section is not supported.
403 return RETURN_UNSUPPORTED
;
407 Retrieves handlers of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER and
408 EXTRACT_GUIDED_SECTION_DECODE_HANDLER for a specific GUID section type.
410 Retrieves the handlers associated with SectionGuid and returns them in
411 GetInfoHandler and DecodeHandler.
413 If the GUID value specified by SectionGuid has not been registered, then
414 return RETURN_NOT_FOUND.
416 If SectionGuid is NULL, then ASSERT().
418 @param[in] SectionGuid A pointer to the GUID associated with the handlersof the GUIDed
419 section type being retrieved.
420 @param[out] GetInfoHandler Pointer to a function that examines a GUIDed section and returns
421 the size of the decoded buffer and the size of an optional scratch
422 buffer required to actually decode the data in a GUIDed section.
423 This is an optional parameter that may be NULL. If it is NULL, then
424 the previously registered handler is not returned.
425 @param[out] DecodeHandler Pointer to a function that decodes a GUIDed section into a caller
426 allocated output buffer. This is an optional parameter that may be NULL.
427 If it is NULL, then the previously registered handler is not returned.
429 @retval RETURN_SUCCESS The handlers were retrieved.
430 @retval RETURN_NOT_FOUND No handlers have been registered with the specified GUID.
435 ExtractGuidedSectionGetHandlers (
436 IN CONST GUID
*SectionGuid
,
437 OUT EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
*GetInfoHandler OPTIONAL
,
438 OUT EXTRACT_GUIDED_SECTION_DECODE_HANDLER
*DecodeHandler OPTIONAL
442 RETURN_STATUS Status
;
443 EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
446 // Check input parameter
448 ASSERT (SectionGuid
!= NULL
);
451 // Get the registered handler information
453 Status
= GetExtractGuidedSectionHandlerInfo (&HandlerInfo
);
454 if (RETURN_ERROR (Status
)) {
459 // Search the match registered GetInfo handler for the input guided section.
461 ASSERT (HandlerInfo
!= NULL
);
462 for (Index
= 0; Index
< HandlerInfo
->NumberOfExtractHandler
; Index
++) {
463 if (CompareGuid (HandlerInfo
->ExtractHandlerGuidTable
+ Index
, SectionGuid
)) {
465 // If the guided handler has been registered before, then return the registered handlers.
467 if (GetInfoHandler
!= NULL
) {
468 *GetInfoHandler
= HandlerInfo
->ExtractGetInfoHandlerTable
[Index
];
471 if (DecodeHandler
!= NULL
) {
472 *DecodeHandler
= HandlerInfo
->ExtractDecodeHandlerTable
[Index
];
475 return RETURN_SUCCESS
;
479 return RETURN_NOT_FOUND
;