]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Core/Dxe/SectionExtraction/CoreSectionExtraction.c
Make type EFI_SAL_STATUS be type INTN instead of type UINTN to match SAL Specificatio...
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / SectionExtraction / CoreSectionExtraction.c
... / ...
CommitLineData
1/** @file\r
2 Section Extraction Protocol implementation.\r
3\r
4 Stream database is implemented as a linked list of section streams,\r
5 where each stream contains a linked list of children, which may be leaves or\r
6 encapsulations.\r
7\r
8 Children that are encapsulations generate new stream entries\r
9 when they are created. Streams can also be created by calls to\r
10 SEP->OpenSectionStream().\r
11\r
12 The database is only created far enough to return the requested data from\r
13 any given stream, or to determine that the requested data is not found.\r
14\r
15 If a GUIDed encapsulation is encountered, there are three possiblilites.\r
16\r
17 1) A support protocol is found, in which the stream is simply processed with\r
18 the support protocol.\r
19\r
20 2) A support protocol is not found, but the data is available to be read\r
21 without processing. In this case, the database is built up through the\r
22 recursions to return the data, and a RPN event is set that will enable\r
23 the stream in question to be refreshed if and when the required section\r
24 extraction protocol is published.This insures the AuthenticationStatus\r
25 does not become stale in the cache.\r
26\r
27 3) A support protocol is not found, and the data is not available to be read\r
28 without it. This results in EFI_PROTOCOL_ERROR.\r
29\r
30Copyright (c) 2006 - 2008, Intel Corporation. <BR>\r
31All rights reserved. This program and the accompanying materials\r
32are licensed and made available under the terms and conditions of the BSD License\r
33which accompanies this distribution. The full text of the license may be found at\r
34http://opensource.org/licenses/bsd-license.php\r
35\r
36THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
37WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
38\r
39**/\r
40\r
41#include "DxeMain.h"\r
42\r
43//\r
44// Local defines and typedefs\r
45//\r
46#define CORE_SECTION_CHILD_SIGNATURE SIGNATURE_32('S','X','C','S')\r
47#define CHILD_SECTION_NODE_FROM_LINK(Node) \\r
48 CR (Node, CORE_SECTION_CHILD_NODE, Link, CORE_SECTION_CHILD_SIGNATURE)\r
49\r
50typedef struct {\r
51 UINT32 Signature;\r
52 LIST_ENTRY Link;\r
53 UINT32 Type;\r
54 UINT32 Size;\r
55 //\r
56 // StreamBase + OffsetInStream == pointer to section header in stream. The\r
57 // stream base is always known when walking the sections within.\r
58 //\r
59 UINT32 OffsetInStream;\r
60 //\r
61 // Then EncapsulatedStreamHandle below is always 0 if the section is NOT an\r
62 // encapsulating section. Otherwise, it contains the stream handle\r
63 // of the encapsulated stream. This handle is ALWAYS produced any time an\r
64 // encapsulating child is encountered, irrespective of whether the\r
65 // encapsulated stream is processed further.\r
66 //\r
67 UINTN EncapsulatedStreamHandle;\r
68 EFI_GUID *EncapsulationGuid;\r
69} CORE_SECTION_CHILD_NODE;\r
70\r
71#define CORE_SECTION_STREAM_SIGNATURE SIGNATURE_32('S','X','S','S')\r
72#define STREAM_NODE_FROM_LINK(Node) \\r
73 CR (Node, CORE_SECTION_STREAM_NODE, Link, CORE_SECTION_STREAM_SIGNATURE)\r
74\r
75typedef struct {\r
76 UINT32 Signature;\r
77 LIST_ENTRY Link;\r
78 UINTN StreamHandle;\r
79 UINT8 *StreamBuffer;\r
80 UINTN StreamLength;\r
81 LIST_ENTRY Children;\r
82 //\r
83 // Authentication status is from GUIDed encapsulations.\r
84 //\r
85 UINT32 AuthenticationStatus;\r
86} CORE_SECTION_STREAM_NODE;\r
87\r
88#define NULL_STREAM_HANDLE 0\r
89\r
90typedef struct {\r
91 CORE_SECTION_CHILD_NODE *ChildNode;\r
92 CORE_SECTION_STREAM_NODE *ParentStream;\r
93 VOID *Registration;\r
94 EFI_EVENT Event;\r
95} RPN_EVENT_CONTEXT;\r
96\r
97\r
98/**\r
99 The ExtractSection() function processes the input section and\r
100 allocates a buffer from the pool in which it returns the section\r
101 contents. If the section being extracted contains\r
102 authentication information (the section's\r
103 GuidedSectionHeader.Attributes field has the\r
104 EFI_GUIDED_SECTION_AUTH_STATUS_VALID bit set), the values\r
105 returned in AuthenticationStatus must reflect the results of\r
106 the authentication operation. Depending on the algorithm and\r
107 size of the encapsulated data, the time that is required to do\r
108 a full authentication may be prohibitively long for some\r
109 classes of systems. To indicate this, use\r
110 EFI_SECURITY_POLICY_PROTOCOL_GUID, which may be published by\r
111 the security policy driver (see the Platform Initialization\r
112 Driver Execution Environment Core Interface Specification for\r
113 more details and the GUID definition). If the\r
114 EFI_SECURITY_POLICY_PROTOCOL_GUID exists in the handle\r
115 database, then, if possible, full authentication should be\r
116 skipped and the section contents simply returned in the\r
117 OutputBuffer. In this case, the\r
118 EFI_AUTH_STATUS_PLATFORM_OVERRIDE bit AuthenticationStatus\r
119 must be set on return. ExtractSection() is callable only from\r
120 TPL_NOTIFY and below. Behavior of ExtractSection() at any\r
121 EFI_TPL above TPL_NOTIFY is undefined. Type EFI_TPL is\r
122 defined in RaiseTPL() in the UEFI 2.0 specification.\r
123\r
124\r
125 @param This Indicates the\r
126 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL instance.\r
127 @param InputSection Buffer containing the input GUIDed section\r
128 to be processed. OutputBuffer OutputBuffer\r
129 is allocated from boot services pool\r
130 memory and contains the new section\r
131 stream. The caller is responsible for\r
132 freeing this buffer.\r
133 @param OutputBuffer *OutputBuffer is allocated from boot services\r
134 pool memory and contains the new section stream.\r
135 The caller is responsible for freeing this buffer.\r
136 @param OutputSize A pointer to a caller-allocated UINTN in\r
137 which the size of OutputBuffer allocation\r
138 is stored. If the function returns\r
139 anything other than EFI_SUCCESS, the value\r
140 of OutputSize is undefined.\r
141\r
142 @param AuthenticationStatus A pointer to a caller-allocated\r
143 UINT32 that indicates the\r
144 authentication status of the\r
145 output buffer. If the input\r
146 section's\r
147 GuidedSectionHeader.Attributes\r
148 field has the\r
149 EFI_GUIDED_SECTION_AUTH_STATUS_VAL\r
150 bit as clear, AuthenticationStatus\r
151 must return zero. Both local bits\r
152 (19:16) and aggregate bits (3:0)\r
153 in AuthenticationStatus are\r
154 returned by ExtractSection().\r
155 These bits reflect the status of\r
156 the extraction operation. The bit\r
157 pattern in both regions must be\r
158 the same, as the local and\r
159 aggregate authentication statuses\r
160 have equivalent meaning at this\r
161 level. If the function returns\r
162 anything other than EFI_SUCCESS,\r
163 the value of AuthenticationStatus\r
164 is undefined.\r
165\r
166\r
167 @retval EFI_SUCCESS The InputSection was successfully\r
168 processed and the section contents were\r
169 returned.\r
170\r
171 @retval EFI_OUT_OF_RESOURCES The system has insufficient\r
172 resources to process the\r
173 request.\r
174\r
175 @retval EFI_INVALID_PARAMETER The GUID in InputSection does\r
176 not match this instance of the\r
177 GUIDed Section Extraction\r
178 Protocol.\r
179\r
180**/\r
181EFI_STATUS\r
182EFIAPI\r
183CustomGuidedSectionExtract (\r
184 IN CONST EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *This,\r
185 IN CONST VOID *InputSection,\r
186 OUT VOID **OutputBuffer,\r
187 OUT UINTN *OutputSize,\r
188 OUT UINT32 *AuthenticationStatus\r
189 );\r
190\r
191//\r
192// Module globals\r
193//\r
194LIST_ENTRY mStreamRoot = INITIALIZE_LIST_HEAD_VARIABLE (mStreamRoot);\r
195\r
196EFI_HANDLE mSectionExtractionHandle = NULL;\r
197\r
198EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL mCustomGuidedSectionExtractionProtocol = {\r
199 CustomGuidedSectionExtract\r
200};\r
201\r
202\r
203/**\r
204 Entry point of the section extraction code. Initializes an instance of the\r
205 section extraction interface and installs it on a new handle.\r
206\r
207 @param ImageHandle A handle for the image that is initializing this driver\r
208 @param SystemTable A pointer to the EFI system table\r
209\r
210 @retval EFI_SUCCESS Driver initialized successfully\r
211 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources\r
212\r
213**/\r
214EFI_STATUS\r
215EFIAPI\r
216InitializeSectionExtraction (\r
217 IN EFI_HANDLE ImageHandle,\r
218 IN EFI_SYSTEM_TABLE *SystemTable\r
219 )\r
220{\r
221 EFI_STATUS Status;\r
222 EFI_GUID *ExtractHandlerGuidTable;\r
223 UINTN ExtractHandlerNumber;\r
224\r
225 //\r
226 // Get custom extract guided section method guid list\r
227 //\r
228 ExtractHandlerNumber = ExtractGuidedSectionGetGuidList (&ExtractHandlerGuidTable);\r
229\r
230 Status = EFI_SUCCESS;\r
231 //\r
232 // Install custom guided extraction protocol\r
233 //\r
234 while (ExtractHandlerNumber-- > 0) {\r
235 Status = CoreInstallProtocolInterface (\r
236 &mSectionExtractionHandle,\r
237 &ExtractHandlerGuidTable [ExtractHandlerNumber],\r
238 EFI_NATIVE_INTERFACE,\r
239 &mCustomGuidedSectionExtractionProtocol\r
240 );\r
241 ASSERT_EFI_ERROR (Status);\r
242 }\r
243\r
244 return Status;\r
245}\r
246\r
247\r
248/**\r
249 Check if a stream is valid.\r
250\r
251 @param SectionStream The section stream to be checked\r
252 @param SectionStreamLength The length of section stream\r
253\r
254 @return A boolean value indicating the validness of the section stream.\r
255\r
256**/\r
257BOOLEAN\r
258IsValidSectionStream (\r
259 IN VOID *SectionStream,\r
260 IN UINTN SectionStreamLength\r
261 )\r
262{\r
263 UINTN TotalLength;\r
264 UINTN SectionLength;\r
265 EFI_COMMON_SECTION_HEADER *SectionHeader;\r
266 EFI_COMMON_SECTION_HEADER *NextSectionHeader;\r
267\r
268 TotalLength = 0;\r
269 SectionHeader = (EFI_COMMON_SECTION_HEADER *)SectionStream;\r
270\r
271 while (TotalLength < SectionStreamLength) {\r
272 SectionLength = SECTION_SIZE (SectionHeader);\r
273 TotalLength += SectionLength;\r
274\r
275 if (TotalLength == SectionStreamLength) {\r
276 return TRUE;\r
277 }\r
278\r
279 //\r
280 // Move to the next byte following the section...\r
281 //\r
282 SectionHeader = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) SectionHeader + SectionLength);\r
283\r
284 //\r
285 // Figure out where the next section begins\r
286 //\r
287 NextSectionHeader = ALIGN_POINTER(SectionHeader, 4);\r
288 TotalLength += (UINTN) NextSectionHeader - (UINTN) SectionHeader;\r
289 SectionHeader = NextSectionHeader;\r
290 }\r
291\r
292 ASSERT (FALSE);\r
293 return FALSE;\r
294}\r
295\r
296\r
297/**\r
298 Worker function. Constructor for section streams.\r
299\r
300 @param SectionStreamLength Size in bytes of the section stream.\r
301 @param SectionStream Buffer containing the new section stream.\r
302 @param AllocateBuffer Indicates whether the stream buffer is to be\r
303 copied or the input buffer is to be used in\r
304 place. AuthenticationStatus- Indicates the\r
305 default authentication status for the new\r
306 stream.\r
307 @param AuthenticationStatus A pointer to a caller-allocated UINT32 that\r
308 indicates the authentication status of the\r
309 output buffer. If the input section's\r
310 GuidedSectionHeader.Attributes field\r
311 has the EFI_GUIDED_SECTION_AUTH_STATUS_VALID\r
312 bit as clear, AuthenticationStatus must return\r
313 zero. Both local bits (19:16) and aggregate\r
314 bits (3:0) in AuthenticationStatus are returned\r
315 by ExtractSection(). These bits reflect the\r
316 status of the extraction operation. The bit\r
317 pattern in both regions must be the same, as\r
318 the local and aggregate authentication statuses\r
319 have equivalent meaning at this level. If the\r
320 function returns anything other than\r
321 EFI_SUCCESS, the value of *AuthenticationStatus\r
322 is undefined.\r
323 @param SectionStreamHandle A pointer to a caller allocated section stream\r
324 handle.\r
325\r
326 @retval EFI_SUCCESS Stream was added to stream database.\r
327 @retval EFI_OUT_OF_RESOURCES memory allocation failed.\r
328\r
329**/\r
330EFI_STATUS\r
331OpenSectionStreamEx (\r
332 IN UINTN SectionStreamLength,\r
333 IN VOID *SectionStream,\r
334 IN BOOLEAN AllocateBuffer,\r
335 IN UINT32 AuthenticationStatus,\r
336 OUT UINTN *SectionStreamHandle\r
337 )\r
338{\r
339 CORE_SECTION_STREAM_NODE *NewStream;\r
340 EFI_TPL OldTpl;\r
341\r
342 //\r
343 // Allocate a new stream\r
344 //\r
345 NewStream = AllocatePool (sizeof (CORE_SECTION_STREAM_NODE));\r
346 if (NewStream == NULL) {\r
347 return EFI_OUT_OF_RESOURCES;\r
348 }\r
349\r
350 if (AllocateBuffer) {\r
351 //\r
352 // if we're here, we're double buffering, allocate the buffer and copy the\r
353 // data in\r
354 //\r
355 if (SectionStreamLength > 0) {\r
356 NewStream->StreamBuffer = AllocatePool (SectionStreamLength);\r
357 if (NewStream->StreamBuffer == NULL) {\r
358 CoreFreePool (NewStream);\r
359 return EFI_OUT_OF_RESOURCES;\r
360 }\r
361 //\r
362 // Copy in stream data\r
363 //\r
364 CopyMem (NewStream->StreamBuffer, SectionStream, SectionStreamLength);\r
365 } else {\r
366 //\r
367 // It's possible to have a zero length section stream.\r
368 //\r
369 NewStream->StreamBuffer = NULL;\r
370 }\r
371 } else {\r
372 //\r
373 // If were here, the caller has supplied the buffer (it's an internal call)\r
374 // so just assign the buffer. This happens when we open section streams\r
375 // as a result of expanding an encapsulating section.\r
376 //\r
377 NewStream->StreamBuffer = SectionStream;\r
378 }\r
379\r
380 //\r
381 // Initialize the rest of the section stream\r
382 //\r
383 NewStream->Signature = CORE_SECTION_STREAM_SIGNATURE;\r
384 NewStream->StreamHandle = (UINTN) NewStream;\r
385 NewStream->StreamLength = SectionStreamLength;\r
386 InitializeListHead (&NewStream->Children);\r
387 NewStream->AuthenticationStatus = AuthenticationStatus;\r
388\r
389 //\r
390 // Add new stream to stream list\r
391 //\r
392 OldTpl = CoreRaiseTpl (TPL_NOTIFY);\r
393 InsertTailList (&mStreamRoot, &NewStream->Link);\r
394 CoreRestoreTpl (OldTpl);\r
395\r
396 *SectionStreamHandle = NewStream->StreamHandle;\r
397\r
398 return EFI_SUCCESS;\r
399}\r
400\r
401\r
402/**\r
403 SEP member function. This function creates and returns a new section stream\r
404 handle to represent the new section stream.\r
405\r
406 @param SectionStreamLength Size in bytes of the section stream.\r
407 @param SectionStream Buffer containing the new section stream.\r
408 @param SectionStreamHandle A pointer to a caller allocated UINTN that on\r
409 output contains the new section stream handle.\r
410\r
411 @retval EFI_SUCCESS The section stream is created successfully.\r
412 @retval EFI_OUT_OF_RESOURCES memory allocation failed.\r
413 @retval EFI_INVALID_PARAMETER Section stream does not end concident with end\r
414 of last section.\r
415\r
416**/\r
417EFI_STATUS\r
418EFIAPI\r
419OpenSectionStream (\r
420 IN UINTN SectionStreamLength,\r
421 IN VOID *SectionStream,\r
422 OUT UINTN *SectionStreamHandle\r
423 )\r
424{\r
425 //\r
426 // Check to see section stream looks good...\r
427 //\r
428 if (!IsValidSectionStream (SectionStream, SectionStreamLength)) {\r
429 return EFI_INVALID_PARAMETER;\r
430 }\r
431\r
432 return OpenSectionStreamEx (\r
433 SectionStreamLength,\r
434 SectionStream,\r
435 TRUE,\r
436 0,\r
437 SectionStreamHandle\r
438 );\r
439}\r
440\r
441\r
442\r
443/**\r
444 Worker function. Determine if the input stream:child matches the input type.\r
445\r
446 @param Stream Indicates the section stream associated with the\r
447 child\r
448 @param Child Indicates the child to check\r
449 @param SearchType Indicates the type of section to check against\r
450 for\r
451 @param SectionDefinitionGuid Indicates the GUID to check against if the type\r
452 is EFI_SECTION_GUID_DEFINED\r
453\r
454 @retval TRUE The child matches\r
455 @retval FALSE The child doesn't match\r
456\r
457**/\r
458BOOLEAN\r
459ChildIsType (\r
460 IN CORE_SECTION_STREAM_NODE *Stream,\r
461 IN CORE_SECTION_CHILD_NODE *Child,\r
462 IN EFI_SECTION_TYPE SearchType,\r
463 IN EFI_GUID *SectionDefinitionGuid\r
464 )\r
465{\r
466 EFI_GUID_DEFINED_SECTION *GuidedSection;\r
467\r
468 if (SearchType == EFI_SECTION_ALL) {\r
469 return TRUE;\r
470 }\r
471 if (Child->Type != SearchType) {\r
472 return FALSE;\r
473 }\r
474 if (SearchType != EFI_SECTION_GUID_DEFINED) {\r
475 return TRUE;\r
476 }\r
477 GuidedSection = (EFI_GUID_DEFINED_SECTION * )(Stream->StreamBuffer + Child->OffsetInStream);\r
478 return CompareGuid (&GuidedSection->SectionDefinitionGuid, SectionDefinitionGuid);\r
479}\r
480\r
481\r
482/**\r
483 Worker function. Constructor for new child nodes.\r
484\r
485 @param Stream Indicates the section stream in which to add the\r
486 child.\r
487 @param ChildOffset Indicates the offset in Stream that is the\r
488 beginning of the child section.\r
489 @param ChildNode Indicates the Callee allocated and initialized\r
490 child.\r
491\r
492 @retval EFI_SUCCESS Child node was found and returned.\r
493 EFI_OUT_OF_RESOURCES- Memory allocation failed.\r
494 @retval EFI_PROTOCOL_ERROR Encapsulation sections produce new stream\r
495 handles when the child node is created. If the\r
496 section type is GUID defined, and the extraction\r
497 GUID does not exist, and producing the stream\r
498 requires the GUID, then a protocol error is\r
499 generated and no child is produced. Values\r
500 returned by OpenSectionStreamEx.\r
501\r
502**/\r
503EFI_STATUS\r
504CreateChildNode (\r
505 IN CORE_SECTION_STREAM_NODE *Stream,\r
506 IN UINT32 ChildOffset,\r
507 OUT CORE_SECTION_CHILD_NODE **ChildNode\r
508 )\r
509{\r
510 EFI_STATUS Status;\r
511 EFI_COMMON_SECTION_HEADER *SectionHeader;\r
512 EFI_COMPRESSION_SECTION *CompressionHeader;\r
513 EFI_GUID_DEFINED_SECTION *GuidedHeader;\r
514 EFI_DECOMPRESS_PROTOCOL *Decompress;\r
515 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *GuidedExtraction;\r
516 VOID *NewStreamBuffer;\r
517 VOID *ScratchBuffer;\r
518 UINT32 ScratchSize;\r
519 UINTN NewStreamBufferSize;\r
520 UINT32 AuthenticationStatus;\r
521 UINT32 SectionLength;\r
522\r
523 CORE_SECTION_CHILD_NODE *Node;\r
524\r
525 SectionHeader = (EFI_COMMON_SECTION_HEADER *) (Stream->StreamBuffer + ChildOffset);\r
526\r
527 //\r
528 // Allocate a new node\r
529 //\r
530 *ChildNode = AllocatePool (sizeof (CORE_SECTION_CHILD_NODE));\r
531 Node = *ChildNode;\r
532 if (Node == NULL) {\r
533 return EFI_OUT_OF_RESOURCES;\r
534 }\r
535\r
536 //\r
537 // Now initialize it\r
538 //\r
539 Node->Signature = CORE_SECTION_CHILD_SIGNATURE;\r
540 Node->Type = SectionHeader->Type;\r
541 Node->Size = SECTION_SIZE (SectionHeader);\r
542 Node->OffsetInStream = ChildOffset;\r
543 Node->EncapsulatedStreamHandle = NULL_STREAM_HANDLE;\r
544 Node->EncapsulationGuid = NULL;\r
545\r
546 //\r
547 // If it's an encapsulating section, then create the new section stream also\r
548 //\r
549 switch (Node->Type) {\r
550 case EFI_SECTION_COMPRESSION:\r
551 //\r
552 // Get the CompressionSectionHeader\r
553 //\r
554 ASSERT (Node->Size >= sizeof (EFI_COMPRESSION_SECTION));\r
555\r
556 CompressionHeader = (EFI_COMPRESSION_SECTION *) SectionHeader;\r
557\r
558 //\r
559 // Allocate space for the new stream\r
560 //\r
561 if (CompressionHeader->UncompressedLength > 0) {\r
562 NewStreamBufferSize = CompressionHeader->UncompressedLength;\r
563 NewStreamBuffer = AllocatePool (NewStreamBufferSize);\r
564 if (NewStreamBuffer == NULL) {\r
565 CoreFreePool (Node);\r
566 return EFI_OUT_OF_RESOURCES;\r
567 }\r
568\r
569 if (CompressionHeader->CompressionType == EFI_NOT_COMPRESSED) {\r
570 //\r
571 // stream is not actually compressed, just encapsulated. So just copy it.\r
572 //\r
573 CopyMem (NewStreamBuffer, CompressionHeader + 1, NewStreamBufferSize);\r
574 } else if (CompressionHeader->CompressionType == EFI_STANDARD_COMPRESSION) {\r
575 //\r
576 // Only support the EFI_SATNDARD_COMPRESSION algorithm.\r
577 //\r
578\r
579 //\r
580 // Decompress the stream\r
581 //\r
582 Status = CoreLocateProtocol (&gEfiDecompressProtocolGuid, NULL, (VOID **)&Decompress);\r
583\r
584 ASSERT_EFI_ERROR (Status);\r
585\r
586 Status = Decompress->GetInfo (\r
587 Decompress,\r
588 CompressionHeader + 1,\r
589 Node->Size - sizeof (EFI_COMPRESSION_SECTION),\r
590 (UINT32 *)&NewStreamBufferSize,\r
591 &ScratchSize\r
592 );\r
593 ASSERT_EFI_ERROR (Status);\r
594 ASSERT (NewStreamBufferSize == CompressionHeader->UncompressedLength);\r
595\r
596 ScratchBuffer = AllocatePool (ScratchSize);\r
597 if (ScratchBuffer == NULL) {\r
598 CoreFreePool (Node);\r
599 CoreFreePool (NewStreamBuffer);\r
600 return EFI_OUT_OF_RESOURCES;\r
601 }\r
602\r
603 Status = Decompress->Decompress (\r
604 Decompress,\r
605 CompressionHeader + 1,\r
606 Node->Size - sizeof (EFI_COMPRESSION_SECTION),\r
607 NewStreamBuffer,\r
608 (UINT32)NewStreamBufferSize,\r
609 ScratchBuffer,\r
610 ScratchSize\r
611 );\r
612 ASSERT_EFI_ERROR (Status);\r
613 CoreFreePool (ScratchBuffer);\r
614 }\r
615 } else {\r
616 NewStreamBuffer = NULL;\r
617 NewStreamBufferSize = 0;\r
618 }\r
619\r
620 Status = OpenSectionStreamEx (\r
621 NewStreamBufferSize,\r
622 NewStreamBuffer,\r
623 FALSE,\r
624 Stream->AuthenticationStatus,\r
625 &Node->EncapsulatedStreamHandle\r
626 );\r
627 if (EFI_ERROR (Status)) {\r
628 CoreFreePool (Node);\r
629 CoreFreePool (NewStreamBuffer);\r
630 return Status;\r
631 }\r
632 break;\r
633\r
634 case EFI_SECTION_GUID_DEFINED:\r
635 GuidedHeader = (EFI_GUID_DEFINED_SECTION *) SectionHeader;\r
636 Node->EncapsulationGuid = &GuidedHeader->SectionDefinitionGuid;\r
637 Status = CoreLocateProtocol (Node->EncapsulationGuid, NULL, (VOID **)&GuidedExtraction);\r
638 if (!EFI_ERROR (Status)) {\r
639 //\r
640 // NewStreamBuffer is always allocated by ExtractSection... No caller\r
641 // allocation here.\r
642 //\r
643 Status = GuidedExtraction->ExtractSection (\r
644 GuidedExtraction,\r
645 GuidedHeader,\r
646 &NewStreamBuffer,\r
647 &NewStreamBufferSize,\r
648 &AuthenticationStatus\r
649 );\r
650 if (EFI_ERROR (Status)) {\r
651 CoreFreePool (*ChildNode);\r
652 return EFI_PROTOCOL_ERROR;\r
653 }\r
654\r
655 //\r
656 // Make sure we initialize the new stream with the correct\r
657 // authentication status for both aggregate and local status fields.\r
658 //\r
659 if ((GuidedHeader->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) != 0) {\r
660 //\r
661 // OR in the parent stream's aggregate status.\r
662 //\r
663 AuthenticationStatus |= Stream->AuthenticationStatus & EFI_AUTH_STATUS_ALL;\r
664 } else {\r
665 //\r
666 // since there's no authentication data contributed by the section,\r
667 // just inherit the full value from our immediate parent.\r
668 //\r
669 AuthenticationStatus = Stream->AuthenticationStatus;\r
670 }\r
671\r
672 Status = OpenSectionStreamEx (\r
673 NewStreamBufferSize,\r
674 NewStreamBuffer,\r
675 FALSE,\r
676 AuthenticationStatus,\r
677 &Node->EncapsulatedStreamHandle\r
678 );\r
679 if (EFI_ERROR (Status)) {\r
680 CoreFreePool (*ChildNode);\r
681 CoreFreePool (NewStreamBuffer);\r
682 return Status;\r
683 }\r
684 } else {\r
685 //\r
686 // There's no GUIDed section extraction protocol available.\r
687 //\r
688 if ((GuidedHeader->Attributes & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) != 0) {\r
689 //\r
690 // If the section REQUIRES an extraction protocol, then we're toast\r
691 //\r
692 CoreFreePool (*ChildNode);\r
693 return EFI_PROTOCOL_ERROR;\r
694 }\r
695\r
696 //\r
697 // Figure out the proper authentication status\r
698 //\r
699 AuthenticationStatus = Stream->AuthenticationStatus;\r
700\r
701 SectionLength = SECTION_SIZE (GuidedHeader);\r
702 Status = OpenSectionStreamEx (\r
703 SectionLength - GuidedHeader->DataOffset,\r
704 (UINT8 *) GuidedHeader + GuidedHeader->DataOffset,\r
705 TRUE,\r
706 AuthenticationStatus,\r
707 &Node->EncapsulatedStreamHandle\r
708 );\r
709 if (EFI_ERROR (Status)) {\r
710 CoreFreePool (Node);\r
711 return Status;\r
712 }\r
713 }\r
714\r
715 break;\r
716\r
717 default:\r
718\r
719 //\r
720 // Nothing to do if it's a leaf\r
721 //\r
722 break;\r
723 }\r
724\r
725 //\r
726 // Last, add the new child node to the stream\r
727 //\r
728 InsertTailList (&Stream->Children, &Node->Link);\r
729\r
730 return EFI_SUCCESS;\r
731}\r
732\r
733\r
734/**\r
735 Worker function Recursively searches / builds section stream database\r
736 looking for requested section.\r
737\r
738 @param SourceStream Indicates the section stream in which to do the\r
739 search.\r
740 @param SearchType Indicates the type of section to search for.\r
741 @param SectionInstance Indicates which instance of section to find.\r
742 This is an in/out parameter to deal with\r
743 recursions.\r
744 @param SectionDefinitionGuid Guid of section definition\r
745 @param FoundChild Output indicating the child node that is found.\r
746 @param FoundStream Output indicating which section stream the child\r
747 was found in. If this stream was generated as a\r
748 result of an encapsulation section, the\r
749 streamhandle is visible within the SEP driver\r
750 only.\r
751 @param AuthenticationStatus Indicates the authentication status of the found section.\r
752\r
753 @retval EFI_SUCCESS Child node was found and returned.\r
754 EFI_OUT_OF_RESOURCES- Memory allocation failed.\r
755 @retval EFI_NOT_FOUND Requested child node does not exist.\r
756 @retval EFI_PROTOCOL_ERROR a required GUIDED section extraction protocol\r
757 does not exist\r
758\r
759**/\r
760EFI_STATUS\r
761FindChildNode (\r
762 IN CORE_SECTION_STREAM_NODE *SourceStream,\r
763 IN EFI_SECTION_TYPE SearchType,\r
764 IN OUT UINTN *SectionInstance,\r
765 IN EFI_GUID *SectionDefinitionGuid,\r
766 OUT CORE_SECTION_CHILD_NODE **FoundChild,\r
767 OUT CORE_SECTION_STREAM_NODE **FoundStream,\r
768 OUT UINT32 *AuthenticationStatus\r
769 )\r
770{\r
771 CORE_SECTION_CHILD_NODE *CurrentChildNode;\r
772 CORE_SECTION_CHILD_NODE *RecursedChildNode;\r
773 CORE_SECTION_STREAM_NODE *RecursedFoundStream;\r
774 UINT32 NextChildOffset;\r
775 EFI_STATUS ErrorStatus;\r
776 EFI_STATUS Status;\r
777\r
778 CurrentChildNode = NULL;\r
779 ErrorStatus = EFI_NOT_FOUND;\r
780\r
781 if (SourceStream->StreamLength == 0) {\r
782 return EFI_NOT_FOUND;\r
783 }\r
784\r
785 if (IsListEmpty (&SourceStream->Children) &&\r
786 SourceStream->StreamLength >= sizeof (EFI_COMMON_SECTION_HEADER)) {\r
787 //\r
788 // This occurs when a section stream exists, but no child sections\r
789 // have been parsed out yet. Therefore, extract the first child and add it\r
790 // to the list of children so we can get started.\r
791 // Section stream may contain an array of zero or more bytes.\r
792 // So, its size should be >= the size of commen section header.\r
793 //\r
794 Status = CreateChildNode (SourceStream, 0, &CurrentChildNode);\r
795 if (EFI_ERROR (Status)) {\r
796 return Status;\r
797 }\r
798 }\r
799\r
800 //\r
801 // At least one child has been parsed out of the section stream. So, walk\r
802 // through the sections that have already been parsed out looking for the\r
803 // requested section, if necessary, continue parsing section stream and\r
804 // adding children until either the requested section is found, or we run\r
805 // out of data\r
806 //\r
807 CurrentChildNode = CHILD_SECTION_NODE_FROM_LINK (GetFirstNode(&SourceStream->Children));\r
808\r
809 for (;;) {\r
810 if (ChildIsType (SourceStream, CurrentChildNode, SearchType, SectionDefinitionGuid)) {\r
811 //\r
812 // The type matches, so check the instance count to see if it's the one we want\r
813 //\r
814 (*SectionInstance)--;\r
815 if (*SectionInstance == 0) {\r
816 //\r
817 // Got it!\r
818 //\r
819 *FoundChild = CurrentChildNode;\r
820 *FoundStream = SourceStream;\r
821 *AuthenticationStatus = SourceStream->AuthenticationStatus;\r
822 return EFI_SUCCESS;\r
823 }\r
824 }\r
825\r
826 if (CurrentChildNode->EncapsulatedStreamHandle != NULL_STREAM_HANDLE) {\r
827 //\r
828 // If the current node is an encapsulating node, recurse into it...\r
829 //\r
830 Status = FindChildNode (\r
831 (CORE_SECTION_STREAM_NODE *)CurrentChildNode->EncapsulatedStreamHandle,\r
832 SearchType,\r
833 SectionInstance,\r
834 SectionDefinitionGuid,\r
835 &RecursedChildNode,\r
836 &RecursedFoundStream,\r
837 AuthenticationStatus\r
838 );\r
839 //\r
840 // If the status is not EFI_SUCCESS, just save the error code and continue\r
841 // to find the request child node in the rest stream.\r
842 //\r
843 if (*SectionInstance == 0) {\r
844 ASSERT_EFI_ERROR (Status);\r
845 *FoundChild = RecursedChildNode;\r
846 *FoundStream = RecursedFoundStream;\r
847 return EFI_SUCCESS;\r
848 } else {\r
849 ErrorStatus = Status;\r
850 }\r
851 }\r
852\r
853 if (!IsNodeAtEnd (&SourceStream->Children, &CurrentChildNode->Link)) {\r
854 //\r
855 // We haven't found the child node we're interested in yet, but there's\r
856 // still more nodes that have already been parsed so get the next one\r
857 // and continue searching..\r
858 //\r
859 CurrentChildNode = CHILD_SECTION_NODE_FROM_LINK (GetNextNode (&SourceStream->Children, &CurrentChildNode->Link));\r
860 } else {\r
861 //\r
862 // We've exhausted children that have already been parsed, so see if\r
863 // there's any more data and continue parsing out more children if there\r
864 // is.\r
865 //\r
866 NextChildOffset = CurrentChildNode->OffsetInStream + CurrentChildNode->Size;\r
867 //\r
868 // Round up to 4 byte boundary\r
869 //\r
870 NextChildOffset += 3;\r
871 NextChildOffset &= ~(UINTN) 3;\r
872 if (NextChildOffset <= SourceStream->StreamLength - sizeof (EFI_COMMON_SECTION_HEADER)) {\r
873 //\r
874 // There's an unparsed child remaining in the stream, so create a new child node\r
875 //\r
876 Status = CreateChildNode (SourceStream, NextChildOffset, &CurrentChildNode);\r
877 if (EFI_ERROR (Status)) {\r
878 return Status;\r
879 }\r
880 } else {\r
881 ASSERT (EFI_ERROR (ErrorStatus));\r
882 return ErrorStatus;\r
883 }\r
884 }\r
885 }\r
886}\r
887\r
888\r
889/**\r
890 Worker function. Search stream database for requested stream handle.\r
891\r
892 @param SearchHandle Indicates which stream to look for.\r
893 @param FoundStream Output pointer to the found stream.\r
894\r
895 @retval EFI_SUCCESS StreamHandle was found and *FoundStream contains\r
896 the stream node.\r
897 @retval EFI_NOT_FOUND SearchHandle was not found in the stream\r
898 database.\r
899\r
900**/\r
901EFI_STATUS\r
902FindStreamNode (\r
903 IN UINTN SearchHandle,\r
904 OUT CORE_SECTION_STREAM_NODE **FoundStream\r
905 )\r
906{\r
907 CORE_SECTION_STREAM_NODE *StreamNode;\r
908\r
909 if (!IsListEmpty (&mStreamRoot)) {\r
910 StreamNode = STREAM_NODE_FROM_LINK (GetFirstNode (&mStreamRoot));\r
911 for (;;) {\r
912 if (StreamNode->StreamHandle == SearchHandle) {\r
913 *FoundStream = StreamNode;\r
914 return EFI_SUCCESS;\r
915 } else if (IsNodeAtEnd (&mStreamRoot, &StreamNode->Link)) {\r
916 break;\r
917 } else {\r
918 StreamNode = STREAM_NODE_FROM_LINK (GetNextNode (&mStreamRoot, &StreamNode->Link));\r
919 }\r
920 }\r
921 }\r
922\r
923 return EFI_NOT_FOUND;\r
924}\r
925\r
926\r
927/**\r
928 SEP member function. Retrieves requested section from section stream.\r
929\r
930 @param SectionStreamHandle The section stream from which to extract the\r
931 requested section.\r
932 @param SectionType A pointer to the type of section to search for.\r
933 @param SectionDefinitionGuid If the section type is EFI_SECTION_GUID_DEFINED,\r
934 then SectionDefinitionGuid indicates which of\r
935 these types of sections to search for.\r
936 @param SectionInstance Indicates which instance of the requested\r
937 section to return.\r
938 @param Buffer Double indirection to buffer. If *Buffer is\r
939 non-null on input, then the buffer is caller\r
940 allocated. If Buffer is NULL, then the buffer\r
941 is callee allocated. In either case, the\r
942 requried buffer size is returned in *BufferSize.\r
943 @param BufferSize On input, indicates the size of *Buffer if\r
944 *Buffer is non-null on input. On output,\r
945 indicates the required size (allocated size if\r
946 callee allocated) of *Buffer.\r
947 @param AuthenticationStatus A pointer to a caller-allocated UINT32 that\r
948 indicates the authentication status of the\r
949 output buffer. If the input section's\r
950 GuidedSectionHeader.Attributes field\r
951 has the EFI_GUIDED_SECTION_AUTH_STATUS_VALID\r
952 bit as clear, AuthenticationStatus must return\r
953 zero. Both local bits (19:16) and aggregate\r
954 bits (3:0) in AuthenticationStatus are returned\r
955 by ExtractSection(). These bits reflect the\r
956 status of the extraction operation. The bit\r
957 pattern in both regions must be the same, as\r
958 the local and aggregate authentication statuses\r
959 have equivalent meaning at this level. If the\r
960 function returns anything other than\r
961 EFI_SUCCESS, the value of *AuthenticationStatus\r
962 is undefined.\r
963\r
964 @retval EFI_SUCCESS Section was retrieved successfully\r
965 @retval EFI_PROTOCOL_ERROR A GUID defined section was encountered in the\r
966 section stream with its\r
967 EFI_GUIDED_SECTION_PROCESSING_REQUIRED bit set,\r
968 but there was no corresponding GUIDed Section\r
969 Extraction Protocol in the handle database.\r
970 *Buffer is unmodified.\r
971 @retval EFI_NOT_FOUND An error was encountered when parsing the\r
972 SectionStream. This indicates the SectionStream\r
973 is not correctly formatted.\r
974 @retval EFI_NOT_FOUND The requested section does not exist.\r
975 @retval EFI_OUT_OF_RESOURCES The system has insufficient resources to process\r
976 the request.\r
977 @retval EFI_INVALID_PARAMETER The SectionStreamHandle does not exist.\r
978 @retval EFI_WARN_TOO_SMALL The size of the caller allocated input buffer is\r
979 insufficient to contain the requested section.\r
980 The input buffer is filled and section contents\r
981 are truncated.\r
982\r
983**/\r
984EFI_STATUS\r
985EFIAPI\r
986GetSection (\r
987 IN UINTN SectionStreamHandle,\r
988 IN EFI_SECTION_TYPE *SectionType,\r
989 IN EFI_GUID *SectionDefinitionGuid,\r
990 IN UINTN SectionInstance,\r
991 IN VOID **Buffer,\r
992 IN OUT UINTN *BufferSize,\r
993 OUT UINT32 *AuthenticationStatus\r
994 )\r
995{\r
996 CORE_SECTION_STREAM_NODE *StreamNode;\r
997 EFI_TPL OldTpl;\r
998 EFI_STATUS Status;\r
999 CORE_SECTION_CHILD_NODE *ChildNode;\r
1000 CORE_SECTION_STREAM_NODE *ChildStreamNode;\r
1001 UINTN CopySize;\r
1002 UINT32 ExtractedAuthenticationStatus;\r
1003 UINTN Instance;\r
1004 UINT8 *CopyBuffer;\r
1005 UINTN SectionSize;\r
1006\r
1007\r
1008 OldTpl = CoreRaiseTpl (TPL_NOTIFY);\r
1009 Instance = SectionInstance + 1;\r
1010\r
1011 //\r
1012 // Locate target stream\r
1013 //\r
1014 Status = FindStreamNode (SectionStreamHandle, &StreamNode);\r
1015 if (EFI_ERROR (Status)) {\r
1016 Status = EFI_INVALID_PARAMETER;\r
1017 goto GetSection_Done;\r
1018 }\r
1019\r
1020 //\r
1021 // Found the stream, now locate and return the appropriate section\r
1022 //\r
1023 if (SectionType == NULL) {\r
1024 //\r
1025 // SectionType == NULL means return the WHOLE section stream...\r
1026 //\r
1027 CopySize = StreamNode->StreamLength;\r
1028 CopyBuffer = StreamNode->StreamBuffer;\r
1029 *AuthenticationStatus = StreamNode->AuthenticationStatus;\r
1030 } else {\r
1031 //\r
1032 // There's a requested section type, so go find it and return it...\r
1033 //\r
1034 Status = FindChildNode (\r
1035 StreamNode,\r
1036 *SectionType,\r
1037 &Instance,\r
1038 SectionDefinitionGuid,\r
1039 &ChildNode,\r
1040 &ChildStreamNode,\r
1041 &ExtractedAuthenticationStatus\r
1042 );\r
1043 if (EFI_ERROR (Status)) {\r
1044 goto GetSection_Done;\r
1045 }\r
1046 CopySize = ChildNode->Size - sizeof (EFI_COMMON_SECTION_HEADER);\r
1047 CopyBuffer = ChildStreamNode->StreamBuffer + ChildNode->OffsetInStream + sizeof (EFI_COMMON_SECTION_HEADER);\r
1048 *AuthenticationStatus = ExtractedAuthenticationStatus;\r
1049 }\r
1050\r
1051 SectionSize = CopySize;\r
1052 if (*Buffer != NULL) {\r
1053 //\r
1054 // Caller allocated buffer. Fill to size and return required size...\r
1055 //\r
1056 if (*BufferSize < CopySize) {\r
1057 Status = EFI_WARN_BUFFER_TOO_SMALL;\r
1058 CopySize = *BufferSize;\r
1059 }\r
1060 } else {\r
1061 //\r
1062 // Callee allocated buffer. Allocate buffer and return size.\r
1063 //\r
1064 *Buffer = AllocatePool (CopySize);\r
1065 if (*Buffer == NULL) {\r
1066 Status = EFI_OUT_OF_RESOURCES;\r
1067 goto GetSection_Done;\r
1068 }\r
1069 }\r
1070 CopyMem (*Buffer, CopyBuffer, CopySize);\r
1071 *BufferSize = SectionSize;\r
1072\r
1073GetSection_Done:\r
1074 CoreRestoreTpl (OldTpl);\r
1075\r
1076 return Status;\r
1077}\r
1078\r
1079\r
1080/**\r
1081 Worker function. Destructor for child nodes.\r
1082\r
1083 @param ChildNode Indicates the node to destroy\r
1084\r
1085**/\r
1086VOID\r
1087FreeChildNode (\r
1088 IN CORE_SECTION_CHILD_NODE *ChildNode\r
1089 )\r
1090{\r
1091 ASSERT (ChildNode->Signature == CORE_SECTION_CHILD_SIGNATURE);\r
1092 //\r
1093 // Remove the child from it's list\r
1094 //\r
1095 RemoveEntryList (&ChildNode->Link);\r
1096\r
1097 if (ChildNode->EncapsulatedStreamHandle != NULL_STREAM_HANDLE) {\r
1098 //\r
1099 // If it's an encapsulating section, we close the resulting section stream.\r
1100 // CloseSectionStream will free all memory associated with the stream.\r
1101 //\r
1102 CloseSectionStream (ChildNode->EncapsulatedStreamHandle);\r
1103 }\r
1104 //\r
1105 // Last, free the child node itself\r
1106 //\r
1107 CoreFreePool (ChildNode);\r
1108}\r
1109\r
1110\r
1111/**\r
1112 SEP member function. Deletes an existing section stream\r
1113\r
1114 @param StreamHandleToClose Indicates the stream to close\r
1115\r
1116 @retval EFI_SUCCESS The section stream is closed sucessfully.\r
1117 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.\r
1118 @retval EFI_INVALID_PARAMETER Section stream does not end concident with end\r
1119 of last section.\r
1120\r
1121**/\r
1122EFI_STATUS\r
1123EFIAPI\r
1124CloseSectionStream (\r
1125 IN UINTN StreamHandleToClose\r
1126 )\r
1127{\r
1128 CORE_SECTION_STREAM_NODE *StreamNode;\r
1129 EFI_TPL OldTpl;\r
1130 EFI_STATUS Status;\r
1131 LIST_ENTRY *Link;\r
1132 CORE_SECTION_CHILD_NODE *ChildNode;\r
1133\r
1134 OldTpl = CoreRaiseTpl (TPL_NOTIFY);\r
1135\r
1136 //\r
1137 // Locate target stream\r
1138 //\r
1139 Status = FindStreamNode (StreamHandleToClose, &StreamNode);\r
1140 if (!EFI_ERROR (Status)) {\r
1141 //\r
1142 // Found the stream, so close it\r
1143 //\r
1144 RemoveEntryList (&StreamNode->Link);\r
1145 while (!IsListEmpty (&StreamNode->Children)) {\r
1146 Link = GetFirstNode (&StreamNode->Children);\r
1147 ChildNode = CHILD_SECTION_NODE_FROM_LINK (Link);\r
1148 FreeChildNode (ChildNode);\r
1149 }\r
1150 CoreFreePool (StreamNode->StreamBuffer);\r
1151 CoreFreePool (StreamNode);\r
1152 Status = EFI_SUCCESS;\r
1153 } else {\r
1154 Status = EFI_INVALID_PARAMETER;\r
1155 }\r
1156\r
1157 CoreRestoreTpl (OldTpl);\r
1158 return Status;\r
1159}\r
1160\r
1161\r
1162/**\r
1163 The ExtractSection() function processes the input section and\r
1164 allocates a buffer from the pool in which it returns the section\r
1165 contents. If the section being extracted contains\r
1166 authentication information (the section's\r
1167 GuidedSectionHeader.Attributes field has the\r
1168 EFI_GUIDED_SECTION_AUTH_STATUS_VALID bit set), the values\r
1169 returned in AuthenticationStatus must reflect the results of\r
1170 the authentication operation. Depending on the algorithm and\r
1171 size of the encapsulated data, the time that is required to do\r
1172 a full authentication may be prohibitively long for some\r
1173 classes of systems. To indicate this, use\r
1174 EFI_SECURITY_POLICY_PROTOCOL_GUID, which may be published by\r
1175 the security policy driver (see the Platform Initialization\r
1176 Driver Execution Environment Core Interface Specification for\r
1177 more details and the GUID definition). If the\r
1178 EFI_SECURITY_POLICY_PROTOCOL_GUID exists in the handle\r
1179 database, then, if possible, full authentication should be\r
1180 skipped and the section contents simply returned in the\r
1181 OutputBuffer. In this case, the\r
1182 EFI_AUTH_STATUS_PLATFORM_OVERRIDE bit AuthenticationStatus\r
1183 must be set on return. ExtractSection() is callable only from\r
1184 TPL_NOTIFY and below. Behavior of ExtractSection() at any\r
1185 EFI_TPL above TPL_NOTIFY is undefined. Type EFI_TPL is\r
1186 defined in RaiseTPL() in the UEFI 2.0 specification.\r
1187\r
1188\r
1189 @param This Indicates the\r
1190 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL instance.\r
1191 @param InputSection Buffer containing the input GUIDed section\r
1192 to be processed. OutputBuffer OutputBuffer\r
1193 is allocated from boot services pool\r
1194 memory and contains the new section\r
1195 stream. The caller is responsible for\r
1196 freeing this buffer.\r
1197 @param OutputBuffer *OutputBuffer is allocated from boot services\r
1198 pool memory and contains the new section stream.\r
1199 The caller is responsible for freeing this buffer.\r
1200 @param OutputSize A pointer to a caller-allocated UINTN in\r
1201 which the size of OutputBuffer allocation\r
1202 is stored. If the function returns\r
1203 anything other than EFI_SUCCESS, the value\r
1204 of OutputSize is undefined.\r
1205\r
1206 @param AuthenticationStatus A pointer to a caller-allocated\r
1207 UINT32 that indicates the\r
1208 authentication status of the\r
1209 output buffer. If the input\r
1210 section's\r
1211 GuidedSectionHeader.Attributes\r
1212 field has the\r
1213 EFI_GUIDED_SECTION_AUTH_STATUS_VAL\r
1214 bit as clear, AuthenticationStatus\r
1215 must return zero. Both local bits\r
1216 (19:16) and aggregate bits (3:0)\r
1217 in AuthenticationStatus are\r
1218 returned by ExtractSection().\r
1219 These bits reflect the status of\r
1220 the extraction operation. The bit\r
1221 pattern in both regions must be\r
1222 the same, as the local and\r
1223 aggregate authentication statuses\r
1224 have equivalent meaning at this\r
1225 level. If the function returns\r
1226 anything other than EFI_SUCCESS,\r
1227 the value of AuthenticationStatus\r
1228 is undefined.\r
1229\r
1230\r
1231 @retval EFI_SUCCESS The InputSection was successfully\r
1232 processed and the section contents were\r
1233 returned.\r
1234\r
1235 @retval EFI_OUT_OF_RESOURCES The system has insufficient\r
1236 resources to process the\r
1237 request.\r
1238\r
1239 @retval EFI_INVALID_PARAMETER The GUID in InputSection does\r
1240 not match this instance of the\r
1241 GUIDed Section Extraction\r
1242 Protocol.\r
1243\r
1244**/\r
1245EFI_STATUS\r
1246EFIAPI\r
1247CustomGuidedSectionExtract (\r
1248 IN CONST EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *This,\r
1249 IN CONST VOID *InputSection,\r
1250 OUT VOID **OutputBuffer,\r
1251 OUT UINTN *OutputSize,\r
1252 OUT UINT32 *AuthenticationStatus\r
1253 )\r
1254{\r
1255 EFI_STATUS Status;\r
1256 VOID *ScratchBuffer;\r
1257 VOID *AllocatedOutputBuffer;\r
1258 UINT32 OutputBufferSize;\r
1259 UINT32 ScratchBufferSize;\r
1260 UINT16 SectionAttribute;\r
1261\r
1262 //\r
1263 // Init local variable\r
1264 //\r
1265 ScratchBuffer = NULL;\r
1266 AllocatedOutputBuffer = NULL;\r
1267\r
1268 //\r
1269 // Call GetInfo to get the size and attribute of input guided section data.\r
1270 //\r
1271 Status = ExtractGuidedSectionGetInfo (\r
1272 InputSection,\r
1273 &OutputBufferSize,\r
1274 &ScratchBufferSize,\r
1275 &SectionAttribute\r
1276 );\r
1277\r
1278 if (EFI_ERROR (Status)) {\r
1279 DEBUG ((DEBUG_ERROR, "GetInfo from guided section Failed - %r\n", Status));\r
1280 return Status;\r
1281 }\r
1282\r
1283 if (ScratchBufferSize > 0) {\r
1284 //\r
1285 // Allocate scratch buffer\r
1286 //\r
1287 ScratchBuffer = AllocatePool (ScratchBufferSize);\r
1288 if (ScratchBuffer == NULL) {\r
1289 return EFI_OUT_OF_RESOURCES;\r
1290 }\r
1291 }\r
1292\r
1293 if (OutputBufferSize > 0) {\r
1294 //\r
1295 // Allocate output buffer\r
1296 //\r
1297 AllocatedOutputBuffer = AllocatePool (OutputBufferSize);\r
1298 if (AllocatedOutputBuffer == NULL) {\r
1299 FreePool (ScratchBuffer);\r
1300 return EFI_OUT_OF_RESOURCES;\r
1301 }\r
1302 *OutputBuffer = AllocatedOutputBuffer;\r
1303 }\r
1304\r
1305 //\r
1306 // Call decode function to extract raw data from the guided section.\r
1307 //\r
1308 Status = ExtractGuidedSectionDecode (\r
1309 InputSection,\r
1310 OutputBuffer,\r
1311 ScratchBuffer,\r
1312 AuthenticationStatus\r
1313 );\r
1314 if (EFI_ERROR (Status)) {\r
1315 //\r
1316 // Decode failed\r
1317 //\r
1318 if (AllocatedOutputBuffer != NULL) {\r
1319 CoreFreePool (AllocatedOutputBuffer);\r
1320 }\r
1321 if (ScratchBuffer != NULL) {\r
1322 CoreFreePool (ScratchBuffer);\r
1323 }\r
1324 DEBUG ((DEBUG_ERROR, "Extract guided section Failed - %r\n", Status));\r
1325 return Status;\r
1326 }\r
1327\r
1328 if (*OutputBuffer != AllocatedOutputBuffer) {\r
1329 //\r
1330 // OutputBuffer was returned as a different value,\r
1331 // so copy section contents to the allocated memory buffer.\r
1332 //\r
1333 CopyMem (AllocatedOutputBuffer, *OutputBuffer, OutputBufferSize);\r
1334 *OutputBuffer = AllocatedOutputBuffer;\r
1335 }\r
1336\r
1337 //\r
1338 // Set real size of output buffer.\r
1339 //\r
1340 *OutputSize = (UINTN) OutputBufferSize;\r
1341\r
1342 //\r
1343 // Free unused scratch buffer.\r
1344 //\r
1345 if (ScratchBuffer != NULL) {\r
1346 CoreFreePool (ScratchBuffer);\r
1347 }\r
1348\r
1349 return EFI_SUCCESS;\r
1350}\r