]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Universal/SectionExtractionDxe/SectionExtraction.c
Enhance the error handling.
[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 - 2012, 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 if (Node->Size < sizeof (EFI_COMPRESSION_SECTION)) {
727 CoreFreePool (Node);
728 return EFI_NOT_FOUND;
729 }
730
731 CompressionHeader = (EFI_COMPRESSION_SECTION *) SectionHeader;
732
733 if (IS_SECTION2 (CompressionHeader)) {
734 CompressionSource = (VOID *) ((UINT8 *) CompressionHeader + sizeof (EFI_COMPRESSION_SECTION2));
735 CompressionSourceSize = (UINT32) (SECTION2_SIZE (CompressionHeader) - sizeof (EFI_COMPRESSION_SECTION2));
736 UncompressedLength = ((EFI_COMPRESSION_SECTION2 *) CompressionHeader)->UncompressedLength;
737 CompressionType = ((EFI_COMPRESSION_SECTION2 *) CompressionHeader)->CompressionType;
738 } else {
739 CompressionSource = (VOID *) ((UINT8 *) CompressionHeader + sizeof (EFI_COMPRESSION_SECTION));
740 CompressionSourceSize = (UINT32) (SECTION_SIZE (CompressionHeader) - sizeof (EFI_COMPRESSION_SECTION));
741 UncompressedLength = CompressionHeader->UncompressedLength;
742 CompressionType = CompressionHeader->CompressionType;
743 }
744
745 //
746 // Allocate space for the new stream
747 //
748 if (UncompressedLength > 0) {
749 NewStreamBufferSize = UncompressedLength;
750 NewStreamBuffer = AllocatePool (NewStreamBufferSize);
751 if (NewStreamBuffer == NULL) {
752 FreePool (Node);
753 return EFI_OUT_OF_RESOURCES;
754 }
755
756 if (CompressionType == EFI_NOT_COMPRESSED) {
757 //
758 // stream is not actually compressed, just encapsulated. So just copy it.
759 //
760 CopyMem (NewStreamBuffer, CompressionSource, NewStreamBufferSize);
761 } else if (CompressionType == EFI_STANDARD_COMPRESSION) {
762 //
763 // Only support the EFI_SATNDARD_COMPRESSION algorithm.
764 //
765
766 //
767 // Decompress the stream
768 //
769 Status = gBS->LocateProtocol (&gEfiDecompressProtocolGuid, NULL, (VOID **)&Decompress);
770
771 ASSERT_EFI_ERROR (Status);
772
773 Status = Decompress->GetInfo (
774 Decompress,
775 CompressionSource,
776 CompressionSourceSize,
777 (UINT32 *)&NewStreamBufferSize,
778 &ScratchSize
779 );
780 if (EFI_ERROR (Status) || (NewStreamBufferSize != UncompressedLength)) {
781 CoreFreePool (Node);
782 CoreFreePool (NewStreamBuffer);
783 if (!EFI_ERROR (Status)) {
784 Status = EFI_BAD_BUFFER_SIZE;
785 }
786 return Status;
787 }
788
789 ScratchBuffer = AllocatePool (ScratchSize);
790 if (ScratchBuffer == NULL) {
791 FreePool (Node);
792 FreePool (NewStreamBuffer);
793 return EFI_OUT_OF_RESOURCES;
794 }
795
796 Status = Decompress->Decompress (
797 Decompress,
798 CompressionSource,
799 CompressionSourceSize,
800 NewStreamBuffer,
801 (UINT32)NewStreamBufferSize,
802 ScratchBuffer,
803 ScratchSize
804 );
805 FreePool (ScratchBuffer);
806 if (EFI_ERROR (Status)) {
807 CoreFreePool (Node);
808 CoreFreePool (NewStreamBuffer);
809 return Status;
810 }
811 }
812 } else {
813 NewStreamBuffer = NULL;
814 NewStreamBufferSize = 0;
815 }
816
817 Status = OpenSectionStreamEx (
818 NewStreamBufferSize,
819 NewStreamBuffer,
820 FALSE,
821 Stream->AuthenticationStatus,
822 &Node->EncapsulatedStreamHandle
823 );
824 if (EFI_ERROR (Status)) {
825 FreePool (Node);
826 FreePool (NewStreamBuffer);
827 return Status;
828 }
829 break;
830
831 case EFI_SECTION_GUID_DEFINED:
832 GuidedHeader = (EFI_GUID_DEFINED_SECTION *) SectionHeader;
833 if (IS_SECTION2 (GuidedHeader)) {
834 Node->EncapsulationGuid = &(((EFI_GUID_DEFINED_SECTION2 *) GuidedHeader)->SectionDefinitionGuid);
835 GuidedSectionAttributes = ((EFI_GUID_DEFINED_SECTION2 *) GuidedHeader)->Attributes;
836 } else {
837 Node->EncapsulationGuid = &GuidedHeader->SectionDefinitionGuid;
838 GuidedSectionAttributes = GuidedHeader->Attributes;
839 }
840 Status = gBS->LocateProtocol (Node->EncapsulationGuid, NULL, (VOID **)&GuidedExtraction);
841 if (!EFI_ERROR (Status)) {
842 //
843 // NewStreamBuffer is always allocated by ExtractSection... No caller
844 // allocation here.
845 //
846 Status = GuidedExtraction->ExtractSection (
847 GuidedExtraction,
848 GuidedHeader,
849 &NewStreamBuffer,
850 &NewStreamBufferSize,
851 &AuthenticationStatus
852 );
853 if (EFI_ERROR (Status)) {
854 FreePool (*ChildNode);
855 return EFI_PROTOCOL_ERROR;
856 }
857
858 //
859 // Make sure we initialize the new stream with the correct
860 // authentication status for both aggregate and local status fields.
861 //
862 if ((GuidedSectionAttributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) == EFI_GUIDED_SECTION_AUTH_STATUS_VALID) {
863 //
864 // OR in the parent stream's aggregate status.
865 //
866 AuthenticationStatus |= Stream->AuthenticationStatus & EFI_AGGREGATE_AUTH_STATUS_ALL;
867 } else {
868 //
869 // since there's no authentication data contributed by the section,
870 // just inherit the full value from our immediate parent.
871 //
872 AuthenticationStatus = Stream->AuthenticationStatus;
873 }
874
875 Status = OpenSectionStreamEx (
876 NewStreamBufferSize,
877 NewStreamBuffer,
878 FALSE,
879 AuthenticationStatus,
880 &Node->EncapsulatedStreamHandle
881 );
882 if (EFI_ERROR (Status)) {
883 FreePool (*ChildNode);
884 FreePool (NewStreamBuffer);
885 return Status;
886 }
887 } else {
888 //
889 // There's no GUIDed section extraction protocol available.
890 //
891 if ((GuidedSectionAttributes & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) == EFI_GUIDED_SECTION_PROCESSING_REQUIRED) {
892 //
893 // If the section REQUIRES an extraction protocol, register for RPN
894 // when the required GUIDed extraction protocol becomes available.
895 //
896 AuthenticationStatus = 0;
897 CreateGuidedExtractionRpnEvent (Stream, Node);
898 } else {
899 //
900 // Figure out the proper authentication status
901 //
902 AuthenticationStatus = Stream->AuthenticationStatus;
903 if ((GuidedSectionAttributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) == EFI_GUIDED_SECTION_AUTH_STATUS_VALID) {
904 //
905 // The local status of the new stream is contained in
906 // AuthenticaionStatus. This value needs to be ORed into the
907 // Aggregate bits also...
908 //
909
910 //
911 // Clear out and initialize the local status
912 //
913 AuthenticationStatus &= ~EFI_LOCAL_AUTH_STATUS_ALL;
914 AuthenticationStatus |= EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED | EFI_LOCAL_AUTH_STATUS_NOT_TESTED;
915 //
916 // OR local status into aggregate status
917 //
918 AuthenticationStatus |= AuthenticationStatus >> 16;
919 }
920
921 if (IS_SECTION2 (GuidedHeader)) {
922 Status = OpenSectionStreamEx (
923 SECTION2_SIZE (GuidedHeader) - ((EFI_GUID_DEFINED_SECTION2 *) GuidedHeader)->DataOffset,
924 (UINT8 *) GuidedHeader + ((EFI_GUID_DEFINED_SECTION2 *) GuidedHeader)->DataOffset,
925 TRUE,
926 AuthenticationStatus,
927 &Node->EncapsulatedStreamHandle
928 );
929 } else {
930 Status = OpenSectionStreamEx (
931 SECTION_SIZE (GuidedHeader) - ((EFI_GUID_DEFINED_SECTION *) GuidedHeader)->DataOffset,
932 (UINT8 *) GuidedHeader + ((EFI_GUID_DEFINED_SECTION *) GuidedHeader)->DataOffset,
933 TRUE,
934 AuthenticationStatus,
935 &Node->EncapsulatedStreamHandle
936 );
937 }
938 if (EFI_ERROR (Status)) {
939 FreePool (Node);
940 return Status;
941 }
942 }
943 }
944
945 if ((AuthenticationStatus & EFI_LOCAL_AUTH_STATUS_ALL) ==
946 (EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED | EFI_LOCAL_AUTH_STATUS_NOT_TESTED)) {
947 //
948 // Need to register for RPN for when the required GUIDed extraction
949 // protocol becomes available. This will enable us to refresh the
950 // AuthenticationStatus cached in the Stream if it's ever requested
951 // again.
952 //
953 CreateGuidedExtractionRpnEvent (Stream, Node);
954 }
955
956 break;
957
958 default:
959
960 //
961 // Nothing to do if it's a leaf
962 //
963 break;
964 }
965
966 //
967 // Last, add the new child node to the stream
968 //
969 InsertTailList (&Stream->Children, &Node->Link);
970
971 return EFI_SUCCESS;
972 }
973
974 /**
975 Worker function Recursively searches / builds section stream database
976 looking for requested section.
977
978
979 @param SourceStream Indicates the section stream in which to do the search.
980 @param SearchType Indicates the type of section to search for.
981 @param SectionInstance Indicates which instance of section to find. This is
982 an in/out parameter to deal with recursions.
983 @param SectionDefinitionGuid Guid of section definition
984 @param FoundChild Output indicating the child node that is found.
985 @param FoundStream Output indicating which section stream the child was
986 found in. If this stream was generated as a result of
987 an encapsulation section, the streamhandle is visible
988 within the SEP driver only.
989 @param AuthenticationStatus Indicates the authentication status of the found section.
990
991 @retval EFI_SUCCESS Child node was found and returned.
992 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
993 @retval EFI_NOT_FOUND Requested child node does not exist.
994 @retval EFI_PROTOCOL_ERROR A required GUIDED section extraction protocol does not
995 exist
996
997 **/
998 EFI_STATUS
999 FindChildNode (
1000 IN FRAMEWORK_SECTION_STREAM_NODE *SourceStream,
1001 IN EFI_SECTION_TYPE SearchType,
1002 IN OUT UINTN *SectionInstance,
1003 IN EFI_GUID *SectionDefinitionGuid,
1004 OUT FRAMEWORK_SECTION_CHILD_NODE **FoundChild,
1005 OUT FRAMEWORK_SECTION_STREAM_NODE **FoundStream,
1006 OUT UINT32 *AuthenticationStatus
1007 )
1008 {
1009 FRAMEWORK_SECTION_CHILD_NODE *CurrentChildNode;
1010 FRAMEWORK_SECTION_CHILD_NODE *RecursedChildNode;
1011 FRAMEWORK_SECTION_STREAM_NODE *RecursedFoundStream;
1012 UINT32 NextChildOffset;
1013 EFI_STATUS ErrorStatus;
1014 EFI_STATUS Status;
1015
1016 CurrentChildNode = NULL;
1017 ErrorStatus = EFI_NOT_FOUND;
1018
1019 if (SourceStream->StreamLength == 0) {
1020 return EFI_NOT_FOUND;
1021 }
1022
1023 if (IsListEmpty (&SourceStream->Children) &&
1024 SourceStream->StreamLength >= sizeof (EFI_COMMON_SECTION_HEADER)) {
1025 //
1026 // This occurs when a section stream exists, but no child sections
1027 // have been parsed out yet. Therefore, extract the first child and add it
1028 // to the list of children so we can get started.
1029 // Section stream may contain an array of zero or more bytes.
1030 // So, its size should be >= the size of commen section header.
1031 //
1032 Status = CreateChildNode (SourceStream, 0, &CurrentChildNode);
1033 if (EFI_ERROR (Status)) {
1034 return Status;
1035 }
1036 }
1037
1038 //
1039 // At least one child has been parsed out of the section stream. So, walk
1040 // through the sections that have already been parsed out looking for the
1041 // requested section, if necessary, continue parsing section stream and
1042 // adding children until either the requested section is found, or we run
1043 // out of data
1044 //
1045 CurrentChildNode = CHILD_SECTION_NODE_FROM_LINK (GetFirstNode(&SourceStream->Children));
1046
1047 for (;;) {
1048 if (ChildIsType (SourceStream, CurrentChildNode, SearchType, SectionDefinitionGuid)) {
1049 //
1050 // The type matches, so check the instance count to see if it's the one we want
1051 //
1052 (*SectionInstance)--;
1053 if (*SectionInstance == 0) {
1054 //
1055 // Got it!
1056 //
1057 *FoundChild = CurrentChildNode;
1058 *FoundStream = SourceStream;
1059 *AuthenticationStatus = SourceStream->AuthenticationStatus;
1060 return EFI_SUCCESS;
1061 }
1062 }
1063
1064 ASSERT (CurrentChildNode != NULL);
1065 if (CurrentChildNode->EncapsulatedStreamHandle != NULL_STREAM_HANDLE) {
1066 //
1067 // If the current node is an encapsulating node, recurse into it...
1068 //
1069 Status = FindChildNode (
1070 (FRAMEWORK_SECTION_STREAM_NODE *)CurrentChildNode->EncapsulatedStreamHandle,
1071 SearchType,
1072 SectionInstance,
1073 SectionDefinitionGuid,
1074 &RecursedChildNode,
1075 &RecursedFoundStream,
1076 AuthenticationStatus
1077 );
1078 //
1079 // If the status is not EFI_SUCCESS, just save the error code and continue
1080 // to find the request child node in the rest stream.
1081 //
1082 if (*SectionInstance == 0) {
1083 ASSERT_EFI_ERROR (Status);
1084 *FoundChild = RecursedChildNode;
1085 *FoundStream = RecursedFoundStream;
1086 return EFI_SUCCESS;
1087 } else {
1088 ErrorStatus = Status;
1089 }
1090 } else if ((CurrentChildNode->Type == EFI_SECTION_GUID_DEFINED) && (SearchType != EFI_SECTION_GUID_DEFINED)) {
1091 //
1092 // When Node Type is GUIDED section, but Node has no encapsulated data, Node data should not be parsed
1093 // because a required GUIDED section extraction protocol does not exist.
1094 // If SearchType is not GUIDED section, EFI_PROTOCOL_ERROR should return.
1095 //
1096 ErrorStatus = EFI_PROTOCOL_ERROR;
1097 }
1098
1099 if (!IsNodeAtEnd (&SourceStream->Children, &CurrentChildNode->Link)) {
1100 //
1101 // We haven't found the child node we're interested in yet, but there's
1102 // still more nodes that have already been parsed so get the next one
1103 // and continue searching..
1104 //
1105 CurrentChildNode = CHILD_SECTION_NODE_FROM_LINK (GetNextNode (&SourceStream->Children, &CurrentChildNode->Link));
1106 } else {
1107 //
1108 // We've exhausted children that have already been parsed, so see if
1109 // there's any more data and continue parsing out more children if there
1110 // is.
1111 //
1112 NextChildOffset = CurrentChildNode->OffsetInStream + CurrentChildNode->Size;
1113 //
1114 // Round up to 4 byte boundary
1115 //
1116 NextChildOffset += 3;
1117 NextChildOffset &= ~(UINTN)3;
1118 if (NextChildOffset <= SourceStream->StreamLength - sizeof (EFI_COMMON_SECTION_HEADER)) {
1119 //
1120 // There's an unparsed child remaining in the stream, so create a new child node
1121 //
1122 Status = CreateChildNode (SourceStream, NextChildOffset, &CurrentChildNode);
1123 if (EFI_ERROR (Status)) {
1124 return Status;
1125 }
1126 } else {
1127 ASSERT (EFI_ERROR (ErrorStatus));
1128 return ErrorStatus;
1129 }
1130 }
1131 }
1132 }
1133
1134 /**
1135 Worker function. Search stream database for requested stream handle.
1136
1137 @param SearchHandle Indicates which stream to look for.
1138 @param FoundStream Output pointer to the found stream.
1139
1140 @retval EFI_SUCCESS StreamHandle was found and *FoundStream contains
1141 the stream node.
1142 @retval EFI_NOT_FOUND SearchHandle was not found in the stream database.
1143
1144 **/
1145 EFI_STATUS
1146 FindStreamNode (
1147 IN UINTN SearchHandle,
1148 OUT FRAMEWORK_SECTION_STREAM_NODE **FoundStream
1149 )
1150 {
1151 FRAMEWORK_SECTION_STREAM_NODE *StreamNode;
1152
1153 if (!IsListEmpty (&mStreamRoot)) {
1154 StreamNode = STREAM_NODE_FROM_LINK (GetFirstNode (&mStreamRoot));
1155 for (;;) {
1156 if (StreamNode->StreamHandle == SearchHandle) {
1157 *FoundStream = StreamNode;
1158 return EFI_SUCCESS;
1159 } else if (IsNodeAtEnd (&mStreamRoot, &StreamNode->Link)) {
1160 break;
1161 } else {
1162 StreamNode = STREAM_NODE_FROM_LINK (GetNextNode (&mStreamRoot, &StreamNode->Link));
1163 }
1164 }
1165 }
1166
1167 return EFI_NOT_FOUND;
1168 }
1169
1170 /**
1171 SEP member function. Retrieves requested section from section stream.
1172
1173 @param This Pointer to SEP instance.
1174 @param SectionStreamHandle The section stream from which to extract the requested
1175 section.
1176 @param SectionType A pointer to the type of section to search for.
1177 @param SectionDefinitionGuid If the section type is EFI_SECTION_GUID_DEFINED, then
1178 SectionDefinitionGuid indicates which of these types
1179 of sections to search for.
1180 @param SectionInstance Indicates which instance of the requested section to
1181 return.
1182 @param Buffer Double indirection to buffer. If *Buffer is non-null on
1183 input, then the buffer is caller allocated. If
1184 *Buffer is NULL, then the buffer is callee allocated.
1185 In either case, the requried buffer size is returned
1186 in *BufferSize.
1187 @param BufferSize On input, indicates the size of *Buffer if *Buffer is
1188 non-null on input. On output, indicates the required
1189 size (allocated size if callee allocated) of *Buffer.
1190 @param AuthenticationStatus Indicates the authentication status of the retrieved
1191 section.
1192
1193
1194 @retval EFI_SUCCESS Section was retrieved successfully
1195 @retval EFI_PROTOCOL_ERROR A GUID defined section was encountered in the section
1196 stream with its EFI_GUIDED_SECTION_PROCESSING_REQUIRED
1197 bit set, but there was no corresponding GUIDed Section
1198 Extraction Protocol in the handle database. *Buffer is
1199 unmodified.
1200 @retval EFI_NOT_FOUND An error was encountered when parsing the SectionStream.
1201 This indicates the SectionStream is not correctly
1202 formatted.
1203 @retval EFI_NOT_FOUND The requested section does not exist.
1204 @retval EFI_OUT_OF_RESOURCES The system has insufficient resources to process the
1205 request.
1206 @retval EFI_INVALID_PARAMETER The SectionStreamHandle does not exist.
1207 @retval EFI_WARN_TOO_SMALL The size of the caller allocated input buffer is
1208 insufficient to contain the requested section. The
1209 input buffer is filled and contents are section contents
1210 are truncated.
1211
1212 **/
1213 EFI_STATUS
1214 EFIAPI
1215 GetSection (
1216 IN EFI_SECTION_EXTRACTION_PROTOCOL *This,
1217 IN UINTN SectionStreamHandle,
1218 IN EFI_SECTION_TYPE *SectionType,
1219 IN EFI_GUID *SectionDefinitionGuid,
1220 IN UINTN SectionInstance,
1221 IN VOID **Buffer,
1222 IN OUT UINTN *BufferSize,
1223 OUT UINT32 *AuthenticationStatus
1224 )
1225 {
1226 FRAMEWORK_SECTION_STREAM_NODE *StreamNode;
1227 EFI_TPL OldTpl;
1228 EFI_STATUS Status;
1229 FRAMEWORK_SECTION_CHILD_NODE *ChildNode;
1230 FRAMEWORK_SECTION_STREAM_NODE *ChildStreamNode;
1231 UINTN CopySize;
1232 UINT32 ExtractedAuthenticationStatus;
1233 UINTN Instance;
1234 UINT8 *CopyBuffer;
1235 UINTN SectionSize;
1236 EFI_COMMON_SECTION_HEADER *Section;
1237
1238
1239 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
1240 Instance = SectionInstance + 1;
1241
1242 //
1243 // Locate target stream
1244 //
1245 Status = FindStreamNode (SectionStreamHandle, &StreamNode);
1246 if (EFI_ERROR (Status)) {
1247 Status = EFI_INVALID_PARAMETER;
1248 goto GetSection_Done;
1249 }
1250
1251 //
1252 // Found the stream, now locate and return the appropriate section
1253 //
1254 if (SectionType == NULL) {
1255 //
1256 // SectionType == NULL means return the WHOLE section stream...
1257 //
1258 CopySize = StreamNode->StreamLength;
1259 CopyBuffer = StreamNode->StreamBuffer;
1260 *AuthenticationStatus = StreamNode->AuthenticationStatus;
1261 } else {
1262 //
1263 // There's a requested section type, so go find it and return it...
1264 //
1265 Status = FindChildNode (
1266 StreamNode,
1267 *SectionType,
1268 &Instance,
1269 SectionDefinitionGuid,
1270 &ChildNode,
1271 &ChildStreamNode,
1272 &ExtractedAuthenticationStatus
1273 );
1274 if (EFI_ERROR (Status)) {
1275 goto GetSection_Done;
1276 }
1277 ASSERT (ChildNode != NULL);
1278 ASSERT (ChildStreamNode != NULL);
1279 Section = (EFI_COMMON_SECTION_HEADER *) (ChildStreamNode->StreamBuffer + ChildNode->OffsetInStream);
1280
1281 if (IS_SECTION2 (Section)) {
1282 CopySize = SECTION2_SIZE (Section) - sizeof (EFI_COMMON_SECTION_HEADER2);
1283 CopyBuffer = (UINT8 *) Section + sizeof (EFI_COMMON_SECTION_HEADER2);
1284 } else {
1285 CopySize = SECTION_SIZE (Section) - sizeof (EFI_COMMON_SECTION_HEADER);
1286 CopyBuffer = (UINT8 *) Section + sizeof (EFI_COMMON_SECTION_HEADER);
1287 }
1288 *AuthenticationStatus = ExtractedAuthenticationStatus;
1289 }
1290
1291 SectionSize = CopySize;
1292 if (*Buffer != NULL) {
1293 //
1294 // Caller allocated buffer. Fill to size and return required size...
1295 //
1296 if (*BufferSize < CopySize) {
1297 Status = EFI_WARN_BUFFER_TOO_SMALL;
1298 CopySize = *BufferSize;
1299 }
1300 } else {
1301 //
1302 // Callee allocated buffer. Allocate buffer and return size.
1303 //
1304 *Buffer = AllocatePool (CopySize);
1305 if (*Buffer == NULL) {
1306 Status = EFI_OUT_OF_RESOURCES;
1307 goto GetSection_Done;
1308 }
1309 }
1310 CopyMem (*Buffer, CopyBuffer, CopySize);
1311 *BufferSize = SectionSize;
1312
1313 GetSection_Done:
1314 gBS->RestoreTPL (OldTpl);
1315 return Status;
1316 }
1317
1318 /**
1319 Worker function. Destructor for child nodes.
1320
1321 @param ChildNode Indicates the node to destroy
1322
1323 **/
1324 VOID
1325 FreeChildNode (
1326 IN FRAMEWORK_SECTION_CHILD_NODE *ChildNode
1327 )
1328 {
1329 ASSERT (ChildNode->Signature == FRAMEWORK_SECTION_CHILD_SIGNATURE);
1330 //
1331 // Remove the child from it's list
1332 //
1333 RemoveEntryList (&ChildNode->Link);
1334
1335 if (ChildNode->EncapsulatedStreamHandle != NULL_STREAM_HANDLE) {
1336 //
1337 // If it's an encapsulating section, we close the resulting section stream.
1338 // CloseSectionStream will free all memory associated with the stream.
1339 //
1340 CloseSectionStream (&mSectionExtraction, ChildNode->EncapsulatedStreamHandle);
1341 }
1342 //
1343 // Last, free the child node itself
1344 //
1345 FreePool (ChildNode);
1346 }
1347
1348 /**
1349 SEP member function. Deletes an existing section stream
1350
1351 @param This Indicates the calling context.
1352 @param StreamHandleToClose Indicates the stream to close
1353
1354 @retval EFI_SUCCESS Section stream was closed successfully.
1355 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
1356 @retval EFI_INVALID_PARAMETER Section stream does not end concident with end of
1357 last section.
1358
1359 **/
1360 EFI_STATUS
1361 EFIAPI
1362 CloseSectionStream (
1363 IN EFI_SECTION_EXTRACTION_PROTOCOL *This,
1364 IN UINTN StreamHandleToClose
1365 )
1366 {
1367 FRAMEWORK_SECTION_STREAM_NODE *StreamNode;
1368 EFI_TPL OldTpl;
1369 EFI_STATUS Status;
1370 LIST_ENTRY *Link;
1371 FRAMEWORK_SECTION_CHILD_NODE *ChildNode;
1372
1373 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
1374
1375 //
1376 // Locate target stream
1377 //
1378 Status = FindStreamNode (StreamHandleToClose, &StreamNode);
1379 if (!EFI_ERROR (Status)) {
1380 //
1381 // Found the stream, so close it
1382 //
1383 RemoveEntryList (&StreamNode->Link);
1384 while (!IsListEmpty (&StreamNode->Children)) {
1385 Link = GetFirstNode (&StreamNode->Children);
1386 ChildNode = CHILD_SECTION_NODE_FROM_LINK (Link);
1387 FreeChildNode (ChildNode);
1388 }
1389 FreePool (StreamNode->StreamBuffer);
1390 FreePool (StreamNode);
1391 Status = EFI_SUCCESS;
1392 } else {
1393 Status = EFI_INVALID_PARAMETER;
1394 }
1395
1396 gBS->RestoreTPL (OldTpl);
1397 return Status;
1398 }