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