]>
Commit | Line | Data |
---|---|---|
504214c4 | 1 | /** @file\r |
d51ffc1f | 2 | \r |
109e9a61 LG |
3 | This library registers CRC32 guided section handler \r |
4 | to parse CRC32 encapsulation section and extract raw data.\r | |
5 | It uses UEFI boot service CalculateCrc32 to authenticate 32 bit CRC value.\r | |
504214c4 LG |
6 | \r |
7 | Copyright (c) 2007 - 2008, Intel Corporation \r | |
d51ffc1f LG |
8 | All rights reserved. This program and the accompanying materials \r |
9 | are licensed and made available under the terms and conditions of the BSD License \r | |
10 | which accompanies this distribution. The full text of the license may be found at \r | |
11 | http://opensource.org/licenses/bsd-license.php \r | |
12 | \r | |
13 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
14 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r | |
15 | \r | |
504214c4 | 16 | **/\r |
d51ffc1f LG |
17 | \r |
18 | #include <PiDxe.h>\r | |
76f1dde2 | 19 | #include <Guid/Crc32GuidedSectionExtraction.h>\r |
d51ffc1f LG |
20 | #include <Protocol/SecurityPolicy.h>\r |
21 | #include <Library/ExtractGuidedSectionLib.h>\r | |
22 | #include <Library/DebugLib.h>\r | |
e6c560aa | 23 | #include <Library/BaseMemoryLib.h>\r |
d51ffc1f LG |
24 | #include <Library/UefiBootServicesTableLib.h>\r |
25 | \r | |
2a86ff1c LG |
26 | #define EFI_SECITON_SIZE_MASK 0x00ffffff\r |
27 | \r | |
109e9a61 LG |
28 | ///\r |
29 | /// CRC32 Guided Section header\r | |
30 | ///\r | |
d51ffc1f | 31 | typedef struct {\r |
109e9a61 LG |
32 | EFI_GUID_DEFINED_SECTION GuidedSectionHeader; ///< EFI guided section header\r |
33 | UINT32 CRC32Checksum; ///< 32bit CRC check sum\r | |
d51ffc1f LG |
34 | } CRC32_SECTION_HEADER;\r |
35 | \r | |
5d69642d LG |
36 | /**\r |
37 | \r | |
109e9a61 LG |
38 | GetInfo gets raw data size and attribute of the input guided section.\r |
39 | It first checks whether the input guid section is supported. \r | |
40 | If not, EFI_INVALID_PARAMETER will return.\r | |
5d69642d LG |
41 | \r |
42 | @param InputSection Buffer containing the input GUIDed section to be processed.\r | |
43 | @param OutputBufferSize The size of OutputBuffer.\r | |
44 | @param ScratchBufferSize The size of ScratchBuffer.\r | |
45 | @param SectionAttribute The attribute of the input guided section.\r | |
46 | \r | |
47 | @retval EFI_SUCCESS The size of destination buffer, the size of scratch buffer and \r | |
48 | the attribute of the input section are successull retrieved.\r | |
49 | @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.\r | |
50 | \r | |
51 | **/\r | |
d51ffc1f LG |
52 | EFI_STATUS\r |
53 | EFIAPI\r | |
54 | Crc32GuidedSectionGetInfo (\r | |
55 | IN CONST VOID *InputSection,\r | |
56 | OUT UINT32 *OutputBufferSize,\r | |
57 | OUT UINT32 *ScratchBufferSize,\r | |
58 | OUT UINT16 *SectionAttribute\r | |
59 | )\r | |
d51ffc1f | 60 | {\r |
5d69642d LG |
61 | //\r |
62 | // Check whether the input guid section is recognized.\r | |
63 | //\r | |
e6c560aa | 64 | if (!CompareGuid (\r |
76f1dde2 | 65 | &gEfiCrc32GuidedSectionExtractionGuid, \r |
e6c560aa LG |
66 | &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r |
67 | return EFI_INVALID_PARAMETER;\r | |
68 | }\r | |
d51ffc1f LG |
69 | //\r |
70 | // Retrieve the size and attribute of the input section data.\r | |
71 | //\r | |
72 | *SectionAttribute = ((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes;\r | |
73 | *ScratchBufferSize = 0;\r | |
2a86ff1c | 74 | *OutputBufferSize = *(UINT32 *) (((EFI_COMMON_SECTION_HEADER *) InputSection)->Size) & EFI_SECITON_SIZE_MASK;\r |
d51ffc1f LG |
75 | *OutputBufferSize -= ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;\r |
76 | \r | |
77 | return EFI_SUCCESS;\r | |
78 | }\r | |
79 | \r | |
5d69642d LG |
80 | /**\r |
81 | \r | |
109e9a61 LG |
82 | Extraction handler tries to extract raw data from the input guided section.\r |
83 | It also does authentication check for 32bit CRC value in the input guided section.\r | |
84 | It first checks whether the input guid section is supported. \r | |
85 | If not, EFI_INVALID_PARAMETER will return.\r | |
5d69642d LG |
86 | \r |
87 | @param InputSection Buffer containing the input GUIDed section to be processed.\r | |
109e9a61 | 88 | @param OutputBuffer Buffer to contain the output raw data allocated by the caller.\r |
5d69642d LG |
89 | @param ScratchBuffer A pointer to a caller-allocated buffer for function internal use.\r |
90 | @param AuthenticationStatus A pointer to a caller-allocated UINT32 that indicates the\r | |
109e9a61 | 91 | authentication status of the output buffer.\r |
5d69642d LG |
92 | \r |
93 | @retval EFI_SUCCESS Section Data and Auth Status is extracted successfully.\r | |
94 | @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.\r | |
95 | \r | |
96 | **/\r | |
d51ffc1f LG |
97 | EFI_STATUS\r |
98 | EFIAPI\r | |
99 | Crc32GuidedSectionHandler (\r | |
100 | IN CONST VOID *InputSection,\r | |
101 | OUT VOID **OutputBuffer,\r | |
102 | IN VOID *ScratchBuffer, OPTIONAL\r | |
103 | OUT UINT32 *AuthenticationStatus\r | |
104 | )\r | |
d51ffc1f LG |
105 | {\r |
106 | EFI_STATUS Status;\r | |
107 | CRC32_SECTION_HEADER *Crc32SectionHeader;\r | |
108 | UINT32 Crc32Checksum;\r | |
109 | UINT32 OutputBufferSize;\r | |
110 | VOID *DummyInterface;\r | |
111 | \r | |
5d69642d LG |
112 | //\r |
113 | // Check whether the input guid section is recognized.\r | |
114 | //\r | |
e6c560aa | 115 | if (!CompareGuid (\r |
76f1dde2 | 116 | &gEfiCrc32GuidedSectionExtractionGuid, \r |
e6c560aa LG |
117 | &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r |
118 | return EFI_INVALID_PARAMETER;\r | |
119 | }\r | |
5d69642d LG |
120 | \r |
121 | //\r | |
122 | // Init Checksum value to Zero.\r | |
123 | //\r | |
d51ffc1f LG |
124 | Crc32Checksum = 0;\r |
125 | //\r | |
126 | // Points to the Crc32 section header\r | |
127 | //\r | |
128 | Crc32SectionHeader = (CRC32_SECTION_HEADER *) InputSection;\r | |
129 | *OutputBuffer = (UINT8 *) InputSection + Crc32SectionHeader->GuidedSectionHeader.DataOffset;\r | |
2a86ff1c | 130 | OutputBufferSize = *(UINT32 *) (((EFI_COMMON_SECTION_HEADER *) InputSection)->Size) & EFI_SECITON_SIZE_MASK; \r |
d51ffc1f LG |
131 | OutputBufferSize -= Crc32SectionHeader->GuidedSectionHeader.DataOffset;\r |
132 | \r | |
133 | //\r | |
134 | // Implictly CRC32 GUIDed section should have STATUS_VALID bit set\r | |
135 | //\r | |
136 | ASSERT (Crc32SectionHeader->GuidedSectionHeader.Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);\r | |
137 | *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;\r | |
138 | \r | |
139 | //\r | |
140 | // Check whether there exists EFI_SECURITY_POLICY_PROTOCOL_GUID.\r | |
141 | //\r | |
142 | Status = gBS->LocateProtocol (&gEfiSecurityPolicyProtocolGuid, NULL, &DummyInterface);\r | |
143 | if (!EFI_ERROR (Status)) {\r | |
5d69642d LG |
144 | //\r |
145 | // If SecurityPolicy Protocol exist, AUTH platform override bit is set.\r | |
146 | //\r | |
d51ffc1f LG |
147 | *AuthenticationStatus |= EFI_AUTH_STATUS_PLATFORM_OVERRIDE;\r |
148 | } else {\r | |
149 | //\r | |
150 | // Calculate CRC32 Checksum of Image\r | |
151 | //\r | |
152 | Status = gBS->CalculateCrc32 (*OutputBuffer, OutputBufferSize, &Crc32Checksum);\r | |
153 | if (Status == EFI_SUCCESS) {\r | |
154 | if (Crc32Checksum != Crc32SectionHeader->CRC32Checksum) {\r | |
5d69642d LG |
155 | //\r |
156 | // If Crc32 checksum is not matched, AUTH tested failed bit is set.\r | |
157 | //\r | |
d51ffc1f LG |
158 | *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r |
159 | }\r | |
160 | } else {\r | |
5d69642d LG |
161 | //\r |
162 | // If Crc32 checksum is not calculated, AUTH not tested bit is set.\r | |
163 | //\r | |
d51ffc1f LG |
164 | *AuthenticationStatus |= EFI_AUTH_STATUS_NOT_TESTED;\r |
165 | }\r | |
166 | }\r | |
167 | \r | |
168 | return EFI_SUCCESS;\r | |
169 | }\r | |
170 | \r | |
5d69642d | 171 | /**\r |
109e9a61 | 172 | Register the handler to extract CRC32 guided section.\r |
5d69642d | 173 | \r |
cebc8d48 LG |
174 | @param ImageHandle ImageHandle of the loaded driver.\r |
175 | @param SystemTable Pointer to the EFI System Table.\r | |
176 | \r | |
109e9a61 LG |
177 | @retval EFI_SUCCESS Register successfully.\r |
178 | @retval EFI_OUT_OF_RESOURCES No enough memory to register this handler.\r | |
d51ffc1f LG |
179 | **/\r |
180 | EFI_STATUS\r | |
181 | EFIAPI\r | |
182 | DxeCrc32GuidedSectionExtractLibConstructor (\r | |
a5ff00a6 | 183 | IN EFI_HANDLE ImageHandle,\r |
184 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
d51ffc1f LG |
185 | )\r |
186 | {\r | |
187 | return ExtractGuidedSectionRegisterHandlers (\r | |
76f1dde2 | 188 | &gEfiCrc32GuidedSectionExtractionGuid,\r |
d51ffc1f LG |
189 | Crc32GuidedSectionGetInfo,\r |
190 | Crc32GuidedSectionHandler\r | |
191 | );\r | |
192 | }\r | |
193 | \r |