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