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