]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/DxeExtractGuidedSectionLib/DxeExtractGuidedSectionLib.c
Update to use DOS format
[mirror_edk2.git] / MdePkg / Library / DxeExtractGuidedSectionLib / DxeExtractGuidedSectionLib.c
1 /** @file
2 Provide generic extract guided section functions for Dxe phase.
3
4 Copyright (c) 2007 - 2008, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include <PiDxe.h>
16
17 #include <Library/DebugLib.h>
18 #include <Library/PcdLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/MemoryAllocationLib.h>
21 #include <Library/ExtractGuidedSectionLib.h>
22
23 GUID *mExtractHandlerGuidTable;
24 UINT32 mNumberOfExtractHandler = 0;
25
26 EXTRACT_GUIDED_SECTION_DECODE_HANDLER *mExtractDecodeHandlerTable;
27 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *mExtractGetInfoHandlerTable;
28
29 /**
30 Construtor allocates the global memory to store the registered guid and Handler list.
31
32 @param ImageHandle The firmware allocated handle for the EFI image.
33 @param SystemTable A pointer to the EFI System Table.
34
35 @retval RETURN_SUCCESS Allocate the global memory space to store guid and funciton tables.
36 @retval RETURN_OUT_OF_RESOURCES No enough memory to allocated.
37 **/
38 RETURN_STATUS
39 EFIAPI
40 DxeExtractGuidedSectionLibConstructor (
41 IN EFI_HANDLE ImageHandle,
42 IN EFI_SYSTEM_TABLE *SystemTable
43 )
44 {
45 //
46 // Allocate global pool space to store the registered handler and its guid value.
47 //
48 mExtractHandlerGuidTable = (GUID *) AllocatePool (PcdGet32 (PcdMaximumGuidedExtractHandler) * sizeof (GUID));
49 if (mExtractHandlerGuidTable == NULL) {
50 return RETURN_OUT_OF_RESOURCES;
51 }
52
53 mExtractDecodeHandlerTable = (EXTRACT_GUIDED_SECTION_DECODE_HANDLER *) AllocatePool (PcdGet32 (PcdMaximumGuidedExtractHandler) * sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER));
54 if (mExtractDecodeHandlerTable == NULL) {
55 FreePool (mExtractHandlerGuidTable);
56 return RETURN_OUT_OF_RESOURCES;
57 }
58
59 mExtractGetInfoHandlerTable = (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *) AllocatePool (PcdGet32 (PcdMaximumGuidedExtractHandler) * sizeof (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER));
60 if (mExtractGetInfoHandlerTable == NULL) {
61 FreePool (mExtractHandlerGuidTable);
62 FreePool (mExtractDecodeHandlerTable);
63 return RETURN_OUT_OF_RESOURCES;
64 }
65
66 return RETURN_SUCCESS;
67 }
68
69 /**
70 Get the supported exract guided section Handler guid table, which is maintained
71 by library. The caller can directly get the guid table
72 without responsibility to allocate or free this table buffer.
73 It will ASSERT () if ExtractHandlerGuidTable = NULL.
74
75 @param[out] ExtractHandlerGuidTable The extract Handler guid pointer list.
76
77 @return the number of the supported extract guided Handler.
78 **/
79 UINTN
80 EFIAPI
81 ExtractGuidedSectionGetGuidList (
82 OUT GUID **ExtractHandlerGuidTable
83 )
84 {
85 ASSERT (ExtractHandlerGuidTable != NULL);
86
87 *ExtractHandlerGuidTable = mExtractHandlerGuidTable;
88 return mNumberOfExtractHandler;
89 }
90
91 /**
92 Register Guided Section Extract and GetInfo handler.
93
94 @param[in] SectionGuid The guid matches this Extraction function.
95 @param[in] GetInfoHandler Function to get info from guided section.
96 @param[in] DecodeHandler Function to extract guided section.
97
98 @retval RETURN_SUCCESS Register Guided Section Extract function successfully.
99 @retval RETURN_OUT_OF_RESOURCES Resource is not enough to register new function.
100 @retval RETURN_INVALID_PARAMETER Input pointer to Guid value is not valid.
101 **/
102 RETURN_STATUS
103 EFIAPI
104 ExtractGuidedSectionRegisterHandlers (
105 IN CONST GUID *SectionGuid,
106 IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER GetInfoHandler,
107 IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER DecodeHandler
108 )
109 {
110 UINT32 Index;
111 //
112 // Check input paramter.
113 //
114 if (SectionGuid == NULL) {
115 return RETURN_INVALID_PARAMETER;
116 }
117
118 //
119 // Search the match registered GetInfo handler for the input guided section.
120 //
121 for (Index = 0; Index < mNumberOfExtractHandler; Index ++) {
122 if (CompareGuid (&mExtractHandlerGuidTable[Index], SectionGuid)) {
123 //
124 // If the guided handler has been registered before, only update its handler.
125 //
126 mExtractDecodeHandlerTable [Index] = DecodeHandler;
127 mExtractGetInfoHandlerTable [Index] = GetInfoHandler;
128 return RETURN_SUCCESS;
129 }
130 }
131
132 //
133 // Check the global table is enough to contain new Handler.
134 //
135 if (mNumberOfExtractHandler >= PcdGet32 (PcdMaximumGuidedExtractHandler)) {
136 return RETURN_OUT_OF_RESOURCES;
137 }
138
139 //
140 // Register new Handler and guid value.
141 //
142 CopyGuid (&mExtractHandlerGuidTable [mNumberOfExtractHandler], SectionGuid);
143 mExtractDecodeHandlerTable [mNumberOfExtractHandler] = DecodeHandler;
144 mExtractGetInfoHandlerTable [mNumberOfExtractHandler++] = GetInfoHandler;
145
146 return RETURN_SUCCESS;
147 }
148
149 /**
150 Get information from the guided section. This function first gets the guid value
151 from guided section header, then match this guid in the registered extract Handler list
152 to its corresponding getinfo Handler.
153 If not found, RETURN_INVALID_PARAMETER will be return.
154 If found, it will call the getinfo Handler to get the required size and attribute.
155
156 It will ASSERT () if the pointer to OutputBufferSize is NULL.
157 It will ASSERT () if the pointer to ScratchBufferSize is NULL.
158 It will ASSERT () if the pointer to SectionAttribute is NULL.
159
160 @param[in] InputSection Buffer containing the input GUIDed section to be processed.
161 @param[out] OutputBufferSize The size of OutputBuffer.
162 @param[out] ScratchBufferSize The size of ScratchBuffer.
163 @param[out] SectionAttribute The attribute of the input guided section.
164
165 @retval RETURN_SUCCESS Get the required information successfully.
166 @retval RETURN_UNSUPPORTED Guided section data is not supported.
167 @retval RETURN_INVALID_PARAMETER The input data is not the valid guided section.
168
169 **/
170 RETURN_STATUS
171 EFIAPI
172 ExtractGuidedSectionGetInfo (
173 IN CONST VOID *InputSection,
174 OUT UINT32 *OutputBufferSize,
175 OUT UINT32 *ScratchBufferSize,
176 OUT UINT16 *SectionAttribute
177 )
178 {
179 UINT32 Index;
180
181 if (InputSection == NULL) {
182 return RETURN_INVALID_PARAMETER;
183 }
184
185 ASSERT (OutputBufferSize != NULL);
186 ASSERT (ScratchBufferSize != NULL);
187 ASSERT (SectionAttribute != NULL);
188
189 //
190 // Search the match registered GetInfo handler for the input guided section.
191 //
192 for (Index = 0; Index < mNumberOfExtractHandler; Index ++) {
193 if (CompareGuid (&mExtractHandlerGuidTable[Index], &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
194 //
195 // Call the match handler to getinfo for the input section data.
196 //
197 return mExtractGetInfoHandlerTable [Index] (
198 InputSection,
199 OutputBufferSize,
200 ScratchBufferSize,
201 SectionAttribute
202 );
203 }
204 }
205
206 //
207 // Not found, the input guided section is not supported.
208 //
209 return RETURN_UNSUPPORTED;
210 }
211
212 /**
213 Extract data from the guided section. This function first gets the guid value
214 from guided section header, then match this guid in the registered extract Handler list
215 to its corresponding extract Handler.
216 If not found, RETURN_INVALID_PARAMETER will be return.
217 If found, it will call this extract Handler to get output data and AuthenticationStatus.
218
219 It will ASSERT () if the pointer to OutputBuffer is NULL.
220 It will ASSERT () if the pointer to AuthenticationStatus is NULL.
221
222 @param[in] InputSection Buffer containing the input GUIDed section to be processed.
223 @param[out] OutputBuffer OutputBuffer to point the start of the section's contents
224 if guided data is not required prcessing. Otherwise,
225 OutputBuffer to contain the output data, which is
226 allocated by the caller.
227 @param[out] ScratchBuffer A pointer to a caller-allocated buffer for function internal use.
228 @param[out] AuthenticationStatus
229 A pointer to a caller-allocated UINT32 that indicates the
230 authentication status of the output buffer.
231
232 @retval RETURN_SUCCESS Get the output data and AuthenticationStatus successfully.
233 @retval RETURN_UNSUPPORTED Guided section data is not supported to be decoded.
234 @retval RETURN_INVALID_PARAMETER The input data is not the valid guided section.
235
236 **/
237 RETURN_STATUS
238 EFIAPI
239 ExtractGuidedSectionDecode (
240 IN CONST VOID *InputSection,
241 OUT VOID **OutputBuffer,
242 OUT VOID *ScratchBuffer, OPTIONAL
243 OUT UINT32 *AuthenticationStatus
244 )
245 {
246 UINT32 Index;
247
248 //
249 // Check the input parameters
250 //
251 if (InputSection == NULL) {
252 return RETURN_INVALID_PARAMETER;
253 }
254
255 ASSERT (OutputBuffer != NULL);
256 ASSERT (AuthenticationStatus != NULL);
257
258 //
259 // Search the match registered extract handler for the input guided section.
260 //
261 for (Index = 0; Index < mNumberOfExtractHandler; Index ++) {
262 if (CompareGuid (&mExtractHandlerGuidTable[Index], &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
263 //
264 // Call the match handler to extract raw data for the input section data.
265 //
266 return mExtractDecodeHandlerTable [Index] (
267 InputSection,
268 OutputBuffer,
269 ScratchBuffer,
270 AuthenticationStatus
271 );
272 }
273 }
274
275 //
276 // Not found, the input guided section is not supported.
277 //
278 return RETURN_UNSUPPORTED;
279 }