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