MdeModulePkg SectionExtractionPei: remove the hard code alignment adjustment
[mirror_edk2.git] / MdeModulePkg / Universal / SectionExtractionPei / SectionExtractionPei.c
CommitLineData
a402e129
MK
1/** @file\r
2 Section Extraction PEIM\r
3\r
bc6a7a27 4Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>\r
a402e129
MK
5This program and the accompanying materials \r
6are licensed and made available under the terms and conditions of the BSD License \r
7which accompanies this distribution. The full text of the license may be found at \r
8http://opensource.org/licenses/bsd-license.php \r
9 \r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
12\r
13**/\r
14\r
15#include <PiPei.h>\r
16#include <Ppi/GuidedSectionExtraction.h>\r
17#include <Library/DebugLib.h>\r
18#include <Library/ExtractGuidedSectionLib.h>\r
19#include <Library/MemoryAllocationLib.h>\r
20#include <Library/PeiServicesLib.h>\r
21\r
d5eea98e
ED
22/**\r
23 The ExtractSection() function processes the input section and\r
24 returns a pointer to the section contents. If the section being\r
25 extracted does not require processing (if the section\r
26 GuidedSectionHeader.Attributes has the\r
27 EFI_GUIDED_SECTION_PROCESSING_REQUIRED field cleared), then\r
28 OutputBuffer is just updated to point to the start of the\r
29 section's contents. Otherwise, *Buffer must be allocated\r
30 from PEI permanent memory.\r
31\r
32 @param This Indicates the\r
33 EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI instance.\r
34 Buffer containing the input GUIDed section to be\r
35 processed. OutputBuffer OutputBuffer is\r
36 allocated from PEI permanent memory and contains\r
37 the new section stream.\r
38 @param InputSection A pointer to the input buffer, which contains\r
39 the input section to be processed.\r
40 @param OutputBuffer A pointer to a caller-allocated buffer, whose\r
41 size is specified by the contents of OutputSize.\r
42 @param OutputSize A pointer to a caller-allocated\r
43 UINTN in which the size of *OutputBuffer\r
44 allocation is stored. If the function\r
45 returns anything other than EFI_SUCCESS,\r
46 the value of OutputSize is undefined.\r
47 @param AuthenticationStatus A pointer to a caller-allocated\r
48 UINT32 that indicates the\r
49 authentication status of the\r
50 output buffer. If the input\r
51 section's GuidedSectionHeader.\r
52 Attributes field has the\r
53 EFI_GUIDED_SECTION_AUTH_STATUS_VALID \r
54 bit as clear,\r
55 AuthenticationStatus must return\r
56 zero. These bits reflect the\r
57 status of the extraction\r
58 operation. If the function\r
59 returns anything other than\r
60 EFI_SUCCESS, the value of\r
61 AuthenticationStatus is\r
62 undefined.\r
63 \r
64 @retval EFI_SUCCESS The InputSection was\r
65 successfully processed and the\r
66 section contents were returned.\r
67 \r
68 @retval EFI_OUT_OF_RESOURCES The system has insufficient\r
69 resources to process the request.\r
70 \r
71 @retval EFI_INVALID_PARAMETER The GUID in InputSection does\r
72 not match this instance of the\r
73 GUIDed Section Extraction PPI.\r
74\r
75**/\r
a402e129
MK
76EFI_STATUS\r
77EFIAPI\r
78CustomGuidedSectionExtract (\r
79 IN CONST EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI *This,\r
80 IN CONST VOID *InputSection,\r
81 OUT VOID **OutputBuffer,\r
82 OUT UINTN *OutputSize,\r
83 OUT UINT32 *AuthenticationStatus\r
84 );\r
85\r
86//\r
87// Module global for the Section Extraction PPI instance\r
88//\r
89CONST EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI mCustomGuidedSectionExtractionPpi = {\r
90 CustomGuidedSectionExtract\r
91};\r
92\r
93/**\r
94 The ExtractSection() function processes the input section and\r
95 returns a pointer to the section contents. If the section being\r
96 extracted does not require processing (if the section\r
97 GuidedSectionHeader.Attributes has the\r
98 EFI_GUIDED_SECTION_PROCESSING_REQUIRED field cleared), then\r
99 OutputBuffer is just updated to point to the start of the\r
100 section's contents. Otherwise, *Buffer must be allocated\r
101 from PEI permanent memory.\r
102\r
103 @param This Indicates the\r
104 EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI instance.\r
105 Buffer containing the input GUIDed section to be\r
106 processed. OutputBuffer OutputBuffer is\r
107 allocated from PEI permanent memory and contains\r
108 the new section stream.\r
109 @param InputSection A pointer to the input buffer, which contains\r
110 the input section to be processed.\r
111 @param OutputBuffer A pointer to a caller-allocated buffer, whose\r
112 size is specified by the contents of OutputSize.\r
113 @param OutputSize A pointer to a caller-allocated\r
114 UINTN in which the size of *OutputBuffer\r
115 allocation is stored. If the function\r
116 returns anything other than EFI_SUCCESS,\r
117 the value of OutputSize is undefined.\r
118 @param AuthenticationStatus A pointer to a caller-allocated\r
119 UINT32 that indicates the\r
120 authentication status of the\r
121 output buffer. If the input\r
122 section's GuidedSectionHeader.\r
123 Attributes field has the\r
124 EFI_GUIDED_SECTION_AUTH_STATUS_VALID \r
125 bit as clear,\r
126 AuthenticationStatus must return\r
127 zero. These bits reflect the\r
128 status of the extraction\r
129 operation. If the function\r
130 returns anything other than\r
131 EFI_SUCCESS, the value of\r
132 AuthenticationStatus is\r
133 undefined.\r
134 \r
135 @retval EFI_SUCCESS The InputSection was\r
136 successfully processed and the\r
137 section contents were returned.\r
138 \r
139 @retval EFI_OUT_OF_RESOURCES The system has insufficient\r
140 resources to process the request.\r
141 \r
142 @retval EFI_INVALID_PARAMETER The GUID in InputSection does\r
143 not match this instance of the\r
144 GUIDed Section Extraction PPI.\r
145\r
146**/\r
147EFI_STATUS\r
148EFIAPI\r
149CustomGuidedSectionExtract (\r
150 IN CONST EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI *This,\r
151 IN CONST VOID *InputSection,\r
152 OUT VOID **OutputBuffer,\r
153 OUT UINTN *OutputSize,\r
154 OUT UINT32 *AuthenticationStatus\r
155 )\r
156{\r
157 EFI_STATUS Status;\r
158 UINT8 *ScratchBuffer;\r
159 UINT32 ScratchBufferSize;\r
160 UINT32 OutputBufferSize;\r
161 UINT16 SectionAttribute;\r
162 \r
163 //\r
164 // Init local variable\r
165 //\r
166 ScratchBuffer = NULL;\r
167\r
168 //\r
169 // Call GetInfo to get the size and attribute of input guided section data.\r
170 //\r
171 Status = ExtractGuidedSectionGetInfo (\r
172 InputSection,\r
173 &OutputBufferSize,\r
174 &ScratchBufferSize,\r
175 &SectionAttribute\r
176 );\r
177 \r
178 if (EFI_ERROR (Status)) {\r
179 DEBUG ((DEBUG_ERROR, "GetInfo from guided section Failed - %r\n", Status));\r
180 return Status;\r
181 }\r
182 \r
183 if (ScratchBufferSize != 0) {\r
184 //\r
185 // Allocate scratch buffer\r
186 //\r
187 ScratchBuffer = AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize));\r
188 if (ScratchBuffer == NULL) {\r
189 return EFI_OUT_OF_RESOURCES;\r
190 }\r
191 }\r
192\r
193 if (((SectionAttribute & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) != 0) && OutputBufferSize > 0) { \r
194 //\r
195 // Allocate output buffer\r
196 //\r
bc6a7a27 197 *OutputBuffer = AllocatePages (EFI_SIZE_TO_PAGES (OutputBufferSize));\r
a402e129
MK
198 if (*OutputBuffer == NULL) {\r
199 return EFI_OUT_OF_RESOURCES;\r
200 }\r
201 DEBUG ((DEBUG_INFO, "Customized Guided section Memory Size required is 0x%x and address is 0x%p\n", OutputBufferSize, *OutputBuffer));\r
a402e129
MK
202 }\r
203 \r
204 Status = ExtractGuidedSectionDecode (\r
205 InputSection, \r
206 OutputBuffer,\r
207 ScratchBuffer,\r
208 AuthenticationStatus\r
209 );\r
210 if (EFI_ERROR (Status)) {\r
211 //\r
212 // Decode failed\r
213 //\r
214 DEBUG ((DEBUG_ERROR, "Extract guided section Failed - %r\n", Status));\r
215 return Status;\r
216 }\r
217 \r
218 *OutputSize = (UINTN) OutputBufferSize;\r
219 \r
220 return EFI_SUCCESS;\r
221}\r
222\r
223/**\r
224 Main entry for Section Extraction PEIM driver.\r
225 \r
226 This routine registers the Section Extraction PPIs that have been registered \r
227 with the Section Extraction Library.\r
228\r
229 @param FileHandle Handle of the file being invoked.\r
230 @param PeiServices Describes the list of possible PEI Services.\r
231\r
232 @retval EFI_SUCCESS The entry point is executed successfully.\r
233 @retval other Some error occurs when executing this entry point.\r
234\r
235**/\r
236EFI_STATUS\r
237EFIAPI\r
238SectionExtractionPeiEntry (\r
239 IN EFI_PEI_FILE_HANDLE FileHandle,\r
240 IN CONST EFI_PEI_SERVICES **PeiServices\r
241 ) \r
242{\r
243 EFI_STATUS Status;\r
244 EFI_GUID *ExtractHandlerGuidTable;\r
245 UINTN ExtractHandlerNumber;\r
246 EFI_PEI_PPI_DESCRIPTOR *GuidPpi;\r
247\r
248 //\r
249 // Get custom extract guided section method guid list \r
250 //\r
251 ExtractHandlerNumber = ExtractGuidedSectionGetGuidList (&ExtractHandlerGuidTable);\r
252 \r
253 //\r
254 // Install custom extraction guid PPI\r
255 //\r
256 if (ExtractHandlerNumber > 0) {\r
257 GuidPpi = (EFI_PEI_PPI_DESCRIPTOR *) AllocatePool (ExtractHandlerNumber * sizeof (EFI_PEI_PPI_DESCRIPTOR));\r
258 ASSERT (GuidPpi != NULL);\r
259 while (ExtractHandlerNumber-- > 0) {\r
260 GuidPpi->Flags = EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST;\r
261 GuidPpi->Ppi = (VOID *) &mCustomGuidedSectionExtractionPpi;\r
262 GuidPpi->Guid = &ExtractHandlerGuidTable[ExtractHandlerNumber];\r
263 Status = PeiServicesInstallPpi (GuidPpi++);\r
264 ASSERT_EFI_ERROR (Status);\r
265 }\r
266 }\r
267 \r
268 return EFI_SUCCESS;\r
269}\r