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