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