]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/DxeExtractGuidedSectionLib/DxeExtractGuidedSectionLib.c
Synchronize interface function comment from declaration in library class header file...
[mirror_edk2.git] / MdePkg / Library / DxeExtractGuidedSectionLib / DxeExtractGuidedSectionLib.c
1 /** @file
2 Provide generic extract guided section functions for Dxe phase.
3
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
9
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.
12
13 **/
14
15 #include <PiDxe.h>
16
17 #include <Library/DebugLib.h>
18 #include <Library/PcdLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/MemoryAllocationLib.h>
21 #include <Library/ExtractGuidedSectionLib.h>
22
23 GUID *mExtractHandlerGuidTable;
24 UINT32 mNumberOfExtractHandler = 0;
25
26 EXTRACT_GUIDED_SECTION_DECODE_HANDLER *mExtractDecodeHandlerTable;
27 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *mExtractGetInfoHandlerTable;
28
29 /**
30 Constructor allocates the global memory to store the registered guid and Handler list.
31
32 @param ImageHandle The firmware allocated handle for the EFI image.
33 @param SystemTable A pointer to the EFI System Table.
34
35 @retval RETURN_SUCCESS Allocate the global memory space to store guid and function tables.
36 @retval RETURN_OUT_OF_RESOURCES No enough memory to allocated.
37 **/
38 RETURN_STATUS
39 EFIAPI
40 DxeExtractGuidedSectionLibConstructor (
41 IN EFI_HANDLE ImageHandle,
42 IN EFI_SYSTEM_TABLE *SystemTable
43 )
44 {
45 //
46 // Allocate global pool space to store the registered handler and its guid value.
47 //
48 mExtractHandlerGuidTable = (GUID *) AllocatePool (PcdGet32 (PcdMaximumGuidedExtractHandler) * sizeof (GUID));
49 if (mExtractHandlerGuidTable == NULL) {
50 return RETURN_OUT_OF_RESOURCES;
51 }
52
53 mExtractDecodeHandlerTable = (EXTRACT_GUIDED_SECTION_DECODE_HANDLER *) AllocatePool (PcdGet32 (PcdMaximumGuidedExtractHandler) * sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER));
54 if (mExtractDecodeHandlerTable == NULL) {
55 FreePool (mExtractHandlerGuidTable);
56 return RETURN_OUT_OF_RESOURCES;
57 }
58
59 mExtractGetInfoHandlerTable = (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *) AllocatePool (PcdGet32 (PcdMaximumGuidedExtractHandler) * sizeof (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER));
60 if (mExtractGetInfoHandlerTable == NULL) {
61 FreePool (mExtractHandlerGuidTable);
62 FreePool (mExtractDecodeHandlerTable);
63 return RETURN_OUT_OF_RESOURCES;
64 }
65
66 return RETURN_SUCCESS;
67 }
68
69 /**
70 Retrieve the list GUIDs that have been registered through ExtractGuidedSectionRegisterHandlers().
71
72 Sets ExtractHandlerGuidTable so it points at a callee allocated array of registered GUIDs.
73 The total number of GUIDs in the array are returned. Since the array of GUIDs is callee allocated
74 and caller must treat this array of GUIDs as read-only data.
75 If ExtractHandlerGuidTable is NULL, then ASSERT().
76
77 @param[out] ExtractHandlerGuidTable A pointer to the array of GUIDs that have been registered through
78 ExtractGuidedSectionRegisterHandlers().
79
80 @return the number of the supported extract guided Handler.
81
82 **/
83 UINTN
84 EFIAPI
85 ExtractGuidedSectionGetGuidList (
86 OUT GUID **ExtractHandlerGuidTable
87 )
88 {
89 ASSERT (ExtractHandlerGuidTable != NULL);
90
91 *ExtractHandlerGuidTable = mExtractHandlerGuidTable;
92 return mNumberOfExtractHandler;
93 }
94
95 /**
96 Registers handlers of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER and EXTRACT_GUIDED_SECTION_DECODE_HANDLER
97 for a specific GUID section type.
98
99 Registers the handlers specified by GetInfoHandler and DecodeHandler with the GUID specified by SectionGuid.
100 If the GUID value specified by SectionGuid has already been registered, then return RETURN_ALREADY_STARTED.
101 If there are not enough resources available to register the handlers then RETURN_OUT_OF_RESOURCES is returned.
102
103 If SectionGuid is NULL, then ASSERT().
104 If GetInfoHandler is NULL, then ASSERT().
105 If DecodeHandler is NULL, then ASSERT().
106
107 @param[in] SectionGuid A pointer to the GUID associated with the the handlers
108 of the GUIDed section type being registered.
109 @param[in] GetInfoHandler Pointer to a function that examines a GUIDed section and returns the
110 size of the decoded buffer and the size of an optional scratch buffer
111 required to actually decode the data in a GUIDed section.
112 @param[in] DecodeHandler Pointer to a function that decodes a GUIDed section into a caller
113 allocated output buffer.
114
115 @retval RETURN_SUCCESS The handlers were registered.
116 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to register the handlers.
117
118 **/
119 RETURN_STATUS
120 EFIAPI
121 ExtractGuidedSectionRegisterHandlers (
122 IN CONST GUID *SectionGuid,
123 IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER GetInfoHandler,
124 IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER DecodeHandler
125 )
126 {
127 UINT32 Index;
128 //
129 // Check input paramter.
130 //
131 ASSERT (SectionGuid != NULL);
132 ASSERT (GetInfoHandler != NULL);
133 ASSERT (DecodeHandler != NULL);
134
135 //
136 // Search the match registered GetInfo handler for the input guided section.
137 //
138 for (Index = 0; Index < mNumberOfExtractHandler; Index ++) {
139 if (CompareGuid (&mExtractHandlerGuidTable[Index], SectionGuid)) {
140 //
141 // If the guided handler has been registered before, only update its handler.
142 //
143 mExtractDecodeHandlerTable [Index] = DecodeHandler;
144 mExtractGetInfoHandlerTable [Index] = GetInfoHandler;
145 return RETURN_SUCCESS;
146 }
147 }
148
149 //
150 // Check the global table is enough to contain new Handler.
151 //
152 if (mNumberOfExtractHandler >= PcdGet32 (PcdMaximumGuidedExtractHandler)) {
153 return RETURN_OUT_OF_RESOURCES;
154 }
155
156 //
157 // Register new Handler and guid value.
158 //
159 CopyGuid (&mExtractHandlerGuidTable [mNumberOfExtractHandler], SectionGuid);
160 mExtractDecodeHandlerTable [mNumberOfExtractHandler] = DecodeHandler;
161 mExtractGetInfoHandlerTable [mNumberOfExtractHandler++] = GetInfoHandler;
162
163 return RETURN_SUCCESS;
164 }
165
166 /**
167 Retrieves a GUID from a GUIDed section and uses that GUID to select an associated handler of type
168 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().
169 The selected handler is used to retrieve and return the size of the decoded buffer and the size of an
170 optional scratch buffer required to actually decode the data in a GUIDed section.
171
172 Examines a GUIDed section specified by InputSection.
173 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),
174 then RETURN_UNSUPPORTED is returned.
175 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler
176 of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()
177 is used to retrieve the OututBufferSize, ScratchSize, and Attributes values. The return status from the handler of
178 type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER is returned.
179
180 If InputSection is NULL, then ASSERT().
181 If OutputBufferSize is NULL, then ASSERT().
182 If ScratchBufferSize is NULL, then ASSERT().
183 If SectionAttribute is NULL, then ASSERT().
184
185 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
186 @param[out] OutputBufferSize A pointer to the size, in bytes, of an output buffer required if the buffer
187 specified by InputSection were decoded.
188 @param[out] ScratchBufferSize A pointer to the size, in bytes, required as scratch space if the buffer specified by
189 InputSection were decoded.
190 @param[out] SectionAttribute A pointer to the attributes of the GUIDed section. See the Attributes field of
191 EFI_GUID_DEFINED_SECTION in the PI Specification.
192
193 @retval RETURN_SUCCESS Get the required information successfully.
194 @retval RETURN_UNSUPPORTED The GUID from the section specified by InputSection does not match any of
195 the GUIDs registered with ExtractGuidedSectionRegisterHandlers().
196 @retval Others The return status from the handler associated with the GUID retrieved from
197 the section specified by InputSection.
198
199 **/
200 RETURN_STATUS
201 EFIAPI
202 ExtractGuidedSectionGetInfo (
203 IN CONST VOID *InputSection,
204 OUT UINT32 *OutputBufferSize,
205 OUT UINT32 *ScratchBufferSize,
206 OUT UINT16 *SectionAttribute
207 )
208 {
209 UINT32 Index;
210
211 ASSERT (InputSection != NULL);
212 ASSERT (OutputBufferSize != NULL);
213 ASSERT (ScratchBufferSize != NULL);
214 ASSERT (SectionAttribute != NULL);
215
216 //
217 // Search the match registered GetInfo handler for the input guided section.
218 //
219 for (Index = 0; Index < mNumberOfExtractHandler; Index ++) {
220 if (CompareGuid (&mExtractHandlerGuidTable[Index], &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
221 //
222 // Call the match handler to getinfo for the input section data.
223 //
224 return mExtractGetInfoHandlerTable [Index] (
225 InputSection,
226 OutputBufferSize,
227 ScratchBufferSize,
228 SectionAttribute
229 );
230 }
231 }
232
233 //
234 // Not found, the input guided section is not supported.
235 //
236 return RETURN_UNSUPPORTED;
237 }
238
239 /**
240 Retrieves the GUID from a GUIDed section and uses that GUID to select an associated handler of type
241 EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().
242 The selected handler is used to decode the data in a GUIDed section and return the result in a caller
243 allocated output buffer.
244
245 Decodes the GUIDed section specified by InputSection.
246 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),
247 then RETURN_UNSUPPORTED is returned.
248 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler
249 of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()
250 is used to decode InputSection into the buffer specified by OutputBuffer and the authentication status of this
251 decode operation is returned in AuthenticationStatus. If the decoded buffer is identical to the data in InputSection,
252 then OutputBuffer is set to point at the data in InputSection. Otherwise, the decoded data will be placed in caller
253 allocated buffer specified by OutputBuffer. This function is responsible for computing the EFI_AUTH_STATUS_PLATFORM_OVERRIDE
254 bit of in AuthenticationStatus. The return status from the handler of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER is returned.
255
256 If InputSection is NULL, then ASSERT().
257 If OutputBuffer is NULL, then ASSERT().
258 If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().
259 If AuthenticationStatus is NULL, then ASSERT().
260
261 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
262 @param[out] OutputBuffer A pointer to a buffer that contains the result of a decode operation.
263 @param[in] ScratchBuffer A caller allocated buffer that may be required by this function as a scratch buffer to perform the decode operation.
264 @param[out] AuthenticationStatus
265 A pointer to the authentication status of the decoded output buffer. See the definition
266 of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI section of the PI
267 Specification.
268
269 @retval RETURN_SUCCESS The buffer specified by InputSection was decoded.
270 @retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports.
271 @retval RETURN_INVALID_PARAMETER The section specified by InputSection can not be decoded.
272
273 **/
274 RETURN_STATUS
275 EFIAPI
276 ExtractGuidedSectionDecode (
277 IN CONST VOID *InputSection,
278 OUT VOID **OutputBuffer,
279 IN VOID *ScratchBuffer, OPTIONAL
280 OUT UINT32 *AuthenticationStatus
281 )
282 {
283 UINT32 Index;
284
285 //
286 // Check the input parameters
287 //
288 ASSERT (InputSection != NULL);
289 ASSERT (OutputBuffer != NULL);
290 ASSERT (AuthenticationStatus != NULL);
291
292 //
293 // Search the match registered extract handler for the input guided section.
294 //
295 for (Index = 0; Index < mNumberOfExtractHandler; Index ++) {
296 if (CompareGuid (&mExtractHandlerGuidTable[Index], &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
297 //
298 // Call the match handler to extract raw data for the input section data.
299 //
300 return mExtractDecodeHandlerTable [Index] (
301 InputSection,
302 OutputBuffer,
303 ScratchBuffer,
304 AuthenticationStatus
305 );
306 }
307 }
308
309 //
310 // Not found, the input guided section is not supported.
311 //
312 return RETURN_UNSUPPORTED;
313 }