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