]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/PeiExtractGuidedSectionLib/PeiExtractGuidedSectionLib.c
Fixed the bug when set mode value is less than the Max mode value.
[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
e2701217 154 UINT32 Index;\r
0fa00159
LG
155 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
156\r
157 //\r
158 // Check input paramter.\r
159 //\r
160 if (SectionGuid == NULL) {\r
161 return RETURN_INVALID_PARAMETER;\r
162 }\r
163\r
164 //\r
165 // Get the registered handler information\r
166 //\r
167 Status = PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
168 if (EFI_ERROR (Status)) {\r
169 return Status;\r
170 }\r
e2701217
LG
171\r
172 //\r
173 // Search the match registered GetInfo handler for the input guided section.\r
174 //\r
175 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index ++) {\r
176 if (CompareGuid (&(HandlerInfo->ExtractHandlerGuidTable[Index]), SectionGuid)) {\r
177 break;\r
178 }\r
179 }\r
180\r
181 //\r
182 // If the guided handler has been registered before, only update its handler.\r
183 //\r
184 if (Index < HandlerInfo->NumberOfExtractHandler) {\r
185 HandlerInfo->ExtractDecodeHandlerTable [Index] = DecodeHandler;\r
186 HandlerInfo->ExtractGetInfoHandlerTable [Index] = GetInfoHandler;\r
187 return RETURN_SUCCESS;\r
188 }\r
189\r
0fa00159
LG
190 //\r
191 // Check the global table is enough to contain new Handler.\r
192 //\r
193 if (HandlerInfo->NumberOfExtractHandler >= PcdGet32 (PcdMaximumGuidedExtractHandler)) {\r
194 return RETURN_OUT_OF_RESOURCES;\r
195 }\r
196 \r
197 //\r
198 // Register new Handler and guid value.\r
199 //\r
200 CopyGuid (&(HandlerInfo->ExtractHandlerGuidTable [HandlerInfo->NumberOfExtractHandler]), SectionGuid);\r
201 HandlerInfo->ExtractDecodeHandlerTable [HandlerInfo->NumberOfExtractHandler] = DecodeHandler;\r
202 HandlerInfo->ExtractGetInfoHandlerTable [HandlerInfo->NumberOfExtractHandler++] = GetInfoHandler;\r
203 \r
204 return RETURN_SUCCESS;\r
205}\r
206\r
207/**
208 Get information from the guided section. This function first gets the guid value\r
209 from guided section header, then match this guid in the registered extract Handler list\r
210 to its corresponding getinfo Handler. \r
e6c560aa 211 If not found, RETURN_INVALID_PARAMETER will be return. \r
0fa00159
LG
212 If found, it will call the getinfo Handler to get the required size and attribute.\r
213\r
214 It will ASSERT () if the pointer to OutputBufferSize is NULL.\r
215 It will ASSERT () if the pointer to ScratchBufferSize is NULL.
216 It will ASSERT () if the pointer to SectionAttribute is NULL.\r
217\r
218 @param[in] InputSection Buffer containing the input GUIDed section to be processed. \r
219 @param[out] OutputBufferSize The size of OutputBuffer.\r
220 @param[out] ScratchBufferSize The size of ScratchBuffer. \r
221 @param[out] SectionAttribute The attribute of the input guided section.\r
222
223 @retval RETURN_SUCCESS Get the required information successfully.\r
e6c560aa
LG
224 @retval RETURN_INVALID_PARAMETER The input data can't be parsed correctly. \r
225 The GUID in InputSection does not match any registered guid list.\r
0fa00159
LG
226\r
227**/\r
228RETURN_STATUS\r
229EFIAPI\r
230ExtractGuidedSectionGetInfo (\r
231 IN CONST VOID *InputSection,\r
232 OUT UINT32 *OutputBufferSize,\r
233 OUT UINT32 *ScratchBufferSize,\r
234 OUT UINT16 *SectionAttribute \r
235 )\r
236{\r
237 UINT32 Index;\r
238 EFI_STATUS Status;\r
239 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
240 \r
241 //\r
242 // Check input paramter\r
243 //\r
244 if (InputSection == NULL) {\r
245 return RETURN_INVALID_PARAMETER;\r
246 }\r
247 \r
248 ASSERT (OutputBufferSize != NULL);\r
249 ASSERT (ScratchBufferSize != NULL);\r
250 ASSERT (SectionAttribute != NULL);\r
251\r
252 //\r
253 // Get the registered handler information.\r
254 //\r
255 Status = PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
256 if (EFI_ERROR (Status)) {\r
257 return Status;\r
258 }\r
259 \r
260 //\r
261 // Search the match registered GetInfo handler for the input guided section.\r
262 //\r
263 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index ++) {\r
264 if (CompareGuid (&(HandlerInfo->ExtractHandlerGuidTable[Index]), &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
265 break;\r
266 }\r
267 }\r
268\r
269 //\r
270 // Not found, the input guided section is not supported. \r
271 //\r
272 if (Index == HandlerInfo->NumberOfExtractHandler) {\r
e6c560aa 273 return RETURN_INVALID_PARAMETER;\r
0fa00159
LG
274 }\r
275\r
276 //\r
277 // Call the match handler to getinfo for the input section data.\r
278 //\r
279 return HandlerInfo->ExtractGetInfoHandlerTable [Index] (\r
280 InputSection,\r
281 OutputBufferSize,\r
282 ScratchBufferSize,\r
283 SectionAttribute\r
284 );\r
285}\r
286\r
287/**
288 Extract data from the guided section. This function first gets the guid value\r
289 from guided section header, then match this guid in the registered extract Handler list\r
290 to its corresponding extract Handler. \r
e6c560aa 291 If not found, RETURN_INVALID_PARAMETER will be return. \r
0fa00159
LG
292 If found, it will call this extract Handler to get output data and AuthenticationStatus.
293\r
294 It will ASSERT () if the pointer to OutputBuffer is NULL.\r
295 It will ASSERT () if the pointer to AuthenticationStatus is NULL.\r
296\r
297 @param[in] InputSection Buffer containing the input GUIDed section to be processed. \r
298 @param[out] OutputBuffer OutputBuffer to point the start of the section's contents \r
299 if guided data is not required prcessing. Otherwise,\r
300 OutputBuffer to contain the output data, which is \r
301 allocated by the caller.\r
302 @param[out] ScratchBuffer A pointer to a caller-allocated buffer for function internal use. \r
303 @param[out] AuthenticationStatus \r
304 A pointer to a caller-allocated UINT32 that indicates the\r
305 authentication status of the output buffer.
306
307 @retval RETURN_SUCCESS Get the output data, size and AuthenticationStatus successfully.\r
e6c560aa
LG
308 @retval RETURN_INVALID_PARAMETER The input data can't be parsed correctly. \r
309 The GUID in InputSection does not match any registered guid list.\r
0fa00159
LG
310
311**/\r
312RETURN_STATUS\r
313EFIAPI\r
314ExtractGuidedSectionDecode (\r
315 IN CONST VOID *InputSection,\r
316 OUT VOID **OutputBuffer,\r
317 OUT VOID *ScratchBuffer, OPTIONAL\r
318 OUT UINT32 *AuthenticationStatus \r
319 )\r
320{\r
321 UINT32 Index;\r
322 EFI_STATUS Status;\r
323 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
324 \r
325 if (InputSection == NULL) {\r
326 return RETURN_INVALID_PARAMETER;\r
327 }\r
328 \r
329 ASSERT (OutputBuffer != NULL);\r
330 ASSERT (AuthenticationStatus != NULL);\r
331 \r
332 Status = PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
333 if (EFI_ERROR (Status)) {\r
334 return Status;\r
335 }\r
336\r
337 //\r
338 // Search the match registered GetInfo handler for the input guided section.\r
339 //\r
340 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index ++) {\r
341 if (CompareGuid (&(HandlerInfo->ExtractHandlerGuidTable[Index]), &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
342 break;\r
343 }\r
344 }\r
345\r
346 //\r
347 // Not found, the input guided section is not supported. \r
348 //\r
349 if (Index == HandlerInfo->NumberOfExtractHandler) {\r
e6c560aa 350 return RETURN_INVALID_PARAMETER;\r
0fa00159
LG
351 }\r
352\r
353 //\r
354 // Call the match handler to getinfo for the input section data.\r
355 //\r
356 return HandlerInfo->ExtractDecodeHandlerTable [Index] (\r
357 InputSection,\r
358 OutputBuffer,\r
359 ScratchBuffer,\r
360 AuthenticationStatus\r
361 );\r
362}\r