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