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