]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/PeiExtractGuidedSectionLib/PeiExtractGuidedSectionLib.c
43a1a3c22ea48623a3baf3e9ddcf4dc8a9e076b8
[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 //
200 // If the guided handler has been registered before, only update its handler.
201 //
202 HandlerInfo->ExtractDecodeHandlerTable [Index] = DecodeHandler;
203 HandlerInfo->ExtractGetInfoHandlerTable [Index] = GetInfoHandler;
204 return RETURN_SUCCESS;
205 }
206 }
207
208 //
209 // Check the global table is enough to contain new Handler.
210 //
211 if (HandlerInfo->NumberOfExtractHandler >= PcdGet32 (PcdMaximumGuidedExtractHandler)) {
212 return RETURN_OUT_OF_RESOURCES;
213 }
214
215 //
216 // Register new Handler and guid value.
217 //
218 CopyGuid (HandlerInfo->ExtractHandlerGuidTable + HandlerInfo->NumberOfExtractHandler, SectionGuid);
219 HandlerInfo->ExtractDecodeHandlerTable [HandlerInfo->NumberOfExtractHandler] = DecodeHandler;
220 HandlerInfo->ExtractGetInfoHandlerTable [HandlerInfo->NumberOfExtractHandler++] = GetInfoHandler;
221
222 return RETURN_SUCCESS;
223 }
224
225 /**
226 Get information from the guided section. This function first gets the guid value
227 from guided section header, then match this guid in the registered extract Handler list
228 to its corresponding getinfo Handler.
229 If not found, RETURN_INVALID_PARAMETER will be return.
230 If found, it will call the getinfo Handler to get the required size and attribute.
231
232 It will ASSERT () if the pointer to OutputBufferSize is NULL.
233 It will ASSERT () if the pointer to ScratchBufferSize is NULL.
234 It will ASSERT () if the pointer to SectionAttribute is NULL.
235
236 @param[in] InputSection Buffer containing the input GUIDed section to be processed.
237 @param[out] OutputBufferSize The size of OutputBuffer.
238 @param[out] ScratchBufferSize The size of ScratchBuffer.
239 @param[out] SectionAttribute The attribute of the input guided section.
240
241 @retval RETURN_SUCCESS Get the required information successfully.
242 @retval RETURN_UNSUPPORTED Guided section data is not supported.
243 @retval RETURN_INVALID_PARAMETER The input data is not the valid guided section.
244
245 **/
246 RETURN_STATUS
247 EFIAPI
248 ExtractGuidedSectionGetInfo (
249 IN CONST VOID *InputSection,
250 OUT UINT32 *OutputBufferSize,
251 OUT UINT32 *ScratchBufferSize,
252 OUT UINT16 *SectionAttribute
253 )
254 {
255 UINT32 Index;
256 EFI_STATUS Status;
257 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;
258
259 //
260 // Check input paramter
261 //
262 if (InputSection == NULL) {
263 return RETURN_INVALID_PARAMETER;
264 }
265
266 ASSERT (OutputBufferSize != NULL);
267 ASSERT (ScratchBufferSize != NULL);
268 ASSERT (SectionAttribute != NULL);
269
270 //
271 // Get all registered handler information.
272 //
273 Status = PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo);
274 if (EFI_ERROR (Status)) {
275 return Status;
276 }
277
278 //
279 // Search the match registered GetInfo handler for the input guided section.
280 //
281 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index ++) {
282 if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
283 //
284 // Call the match handler to getinfo for the input section data.
285 //
286 return HandlerInfo->ExtractGetInfoHandlerTable [Index] (
287 InputSection,
288 OutputBufferSize,
289 ScratchBufferSize,
290 SectionAttribute
291 );
292 }
293 }
294
295 //
296 // Not found, the input guided section is not supported.
297 //
298 return RETURN_UNSUPPORTED;
299 }
300
301 /**
302 Extract data from the guided section. This function first gets the guid value
303 from guided section header, then match this guid in the registered extract Handler list
304 to its corresponding extract Handler.
305 If not found, RETURN_INVALID_PARAMETER will be return.
306 If found, it will call this extract Handler to get output data and AuthenticationStatus.
307
308 It will ASSERT () if the pointer to OutputBuffer is NULL.
309 It will ASSERT () if the pointer to AuthenticationStatus is NULL.
310
311 @param[in] InputSection Buffer containing the input GUIDed section to be processed.
312 @param[out] OutputBuffer OutputBuffer to point the start of the section's contents
313 if guided data is not required prcessing. Otherwise,
314 OutputBuffer to contain the output data, which is
315 allocated by the caller.
316 @param[out] ScratchBuffer A pointer to a caller-allocated buffer for function internal use.
317 @param[out] AuthenticationStatus
318 A pointer to a caller-allocated UINT32 that indicates the
319 authentication status of the output buffer.
320
321 @retval RETURN_SUCCESS Get the output data, size and AuthenticationStatus successfully.
322 @retval RETURN_UNSUPPORTED Guided section data is not supported to be decoded.
323 @retval RETURN_INVALID_PARAMETER The input data is not the valid guided section.
324
325 **/
326 RETURN_STATUS
327 EFIAPI
328 ExtractGuidedSectionDecode (
329 IN CONST VOID *InputSection,
330 OUT VOID **OutputBuffer,
331 OUT VOID *ScratchBuffer, OPTIONAL
332 OUT UINT32 *AuthenticationStatus
333 )
334 {
335 UINT32 Index;
336 EFI_STATUS Status;
337 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;
338
339 //
340 // Check input parameter
341 //
342 if (InputSection == NULL) {
343 return RETURN_INVALID_PARAMETER;
344 }
345 ASSERT (OutputBuffer != NULL);
346 ASSERT (AuthenticationStatus != NULL);
347
348 //
349 // Get all registered handler information.
350 //
351 Status = PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo);
352 if (EFI_ERROR (Status)) {
353 return Status;
354 }
355
356 //
357 // Search the match registered Extract handler for the input guided section.
358 //
359 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index ++) {
360 if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
361 //
362 // Call the match handler to extract raw data for the input guided section.
363 //
364 return HandlerInfo->ExtractDecodeHandlerTable [Index] (
365 InputSection,
366 OutputBuffer,
367 ScratchBuffer,
368 AuthenticationStatus
369 );
370 }
371 }
372
373 //
374 // Not found, the input guided section is not supported.
375 //
376 return RETURN_UNSUPPORTED;
377 }