Adjust directory structures.
[mirror_edk2.git] / MdeModulePkg / Universal / FirmwareVolume / Crc32SectionExtractDxe / Crc32SectionExtract.c
1 /*++
2
3 Copyright (c) 2006 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 Crc32SectionExtract.c
15
16 Abstract:
17
18 Implements GUIDed section extraction protocol interface with
19 a specific GUID: CRC32.
20
21 Please refer to the Framewokr Firmware Volume Specification 0.9.
22
23 --*/
24
25
26 #include <Crc32SectionExtract.h>
27
28 EFI_STATUS
29 GuidedSectionExtractionProtocolConstructor (
30 OUT EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL **GuidedSep,
31 IN EFI_EXTRACT_GUIDED_SECTION ExtractSection
32 )
33 /*++
34
35 Routine Description:
36
37 Constructor for the GUIDed section extraction protocol. Initializes
38 instance data.
39
40 Arguments:
41
42 This Instance to construct
43
44 Returns:
45
46 EFI_SUCCESS: Instance initialized.
47
48 --*/
49 // TODO: GuidedSep - add argument and description to function comment
50 // TODO: ExtractSection - add argument and description to function comment
51 // TODO: EFI_OUT_OF_RESOURCES - add return value to function comment
52 {
53 *GuidedSep = AllocatePool (sizeof (EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL));
54 if (*GuidedSep == NULL) {
55 return EFI_OUT_OF_RESOURCES;
56 }
57
58 (*GuidedSep)->ExtractSection = ExtractSection;
59
60 return EFI_SUCCESS;
61 }
62
63
64 EFI_STATUS
65 EFIAPI
66 InitializeCrc32GuidedSectionExtractionProtocol (
67 IN EFI_HANDLE ImageHandle,
68 IN EFI_SYSTEM_TABLE *SystemTable
69 )
70 /*++
71
72 Routine Description:
73
74 Entry point of the CRC32 GUIDed section extraction protocol.
75 Creates and initializes an instance of the GUIDed section
76 extraction protocol with CRC32 GUID.
77
78 Arguments:
79
80 ImageHandle EFI_HANDLE: A handle for the image that is initializing
81 this driver
82 SystemTable EFI_SYSTEM_TABLE: A pointer to the EFI system table
83
84 Returns:
85
86 EFI_SUCCESS: Driver initialized successfully
87 EFI_LOAD_ERROR: Failed to Initialize or has been loaded
88 EFI_OUT_OF_RESOURCES: Could not allocate needed resources
89
90 --*/
91 {
92 EFI_STATUS Status;
93 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *Crc32GuidedSep;
94 EFI_HANDLE Handle;
95
96 //
97 // Call all constructors per produced protocols
98 //
99 Status = GuidedSectionExtractionProtocolConstructor (
100 &Crc32GuidedSep,
101 (EFI_EXTRACT_GUIDED_SECTION) Crc32ExtractSection
102 );
103 if (EFI_ERROR (Status)) {
104 if (Crc32GuidedSep != NULL) {
105 FreePool (Crc32GuidedSep);
106 }
107
108 return Status;
109 }
110 //
111 // Pass in a NULL to install to a new handle
112 //
113 Handle = NULL;
114 Status = gBS->InstallProtocolInterface (
115 &Handle,
116 &gEfiCrc32GuidedSectionExtractionProtocolGuid,
117 EFI_NATIVE_INTERFACE,
118 Crc32GuidedSep
119 );
120 if (EFI_ERROR (Status)) {
121 FreePool (Crc32GuidedSep);
122 return EFI_LOAD_ERROR;
123 }
124
125 return EFI_SUCCESS;
126 }
127
128 STATIC
129 UINT32
130 EFIAPI
131 GetSectionLength (
132 IN EFI_COMMON_SECTION_HEADER *CommonHeader
133 )
134 /*++
135
136 Routine Description:
137 Get a length of section.
138
139 Parameters:
140 CommonHeader - Pointer to the common section header.
141
142 Return Value:
143 The length of the section, including the section header.
144
145 --*/
146 // TODO: function comment is missing 'Arguments:'
147 // TODO: function comment is missing 'Returns:'
148 // TODO: CommonHeader - add argument and description to function comment
149 {
150 UINT32 Size;
151
152 Size = *(UINT32 *) CommonHeader->Size & 0x00FFFFFF;
153
154 return Size;
155 }
156
157 STATIC
158 EFI_STATUS
159 EFIAPI
160 Crc32ExtractSection (
161 IN EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *This,
162 IN VOID *InputSection,
163 OUT VOID **OutputBuffer,
164 OUT UINTN *OutputSize,
165 OUT UINT32 *AuthenticationStatus
166 )
167 /*++
168
169 Routine Description:
170 This function reads and extracts contents of a section from an
171 encapsulating section.
172
173 Parameters:
174 This - Indicates the calling context.
175 InputSection - Buffer containing the input GUIDed section
176 to be processed.
177 OutputBuffer - *OutputBuffer is allocated from boot services
178 pool memory and containing the new section
179 stream. The caller is responsible for freeing
180 this buffer.
181 AuthenticationStatus - Pointer to a caller allocated UINT32 that
182 indicates the authentication status of the
183 output buffer
184
185 Return Value:
186 EFI_SUCCESS
187 EFI_OUT_OF_RESOURCES
188 EFI_INVALID_PARAMETER
189 EFI_NOT_AVAILABLE_YET
190
191 --*/
192 // TODO: function comment is missing 'Arguments:'
193 // TODO: function comment is missing 'Returns:'
194 // TODO: This - add argument and description to function comment
195 // TODO: InputSection - add argument and description to function comment
196 // TODO: OutputBuffer - add argument and description to function comment
197 // TODO: OutputSize - add argument and description to function comment
198 // TODO: AuthenticationStatus - add argument and description to function comment
199 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
200 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
201 // TODO: EFI_OUT_OF_RESOURCES - add return value to function comment
202 // TODO: EFI_SUCCESS - add return value to function comment
203 {
204 EFI_STATUS Status;
205 CRC32_SECTION_HEADER *Crc32SectionHeader;
206 EFI_GUID_DEFINED_SECTION *GuidedSectionHeader;
207 UINT8 *Image;
208 UINT32 Crc32Checksum;
209 VOID *DummyInterface;
210
211 if (OutputBuffer == NULL) {
212 return EFI_INVALID_PARAMETER;
213 }
214
215 *OutputBuffer = NULL;
216
217 //
218 // Points to the section header
219 //
220 Crc32SectionHeader = (CRC32_SECTION_HEADER *) InputSection;
221 GuidedSectionHeader = (EFI_GUID_DEFINED_SECTION *) InputSection;
222
223 //
224 // Check if the GUID is a CRC32 section GUID
225 //
226 if (!CompareGuid (
227 &(GuidedSectionHeader->SectionDefinitionGuid),
228 &gEfiCrc32GuidedSectionExtractionProtocolGuid
229 )) {
230 return EFI_INVALID_PARAMETER;
231 }
232
233 Image = (UINT8 *) InputSection + (UINT32) (GuidedSectionHeader->DataOffset);
234 *OutputSize = GetSectionLength ((EFI_COMMON_SECTION_HEADER *) InputSection) - (UINT32) GuidedSectionHeader->DataOffset;
235
236 *OutputBuffer = AllocatePool (*OutputSize);
237 if (*OutputBuffer == NULL) {
238 return EFI_OUT_OF_RESOURCES;
239 }
240 //
241 // Implictly CRC32 GUIDed section should have STATUS_VALID bit set
242 //
243 ASSERT (GuidedSectionHeader->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);
244 *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;
245
246 //
247 // Check whether there exists EFI_SECURITY_POLICY_PROTOCOL_GUID.
248 //
249 Status = gBS->LocateProtocol (&gEfiSecurityPolicyProtocolGuid, NULL, &DummyInterface);
250 if (!EFI_ERROR (Status)) {
251 *AuthenticationStatus |= EFI_AUTH_STATUS_PLATFORM_OVERRIDE;
252 } else {
253 //
254 // Calculate CRC32 Checksum of Image
255 //
256 gBS->CalculateCrc32 (Image, *OutputSize, &Crc32Checksum);
257 if (Crc32Checksum != Crc32SectionHeader->CRC32Checksum) {
258 *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;
259 }
260 }
261
262 CopyMem (*OutputBuffer, Image, *OutputSize);
263
264 return EFI_SUCCESS;
265 }