]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/DxeExtractGuidedSectionLib/DxeExtractGuidedSectionLib.c
Comments have been checked with spec
[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 If InputSection is NULL, then ASSERT().
180 If OutputBufferSize is NULL, then ASSERT().
181 If ScratchBufferSize is NULL, then ASSERT().
182 If SectionAttribute is NULL, then ASSERT().
183
184 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
185 @param[out] OutputBufferSize A pointer to the size, in bytes, of an output buffer required if the buffer
186 specified by InputSection were decoded.
187 @param[out] ScratchBufferSize A pointer to the size, in bytes, required as scratch space if the buffer specified by
188 InputSection were decoded.
189 @param[out] SectionAttribute A pointer to the attributes of the GUIDed section. See the Attributes field of
190 EFI_GUID_DEFINED_SECTION in the PI Specification.
191
192 @retval RETURN_SUCCESS Get the required information successfully.
193 @retval RETURN_UNSUPPORTED The GUID from the section specified by InputSection does not match any of
194 the GUIDs registered with ExtractGuidedSectionRegisterHandlers().
195 @retval Others The return status from the handler associated with the GUID retrieved from
196 the section specified by InputSection.
197
198 **/
199 RETURN_STATUS
200 EFIAPI
201 ExtractGuidedSectionGetInfo (
202 IN CONST VOID *InputSection,
203 OUT UINT32 *OutputBufferSize,
204 OUT UINT32 *ScratchBufferSize,
205 OUT UINT16 *SectionAttribute
206 )
207 {
208 UINT32 Index;
209
210 ASSERT (InputSection != NULL);
211 ASSERT (OutputBufferSize != NULL);
212 ASSERT (ScratchBufferSize != NULL);
213 ASSERT (SectionAttribute != NULL);
214
215 //
216 // Search the match registered GetInfo handler for the input guided section.
217 //
218 for (Index = 0; Index < mNumberOfExtractHandler; Index ++) {
219 if (CompareGuid (&mExtractHandlerGuidTable[Index], &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
220 //
221 // Call the match handler to getinfo for the input section data.
222 //
223 return mExtractGetInfoHandlerTable [Index] (
224 InputSection,
225 OutputBufferSize,
226 ScratchBufferSize,
227 SectionAttribute
228 );
229 }
230 }
231
232 //
233 // Not found, the input guided section is not supported.
234 //
235 return RETURN_UNSUPPORTED;
236 }
237
238 /**
239 Retrieves the GUID from a GUIDed section and uses that GUID to select an associated handler of type
240 EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().
241 The selected handler is used to decode the data in a GUIDed section and return the result in a caller
242 allocated output buffer.
243
244 Decodes the GUIDed section specified by InputSection.
245 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),
246 then RETURN_UNSUPPORTED is returned.
247 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler
248 of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()
249 is used to decode InputSection into the buffer specified by OutputBuffer and the authentication status of this
250 decode operation is returned in AuthenticationStatus. If the decoded buffer is identical to the data in InputSection,
251 then OutputBuffer is set to point at the data in InputSection. Otherwise, the decoded data will be placed in caller
252 allocated buffer specified by OutputBuffer. This function is responsible for computing the EFI_AUTH_STATUS_PLATFORM_OVERRIDE
253 bit of in AuthenticationStatus. The return status from the handler of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER is returned.
254 If InputSection is NULL, then ASSERT().
255 If OutputBuffer is NULL, then ASSERT().
256 If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().
257 If AuthenticationStatus is NULL, then ASSERT().
258
259 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
260 @param[out] OutputBuffer A pointer to a buffer that contains the result of a decode operation.
261 @param[in] ScratchBuffer A caller allocated buffer that may be required by this function as a scratch buffer to perform the decode operation.
262 @param[out] AuthenticationStatus
263 A pointer to the authentication status of the decoded output buffer. See the definition
264 of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI section of the PI
265 Specification.
266
267 @retval RETURN_SUCCESS The buffer specified by InputSection was decoded.
268 @retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports.
269 @retval RETURN_INVALID_PARAMETER The section specified by InputSection can not be decoded.
270
271 **/
272 RETURN_STATUS
273 EFIAPI
274 ExtractGuidedSectionDecode (
275 IN CONST VOID *InputSection,
276 OUT VOID **OutputBuffer,
277 IN VOID *ScratchBuffer, OPTIONAL
278 OUT UINT32 *AuthenticationStatus
279 )
280 {
281 UINT32 Index;
282
283 //
284 // Check the input parameters
285 //
286 ASSERT (InputSection != NULL);
287 ASSERT (OutputBuffer != NULL);
288 ASSERT (AuthenticationStatus != NULL);
289
290 //
291 // Search the match registered extract handler for the input guided section.
292 //
293 for (Index = 0; Index < mNumberOfExtractHandler; Index ++) {
294 if (CompareGuid (&mExtractHandlerGuidTable[Index], &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
295 //
296 // Call the match handler to extract raw data for the input section data.
297 //
298 return mExtractDecodeHandlerTable [Index] (
299 InputSection,
300 OutputBuffer,
301 ScratchBuffer,
302 AuthenticationStatus
303 );
304 }
305 }
306
307 //
308 // Not found, the input guided section is not supported.
309 //
310 return RETURN_UNSUPPORTED;
311 }