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