]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdePkg/Library/BaseExtractGuidedSectionLib/BaseExtractGuidedSectionLib.c
MdePkg: Apply uncrustify changes
[mirror_edk2.git] / MdePkg / Library / BaseExtractGuidedSectionLib / BaseExtractGuidedSectionLib.c
... / ...
CommitLineData
1/** @file\r
2 Provide generic extract guided section functions.\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 <PiPei.h>\r
10\r
11#include <Library/DebugLib.h>\r
12#include <Library/PcdLib.h>\r
13#include <Library/BaseMemoryLib.h>\r
14#include <Library/ExtractGuidedSectionLib.h>\r
15\r
16#define EXTRACT_HANDLER_INFO_SIGNATURE SIGNATURE_32 ('E', 'G', 'S', 'I')\r
17\r
18typedef struct {\r
19 UINT32 Signature;\r
20 UINT32 NumberOfExtractHandler;\r
21 GUID *ExtractHandlerGuidTable;\r
22 EXTRACT_GUIDED_SECTION_DECODE_HANDLER *ExtractDecodeHandlerTable;\r
23 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *ExtractGetInfoHandlerTable;\r
24} EXTRACT_GUIDED_SECTION_HANDLER_INFO;\r
25\r
26/**\r
27 HandlerInfo table address is set by PcdGuidedExtractHandlerTableAddress, which is used to store\r
28 the registered guid and Handler list. When it is initialized, it will be directly returned.\r
29 Or, HandlerInfo table will be initialized in this function.\r
30\r
31 @param[in, out] InfoPointer The pointer to the handler information structure.\r
32\r
33 @retval RETURN_SUCCESS HandlerInfo table can be used to store guid and function tables.\r
34 @retval RETURN_OUT_OF_RESOURCES HandlerInfo table address is not writable.\r
35**/\r
36RETURN_STATUS\r
37GetExtractGuidedSectionHandlerInfo (\r
38 IN OUT EXTRACT_GUIDED_SECTION_HANDLER_INFO **InfoPointer\r
39 )\r
40{\r
41 EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
42\r
43 //\r
44 // Set the available memory address to handler info.\r
45 //\r
46 HandlerInfo = (EXTRACT_GUIDED_SECTION_HANDLER_INFO *)(VOID *)(UINTN)PcdGet64 (PcdGuidedExtractHandlerTableAddress);\r
47 if (HandlerInfo == NULL) {\r
48 *InfoPointer = NULL;\r
49 return EFI_OUT_OF_RESOURCES;\r
50 }\r
51\r
52 //\r
53 // First check whether the handler information structure is initialized.\r
54 //\r
55 if (HandlerInfo->Signature == EXTRACT_HANDLER_INFO_SIGNATURE) {\r
56 //\r
57 // The handler information has been initialized and is returned.\r
58 //\r
59 *InfoPointer = HandlerInfo;\r
60 return RETURN_SUCCESS;\r
61 }\r
62\r
63 //\r
64 // Try to initialize the handler information structure\r
65 //\r
66 HandlerInfo->Signature = EXTRACT_HANDLER_INFO_SIGNATURE;\r
67 if (HandlerInfo->Signature != EXTRACT_HANDLER_INFO_SIGNATURE) {\r
68 //\r
69 // The handler information structure was not writeable because the memory is not ready.\r
70 //\r
71 *InfoPointer = NULL;\r
72 return RETURN_OUT_OF_RESOURCES;\r
73 }\r
74\r
75 //\r
76 // Init HandlerInfo structure\r
77 //\r
78 HandlerInfo->NumberOfExtractHandler = 0;\r
79 HandlerInfo->ExtractHandlerGuidTable = (GUID *)(HandlerInfo + 1);\r
80 HandlerInfo->ExtractDecodeHandlerTable = (EXTRACT_GUIDED_SECTION_DECODE_HANDLER *)(\r
81 (UINT8 *)HandlerInfo->ExtractHandlerGuidTable +\r
82 PcdGet32 (PcdMaximumGuidedExtractHandler) * sizeof (GUID)\r
83 );\r
84 HandlerInfo->ExtractGetInfoHandlerTable = (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *)(\r
85 (UINT8 *)HandlerInfo->ExtractDecodeHandlerTable +\r
86 PcdGet32 (PcdMaximumGuidedExtractHandler) *\r
87 sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER)\r
88 );\r
89 *InfoPointer = HandlerInfo;\r
90 return RETURN_SUCCESS;\r
91}\r
92\r
93/**\r
94 Retrieve the list GUIDs that have been registered through ExtractGuidedSectionRegisterHandlers().\r
95\r
96 Sets ExtractHandlerGuidTable so it points at a callee allocated array of registered GUIDs.\r
97 The total number of GUIDs in the array are returned. Since the array of GUIDs is callee allocated\r
98 and caller must treat this array of GUIDs as read-only data.\r
99 If ExtractHandlerGuidTable is NULL, then ASSERT().\r
100\r
101 @param[out] ExtractHandlerGuidTable A pointer to the array of GUIDs that have been registered through\r
102 ExtractGuidedSectionRegisterHandlers().\r
103\r
104 @return The number of the supported extract guided Handler.\r
105\r
106**/\r
107UINTN\r
108EFIAPI\r
109ExtractGuidedSectionGetGuidList (\r
110 OUT GUID **ExtractHandlerGuidTable\r
111 )\r
112{\r
113 RETURN_STATUS Status;\r
114 EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
115\r
116 ASSERT (ExtractHandlerGuidTable != NULL);\r
117\r
118 //\r
119 // Get all registered handler information\r
120 //\r
121 Status = GetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
122 if (RETURN_ERROR (Status)) {\r
123 *ExtractHandlerGuidTable = NULL;\r
124 return 0;\r
125 }\r
126\r
127 //\r
128 // Get GuidTable and Table Number\r
129 //\r
130 ASSERT (HandlerInfo != NULL);\r
131 *ExtractHandlerGuidTable = HandlerInfo->ExtractHandlerGuidTable;\r
132 return HandlerInfo->NumberOfExtractHandler;\r
133}\r
134\r
135/**\r
136 Registers handlers of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER and EXTRACT_GUIDED_SECTION_DECODE_HANDLER\r
137 for a specific GUID section type.\r
138\r
139 Registers the handlers specified by GetInfoHandler and DecodeHandler with the GUID specified by SectionGuid.\r
140 If the GUID value specified by SectionGuid has already been registered, then return RETURN_ALREADY_STARTED.\r
141 If there are not enough resources available to register the handlers then RETURN_OUT_OF_RESOURCES is returned.\r
142\r
143 If SectionGuid is NULL, then ASSERT().\r
144 If GetInfoHandler is NULL, then ASSERT().\r
145 If DecodeHandler is NULL, then ASSERT().\r
146\r
147 @param[in] SectionGuid A pointer to the GUID associated with the the handlers\r
148 of the GUIDed section type being registered.\r
149 @param[in] GetInfoHandler The pointer to a function that examines a GUIDed section and returns the\r
150 size of the decoded buffer and the size of an optional scratch buffer\r
151 required to actually decode the data in a GUIDed section.\r
152 @param[in] DecodeHandler The pointer to a function that decodes a GUIDed section into a caller\r
153 allocated output buffer.\r
154\r
155 @retval RETURN_SUCCESS The handlers were registered.\r
156 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to register the handlers.\r
157\r
158**/\r
159RETURN_STATUS\r
160EFIAPI\r
161ExtractGuidedSectionRegisterHandlers (\r
162 IN CONST GUID *SectionGuid,\r
163 IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER GetInfoHandler,\r
164 IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER DecodeHandler\r
165 )\r
166{\r
167 UINT32 Index;\r
168 RETURN_STATUS Status;\r
169 EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
170\r
171 //\r
172 // Check input parameter\r
173 //\r
174 ASSERT (SectionGuid != NULL);\r
175 ASSERT (GetInfoHandler != NULL);\r
176 ASSERT (DecodeHandler != NULL);\r
177\r
178 //\r
179 // Get the registered handler information\r
180 //\r
181 Status = GetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
182 if (RETURN_ERROR (Status)) {\r
183 return Status;\r
184 }\r
185\r
186 //\r
187 // Search the match registered GetInfo handler for the input guided section.\r
188 //\r
189 ASSERT (HandlerInfo != NULL);\r
190 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index++) {\r
191 if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, SectionGuid)) {\r
192 //\r
193 // If the guided handler has been registered before, only update its handler.\r
194 //\r
195 HandlerInfo->ExtractDecodeHandlerTable[Index] = DecodeHandler;\r
196 HandlerInfo->ExtractGetInfoHandlerTable[Index] = GetInfoHandler;\r
197 return RETURN_SUCCESS;\r
198 }\r
199 }\r
200\r
201 //\r
202 // Check the global table is enough to contain new Handler.\r
203 //\r
204 if (HandlerInfo->NumberOfExtractHandler >= PcdGet32 (PcdMaximumGuidedExtractHandler)) {\r
205 return RETURN_OUT_OF_RESOURCES;\r
206 }\r
207\r
208 //\r
209 // Register new Handler and guid value.\r
210 //\r
211 CopyGuid (HandlerInfo->ExtractHandlerGuidTable + HandlerInfo->NumberOfExtractHandler, SectionGuid);\r
212 HandlerInfo->ExtractDecodeHandlerTable[HandlerInfo->NumberOfExtractHandler] = DecodeHandler;\r
213 HandlerInfo->ExtractGetInfoHandlerTable[HandlerInfo->NumberOfExtractHandler++] = GetInfoHandler;\r
214\r
215 return RETURN_SUCCESS;\r
216}\r
217\r
218/**\r
219 Retrieves a GUID from a GUIDed section and uses that GUID to select an associated handler of type\r
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
231\r
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
245 @retval RETURN_SUCCESS Successfully retrieved the required information.\r
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
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
262 RETURN_STATUS Status;\r
263 EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
264 EFI_GUID *SectionDefinitionGuid;\r
265\r
266 //\r
267 // Check input parameter\r
268 //\r
269 ASSERT (InputSection != NULL);\r
270 ASSERT (OutputBufferSize != NULL);\r
271 ASSERT (ScratchBufferSize != NULL);\r
272 ASSERT (SectionAttribute != NULL);\r
273\r
274 //\r
275 // Get all registered handler information.\r
276 //\r
277 Status = GetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
278 if (RETURN_ERROR (Status)) {\r
279 return Status;\r
280 }\r
281\r
282 if (IS_SECTION2 (InputSection)) {\r
283 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION2 *)InputSection)->SectionDefinitionGuid);\r
284 } else {\r
285 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION *)InputSection)->SectionDefinitionGuid);\r
286 }\r
287\r
288 //\r
289 // Search the match registered GetInfo handler for the input guided section.\r
290 //\r
291 ASSERT (HandlerInfo != NULL);\r
292 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index++) {\r
293 if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, SectionDefinitionGuid)) {\r
294 //\r
295 // Call the match handler to get information for the input section data.\r
296 //\r
297 return HandlerInfo->ExtractGetInfoHandlerTable[Index](\r
298 InputSection,\r
299 OutputBufferSize,\r
300 ScratchBufferSize,\r
301 SectionAttribute\r
302 );\r
303 }\r
304 }\r
305\r
306 //\r
307 // Not found, the input guided section is not supported.\r
308 //\r
309 return RETURN_UNSUPPORTED;\r
310}\r
311\r
312/**\r
313 Retrieves the GUID from a GUIDed section and uses that GUID to select an associated handler of type\r
314 EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().\r
315 The selected handler is used to decode the data in a GUIDed section and return the result in a caller\r
316 allocated output buffer.\r
317\r
318 Decodes the GUIDed section specified by InputSection.\r
319 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),\r
320 then RETURN_UNSUPPORTED is returned.\r
321 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler\r
322 of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()\r
323 is used to decode InputSection into the buffer specified by OutputBuffer and the authentication status of this\r
324 decode operation is returned in AuthenticationStatus. If the decoded buffer is identical to the data in InputSection,\r
325 then OutputBuffer is set to point at the data in InputSection. Otherwise, the decoded data will be placed in a caller\r
326 allocated buffer specified by OutputBuffer. This function is responsible for computing the EFI_AUTH_STATUS_PLATFORM_OVERRIDE\r
327 bit of in AuthenticationStatus. The return status from the handler of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER is returned.\r
328\r
329 If InputSection is NULL, then ASSERT().\r
330 If OutputBuffer is NULL, then ASSERT().\r
331 If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().\r
332 If AuthenticationStatus is NULL, then ASSERT().\r
333\r
334 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.\r
335 @param[out] OutputBuffer A pointer to a buffer that contains the result of a decode operation.\r
336 @param[in] ScratchBuffer A caller allocated buffer that may be required by this function as a scratch buffer to perform the decode operation.\r
337 @param[out] AuthenticationStatus\r
338 A pointer to the authentication status of the decoded output buffer. See the definition\r
339 of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI section of the PI\r
340 Specification.\r
341\r
342 @retval RETURN_SUCCESS The buffer specified by InputSection was decoded.\r
343 @retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports.\r
344 @retval RETURN_INVALID_PARAMETER The section specified by InputSection can not be decoded.\r
345\r
346**/\r
347RETURN_STATUS\r
348EFIAPI\r
349ExtractGuidedSectionDecode (\r
350 IN CONST VOID *InputSection,\r
351 OUT VOID **OutputBuffer,\r
352 IN VOID *ScratchBuffer OPTIONAL,\r
353 OUT UINT32 *AuthenticationStatus\r
354 )\r
355{\r
356 UINT32 Index;\r
357 RETURN_STATUS Status;\r
358 EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
359 EFI_GUID *SectionDefinitionGuid;\r
360\r
361 //\r
362 // Check input parameter\r
363 //\r
364 ASSERT (InputSection != NULL);\r
365 ASSERT (OutputBuffer != NULL);\r
366 ASSERT (AuthenticationStatus != NULL);\r
367\r
368 //\r
369 // Get all registered handler information.\r
370 //\r
371 Status = GetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
372 if (RETURN_ERROR (Status)) {\r
373 return Status;\r
374 }\r
375\r
376 if (IS_SECTION2 (InputSection)) {\r
377 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION2 *)InputSection)->SectionDefinitionGuid);\r
378 } else {\r
379 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION *)InputSection)->SectionDefinitionGuid);\r
380 }\r
381\r
382 //\r
383 // Search the match registered Extract handler for the input guided section.\r
384 //\r
385 ASSERT (HandlerInfo != NULL);\r
386 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index++) {\r
387 if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, SectionDefinitionGuid)) {\r
388 //\r
389 // Call the match handler to extract raw data for the input guided section.\r
390 //\r
391 return HandlerInfo->ExtractDecodeHandlerTable[Index](\r
392 InputSection,\r
393 OutputBuffer,\r
394 ScratchBuffer,\r
395 AuthenticationStatus\r
396 );\r
397 }\r
398 }\r
399\r
400 //\r
401 // Not found, the input guided section is not supported.\r
402 //\r
403 return RETURN_UNSUPPORTED;\r
404}\r
405\r
406/**\r
407 Retrieves handlers of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER and\r
408 EXTRACT_GUIDED_SECTION_DECODE_HANDLER for a specific GUID section type.\r
409\r
410 Retrieves the handlers associated with SectionGuid and returns them in\r
411 GetInfoHandler and DecodeHandler.\r
412\r
413 If the GUID value specified by SectionGuid has not been registered, then\r
414 return RETURN_NOT_FOUND.\r
415\r
416 If SectionGuid is NULL, then ASSERT().\r
417\r
418 @param[in] SectionGuid A pointer to the GUID associated with the handlersof the GUIDed\r
419 section type being retrieved.\r
420 @param[out] GetInfoHandler Pointer to a function that examines a GUIDed section and returns\r
421 the size of the decoded buffer and the size of an optional scratch\r
422 buffer required to actually decode the data in a GUIDed section.\r
423 This is an optional parameter that may be NULL. If it is NULL, then\r
424 the previously registered handler is not returned.\r
425 @param[out] DecodeHandler Pointer to a function that decodes a GUIDed section into a caller\r
426 allocated output buffer. This is an optional parameter that may be NULL.\r
427 If it is NULL, then the previously registered handler is not returned.\r
428\r
429 @retval RETURN_SUCCESS The handlers were retrieved.\r
430 @retval RETURN_NOT_FOUND No handlers have been registered with the specified GUID.\r
431\r
432**/\r
433RETURN_STATUS\r
434EFIAPI\r
435ExtractGuidedSectionGetHandlers (\r
436 IN CONST GUID *SectionGuid,\r
437 OUT EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *GetInfoHandler OPTIONAL,\r
438 OUT EXTRACT_GUIDED_SECTION_DECODE_HANDLER *DecodeHandler OPTIONAL\r
439 )\r
440{\r
441 UINT32 Index;\r
442 RETURN_STATUS Status;\r
443 EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
444\r
445 //\r
446 // Check input parameter\r
447 //\r
448 ASSERT (SectionGuid != NULL);\r
449\r
450 //\r
451 // Get the registered handler information\r
452 //\r
453 Status = GetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
454 if (RETURN_ERROR (Status)) {\r
455 return Status;\r
456 }\r
457\r
458 //\r
459 // Search the match registered GetInfo handler for the input guided section.\r
460 //\r
461 ASSERT (HandlerInfo != NULL);\r
462 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index++) {\r
463 if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, SectionGuid)) {\r
464 //\r
465 // If the guided handler has been registered before, then return the registered handlers.\r
466 //\r
467 if (GetInfoHandler != NULL) {\r
468 *GetInfoHandler = HandlerInfo->ExtractGetInfoHandlerTable[Index];\r
469 }\r
470\r
471 if (DecodeHandler != NULL) {\r
472 *DecodeHandler = HandlerInfo->ExtractDecodeHandlerTable[Index];\r
473 }\r
474\r
475 return RETURN_SUCCESS;\r
476 }\r
477 }\r
478\r
479 return RETURN_NOT_FOUND;\r
480}\r