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