]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/PeiExtractGuidedSectionLib/PeiExtractGuidedSectionLib.c
Sync with other libs
[mirror_edk2.git] / MdePkg / Library / PeiExtractGuidedSectionLib / PeiExtractGuidedSectionLib.c
CommitLineData
0fa00159
LG
1/*++\r
2\r
3Copyright (c) 2007, Intel Corporation\r
4All rights reserved. This program and the accompanying materials\r
5are licensed and made available under the terms and conditions of the BSD License\r
6which accompanies this distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12Module Name:\r
13\r
14 PeiExtractGuidedSectionLib.c\r
15\r
16Abstract:\r
17\r
18 Provide generic extract guided section functions. \r
19\r
20--*/\r
21\r
22#include <PiPei.h>\r
23\r
24#include <Library/DebugLib.h>\r
25#include <Library/PcdLib.h>\r
26#include <Library/BaseMemoryLib.h>\r
27#include <Library/HobLib.h>\r
28#include <Library/ExtractGuidedSectionLib.h>\r
29\r
30#define PEI_EXTRACT_HANDLER_INFO_SIGNATURE EFI_SIGNATURE_32 ('P', 'E', 'H', 'I')\r
31\r
32typedef struct {\r
33 UINT32 Signature;\r
34 UINT32 NumberOfExtractHandler;\r
35 GUID *ExtractHandlerGuidTable;\r
36 EXTRACT_GUIDED_SECTION_DECODE_HANDLER *ExtractDecodeHandlerTable;\r
37 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *ExtractGetInfoHandlerTable;\r
38} PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO;\r
39\r
40/**
41 Build guid hob for the global memory to store the registered guid and Handler list.\r
42 If GuidHob exists, HandlerInfo will be directly got from Guid hob data.\r
43\r
44 @param[in, out] InfoPointer Pointer to pei handler info structure.
45\r
46 @retval RETURN_SUCCESS Build Guid hob for the global memory space to store guid and funciton tables.\r
47 @retval RETURN_OUT_OF_RESOURCES No enough memory to allocated.
48**/\r
49RETURN_STATUS\r
50EFIAPI\r
51PeiGetExtractGuidedSectionHandlerInfo (\r
52 IN OUT PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO **InfoPointer\r
53 )\r
54{\r
55 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
56 EFI_PEI_HOB_POINTERS Hob;\r
57 \r
58 //\r
59 // First try to get handler info from guid hob specified by CallerId.\r
60 //\r
61 Hob.Raw = GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION, GetHobList ());\r
62 while (Hob.Raw != NULL) {\r
63 if (CompareGuid (&(Hob.Guid->Name), &gEfiCallerIdGuid)) {\r
64 HandlerInfo = (PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO *) GET_GUID_HOB_DATA (Hob.Guid);\r
65 if (HandlerInfo->Signature == PEI_EXTRACT_HANDLER_INFO_SIGNATURE) {\r
66 *InfoPointer = HandlerInfo;\r
67 return EFI_SUCCESS;\r
68 }\r
69 }\r
70 Hob.Raw = GET_NEXT_HOB (Hob);\r
71 Hob.Raw = GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION, Hob.Raw);\r
72 }\r
73 \r
74 //\r
75 // If Guid Hob is not found, Build CallerId Guid hob to store Handler Info\r
76 //\r
77 HandlerInfo = BuildGuidHob (\r
78 &gEfiCallerIdGuid, \r
79 sizeof (PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO) +\r
80 PcdGet32 (PcdMaximumGuidedExtractHandler) * \r
81 (sizeof (GUID) + sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER) + sizeof (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER))\r
82 );\r
83 if (HandlerInfo == NULL) {\r
84 //\r
85 // No enough resource to build guid hob.\r
86 //\r
87 return EFI_OUT_OF_RESOURCES;\r
88 }\r
89 HandlerInfo->Signature = PEI_EXTRACT_HANDLER_INFO_SIGNATURE;\r
90 HandlerInfo->NumberOfExtractHandler = 0;\r
91 HandlerInfo->ExtractHandlerGuidTable = (GUID *) (HandlerInfo + 1);\r
92 HandlerInfo->ExtractDecodeHandlerTable = (EXTRACT_GUIDED_SECTION_DECODE_HANDLER *) (\r
93 (UINT8 *)HandlerInfo->ExtractHandlerGuidTable + \r
94 PcdGet32 (PcdMaximumGuidedExtractHandler) * sizeof (GUID)\r
95 );\r
96 HandlerInfo->ExtractGetInfoHandlerTable = (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *) (\r
97 (UINT8 *)HandlerInfo->ExtractDecodeHandlerTable + \r
98 PcdGet32 (PcdMaximumGuidedExtractHandler) * \r
99 sizeof (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER)\r
100 );\r
101 \r
102 *InfoPointer = HandlerInfo;\r
103 return EFI_SUCCESS;\r
104}\r
105\r
106/**
107 Get the supported exract guided section Handler guid list.\r
108 If ExtractHandlerGuidTable = NULL, then ASSERT.\r
109\r
110 @param[in, out] ExtractHandlerGuidTable The extract Handler guid pointer list.
111\r
112 @retval return the number of the supported extract guided Handler.
113**/\r
114UINTN\r
115EFIAPI\r
116ExtractGuidedSectionGetGuidList (\r
117 IN OUT GUID **ExtractHandlerGuidTable\r
118 )\r
119{\r
120 EFI_STATUS Status;\r
121 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
122\r
123 ASSERT (ExtractHandlerGuidTable != NULL);\r
124\r
125 Status = PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
126 if (EFI_ERROR (Status)) {\r
127 return Status;\r
128 }\r
129\r
130 *ExtractHandlerGuidTable = HandlerInfo->ExtractHandlerGuidTable;\r
131 return HandlerInfo->NumberOfExtractHandler;\r
132}\r
133\r
134/**
135 Register Guided Section Extract and GetInfo handler.\r
136\r
137 @param[in] SectionGuid The guid matches this Extraction function.
138 @param[in] GetInfoHandler Function to get info from guided section.\r
139 @param[in] DecodeHandler Function to extract guided section.
140
141 @retval RETURN_SUCCESS Register Guided Section Extract function successfully.
142 @retval RETURN_OUT_OF_RESOURCES Resource is not enough to register new function. \r
143 @retval RETURN_INVALID_PARAMETER Input pointer to Guid value is not valid.\r
144**/\r
145RETURN_STATUS\r
146EFIAPI\r
147ExtractGuidedSectionRegisterHandlers (\r
148 IN CONST GUID *SectionGuid,\r
149 IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER GetInfoHandler,\r
150 IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER DecodeHandler\r
151 )\r
152{\r
153 EFI_STATUS Status;\r
154 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
155\r
156 //\r
157 // Check input paramter.\r
158 //\r
159 if (SectionGuid == NULL) {\r
160 return RETURN_INVALID_PARAMETER;\r
161 }\r
162\r
163 //\r
164 // Get the registered handler information\r
165 //\r
166 Status = PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
167 if (EFI_ERROR (Status)) {\r
168 return Status;\r
169 }\r
170 //\r
171 // Check the global table is enough to contain new Handler.\r
172 //\r
173 if (HandlerInfo->NumberOfExtractHandler >= PcdGet32 (PcdMaximumGuidedExtractHandler)) {\r
174 return RETURN_OUT_OF_RESOURCES;\r
175 }\r
176 \r
177 //\r
178 // Register new Handler and guid value.\r
179 //\r
180 CopyGuid (&(HandlerInfo->ExtractHandlerGuidTable [HandlerInfo->NumberOfExtractHandler]), SectionGuid);\r
181 HandlerInfo->ExtractDecodeHandlerTable [HandlerInfo->NumberOfExtractHandler] = DecodeHandler;\r
182 HandlerInfo->ExtractGetInfoHandlerTable [HandlerInfo->NumberOfExtractHandler++] = GetInfoHandler;\r
183 \r
184 return RETURN_SUCCESS;\r
185}\r
186\r
187/**
188 Get information from the guided section. This function first gets the guid value\r
189 from guided section header, then match this guid in the registered extract Handler list\r
190 to its corresponding getinfo Handler. \r
191 If not found, RETURN_UNSUPPORTED will be return. \r
192 If found, it will call the getinfo Handler to get the required size and attribute.\r
193\r
194 It will ASSERT () if the pointer to OutputBufferSize is NULL.\r
195 It will ASSERT () if the pointer to ScratchBufferSize is NULL.
196 It will ASSERT () if the pointer to SectionAttribute is NULL.\r
197\r
198 @param[in] InputSection Buffer containing the input GUIDed section to be processed. \r
199 @param[out] OutputBufferSize The size of OutputBuffer.\r
200 @param[out] ScratchBufferSize The size of ScratchBuffer. \r
201 @param[out] SectionAttribute The attribute of the input guided section.\r
202
203 @retval RETURN_SUCCESS Get the required information successfully.\r
204 @retval RETURN_UNSUPPORTED Guided section data is not supported.\r
205 @retval RETURN_INVALID_PARAMETER The input data can't be parsed correctly.\r
206\r
207**/\r
208RETURN_STATUS\r
209EFIAPI\r
210ExtractGuidedSectionGetInfo (\r
211 IN CONST VOID *InputSection,\r
212 OUT UINT32 *OutputBufferSize,\r
213 OUT UINT32 *ScratchBufferSize,\r
214 OUT UINT16 *SectionAttribute \r
215 )\r
216{\r
217 UINT32 Index;\r
218 EFI_STATUS Status;\r
219 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
220 \r
221 //\r
222 // Check input paramter\r
223 //\r
224 if (InputSection == NULL) {\r
225 return RETURN_INVALID_PARAMETER;\r
226 }\r
227 \r
228 ASSERT (OutputBufferSize != NULL);\r
229 ASSERT (ScratchBufferSize != NULL);\r
230 ASSERT (SectionAttribute != NULL);\r
231\r
232 //\r
233 // Get the registered handler information.\r
234 //\r
235 Status = PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
236 if (EFI_ERROR (Status)) {\r
237 return Status;\r
238 }\r
239 \r
240 //\r
241 // Search the match registered GetInfo handler for the input guided section.\r
242 //\r
243 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index ++) {\r
244 if (CompareGuid (&(HandlerInfo->ExtractHandlerGuidTable[Index]), &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
245 break;\r
246 }\r
247 }\r
248\r
249 //\r
250 // Not found, the input guided section is not supported. \r
251 //\r
252 if (Index == HandlerInfo->NumberOfExtractHandler) {\r
253 return RETURN_UNSUPPORTED;\r
254 }\r
255\r
256 //\r
257 // Call the match handler to getinfo for the input section data.\r
258 //\r
259 return HandlerInfo->ExtractGetInfoHandlerTable [Index] (\r
260 InputSection,\r
261 OutputBufferSize,\r
262 ScratchBufferSize,\r
263 SectionAttribute\r
264 );\r
265}\r
266\r
267/**
268 Extract data from the guided section. This function first gets the guid value\r
269 from guided section header, then match this guid in the registered extract Handler list\r
270 to its corresponding extract Handler. \r
271 If not found, RETURN_UNSUPPORTED will be return. \r
272 If found, it will call this extract Handler to get output data and AuthenticationStatus.
273\r
274 It will ASSERT () if the pointer to OutputBuffer is NULL.\r
275 It will ASSERT () if the pointer to AuthenticationStatus is NULL.\r
276\r
277 @param[in] InputSection Buffer containing the input GUIDed section to be processed. \r
278 @param[out] OutputBuffer OutputBuffer to point the start of the section's contents \r
279 if guided data is not required prcessing. Otherwise,\r
280 OutputBuffer to contain the output data, which is \r
281 allocated by the caller.\r
282 @param[out] ScratchBuffer A pointer to a caller-allocated buffer for function internal use. \r
283 @param[out] AuthenticationStatus \r
284 A pointer to a caller-allocated UINT32 that indicates the\r
285 authentication status of the output buffer.
286
287 @retval RETURN_SUCCESS Get the output data, size and AuthenticationStatus successfully.\r
288 @retval RETURN_UNSUPPORTED Guided section data is not supported to be decoded.\r
289 @retval RETURN_INVALID_PARAMETER The input data can't be parsed correctly.\r
290
291**/\r
292RETURN_STATUS\r
293EFIAPI\r
294ExtractGuidedSectionDecode (\r
295 IN CONST VOID *InputSection,\r
296 OUT VOID **OutputBuffer,\r
297 OUT VOID *ScratchBuffer, OPTIONAL\r
298 OUT UINT32 *AuthenticationStatus \r
299 )\r
300{\r
301 UINT32 Index;\r
302 EFI_STATUS Status;\r
303 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
304 \r
305 if (InputSection == NULL) {\r
306 return RETURN_INVALID_PARAMETER;\r
307 }\r
308 \r
309 ASSERT (OutputBuffer != NULL);\r
310 ASSERT (AuthenticationStatus != NULL);\r
311 \r
312 Status = PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
313 if (EFI_ERROR (Status)) {\r
314 return Status;\r
315 }\r
316\r
317 //\r
318 // Search the match registered GetInfo handler for the input guided section.\r
319 //\r
320 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index ++) {\r
321 if (CompareGuid (&(HandlerInfo->ExtractHandlerGuidTable[Index]), &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
322 break;\r
323 }\r
324 }\r
325\r
326 //\r
327 // Not found, the input guided section is not supported. \r
328 //\r
329 if (Index == HandlerInfo->NumberOfExtractHandler) {\r
330 return RETURN_UNSUPPORTED;\r
331 }\r
332\r
333 //\r
334 // Call the match handler to getinfo for the input section data.\r
335 //\r
336 return HandlerInfo->ExtractDecodeHandlerTable [Index] (\r
337 InputSection,\r
338 OutputBuffer,\r
339 ScratchBuffer,\r
340 AuthenticationStatus\r
341 );\r
342}\r