]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/LzmaCustomDecompressLib/GuidedSectionExtraction.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Library / LzmaCustomDecompressLib / GuidedSectionExtraction.c
1 /** @file
2 LZMA Decompress GUIDed Section Extraction Library.
3 It wraps Lzma decompress interfaces to GUIDed Section Extraction interfaces
4 and registers them into GUIDed handler table.
5
6 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include "LzmaDecompressLibInternal.h"
12
13 /**
14 Examines a GUIDed section and returns the size of the decoded buffer and the
15 size of an scratch buffer required to actually decode the data in a GUIDed section.
16
17 Examines a GUIDed section specified by InputSection.
18 If GUID for InputSection does not match the GUID that this handler supports,
19 then RETURN_UNSUPPORTED is returned.
20 If the required information can not be retrieved from InputSection,
21 then RETURN_INVALID_PARAMETER is returned.
22 If the GUID of InputSection does match the GUID that this handler supports,
23 then the size required to hold the decoded buffer is returned in OututBufferSize,
24 the size of an optional scratch buffer is returned in ScratchSize, and the Attributes field
25 from EFI_GUID_DEFINED_SECTION header of InputSection is returned in SectionAttribute.
26
27 If InputSection is NULL, then ASSERT().
28 If OutputBufferSize is NULL, then ASSERT().
29 If ScratchBufferSize is NULL, then ASSERT().
30 If SectionAttribute is NULL, then ASSERT().
31
32
33 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
34 @param[out] OutputBufferSize A pointer to the size, in bytes, of an output buffer required
35 if the buffer specified by InputSection were decoded.
36 @param[out] ScratchBufferSize A pointer to the size, in bytes, required as scratch space
37 if the buffer specified by InputSection were decoded.
38 @param[out] SectionAttribute A pointer to the attributes of the GUIDed section. See the Attributes
39 field of EFI_GUID_DEFINED_SECTION in the PI Specification.
40
41 @retval RETURN_SUCCESS The information about InputSection was returned.
42 @retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports.
43 @retval RETURN_INVALID_PARAMETER The information can not be retrieved from the section specified by InputSection.
44
45 **/
46 RETURN_STATUS
47 EFIAPI
48 LzmaGuidedSectionGetInfo (
49 IN CONST VOID *InputSection,
50 OUT UINT32 *OutputBufferSize,
51 OUT UINT32 *ScratchBufferSize,
52 OUT UINT16 *SectionAttribute
53 )
54 {
55 ASSERT (InputSection != NULL);
56 ASSERT (OutputBufferSize != NULL);
57 ASSERT (ScratchBufferSize != NULL);
58 ASSERT (SectionAttribute != NULL);
59
60 if (IS_SECTION2 (InputSection)) {
61 if (!CompareGuid (
62 &gLzmaCustomDecompressGuid,
63 &(((EFI_GUID_DEFINED_SECTION2 *)InputSection)->SectionDefinitionGuid)
64 ))
65 {
66 return RETURN_INVALID_PARAMETER;
67 }
68
69 *SectionAttribute = ((EFI_GUID_DEFINED_SECTION2 *)InputSection)->Attributes;
70
71 return LzmaUefiDecompressGetInfo (
72 (UINT8 *)InputSection + ((EFI_GUID_DEFINED_SECTION2 *)InputSection)->DataOffset,
73 SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *)InputSection)->DataOffset,
74 OutputBufferSize,
75 ScratchBufferSize
76 );
77 } else {
78 if (!CompareGuid (
79 &gLzmaCustomDecompressGuid,
80 &(((EFI_GUID_DEFINED_SECTION *)InputSection)->SectionDefinitionGuid)
81 ))
82 {
83 return RETURN_INVALID_PARAMETER;
84 }
85
86 *SectionAttribute = ((EFI_GUID_DEFINED_SECTION *)InputSection)->Attributes;
87
88 return LzmaUefiDecompressGetInfo (
89 (UINT8 *)InputSection + ((EFI_GUID_DEFINED_SECTION *)InputSection)->DataOffset,
90 SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *)InputSection)->DataOffset,
91 OutputBufferSize,
92 ScratchBufferSize
93 );
94 }
95 }
96
97 /**
98 Decompress a LZAM compressed GUIDed section into a caller allocated output buffer.
99
100 Decodes the GUIDed section specified by InputSection.
101 If GUID for InputSection does not match the GUID that this handler supports, then RETURN_UNSUPPORTED is returned.
102 If the data in InputSection can not be decoded, then RETURN_INVALID_PARAMETER is returned.
103 If the GUID of InputSection does match the GUID that this handler supports, then InputSection
104 is decoded into the buffer specified by OutputBuffer and the authentication status of this
105 decode operation is returned in AuthenticationStatus. If the decoded buffer is identical to the
106 data in InputSection, then OutputBuffer is set to point at the data in InputSection. Otherwise,
107 the decoded data will be placed in caller allocated buffer specified by OutputBuffer.
108
109 If InputSection is NULL, then ASSERT().
110 If OutputBuffer is NULL, then ASSERT().
111 If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().
112 If AuthenticationStatus is NULL, then ASSERT().
113
114
115 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
116 @param[out] OutputBuffer A pointer to a buffer that contains the result of a decode operation.
117 @param[out] ScratchBuffer A caller allocated buffer that may be required by this function
118 as a scratch buffer to perform the decode operation.
119 @param[out] AuthenticationStatus
120 A pointer to the authentication status of the decoded output buffer.
121 See the definition of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI
122 section of the PI Specification. EFI_AUTH_STATUS_PLATFORM_OVERRIDE must
123 never be set by this handler.
124
125 @retval RETURN_SUCCESS The buffer specified by InputSection was decoded.
126 @retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports.
127 @retval RETURN_INVALID_PARAMETER The section specified by InputSection can not be decoded.
128
129 **/
130 RETURN_STATUS
131 EFIAPI
132 LzmaGuidedSectionExtraction (
133 IN CONST VOID *InputSection,
134 OUT VOID **OutputBuffer,
135 OUT VOID *ScratchBuffer OPTIONAL,
136 OUT UINT32 *AuthenticationStatus
137 )
138 {
139 ASSERT (OutputBuffer != NULL);
140 ASSERT (InputSection != NULL);
141
142 if (IS_SECTION2 (InputSection)) {
143 if (!CompareGuid (
144 &gLzmaCustomDecompressGuid,
145 &(((EFI_GUID_DEFINED_SECTION2 *)InputSection)->SectionDefinitionGuid)
146 ))
147 {
148 return RETURN_INVALID_PARAMETER;
149 }
150
151 //
152 // Authentication is set to Zero, which may be ignored.
153 //
154 *AuthenticationStatus = 0;
155
156 return LzmaUefiDecompress (
157 (UINT8 *)InputSection + ((EFI_GUID_DEFINED_SECTION2 *)InputSection)->DataOffset,
158 SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *)InputSection)->DataOffset,
159 *OutputBuffer,
160 ScratchBuffer
161 );
162 } else {
163 if (!CompareGuid (
164 &gLzmaCustomDecompressGuid,
165 &(((EFI_GUID_DEFINED_SECTION *)InputSection)->SectionDefinitionGuid)
166 ))
167 {
168 return RETURN_INVALID_PARAMETER;
169 }
170
171 //
172 // Authentication is set to Zero, which may be ignored.
173 //
174 *AuthenticationStatus = 0;
175
176 return LzmaUefiDecompress (
177 (UINT8 *)InputSection + ((EFI_GUID_DEFINED_SECTION *)InputSection)->DataOffset,
178 SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *)InputSection)->DataOffset,
179 *OutputBuffer,
180 ScratchBuffer
181 );
182 }
183 }
184
185 /**
186 Register LzmaDecompress and LzmaDecompressGetInfo handlers with LzmaCustomerDecompressGuid.
187
188 @retval RETURN_SUCCESS Register successfully.
189 @retval RETURN_OUT_OF_RESOURCES No enough memory to store this handler.
190 **/
191 EFI_STATUS
192 EFIAPI
193 LzmaDecompressLibConstructor (
194 VOID
195 )
196 {
197 return ExtractGuidedSectionRegisterHandlers (
198 &gLzmaCustomDecompressGuid,
199 LzmaGuidedSectionGetInfo,
200 LzmaGuidedSectionExtraction
201 );
202 }