]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/SectionExtractionPei/SectionExtractionPei.c
MdeModulePkg: Clean up source files
[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
d1102dba
LG
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
a402e129
MK
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
d1102dba 53 EFI_GUIDED_SECTION_AUTH_STATUS_VALID\r
d5eea98e
ED
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
d1102dba 63\r
d5eea98e
ED
64 @retval EFI_SUCCESS The InputSection was\r
65 successfully processed and the\r
66 section contents were returned.\r
d1102dba 67\r
d5eea98e
ED
68 @retval EFI_OUT_OF_RESOURCES The system has insufficient\r
69 resources to process the request.\r
d1102dba 70\r
d5eea98e
ED
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
d1102dba 124 EFI_GUIDED_SECTION_AUTH_STATUS_VALID\r
a402e129
MK
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
d1102dba 134\r
a402e129
MK
135 @retval EFI_SUCCESS The InputSection was\r
136 successfully processed and the\r
137 section contents were returned.\r
d1102dba 138\r
a402e129
MK
139 @retval EFI_OUT_OF_RESOURCES The system has insufficient\r
140 resources to process the request.\r
d1102dba 141\r
a402e129
MK
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
d1102dba 162\r
a402e129
MK
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
d1102dba 177\r
a402e129
MK
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
d1102dba 182\r
a402e129
MK
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
d1102dba 193 if (((SectionAttribute & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) != 0) && OutputBufferSize > 0) {\r
a402e129
MK
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 202 }\r
d1102dba 203\r
a402e129 204 Status = ExtractGuidedSectionDecode (\r
d1102dba 205 InputSection,\r
a402e129
MK
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
d1102dba 217\r
a402e129 218 *OutputSize = (UINTN) OutputBufferSize;\r
d1102dba 219\r
a402e129
MK
220 return EFI_SUCCESS;\r
221}\r
222\r
223/**\r
224 Main entry for Section Extraction PEIM driver.\r
d1102dba
LG
225\r
226 This routine registers the Section Extraction PPIs that have been registered\r
a402e129
MK
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
d1102dba 241 )\r
a402e129
MK
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
d1102dba 249 // Get custom extract guided section method guid list\r
a402e129
MK
250 //\r
251 ExtractHandlerNumber = ExtractGuidedSectionGetGuidList (&ExtractHandlerGuidTable);\r
d1102dba 252\r
a402e129
MK
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
d1102dba 267\r
a402e129
MK
268 return EFI_SUCCESS;\r
269}\r