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