]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/DxeCrc32GuidedSectionExtractLib/DxeCrc32GuidedSectionExtractLib.c
Code scrub for the Capsule, SecurityStub, and Crc32 library instance.
[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
25typedef struct {\r
26 EFI_GUID_DEFINED_SECTION GuidedSectionHeader;\r
27 UINT32 CRC32Checksum;\r
28} CRC32_SECTION_HEADER;\r
29\r
5d69642d
LG
30/**\r
31\r
32 The implementation of Crc32 guided section GetInfo() to get \r
33 size and attribute of the guided section.\r
34\r
35 @param InputSection Buffer containing the input GUIDed section to be processed.\r
36 @param OutputBufferSize The size of OutputBuffer.\r
37 @param ScratchBufferSize The size of ScratchBuffer.\r
38 @param SectionAttribute The attribute of the input guided section.\r
39\r
40 @retval EFI_SUCCESS The size of destination buffer, the size of scratch buffer and \r
41 the attribute of the input section are successull retrieved.\r
42 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.\r
43\r
44**/\r
d51ffc1f
LG
45EFI_STATUS\r
46EFIAPI\r
47Crc32GuidedSectionGetInfo (\r
48 IN CONST VOID *InputSection,\r
49 OUT UINT32 *OutputBufferSize,\r
50 OUT UINT32 *ScratchBufferSize,\r
51 OUT UINT16 *SectionAttribute\r
52 )\r
d51ffc1f 53{\r
5d69642d
LG
54 //\r
55 // Check whether the input guid section is recognized.\r
56 //\r
e6c560aa
LG
57 if (!CompareGuid (\r
58 &gEfiCrc32GuidedSectionExtractionProtocolGuid, \r
59 &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
60 return EFI_INVALID_PARAMETER;\r
61 }\r
d51ffc1f
LG
62 //\r
63 // Retrieve the size and attribute of the input section data.\r
64 //\r
65 *SectionAttribute = ((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes;\r
66 *ScratchBufferSize = 0;\r
67 *OutputBufferSize = *(UINT32 *) (((EFI_COMMON_SECTION_HEADER *) InputSection)->Size) & 0x00ffffff;\r
68 *OutputBufferSize -= ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;\r
69\r
70 return EFI_SUCCESS;\r
71}\r
72\r
5d69642d
LG
73/**\r
74\r
75 The implementation of Crc32 Guided section extraction to get the section data.\r
76\r
77 @param InputSection Buffer containing the input GUIDed section to be processed.\r
78 @param OutputBuffer to contain the output data, which is allocated by the caller.\r
79 @param ScratchBuffer A pointer to a caller-allocated buffer for function internal use.\r
80 @param AuthenticationStatus A pointer to a caller-allocated UINT32 that indicates the\r
81 authentication status of the output buffer.\r
82\r
83 @retval EFI_SUCCESS Section Data and Auth Status is extracted successfully.\r
84 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.\r
85\r
86**/\r
d51ffc1f
LG
87EFI_STATUS\r
88EFIAPI\r
89Crc32GuidedSectionHandler (\r
90 IN CONST VOID *InputSection,\r
91 OUT VOID **OutputBuffer,\r
92 IN VOID *ScratchBuffer, OPTIONAL\r
93 OUT UINT32 *AuthenticationStatus\r
94 )\r
d51ffc1f
LG
95{\r
96 EFI_STATUS Status;\r
97 CRC32_SECTION_HEADER *Crc32SectionHeader;\r
98 UINT32 Crc32Checksum;\r
99 UINT32 OutputBufferSize;\r
100 VOID *DummyInterface;\r
101\r
5d69642d
LG
102 //\r
103 // Check whether the input guid section is recognized.\r
104 //\r
e6c560aa
LG
105 if (!CompareGuid (\r
106 &gEfiCrc32GuidedSectionExtractionProtocolGuid, \r
107 &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
108 return EFI_INVALID_PARAMETER;\r
109 }\r
5d69642d
LG
110 \r
111 //\r
112 // Init Checksum value to Zero.\r
113 //\r
d51ffc1f
LG
114 Crc32Checksum = 0;\r
115 //\r
116 // Points to the Crc32 section header\r
117 //\r
118 Crc32SectionHeader = (CRC32_SECTION_HEADER *) InputSection;\r
119 *OutputBuffer = (UINT8 *) InputSection + Crc32SectionHeader->GuidedSectionHeader.DataOffset;\r
120 OutputBufferSize = *(UINT32 *) (((EFI_COMMON_SECTION_HEADER *) InputSection)->Size) & 0x00ffffff; \r
121 OutputBufferSize -= Crc32SectionHeader->GuidedSectionHeader.DataOffset;\r
122\r
123 //\r
124 // Implictly CRC32 GUIDed section should have STATUS_VALID bit set\r
125 //\r
126 ASSERT (Crc32SectionHeader->GuidedSectionHeader.Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);\r
127 *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;\r
128\r
129 //\r
130 // Check whether there exists EFI_SECURITY_POLICY_PROTOCOL_GUID.\r
131 //\r
132 Status = gBS->LocateProtocol (&gEfiSecurityPolicyProtocolGuid, NULL, &DummyInterface);\r
133 if (!EFI_ERROR (Status)) {\r
5d69642d
LG
134 //\r
135 // If SecurityPolicy Protocol exist, AUTH platform override bit is set.\r
136 //\r
d51ffc1f
LG
137 *AuthenticationStatus |= EFI_AUTH_STATUS_PLATFORM_OVERRIDE;\r
138 } else {\r
139 //\r
140 // Calculate CRC32 Checksum of Image\r
141 //\r
142 Status = gBS->CalculateCrc32 (*OutputBuffer, OutputBufferSize, &Crc32Checksum);\r
143 if (Status == EFI_SUCCESS) {\r
144 if (Crc32Checksum != Crc32SectionHeader->CRC32Checksum) {\r
5d69642d
LG
145 //\r
146 // If Crc32 checksum is not matched, AUTH tested failed bit is set.\r
147 //\r
d51ffc1f
LG
148 *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
149 }\r
150 } else {\r
5d69642d
LG
151 //\r
152 // If Crc32 checksum is not calculated, AUTH not tested bit is set.\r
153 //\r
d51ffc1f
LG
154 *AuthenticationStatus |= EFI_AUTH_STATUS_NOT_TESTED;\r
155 }\r
156 }\r
157\r
158 return EFI_SUCCESS;\r
159}\r
160\r
5d69642d 161/**\r
d51ffc1f 162 Register Crc32 section handler.\r
5d69642d 163\r
d51ffc1f 164 @retval RETURN_SUCCESS Register successfully.\r
5d69642d 165 @retval RETURN_OUT_OF_RESOURCES No enough memory to register this handler.\r
d51ffc1f
LG
166**/\r
167EFI_STATUS\r
168EFIAPI\r
169DxeCrc32GuidedSectionExtractLibConstructor (\r
170 )\r
171{\r
172 return ExtractGuidedSectionRegisterHandlers (\r
173 &gEfiCrc32GuidedSectionExtractionProtocolGuid,\r
174 Crc32GuidedSectionGetInfo,\r
175 Crc32GuidedSectionHandler\r
176 );\r
177}\r
178\r