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