2 Provide generic extract guided section functions for Dxe phase.
4 Copyright (c) 2007 - 2012, Intel Corporation. All rights reserved.<BR>
5 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/BaseMemoryLib.h>
19 #include <Library/MemoryAllocationLib.h>
20 #include <Library/ExtractGuidedSectionLib.h>
21 #include <Library/UefiBootServicesTableLib.h>
23 #define EXTRACT_HANDLER_TABLE_SIZE 0x10
25 UINT32 mNumberOfExtractHandler
= 0;
26 UINT32 mMaxNumberOfExtractHandler
= 0;
28 GUID
*mExtractHandlerGuidTable
= NULL
;
29 EXTRACT_GUIDED_SECTION_DECODE_HANDLER
*mExtractDecodeHandlerTable
= NULL
;
30 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
*mExtractGetInfoHandlerTable
= NULL
;
33 Reallocates more global memory to store the registered guid and Handler list.
35 @retval RETURN_SUCCESS Reallocated more global memory space to store guid and function tables.
36 @retval RETURN_OUT_OF_RESOURCES Not enough memory to allocate.
40 ReallocateExtractHandlerTable (
44 // Reallocate memory for GuidTable
46 mExtractHandlerGuidTable
= ReallocatePool (
47 mMaxNumberOfExtractHandler
* sizeof (GUID
),
48 (mMaxNumberOfExtractHandler
+ EXTRACT_HANDLER_TABLE_SIZE
) * sizeof (GUID
),
49 mExtractHandlerGuidTable
52 if (mExtractHandlerGuidTable
== NULL
) {
57 // Reallocate memory for Decode handler Table
59 mExtractDecodeHandlerTable
= ReallocatePool (
60 mMaxNumberOfExtractHandler
* sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER
),
61 (mMaxNumberOfExtractHandler
+ EXTRACT_HANDLER_TABLE_SIZE
) * sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER
),
62 mExtractDecodeHandlerTable
65 if (mExtractDecodeHandlerTable
== NULL
) {
70 // Reallocate memory for GetInfo handler Table
72 mExtractGetInfoHandlerTable
= ReallocatePool (
73 mMaxNumberOfExtractHandler
* sizeof (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
),
74 (mMaxNumberOfExtractHandler
+ EXTRACT_HANDLER_TABLE_SIZE
) * sizeof (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
),
75 mExtractGetInfoHandlerTable
78 if (mExtractGetInfoHandlerTable
== NULL
) {
83 // Increase max handler number
85 mMaxNumberOfExtractHandler
= mMaxNumberOfExtractHandler
+ EXTRACT_HANDLER_TABLE_SIZE
;
86 return RETURN_SUCCESS
;
89 if (mExtractHandlerGuidTable
!= NULL
) {
90 FreePool (mExtractHandlerGuidTable
);
92 if (mExtractDecodeHandlerTable
!= NULL
) {
93 FreePool (mExtractDecodeHandlerTable
);
95 if (mExtractGetInfoHandlerTable
!= NULL
) {
96 FreePool (mExtractGetInfoHandlerTable
);
99 return RETURN_OUT_OF_RESOURCES
;
102 Constructor allocates the global memory to store the registered guid and Handler list.
104 @param ImageHandle The firmware allocated handle for the EFI image.
105 @param SystemTable A pointer to the EFI System Table.
107 @retval RETURN_SUCCESS Allocated the global memory space to store guid and function tables.
108 @retval RETURN_OUT_OF_RESOURCES Not enough memory to allocate.
112 DxeExtractGuidedSectionLibConstructor (
113 IN EFI_HANDLE ImageHandle
,
114 IN EFI_SYSTEM_TABLE
*SystemTable
117 return ReallocateExtractHandlerTable ();
121 Retrieve the list GUIDs that have been registered through ExtractGuidedSectionRegisterHandlers().
123 Sets ExtractHandlerGuidTable so it points at a callee allocated array of registered GUIDs.
124 The total number of GUIDs in the array are returned. Since the array of GUIDs is callee allocated
125 and caller must treat this array of GUIDs as read-only data.
126 If ExtractHandlerGuidTable is NULL, then ASSERT().
128 @param[out] ExtractHandlerGuidTable A pointer to the array of GUIDs that have been registered through
129 ExtractGuidedSectionRegisterHandlers().
131 @return The number of the supported extract guided Handler.
136 ExtractGuidedSectionGetGuidList (
137 OUT GUID
**ExtractHandlerGuidTable
140 ASSERT (ExtractHandlerGuidTable
!= NULL
);
142 *ExtractHandlerGuidTable
= mExtractHandlerGuidTable
;
143 return mNumberOfExtractHandler
;
147 Registers handlers of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER and EXTRACT_GUIDED_SECTION_DECODE_HANDLER
148 for a specific GUID section type.
150 Registers the handlers specified by GetInfoHandler and DecodeHandler with the GUID specified by SectionGuid.
151 If the GUID value specified by SectionGuid has already been registered, then return RETURN_ALREADY_STARTED.
152 If there are not enough resources available to register the handlers then RETURN_OUT_OF_RESOURCES is returned.
154 If SectionGuid is NULL, then ASSERT().
155 If GetInfoHandler is NULL, then ASSERT().
156 If DecodeHandler is NULL, then ASSERT().
158 @param[in] SectionGuid A pointer to the GUID associated with the the handlers
159 of the GUIDed section type being registered.
160 @param[in] GetInfoHandler The pointer to a function that examines a GUIDed section and returns the
161 size of the decoded buffer and the size of an optional scratch buffer
162 required to actually decode the data in a GUIDed section.
163 @param[in] DecodeHandler The pointer to a function that decodes a GUIDed section into a caller
164 allocated output buffer.
166 @retval RETURN_SUCCESS The handlers were registered.
167 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to register the handlers.
172 ExtractGuidedSectionRegisterHandlers (
173 IN CONST GUID
*SectionGuid
,
174 IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER GetInfoHandler
,
175 IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER DecodeHandler
182 // Check input paramter.
184 ASSERT (SectionGuid
!= NULL
);
185 ASSERT (GetInfoHandler
!= NULL
);
186 ASSERT (DecodeHandler
!= NULL
);
189 // Search the match registered GetInfo handler for the input guided section.
191 for (Index
= 0; Index
< mNumberOfExtractHandler
; Index
++) {
192 if (CompareGuid (&mExtractHandlerGuidTable
[Index
], SectionGuid
)) {
194 // If the guided handler has been registered before, only update its handler.
196 mExtractDecodeHandlerTable
[Index
] = DecodeHandler
;
197 mExtractGetInfoHandlerTable
[Index
] = GetInfoHandler
;
198 return RETURN_SUCCESS
;
203 // Check the global table is enough to contain new Handler.
205 if (mNumberOfExtractHandler
>= mMaxNumberOfExtractHandler
) {
206 if (ReallocateExtractHandlerTable () != RETURN_SUCCESS
) {
207 return RETURN_OUT_OF_RESOURCES
;
212 // Register new Handler and guid value.
214 CopyGuid (&mExtractHandlerGuidTable
[mNumberOfExtractHandler
], SectionGuid
);
215 mExtractDecodeHandlerTable
[mNumberOfExtractHandler
] = DecodeHandler
;
216 mExtractGetInfoHandlerTable
[mNumberOfExtractHandler
++] = GetInfoHandler
;
219 // Install the Guided Section GUID configuration table to record the GUID itself.
220 // Then the content of the configuration table buffer will be the same as the GUID value itself.
222 GuidData
= AllocateCopyPool (sizeof (GUID
), (VOID
*) SectionGuid
);
223 if (GuidData
!= NULL
) {
224 gBS
->InstallConfigurationTable ((EFI_GUID
*) SectionGuid
, GuidData
);
227 return RETURN_SUCCESS
;
231 Retrieves a GUID from a GUIDed section and uses that GUID to select an associated handler of type
232 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().
233 The selected handler is used to retrieve and return the size of the decoded buffer and the size of an
234 optional scratch buffer required to actually decode the data in a GUIDed section.
236 Examines a GUIDed section specified by InputSection.
237 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),
238 then RETURN_UNSUPPORTED is returned.
239 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler
240 of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()
241 is used to retrieve the OututBufferSize, ScratchSize, and Attributes values. The return status from the handler of
242 type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER is returned.
244 If InputSection is NULL, then ASSERT().
245 If OutputBufferSize is NULL, then ASSERT().
246 If ScratchBufferSize is NULL, then ASSERT().
247 If SectionAttribute is NULL, then ASSERT().
249 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
250 @param[out] OutputBufferSize A pointer to the size, in bytes, of an output buffer required if the buffer
251 specified by InputSection were decoded.
252 @param[out] ScratchBufferSize A pointer to the size, in bytes, required as scratch space if the buffer specified by
253 InputSection were decoded.
254 @param[out] SectionAttribute A pointer to the attributes of the GUIDed section. See the Attributes field of
255 EFI_GUID_DEFINED_SECTION in the PI Specification.
257 @retval RETURN_SUCCESS Successfully obtained the required information.
258 @retval RETURN_UNSUPPORTED The GUID from the section specified by InputSection does not match any of
259 the GUIDs registered with ExtractGuidedSectionRegisterHandlers().
260 @retval Others The return status from the handler associated with the GUID retrieved from
261 the section specified by InputSection.
266 ExtractGuidedSectionGetInfo (
267 IN CONST VOID
*InputSection
,
268 OUT UINT32
*OutputBufferSize
,
269 OUT UINT32
*ScratchBufferSize
,
270 OUT UINT16
*SectionAttribute
274 EFI_GUID
*SectionDefinitionGuid
;
276 ASSERT (InputSection
!= NULL
);
277 ASSERT (OutputBufferSize
!= NULL
);
278 ASSERT (ScratchBufferSize
!= NULL
);
279 ASSERT (SectionAttribute
!= NULL
);
281 if (IS_SECTION2 (InputSection
)) {
282 SectionDefinitionGuid
= &(((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->SectionDefinitionGuid
);
284 SectionDefinitionGuid
= &(((EFI_GUID_DEFINED_SECTION
*) InputSection
)->SectionDefinitionGuid
);
288 // Search the match registered GetInfo handler for the input guided section.
290 for (Index
= 0; Index
< mNumberOfExtractHandler
; Index
++) {
291 if (CompareGuid (&mExtractHandlerGuidTable
[Index
], SectionDefinitionGuid
)) {
293 // Call the match handler to getinfo for the input section data.
295 return mExtractGetInfoHandlerTable
[Index
] (
305 // Not found, the input guided section is not supported.
307 return RETURN_UNSUPPORTED
;
311 Retrieves the GUID from a GUIDed section and uses that GUID to select an associated handler of type
312 EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().
313 The selected handler is used to decode the data in a GUIDed section and return the result in a caller
314 allocated output buffer.
316 Decodes the GUIDed section specified by InputSection.
317 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),
318 then RETURN_UNSUPPORTED is returned.
319 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler
320 of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()
321 is used to decode InputSection into the buffer specified by OutputBuffer and the authentication status of this
322 decode operation is returned in AuthenticationStatus. If the decoded buffer is identical to the data in InputSection,
323 then OutputBuffer is set to point at the data in InputSection. Otherwise, the decoded data will be placed in caller
324 allocated buffer specified by OutputBuffer. This function is responsible for computing the EFI_AUTH_STATUS_PLATFORM_OVERRIDE
325 bit of in AuthenticationStatus. The return status from the handler of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER is returned.
327 If InputSection is NULL, then ASSERT().
328 If OutputBuffer is NULL, then ASSERT().
329 If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().
330 If AuthenticationStatus is NULL, then ASSERT().
332 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
333 @param[out] OutputBuffer A pointer to a buffer that contains the result of a decode operation.
334 @param[in] ScratchBuffer A caller allocated buffer that may be required by this function as a scratch buffer to perform the decode operation.
335 @param[out] AuthenticationStatus
336 A pointer to the authentication status of the decoded output buffer. See the definition
337 of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI section of the PI
340 @retval RETURN_SUCCESS The buffer specified by InputSection was decoded.
341 @retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports.
342 @retval RETURN_INVALID_PARAMETER The section specified by InputSection can not be decoded.
347 ExtractGuidedSectionDecode (
348 IN CONST VOID
*InputSection
,
349 OUT VOID
**OutputBuffer
,
350 IN VOID
*ScratchBuffer
, OPTIONAL
351 OUT UINT32
*AuthenticationStatus
355 EFI_GUID
*SectionDefinitionGuid
;
358 // Check the input parameters
360 ASSERT (InputSection
!= NULL
);
361 ASSERT (OutputBuffer
!= NULL
);
362 ASSERT (AuthenticationStatus
!= NULL
);
364 if (IS_SECTION2 (InputSection
)) {
365 SectionDefinitionGuid
= &(((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->SectionDefinitionGuid
);
367 SectionDefinitionGuid
= &(((EFI_GUID_DEFINED_SECTION
*) InputSection
)->SectionDefinitionGuid
);
371 // Search the match registered extract handler for the input guided section.
373 for (Index
= 0; Index
< mNumberOfExtractHandler
; Index
++) {
374 if (CompareGuid (&mExtractHandlerGuidTable
[Index
], SectionDefinitionGuid
)) {
376 // Call the match handler to extract raw data for the input section data.
378 return mExtractDecodeHandlerTable
[Index
] (
388 // Not found, the input guided section is not supported.
390 return RETURN_UNSUPPORTED
;
394 Retrieves handlers of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER and
395 EXTRACT_GUIDED_SECTION_DECODE_HANDLER for a specific GUID section type.
397 Retrieves the handlers associated with SectionGuid and returns them in
398 GetInfoHandler and DecodeHandler.
400 If the GUID value specified by SectionGuid has not been registered, then
401 return RETURN_NOT_FOUND.
403 If SectionGuid is NULL, then ASSERT().
405 @param[in] SectionGuid A pointer to the GUID associated with the handlersof the GUIDed
406 section type being retrieved.
407 @param[out] GetInfoHandler Pointer to a function that examines a GUIDed section and returns
408 the size of the decoded buffer and the size of an optional scratch
409 buffer required to actually decode the data in a GUIDed section.
410 This is an optional parameter that may be NULL. If it is NULL, then
411 the previously registered handler is not returned.
412 @param[out] DecodeHandler Pointer to a function that decodes a GUIDed section into a caller
413 allocated output buffer. This is an optional parameter that may be NULL.
414 If it is NULL, then the previously registered handler is not returned.
416 @retval RETURN_SUCCESS The handlers were retrieved.
417 @retval RETURN_NOT_FOUND No handlers have been registered with the specified GUID.
422 ExtractGuidedSectionGetHandlers (
423 IN CONST GUID
*SectionGuid
,
424 OUT EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER
*GetInfoHandler
, OPTIONAL
425 OUT EXTRACT_GUIDED_SECTION_DECODE_HANDLER
*DecodeHandler OPTIONAL
431 // Check input parameter.
433 ASSERT (SectionGuid
!= NULL
);
436 // Search the match registered GetInfo handler for the input guided section.
438 for (Index
= 0; Index
< mNumberOfExtractHandler
; Index
++) {
439 if (CompareGuid (&mExtractHandlerGuidTable
[Index
], SectionGuid
)) {
442 // If the guided handler has been registered before, then return the registered handlers.
444 if (GetInfoHandler
!= NULL
) {
445 *GetInfoHandler
= mExtractGetInfoHandlerTable
[Index
];
447 if (DecodeHandler
!= NULL
) {
448 *DecodeHandler
= mExtractDecodeHandlerTable
[Index
];
450 return RETURN_SUCCESS
;
453 return RETURN_NOT_FOUND
;