]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/DxeExtractGuidedSectionLib/DxeExtractGuidedSectionLib.c
Update comments in BaseExtractGuidedSectionLib library instance to say its used memor...
[mirror_edk2.git] / MdePkg / Library / DxeExtractGuidedSectionLib / DxeExtractGuidedSectionLib.c
CommitLineData
8069d49e 1/** @file\r
eceb3a4c 2 Provide generic extract guided section functions for Dxe phase.\r
0fa00159 3\r
30f001ca 4 Copyright (c) 2007 - 2011, Intel Corporation. All rights reserved.<BR>\r
19388d29 5 This program and the accompanying materials\r
8069d49e
LG
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
2fc59a00 8 http://opensource.org/licenses/bsd-license.php.\r
0fa00159 9\r
8069d49e
LG
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
0fa00159 12\r
8069d49e 13**/\r
0fa00159
LG
14\r
15#include <PiDxe.h>\r
16\r
17#include <Library/DebugLib.h>\r
0fa00159
LG
18#include <Library/BaseMemoryLib.h>\r
19#include <Library/MemoryAllocationLib.h>\r
20#include <Library/ExtractGuidedSectionLib.h>\r
21\r
de2314f8
LG
22#define EXTRACT_HANDLER_TABLE_SIZE 0x10\r
23\r
fe467413 24UINT32 mNumberOfExtractHandler = 0;\r
de2314f8 25UINT32 mMaxNumberOfExtractHandler = 0;\r
0fa00159 26\r
de2314f8
LG
27GUID *mExtractHandlerGuidTable = NULL;\r
28EXTRACT_GUIDED_SECTION_DECODE_HANDLER *mExtractDecodeHandlerTable = NULL;\r
29EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *mExtractGetInfoHandlerTable = NULL;\r
0fa00159 30\r
4754c98b 31/**\r
de2314f8 32 Reallocates more global memory to store the registered guid and Handler list.\r
0fa00159 33\r
58380e9c 34 @retval RETURN_SUCCESS Reallocated more global memory space to store guid and function tables.\r
35 @retval RETURN_OUT_OF_RESOURCES Not enough memory to allocate.\r
0fa00159
LG
36**/\r
37RETURN_STATUS\r
38EFIAPI\r
de2314f8 39ReallocateExtractHandlerTable (\r
0fa00159 40 )\r
de2314f8 41{ \r
0fa00159 42 //\r
de2314f8 43 // Reallocate memory for GuidTable\r
0fa00159 44 //\r
de2314f8
LG
45 mExtractHandlerGuidTable = ReallocatePool (\r
46 mMaxNumberOfExtractHandler * sizeof (GUID), \r
47 (mMaxNumberOfExtractHandler + EXTRACT_HANDLER_TABLE_SIZE) * sizeof (GUID), \r
48 mExtractHandlerGuidTable\r
49 );\r
50\r
0fa00159 51 if (mExtractHandlerGuidTable == NULL) {\r
de2314f8 52 goto Done;\r
0fa00159 53 }\r
de2314f8
LG
54\r
55 //\r
56 // Reallocate memory for Decode handler Table\r
57 //\r
58 mExtractDecodeHandlerTable = ReallocatePool (\r
59 mMaxNumberOfExtractHandler * sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER), \r
60 (mMaxNumberOfExtractHandler + EXTRACT_HANDLER_TABLE_SIZE) * sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER), \r
61 mExtractDecodeHandlerTable\r
62 );\r
63\r
0fa00159 64 if (mExtractDecodeHandlerTable == NULL) {\r
de2314f8 65 goto Done;\r
0fa00159
LG
66 }\r
67\r
de2314f8
LG
68 //\r
69 // Reallocate memory for GetInfo handler Table\r
70 //\r
71 mExtractGetInfoHandlerTable = ReallocatePool (\r
72 mMaxNumberOfExtractHandler * sizeof (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER), \r
73 (mMaxNumberOfExtractHandler + EXTRACT_HANDLER_TABLE_SIZE) * sizeof (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER), \r
74 mExtractGetInfoHandlerTable\r
75 );\r
76\r
0fa00159 77 if (mExtractGetInfoHandlerTable == NULL) {\r
de2314f8
LG
78 goto Done;\r
79 }\r
80 \r
81 //\r
82 // Increase max handler number\r
83 //\r
84 mMaxNumberOfExtractHandler = mMaxNumberOfExtractHandler + EXTRACT_HANDLER_TABLE_SIZE;\r
85 return RETURN_SUCCESS;\r
86\r
87Done:\r
88 if (mExtractHandlerGuidTable != NULL) {\r
b911d09f 89 FreePool (mExtractHandlerGuidTable);\r
de2314f8
LG
90 }\r
91 if (mExtractDecodeHandlerTable != NULL) {\r
b911d09f 92 FreePool (mExtractDecodeHandlerTable);\r
de2314f8
LG
93 }\r
94 if (mExtractGetInfoHandlerTable != NULL) {\r
95 FreePool (mExtractGetInfoHandlerTable);\r
0fa00159
LG
96 }\r
97 \r
de2314f8
LG
98 return RETURN_OUT_OF_RESOURCES;\r
99}\r
100/**\r
101 Constructor allocates the global memory to store the registered guid and Handler list.\r
102\r
103 @param ImageHandle The firmware allocated handle for the EFI image.\r
104 @param SystemTable A pointer to the EFI System Table.\r
105\r
58380e9c 106 @retval RETURN_SUCCESS Allocated the global memory space to store guid and function tables.\r
107 @retval RETURN_OUT_OF_RESOURCES Not enough memory to allocate.\r
de2314f8
LG
108**/\r
109RETURN_STATUS\r
110EFIAPI\r
111DxeExtractGuidedSectionLibConstructor (\r
112 IN EFI_HANDLE ImageHandle,\r
113 IN EFI_SYSTEM_TABLE *SystemTable\r
114 )\r
115{\r
116 return ReallocateExtractHandlerTable ();\r
0fa00159
LG
117}\r
118\r
4754c98b 119/**\r
f1db45f8 120 Retrieve the list GUIDs that have been registered through ExtractGuidedSectionRegisterHandlers().\r
0fa00159 121\r
f1db45f8 122 Sets ExtractHandlerGuidTable so it points at a callee allocated array of registered GUIDs.\r
123 The total number of GUIDs in the array are returned. Since the array of GUIDs is callee allocated\r
124 and caller must treat this array of GUIDs as read-only data. \r
125 If ExtractHandlerGuidTable is NULL, then ASSERT().\r
126\r
0057fda6 127 @param[out] ExtractHandlerGuidTable A pointer to the array of GUIDs that have been registered through\r
f1db45f8 128 ExtractGuidedSectionRegisterHandlers().\r
0fa00159 129\r
58380e9c 130 @return The number of the supported extract guided Handler.\r
f1db45f8 131\r
0fa00159
LG
132**/\r
133UINTN\r
134EFIAPI\r
135ExtractGuidedSectionGetGuidList (\r
eceb3a4c 136 OUT GUID **ExtractHandlerGuidTable\r
0fa00159
LG
137 )\r
138{\r
139 ASSERT (ExtractHandlerGuidTable != NULL);\r
140\r
141 *ExtractHandlerGuidTable = mExtractHandlerGuidTable;\r
142 return mNumberOfExtractHandler;\r
143}\r
144\r
4754c98b 145/**\r
f1db45f8 146 Registers handlers of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER and EXTRACT_GUIDED_SECTION_DECODE_HANDLER\r
147 for a specific GUID section type.\r
148\r
0057fda6 149 Registers the handlers specified by GetInfoHandler and DecodeHandler with the GUID specified by SectionGuid.\r
f1db45f8 150 If the GUID value specified by SectionGuid has already been registered, then return RETURN_ALREADY_STARTED.\r
151 If there are not enough resources available to register the handlers then RETURN_OUT_OF_RESOURCES is returned.\r
57209470 152 \r
f1db45f8 153 If SectionGuid is NULL, then ASSERT().\r
154 If GetInfoHandler is NULL, then ASSERT().\r
155 If DecodeHandler is NULL, then ASSERT().\r
156\r
157 @param[in] SectionGuid A pointer to the GUID associated with the the handlers\r
158 of the GUIDed section type being registered.\r
2fc59a00 159 @param[in] GetInfoHandler The pointer to a function that examines a GUIDed section and returns the\r
f1db45f8 160 size of the decoded buffer and the size of an optional scratch buffer\r
161 required to actually decode the data in a GUIDed section.\r
2fc59a00 162 @param[in] DecodeHandler The pointer to a function that decodes a GUIDed section into a caller\r
f1db45f8 163 allocated output buffer. \r
164\r
165 @retval RETURN_SUCCESS The handlers were registered.\r
f1db45f8 166 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to register the handlers.\r
0fa00159 167\r
0fa00159
LG
168**/\r
169RETURN_STATUS\r
170EFIAPI\r
171ExtractGuidedSectionRegisterHandlers (\r
172 IN CONST GUID *SectionGuid,\r
173 IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER GetInfoHandler,\r
174 IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER DecodeHandler\r
175 )\r
176{\r
e2701217 177 UINT32 Index;\r
0fa00159
LG
178 //\r
179 // Check input paramter.\r
180 //\r
f1db45f8 181 ASSERT (SectionGuid != NULL);\r
182 ASSERT (GetInfoHandler != NULL);\r
183 ASSERT (DecodeHandler != NULL);\r
e2701217
LG
184\r
185 //\r
186 // Search the match registered GetInfo handler for the input guided section.\r
187 //\r
188 for (Index = 0; Index < mNumberOfExtractHandler; Index ++) {\r
189 if (CompareGuid (&mExtractHandlerGuidTable[Index], SectionGuid)) {\r
b911d09f
LG
190 //\r
191 // If the guided handler has been registered before, only update its handler.\r
192 //\r
193 mExtractDecodeHandlerTable [Index] = DecodeHandler;\r
194 mExtractGetInfoHandlerTable [Index] = GetInfoHandler;\r
195 return RETURN_SUCCESS;\r
e2701217
LG
196 }\r
197 }\r
e2701217 198 \r
0fa00159
LG
199 //\r
200 // Check the global table is enough to contain new Handler.\r
201 //\r
de2314f8
LG
202 if (mNumberOfExtractHandler >= mMaxNumberOfExtractHandler) {\r
203 if (ReallocateExtractHandlerTable () != RETURN_SUCCESS) {\r
204 return RETURN_OUT_OF_RESOURCES;\r
205 }\r
0fa00159
LG
206 }\r
207 \r
208 //\r
209 // Register new Handler and guid value.\r
210 //\r
211 CopyGuid (&mExtractHandlerGuidTable [mNumberOfExtractHandler], SectionGuid);\r
212 mExtractDecodeHandlerTable [mNumberOfExtractHandler] = DecodeHandler;\r
213 mExtractGetInfoHandlerTable [mNumberOfExtractHandler++] = GetInfoHandler;\r
214 \r
215 return RETURN_SUCCESS;\r
216}\r
217\r
4754c98b 218/**\r
0057fda6 219 Retrieves a GUID from a GUIDed section and uses that GUID to select an associated handler of type\r
f1db45f8 220 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().\r
221 The selected handler is used to retrieve and return the size of the decoded buffer and the size of an\r
222 optional scratch buffer required to actually decode the data in a GUIDed section.\r
223\r
224 Examines a GUIDed section specified by InputSection. \r
225 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),\r
226 then RETURN_UNSUPPORTED is returned. \r
227 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler \r
228 of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()\r
229 is used to retrieve the OututBufferSize, ScratchSize, and Attributes values. The return status from the handler of\r
230 type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER is returned.\r
518db1d9 231 \r
f1db45f8 232 If InputSection is NULL, then ASSERT().\r
233 If OutputBufferSize is NULL, then ASSERT().\r
234 If ScratchBufferSize is NULL, then ASSERT().\r
235 If SectionAttribute is NULL, then ASSERT().\r
236\r
237 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.\r
238 @param[out] OutputBufferSize A pointer to the size, in bytes, of an output buffer required if the buffer\r
239 specified by InputSection were decoded.\r
240 @param[out] ScratchBufferSize A pointer to the size, in bytes, required as scratch space if the buffer specified by\r
241 InputSection were decoded.\r
242 @param[out] SectionAttribute A pointer to the attributes of the GUIDed section. See the Attributes field of\r
243 EFI_GUID_DEFINED_SECTION in the PI Specification.\r
244\r
58380e9c 245 @retval RETURN_SUCCESS Successfully obtained the required information.\r
f1db45f8 246 @retval RETURN_UNSUPPORTED The GUID from the section specified by InputSection does not match any of\r
247 the GUIDs registered with ExtractGuidedSectionRegisterHandlers().\r
248 @retval Others The return status from the handler associated with the GUID retrieved from\r
249 the section specified by InputSection.\r
0fa00159
LG
250\r
251**/\r
252RETURN_STATUS\r
253EFIAPI\r
254ExtractGuidedSectionGetInfo (\r
255 IN CONST VOID *InputSection,\r
256 OUT UINT32 *OutputBufferSize,\r
257 OUT UINT32 *ScratchBufferSize,\r
258 OUT UINT16 *SectionAttribute \r
259 )\r
260{\r
261 UINT32 Index;\r
30f001ca 262 EFI_GUID *SectionDefinitionGuid;\r
f1db45f8 263\r
264 ASSERT (InputSection != NULL); \r
0fa00159
LG
265 ASSERT (OutputBufferSize != NULL);\r
266 ASSERT (ScratchBufferSize != NULL);\r
267 ASSERT (SectionAttribute != NULL);\r
30f001ca
SZ
268\r
269 if (IS_SECTION2 (InputSection)) {\r
270 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid);\r
271 } else {\r
272 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid);\r
273 }\r
0fa00159
LG
274 \r
275 //\r
276 // Search the match registered GetInfo handler for the input guided section.\r
277 //\r
278 for (Index = 0; Index < mNumberOfExtractHandler; Index ++) {\r
30f001ca 279 if (CompareGuid (&mExtractHandlerGuidTable[Index], SectionDefinitionGuid)) {\r
b911d09f
LG
280 //\r
281 // Call the match handler to getinfo for the input section data.\r
282 //\r
283 return mExtractGetInfoHandlerTable [Index] (\r
284 InputSection,\r
285 OutputBufferSize,\r
286 ScratchBufferSize,\r
287 SectionAttribute\r
288 );\r
0fa00159
LG
289 }\r
290 }\r
291\r
292 //\r
293 // Not found, the input guided section is not supported. \r
294 //\r
b911d09f 295 return RETURN_UNSUPPORTED;\r
0fa00159
LG
296}\r
297\r
4754c98b 298/**\r
0057fda6 299 Retrieves the GUID from a GUIDed section and uses that GUID to select an associated handler of type\r
f1db45f8 300 EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().\r
301 The selected handler is used to decode the data in a GUIDed section and return the result in a caller\r
302 allocated output buffer.\r
303\r
304 Decodes the GUIDed section specified by InputSection. \r
305 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),\r
306 then RETURN_UNSUPPORTED is returned. \r
307 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler\r
308 of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()\r
309 is used to decode InputSection into the buffer specified by OutputBuffer and the authentication status of this\r
310 decode operation is returned in AuthenticationStatus. If the decoded buffer is identical to the data in InputSection,\r
311 then OutputBuffer is set to point at the data in InputSection. Otherwise, the decoded data will be placed in caller\r
312 allocated buffer specified by OutputBuffer. This function is responsible for computing the EFI_AUTH_STATUS_PLATFORM_OVERRIDE\r
518db1d9 313 bit of in AuthenticationStatus. The return status from the handler of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER is returned. \r
314 \r
f1db45f8 315 If InputSection is NULL, then ASSERT().\r
316 If OutputBuffer is NULL, then ASSERT().\r
317 If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().\r
318 If AuthenticationStatus is NULL, then ASSERT(). \r
319\r
320 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.\r
321 @param[out] OutputBuffer A pointer to a buffer that contains the result of a decode operation. \r
322 @param[in] ScratchBuffer A caller allocated buffer that may be required by this function as a scratch buffer to perform the decode operation. \r
0fa00159 323 @param[out] AuthenticationStatus \r
f1db45f8 324 A pointer to the authentication status of the decoded output buffer. See the definition\r
325 of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI section of the PI\r
326 Specification.\r
4754c98b 327\r
f1db45f8 328 @retval RETURN_SUCCESS The buffer specified by InputSection was decoded.\r
329 @retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports.\r
330 @retval RETURN_INVALID_PARAMETER The section specified by InputSection can not be decoded.\r
4754c98b 331\r
0fa00159
LG
332**/\r
333RETURN_STATUS\r
334EFIAPI\r
335ExtractGuidedSectionDecode (\r
336 IN CONST VOID *InputSection,\r
337 OUT VOID **OutputBuffer,\r
f1db45f8 338 IN VOID *ScratchBuffer, OPTIONAL\r
0fa00159
LG
339 OUT UINT32 *AuthenticationStatus \r
340 )\r
341{\r
342 UINT32 Index;\r
30f001ca 343 EFI_GUID *SectionDefinitionGuid;\r
0fa00159 344 \r
eceb3a4c
LG
345 //\r
346 // Check the input parameters\r
347 //\r
f1db45f8 348 ASSERT (InputSection != NULL);\r
0fa00159
LG
349 ASSERT (OutputBuffer != NULL);\r
350 ASSERT (AuthenticationStatus != NULL);\r
351\r
30f001ca
SZ
352 if (IS_SECTION2 (InputSection)) {\r
353 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid);\r
354 } else {\r
355 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid);\r
356 }\r
357\r
0fa00159 358 //\r
eceb3a4c 359 // Search the match registered extract handler for the input guided section.\r
0fa00159
LG
360 //\r
361 for (Index = 0; Index < mNumberOfExtractHandler; Index ++) {\r
30f001ca 362 if (CompareGuid (&mExtractHandlerGuidTable[Index], SectionDefinitionGuid)) {\r
b911d09f
LG
363 //\r
364 // Call the match handler to extract raw data for the input section data.\r
365 //\r
366 return mExtractDecodeHandlerTable [Index] (\r
367 InputSection,\r
368 OutputBuffer,\r
369 ScratchBuffer,\r
370 AuthenticationStatus\r
371 );\r
0fa00159
LG
372 }\r
373 }\r
374\r
375 //\r
376 // Not found, the input guided section is not supported. \r
377 //\r
b911d09f 378 return RETURN_UNSUPPORTED;\r
0fa00159 379}\r
9be899c5
ED
380\r
381/**\r
382 Retrieves handlers of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER and \r
383 EXTRACT_GUIDED_SECTION_DECODE_HANDLER for a specific GUID section type.\r
384 \r
385 Retrieves the handlers associated with SectionGuid and returns them in \r
386 GetInfoHandler and DecodeHandler.\r
387\r
388 If the GUID value specified by SectionGuid has not been registered, then \r
389 return RETURN_NOT_FOUND.\r
390 \r
391 If SectionGuid is NULL, then ASSERT().\r
392\r
393 @param[in] SectionGuid A pointer to the GUID associated with the handlersof the GUIDed \r
394 section type being retrieved.\r
395 @param[out] GetInfoHandler Pointer to a function that examines a GUIDed section and returns \r
396 the size of the decoded buffer and the size of an optional scratch \r
397 buffer required to actually decode the data in a GUIDed section. \r
398 This is an optional parameter that may be NULL. If it is NULL, then \r
399 the previously registered handler is not returned.\r
400 @param[out] DecodeHandler Pointer to a function that decodes a GUIDed section into a caller\r
401 allocated output buffer. This is an optional parameter that may be NULL.\r
402 If it is NULL, then the previously registered handler is not returned.\r
403\r
404 @retval RETURN_SUCCESS The handlers were retrieved.\r
405 @retval RETURN_NOT_FOUND No handlers have been registered with the specified GUID.\r
406\r
407**/\r
408RETURN_STATUS\r
409EFIAPI\r
410ExtractGuidedSectionGetHandlers (\r
411 IN CONST GUID *SectionGuid,\r
412 OUT EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *GetInfoHandler, OPTIONAL\r
413 OUT EXTRACT_GUIDED_SECTION_DECODE_HANDLER *DecodeHandler OPTIONAL\r
414 )\r
415{\r
416 UINT32 Index; \r
417\r
418 //\r
419 // Check input parameter.\r
420 //\r
421 ASSERT (SectionGuid != NULL);\r
422\r
423 //\r
424 // Search the match registered GetInfo handler for the input guided section.\r
425 //\r
426 for (Index = 0; Index < mNumberOfExtractHandler; Index ++) {\r
427 if (CompareGuid (&mExtractHandlerGuidTable[Index], SectionGuid)) {\r
428 \r
429 //\r
430 // If the guided handler has been registered before, then return the registered handlers.\r
431 //\r
432 if (GetInfoHandler != NULL) {\r
433 *GetInfoHandler = mExtractGetInfoHandlerTable[Index];\r
434 }\r
435 if (DecodeHandler != NULL) {\r
436 *DecodeHandler = mExtractDecodeHandlerTable[Index];\r
437 }\r
438 return RETURN_SUCCESS;\r
439 }\r
440 }\r
441 return RETURN_NOT_FOUND;\r
442}\r