3 Copyright (c) 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 PeiExtractGuidedSectionLib.c
18 Provide generic extract guided section functions.
24 #include <Library/DebugLib.h>
25 #include <Library/PcdLib.h>
26 #include <Library/BaseMemoryLib.h>
27 #include <Library/HobLib.h>
28 #include <Library/ExtractGuidedSectionLib.h>
30 #define PEI_EXTRACT_HANDLER_INFO_SIGNATURE EFI_SIGNATURE_32 ('P', 'E', 'H', 'I')
34 UINT32 NumberOfExtractHandler
;
35 GUID
*ExtractHandlerGuidTable
;
36 EXTRACT_GUIDED_SECTION_DECODE_HANDLER
*ExtractDecodeHandlerTable
;
37 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
*ExtractGetInfoHandlerTable
;
38 } PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
;
41 Build guid hob for the global memory to store the registered guid and Handler list.
42 If GuidHob exists, HandlerInfo will be directly got from Guid hob data.
44 @param[in, out] InfoPointer Pointer to pei handler info structure.
46 @retval RETURN_SUCCESS Build Guid hob for the global memory space to store guid and funciton tables.
47 @retval RETURN_OUT_OF_RESOURCES No enough memory to allocated.
51 PeiGetExtractGuidedSectionHandlerInfo (
52 IN OUT PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
**InfoPointer
55 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
56 EFI_PEI_HOB_POINTERS Hob
;
59 // First try to get handler info from guid hob specified by CallerId.
61 Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION
, GetHobList ());
62 while (Hob
.Raw
!= NULL
) {
63 if (CompareGuid (&(Hob
.Guid
->Name
), &gEfiCallerIdGuid
)) {
64 HandlerInfo
= (PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
*) GET_GUID_HOB_DATA (Hob
.Guid
);
65 if (HandlerInfo
->Signature
== PEI_EXTRACT_HANDLER_INFO_SIGNATURE
) {
66 *InfoPointer
= HandlerInfo
;
70 Hob
.Raw
= GET_NEXT_HOB (Hob
);
71 Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION
, Hob
.Raw
);
75 // If Guid Hob is not found, Build CallerId Guid hob to store Handler Info
77 HandlerInfo
= BuildGuidHob (
79 sizeof (PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
) +
80 PcdGet32 (PcdMaximumGuidedExtractHandler
) *
81 (sizeof (GUID
) + sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER
) + sizeof (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
))
83 if (HandlerInfo
== NULL
) {
85 // No enough resource to build guid hob.
87 return EFI_OUT_OF_RESOURCES
;
89 HandlerInfo
->Signature
= PEI_EXTRACT_HANDLER_INFO_SIGNATURE
;
90 HandlerInfo
->NumberOfExtractHandler
= 0;
91 HandlerInfo
->ExtractHandlerGuidTable
= (GUID
*) (HandlerInfo
+ 1);
92 HandlerInfo
->ExtractDecodeHandlerTable
= (EXTRACT_GUIDED_SECTION_DECODE_HANDLER
*) (
93 (UINT8
*)HandlerInfo
->ExtractHandlerGuidTable
+
94 PcdGet32 (PcdMaximumGuidedExtractHandler
) * sizeof (GUID
)
96 HandlerInfo
->ExtractGetInfoHandlerTable
= (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
*) (
97 (UINT8
*)HandlerInfo
->ExtractDecodeHandlerTable
+
98 PcdGet32 (PcdMaximumGuidedExtractHandler
) *
99 sizeof (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
)
102 *InfoPointer
= HandlerInfo
;
107 Get the supported exract guided section Handler guid list.
108 If ExtractHandlerGuidTable = NULL, then ASSERT.
110 @param[in, out] ExtractHandlerGuidTable The extract Handler guid pointer list.
112 @retval return the number of the supported extract guided Handler.
116 ExtractGuidedSectionGetGuidList (
117 IN OUT GUID
**ExtractHandlerGuidTable
121 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
123 ASSERT (ExtractHandlerGuidTable
!= NULL
);
125 Status
= PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo
);
126 if (EFI_ERROR (Status
)) {
130 *ExtractHandlerGuidTable
= HandlerInfo
->ExtractHandlerGuidTable
;
131 return HandlerInfo
->NumberOfExtractHandler
;
135 Register Guided Section Extract and GetInfo handler.
137 @param[in] SectionGuid The guid matches this Extraction function.
138 @param[in] GetInfoHandler Function to get info from guided section.
139 @param[in] DecodeHandler Function to extract guided section.
141 @retval RETURN_SUCCESS Register Guided Section Extract function successfully.
142 @retval RETURN_OUT_OF_RESOURCES Resource is not enough to register new function.
143 @retval RETURN_INVALID_PARAMETER Input pointer to Guid value is not valid.
147 ExtractGuidedSectionRegisterHandlers (
148 IN CONST GUID
*SectionGuid
,
149 IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER GetInfoHandler
,
150 IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER DecodeHandler
155 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
158 // Check input paramter.
160 if (SectionGuid
== NULL
) {
161 return RETURN_INVALID_PARAMETER
;
165 // Get the registered handler information
167 Status
= PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo
);
168 if (EFI_ERROR (Status
)) {
173 // Search the match registered GetInfo handler for the input guided section.
175 for (Index
= 0; Index
< HandlerInfo
->NumberOfExtractHandler
; Index
++) {
176 if (CompareGuid (&(HandlerInfo
->ExtractHandlerGuidTable
[Index
]), SectionGuid
)) {
182 // If the guided handler has been registered before, only update its handler.
184 if (Index
< HandlerInfo
->NumberOfExtractHandler
) {
185 HandlerInfo
->ExtractDecodeHandlerTable
[Index
] = DecodeHandler
;
186 HandlerInfo
->ExtractGetInfoHandlerTable
[Index
] = GetInfoHandler
;
187 return RETURN_SUCCESS
;
191 // Check the global table is enough to contain new Handler.
193 if (HandlerInfo
->NumberOfExtractHandler
>= PcdGet32 (PcdMaximumGuidedExtractHandler
)) {
194 return RETURN_OUT_OF_RESOURCES
;
198 // Register new Handler and guid value.
200 CopyGuid (&(HandlerInfo
->ExtractHandlerGuidTable
[HandlerInfo
->NumberOfExtractHandler
]), SectionGuid
);
201 HandlerInfo
->ExtractDecodeHandlerTable
[HandlerInfo
->NumberOfExtractHandler
] = DecodeHandler
;
202 HandlerInfo
->ExtractGetInfoHandlerTable
[HandlerInfo
->NumberOfExtractHandler
++] = GetInfoHandler
;
204 return RETURN_SUCCESS
;
208 Get information from the guided section. This function first gets the guid value
209 from guided section header, then match this guid in the registered extract Handler list
210 to its corresponding getinfo Handler.
211 If not found, RETURN_INVALID_PARAMETER will be return.
212 If found, it will call the getinfo Handler to get the required size and attribute.
214 It will ASSERT () if the pointer to OutputBufferSize is NULL.
215 It will ASSERT () if the pointer to ScratchBufferSize is NULL.
216 It will ASSERT () if the pointer to SectionAttribute is NULL.
218 @param[in] InputSection Buffer containing the input GUIDed section to be processed.
219 @param[out] OutputBufferSize The size of OutputBuffer.
220 @param[out] ScratchBufferSize The size of ScratchBuffer.
221 @param[out] SectionAttribute The attribute of the input guided section.
223 @retval RETURN_SUCCESS Get the required information successfully.
224 @retval RETURN_INVALID_PARAMETER The input data can't be parsed correctly.
225 The GUID in InputSection does not match any registered guid list.
230 ExtractGuidedSectionGetInfo (
231 IN CONST VOID
*InputSection
,
232 OUT UINT32
*OutputBufferSize
,
233 OUT UINT32
*ScratchBufferSize
,
234 OUT UINT16
*SectionAttribute
239 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
242 // Check input paramter
244 if (InputSection
== NULL
) {
245 return RETURN_INVALID_PARAMETER
;
248 ASSERT (OutputBufferSize
!= NULL
);
249 ASSERT (ScratchBufferSize
!= NULL
);
250 ASSERT (SectionAttribute
!= NULL
);
253 // Get the registered handler information.
255 Status
= PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo
);
256 if (EFI_ERROR (Status
)) {
261 // Search the match registered GetInfo handler for the input guided section.
263 for (Index
= 0; Index
< HandlerInfo
->NumberOfExtractHandler
; Index
++) {
264 if (CompareGuid (&(HandlerInfo
->ExtractHandlerGuidTable
[Index
]), &(((EFI_GUID_DEFINED_SECTION
*) InputSection
)->SectionDefinitionGuid
))) {
270 // Not found, the input guided section is not supported.
272 if (Index
== HandlerInfo
->NumberOfExtractHandler
) {
273 return RETURN_INVALID_PARAMETER
;
277 // Call the match handler to getinfo for the input section data.
279 return HandlerInfo
->ExtractGetInfoHandlerTable
[Index
] (
288 Extract data from the guided section. This function first gets the guid value
289 from guided section header, then match this guid in the registered extract Handler list
290 to its corresponding extract Handler.
291 If not found, RETURN_INVALID_PARAMETER will be return.
292 If found, it will call this extract Handler to get output data and AuthenticationStatus.
294 It will ASSERT () if the pointer to OutputBuffer is NULL.
295 It will ASSERT () if the pointer to AuthenticationStatus is NULL.
297 @param[in] InputSection Buffer containing the input GUIDed section to be processed.
298 @param[out] OutputBuffer OutputBuffer to point the start of the section's contents
299 if guided data is not required prcessing. Otherwise,
300 OutputBuffer to contain the output data, which is
301 allocated by the caller.
302 @param[out] ScratchBuffer A pointer to a caller-allocated buffer for function internal use.
303 @param[out] AuthenticationStatus
304 A pointer to a caller-allocated UINT32 that indicates the
305 authentication status of the output buffer.
307 @retval RETURN_SUCCESS Get the output data, size and AuthenticationStatus successfully.
308 @retval RETURN_INVALID_PARAMETER The input data can't be parsed correctly.
309 The GUID in InputSection does not match any registered guid list.
314 ExtractGuidedSectionDecode (
315 IN CONST VOID
*InputSection
,
316 OUT VOID
**OutputBuffer
,
317 OUT VOID
*ScratchBuffer
, OPTIONAL
318 OUT UINT32
*AuthenticationStatus
323 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO
*HandlerInfo
;
325 if (InputSection
== NULL
) {
326 return RETURN_INVALID_PARAMETER
;
329 ASSERT (OutputBuffer
!= NULL
);
330 ASSERT (AuthenticationStatus
!= NULL
);
332 Status
= PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo
);
333 if (EFI_ERROR (Status
)) {
338 // Search the match registered GetInfo handler for the input guided section.
340 for (Index
= 0; Index
< HandlerInfo
->NumberOfExtractHandler
; Index
++) {
341 if (CompareGuid (&(HandlerInfo
->ExtractHandlerGuidTable
[Index
]), &(((EFI_GUID_DEFINED_SECTION
*) InputSection
)->SectionDefinitionGuid
))) {
347 // Not found, the input guided section is not supported.
349 if (Index
== HandlerInfo
->NumberOfExtractHandler
) {
350 return RETURN_INVALID_PARAMETER
;
354 // Call the match handler to getinfo for the input section data.
356 return HandlerInfo
->ExtractDecodeHandlerTable
[Index
] (