]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/DxeCrc32GuidedSectionExtractLib/DxeCrc32GuidedSectionExtractLib.c
Add comments and DoxyGen format for these files.
[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 EFI_STATUS
31 EFIAPI
32 Crc32GuidedSectionGetInfo (
33 IN CONST VOID *InputSection,
34 OUT UINT32 *OutputBufferSize,
35 OUT UINT32 *ScratchBufferSize,
36 OUT UINT16 *SectionAttribute
37 )
38 /*++
39
40 Routine Description:
41
42 The implementation of Crc32 guided section GetInfo().
43
44 Arguments:
45 InputSection Buffer containing the input GUIDed section to be processed.
46 OutputBufferSize The size of OutputBuffer.
47 ScratchBufferSize The size of ScratchBuffer.
48 SectionAttribute The attribute of the input guided section.
49
50 Returns:
51
52 EFI_SUCCESS - The size of destination buffer and the size of scratch buffer are successull retrieved.
53 EFI_INVALID_PARAMETER - The source data is corrupted, or
54 The GUID in InputSection does not match this instance guid.
55
56 --*/
57 {
58 if (!CompareGuid (
59 &gEfiCrc32GuidedSectionExtractionProtocolGuid,
60 &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
61 return EFI_INVALID_PARAMETER;
62 }
63 //
64 // Retrieve the size and attribute of the input section data.
65 //
66 *SectionAttribute = ((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes;
67 *ScratchBufferSize = 0;
68 *OutputBufferSize = *(UINT32 *) (((EFI_COMMON_SECTION_HEADER *) InputSection)->Size) & 0x00ffffff;
69 *OutputBufferSize -= ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;
70
71 return EFI_SUCCESS;
72 }
73
74 EFI_STATUS
75 EFIAPI
76 Crc32GuidedSectionHandler (
77 IN CONST VOID *InputSection,
78 OUT VOID **OutputBuffer,
79 IN VOID *ScratchBuffer, OPTIONAL
80 OUT UINT32 *AuthenticationStatus
81 )
82 /*++
83
84 Routine Description:
85
86 The implementation of Crc32 Guided section extraction.
87
88 Arguments:
89 InputSection Buffer containing the input GUIDed section to be processed.
90 OutputBuffer OutputBuffer to point to the start of the section's contents.
91 if guided data is not prcessed. Otherwise,
92 OutputBuffer to contain the output data, which is allocated by the caller.
93 ScratchBuffer A pointer to a caller-allocated buffer for function internal use.
94 AuthenticationStatus A pointer to a caller-allocated UINT32 that indicates the
95 authentication status of the output buffer.
96
97 Returns:
98
99 RETURN_SUCCESS - Decompression is successfull
100 RETURN_INVALID_PARAMETER - The source data is corrupted, or
101 The GUID in InputSection does not match this instance guid.
102
103 --*/
104 {
105 EFI_STATUS Status;
106 CRC32_SECTION_HEADER *Crc32SectionHeader;
107 UINT32 Crc32Checksum;
108 UINT32 OutputBufferSize;
109 VOID *DummyInterface;
110
111 if (!CompareGuid (
112 &gEfiCrc32GuidedSectionExtractionProtocolGuid,
113 &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
114 return EFI_INVALID_PARAMETER;
115 }
116
117 Crc32Checksum = 0;
118 //
119 // Points to the Crc32 section header
120 //
121 Crc32SectionHeader = (CRC32_SECTION_HEADER *) InputSection;
122 *OutputBuffer = (UINT8 *) InputSection + Crc32SectionHeader->GuidedSectionHeader.DataOffset;
123 OutputBufferSize = *(UINT32 *) (((EFI_COMMON_SECTION_HEADER *) InputSection)->Size) & 0x00ffffff;
124 OutputBufferSize -= Crc32SectionHeader->GuidedSectionHeader.DataOffset;
125
126 //
127 // Implictly CRC32 GUIDed section should have STATUS_VALID bit set
128 //
129 ASSERT (Crc32SectionHeader->GuidedSectionHeader.Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);
130 *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;
131
132 //
133 // Check whether there exists EFI_SECURITY_POLICY_PROTOCOL_GUID.
134 //
135 Status = gBS->LocateProtocol (&gEfiSecurityPolicyProtocolGuid, NULL, &DummyInterface);
136 if (!EFI_ERROR (Status)) {
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 *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;
146 }
147 } else {
148 *AuthenticationStatus |= EFI_AUTH_STATUS_NOT_TESTED;
149 }
150 }
151
152 return EFI_SUCCESS;
153 }
154
155 /**
156 Register Crc32 section handler.
157
158 @retval RETURN_SUCCESS Register successfully.
159 @retval RETURN_OUT_OF_RESOURCES No enough memory to store this handler.
160 **/
161 EFI_STATUS
162 EFIAPI
163 DxeCrc32GuidedSectionExtractLibConstructor (
164 )
165 {
166 return ExtractGuidedSectionRegisterHandlers (
167 &gEfiCrc32GuidedSectionExtractionProtocolGuid,
168 Crc32GuidedSectionGetInfo,
169 Crc32GuidedSectionHandler
170 );
171 }
172