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