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