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