TianoCore EDK2 master
Loading...
Searching...
No Matches
PeiCrc32GuidedSectionExtractLib.c
Go to the documentation of this file.
1
11#include <PiPei.h>
13#include <Library/BaseLib.h>
15#include <Library/DebugLib.h>
17
21typedef struct {
22 EFI_GUID_DEFINED_SECTION GuidedSectionHeader;
23 UINT32 CRC32Checksum;
25
26typedef struct {
27 EFI_GUID_DEFINED_SECTION2 GuidedSectionHeader;
28 UINT32 CRC32Checksum;
30
48EFIAPI
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 ))
64 {
65 return EFI_INVALID_PARAMETER;
66 }
67
68 //
69 // Retrieve the size and attribute of the input section data.
70 //
71 *SectionAttribute = ((EFI_GUID_DEFINED_SECTION2 *)InputSection)->Attributes;
72 *ScratchBufferSize = 0;
73 *OutputBufferSize = SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *)InputSection)->DataOffset;
74 } else {
75 //
76 // Check whether the input guid section is recognized.
77 //
78 if (!CompareGuid (
79 &gEfiCrc32GuidedSectionExtractionGuid,
80 &(((EFI_GUID_DEFINED_SECTION *)InputSection)->SectionDefinitionGuid)
81 ))
82 {
83 return EFI_INVALID_PARAMETER;
84 }
85
86 //
87 // Retrieve the size and attribute of the input section data.
88 //
89 *SectionAttribute = ((EFI_GUID_DEFINED_SECTION *)InputSection)->Attributes;
90 *ScratchBufferSize = 0;
91 *OutputBufferSize = SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *)InputSection)->DataOffset;
92 }
93
94 return EFI_SUCCESS;
95}
96
115EFIAPI
117 IN CONST VOID *InputSection,
118 OUT VOID **OutputBuffer,
119 IN VOID *ScratchBuffer OPTIONAL,
120 OUT UINT32 *AuthenticationStatus
121 )
122{
123 UINT32 SectionCrc32Checksum;
124 UINT32 Crc32Checksum;
125 UINT32 OutputBufferSize;
126
127 if (IS_SECTION2 (InputSection)) {
128 //
129 // Check whether the input guid section is recognized.
130 //
131 if (!CompareGuid (
132 &gEfiCrc32GuidedSectionExtractionGuid,
133 &(((EFI_GUID_DEFINED_SECTION2 *)InputSection)->SectionDefinitionGuid)
134 ))
135 {
136 return EFI_INVALID_PARAMETER;
137 }
138
139 //
140 // Get section Crc32 checksum.
141 //
142 SectionCrc32Checksum = ((CRC32_SECTION2_HEADER *)InputSection)->CRC32Checksum;
143 *OutputBuffer = (UINT8 *)InputSection + ((EFI_GUID_DEFINED_SECTION2 *)InputSection)->DataOffset;
144 OutputBufferSize = SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *)InputSection)->DataOffset;
145
146 //
147 // Implicitly CRC32 GUIDed section should have STATUS_VALID bit set
148 //
149 ASSERT (((EFI_GUID_DEFINED_SECTION2 *)InputSection)->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);
150 *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;
151 } else {
152 //
153 // Check whether the input guid section is recognized.
154 //
155 if (!CompareGuid (
156 &gEfiCrc32GuidedSectionExtractionGuid,
157 &(((EFI_GUID_DEFINED_SECTION *)InputSection)->SectionDefinitionGuid)
158 ))
159 {
160 return EFI_INVALID_PARAMETER;
161 }
162
163 //
164 // Get section Crc32 checksum.
165 //
166 SectionCrc32Checksum = ((CRC32_SECTION_HEADER *)InputSection)->CRC32Checksum;
167 *OutputBuffer = (UINT8 *)InputSection + ((EFI_GUID_DEFINED_SECTION *)InputSection)->DataOffset;
168 OutputBufferSize = SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *)InputSection)->DataOffset;
169
170 //
171 // Implicitly CRC32 GUIDed section should have STATUS_VALID bit set
172 //
173 ASSERT (((EFI_GUID_DEFINED_SECTION *)InputSection)->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);
174 *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;
175 }
176
177 //
178 // Calculate CRC32 Checksum of Image
179 //
180 Crc32Checksum = CalculateCrc32 (*OutputBuffer, OutputBufferSize);
181 if (Crc32Checksum != SectionCrc32Checksum) {
182 //
183 // If Crc32 checksum is not matched, AUTH tested failed bit is set.
184 //
185 *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;
186 }
187
188 //
189 // Temp solution until PeiCore checks AUTH Status.
190 //
191 if ((*AuthenticationStatus & (EFI_AUTH_STATUS_TEST_FAILED | EFI_AUTH_STATUS_NOT_TESTED)) != 0) {
192 return EFI_ACCESS_DENIED;
193 }
194
195 return EFI_SUCCESS;
196}
197
209EFIAPI
211 IN EFI_PEI_FILE_HANDLE FileHandle,
212 IN CONST EFI_PEI_SERVICES **PeiServices
213 )
214{
216 &gEfiCrc32GuidedSectionExtractionGuid,
219 );
220}
UINT32 EFIAPI CalculateCrc32(IN VOID *Buffer, IN UINTN Length)
Definition: CheckSum.c:600
BOOLEAN EFIAPI CompareGuid(IN CONST GUID *Guid1, IN CONST GUID *Guid2)
Definition: MemLibGuid.c:73
#define CONST
Definition: Base.h:259
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
EFI_STATUS EFIAPI Crc32GuidedSectionHandler(IN CONST VOID *InputSection, OUT VOID **OutputBuffer, IN VOID *ScratchBuffer OPTIONAL, OUT UINT32 *AuthenticationStatus)
EFI_STATUS EFIAPI Crc32GuidedSectionGetInfo(IN CONST VOID *InputSection, OUT UINT32 *OutputBufferSize, OUT UINT32 *ScratchBufferSize, OUT UINT16 *SectionAttribute)
EFI_STATUS EFIAPI PeiCrc32GuidedSectionExtractLibConstructor(IN EFI_PEI_FILE_HANDLE FileHandle, IN CONST EFI_PEI_SERVICES **PeiServices)
#define SECTION_SIZE(SectionHeaderPtr)
VOID * EFI_PEI_FILE_HANDLE
Definition: PiPeiCis.h:26
RETURN_STATUS EFIAPI ExtractGuidedSectionRegisterHandlers(IN CONST GUID *SectionGuid, IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER GetInfoHandler, IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER DecodeHandler)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112