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