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