]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseExtractGuidedSectionLib/BaseExtractGuidedSectionLib.c
remove --image-base option EfiLdr.inf, and use GenFw to do image base relocation
[mirror_edk2.git] / MdePkg / Library / BaseExtractGuidedSectionLib / BaseExtractGuidedSectionLib.c
CommitLineData
b9d5a7f1
LG
1/** @file\r
2 Provide generic extract guided section functions.\r
3\r
4 Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>\r
5 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
9\r
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
12\r
13**/\r
14\r
15#include <PiPei.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/ExtractGuidedSectionLib.h>\r
21\r
22#define EXTRACT_HANDLER_INFO_SIGNATURE SIGNATURE_32 ('E', 'G', 'S', 'I')\r
23\r
24typedef struct {\r
25 UINT32 Signature;\r
26 UINT32 NumberOfExtractHandler;\r
27 GUID *ExtractHandlerGuidTable;\r
28 EXTRACT_GUIDED_SECTION_DECODE_HANDLER *ExtractDecodeHandlerTable;\r
29 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *ExtractGetInfoHandlerTable;\r
30} EXTRACT_GUIDED_SECTION_HANDLER_INFO;\r
31\r
32/**\r
33 HandlerInfo table address is set by PcdGuidedExtractHandlerTableAddress, which is used to store \r
34 the registered guid and Handler list. When it is initialized, it will be directly returned. \r
35 Or, HandlerInfo table will be initialized in this function.\r
36\r
37 @param[in, out] InfoPointer Pointer to the handler info structure.\r
38\r
39 @retval RETURN_SUCCESS HandlerInfo table can be used to store guid and function tables.\r
40 @retval RETURN_OUT_OF_RESOURCES HandlerInfo table address is not writable.\r
41**/\r
42RETURN_STATUS\r
43GetExtractGuidedSectionHandlerInfo (\r
44 IN OUT EXTRACT_GUIDED_SECTION_HANDLER_INFO **InfoPointer\r
45 )\r
46{\r
47 EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
48 \r
49 //\r
50 // Set the available memory address to handler info.\r
51 //\r
52 HandlerInfo = (EXTRACT_GUIDED_SECTION_HANDLER_INFO*)(VOID*)(UINTN) PcdGet64 (PcdGuidedExtractHandlerTableAddress);\r
53\r
54 //\r
55 // First check whether the handler info structure is initialized.\r
56 //\r
57 if (HandlerInfo->Signature == EXTRACT_HANDLER_INFO_SIGNATURE) {\r
58 //\r
59 // The handler info has been initialized and is returned.\r
60 //\r
61 *InfoPointer = HandlerInfo;\r
62 return RETURN_SUCCESS;\r
63 }\r
64\r
65 //\r
66 // Try to initialize the handler info structure\r
67 //\r
68 HandlerInfo->Signature = EXTRACT_HANDLER_INFO_SIGNATURE;\r
69 if (HandlerInfo->Signature != EXTRACT_HANDLER_INFO_SIGNATURE) {\r
70 //\r
71 // The handler info structure was not writeable because the memory is not ready.\r
72 //\r
73 *InfoPointer = NULL;\r
74 return RETURN_OUT_OF_RESOURCES;\r
75 }\r
76\r
77 //\r
78 // Init HandlerInfo structure\r
79 //\r
80 HandlerInfo->NumberOfExtractHandler = 0;\r
81 HandlerInfo->ExtractHandlerGuidTable = (GUID *) (HandlerInfo + 1);\r
82 HandlerInfo->ExtractDecodeHandlerTable = (EXTRACT_GUIDED_SECTION_DECODE_HANDLER *) (\r
83 (UINT8 *)HandlerInfo->ExtractHandlerGuidTable + \r
84 PcdGet32 (PcdMaximumGuidedExtractHandler) * sizeof (GUID)\r
85 );\r
86 HandlerInfo->ExtractGetInfoHandlerTable = (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *) (\r
87 (UINT8 *)HandlerInfo->ExtractDecodeHandlerTable + \r
88 PcdGet32 (PcdMaximumGuidedExtractHandler) * \r
89 sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER)\r
90 );\r
91 *InfoPointer = HandlerInfo;\r
92 return RETURN_SUCCESS;\r
93}\r
94\r
95/**\r
96 Retrieve the list GUIDs that have been registered through ExtractGuidedSectionRegisterHandlers().\r
97\r
98 Sets ExtractHandlerGuidTable so it points at a callee allocated array of registered GUIDs.\r
99 The total number of GUIDs in the array are returned. Since the array of GUIDs is callee allocated\r
100 and caller must treat this array of GUIDs as read-only data. \r
101 If ExtractHandlerGuidTable is NULL, then ASSERT().\r
102\r
103 @param[out] ExtractHandlerGuidTable A pointer to the array of GUIDs that have been registered through\r
104 ExtractGuidedSectionRegisterHandlers().\r
105\r
106 @return the number of the supported extract guided Handler.\r
107\r
108**/\r
109UINTN\r
110EFIAPI\r
111ExtractGuidedSectionGetGuidList (\r
112 OUT GUID **ExtractHandlerGuidTable\r
113 )\r
114{\r
115 RETURN_STATUS Status;\r
116 EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
117\r
118 ASSERT (ExtractHandlerGuidTable != NULL);\r
119\r
120 //\r
121 // Get all registered handler information\r
122 //\r
123 Status = GetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
124 if (RETURN_ERROR (Status)) {\r
125 *ExtractHandlerGuidTable = NULL;\r
126 return 0;\r
127 }\r
128\r
129 //\r
130 // Get GuidTable and Table Number\r
131 //\r
132 *ExtractHandlerGuidTable = HandlerInfo->ExtractHandlerGuidTable;\r
133 return HandlerInfo->NumberOfExtractHandler;\r
134}\r
135\r
136/**\r
137 Registers handlers of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER and EXTRACT_GUIDED_SECTION_DECODE_HANDLER\r
138 for a specific GUID section type.\r
139\r
140 Registers the handlers specified by GetInfoHandler and DecodeHandler with the GUID specified by SectionGuid.\r
141 If the GUID value specified by SectionGuid has already been registered, then return RETURN_ALREADY_STARTED.\r
142 If there are not enough resources available to register the handlers then RETURN_OUT_OF_RESOURCES is returned.\r
143 \r
144 If SectionGuid is NULL, then ASSERT().\r
145 If GetInfoHandler is NULL, then ASSERT().\r
146 If DecodeHandler is NULL, then ASSERT().\r
147\r
148 @param[in] SectionGuid A pointer to the GUID associated with the the handlers\r
149 of the GUIDed section type being registered.\r
150 @param[in] GetInfoHandler Pointer to a function that examines a GUIDed section and returns the\r
151 size of the decoded buffer and the size of an optional scratch buffer\r
152 required to actually decode the data in a GUIDed section.\r
153 @param[in] DecodeHandler Pointer to a function that decodes a GUIDed section into a caller\r
154 allocated output buffer. \r
155\r
156 @retval RETURN_SUCCESS The handlers were registered.\r
157 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to register the handlers.\r
158\r
159**/\r
160RETURN_STATUS\r
161EFIAPI\r
162ExtractGuidedSectionRegisterHandlers (\r
163 IN CONST GUID *SectionGuid,\r
164 IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER GetInfoHandler,\r
165 IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER DecodeHandler\r
166 )\r
167{\r
168 UINT32 Index;\r
169 RETURN_STATUS Status;\r
170 EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
171\r
172 //\r
173 // Check input paramter\r
174 //\r
175 ASSERT (SectionGuid != NULL);\r
176 ASSERT (GetInfoHandler != NULL);\r
177 ASSERT (DecodeHandler != NULL);\r
178\r
179 //\r
180 // Get the registered handler information\r
181 //\r
182 Status = GetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
183 if (RETURN_ERROR (Status)) {\r
184 return Status;\r
185 }\r
186\r
187 //\r
188 // Search the match registered GetInfo handler for the input guided section.\r
189 //\r
190 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index ++) {\r
191 if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, SectionGuid)) {\r
192 //\r
193 // If the guided handler has been registered before, only update its handler.\r
194 //\r
195 HandlerInfo->ExtractDecodeHandlerTable [Index] = DecodeHandler;\r
196 HandlerInfo->ExtractGetInfoHandlerTable [Index] = GetInfoHandler;\r
197 return RETURN_SUCCESS;\r
198 }\r
199 }\r
200\r
201 //\r
202 // Check the global table is enough to contain new Handler.\r
203 //\r
204 if (HandlerInfo->NumberOfExtractHandler >= PcdGet32 (PcdMaximumGuidedExtractHandler)) {\r
205 return RETURN_OUT_OF_RESOURCES;\r
206 }\r
207 \r
208 //\r
209 // Register new Handler and guid value.\r
210 //\r
211 CopyGuid (HandlerInfo->ExtractHandlerGuidTable + HandlerInfo->NumberOfExtractHandler, SectionGuid);\r
212 HandlerInfo->ExtractDecodeHandlerTable [HandlerInfo->NumberOfExtractHandler] = DecodeHandler;\r
213 HandlerInfo->ExtractGetInfoHandlerTable [HandlerInfo->NumberOfExtractHandler++] = GetInfoHandler;\r
214\r
215 return RETURN_SUCCESS;\r
216}\r
217\r
218/**\r
219 Retrieves a GUID from a GUIDed section and uses that GUID to select an associated handler of type\r
220 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().\r
221 The selected handler is used to retrieve and return the size of the decoded buffer and the size of an\r
222 optional scratch buffer required to actually decode the data in a GUIDed section.\r
223\r
224 Examines a GUIDed section specified by InputSection. \r
225 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),\r
226 then RETURN_UNSUPPORTED is returned. \r
227 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler \r
228 of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()\r
229 is used to retrieve the OututBufferSize, ScratchSize, and Attributes values. The return status from the handler of\r
230 type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER is returned.\r
231 \r
232 If InputSection is NULL, then ASSERT().\r
233 If OutputBufferSize is NULL, then ASSERT().\r
234 If ScratchBufferSize is NULL, then ASSERT().\r
235 If SectionAttribute is NULL, then ASSERT().\r
236\r
237 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.\r
238 @param[out] OutputBufferSize A pointer to the size, in bytes, of an output buffer required if the buffer\r
239 specified by InputSection were decoded.\r
240 @param[out] ScratchBufferSize A pointer to the size, in bytes, required as scratch space if the buffer specified by\r
241 InputSection were decoded.\r
242 @param[out] SectionAttribute A pointer to the attributes of the GUIDed section. See the Attributes field of\r
243 EFI_GUID_DEFINED_SECTION in the PI Specification.\r
244\r
245 @retval RETURN_SUCCESS Get the required information successfully.\r
246 @retval RETURN_UNSUPPORTED The GUID from the section specified by InputSection does not match any of\r
247 the GUIDs registered with ExtractGuidedSectionRegisterHandlers().\r
248 @retval Others The return status from the handler associated with the GUID retrieved from\r
249 the section specified by InputSection.\r
250\r
251**/\r
252RETURN_STATUS\r
253EFIAPI\r
254ExtractGuidedSectionGetInfo (\r
255 IN CONST VOID *InputSection,\r
256 OUT UINT32 *OutputBufferSize,\r
257 OUT UINT32 *ScratchBufferSize,\r
258 OUT UINT16 *SectionAttribute \r
259 )\r
260{\r
261 UINT32 Index;\r
262 RETURN_STATUS Status;\r
263 EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
264 \r
265 //\r
266 // Check input paramter\r
267 //\r
268 ASSERT (InputSection != NULL);\r
269 ASSERT (OutputBufferSize != NULL);\r
270 ASSERT (ScratchBufferSize != NULL);\r
271 ASSERT (SectionAttribute != NULL);\r
272\r
273 //\r
274 // Get all registered handler information.\r
275 //\r
276 Status = GetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
277 if (RETURN_ERROR (Status)) {\r
278 return Status;\r
279 }\r
280\r
281 //\r
282 // Search the match registered GetInfo handler for the input guided section.\r
283 //\r
284 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index ++) {\r
285 if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
286 //\r
287 // Call the match handler to get info for the input section data.\r
288 //\r
289 return HandlerInfo->ExtractGetInfoHandlerTable [Index] (\r
290 InputSection,\r
291 OutputBufferSize,\r
292 ScratchBufferSize,\r
293 SectionAttribute\r
294 );\r
295 }\r
296 }\r
297\r
298 //\r
299 // Not found, the input guided section is not supported. \r
300 //\r
301 return RETURN_UNSUPPORTED;\r
302}\r
303\r
304/**\r
305 Retrieves the GUID from a GUIDed section and uses that GUID to select an associated handler of type\r
306 EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().\r
307 The selected handler is used to decode the data in a GUIDed section and return the result in a caller\r
308 allocated output buffer.\r
309\r
310 Decodes the GUIDed section specified by InputSection. \r
311 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),\r
312 then RETURN_UNSUPPORTED is returned. \r
313 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler\r
314 of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()\r
315 is used to decode InputSection into the buffer specified by OutputBuffer and the authentication status of this\r
316 decode operation is returned in AuthenticationStatus. If the decoded buffer is identical to the data in InputSection,\r
317 then OutputBuffer is set to point at the data in InputSection. Otherwise, the decoded data will be placed in caller\r
318 allocated buffer specified by OutputBuffer. This function is responsible for computing the EFI_AUTH_STATUS_PLATFORM_OVERRIDE\r
319 bit of in AuthenticationStatus. The return status from the handler of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER is returned. \r
320 \r
321 If InputSection is NULL, then ASSERT().\r
322 If OutputBuffer is NULL, then ASSERT().\r
323 If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().\r
324 If AuthenticationStatus is NULL, then ASSERT(). \r
325\r
326 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.\r
327 @param[out] OutputBuffer A pointer to a buffer that contains the result of a decode operation. \r
328 @param[in] ScratchBuffer A caller allocated buffer that may be required by this function as a scratch buffer to perform the decode operation. \r
329 @param[out] AuthenticationStatus \r
330 A pointer to the authentication status of the decoded output buffer. See the definition\r
331 of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI section of the PI\r
332 Specification.\r
333\r
334 @retval RETURN_SUCCESS The buffer specified by InputSection was decoded.\r
335 @retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports.\r
336 @retval RETURN_INVALID_PARAMETER The section specified by InputSection can not be decoded.\r
337\r
338**/\r
339RETURN_STATUS\r
340EFIAPI\r
341ExtractGuidedSectionDecode (\r
342 IN CONST VOID *InputSection,\r
343 OUT VOID **OutputBuffer,\r
344 IN VOID *ScratchBuffer, OPTIONAL\r
345 OUT UINT32 *AuthenticationStatus \r
346 )\r
347{\r
348 UINT32 Index;\r
349 RETURN_STATUS Status;\r
350 EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
351 \r
352 //\r
353 // Check input parameter\r
354 //\r
355 ASSERT (InputSection != NULL);\r
356 ASSERT (OutputBuffer != NULL);\r
357 ASSERT (AuthenticationStatus != NULL);\r
358\r
359 //\r
360 // Get all registered handler information.\r
361 // \r
362 Status = GetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
363 if (RETURN_ERROR (Status)) {\r
364 return Status;\r
365 }\r
366\r
367 //\r
368 // Search the match registered Extract handler for the input guided section.\r
369 //\r
370 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index ++) {\r
371 if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
372 //\r
373 // Call the match handler to extract raw data for the input guided section.\r
374 //\r
375 return HandlerInfo->ExtractDecodeHandlerTable [Index] (\r
376 InputSection,\r
377 OutputBuffer,\r
378 ScratchBuffer,\r
379 AuthenticationStatus\r
380 );\r
381 }\r
382 }\r
383\r
384 //\r
385 // Not found, the input guided section is not supported. \r
386 //\r
387 return RETURN_UNSUPPORTED;\r
388}\r