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