]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Library/SecExtractGuidedSectionLib/SecExtractGuidedSectionLib.c
Use new API GetSectionFromAnyFvByFileType() of MdePkg DxeServicesLib library to...
[mirror_edk2.git] / OvmfPkg / Library / SecExtractGuidedSectionLib / SecExtractGuidedSectionLib.c
CommitLineData
451e64b0 1/** @file\r
2 Provide generic extract guided section functions for SEC phase.\r
3\r
56d7640a
HT
4 Copyright (c) 2007 - 2009, Intel Corporation. All rights reserved.<BR>\r
5 This program and the accompanying materials\r
451e64b0 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
8 http://opensource.org/licenses/bsd-license.php\r
9\r
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
12\r
13**/\r
14\r
15#include <PiPei.h>\r
16\r
17#include <Library/DebugLib.h>\r
18#include <Library/PcdLib.h>\r
19#include <Library/BaseMemoryLib.h>\r
20#include <Library/ExtractGuidedSectionLib.h>\r
21\r
22#define EXTRACT_HANDLER_INFO_SIGNATURE SIGNATURE_32 ('E', 'G', 'S', 'I')\r
23\r
24typedef struct {\r
25 UINT32 Signature;\r
26 UINT32 NumberOfExtractHandler;\r
27 GUID *ExtractHandlerGuidTable;\r
28 EXTRACT_GUIDED_SECTION_DECODE_HANDLER *ExtractDecodeHandlerTable;\r
29 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *ExtractGetInfoHandlerTable;\r
30} EXTRACT_GUIDED_SECTION_HANDLER_INFO;\r
31\r
32STATIC EXTRACT_GUIDED_SECTION_HANDLER_INFO mHandlerInfo = {\r
33 0, // Signature;\r
34};\r
35\r
36/**\r
37 Check if the info structure can be used. If it can be used, but it\r
38 is not currently initialized, then it will be initialized.\r
39\r
40 @param[in] Info Pointer to handler info structure.\r
41\r
42 @retval RETURN_SUCCESS The info structure is initialized\r
43 @retval EFI_WRITE_PROTECTED The info structure could not be written to.\r
44**/\r
45STATIC\r
46RETURN_STATUS\r
47CheckOrInitializeHandlerInfo (\r
48 IN volatile EXTRACT_GUIDED_SECTION_HANDLER_INFO *Info\r
49 )\r
50{\r
51 //\r
52 // First try access the handler info structure as a global variable\r
53 //\r
54 if (Info->Signature == EXTRACT_HANDLER_INFO_SIGNATURE) {\r
55 //\r
56 // The global variable version of the handler info has been initialized\r
57 //\r
58 return EFI_SUCCESS;\r
59 }\r
60\r
61 //\r
62 // Try to initialize the handler info structure\r
63 //\r
64 Info->Signature = EXTRACT_HANDLER_INFO_SIGNATURE;\r
65 if (Info->Signature != EXTRACT_HANDLER_INFO_SIGNATURE) {\r
66 //\r
67 // The structure was not writeable\r
68 //\r
69 return EFI_WRITE_PROTECTED;\r
70 }\r
71\r
72 Info->NumberOfExtractHandler = 0;\r
73 Info->ExtractHandlerGuidTable = (GUID*) (Info + 1);\r
74 Info->ExtractDecodeHandlerTable =\r
75 (EXTRACT_GUIDED_SECTION_DECODE_HANDLER*)\r
76 &(Info->ExtractHandlerGuidTable [PcdGet32 (PcdMaximumGuidedExtractHandler)]);\r
77 Info->ExtractGetInfoHandlerTable =\r
78 (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER*)\r
79 &(Info->ExtractDecodeHandlerTable [PcdGet32 (PcdMaximumGuidedExtractHandler)]);\r
80 \r
81 return EFI_SUCCESS;\r
82}\r
83\r
84\r
85/**\r
86 Build guid hob for the global memory to store the registered guid and Handler list.\r
87 If GuidHob exists, HandlerInfo will be directly got from Guid hob data.\r
88\r
89 @param[in, out] InfoPointer Pointer to pei handler info structure.\r
90\r
91 @retval RETURN_SUCCESS Build Guid hob for the global memory space to store guid and function tables.\r
92 @retval RETURN_OUT_OF_RESOURCES No enough memory to allocated.\r
93**/\r
94RETURN_STATUS\r
95GetExtractGuidedSectionHandlerInfo (\r
96 IN OUT EXTRACT_GUIDED_SECTION_HANDLER_INFO **InfoPointer\r
97 )\r
98{\r
99 STATIC EXTRACT_GUIDED_SECTION_HANDLER_INFO* PotentialInfoLocations[] = {\r
100 //\r
101 // This entry will work if the global variables in the module are\r
102 // writeable.\r
103 //\r
104 &mHandlerInfo,\r
105\r
106 //\r
107 // This entry will work if the system memory is already initialized\r
108 // and ready for use. (For example, in a virtual machine, the memory\r
109 // will not require initialization.)\r
110 //\r
111 (EXTRACT_GUIDED_SECTION_HANDLER_INFO*)(VOID*)(UINTN) 0x1000,\r
112 };\r
113 UINTN Loop;\r
114\r
115 for (Loop = 0;\r
116 Loop < sizeof (PotentialInfoLocations) / sizeof (PotentialInfoLocations[0]);\r
117 Loop ++\r
118 ) {\r
119 //\r
120 // First try access the handler info structure as a global variable\r
121 //\r
122 if (!EFI_ERROR (CheckOrInitializeHandlerInfo (PotentialInfoLocations[Loop]))) {\r
123 //\r
124 // The global variable version of the handler info has been initialized\r
125 //\r
126 *InfoPointer = PotentialInfoLocations[Loop];\r
127 return EFI_SUCCESS;\r
128 }\r
129 }\r
130\r
131 *InfoPointer = (EXTRACT_GUIDED_SECTION_HANDLER_INFO*) NULL;\r
132 return RETURN_OUT_OF_RESOURCES;\r
133}\r
134\r
135/**\r
136 Retrieve the list GUIDs that have been registered through ExtractGuidedSectionRegisterHandlers().\r
137\r
138 Sets ExtractHandlerGuidTable so it points at a callee allocated array of registered GUIDs.\r
139 The total number of GUIDs in the array are returned. Since the array of GUIDs is callee allocated\r
140 and caller must treat this array of GUIDs as read-only data. \r
141 If ExtractHandlerGuidTable is NULL, then ASSERT().\r
142\r
143 @param[out] ExtractHandlerGuidTable A pointer to the array of GUIDs that have been registered through\r
144 ExtractGuidedSectionRegisterHandlers().\r
145\r
146 @return the number of the supported extract guided Handler.\r
147\r
148**/\r
149UINTN\r
150EFIAPI\r
151ExtractGuidedSectionGetGuidList (\r
152 OUT GUID **ExtractHandlerGuidTable\r
153 )\r
154{\r
155 EFI_STATUS Status;\r
156 EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
157\r
158 ASSERT (ExtractHandlerGuidTable != NULL);\r
159\r
160 //\r
161 // Get all registered handler information\r
162 //\r
163 Status = GetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
164 if (EFI_ERROR (Status)) {\r
165 return Status;\r
166 }\r
167\r
168 //\r
169 // Get GuidTable and Table Number\r
170 //\r
171 *ExtractHandlerGuidTable = HandlerInfo->ExtractHandlerGuidTable;\r
172 return HandlerInfo->NumberOfExtractHandler;\r
173}\r
174\r
175/**\r
176 Registers handlers of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER and EXTRACT_GUIDED_SECTION_DECODE_HANDLER\r
177 for a specific GUID section type.\r
178\r
179 Registers the handlers specified by GetInfoHandler and DecodeHandler with the GUID specified by SectionGuid.\r
180 If the GUID value specified by SectionGuid has already been registered, then return RETURN_ALREADY_STARTED.\r
181 If there are not enough resources available to register the handlers then RETURN_OUT_OF_RESOURCES is returned.\r
182 \r
183 If SectionGuid is NULL, then ASSERT().\r
184 If GetInfoHandler is NULL, then ASSERT().\r
185 If DecodeHandler is NULL, then ASSERT().\r
186\r
187 @param[in] SectionGuid A pointer to the GUID associated with the the handlers\r
188 of the GUIDed section type being registered.\r
189 @param[in] GetInfoHandler Pointer to a function that examines a GUIDed section and returns the\r
190 size of the decoded buffer and the size of an optional scratch buffer\r
191 required to actually decode the data in a GUIDed section.\r
192 @param[in] DecodeHandler Pointer to a function that decodes a GUIDed section into a caller\r
193 allocated output buffer. \r
194\r
195 @retval RETURN_SUCCESS The handlers were registered.\r
196 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to register the handlers.\r
197\r
198**/\r
199RETURN_STATUS\r
200EFIAPI\r
201ExtractGuidedSectionRegisterHandlers (\r
202 IN CONST GUID *SectionGuid,\r
203 IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER GetInfoHandler,\r
204 IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER DecodeHandler\r
205 )\r
206{\r
207 EFI_STATUS Status;\r
208 UINT32 Index;\r
209 EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
210\r
211 //\r
212 // Check input paramter\r
213 //\r
214 ASSERT (SectionGuid != NULL);\r
215 ASSERT (GetInfoHandler != NULL);\r
216 ASSERT (DecodeHandler != NULL);\r
217\r
218 //\r
219 // Get the registered handler information\r
220 //\r
221 Status = GetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
222 if (EFI_ERROR (Status)) {\r
223 return Status;\r
224 }\r
225\r
226 //\r
227 // Search the match registered GetInfo handler for the input guided section.\r
228 //\r
229 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index ++) {\r
230 if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, SectionGuid)) {\r
231 //\r
232 // If the guided handler has been registered before, only update its handler.\r
233 //\r
234 HandlerInfo->ExtractDecodeHandlerTable [Index] = DecodeHandler;\r
235 HandlerInfo->ExtractGetInfoHandlerTable [Index] = GetInfoHandler;\r
236 return RETURN_SUCCESS;\r
237 }\r
238 }\r
239\r
240 //\r
241 // Check the global table is enough to contain new Handler.\r
242 //\r
243 if (HandlerInfo->NumberOfExtractHandler >= PcdGet32 (PcdMaximumGuidedExtractHandler)) {\r
244 return RETURN_OUT_OF_RESOURCES;\r
245 }\r
246 \r
247 //\r
248 // Register new Handler and guid value.\r
249 //\r
250 CopyGuid (HandlerInfo->ExtractHandlerGuidTable + HandlerInfo->NumberOfExtractHandler, SectionGuid);\r
251 HandlerInfo->ExtractDecodeHandlerTable [HandlerInfo->NumberOfExtractHandler] = DecodeHandler;\r
252 HandlerInfo->ExtractGetInfoHandlerTable [HandlerInfo->NumberOfExtractHandler++] = GetInfoHandler;\r
253\r
254 return RETURN_SUCCESS;\r
255}\r
256\r
257/**\r
258 Retrieves a GUID from a GUIDed section and uses that GUID to select an associated handler of type\r
259 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().\r
260 The selected handler is used to retrieve and return the size of the decoded buffer and the size of an\r
261 optional scratch buffer required to actually decode the data in a GUIDed section.\r
262\r
263 Examines a GUIDed section specified by InputSection. \r
264 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),\r
265 then RETURN_UNSUPPORTED is returned. \r
266 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler \r
267 of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()\r
268 is used to retrieve the OututBufferSize, ScratchSize, and Attributes values. The return status from the handler of\r
269 type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER is returned.\r
270 \r
271 If InputSection is NULL, then ASSERT().\r
272 If OutputBufferSize is NULL, then ASSERT().\r
273 If ScratchBufferSize is NULL, then ASSERT().\r
274 If SectionAttribute is NULL, then ASSERT().\r
275\r
276 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.\r
277 @param[out] OutputBufferSize A pointer to the size, in bytes, of an output buffer required if the buffer\r
278 specified by InputSection were decoded.\r
279 @param[out] ScratchBufferSize A pointer to the size, in bytes, required as scratch space if the buffer specified by\r
280 InputSection were decoded.\r
281 @param[out] SectionAttribute A pointer to the attributes of the GUIDed section. See the Attributes field of\r
282 EFI_GUID_DEFINED_SECTION in the PI Specification.\r
283\r
284 @retval RETURN_SUCCESS Get the required information successfully.\r
285 @retval RETURN_UNSUPPORTED The GUID from the section specified by InputSection does not match any of\r
286 the GUIDs registered with ExtractGuidedSectionRegisterHandlers().\r
287 @retval Others The return status from the handler associated with the GUID retrieved from\r
288 the section specified by InputSection.\r
289\r
290**/\r
291RETURN_STATUS\r
292EFIAPI\r
293ExtractGuidedSectionGetInfo (\r
294 IN CONST VOID *InputSection,\r
295 OUT UINT32 *OutputBufferSize,\r
296 OUT UINT32 *ScratchBufferSize,\r
297 OUT UINT16 *SectionAttribute \r
298 )\r
299{\r
300 UINT32 Index;\r
301 EFI_STATUS Status;\r
302 EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
303 \r
304 //\r
305 // Check input paramter\r
306 //\r
307 ASSERT (InputSection != NULL);\r
308 ASSERT (OutputBufferSize != NULL);\r
309 ASSERT (ScratchBufferSize != NULL);\r
310 ASSERT (SectionAttribute != NULL);\r
311\r
312 //\r
313 // Get all registered handler information.\r
314 //\r
315 Status = GetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
316 if (EFI_ERROR (Status)) {\r
317 return Status;\r
318 }\r
319\r
320 //\r
321 // Search the match registered GetInfo handler for the input guided section.\r
322 //\r
323 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index ++) {\r
324 if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
325 //\r
326 // Call the match handler to get info for the input section data.\r
327 //\r
328 return HandlerInfo->ExtractGetInfoHandlerTable [Index] (\r
329 InputSection,\r
330 OutputBufferSize,\r
331 ScratchBufferSize,\r
332 SectionAttribute\r
333 );\r
334 }\r
335 }\r
336\r
337 //\r
338 // Not found, the input guided section is not supported. \r
339 //\r
340 return RETURN_UNSUPPORTED;\r
341}\r
342\r
343/**\r
344 Retrieves the GUID from a GUIDed section and uses that GUID to select an associated handler of type\r
345 EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().\r
346 The selected handler is used to decode the data in a GUIDed section and return the result in a caller\r
347 allocated output buffer.\r
348\r
349 Decodes the GUIDed section specified by InputSection. \r
350 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),\r
351 then RETURN_UNSUPPORTED is returned. \r
352 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler\r
353 of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()\r
354 is used to decode InputSection into the buffer specified by OutputBuffer and the authentication status of this\r
355 decode operation is returned in AuthenticationStatus. If the decoded buffer is identical to the data in InputSection,\r
356 then OutputBuffer is set to point at the data in InputSection. Otherwise, the decoded data will be placed in caller\r
357 allocated buffer specified by OutputBuffer. This function is responsible for computing the EFI_AUTH_STATUS_PLATFORM_OVERRIDE\r
358 bit of in AuthenticationStatus. The return status from the handler of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER is returned. \r
359 \r
360 If InputSection is NULL, then ASSERT().\r
361 If OutputBuffer is NULL, then ASSERT().\r
362 If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().\r
363 If AuthenticationStatus is NULL, then ASSERT(). \r
364\r
365 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.\r
366 @param[out] OutputBuffer A pointer to a buffer that contains the result of a decode operation. \r
367 @param[in] ScratchBuffer A caller allocated buffer that may be required by this function as a scratch buffer to perform the decode operation. \r
368 @param[out] AuthenticationStatus \r
369 A pointer to the authentication status of the decoded output buffer. See the definition\r
370 of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI section of the PI\r
371 Specification.\r
372\r
373 @retval RETURN_SUCCESS The buffer specified by InputSection was decoded.\r
374 @retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports.\r
375 @retval RETURN_INVALID_PARAMETER The section specified by InputSection can not be decoded.\r
376\r
377**/\r
378RETURN_STATUS\r
379EFIAPI\r
380ExtractGuidedSectionDecode (\r
381 IN CONST VOID *InputSection,\r
382 OUT VOID **OutputBuffer,\r
383 IN VOID *ScratchBuffer, OPTIONAL\r
384 OUT UINT32 *AuthenticationStatus \r
385 )\r
386{\r
387 UINT32 Index;\r
388 EFI_STATUS Status;\r
389 EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
390 \r
391 //\r
392 // Check input parameter\r
393 //\r
394 ASSERT (InputSection != NULL);\r
395 ASSERT (OutputBuffer != NULL);\r
396 ASSERT (AuthenticationStatus != NULL);\r
397\r
398 //\r
399 // Get all registered handler information.\r
400 // \r
401 Status = GetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
402 if (EFI_ERROR (Status)) {\r
403 return Status;\r
404 }\r
405\r
406 //\r
407 // Search the match registered Extract handler for the input guided section.\r
408 //\r
409 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index ++) {\r
410 if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
411 //\r
412 // Call the match handler to extract raw data for the input guided section.\r
413 //\r
414 return HandlerInfo->ExtractDecodeHandlerTable [Index] (\r
415 InputSection,\r
416 OutputBuffer,\r
417 ScratchBuffer,\r
418 AuthenticationStatus\r
419 );\r
420 }\r
421 }\r
422\r
423 //\r
424 // Not found, the input guided section is not supported. \r
425 //\r
426 return RETURN_UNSUPPORTED;\r
427}\r