]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseExtractGuidedSectionLib/BaseExtractGuidedSectionLib.c
Remove ASSERT to let DXE core return gracefully when loading an invalid image.
[mirror_edk2.git] / MdePkg / Library / BaseExtractGuidedSectionLib / BaseExtractGuidedSectionLib.c
CommitLineData
b9d5a7f1
LG
1/** @file\r
2 Provide generic extract guided section functions.\r
3\r
30f001ca 4 Copyright (c) 2007 - 2011, Intel Corporation. All rights reserved.<BR>\r
b9d5a7f1
LG
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
35a17154 8 http://opensource.org/licenses/bsd-license.php.\r
b9d5a7f1
LG
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
58380e9c 37 @param[in, out] InfoPointer The pointer to the handler information structure.\r
b9d5a7f1
LG
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
58380e9c 55 // First check whether the handler information structure is initialized.\r
b9d5a7f1
LG
56 //\r
57 if (HandlerInfo->Signature == EXTRACT_HANDLER_INFO_SIGNATURE) {\r
58 //\r
58380e9c 59 // The handler information has been initialized and is returned.\r
b9d5a7f1
LG
60 //\r
61 *InfoPointer = HandlerInfo;\r
62 return RETURN_SUCCESS;\r
63 }\r
64\r
65 //\r
58380e9c 66 // Try to initialize the handler information structure\r
b9d5a7f1
LG
67 //\r
68 HandlerInfo->Signature = EXTRACT_HANDLER_INFO_SIGNATURE;\r
69 if (HandlerInfo->Signature != EXTRACT_HANDLER_INFO_SIGNATURE) {\r
70 //\r
58380e9c 71 // The handler information structure was not writeable because the memory is not ready.\r
b9d5a7f1
LG
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
35a17154 106 @return The number of the supported extract guided Handler.\r
b9d5a7f1
LG
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
2fc59a00 150 @param[in] GetInfoHandler The pointer to a function that examines a GUIDed section and returns the\r
b9d5a7f1
LG
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
2fc59a00 153 @param[in] DecodeHandler The pointer to a function that decodes a GUIDed section into a caller\r
b9d5a7f1
LG
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
35a17154 245 @retval RETURN_SUCCESS Successfully retrieved the required information.\r
b9d5a7f1
LG
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
30f001ca 264 EFI_GUID *SectionDefinitionGuid;\r
b9d5a7f1
LG
265 \r
266 //\r
267 // Check input paramter\r
268 //\r
269 ASSERT (InputSection != NULL);\r
270 ASSERT (OutputBufferSize != NULL);\r
271 ASSERT (ScratchBufferSize != NULL);\r
272 ASSERT (SectionAttribute != NULL);\r
273\r
274 //\r
275 // Get all registered handler information.\r
276 //\r
277 Status = GetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
278 if (RETURN_ERROR (Status)) {\r
279 return Status;\r
280 }\r
281\r
30f001ca
SZ
282 if (IS_SECTION2 (InputSection)) {\r
283 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid);\r
284 } else {\r
285 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid);\r
286 }\r
287\r
b9d5a7f1
LG
288 //\r
289 // Search the match registered GetInfo handler for the input guided section.\r
290 //\r
291 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index ++) {\r
30f001ca 292 if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, SectionDefinitionGuid)) {\r
b9d5a7f1 293 //\r
58380e9c 294 // Call the match handler to get information for the input section data.\r
b9d5a7f1
LG
295 //\r
296 return HandlerInfo->ExtractGetInfoHandlerTable [Index] (\r
297 InputSection,\r
298 OutputBufferSize,\r
299 ScratchBufferSize,\r
300 SectionAttribute\r
301 );\r
302 }\r
303 }\r
304\r
305 //\r
306 // Not found, the input guided section is not supported. \r
307 //\r
308 return RETURN_UNSUPPORTED;\r
309}\r
310\r
311/**\r
312 Retrieves the GUID from a GUIDed section and uses that GUID to select an associated handler of type\r
313 EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().\r
314 The selected handler is used to decode the data in a GUIDed section and return the result in a caller\r
315 allocated output buffer.\r
316\r
317 Decodes the GUIDed section specified by InputSection. \r
318 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),\r
319 then RETURN_UNSUPPORTED is returned. \r
320 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler\r
321 of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()\r
322 is used to decode InputSection into the buffer specified by OutputBuffer and the authentication status of this\r
323 decode operation is returned in AuthenticationStatus. If the decoded buffer is identical to the data in InputSection,\r
35a17154 324 then OutputBuffer is set to point at the data in InputSection. Otherwise, the decoded data will be placed in a caller\r
b9d5a7f1
LG
325 allocated buffer specified by OutputBuffer. This function is responsible for computing the EFI_AUTH_STATUS_PLATFORM_OVERRIDE\r
326 bit of in AuthenticationStatus. The return status from the handler of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER is returned. \r
327 \r
328 If InputSection is NULL, then ASSERT().\r
329 If OutputBuffer is NULL, then ASSERT().\r
330 If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().\r
331 If AuthenticationStatus is NULL, then ASSERT(). \r
332\r
333 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.\r
334 @param[out] OutputBuffer A pointer to a buffer that contains the result of a decode operation. \r
335 @param[in] ScratchBuffer A caller allocated buffer that may be required by this function as a scratch buffer to perform the decode operation. \r
336 @param[out] AuthenticationStatus \r
337 A pointer to the authentication status of the decoded output buffer. See the definition\r
338 of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI section of the PI\r
339 Specification.\r
340\r
341 @retval RETURN_SUCCESS The buffer specified by InputSection was decoded.\r
342 @retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports.\r
343 @retval RETURN_INVALID_PARAMETER The section specified by InputSection can not be decoded.\r
344\r
345**/\r
346RETURN_STATUS\r
347EFIAPI\r
348ExtractGuidedSectionDecode (\r
349 IN CONST VOID *InputSection,\r
350 OUT VOID **OutputBuffer,\r
351 IN VOID *ScratchBuffer, OPTIONAL\r
352 OUT UINT32 *AuthenticationStatus \r
353 )\r
354{\r
355 UINT32 Index;\r
356 RETURN_STATUS Status;\r
357 EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
30f001ca 358 EFI_GUID *SectionDefinitionGuid;\r
b9d5a7f1
LG
359 \r
360 //\r
361 // Check input parameter\r
362 //\r
363 ASSERT (InputSection != NULL);\r
364 ASSERT (OutputBuffer != NULL);\r
365 ASSERT (AuthenticationStatus != NULL);\r
366\r
367 //\r
368 // Get all registered handler information.\r
369 // \r
370 Status = GetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
371 if (RETURN_ERROR (Status)) {\r
372 return Status;\r
373 }\r
374\r
30f001ca
SZ
375 if (IS_SECTION2 (InputSection)) {\r
376 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid);\r
377 } else {\r
378 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid);\r
379 }\r
380\r
b9d5a7f1
LG
381 //\r
382 // Search the match registered Extract handler for the input guided section.\r
383 //\r
384 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index ++) {\r
30f001ca 385 if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, SectionDefinitionGuid)) {\r
b9d5a7f1
LG
386 //\r
387 // Call the match handler to extract raw data for the input guided section.\r
388 //\r
389 return HandlerInfo->ExtractDecodeHandlerTable [Index] (\r
390 InputSection,\r
391 OutputBuffer,\r
392 ScratchBuffer,\r
393 AuthenticationStatus\r
394 );\r
395 }\r
396 }\r
397\r
398 //\r
399 // Not found, the input guided section is not supported. \r
400 //\r
401 return RETURN_UNSUPPORTED;\r
402}\r
9be899c5
ED
403\r
404/**\r
405 Retrieves handlers of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER and \r
406 EXTRACT_GUIDED_SECTION_DECODE_HANDLER for a specific GUID section type.\r
407 \r
408 Retrieves the handlers associated with SectionGuid and returns them in \r
409 GetInfoHandler and DecodeHandler.\r
410\r
411 If the GUID value specified by SectionGuid has not been registered, then \r
412 return RETURN_NOT_FOUND.\r
413 \r
414 If SectionGuid is NULL, then ASSERT().\r
415\r
416 @param[in] SectionGuid A pointer to the GUID associated with the handlersof the GUIDed \r
417 section type being retrieved.\r
418 @param[out] GetInfoHandler Pointer to a function that examines a GUIDed section and returns \r
419 the size of the decoded buffer and the size of an optional scratch \r
420 buffer required to actually decode the data in a GUIDed section. \r
421 This is an optional parameter that may be NULL. If it is NULL, then \r
422 the previously registered handler is not returned.\r
423 @param[out] DecodeHandler Pointer to a function that decodes a GUIDed section into a caller\r
424 allocated output buffer. This is an optional parameter that may be NULL.\r
425 If it is NULL, then the previously registered handler is not returned.\r
426\r
427 @retval RETURN_SUCCESS The handlers were retrieved.\r
428 @retval RETURN_NOT_FOUND No handlers have been registered with the specified GUID.\r
429\r
430**/\r
431RETURN_STATUS\r
432EFIAPI\r
433ExtractGuidedSectionGetHandlers (\r
434 IN CONST GUID *SectionGuid,\r
435 OUT EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *GetInfoHandler, OPTIONAL\r
436 OUT EXTRACT_GUIDED_SECTION_DECODE_HANDLER *DecodeHandler OPTIONAL\r
437 )\r
438{\r
439 UINT32 Index;\r
440 RETURN_STATUS Status;\r
441 EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;\r
442\r
443 //\r
444 // Check input paramter\r
445 //\r
446 ASSERT (SectionGuid != NULL);\r
447\r
448 //\r
449 // Get the registered handler information\r
450 //\r
451 Status = GetExtractGuidedSectionHandlerInfo (&HandlerInfo);\r
452 if (RETURN_ERROR (Status)) {\r
453 return Status;\r
454 }\r
455\r
456 //\r
457 // Search the match registered GetInfo handler for the input guided section.\r
458 //\r
459 ASSERT (HandlerInfo != NULL);\r
460 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index ++) {\r
461 if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, SectionGuid)) {\r
462\r
463 //\r
464 // If the guided handler has been registered before, then return the registered handlers.\r
465 //\r
466 if (GetInfoHandler != NULL) {\r
467 *GetInfoHandler = HandlerInfo->ExtractGetInfoHandlerTable[Index];\r
468 }\r
469 if (DecodeHandler != NULL) {\r
470 *DecodeHandler = HandlerInfo->ExtractDecodeHandlerTable[Index];\r
471 }\r
472 return RETURN_SUCCESS;\r
473 }\r
474 }\r
475 return RETURN_NOT_FOUND;\r
476}\r