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