]> git.proxmox.com Git - mirror_edk2.git/blob - EdkModulePkg/Universal/FirmwareVolume/GuidedSectionExtraction/Crc32SectionExtract/Dxe/Crc32SectionExtract.c
143836be8209b2ec363e57eeb0ffc1b38996e38e
[mirror_edk2.git] / EdkModulePkg / Universal / FirmwareVolume / GuidedSectionExtraction / Crc32SectionExtract / Dxe / Crc32SectionExtract.c
1 /*++
2
3 Copyright (c) 2006, 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 Tiano File Image Format Specification,
22 FV spec 0.3.6
23
24 --*/
25
26
27 #include <GuidedSection.h>
28 #include <Crc32SectionExtract.h>
29
30 EFI_STATUS
31 EFIAPI
32 InitializeCrc32GuidedSectionExtractionProtocol (
33 IN EFI_HANDLE ImageHandle,
34 IN EFI_SYSTEM_TABLE *SystemTable
35 );
36
37 EFI_STATUS
38 EFIAPI
39 InitializeCrc32GuidedSectionExtractionProtocol (
40 IN EFI_HANDLE ImageHandle,
41 IN EFI_SYSTEM_TABLE *SystemTable
42 )
43 /*++
44
45 Routine Description:
46
47 Entry point of the CRC32 GUIDed section extraction protocol.
48 Creates and initializes an instance of the GUIDed section
49 extraction protocol with CRC32 GUID.
50
51 Arguments:
52
53 ImageHandle EFI_HANDLE: A handle for the image that is initializing
54 this driver
55 SystemTable EFI_SYSTEM_TABLE: A pointer to the EFI system table
56
57 Returns:
58
59 EFI_SUCCESS: Driver initialized successfully
60 EFI_LOAD_ERROR: Failed to Initialize or has been loaded
61 EFI_OUT_OF_RESOURCES: Could not allocate needed resources
62
63 --*/
64 {
65 EFI_STATUS Status;
66 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *Crc32GuidedSep;
67 EFI_HANDLE Handle;
68
69 //
70 // Call all constructors per produced protocols
71 //
72 Status = GuidedSectionExtractionProtocolConstructor (
73 &Crc32GuidedSep,
74 (EFI_EXTRACT_GUIDED_SECTION) Crc32ExtractSection
75 );
76 if (EFI_ERROR (Status)) {
77 if (Crc32GuidedSep != NULL) {
78 gBS->FreePool (Crc32GuidedSep);
79 }
80
81 return Status;
82 }
83 //
84 // Pass in a NULL to install to a new handle
85 //
86 Handle = NULL;
87 Status = gBS->InstallProtocolInterface (
88 &Handle,
89 &gEfiCrc32GuidedSectionExtractionProtocolGuid,
90 EFI_NATIVE_INTERFACE,
91 Crc32GuidedSep
92 );
93 if (EFI_ERROR (Status)) {
94 gBS->FreePool (Crc32GuidedSep);
95 return EFI_LOAD_ERROR;
96 }
97
98 return EFI_SUCCESS;
99 }
100
101 STATIC
102 UINT32
103 EFIAPI
104 GetSectionLength (
105 IN EFI_COMMON_SECTION_HEADER *CommonHeader
106 )
107 /*++
108
109 Routine Description:
110 Get a length of section.
111
112 Parameters:
113 CommonHeader - Pointer to the common section header.
114
115 Return Value:
116 The length of the section, including the section header.
117
118 --*/
119 // TODO: function comment is missing 'Arguments:'
120 // TODO: function comment is missing 'Returns:'
121 // TODO: CommonHeader - add argument and description to function comment
122 {
123 UINT32 Size;
124
125 Size = *(UINT32 *) CommonHeader->Size & 0x00FFFFFF;
126
127 return Size;
128 }
129
130 STATIC
131 EFI_STATUS
132 EFIAPI
133 Crc32ExtractSection (
134 IN EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *This,
135 IN VOID *InputSection,
136 OUT VOID **OutputBuffer,
137 OUT UINTN *OutputSize,
138 OUT UINT32 *AuthenticationStatus
139 )
140 /*++
141
142 Routine Description:
143 This function reads and extracts contents of a section from an
144 encapsulating section.
145
146 Parameters:
147 This - Indicates the calling context.
148 InputSection - Buffer containing the input GUIDed section
149 to be processed.
150 OutputBuffer - *OutputBuffer is allocated from boot services
151 pool memory and containing the new section
152 stream. The caller is responsible for freeing
153 this buffer.
154 AuthenticationStatus - Pointer to a caller allocated UINT32 that
155 indicates the authentication status of the
156 output buffer
157
158 Return Value:
159 EFI_SUCCESS
160 EFI_OUT_OF_RESOURCES
161 EFI_INVALID_PARAMETER
162 EFI_NOT_AVAILABLE_YET
163
164 --*/
165 // TODO: function comment is missing 'Arguments:'
166 // TODO: function comment is missing 'Returns:'
167 // TODO: This - add argument and description to function comment
168 // TODO: InputSection - add argument and description to function comment
169 // TODO: OutputBuffer - add argument and description to function comment
170 // TODO: OutputSize - add argument and description to function comment
171 // TODO: AuthenticationStatus - add argument and description to function comment
172 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
173 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
174 // TODO: EFI_OUT_OF_RESOURCES - add return value to function comment
175 // TODO: EFI_SUCCESS - add return value to function comment
176 {
177 EFI_STATUS Status;
178 CRC32_SECTION_HEADER *Crc32SectionHeader;
179 EFI_GUID_DEFINED_SECTION *GuidedSectionHeader;
180 UINT8 *Image;
181 UINT32 Crc32Checksum;
182 VOID *DummyInterface;
183
184 if (OutputBuffer == NULL) {
185 return EFI_INVALID_PARAMETER;
186 }
187
188 *OutputBuffer = NULL;
189
190 //
191 // Points to the section header
192 //
193 Crc32SectionHeader = (CRC32_SECTION_HEADER *) InputSection;
194 GuidedSectionHeader = (EFI_GUID_DEFINED_SECTION *) InputSection;
195
196 //
197 // Check if the GUID is a CRC32 section GUID
198 //
199 if (!CompareGuid (
200 &(GuidedSectionHeader->SectionDefinitionGuid),
201 &gEfiCrc32GuidedSectionExtractionProtocolGuid
202 )) {
203 return EFI_INVALID_PARAMETER;
204 }
205
206 Image = (UINT8 *) InputSection + (UINT32) (GuidedSectionHeader->DataOffset);
207 *OutputSize = GetSectionLength ((EFI_COMMON_SECTION_HEADER *) InputSection) - (UINT32) GuidedSectionHeader->DataOffset;
208
209 Status = gBS->AllocatePool (EfiBootServicesData, *OutputSize, OutputBuffer);
210 if (EFI_ERROR (Status)) {
211 return EFI_OUT_OF_RESOURCES;
212 }
213 //
214 // Implictly CRC32 GUIDed section should have STATUS_VALID bit set
215 //
216 ASSERT (GuidedSectionHeader->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);
217 *AuthenticationStatus = EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED | EFI_AGGREGATE_AUTH_STATUS_IMAGE_SIGNED;
218
219 //
220 // Check whether there exists EFI_SECURITY_POLICY_PROTOCOL_GUID.
221 //
222 Status = gBS->LocateProtocol (&gEfiSecurityPolicyProtocolGuid, NULL, &DummyInterface);
223 if (!EFI_ERROR (Status)) {
224 *AuthenticationStatus |= EFI_LOCAL_AUTH_STATUS_PLATFORM_OVERRIDE | EFI_AGGREGATE_AUTH_STATUS_PLATFORM_OVERRIDE;
225 } else {
226 //
227 // Calculate CRC32 Checksum of Image
228 //
229 gBS->CalculateCrc32 (Image, *OutputSize, &Crc32Checksum);
230 if (Crc32Checksum != Crc32SectionHeader->CRC32Checksum) {
231 *AuthenticationStatus |= EFI_LOCAL_AUTH_STATUS_TEST_FAILED | EFI_AGGREGATE_AUTH_STATUS_TEST_FAILED;
232 }
233 }
234
235 CopyMem (*OutputBuffer, Image, *OutputSize);
236
237 return EFI_SUCCESS;
238 }