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