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