]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/PeiDxeExtractGuidedSectionLib/PeiDxeExtractGuidedSectionLib.c
Work around fix to bypass to register GUID on S3 resume.
[mirror_edk2.git] / MdePkg / Library / PeiDxeExtractGuidedSectionLib / PeiDxeExtractGuidedSectionLib.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 PeiDxeExtractGuidedSectionLib.c
15
16 Abstract:
17
18 Provide generic extract guided section functions.
19
20 --*/
21
22 #include <PiPei.h>
23
24 #include <Library/DebugLib.h>
25 #include <Library/PcdLib.h>
26 #include <Library/BaseMemoryLib.h>
27 #include <Library/MemoryAllocationLib.h>
28 #include <Library/ExtractGuidedSectionLib.h>
29 #include <Library/HobLib.h>
30
31 STATIC GUID *mExtractHandlerGuidTable;
32 STATIC UINT32 mNumberOfExtractHandler;
33
34 STATIC EXTRACT_GUIDED_SECTION_DECODE_HANDLER *mExtractDecodeHandlerTable;
35 STATIC EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *mExtractGetInfoHandlerTable;
36
37 /**
38 Construtor allocates the global memory to store the registered guid and Handler list.
39
40 @retval RETURN_SUCCESS Allocate the global memory space to store guid and funciton tables.
41 @retval RETURN_OUT_OF_RESOURCES No enough memory to allocated.
42 **/
43 RETURN_STATUS
44 EFIAPI
45 PeiDxeExtractGuidedSectionLibConstructor (
46 )
47 {
48 //
49 // Allocate global pool space to store the registered handler and its guid value.
50 //
51 mExtractHandlerGuidTable = (GUID *) AllocatePool (PcdGet32 (PcdMaximumGuidedExtractHandler) * sizeof (GUID));
52 if (mExtractHandlerGuidTable == NULL) {
53 return RETURN_OUT_OF_RESOURCES;
54 }
55
56 mExtractDecodeHandlerTable = (EXTRACT_GUIDED_SECTION_DECODE_HANDLER *) AllocatePool (PcdGet32 (PcdMaximumGuidedExtractHandler) * sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER));
57 if (mExtractDecodeHandlerTable == NULL) {
58 return RETURN_OUT_OF_RESOURCES;
59 }
60
61 mExtractGetInfoHandlerTable = (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *) AllocatePool (PcdGet32 (PcdMaximumGuidedExtractHandler) * sizeof (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER));
62 if (mExtractGetInfoHandlerTable == NULL) {
63 return RETURN_OUT_OF_RESOURCES;
64 }
65
66 //
67 // the initialized number is Zero.
68 //
69 mNumberOfExtractHandler = 0;
70
71 return RETURN_SUCCESS;
72 }
73
74 /**
75 Get the supported exract guided section Handler guid list.
76 If ExtractHandlerGuidTable = NULL, then ASSERT.
77
78 @param[in, out] ExtractHandlerGuidTable The extract Handler guid pointer list.
79
80 @retval return the number of the supported extract guided Handler.
81 **/
82 UINTN
83 EFIAPI
84 ExtractGuidedSectionGetGuidList (
85 IN OUT GUID **ExtractHandlerGuidTable
86 )
87 {
88 ASSERT (ExtractHandlerGuidTable != NULL);
89
90 *ExtractHandlerGuidTable = mExtractHandlerGuidTable;
91 return mNumberOfExtractHandler;
92 }
93
94 /**
95 Register Guided Section Extract and GetInfo handler.
96
97 @param[in] SectionGuid The guid matches this Extraction function.
98 @param[in] GetInfoHandler Function to get info from guided section.
99 @param[in] DecodeHandler Function to extract guided section.
100
101 @retval RETURN_SUCCESS Register Guided Section Extract function successfully.
102 @retval RETURN_OUT_OF_RESOURCES Resource is not enough to register new function.
103 @retval RETURN_INVALID_PARAMETER Input pointer to Guid value is not valid.
104 **/
105 RETURN_STATUS
106 EFIAPI
107 ExtractGuidedSectionRegisterHandlers (
108 IN CONST GUID *SectionGuid,
109 IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER GetInfoHandler,
110 IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER DecodeHandler
111 )
112 {
113 if (GetBootModeHob () == BOOT_ON_S3_RESUME) {
114 //
115 // (Work around fix to bypass registeration on S3 resume.)
116 // S3 resume does not shadow DxeIpl.
117 //
118 return RETURN_SUCCESS;
119 }
120 //
121 // Check input paramter.
122 //
123 if (SectionGuid == NULL) {
124 return RETURN_INVALID_PARAMETER;
125 }
126 //
127 // Check the global table is enough to contain new Handler.
128 //
129 if (mNumberOfExtractHandler >= PcdGet32 (PcdMaximumGuidedExtractHandler)) {
130 return RETURN_OUT_OF_RESOURCES;
131 }
132
133 //
134 // Register new Handler and guid value.
135 //
136 CopyGuid (&mExtractHandlerGuidTable [mNumberOfExtractHandler], SectionGuid);
137 mExtractDecodeHandlerTable [mNumberOfExtractHandler] = DecodeHandler;
138 mExtractGetInfoHandlerTable [mNumberOfExtractHandler++] = GetInfoHandler;
139
140 return RETURN_SUCCESS;
141 }
142
143 /**
144 Get information from the guided section. This function first gets the guid value
145 from guided section header, then match this guid in the registered extract Handler list
146 to its corresponding getinfo Handler.
147 If not found, RETURN_UNSUPPORTED will be return.
148 If found, it will call the getinfo Handler to get the required size and attribute.
149
150 It will ASSERT () if the pointer to OutputBufferSize is NULL.
151 It will ASSERT () if the pointer to ScratchBufferSize is NULL.
152 It will ASSERT () if the pointer to SectionAttribute is NULL.
153
154 @param[in] InputSection Buffer containing the input GUIDed section to be processed.
155 @param[out] OutputBufferSize The size of OutputBuffer.
156 @param[out] ScratchBufferSize The size of ScratchBuffer.
157 @param[out] SectionAttribute The attribute of the input guided section.
158
159 @retval RETURN_SUCCESS Get the required information successfully.
160 @retval RETURN_UNSUPPORTED Guided section data is not supported.
161 @retval RETURN_INVALID_PARAMETER The input data can't be parsed correctly.
162
163 **/
164 RETURN_STATUS
165 EFIAPI
166 ExtractGuidedSectionGetInfo (
167 IN CONST VOID *InputSection,
168 OUT UINT32 *OutputBufferSize,
169 OUT UINT32 *ScratchBufferSize,
170 OUT UINT16 *SectionAttribute
171 )
172 {
173 UINT32 Index;
174
175 if (InputSection == NULL) {
176 return RETURN_INVALID_PARAMETER;
177 }
178
179 ASSERT (OutputBufferSize != NULL);
180 ASSERT (ScratchBufferSize != NULL);
181 ASSERT (SectionAttribute != NULL);
182
183 //
184 // Search the match registered GetInfo handler for the input guided section.
185 //
186 for (Index = 0; Index < mNumberOfExtractHandler; Index ++) {
187 if (CompareGuid (&mExtractHandlerGuidTable[Index], &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
188 break;
189 }
190 }
191
192 //
193 // Not found, the input guided section is not supported.
194 //
195 if (Index == mNumberOfExtractHandler) {
196 return RETURN_UNSUPPORTED;
197 }
198
199 //
200 // Call the match handler to getinfo for the input section data.
201 //
202 return mExtractGetInfoHandlerTable [Index] (
203 InputSection,
204 OutputBufferSize,
205 ScratchBufferSize,
206 SectionAttribute
207 );
208 }
209
210 /**
211 Extract data from the guided section. This function first gets the guid value
212 from guided section header, then match this guid in the registered extract Handler list
213 to its corresponding extract Handler.
214 If not found, RETURN_UNSUPPORTED will be return.
215 If found, it will call this extract Handler to get output data and AuthenticationStatus.
216
217 It will ASSERT () if the pointer to OutputBuffer is NULL.
218 It will ASSERT () if the pointer to AuthenticationStatus is NULL.
219
220 @param[in] InputSection Buffer containing the input GUIDed section to be processed.
221 @param[out] OutputBuffer OutputBuffer to point the start of the section's contents
222 if guided data is not required prcessing. Otherwise,
223 OutputBuffer to contain the output data, which is
224 allocated by the caller.
225 @param[out] ScratchBuffer A pointer to a caller-allocated buffer for function internal use.
226 @param[out] AuthenticationStatus
227 A pointer to a caller-allocated UINT32 that indicates the
228 authentication status of the output buffer.
229
230 @retval RETURN_SUCCESS Get the output data, size and AuthenticationStatus successfully.
231 @retval RETURN_UNSUPPORTED Guided section data is not supported to be decoded.
232 @retval RETURN_INVALID_PARAMETER The input data can't be parsed correctly.
233
234 **/
235 RETURN_STATUS
236 EFIAPI
237 ExtractGuidedSectionDecode (
238 IN CONST VOID *InputSection,
239 OUT VOID **OutputBuffer,
240 OUT VOID *ScratchBuffer, OPTIONAL
241 OUT UINT32 *AuthenticationStatus
242 )
243 {
244 UINT32 Index;
245
246 if (InputSection == NULL) {
247 return RETURN_INVALID_PARAMETER;
248 }
249
250 ASSERT (OutputBuffer != NULL);
251 ASSERT (AuthenticationStatus != NULL);
252
253 //
254 // Search the match registered GetInfo handler for the input guided section.
255 //
256 for (Index = 0; Index < mNumberOfExtractHandler; Index ++) {
257 if (CompareGuid (&mExtractHandlerGuidTable[Index], &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
258 break;
259 }
260 }
261
262 //
263 // Not found, the input guided section is not supported.
264 //
265 if (Index == mNumberOfExtractHandler) {
266 return RETURN_UNSUPPORTED;
267 }
268
269 //
270 // Call the match handler to getinfo for the input section data.
271 //
272 return mExtractDecodeHandlerTable [Index] (
273 InputSection,
274 OutputBuffer,
275 ScratchBuffer,
276 AuthenticationStatus
277 );
278 }