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