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