]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/PeiExtractGuidedSectionLib/PeiExtractGuidedSectionLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <PiPei.h>
10
11 #include <Library/DebugLib.h>
12 #include <Library/PcdLib.h>
13 #include <Library/BaseMemoryLib.h>
14 #include <Library/HobLib.h>
15 #include <Library/ExtractGuidedSectionLib.h>
16
17 #define PEI_EXTRACT_HANDLER_INFO_SIGNATURE SIGNATURE_32 ('P', 'E', 'H', 'I')
18
19 typedef struct {
20 UINT32 Signature;
21 UINT32 NumberOfExtractHandler;
22 GUID *ExtractHandlerGuidTable;
23 EXTRACT_GUIDED_SECTION_DECODE_HANDLER *ExtractDecodeHandlerTable;
24 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *ExtractGetInfoHandlerTable;
25 } PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO;
26
27 /**
28 Build guid hob for the global memory to store the registered guid and Handler list.
29 If GuidHob exists, HandlerInfo will be directly got from Guid hob data.
30
31 @param[in, out] InfoPointer The pointer to pei handler information structure.
32
33 @retval RETURN_SUCCESS Build Guid hob for the global memory space to store guid and function tables.
34 @retval RETURN_OUT_OF_RESOURCES No enough memory to allocated.
35 **/
36 RETURN_STATUS
37 PeiGetExtractGuidedSectionHandlerInfo (
38 IN OUT PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO **InfoPointer
39 )
40 {
41 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;
42 EFI_PEI_HOB_POINTERS Hob;
43
44 //
45 // First try to get handler information from guid hob specified by CallerId.
46 //
47 Hob.Raw = GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION, GetHobList ());
48 while (Hob.Raw != NULL) {
49 if (CompareGuid (&(Hob.Guid->Name), &gEfiCallerIdGuid)) {
50 HandlerInfo = (PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO *)GET_GUID_HOB_DATA (Hob.Guid);
51 if (HandlerInfo->Signature == PEI_EXTRACT_HANDLER_INFO_SIGNATURE) {
52 //
53 // Update Table Pointer when hob start address is changed.
54 //
55 if (HandlerInfo->ExtractHandlerGuidTable != (GUID *)(HandlerInfo + 1)) {
56 HandlerInfo->ExtractHandlerGuidTable = (GUID *)(HandlerInfo + 1);
57 HandlerInfo->ExtractDecodeHandlerTable = (EXTRACT_GUIDED_SECTION_DECODE_HANDLER *)(
58 (UINT8 *)HandlerInfo->ExtractHandlerGuidTable +
59 PcdGet32 (PcdMaximumGuidedExtractHandler) * sizeof (GUID)
60 );
61 HandlerInfo->ExtractGetInfoHandlerTable = (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *)(
62 (UINT8 *)HandlerInfo->ExtractDecodeHandlerTable +
63 PcdGet32 (PcdMaximumGuidedExtractHandler) *
64 sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER)
65 );
66 }
67
68 //
69 // Return HandlerInfo pointer.
70 //
71 *InfoPointer = HandlerInfo;
72 return EFI_SUCCESS;
73 }
74 }
75
76 Hob.Raw = GET_NEXT_HOB (Hob);
77 Hob.Raw = GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION, Hob.Raw);
78 }
79
80 //
81 // If Guid Hob is not found, Build CallerId Guid hob to store Handler Info
82 //
83 HandlerInfo = BuildGuidHob (
84 &gEfiCallerIdGuid,
85 sizeof (PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO) +
86 PcdGet32 (PcdMaximumGuidedExtractHandler) *
87 (sizeof (GUID) + sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER) + sizeof (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER))
88 );
89 if (HandlerInfo == NULL) {
90 //
91 // No enough resource to build guid hob.
92 //
93 *InfoPointer = NULL;
94 return EFI_OUT_OF_RESOURCES;
95 }
96
97 //
98 // Init HandlerInfo structure
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_DECODE_HANDLER)
111 );
112 //
113 // return the created HandlerInfo.
114 //
115 *InfoPointer = HandlerInfo;
116 return EFI_SUCCESS;
117 }
118
119 /**
120 Retrieve the list GUIDs that have been registered through ExtractGuidedSectionRegisterHandlers().
121
122 Sets ExtractHandlerGuidTable so it points at a callee allocated array of registered GUIDs.
123 The total number of GUIDs in the array are returned. Since the array of GUIDs is callee allocated
124 and caller must treat this array of GUIDs as read-only data.
125 If ExtractHandlerGuidTable is NULL, then ASSERT().
126
127 @param[out] ExtractHandlerGuidTable A pointer to the array of GUIDs that have been registered through
128 ExtractGuidedSectionRegisterHandlers().
129
130 @return the number of the supported extract guided Handler.
131
132 **/
133 UINTN
134 EFIAPI
135 ExtractGuidedSectionGetGuidList (
136 OUT GUID **ExtractHandlerGuidTable
137 )
138 {
139 EFI_STATUS Status;
140 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;
141
142 ASSERT (ExtractHandlerGuidTable != NULL);
143
144 //
145 // Get all registered handler information
146 //
147 Status = PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo);
148 if (EFI_ERROR (Status)) {
149 *ExtractHandlerGuidTable = NULL;
150 return 0;
151 }
152
153 //
154 // Get GuidTable and Table Number
155 //
156 ASSERT (HandlerInfo != NULL);
157 *ExtractHandlerGuidTable = HandlerInfo->ExtractHandlerGuidTable;
158 return HandlerInfo->NumberOfExtractHandler;
159 }
160
161 /**
162 Registers handlers of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER and EXTRACT_GUIDED_SECTION_DECODE_HANDLER
163 for a specific GUID section type.
164
165 Registers the handlers specified by GetInfoHandler and DecodeHandler with the GUID specified by SectionGuid.
166 If the GUID value specified by SectionGuid has already been registered, then return RETURN_ALREADY_STARTED.
167 If there are not enough resources available to register the handlers then RETURN_OUT_OF_RESOURCES is returned.
168
169 If SectionGuid is NULL, then ASSERT().
170 If GetInfoHandler is NULL, then ASSERT().
171 If DecodeHandler is NULL, then ASSERT().
172
173 @param[in] SectionGuid A pointer to the GUID associated with the the handlers
174 of the GUIDed section type being registered.
175 @param[in] GetInfoHandler The pointer to a function that examines a GUIDed section and returns the
176 size of the decoded buffer and the size of an optional scratch buffer
177 required to actually decode the data in a GUIDed section.
178 @param[in] DecodeHandler The pointer to a function that decodes a GUIDed section into a caller
179 allocated output buffer.
180
181 @retval RETURN_SUCCESS The handlers were registered.
182 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to register the handlers.
183
184 **/
185 RETURN_STATUS
186 EFIAPI
187 ExtractGuidedSectionRegisterHandlers (
188 IN CONST GUID *SectionGuid,
189 IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER GetInfoHandler,
190 IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER DecodeHandler
191 )
192 {
193 EFI_STATUS Status;
194 UINT32 Index;
195 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;
196
197 //
198 // Check input parameter
199 //
200 ASSERT (SectionGuid != NULL);
201 ASSERT (GetInfoHandler != NULL);
202 ASSERT (DecodeHandler != NULL);
203
204 //
205 // Get the registered handler information
206 //
207 Status = PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo);
208 if (EFI_ERROR (Status)) {
209 return Status;
210 }
211
212 //
213 // Search the match registered GetInfo handler for the input guided section.
214 //
215 ASSERT (HandlerInfo != NULL);
216 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index++) {
217 if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, SectionGuid)) {
218 //
219 // If the guided handler has been registered before, only update its handler.
220 //
221 HandlerInfo->ExtractDecodeHandlerTable[Index] = DecodeHandler;
222 HandlerInfo->ExtractGetInfoHandlerTable[Index] = GetInfoHandler;
223 return RETURN_SUCCESS;
224 }
225 }
226
227 //
228 // Check the global table is enough to contain new Handler.
229 //
230 if (HandlerInfo->NumberOfExtractHandler >= PcdGet32 (PcdMaximumGuidedExtractHandler)) {
231 return RETURN_OUT_OF_RESOURCES;
232 }
233
234 //
235 // Register new Handler and guid value.
236 //
237 CopyGuid (HandlerInfo->ExtractHandlerGuidTable + HandlerInfo->NumberOfExtractHandler, SectionGuid);
238 HandlerInfo->ExtractDecodeHandlerTable[HandlerInfo->NumberOfExtractHandler] = DecodeHandler;
239 HandlerInfo->ExtractGetInfoHandlerTable[HandlerInfo->NumberOfExtractHandler++] = GetInfoHandler;
240
241 //
242 // Build the Guided Section GUID HOB to record the GUID itself.
243 // Then the content of the GUIDed HOB will be the same as the GUID value itself.
244 //
245 BuildGuidDataHob (
246 (EFI_GUID *)SectionGuid,
247 (VOID *)SectionGuid,
248 sizeof (GUID)
249 );
250
251 return RETURN_SUCCESS;
252 }
253
254 /**
255 Retrieves a GUID from a GUIDed section and uses that GUID to select an associated handler of type
256 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().
257 The selected handler is used to retrieve and return the size of the decoded buffer and the size of an
258 optional scratch buffer required to actually decode the data in a GUIDed section.
259
260 Examines a GUIDed section specified by InputSection.
261 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),
262 then RETURN_UNSUPPORTED is returned.
263 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler
264 of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()
265 is used to retrieve the OututBufferSize, ScratchSize, and Attributes values. The return status from the handler of
266 type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER is returned.
267
268 If InputSection is NULL, then ASSERT().
269 If OutputBufferSize is NULL, then ASSERT().
270 If ScratchBufferSize is NULL, then ASSERT().
271 If SectionAttribute is NULL, then ASSERT().
272
273 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
274 @param[out] OutputBufferSize A pointer to the size, in bytes, of an output buffer required if the buffer
275 specified by InputSection were decoded.
276 @param[out] ScratchBufferSize A pointer to the size, in bytes, required as scratch space if the buffer specified by
277 InputSection were decoded.
278 @param[out] SectionAttribute A pointer to the attributes of the GUIDed section. See the Attributes field of
279 EFI_GUID_DEFINED_SECTION in the PI Specification.
280
281 @retval RETURN_SUCCESS Get the required information successfully.
282 @retval RETURN_UNSUPPORTED The GUID from the section specified by InputSection does not match any of
283 the GUIDs registered with ExtractGuidedSectionRegisterHandlers().
284 @retval Others The return status from the handler associated with the GUID retrieved from
285 the section specified by InputSection.
286
287 **/
288 RETURN_STATUS
289 EFIAPI
290 ExtractGuidedSectionGetInfo (
291 IN CONST VOID *InputSection,
292 OUT UINT32 *OutputBufferSize,
293 OUT UINT32 *ScratchBufferSize,
294 OUT UINT16 *SectionAttribute
295 )
296 {
297 UINT32 Index;
298 EFI_STATUS Status;
299 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;
300 EFI_GUID *SectionDefinitionGuid;
301
302 //
303 // Check input parameter
304 //
305 ASSERT (InputSection != NULL);
306 ASSERT (OutputBufferSize != NULL);
307 ASSERT (ScratchBufferSize != NULL);
308 ASSERT (SectionAttribute != NULL);
309
310 //
311 // Get all registered handler information.
312 //
313 Status = PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo);
314 if (EFI_ERROR (Status)) {
315 return Status;
316 }
317
318 if (IS_SECTION2 (InputSection)) {
319 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION2 *)InputSection)->SectionDefinitionGuid);
320 } else {
321 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION *)InputSection)->SectionDefinitionGuid);
322 }
323
324 //
325 // Search the match registered GetInfo handler for the input guided section.
326 //
327 ASSERT (HandlerInfo != NULL);
328 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index++) {
329 if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, SectionDefinitionGuid)) {
330 //
331 // Call the match handler to get information for the input section data.
332 //
333 return HandlerInfo->ExtractGetInfoHandlerTable[Index](
334 InputSection,
335 OutputBufferSize,
336 ScratchBufferSize,
337 SectionAttribute
338 );
339 }
340 }
341
342 //
343 // Not found, the input guided section is not supported.
344 //
345 return RETURN_UNSUPPORTED;
346 }
347
348 /**
349 Retrieves the GUID from a GUIDed section and uses that GUID to select an associated handler of type
350 EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().
351 The selected handler is used to decode the data in a GUIDed section and return the result in a caller
352 allocated output buffer.
353
354 Decodes the GUIDed section specified by InputSection.
355 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),
356 then RETURN_UNSUPPORTED is returned.
357 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler
358 of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()
359 is used to decode InputSection into the buffer specified by OutputBuffer and the authentication status of this
360 decode operation is returned in AuthenticationStatus. If the decoded buffer is identical to the data in InputSection,
361 then OutputBuffer is set to point at the data in InputSection. Otherwise, the decoded data will be placed in caller
362 allocated buffer specified by OutputBuffer. This function is responsible for computing the EFI_AUTH_STATUS_PLATFORM_OVERRIDE
363 bit of in AuthenticationStatus. The return status from the handler of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER is returned.
364
365 If InputSection is NULL, then ASSERT().
366 If OutputBuffer is NULL, then ASSERT().
367 If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().
368 If AuthenticationStatus is NULL, then ASSERT().
369
370 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
371 @param[out] OutputBuffer A pointer to a buffer that contains the result of a decode operation.
372 @param[in] ScratchBuffer A caller allocated buffer that may be required by this function as a scratch buffer to perform the decode operation.
373 @param[out] AuthenticationStatus
374 A pointer to the authentication status of the decoded output buffer. See the definition
375 of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI section of the PI
376 Specification.
377
378 @retval RETURN_SUCCESS The buffer specified by InputSection was decoded.
379 @retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports.
380 @retval RETURN_INVALID_PARAMETER The section specified by InputSection can not be decoded.
381
382 **/
383 RETURN_STATUS
384 EFIAPI
385 ExtractGuidedSectionDecode (
386 IN CONST VOID *InputSection,
387 OUT VOID **OutputBuffer,
388 IN VOID *ScratchBuffer OPTIONAL,
389 OUT UINT32 *AuthenticationStatus
390 )
391 {
392 UINT32 Index;
393 EFI_STATUS Status;
394 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;
395 EFI_GUID *SectionDefinitionGuid;
396
397 //
398 // Check input parameter
399 //
400 ASSERT (InputSection != NULL);
401 ASSERT (OutputBuffer != NULL);
402 ASSERT (AuthenticationStatus != NULL);
403
404 //
405 // Get all registered handler information.
406 //
407 Status = PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo);
408 if (EFI_ERROR (Status)) {
409 return Status;
410 }
411
412 if (IS_SECTION2 (InputSection)) {
413 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION2 *)InputSection)->SectionDefinitionGuid);
414 } else {
415 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION *)InputSection)->SectionDefinitionGuid);
416 }
417
418 //
419 // Search the match registered Extract handler for the input guided section.
420 //
421 ASSERT (HandlerInfo != NULL);
422 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index++) {
423 if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, SectionDefinitionGuid)) {
424 //
425 // Call the match handler to extract raw data for the input guided section.
426 //
427 return HandlerInfo->ExtractDecodeHandlerTable[Index](
428 InputSection,
429 OutputBuffer,
430 ScratchBuffer,
431 AuthenticationStatus
432 );
433 }
434 }
435
436 //
437 // Not found, the input guided section is not supported.
438 //
439 return RETURN_UNSUPPORTED;
440 }
441
442 /**
443 Retrieves handlers of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER and
444 EXTRACT_GUIDED_SECTION_DECODE_HANDLER for a specific GUID section type.
445
446 Retrieves the handlers associated with SectionGuid and returns them in
447 GetInfoHandler and DecodeHandler.
448
449 If the GUID value specified by SectionGuid has not been registered, then
450 return RETURN_NOT_FOUND.
451
452 If SectionGuid is NULL, then ASSERT().
453
454 @param[in] SectionGuid A pointer to the GUID associated with the handlersof the GUIDed
455 section type being retrieved.
456 @param[out] GetInfoHandler Pointer to a function that examines a GUIDed section and returns
457 the size of the decoded buffer and the size of an optional scratch
458 buffer required to actually decode the data in a GUIDed section.
459 This is an optional parameter that may be NULL. If it is NULL, then
460 the previously registered handler is not returned.
461 @param[out] DecodeHandler Pointer to a function that decodes a GUIDed section into a caller
462 allocated output buffer. This is an optional parameter that may be NULL.
463 If it is NULL, then the previously registered handler is not returned.
464
465 @retval RETURN_SUCCESS The handlers were retrieved.
466 @retval RETURN_NOT_FOUND No handlers have been registered with the specified GUID.
467
468 **/
469 RETURN_STATUS
470 EFIAPI
471 ExtractGuidedSectionGetHandlers (
472 IN CONST GUID *SectionGuid,
473 OUT EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *GetInfoHandler OPTIONAL,
474 OUT EXTRACT_GUIDED_SECTION_DECODE_HANDLER *DecodeHandler OPTIONAL
475 )
476 {
477 EFI_STATUS Status;
478 UINT32 Index;
479 PEI_EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;
480
481 //
482 // Check input parameter
483 //
484 ASSERT (SectionGuid != NULL);
485
486 //
487 // Get the registered handler information
488 //
489 Status = PeiGetExtractGuidedSectionHandlerInfo (&HandlerInfo);
490 if (EFI_ERROR (Status)) {
491 return Status;
492 }
493
494 //
495 // Search the match registered GetInfo handler for the input guided section.
496 //
497 ASSERT (HandlerInfo != NULL);
498 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index++) {
499 if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, SectionGuid)) {
500 //
501 // If the guided handler has been registered before, then return the registered handlers.
502 //
503 if (GetInfoHandler != NULL) {
504 *GetInfoHandler = HandlerInfo->ExtractGetInfoHandlerTable[Index];
505 }
506
507 if (DecodeHandler != NULL) {
508 *DecodeHandler = HandlerInfo->ExtractDecodeHandlerTable[Index];
509 }
510
511 return RETURN_SUCCESS;
512 }
513 }
514
515 return RETURN_NOT_FOUND;
516 }