]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/SectionExtractionDxe/SectionExtractionDxe.c
Contributed-under: TianoCore Contribution Agreement 1.0
[mirror_edk2.git] / MdeModulePkg / Universal / SectionExtractionDxe / SectionExtractionDxe.c
1 /** @file
2 Section Extraction DXE Driver
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 <PiDxe.h>
16 #include <Protocol/GuidedSectionExtraction.h>
17 #include <Library/DebugLib.h>
18 #include <Library/ExtractGuidedSectionLib.h>
19 #include <Library/MemoryAllocationLib.h>
20 #include <Library/BaseMemoryLib.h>
21 #include <Library/UefiBootServicesTableLib.h>
22
23 //
24 // Function prototype for Section Extraction Protocol service
25 //
26 EFI_STATUS
27 EFIAPI
28 CustomGuidedSectionExtract (
29 IN CONST EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *This,
30 IN CONST VOID *InputSection,
31 OUT VOID **OutputBuffer,
32 OUT UINTN *OutputSize,
33 OUT UINT32 *AuthenticationStatus
34 );
35
36 //
37 // Module global for the Section Extraction Protocol handle
38 //
39 EFI_HANDLE mSectionExtractionHandle = NULL;
40
41 //
42 // Module global for the Section Extraction Protocol instance
43 //
44 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL mCustomGuidedSectionExtractionProtocol = {
45 CustomGuidedSectionExtract
46 };
47
48 /**
49 The ExtractSection() function processes the input section and
50 allocates a buffer from the pool in which it returns the section
51 contents. If the section being extracted contains
52 authentication information (the section's
53 GuidedSectionHeader.Attributes field has the
54 EFI_GUIDED_SECTION_AUTH_STATUS_VALID bit set), the values
55 returned in AuthenticationStatus must reflect the results of
56 the authentication operation. Depending on the algorithm and
57 size of the encapsulated data, the time that is required to do
58 a full authentication may be prohibitively long for some
59 classes of systems. To indicate this, use
60 EFI_SECURITY_POLICY_PROTOCOL_GUID, which may be published by
61 the security policy driver (see the Platform Initialization
62 Driver Execution Environment Core Interface Specification for
63 more details and the GUID definition). If the
64 EFI_SECURITY_POLICY_PROTOCOL_GUID exists in the handle
65 database, then, if possible, full authentication should be
66 skipped and the section contents simply returned in the
67 OutputBuffer. In this case, the
68 EFI_AUTH_STATUS_PLATFORM_OVERRIDE bit AuthenticationStatus
69 must be set on return. ExtractSection() is callable only from
70 TPL_NOTIFY and below. Behavior of ExtractSection() at any
71 EFI_TPL above TPL_NOTIFY is undefined. Type EFI_TPL is
72 defined in RaiseTPL() in the UEFI 2.0 specification.
73
74
75 @param This Indicates the
76 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL instance.
77 @param InputSection Buffer containing the input GUIDed section
78 to be processed. OutputBuffer OutputBuffer
79 is allocated from boot services pool
80 memory and contains the new section
81 stream. The caller is responsible for
82 freeing this buffer.
83 @param OutputBuffer *OutputBuffer is allocated from boot services
84 pool memory and contains the new section stream.
85 The caller is responsible for freeing this buffer.
86 @param OutputSize A pointer to a caller-allocated UINTN in
87 which the size of OutputBuffer allocation
88 is stored. If the function returns
89 anything other than EFI_SUCCESS, the value
90 of OutputSize is undefined.
91
92 @param AuthenticationStatus A pointer to a caller-allocated
93 UINT32 that indicates the
94 authentication status of the
95 output buffer. If the input
96 section's
97 GuidedSectionHeader.Attributes
98 field has the
99 EFI_GUIDED_SECTION_AUTH_STATUS_VAL
100 bit as clear, AuthenticationStatus
101 must return zero. Both local bits
102 (19:16) and aggregate bits (3:0)
103 in AuthenticationStatus are
104 returned by ExtractSection().
105 These bits reflect the status of
106 the extraction operation. The bit
107 pattern in both regions must be
108 the same, as the local and
109 aggregate authentication statuses
110 have equivalent meaning at this
111 level. If the function returns
112 anything other than EFI_SUCCESS,
113 the value of AuthenticationStatus
114 is undefined.
115
116
117 @retval EFI_SUCCESS The InputSection was successfully
118 processed and the section contents were
119 returned.
120
121 @retval EFI_OUT_OF_RESOURCES The system has insufficient
122 resources to process the
123 request.
124
125 @retval EFI_INVALID_PARAMETER The GUID in InputSection does
126 not match this instance of the
127 GUIDed Section Extraction
128 Protocol.
129
130 **/
131 EFI_STATUS
132 EFIAPI
133 CustomGuidedSectionExtract (
134 IN CONST EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *This,
135 IN CONST VOID *InputSection,
136 OUT VOID **OutputBuffer,
137 OUT UINTN *OutputSize,
138 OUT UINT32 *AuthenticationStatus
139 )
140 {
141 EFI_STATUS Status;
142 VOID *ScratchBuffer;
143 VOID *AllocatedOutputBuffer;
144 UINT32 OutputBufferSize;
145 UINT32 ScratchBufferSize;
146 UINT16 SectionAttribute;
147
148 //
149 // Init local variable
150 //
151 ScratchBuffer = NULL;
152 AllocatedOutputBuffer = NULL;
153
154 //
155 // Call GetInfo to get the size and attribute of input guided section data.
156 //
157 Status = ExtractGuidedSectionGetInfo (
158 InputSection,
159 &OutputBufferSize,
160 &ScratchBufferSize,
161 &SectionAttribute
162 );
163
164 if (EFI_ERROR (Status)) {
165 DEBUG ((DEBUG_ERROR, "GetInfo from guided section Failed - %r\n", Status));
166 return Status;
167 }
168
169 if (ScratchBufferSize > 0) {
170 //
171 // Allocate scratch buffer
172 //
173 ScratchBuffer = AllocatePool (ScratchBufferSize);
174 if (ScratchBuffer == NULL) {
175 return EFI_OUT_OF_RESOURCES;
176 }
177 }
178
179 if (OutputBufferSize > 0) {
180 //
181 // Allocate output buffer
182 //
183 AllocatedOutputBuffer = AllocatePool (OutputBufferSize);
184 if (AllocatedOutputBuffer == NULL) {
185 FreePool (ScratchBuffer);
186 return EFI_OUT_OF_RESOURCES;
187 }
188 *OutputBuffer = AllocatedOutputBuffer;
189 }
190
191 //
192 // Call decode function to extract raw data from the guided section.
193 //
194 Status = ExtractGuidedSectionDecode (
195 InputSection,
196 OutputBuffer,
197 ScratchBuffer,
198 AuthenticationStatus
199 );
200 if (EFI_ERROR (Status)) {
201 //
202 // Decode failed
203 //
204 if (AllocatedOutputBuffer != NULL) {
205 FreePool (AllocatedOutputBuffer);
206 }
207 if (ScratchBuffer != NULL) {
208 FreePool (ScratchBuffer);
209 }
210 DEBUG ((DEBUG_ERROR, "Extract guided section Failed - %r\n", Status));
211 return Status;
212 }
213
214 if (*OutputBuffer != AllocatedOutputBuffer) {
215 //
216 // OutputBuffer was returned as a different value,
217 // so copy section contents to the allocated memory buffer.
218 //
219 CopyMem (AllocatedOutputBuffer, *OutputBuffer, OutputBufferSize);
220 *OutputBuffer = AllocatedOutputBuffer;
221 }
222
223 //
224 // Set real size of output buffer.
225 //
226 *OutputSize = (UINTN) OutputBufferSize;
227
228 //
229 // Free unused scratch buffer.
230 //
231 if (ScratchBuffer != NULL) {
232 FreePool (ScratchBuffer);
233 }
234
235 return EFI_SUCCESS;
236 }
237
238 /**
239 Main entry for the Section Extraction DXE module.
240
241 This routine registers the Section Extraction Protocols that have been registered
242 with the Section Extraction Library.
243
244 @param[in] ImageHandle The firmware allocated handle for the EFI image.
245 @param[in] SystemTable A pointer to the EFI System Table.
246
247 @retval EFI_SUCCESS The entry point is executed successfully.
248 @retval other Some error occurs when executing this entry point.
249
250 **/
251 EFI_STATUS
252 EFIAPI
253 SectionExtractionDxeEntry (
254 IN EFI_HANDLE ImageHandle,
255 IN EFI_SYSTEM_TABLE *SystemTable
256 )
257 {
258 EFI_STATUS Status;
259 EFI_GUID *ExtractHandlerGuidTable;
260 UINTN ExtractHandlerNumber;
261
262 //
263 // Get custom extract guided section method guid list
264 //
265 ExtractHandlerNumber = ExtractGuidedSectionGetGuidList (&ExtractHandlerGuidTable);
266
267 //
268 // Install custom guided extraction protocol
269 //
270 while (ExtractHandlerNumber-- > 0) {
271 Status = gBS->InstallMultipleProtocolInterfaces (
272 &mSectionExtractionHandle,
273 &ExtractHandlerGuidTable [ExtractHandlerNumber], &mCustomGuidedSectionExtractionProtocol,
274 NULL
275 );
276 ASSERT_EFI_ERROR (Status);
277 }
278
279 return EFI_SUCCESS;
280 }