]>
Commit | Line | Data |
---|---|---|
a402e129 MK |
1 | /** @file\r |
2 | \r | |
3 | This library registers CRC32 guided section handler \r | |
4 | to parse CRC32 encapsulation section and extract raw data.\r | |
5 | \r | |
05542f49 | 6 | Copyright (c) 2007 - 2017, Intel Corporation. All rights reserved.<BR>\r |
a402e129 MK |
7 | This program and the accompanying materials \r |
8 | are licensed and made available under the terms and conditions of the BSD License \r | |
9 | which accompanies this distribution. The full text of the license may be found at \r | |
10 | http://opensource.org/licenses/bsd-license.php \r | |
11 | \r | |
12 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
13 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r | |
14 | \r | |
15 | **/\r | |
16 | \r | |
17 | #include <PiPei.h>\r | |
18 | #include <Guid/Crc32GuidedSectionExtraction.h>\r | |
05542f49 | 19 | #include <Library/BaseLib.h>\r |
a402e129 MK |
20 | #include <Library/ExtractGuidedSectionLib.h>\r |
21 | #include <Library/DebugLib.h>\r | |
22 | #include <Library/BaseMemoryLib.h>\r | |
23 | \r | |
24 | ///\r | |
25 | /// CRC32 Guided Section header\r | |
26 | ///\r | |
27 | typedef struct {\r | |
28 | EFI_GUID_DEFINED_SECTION GuidedSectionHeader; ///< EFI guided section header\r | |
29 | UINT32 CRC32Checksum; ///< 32bit CRC check sum\r | |
30 | } CRC32_SECTION_HEADER;\r | |
31 | \r | |
32 | typedef struct {\r | |
33 | EFI_GUID_DEFINED_SECTION2 GuidedSectionHeader; ///< EFI guided section header\r | |
34 | UINT32 CRC32Checksum; ///< 32bit CRC check sum\r | |
35 | } CRC32_SECTION2_HEADER;\r | |
36 | \r | |
a402e129 MK |
37 | /**\r |
38 | \r | |
39 | GetInfo gets raw data size and attribute of the input guided section.\r | |
40 | It first checks whether the input guid section is supported. \r | |
41 | If not, EFI_INVALID_PARAMETER will return.\r | |
42 | \r | |
43 | @param InputSection Buffer containing the input GUIDed section to be processed.\r | |
44 | @param OutputBufferSize The size of OutputBuffer.\r | |
45 | @param ScratchBufferSize The size of ScratchBuffer.\r | |
46 | @param SectionAttribute The attribute of the input guided section.\r | |
47 | \r | |
48 | @retval EFI_SUCCESS The size of destination buffer, the size of scratch buffer and \r | |
3b28e744 | 49 | the attribute of the input section are successfully retrieved.\r |
a402e129 MK |
50 | @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.\r |
51 | \r | |
52 | **/\r | |
53 | EFI_STATUS\r | |
54 | EFIAPI\r | |
55 | Crc32GuidedSectionGetInfo (\r | |
56 | IN CONST VOID *InputSection,\r | |
57 | OUT UINT32 *OutputBufferSize,\r | |
58 | OUT UINT32 *ScratchBufferSize,\r | |
59 | OUT UINT16 *SectionAttribute\r | |
60 | )\r | |
61 | {\r | |
62 | if (IS_SECTION2 (InputSection)) {\r | |
63 | //\r | |
64 | // Check whether the input guid section is recognized.\r | |
65 | //\r | |
66 | if (!CompareGuid (\r | |
67 | &gEfiCrc32GuidedSectionExtractionGuid,\r | |
68 | &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {\r | |
69 | return EFI_INVALID_PARAMETER;\r | |
70 | }\r | |
71 | //\r | |
72 | // Retrieve the size and attribute of the input section data.\r | |
73 | //\r | |
74 | *SectionAttribute = ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->Attributes;\r | |
75 | *ScratchBufferSize = 0;\r | |
76 | *OutputBufferSize = SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset;\r | |
77 | } else {\r | |
78 | //\r | |
79 | // Check whether the input guid section is recognized.\r | |
80 | //\r | |
81 | if (!CompareGuid (\r | |
82 | &gEfiCrc32GuidedSectionExtractionGuid,\r | |
83 | &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r | |
84 | return EFI_INVALID_PARAMETER;\r | |
85 | }\r | |
86 | //\r | |
87 | // Retrieve the size and attribute of the input section data.\r | |
88 | //\r | |
89 | *SectionAttribute = ((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes;\r | |
90 | *ScratchBufferSize = 0;\r | |
91 | *OutputBufferSize = SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;\r | |
92 | }\r | |
93 | \r | |
94 | return EFI_SUCCESS;\r | |
95 | }\r | |
96 | \r | |
97 | /**\r | |
98 | \r | |
99 | Extraction handler tries to extract raw data from the input guided section.\r | |
100 | It also does authentication check for 32bit CRC value in the input guided section.\r | |
101 | It first checks whether the input guid section is supported. \r | |
102 | If not, EFI_INVALID_PARAMETER will return.\r | |
103 | \r | |
104 | @param InputSection Buffer containing the input GUIDed section to be processed.\r | |
105 | @param OutputBuffer Buffer to contain the output raw data allocated by the caller.\r | |
106 | @param ScratchBuffer A pointer to a caller-allocated buffer for function internal use.\r | |
107 | @param AuthenticationStatus A pointer to a caller-allocated UINT32 that indicates the\r | |
108 | authentication status of the output buffer.\r | |
109 | \r | |
110 | @retval EFI_SUCCESS Section Data and Auth Status is extracted successfully.\r | |
111 | @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.\r | |
112 | \r | |
113 | **/\r | |
114 | EFI_STATUS\r | |
115 | EFIAPI\r | |
116 | Crc32GuidedSectionHandler (\r | |
117 | IN CONST VOID *InputSection,\r | |
118 | OUT VOID **OutputBuffer,\r | |
119 | IN VOID *ScratchBuffer, OPTIONAL\r | |
120 | OUT UINT32 *AuthenticationStatus\r | |
121 | )\r | |
122 | {\r | |
a402e129 MK |
123 | UINT32 SectionCrc32Checksum;\r |
124 | UINT32 Crc32Checksum;\r | |
125 | UINT32 OutputBufferSize;\r | |
126 | \r | |
127 | if (IS_SECTION2 (InputSection)) {\r | |
128 | //\r | |
129 | // Check whether the input guid section is recognized.\r | |
130 | //\r | |
131 | if (!CompareGuid (\r | |
132 | &gEfiCrc32GuidedSectionExtractionGuid,\r | |
133 | &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {\r | |
134 | return EFI_INVALID_PARAMETER;\r | |
135 | }\r | |
136 | \r | |
137 | //\r | |
138 | // Get section Crc32 checksum.\r | |
139 | //\r | |
140 | SectionCrc32Checksum = ((CRC32_SECTION2_HEADER *) InputSection)->CRC32Checksum;\r | |
141 | *OutputBuffer = (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset;\r | |
142 | OutputBufferSize = SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset;\r | |
143 | \r | |
144 | //\r | |
145 | // Implicitly CRC32 GUIDed section should have STATUS_VALID bit set\r | |
146 | //\r | |
147 | ASSERT (((EFI_GUID_DEFINED_SECTION2 *) InputSection)->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);\r | |
148 | *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;\r | |
149 | } else {\r | |
150 | //\r | |
151 | // Check whether the input guid section is recognized.\r | |
152 | //\r | |
153 | if (!CompareGuid (\r | |
154 | &gEfiCrc32GuidedSectionExtractionGuid,\r | |
155 | &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r | |
156 | return EFI_INVALID_PARAMETER;\r | |
157 | }\r | |
158 | \r | |
159 | //\r | |
160 | // Get section Crc32 checksum.\r | |
161 | //\r | |
162 | SectionCrc32Checksum = ((CRC32_SECTION_HEADER *) InputSection)->CRC32Checksum;\r | |
163 | *OutputBuffer = (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;\r | |
164 | OutputBufferSize = SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;\r | |
165 | \r | |
166 | //\r | |
167 | // Implicitly CRC32 GUIDed section should have STATUS_VALID bit set\r | |
168 | //\r | |
169 | ASSERT (((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);\r | |
170 | *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;\r | |
171 | }\r | |
172 | \r | |
a402e129 MK |
173 | //\r |
174 | // Calculate CRC32 Checksum of Image\r | |
175 | //\r | |
05542f49 LG |
176 | Crc32Checksum = CalculateCrc32 (*OutputBuffer, OutputBufferSize);\r |
177 | if (Crc32Checksum != SectionCrc32Checksum) {\r | |
a402e129 | 178 | //\r |
05542f49 | 179 | // If Crc32 checksum is not matched, AUTH tested failed bit is set.\r |
a402e129 | 180 | //\r |
05542f49 | 181 | *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r |
a402e129 MK |
182 | }\r |
183 | \r | |
184 | //\r | |
185 | // Temp solution until PeiCore checks AUTH Status.\r | |
186 | //\r | |
187 | if ((*AuthenticationStatus & (EFI_AUTH_STATUS_TEST_FAILED | EFI_AUTH_STATUS_NOT_TESTED)) != 0) {\r | |
188 | return EFI_ACCESS_DENIED;\r | |
189 | }\r | |
190 | \r | |
191 | return EFI_SUCCESS;\r | |
192 | }\r | |
193 | \r | |
194 | /**\r | |
195 | Register the handler to extract CRC32 guided section.\r | |
196 | \r | |
197 | @param FileHandle The handle of FFS header the loaded driver.\r | |
198 | @param PeiServices The pointer to the PEI services.\r | |
199 | \r | |
200 | @retval EFI_SUCCESS Register successfully.\r | |
201 | @retval EFI_OUT_OF_RESOURCES Not enough memory to register this handler.\r | |
202 | \r | |
203 | **/\r | |
204 | EFI_STATUS\r | |
205 | EFIAPI\r | |
206 | PeiCrc32GuidedSectionExtractLibConstructor (\r | |
207 | IN EFI_PEI_FILE_HANDLE FileHandle,\r | |
208 | IN CONST EFI_PEI_SERVICES **PeiServices\r | |
209 | )\r | |
210 | {\r | |
211 | return ExtractGuidedSectionRegisterHandlers (\r | |
212 | &gEfiCrc32GuidedSectionExtractionGuid,\r | |
213 | Crc32GuidedSectionGetInfo,\r | |
214 | Crc32GuidedSectionHandler\r | |
215 | );\r | |
216 | }\r |