]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/PeiExtractGuidedSectionLib/PeiExtractGuidedSectionLib.c
Add comments for parameter.
[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
e111752c
LG
66 //\r
67 // Update Table Pointer when hob start address is changed.\r
68 //\r
69 if (HandlerInfo->ExtractHandlerGuidTable != (GUID *) (HandlerInfo + 1)) {\r
70 HandlerInfo->ExtractHandlerGuidTable = (GUID *) (HandlerInfo + 1);\r
71 HandlerInfo->ExtractDecodeHandlerTable = (EXTRACT_GUIDED_SECTION_DECODE_HANDLER *) (\r
72 (UINT8 *)HandlerInfo->ExtractHandlerGuidTable + \r
73 PcdGet32 (PcdMaximumGuidedExtractHandler) * sizeof (GUID)\r
74 );\r
75 HandlerInfo->ExtractGetInfoHandlerTable = (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *) (\r
76 (UINT8 *)HandlerInfo->ExtractDecodeHandlerTable + \r
77 PcdGet32 (PcdMaximumGuidedExtractHandler) * \r
78 sizeof (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER)\r
79 );\r
80 }\r
81 //\r
82 // Return HandlerInfo pointer.\r
83 //\r
0fa00159
LG
84 *InfoPointer = HandlerInfo;\r
85 return EFI_SUCCESS;\r
86 }\r
87 }\r
88 Hob.Raw = GET_NEXT_HOB (Hob);\r
89 Hob.Raw = GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION, Hob.Raw);\r
90 }\r
91 \r
92 //\r
93 // If Guid Hob is not found, Build CallerId Guid hob to store Handler Info\r
94 //\r
95 HandlerInfo = BuildGuidHob (\r
96 &gEfiCallerIdGuid, \r
97 sizeof (PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO) +\r
98 PcdGet32 (PcdMaximumGuidedExtractHandler) * \r
99 (sizeof (GUID) + sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER) + sizeof (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER))\r
100 );\r
101 if (HandlerInfo == NULL) {\r
102 //\r
103 // No enough resource to build guid hob.\r
104 //\r
105 return EFI_OUT_OF_RESOURCES;\r
106 }\r
107 HandlerInfo->Signature = PEI_EXTRACT_HANDLER_INFO_SIGNATURE;\r
108 HandlerInfo->NumberOfExtractHandler = 0;\r
109 HandlerInfo->ExtractHandlerGuidTable = (GUID *) (HandlerInfo + 1);\r
110 HandlerInfo->ExtractDecodeHandlerTable = (EXTRACT_GUIDED_SECTION_DECODE_HANDLER *) (\r
111 (UINT8 *)HandlerInfo->ExtractHandlerGuidTable + \r
112 PcdGet32 (PcdMaximumGuidedExtractHandler) * sizeof (GUID)\r
113 );\r
114 HandlerInfo->ExtractGetInfoHandlerTable = (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *) (\r
115 (UINT8 *)HandlerInfo->ExtractDecodeHandlerTable + \r
116 PcdGet32 (PcdMaximumGuidedExtractHandler) * \r
117 sizeof (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER)\r
118 );\r
119 \r
120 *InfoPointer = HandlerInfo;\r
121 return EFI_SUCCESS;\r
122}\r
123\r
124/**
125 Get the supported exract guided section Handler guid list.\r
126 If ExtractHandlerGuidTable = NULL, then ASSERT.\r
127\r
128 @param[in, out] ExtractHandlerGuidTable The extract Handler guid pointer list.
129\r
130 @retval return the number of the supported extract guided Handler.
131**/\r
132UINTN\r
133EFIAPI\r
134ExtractGuidedSectionGetGuidList (\r
135 IN OUT GUID **ExtractHandlerGuidTable\r
136 )\r
137{\r
138 EFI_STATUS Status;\r
139 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
140\r
141 ASSERT (ExtractHandlerGuidTable != NULL);\r
142\r
143 Status = PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
144 if (EFI_ERROR (Status)) {\r
145 return Status;\r
146 }\r
147\r
148 *ExtractHandlerGuidTable = HandlerInfo->ExtractHandlerGuidTable;\r
149 return HandlerInfo->NumberOfExtractHandler;\r
150}\r
151\r
152/**
153 Register Guided Section Extract and GetInfo handler.\r
154\r
155 @param[in] SectionGuid The guid matches this Extraction function.
156 @param[in] GetInfoHandler Function to get info from guided section.\r
157 @param[in] DecodeHandler Function to extract guided section.
158
159 @retval RETURN_SUCCESS Register Guided Section Extract function successfully.
160 @retval RETURN_OUT_OF_RESOURCES Resource is not enough to register new function. \r
161 @retval RETURN_INVALID_PARAMETER Input pointer to Guid value is not valid.\r
162**/\r
163RETURN_STATUS\r
164EFIAPI\r
165ExtractGuidedSectionRegisterHandlers (\r
166 IN CONST GUID *SectionGuid,\r
167 IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER GetInfoHandler,\r
168 IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER DecodeHandler\r
169 )\r
170{\r
171 EFI_STATUS Status;\r
e2701217 172 UINT32 Index;\r
0fa00159
LG
173 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
174\r
175 //\r
176 // Check input paramter.\r
177 //\r
178 if (SectionGuid == NULL) {\r
179 return RETURN_INVALID_PARAMETER;\r
180 }\r
181\r
182 //\r
183 // Get the registered handler information\r
184 //\r
185 Status = PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
186 if (EFI_ERROR (Status)) {\r
187 return Status;\r
188 }\r
e2701217
LG
189\r
190 //\r
191 // Search the match registered GetInfo handler for the input guided section.\r
192 //\r
193 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index ++) {\r
e111752c 194 if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, SectionGuid)) {\r
e2701217
LG
195 break;\r
196 }\r
197 }\r
198\r
199 //\r
200 // If the guided handler has been registered before, only update its handler.\r
201 //\r
202 if (Index < HandlerInfo->NumberOfExtractHandler) {\r
203 HandlerInfo->ExtractDecodeHandlerTable [Index] = DecodeHandler;\r
204 HandlerInfo->ExtractGetInfoHandlerTable [Index] = GetInfoHandler;\r
205 return RETURN_SUCCESS;\r
206 }\r
207\r
0fa00159
LG
208 //\r
209 // Check the global table is enough to contain new Handler.\r
210 //\r
211 if (HandlerInfo->NumberOfExtractHandler >= PcdGet32 (PcdMaximumGuidedExtractHandler)) {\r
212 return RETURN_OUT_OF_RESOURCES;\r
213 }\r
214 \r
215 //\r
216 // Register new Handler and guid value.\r
217 //\r
e111752c 218 CopyGuid (HandlerInfo->ExtractHandlerGuidTable + HandlerInfo->NumberOfExtractHandler, SectionGuid);\r
0fa00159
LG
219 HandlerInfo->ExtractDecodeHandlerTable [HandlerInfo->NumberOfExtractHandler] = DecodeHandler;\r
220 HandlerInfo->ExtractGetInfoHandlerTable [HandlerInfo->NumberOfExtractHandler++] = GetInfoHandler;\r
221 \r
222 return RETURN_SUCCESS;\r
223}\r
224\r
225/**
226 Get information from the guided section. This function first gets the guid value\r
227 from guided section header, then match this guid in the registered extract Handler list\r
228 to its corresponding getinfo Handler. \r
e6c560aa 229 If not found, RETURN_INVALID_PARAMETER will be return. \r
0fa00159
LG
230 If found, it will call the getinfo Handler to get the required size and attribute.\r
231\r
232 It will ASSERT () if the pointer to OutputBufferSize is NULL.\r
233 It will ASSERT () if the pointer to ScratchBufferSize is NULL.
234 It will ASSERT () if the pointer to SectionAttribute is NULL.\r
235\r
236 @param[in] InputSection Buffer containing the input GUIDed section to be processed. \r
237 @param[out] OutputBufferSize The size of OutputBuffer.\r
238 @param[out] ScratchBufferSize The size of ScratchBuffer. \r
239 @param[out] SectionAttribute The attribute of the input guided section.\r
240
241 @retval RETURN_SUCCESS Get the required information successfully.\r
e6c560aa
LG
242 @retval RETURN_INVALID_PARAMETER The input data can't be parsed correctly. \r
243 The GUID in InputSection does not match any registered guid list.\r
0fa00159
LG
244\r
245**/\r
246RETURN_STATUS\r
247EFIAPI\r
248ExtractGuidedSectionGetInfo (\r
249 IN CONST VOID *InputSection,\r
250 OUT UINT32 *OutputBufferSize,\r
251 OUT UINT32 *ScratchBufferSize,\r
252 OUT UINT16 *SectionAttribute \r
253 )\r
254{\r
255 UINT32 Index;\r
256 EFI_STATUS Status;\r
257 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
258 \r
259 //\r
260 // Check input paramter\r
261 //\r
262 if (InputSection == NULL) {\r
263 return RETURN_INVALID_PARAMETER;\r
264 }\r
265 \r
266 ASSERT (OutputBufferSize != NULL);\r
267 ASSERT (ScratchBufferSize != NULL);\r
268 ASSERT (SectionAttribute != NULL);\r
269\r
270 //\r
271 // Get the registered handler information.\r
272 //\r
273 Status = PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
274 if (EFI_ERROR (Status)) {\r
275 return Status;\r
276 }\r
277 \r
278 //\r
279 // Search the match registered GetInfo handler for the input guided section.\r
280 //\r
281 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index ++) {\r
e111752c 282 if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
0fa00159
LG
283 break;\r
284 }\r
285 }\r
286\r
287 //\r
288 // Not found, the input guided section is not supported. \r
289 //\r
290 if (Index == HandlerInfo->NumberOfExtractHandler) {\r
e6c560aa 291 return RETURN_INVALID_PARAMETER;\r
0fa00159
LG
292 }\r
293\r
294 //\r
295 // Call the match handler to getinfo for the input section data.\r
296 //\r
297 return HandlerInfo->ExtractGetInfoHandlerTable [Index] (\r
298 InputSection,\r
299 OutputBufferSize,\r
300 ScratchBufferSize,\r
301 SectionAttribute\r
302 );\r
303}\r
304\r
305/**
306 Extract data from the guided section. This function first gets the guid value\r
307 from guided section header, then match this guid in the registered extract Handler list\r
308 to its corresponding extract Handler. \r
e6c560aa 309 If not found, RETURN_INVALID_PARAMETER will be return. \r
0fa00159
LG
310 If found, it will call this extract Handler to get output data and AuthenticationStatus.
311\r
312 It will ASSERT () if the pointer to OutputBuffer is NULL.\r
313 It will ASSERT () if the pointer to AuthenticationStatus is NULL.\r
314\r
315 @param[in] InputSection Buffer containing the input GUIDed section to be processed. \r
316 @param[out] OutputBuffer OutputBuffer to point the start of the section's contents \r
317 if guided data is not required prcessing. Otherwise,\r
318 OutputBuffer to contain the output data, which is \r
319 allocated by the caller.\r
320 @param[out] ScratchBuffer A pointer to a caller-allocated buffer for function internal use. \r
321 @param[out] AuthenticationStatus \r
322 A pointer to a caller-allocated UINT32 that indicates the\r
323 authentication status of the output buffer.
324
325 @retval RETURN_SUCCESS Get the output data, size and AuthenticationStatus successfully.\r
e6c560aa
LG
326 @retval RETURN_INVALID_PARAMETER The input data can't be parsed correctly. \r
327 The GUID in InputSection does not match any registered guid list.\r
0fa00159
LG
328
329**/\r
330RETURN_STATUS\r
331EFIAPI\r
332ExtractGuidedSectionDecode (\r
333 IN CONST VOID *InputSection,\r
334 OUT VOID **OutputBuffer,\r
335 OUT VOID *ScratchBuffer, OPTIONAL\r
336 OUT UINT32 *AuthenticationStatus \r
337 )\r
338{\r
339 UINT32 Index;\r
340 EFI_STATUS Status;\r
341 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
342 \r
343 if (InputSection == NULL) {\r
344 return RETURN_INVALID_PARAMETER;\r
345 }\r
346 \r
347 ASSERT (OutputBuffer != NULL);\r
348 ASSERT (AuthenticationStatus != NULL);\r
349 \r
350 Status = PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
351 if (EFI_ERROR (Status)) {\r
352 return Status;\r
353 }\r
354\r
355 //\r
356 // Search the match registered GetInfo handler for the input guided section.\r
357 //\r
358 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index ++) {\r
e111752c 359 if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
0fa00159
LG
360 break;\r
361 }\r
362 }\r
363\r
364 //\r
365 // Not found, the input guided section is not supported. \r
366 //\r
367 if (Index == HandlerInfo->NumberOfExtractHandler) {\r
e6c560aa 368 return RETURN_INVALID_PARAMETER;\r
0fa00159
LG
369 }\r
370\r
371 //\r
372 // Call the match handler to getinfo for the input section data.\r
373 //\r
374 return HandlerInfo->ExtractDecodeHandlerTable [Index] (\r
375 InputSection,\r
376 OutputBuffer,\r
377 ScratchBuffer,\r
378 AuthenticationStatus\r
379 );\r
380}\r