]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/DxeExtractGuidedSectionLib/DxeExtractGuidedSectionLib.c
add GLOBAL_REMOVE_IF_UNREFERENCED and CONST modifier.
[mirror_edk2.git] / MdePkg / Library / DxeExtractGuidedSectionLib / DxeExtractGuidedSectionLib.c
CommitLineData
8069d49e
LG
1/** @file\r
2 Provide generic extract guided section functions.\r
0fa00159 3\r
8069d49e
LG
4 Copyright (c) 2007, Intel Corporation<BR>\r
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/**
72 Get the supported exract guided section Handler guid list.\r
73 If ExtractHandlerGuidTable = NULL, then ASSERT.\r
74\r
75 @param[in, out] ExtractHandlerGuidTable The extract Handler guid pointer list.
76\r
77 @retval return the number of the supported extract guided Handler.
78**/\r
79UINTN\r
80EFIAPI\r
81ExtractGuidedSectionGetGuidList (\r
82 IN OUT GUID **ExtractHandlerGuidTable\r
83 )\r
84{\r
85 ASSERT (ExtractHandlerGuidTable != NULL);\r
86\r
87 *ExtractHandlerGuidTable = mExtractHandlerGuidTable;\r
88 return mNumberOfExtractHandler;\r
89}\r
90\r
91/**
92 Register Guided Section Extract and GetInfo handler.\r
93\r
94 @param[in] SectionGuid The guid matches this Extraction function.
95 @param[in] GetInfoHandler Function to get info from guided section.\r
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. \r
100 @retval RETURN_INVALID_PARAMETER Input pointer to Guid value is not valid.\r
101**/\r
102RETURN_STATUS\r
103EFIAPI\r
104ExtractGuidedSectionRegisterHandlers (\r
105 IN CONST GUID *SectionGuid,\r
106 IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER GetInfoHandler,\r
107 IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER DecodeHandler\r
108 )\r
109{\r
e2701217 110 UINT32 Index;\r
0fa00159
LG
111 //\r
112 // Check input paramter.\r
113 //\r
114 if (SectionGuid == NULL) {\r
115 return RETURN_INVALID_PARAMETER;\r
116 }\r
e2701217
LG
117\r
118 //\r
119 // Search the match registered GetInfo handler for the input guided section.\r
120 //\r
121 for (Index = 0; Index < mNumberOfExtractHandler; Index ++) {\r
122 if (CompareGuid (&mExtractHandlerGuidTable[Index], SectionGuid)) {\r
123 break;\r
124 }\r
125 }\r
126\r
127 //\r
128 // If the guided handler has been registered before, only update its handler.\r
129 //\r
130 if (Index < mNumberOfExtractHandler) {\r
131 mExtractDecodeHandlerTable [Index] = DecodeHandler;\r
132 mExtractGetInfoHandlerTable [Index] = GetInfoHandler;\r
133 return RETURN_SUCCESS;\r
134 }\r
135 \r
0fa00159
LG
136 //\r
137 // Check the global table is enough to contain new Handler.\r
138 //\r
139 if (mNumberOfExtractHandler >= PcdGet32 (PcdMaximumGuidedExtractHandler)) {\r
140 return RETURN_OUT_OF_RESOURCES;\r
141 }\r
142 \r
143 //\r
144 // Register new Handler and guid value.\r
145 //\r
146 CopyGuid (&mExtractHandlerGuidTable [mNumberOfExtractHandler], SectionGuid);\r
147 mExtractDecodeHandlerTable [mNumberOfExtractHandler] = DecodeHandler;\r
148 mExtractGetInfoHandlerTable [mNumberOfExtractHandler++] = GetInfoHandler;\r
149 \r
150 return RETURN_SUCCESS;\r
151}\r
152\r
153/**
154 Get information from the guided section. This function first gets the guid value\r
155 from guided section header, then match this guid in the registered extract Handler list\r
156 to its corresponding getinfo Handler. \r
e6c560aa 157 If not found, RETURN_INVALID_PARAMETER will be return. \r
0fa00159
LG
158 If found, it will call the getinfo Handler to get the required size and attribute.\r
159\r
160 It will ASSERT () if the pointer to OutputBufferSize is NULL.\r
161 It will ASSERT () if the pointer to ScratchBufferSize is NULL.
162 It will ASSERT () if the pointer to SectionAttribute is NULL.\r
163\r
164 @param[in] InputSection Buffer containing the input GUIDed section to be processed. \r
165 @param[out] OutputBufferSize The size of OutputBuffer.\r
166 @param[out] ScratchBufferSize The size of ScratchBuffer. \r
167 @param[out] SectionAttribute The attribute of the input guided section.\r
168
169 @retval RETURN_SUCCESS Get the required information successfully.\r
e6c560aa
LG
170 @retval RETURN_INVALID_PARAMETER The input data can't be parsed correctly. \r
171 The GUID in InputSection does not match any registered guid list.\r
0fa00159
LG
172\r
173**/\r
174RETURN_STATUS\r
175EFIAPI\r
176ExtractGuidedSectionGetInfo (\r
177 IN CONST VOID *InputSection,\r
178 OUT UINT32 *OutputBufferSize,\r
179 OUT UINT32 *ScratchBufferSize,\r
180 OUT UINT16 *SectionAttribute \r
181 )\r
182{\r
183 UINT32 Index;\r
184 \r
185 if (InputSection == NULL) {\r
186 return RETURN_INVALID_PARAMETER;\r
187 }\r
188 \r
189 ASSERT (OutputBufferSize != NULL);\r
190 ASSERT (ScratchBufferSize != NULL);\r
191 ASSERT (SectionAttribute != NULL);\r
192 \r
193 //\r
194 // Search the match registered GetInfo handler for the input guided section.\r
195 //\r
196 for (Index = 0; Index < mNumberOfExtractHandler; Index ++) {\r
197 if (CompareGuid (&mExtractHandlerGuidTable[Index], &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
198 break;\r
199 }\r
200 }\r
201\r
202 //\r
203 // Not found, the input guided section is not supported. \r
204 //\r
205 if (Index == mNumberOfExtractHandler) {\r
e6c560aa 206 return RETURN_INVALID_PARAMETER;\r
0fa00159
LG
207 }\r
208\r
209 //\r
210 // Call the match handler to getinfo for the input section data.\r
211 //\r
212 return mExtractGetInfoHandlerTable [Index] (\r
213 InputSection,\r
214 OutputBufferSize,\r
215 ScratchBufferSize,\r
216 SectionAttribute\r
217 );\r
218}\r
219\r
220/**
221 Extract data from the guided section. This function first gets the guid value\r
222 from guided section header, then match this guid in the registered extract Handler list\r
223 to its corresponding extract Handler. \r
e6c560aa 224 If not found, RETURN_INVALID_PARAMETER will be return. \r
0fa00159
LG
225 If found, it will call this extract Handler to get output data and AuthenticationStatus.
226\r
227 It will ASSERT () if the pointer to OutputBuffer is NULL.\r
228 It will ASSERT () if the pointer to AuthenticationStatus is NULL.\r
229\r
230 @param[in] InputSection Buffer containing the input GUIDed section to be processed. \r
231 @param[out] OutputBuffer OutputBuffer to point the start of the section's contents \r
232 if guided data is not required prcessing. Otherwise,\r
233 OutputBuffer to contain the output data, which is \r
234 allocated by the caller.\r
235 @param[out] ScratchBuffer A pointer to a caller-allocated buffer for function internal use. \r
236 @param[out] AuthenticationStatus \r
237 A pointer to a caller-allocated UINT32 that indicates the\r
238 authentication status of the output buffer.
239
240 @retval RETURN_SUCCESS Get the output data, size and AuthenticationStatus successfully.\r
e6c560aa
LG
241 @retval RETURN_INVALID_PARAMETER The input data can't be parsed correctly. \r
242 The GUID in InputSection does not match any registered guid.\r
0fa00159
LG
243
244**/\r
245RETURN_STATUS\r
246EFIAPI\r
247ExtractGuidedSectionDecode (\r
248 IN CONST VOID *InputSection,\r
249 OUT VOID **OutputBuffer,\r
250 OUT VOID *ScratchBuffer, OPTIONAL\r
251 OUT UINT32 *AuthenticationStatus \r
252 )\r
253{\r
254 UINT32 Index;\r
255 \r
256 if (InputSection == NULL) {\r
257 return RETURN_INVALID_PARAMETER;\r
258 }\r
259 \r
260 ASSERT (OutputBuffer != NULL);\r
261 ASSERT (AuthenticationStatus != NULL);\r
262\r
263 //\r
264 // Search the match registered GetInfo handler for the input guided section.\r
265 //\r
266 for (Index = 0; Index < mNumberOfExtractHandler; Index ++) {\r
267 if (CompareGuid (&mExtractHandlerGuidTable[Index], &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
268 break;\r
269 }\r
270 }\r
271\r
272 //\r
273 // Not found, the input guided section is not supported. \r
274 //\r
275 if (Index == mNumberOfExtractHandler) {\r
e6c560aa 276 return RETURN_INVALID_PARAMETER;\r
0fa00159
LG
277 }\r
278\r
279 //\r
280 // Call the match handler to getinfo for the input section data.\r
281 //\r
282 return mExtractDecodeHandlerTable [Index] (\r
283 InputSection,\r
284 OutputBuffer,\r
285 ScratchBuffer,\r
286 AuthenticationStatus\r
287 );\r
288}\r