2 Provide generic extract guided section functions for PEI phase.
4 Copyright (c) 2007 - 2008, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Library/DebugLib.h>
18 #include <Library/PcdLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/HobLib.h>
21 #include <Library/ExtractGuidedSectionLib.h>
23 #define PEI_EXTRACT_HANDLER_INFO_SIGNATURE EFI_SIGNATURE_32 ('P', 'E', 'H', 'I')
27 UINT32 NumberOfExtractHandler
;
28 GUID
*ExtractHandlerGuidTable
;
29 EXTRACT_GUIDED_SECTION_DECODE_HANDLER
*ExtractDecodeHandlerTable
;
30 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
*ExtractGetInfoHandlerTable
;
31 } PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
;
34 Build guid hob for the global memory to store the registered guid and Handler list.
35 If GuidHob exists, HandlerInfo will be directly got from Guid hob data.
37 @param[in, out] InfoPointer Pointer to pei handler info structure.
39 @retval RETURN_SUCCESS Build Guid hob for the global memory space to store guid and funciton tables.
40 @retval RETURN_OUT_OF_RESOURCES No enough memory to allocated.
44 PeiGetExtractGuidedSectionHandlerInfo (
45 IN OUT PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
**InfoPointer
48 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
49 EFI_PEI_HOB_POINTERS Hob
;
52 // First try to get handler info from guid hob specified by CallerId.
54 Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION
, GetHobList ());
55 while (Hob
.Raw
!= NULL
) {
56 if (CompareGuid (&(Hob
.Guid
->Name
), &gEfiCallerIdGuid
)) {
57 HandlerInfo
= (PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
*) GET_GUID_HOB_DATA (Hob
.Guid
);
58 if (HandlerInfo
->Signature
== PEI_EXTRACT_HANDLER_INFO_SIGNATURE
) {
60 // Update Table Pointer when hob start address is changed.
62 if (HandlerInfo
->ExtractHandlerGuidTable
!= (GUID
*) (HandlerInfo
+ 1)) {
63 HandlerInfo
->ExtractHandlerGuidTable
= (GUID
*) (HandlerInfo
+ 1);
64 HandlerInfo
->ExtractDecodeHandlerTable
= (EXTRACT_GUIDED_SECTION_DECODE_HANDLER
*) (
65 (UINT8
*)HandlerInfo
->ExtractHandlerGuidTable
+
66 PcdGet32 (PcdMaximumGuidedExtractHandler
) * sizeof (GUID
)
68 HandlerInfo
->ExtractGetInfoHandlerTable
= (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
*) (
69 (UINT8
*)HandlerInfo
->ExtractDecodeHandlerTable
+
70 PcdGet32 (PcdMaximumGuidedExtractHandler
) *
71 sizeof (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
)
75 // Return HandlerInfo pointer.
77 *InfoPointer
= HandlerInfo
;
81 Hob
.Raw
= GET_NEXT_HOB (Hob
);
82 Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION
, Hob
.Raw
);
86 // If Guid Hob is not found, Build CallerId Guid hob to store Handler Info
88 HandlerInfo
= BuildGuidHob (
90 sizeof (PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
) +
91 PcdGet32 (PcdMaximumGuidedExtractHandler
) *
92 (sizeof (GUID
) + sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER
) + sizeof (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
))
94 if (HandlerInfo
== NULL
) {
96 // No enough resource to build guid hob.
98 return EFI_OUT_OF_RESOURCES
;
101 // Init HandlerInfo structure
103 HandlerInfo
->Signature
= PEI_EXTRACT_HANDLER_INFO_SIGNATURE
;
104 HandlerInfo
->NumberOfExtractHandler
= 0;
105 HandlerInfo
->ExtractHandlerGuidTable
= (GUID
*) (HandlerInfo
+ 1);
106 HandlerInfo
->ExtractDecodeHandlerTable
= (EXTRACT_GUIDED_SECTION_DECODE_HANDLER
*) (
107 (UINT8
*)HandlerInfo
->ExtractHandlerGuidTable
+
108 PcdGet32 (PcdMaximumGuidedExtractHandler
) * sizeof (GUID
)
110 HandlerInfo
->ExtractGetInfoHandlerTable
= (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
*) (
111 (UINT8
*)HandlerInfo
->ExtractDecodeHandlerTable
+
112 PcdGet32 (PcdMaximumGuidedExtractHandler
) *
113 sizeof (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
)
116 // return the created HandlerInfo.
118 *InfoPointer
= HandlerInfo
;
123 Retrieve the list GUIDs that have been registered through ExtractGuidedSectionRegisterHandlers().
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().
130 @param[out] ExtractHandlerGuidTable A pointer to the array of GUIDs tht have been registerd through
131 ExtractGuidedSectionRegisterHandlers().
133 @return the number of the supported extract guided Handler.
138 ExtractGuidedSectionGetGuidList (
139 OUT GUID
**ExtractHandlerGuidTable
143 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
145 ASSERT (ExtractHandlerGuidTable
!= NULL
);
148 // Get all registered handler information
150 Status
= PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo
);
151 if (EFI_ERROR (Status
)) {
156 // Get GuidTable and Table Number
158 *ExtractHandlerGuidTable
= HandlerInfo
->ExtractHandlerGuidTable
;
159 return HandlerInfo
->NumberOfExtractHandler
;
163 Registers handlers of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER and EXTRACT_GUIDED_SECTION_DECODE_HANDLER
164 for a specific GUID section type.
166 Registers the handlers specified by GetInfoHandler and DecodeHandler witg the GUID specified by SectionGuid.
167 If the GUID value specified by SectionGuid has already been registered, then return RETURN_ALREADY_STARTED.
168 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 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 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_ALREADY_STARTED Handlers have already been registered for the GUID specified by SectionGuid.
183 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to register the handlers.
188 ExtractGuidedSectionRegisterHandlers (
189 IN CONST GUID
*SectionGuid
,
190 IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER GetInfoHandler
,
191 IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER DecodeHandler
196 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
199 // Check input paramter
201 ASSERT (SectionGuid
!= NULL
);
202 ASSERT (GetInfoHandler
!= NULL
);
203 ASSERT (DecodeHandler
!= NULL
);
208 // Get the registered handler information
210 Status
= PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo
);
211 if (EFI_ERROR (Status
)) {
216 // Search the match registered GetInfo handler for the input guided section.
218 for (Index
= 0; Index
< HandlerInfo
->NumberOfExtractHandler
; Index
++) {
219 if (CompareGuid (HandlerInfo
->ExtractHandlerGuidTable
+ Index
, SectionGuid
)) {
221 // If the guided handler has been registered before, only update its handler.
223 HandlerInfo
->ExtractDecodeHandlerTable
[Index
] = DecodeHandler
;
224 HandlerInfo
->ExtractGetInfoHandlerTable
[Index
] = GetInfoHandler
;
225 return RETURN_SUCCESS
;
230 // Check the global table is enough to contain new Handler.
232 if (HandlerInfo
->NumberOfExtractHandler
>= PcdGet32 (PcdMaximumGuidedExtractHandler
)) {
233 return RETURN_OUT_OF_RESOURCES
;
237 // Register new Handler and guid value.
239 CopyGuid (HandlerInfo
->ExtractHandlerGuidTable
+ HandlerInfo
->NumberOfExtractHandler
, SectionGuid
);
240 HandlerInfo
->ExtractDecodeHandlerTable
[HandlerInfo
->NumberOfExtractHandler
] = DecodeHandler
;
241 HandlerInfo
->ExtractGetInfoHandlerTable
[HandlerInfo
->NumberOfExtractHandler
++] = GetInfoHandler
;
243 return RETURN_SUCCESS
;
247 Retrives a GUID from a GUIDed section and uses that GUID to select an associated handler of type
248 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().
249 The selected handler is used to retrieve and return the size of the decoded buffer and the size of an
250 optional scratch buffer required to actually decode the data in a GUIDed section.
252 Examines a GUIDed section specified by InputSection.
253 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),
254 then RETURN_UNSUPPORTED is returned.
255 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler
256 of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()
257 is used to retrieve the OututBufferSize, ScratchSize, and Attributes values. The return status from the handler of
258 type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER is returned.
259 If InputSection is NULL, then ASSERT().
260 If OutputBufferSize is NULL, then ASSERT().
261 If ScratchBufferSize is NULL, then ASSERT().
262 If SectionAttribute is NULL, then ASSERT().
264 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
265 @param[out] OutputBufferSize A pointer to the size, in bytes, of an output buffer required if the buffer
266 specified by InputSection were decoded.
267 @param[out] ScratchBufferSize A pointer to the size, in bytes, required as scratch space if the buffer specified by
268 InputSection were decoded.
269 @param[out] SectionAttribute A pointer to the attributes of the GUIDed section. See the Attributes field of
270 EFI_GUID_DEFINED_SECTION in the PI Specification.
272 @retval RETURN_SUCCESS Get the required information successfully.
273 @retval RETURN_UNSUPPORTED The GUID from the section specified by InputSection does not match any of
274 the GUIDs registered with ExtractGuidedSectionRegisterHandlers().
275 @retval Others The return status from the handler associated with the GUID retrieved from
276 the section specified by InputSection.
281 ExtractGuidedSectionGetInfo (
282 IN CONST VOID
*InputSection
,
283 OUT UINT32
*OutputBufferSize
,
284 OUT UINT32
*ScratchBufferSize
,
285 OUT UINT16
*SectionAttribute
290 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
293 // Check input paramter
295 ASSERT (InputSection
!= NULL
);
296 ASSERT (OutputBufferSize
!= NULL
);
297 ASSERT (ScratchBufferSize
!= NULL
);
298 ASSERT (SectionAttribute
!= NULL
);
301 // Get all registered handler information.
303 Status
= PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo
);
304 if (EFI_ERROR (Status
)) {
309 // Search the match registered GetInfo handler for the input guided section.
311 for (Index
= 0; Index
< HandlerInfo
->NumberOfExtractHandler
; Index
++) {
312 if (CompareGuid (HandlerInfo
->ExtractHandlerGuidTable
+ Index
, &(((EFI_GUID_DEFINED_SECTION
*) InputSection
)->SectionDefinitionGuid
))) {
314 // Call the match handler to getinfo for the input section data.
316 return HandlerInfo
->ExtractGetInfoHandlerTable
[Index
] (
326 // Not found, the input guided section is not supported.
328 return RETURN_UNSUPPORTED
;
332 Retrives the GUID from a GUIDed section and uses that GUID to select an associated handler of type
333 EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().
334 The selected handler is used to decode the data in a GUIDed section and return the result in a caller
335 allocated output buffer.
337 Decodes the GUIDed section specified by InputSection.
338 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),
339 then RETURN_UNSUPPORTED is returned.
340 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler
341 of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()
342 is used to decode InputSection into the buffer specified by OutputBuffer and the authentication status of this
343 decode operation is returned in AuthenticationStatus. If the decoded buffer is identical to the data in InputSection,
344 then OutputBuffer is set to point at the data in InputSection. Otherwise, the decoded data will be placed in caller
345 allocated buffer specified by OutputBuffer. This function is responsible for computing the EFI_AUTH_STATUS_PLATFORM_OVERRIDE
346 bit of in AuthenticationStatus. The return status from the handler of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER is returned.
347 If InputSection is NULL, then ASSERT().
348 If OutputBuffer is NULL, then ASSERT().
349 If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().
350 If AuthenticationStatus is NULL, then ASSERT().
352 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
353 @param[out] OutputBuffer A pointer to a buffer that contains the result of a decode operation.
354 @param[in] ScratchBuffer A caller allocated buffer that may be required by this function as a scratch buffer to perform the decode operation.
355 @param[out] AuthenticationStatus
356 A pointer to the authentication status of the decoded output buffer. See the definition
357 of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI section of the PI
360 @retval RETURN_SUCCESS The buffer specified by InputSection was decoded.
361 @retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports.
362 @retval RETURN_INVALID_PARAMETER The section specified by InputSection can not be decoded.
367 ExtractGuidedSectionDecode (
368 IN CONST VOID
*InputSection
,
369 OUT VOID
**OutputBuffer
,
370 IN VOID
*ScratchBuffer
, OPTIONAL
371 OUT UINT32
*AuthenticationStatus
376 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
379 // Check input parameter
381 ASSERT (InputSection
!= NULL
);
382 ASSERT (OutputBuffer
!= NULL
);
383 ASSERT (AuthenticationStatus
!= NULL
);
386 // Get all registered handler information.
388 Status
= PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo
);
389 if (EFI_ERROR (Status
)) {
394 // Search the match registered Extract handler for the input guided section.
396 for (Index
= 0; Index
< HandlerInfo
->NumberOfExtractHandler
; Index
++) {
397 if (CompareGuid (HandlerInfo
->ExtractHandlerGuidTable
+ Index
, &(((EFI_GUID_DEFINED_SECTION
*) InputSection
)->SectionDefinitionGuid
))) {
399 // Call the match handler to extract raw data for the input guided section.
401 return HandlerInfo
->ExtractDecodeHandlerTable
[Index
] (
411 // Not found, the input guided section is not supported.
413 return RETURN_UNSUPPORTED
;