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