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