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