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