2 Section Extraction PEIM
4 Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <Ppi/GuidedSectionExtraction.h>
11 #include <Library/DebugLib.h>
12 #include <Library/ExtractGuidedSectionLib.h>
13 #include <Library/MemoryAllocationLib.h>
14 #include <Library/PeiServicesLib.h>
17 The ExtractSection() function processes the input section and
18 returns a pointer to the section contents. If the section being
19 extracted does not require processing (if the section
20 GuidedSectionHeader.Attributes has the
21 EFI_GUIDED_SECTION_PROCESSING_REQUIRED field cleared), then
22 OutputBuffer is just updated to point to the start of the
23 section's contents. Otherwise, *Buffer must be allocated
24 from PEI permanent memory.
26 @param This Indicates the
27 EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI instance.
28 Buffer containing the input GUIDed section to be
29 processed. OutputBuffer OutputBuffer is
30 allocated from PEI permanent memory and contains
31 the new section stream.
32 @param InputSection A pointer to the input buffer, which contains
33 the input section to be processed.
34 @param OutputBuffer A pointer to a caller-allocated buffer, whose
35 size is specified by the contents of OutputSize.
36 @param OutputSize A pointer to a caller-allocated
37 UINTN in which the size of *OutputBuffer
38 allocation is stored. If the function
39 returns anything other than EFI_SUCCESS,
40 the value of OutputSize is undefined.
41 @param AuthenticationStatus A pointer to a caller-allocated
42 UINT32 that indicates the
43 authentication status of the
44 output buffer. If the input
45 section's GuidedSectionHeader.
46 Attributes field has the
47 EFI_GUIDED_SECTION_AUTH_STATUS_VALID
49 AuthenticationStatus must return
50 zero. These bits reflect the
51 status of the extraction
52 operation. If the function
53 returns anything other than
54 EFI_SUCCESS, the value of
55 AuthenticationStatus is
58 @retval EFI_SUCCESS The InputSection was
59 successfully processed and the
60 section contents were returned.
62 @retval EFI_OUT_OF_RESOURCES The system has insufficient
63 resources to process the request.
65 @retval EFI_INVALID_PARAMETER The GUID in InputSection does
66 not match this instance of the
67 GUIDed Section Extraction PPI.
72 CustomGuidedSectionExtract (
73 IN CONST EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI
*This
,
74 IN CONST VOID
*InputSection
,
75 OUT VOID
**OutputBuffer
,
76 OUT UINTN
*OutputSize
,
77 OUT UINT32
*AuthenticationStatus
81 // Module global for the Section Extraction PPI instance
83 CONST EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI mCustomGuidedSectionExtractionPpi
= {
84 CustomGuidedSectionExtract
88 The ExtractSection() function processes the input section and
89 returns a pointer to the section contents. If the section being
90 extracted does not require processing (if the section
91 GuidedSectionHeader.Attributes has the
92 EFI_GUIDED_SECTION_PROCESSING_REQUIRED field cleared), then
93 OutputBuffer is just updated to point to the start of the
94 section's contents. Otherwise, *Buffer must be allocated
95 from PEI permanent memory.
97 @param This Indicates the
98 EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI instance.
99 Buffer containing the input GUIDed section to be
100 processed. OutputBuffer OutputBuffer is
101 allocated from PEI permanent memory and contains
102 the new section stream.
103 @param InputSection A pointer to the input buffer, which contains
104 the input section to be processed.
105 @param OutputBuffer A pointer to a caller-allocated buffer, whose
106 size is specified by the contents of OutputSize.
107 @param OutputSize A pointer to a caller-allocated
108 UINTN in which the size of *OutputBuffer
109 allocation is stored. If the function
110 returns anything other than EFI_SUCCESS,
111 the value of OutputSize is undefined.
112 @param AuthenticationStatus A pointer to a caller-allocated
113 UINT32 that indicates the
114 authentication status of the
115 output buffer. If the input
116 section's GuidedSectionHeader.
117 Attributes field has the
118 EFI_GUIDED_SECTION_AUTH_STATUS_VALID
120 AuthenticationStatus must return
121 zero. These bits reflect the
122 status of the extraction
123 operation. If the function
124 returns anything other than
125 EFI_SUCCESS, the value of
126 AuthenticationStatus is
129 @retval EFI_SUCCESS The InputSection was
130 successfully processed and the
131 section contents were returned.
133 @retval EFI_OUT_OF_RESOURCES The system has insufficient
134 resources to process the request.
136 @retval EFI_INVALID_PARAMETER The GUID in InputSection does
137 not match this instance of the
138 GUIDed Section Extraction PPI.
143 CustomGuidedSectionExtract (
144 IN CONST EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI
*This
,
145 IN CONST VOID
*InputSection
,
146 OUT VOID
**OutputBuffer
,
147 OUT UINTN
*OutputSize
,
148 OUT UINT32
*AuthenticationStatus
152 UINT8
*ScratchBuffer
;
153 UINT32 ScratchBufferSize
;
154 UINT32 OutputBufferSize
;
155 UINT16 SectionAttribute
;
158 // Init local variable
160 ScratchBuffer
= NULL
;
163 // Call GetInfo to get the size and attribute of input guided section data.
165 Status
= ExtractGuidedSectionGetInfo (
172 if (EFI_ERROR (Status
)) {
173 DEBUG ((DEBUG_ERROR
, "GetInfo from guided section Failed - %r\n", Status
));
177 if (ScratchBufferSize
!= 0) {
179 // Allocate scratch buffer
181 ScratchBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize
));
182 if (ScratchBuffer
== NULL
) {
183 return EFI_OUT_OF_RESOURCES
;
187 if (((SectionAttribute
& EFI_GUIDED_SECTION_PROCESSING_REQUIRED
) != 0) && OutputBufferSize
> 0) {
189 // Allocate output buffer
191 *OutputBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (OutputBufferSize
));
192 if (*OutputBuffer
== NULL
) {
193 return EFI_OUT_OF_RESOURCES
;
195 DEBUG ((DEBUG_INFO
, "Customized Guided section Memory Size required is 0x%x and address is 0x%p\n", OutputBufferSize
, *OutputBuffer
));
198 Status
= ExtractGuidedSectionDecode (
204 if (EFI_ERROR (Status
)) {
208 DEBUG ((DEBUG_ERROR
, "Extract guided section Failed - %r\n", Status
));
212 *OutputSize
= (UINTN
) OutputBufferSize
;
218 Main entry for Section Extraction PEIM driver.
220 This routine registers the Section Extraction PPIs that have been registered
221 with the Section Extraction Library.
223 @param FileHandle Handle of the file being invoked.
224 @param PeiServices Describes the list of possible PEI Services.
226 @retval EFI_SUCCESS The entry point is executed successfully.
227 @retval other Some error occurs when executing this entry point.
232 SectionExtractionPeiEntry (
233 IN EFI_PEI_FILE_HANDLE FileHandle
,
234 IN CONST EFI_PEI_SERVICES
**PeiServices
238 EFI_GUID
*ExtractHandlerGuidTable
;
239 UINTN ExtractHandlerNumber
;
240 EFI_PEI_PPI_DESCRIPTOR
*GuidPpi
;
243 // Get custom extract guided section method guid list
245 ExtractHandlerNumber
= ExtractGuidedSectionGetGuidList (&ExtractHandlerGuidTable
);
248 // Install custom extraction guid PPI
250 if (ExtractHandlerNumber
> 0) {
251 GuidPpi
= (EFI_PEI_PPI_DESCRIPTOR
*) AllocatePool (ExtractHandlerNumber
* sizeof (EFI_PEI_PPI_DESCRIPTOR
));
252 ASSERT (GuidPpi
!= NULL
);
253 while (ExtractHandlerNumber
-- > 0) {
254 GuidPpi
->Flags
= EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
;
255 GuidPpi
->Ppi
= (VOID
*) &mCustomGuidedSectionExtractionPpi
;
256 GuidPpi
->Guid
= &ExtractHandlerGuidTable
[ExtractHandlerNumber
];
257 Status
= PeiServicesInstallPpi (GuidPpi
++);
258 ASSERT_EFI_ERROR (Status
);