]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/DxeCrc32GuidedSectionExtractLib/DxeCrc32GuidedSectionExtractLib.c
6deb8dc6d26bbcbcfac6035628b0bea35c04fcd7
[mirror_edk2.git] / MdeModulePkg / Library / DxeCrc32GuidedSectionExtractLib / DxeCrc32GuidedSectionExtractLib.c
1 /*++
2
3 Copyright (c) 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 DxeCrc32GuidedSectionExtractLib.c
15
16 Abstract:
17
18 Implements GUIDed section extraction protocol interface with
19 a specific GUID: CRC32.
20
21 Please refer to the Framewokr Firmware Volume Specification 0.9.
22
23 --*/
24
25 #include <PiDxe.h>
26 #include <Protocol/Crc32GuidedSectionExtraction.h>
27 #include <Protocol/SecurityPolicy.h>
28 #include <Library/ExtractGuidedSectionLib.h>
29 #include <Library/DebugLib.h>
30 #include <Library/UefiBootServicesTableLib.h>
31
32 typedef struct {
33 EFI_GUID_DEFINED_SECTION GuidedSectionHeader;
34 UINT32 CRC32Checksum;
35 } CRC32_SECTION_HEADER;
36
37 EFI_STATUS
38 EFIAPI
39 Crc32GuidedSectionGetInfo (
40 IN CONST VOID *InputSection,
41 OUT UINT32 *OutputBufferSize,
42 OUT UINT32 *ScratchBufferSize,
43 OUT UINT16 *SectionAttribute
44 )
45 /*++
46
47 Routine Description:
48
49 The implementation of Crc32 guided section GetInfo().
50
51 Arguments:
52 InputSection Buffer containing the input GUIDed section to be processed.
53 OutputBufferSize The size of OutputBuffer.
54 ScratchBufferSize The size of ScratchBuffer.
55 SectionAttribute The attribute of the input guided section.
56
57 Returns:
58
59 EFI_SUCCESS - The size of destination buffer and the size of scratch buffer are successull retrieved.
60 EFI_INVALID_PARAMETER - The source data is corrupted
61
62 --*/
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) & 0x00ffffff;
70 *OutputBufferSize -= ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;
71
72 return EFI_SUCCESS;
73 }
74
75 EFI_STATUS
76 EFIAPI
77 Crc32GuidedSectionHandler (
78 IN CONST VOID *InputSection,
79 OUT VOID **OutputBuffer,
80 IN VOID *ScratchBuffer, OPTIONAL
81 OUT UINT32 *AuthenticationStatus
82 )
83 /*++
84
85 Routine Description:
86
87 The implementation of Crc32 Guided section extraction.
88
89 Arguments:
90 InputSection Buffer containing the input GUIDed section to be processed.
91 OutputBuffer OutputBuffer to point to the start of the section's contents.
92 if guided data is not prcessed. Otherwise,
93 OutputBuffer to contain the output data, which is allocated by the caller.
94 ScratchBuffer A pointer to a caller-allocated buffer for function internal use.
95 AuthenticationStatus A pointer to a caller-allocated UINT32 that indicates the
96 authentication status of the output buffer.
97
98 Returns:
99
100 EFI_SUCCESS - Decompression is successfull
101 EFI_INVALID_PARAMETER - The source data is corrupted
102
103 --*/
104 {
105 EFI_STATUS Status;
106 CRC32_SECTION_HEADER *Crc32SectionHeader;
107 UINT32 Crc32Checksum;
108 UINT32 OutputBufferSize;
109 VOID *DummyInterface;
110
111 Crc32Checksum = 0;
112 //
113 // Points to the Crc32 section header
114 //
115 Crc32SectionHeader = (CRC32_SECTION_HEADER *) InputSection;
116 *OutputBuffer = (UINT8 *) InputSection + Crc32SectionHeader->GuidedSectionHeader.DataOffset;
117 OutputBufferSize = *(UINT32 *) (((EFI_COMMON_SECTION_HEADER *) InputSection)->Size) & 0x00ffffff;
118 OutputBufferSize -= Crc32SectionHeader->GuidedSectionHeader.DataOffset;
119
120 //
121 // Implictly CRC32 GUIDed section should have STATUS_VALID bit set
122 //
123 ASSERT (Crc32SectionHeader->GuidedSectionHeader.Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);
124 *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;
125
126 //
127 // Check whether there exists EFI_SECURITY_POLICY_PROTOCOL_GUID.
128 //
129 Status = gBS->LocateProtocol (&gEfiSecurityPolicyProtocolGuid, NULL, &DummyInterface);
130 if (!EFI_ERROR (Status)) {
131 *AuthenticationStatus |= EFI_AUTH_STATUS_PLATFORM_OVERRIDE;
132 } else {
133 //
134 // Calculate CRC32 Checksum of Image
135 //
136 Status = gBS->CalculateCrc32 (*OutputBuffer, OutputBufferSize, &Crc32Checksum);
137 if (Status == EFI_SUCCESS) {
138 if (Crc32Checksum != Crc32SectionHeader->CRC32Checksum) {
139 *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;
140 }
141 } else {
142 *AuthenticationStatus |= EFI_AUTH_STATUS_NOT_TESTED;
143 }
144 }
145
146 return EFI_SUCCESS;
147 }
148
149 /**
150 Register Crc32 section handler.
151
152 @retval RETURN_SUCCESS Register successfully.
153 @retval RETURN_OUT_OF_RESOURCES No enough memory to store this handler.
154 **/
155 EFI_STATUS
156 EFIAPI
157 DxeCrc32GuidedSectionExtractLibConstructor (
158 )
159 {
160 return ExtractGuidedSectionRegisterHandlers (
161 &gEfiCrc32GuidedSectionExtractionProtocolGuid,
162 Crc32GuidedSectionGetInfo,
163 Crc32GuidedSectionHandler
164 );
165 }
166