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