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