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