]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Dxe/SectionExtraction/CoreSectionExtraction.c
1. Sync the binary with the latest source modification
[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
30Copyright (c) 2006 - 2008, Intel Corporation. <BR>\r
31All rights reserved. This program and the accompanying materials\r
32are licensed and made available under the terms and conditions of the BSD License\r
33which accompanies this distribution. The full text of the license may be found at\r
34http://opensource.org/licenses/bsd-license.php\r
35\r
36THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
37WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
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
46#define CORE_SECTION_CHILD_SIGNATURE EFI_SIGNATURE_32('S','X','C','S')\r
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
71#define CORE_SECTION_STREAM_SIGNATURE EFI_SIGNATURE_32('S','X','S','S')\r
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
474 if (SearchType != EFI_SECTION_GUID_DEFINED) {\r
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
022c6d45 583\r
28a00297 584 ASSERT_EFI_ERROR (Status);\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
638 if (!EFI_ERROR (Status)) {\r
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
659 if (GuidedHeader->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) {\r
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
688 if (GuidedHeader->Attributes & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) {\r
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
810 if (ChildIsType (SourceStream, CurrentChildNode, SearchType, SectionDefinitionGuid)) {\r
811 //\r
812 // The type matches, so check the instance count to see if it's the one we want\r
813 //\r
814 (*SectionInstance)--;\r
815 if (*SectionInstance == 0) {\r
816 //\r
817 // Got it!\r
818 //\r
819 *FoundChild = CurrentChildNode;\r
820 *FoundStream = SourceStream;\r
821 *AuthenticationStatus = SourceStream->AuthenticationStatus;\r
822 return EFI_SUCCESS;\r
823 }\r
824 }\r
162ed594 825\r
de4e6bf9 826 if (CurrentChildNode->EncapsulatedStreamHandle != NULL_STREAM_HANDLE) {\r
827 //\r
828 // If the current node is an encapsulating node, recurse into it...\r
829 //\r
830 Status = FindChildNode (\r
831 (CORE_SECTION_STREAM_NODE *)CurrentChildNode->EncapsulatedStreamHandle,\r
832 SearchType,\r
833 SectionInstance,\r
834 SectionDefinitionGuid,\r
835 &RecursedChildNode,\r
836 &RecursedFoundStream,\r
837 AuthenticationStatus\r
838 );\r
839 //\r
840 // If the status is not EFI_SUCCESS, just save the error code and continue\r
841 // to find the request child node in the rest stream.\r
842 //\r
843 if (*SectionInstance == 0) {\r
844 ASSERT_EFI_ERROR (Status);\r
845 *FoundChild = RecursedChildNode;\r
846 *FoundStream = RecursedFoundStream;\r
847 return EFI_SUCCESS;\r
848 } else {\r
849 ErrorStatus = Status;\r
28a00297 850 }\r
de4e6bf9 851 }\r
852\r
853 if (!IsNodeAtEnd (&SourceStream->Children, &CurrentChildNode->Link)) {\r
28a00297 854 //\r
de4e6bf9 855 // We haven't found the child node we're interested in yet, but there's\r
856 // still more nodes that have already been parsed so get the next one\r
857 // and continue searching..\r
28a00297 858 //\r
de4e6bf9 859 CurrentChildNode = CHILD_SECTION_NODE_FROM_LINK (GetNextNode (&SourceStream->Children, &CurrentChildNode->Link));\r
28a00297 860 } else {\r
861 //\r
de4e6bf9 862 // We've exhausted children that have already been parsed, so see if\r
863 // there's any more data and continue parsing out more children if there\r
864 // is.\r
28a00297 865 //\r
de4e6bf9 866 NextChildOffset = CurrentChildNode->OffsetInStream + CurrentChildNode->Size;\r
867 //\r
868 // Round up to 4 byte boundary\r
869 //\r
870 NextChildOffset += 3;\r
871 NextChildOffset &= ~(UINTN) 3;\r
872 if (NextChildOffset <= SourceStream->StreamLength - sizeof (EFI_COMMON_SECTION_HEADER)) {\r
873 //\r
874 // There's an unparsed child remaining in the stream, so create a new child node\r
875 //\r
876 Status = CreateChildNode (SourceStream, NextChildOffset, &CurrentChildNode);\r
877 if (EFI_ERROR (Status)) {\r
878 return Status;\r
879 }\r
880 } else {\r
881 ASSERT (EFI_ERROR (ErrorStatus));\r
882 return ErrorStatus;\r
883 }\r
28a00297 884 }\r
28a00297 885 }\r
28a00297 886}\r
887\r
888\r
162ed594 889/**\r
890 Worker function. Search stream database for requested stream handle.\r
891\r
022c6d45 892 @param SearchHandle Indicates which stream to look for.\r
893 @param FoundStream Output pointer to the found stream.\r
162ed594 894\r
022c6d45 895 @retval EFI_SUCCESS StreamHandle was found and *FoundStream contains\r
896 the stream node.\r
897 @retval EFI_NOT_FOUND SearchHandle was not found in the stream\r
162ed594 898 database.\r
899\r
900**/\r
28a00297 901EFI_STATUS\r
902FindStreamNode (\r
903 IN UINTN SearchHandle,\r
904 OUT CORE_SECTION_STREAM_NODE **FoundStream\r
905 )\r
022c6d45 906{\r
28a00297 907 CORE_SECTION_STREAM_NODE *StreamNode;\r
022c6d45 908\r
28a00297 909 if (!IsListEmpty (&mStreamRoot)) {\r
910 StreamNode = STREAM_NODE_FROM_LINK (GetFirstNode (&mStreamRoot));\r
911 for (;;) {\r
912 if (StreamNode->StreamHandle == SearchHandle) {\r
913 *FoundStream = StreamNode;\r
914 return EFI_SUCCESS;\r
915 } else if (IsNodeAtEnd (&mStreamRoot, &StreamNode->Link)) {\r
916 break;\r
917 } else {\r
918 StreamNode = STREAM_NODE_FROM_LINK (GetNextNode (&mStreamRoot, &StreamNode->Link));\r
919 }\r
920 }\r
921 }\r
022c6d45 922\r
28a00297 923 return EFI_NOT_FOUND;\r
924}\r
925\r
926\r
162ed594 927/**\r
de4e6bf9 928 SEP member function. Retrieves requested section from section stream.\r
162ed594 929\r
de4e6bf9 930 @param SectionStreamHandle The section stream from which to extract the\r
931 requested section.\r
932 @param SectionType A pointer to the type of section to search for.\r
933 @param SectionDefinitionGuid If the section type is EFI_SECTION_GUID_DEFINED,\r
934 then SectionDefinitionGuid indicates which of\r
935 these types of sections to search for.\r
936 @param SectionInstance Indicates which instance of the requested\r
937 section to return.\r
938 @param Buffer Double indirection to buffer. If *Buffer is\r
939 non-null on input, then the buffer is caller\r
940 allocated. If Buffer is NULL, then the buffer\r
941 is callee allocated. In either case, the\r
942 requried buffer size is returned in *BufferSize.\r
943 @param BufferSize On input, indicates the size of *Buffer if\r
944 *Buffer is non-null on input. On output,\r
945 indicates the required size (allocated size if\r
946 callee allocated) of *Buffer.\r
947 @param AuthenticationStatus A pointer to a caller-allocated UINT32 that\r
948 indicates the authentication status of the\r
949 output buffer. If the input section's\r
950 GuidedSectionHeader.Attributes field\r
951 has the EFI_GUIDED_SECTION_AUTH_STATUS_VALID\r
952 bit as clear, AuthenticationStatus must return\r
953 zero. Both local bits (19:16) and aggregate\r
954 bits (3:0) in AuthenticationStatus are returned\r
955 by ExtractSection(). These bits reflect the\r
956 status of the extraction operation. The bit\r
957 pattern in both regions must be the same, as\r
958 the local and aggregate authentication statuses\r
959 have equivalent meaning at this level. If the\r
960 function returns anything other than\r
961 EFI_SUCCESS, the value of *AuthenticationStatus\r
962 is undefined.\r
162ed594 963\r
de4e6bf9 964 @retval EFI_SUCCESS Section was retrieved successfully\r
965 @retval EFI_PROTOCOL_ERROR A GUID defined section was encountered in the\r
966 section stream with its\r
967 EFI_GUIDED_SECTION_PROCESSING_REQUIRED bit set,\r
968 but there was no corresponding GUIDed Section\r
969 Extraction Protocol in the handle database.\r
970 *Buffer is unmodified.\r
971 @retval EFI_NOT_FOUND An error was encountered when parsing the\r
972 SectionStream. This indicates the SectionStream\r
973 is not correctly formatted.\r
974 @retval EFI_NOT_FOUND The requested section does not exist.\r
975 @retval EFI_OUT_OF_RESOURCES The system has insufficient resources to process\r
976 the request.\r
977 @retval EFI_INVALID_PARAMETER The SectionStreamHandle does not exist.\r
978 @retval EFI_WARN_TOO_SMALL The size of the caller allocated input buffer is\r
979 insufficient to contain the requested section.\r
980 The input buffer is filled and section contents\r
981 are truncated.\r
162ed594 982\r
983**/\r
de4e6bf9 984EFI_STATUS\r
985EFIAPI\r
986GetSection (\r
987 IN UINTN SectionStreamHandle,\r
988 IN EFI_SECTION_TYPE *SectionType,\r
989 IN EFI_GUID *SectionDefinitionGuid,\r
990 IN UINTN SectionInstance,\r
991 IN VOID **Buffer,\r
992 IN OUT UINTN *BufferSize,\r
993 OUT UINT32 *AuthenticationStatus\r
28a00297 994 )\r
28a00297 995{\r
de4e6bf9 996 CORE_SECTION_STREAM_NODE *StreamNode;\r
997 EFI_TPL OldTpl;\r
998 EFI_STATUS Status;\r
999 CORE_SECTION_CHILD_NODE *ChildNode;\r
1000 CORE_SECTION_STREAM_NODE *ChildStreamNode;\r
1001 UINTN CopySize;\r
1002 UINT32 ExtractedAuthenticationStatus;\r
1003 UINTN Instance;\r
1004 UINT8 *CopyBuffer;\r
1005 UINTN SectionSize;\r
28a00297 1006\r
022c6d45 1007\r
de4e6bf9 1008 OldTpl = CoreRaiseTpl (TPL_NOTIFY);\r
1009 Instance = SectionInstance + 1;\r
28a00297 1010\r
de4e6bf9 1011 //\r
1012 // Locate target stream\r
1013 //\r
1014 Status = FindStreamNode (SectionStreamHandle, &StreamNode);\r
1015 if (EFI_ERROR (Status)) {\r
1016 Status = EFI_INVALID_PARAMETER;\r
1017 goto GetSection_Done;\r
1018 }\r
1019\r
1020 //\r
1021 // Found the stream, now locate and return the appropriate section\r
1022 //\r
1023 if (SectionType == NULL) {\r
1024 //\r
1025 // SectionType == NULL means return the WHOLE section stream...\r
1026 //\r
1027 CopySize = StreamNode->StreamLength;\r
1028 CopyBuffer = StreamNode->StreamBuffer;\r
1029 *AuthenticationStatus = StreamNode->AuthenticationStatus;\r
1030 } else {\r
1031 //\r
1032 // There's a requested section type, so go find it and return it...\r
1033 //\r
1034 Status = FindChildNode (\r
1035 StreamNode,\r
1036 *SectionType,\r
1037 &Instance,\r
1038 SectionDefinitionGuid,\r
1039 &ChildNode,\r
1040 &ChildStreamNode,\r
1041 &ExtractedAuthenticationStatus\r
1042 );\r
1043 if (EFI_ERROR (Status)) {\r
1044 goto GetSection_Done;\r
28a00297 1045 }\r
de4e6bf9 1046 CopySize = ChildNode->Size - sizeof (EFI_COMMON_SECTION_HEADER);\r
1047 CopyBuffer = ChildStreamNode->StreamBuffer + ChildNode->OffsetInStream + sizeof (EFI_COMMON_SECTION_HEADER);\r
1048 *AuthenticationStatus = ExtractedAuthenticationStatus;\r
1049 }\r
28a00297 1050\r
de4e6bf9 1051 SectionSize = CopySize;\r
1052 if (*Buffer != NULL) {\r
28a00297 1053 //\r
de4e6bf9 1054 // Caller allocated buffer. Fill to size and return required size...\r
28a00297 1055 //\r
de4e6bf9 1056 if (*BufferSize < CopySize) {\r
1057 Status = EFI_WARN_BUFFER_TOO_SMALL;\r
1058 CopySize = *BufferSize;\r
1059 }\r
1060 } else {\r
1061 //\r
1062 // Callee allocated buffer. Allocate buffer and return size.\r
1063 //\r
1064 *Buffer = AllocatePool (CopySize);\r
1065 if (*Buffer == NULL) {\r
1066 Status = EFI_OUT_OF_RESOURCES;\r
1067 goto GetSection_Done;\r
1068 }\r
1069 }\r
1070 CopyMem (*Buffer, CopyBuffer, CopySize);\r
1071 *BufferSize = SectionSize;\r
1072\r
1073GetSection_Done:\r
1074 CoreRestoreTpl (OldTpl);\r
1075\r
1076 return Status;\r
1077}\r
1078\r
1079\r
1080/**\r
1081 Worker function. Destructor for child nodes.\r
1082\r
1083 @param ChildNode Indicates the node to destroy\r
022c6d45 1084\r
de4e6bf9 1085**/\r
1086VOID\r
1087FreeChildNode (\r
1088 IN CORE_SECTION_CHILD_NODE *ChildNode\r
1089 )\r
1090{\r
1091 ASSERT (ChildNode->Signature == CORE_SECTION_CHILD_SIGNATURE);\r
1092 //\r
1093 // Remove the child from it's list\r
1094 //\r
1095 RemoveEntryList (&ChildNode->Link);\r
1096\r
1097 if (ChildNode->EncapsulatedStreamHandle != NULL_STREAM_HANDLE) {\r
28a00297 1098 //\r
de4e6bf9 1099 // If it's an encapsulating section, we close the resulting section stream.\r
1100 // CloseSectionStream will free all memory associated with the stream.\r
28a00297 1101 //\r
de4e6bf9 1102 CloseSectionStream (ChildNode->EncapsulatedStreamHandle);\r
28a00297 1103 }\r
de4e6bf9 1104 //\r
1105 // Last, free the child node itself\r
1106 //\r
1107 CoreFreePool (ChildNode);\r
1108}\r
28a00297 1109\r
de4e6bf9 1110\r
1111/**\r
1112 SEP member function. Deletes an existing section stream\r
1113\r
1114 @param StreamHandleToClose Indicates the stream to close\r
1115\r
1116 @retval EFI_SUCCESS The section stream is closed sucessfully.\r
1117 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.\r
1118 @retval EFI_INVALID_PARAMETER Section stream does not end concident with end\r
1119 of last section.\r
1120\r
1121**/\r
1122EFI_STATUS\r
1123EFIAPI\r
1124CloseSectionStream (\r
1125 IN UINTN StreamHandleToClose\r
1126 )\r
1127{\r
1128 CORE_SECTION_STREAM_NODE *StreamNode;\r
1129 EFI_TPL OldTpl;\r
1130 EFI_STATUS Status;\r
1131 LIST_ENTRY *Link;\r
1132 CORE_SECTION_CHILD_NODE *ChildNode;\r
1133\r
1134 OldTpl = CoreRaiseTpl (TPL_NOTIFY);\r
1135\r
1136 //\r
1137 // Locate target stream\r
1138 //\r
1139 Status = FindStreamNode (StreamHandleToClose, &StreamNode);\r
1140 if (!EFI_ERROR (Status)) {\r
1141 //\r
1142 // Found the stream, so close it\r
1143 //\r
1144 RemoveEntryList (&StreamNode->Link);\r
1145 while (!IsListEmpty (&StreamNode->Children)) {\r
1146 Link = GetFirstNode (&StreamNode->Children);\r
1147 ChildNode = CHILD_SECTION_NODE_FROM_LINK (Link);\r
1148 FreeChildNode (ChildNode);\r
1149 }\r
1150 CoreFreePool (StreamNode->StreamBuffer);\r
1151 CoreFreePool (StreamNode);\r
1152 Status = EFI_SUCCESS;\r
1153 } else {\r
1154 Status = EFI_INVALID_PARAMETER;\r
1155 }\r
1156\r
1157 CoreRestoreTpl (OldTpl);\r
1158 return Status;\r
28a00297 1159}\r
d8c79a81 1160\r
e94a9ff7 1161\r
d8c79a81
LG
1162/**\r
1163 The ExtractSection() function processes the input section and\r
1164 allocates a buffer from the pool in which it returns the section\r
1165 contents. If the section being extracted contains\r
1166 authentication information (the section's\r
1167 GuidedSectionHeader.Attributes field has the\r
1168 EFI_GUIDED_SECTION_AUTH_STATUS_VALID bit set), the values\r
1169 returned in AuthenticationStatus must reflect the results of\r
1170 the authentication operation. Depending on the algorithm and\r
1171 size of the encapsulated data, the time that is required to do\r
1172 a full authentication may be prohibitively long for some\r
1173 classes of systems. To indicate this, use\r
1174 EFI_SECURITY_POLICY_PROTOCOL_GUID, which may be published by\r
1175 the security policy driver (see the Platform Initialization\r
1176 Driver Execution Environment Core Interface Specification for\r
1177 more details and the GUID definition). If the\r
1178 EFI_SECURITY_POLICY_PROTOCOL_GUID exists in the handle\r
1179 database, then, if possible, full authentication should be\r
1180 skipped and the section contents simply returned in the\r
1181 OutputBuffer. In this case, the\r
1182 EFI_AUTH_STATUS_PLATFORM_OVERRIDE bit AuthenticationStatus\r
1183 must be set on return. ExtractSection() is callable only from\r
1184 TPL_NOTIFY and below. Behavior of ExtractSection() at any\r
1185 EFI_TPL above TPL_NOTIFY is undefined. Type EFI_TPL is\r
1186 defined in RaiseTPL() in the UEFI 2.0 specification.\r
1187\r
022c6d45 1188\r
162ed594 1189 @param This Indicates the\r
1190 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL instance.\r
d8c79a81
LG
1191 @param InputSection Buffer containing the input GUIDed section\r
1192 to be processed. OutputBuffer OutputBuffer\r
1193 is allocated from boot services pool\r
1194 memory and contains the new section\r
1195 stream. The caller is responsible for\r
1196 freeing this buffer.\r
162ed594 1197 @param OutputBuffer *OutputBuffer is allocated from boot services\r
1198 pool memory and contains the new section stream.\r
1199 The caller is responsible for freeing this buffer.\r
d8c79a81
LG
1200 @param OutputSize A pointer to a caller-allocated UINTN in\r
1201 which the size of OutputBuffer allocation\r
1202 is stored. If the function returns\r
1203 anything other than EFI_SUCCESS, the value\r
1204 of OutputSize is undefined.\r
1205\r
1206 @param AuthenticationStatus A pointer to a caller-allocated\r
1207 UINT32 that indicates the\r
1208 authentication status of the\r
1209 output buffer. If the input\r
1210 section's\r
1211 GuidedSectionHeader.Attributes\r
1212 field has the\r
1213 EFI_GUIDED_SECTION_AUTH_STATUS_VAL\r
1214 bit as clear, AuthenticationStatus\r
1215 must return zero. Both local bits\r
1216 (19:16) and aggregate bits (3:0)\r
1217 in AuthenticationStatus are\r
1218 returned by ExtractSection().\r
1219 These bits reflect the status of\r
1220 the extraction operation. The bit\r
1221 pattern in both regions must be\r
1222 the same, as the local and\r
1223 aggregate authentication statuses\r
1224 have equivalent meaning at this\r
1225 level. If the function returns\r
1226 anything other than EFI_SUCCESS,\r
1227 the value of AuthenticationStatus\r
1228 is undefined.\r
1229\r
1230\r
162ed594 1231 @retval EFI_SUCCESS The InputSection was successfully\r
1232 processed and the section contents were\r
1233 returned.\r
d8c79a81 1234\r
162ed594 1235 @retval EFI_OUT_OF_RESOURCES The system has insufficient\r
1236 resources to process the\r
1237 request.\r
d8c79a81
LG
1238\r
1239 @retval EFI_INVALID_PARAMETER The GUID in InputSection does\r
1240 not match this instance of the\r
1241 GUIDed Section Extraction\r
1242 Protocol.\r
1243\r
1244**/\r
1245EFI_STATUS\r
797a9d67 1246EFIAPI\r
18fd8d65 1247CustomGuidedSectionExtract (\r
d8c79a81
LG
1248 IN CONST EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *This,\r
1249 IN CONST VOID *InputSection,\r
1250 OUT VOID **OutputBuffer,\r
1251 OUT UINTN *OutputSize,\r
1252 OUT UINT32 *AuthenticationStatus\r
1253 )\r
1254{\r
1255 EFI_STATUS Status;\r
18fd8d65
LG
1256 VOID *ScratchBuffer;\r
1257 VOID *AllocatedOutputBuffer;\r
1258 UINT32 OutputBufferSize;\r
1259 UINT32 ScratchBufferSize;\r
1260 UINT16 SectionAttribute;\r
022c6d45 1261\r
d8c79a81 1262 //\r
18fd8d65 1263 // Init local variable\r
d8c79a81 1264 //\r
18fd8d65
LG
1265 ScratchBuffer = NULL;\r
1266 AllocatedOutputBuffer = NULL;\r
1267\r
d8c79a81 1268 //\r
18fd8d65 1269 // Call GetInfo to get the size and attribute of input guided section data.\r
d8c79a81 1270 //\r
18fd8d65 1271 Status = ExtractGuidedSectionGetInfo (\r
162ed594 1272 InputSection,\r
1273 &OutputBufferSize,\r
1274 &ScratchBufferSize,\r
1275 &SectionAttribute\r
1276 );\r
022c6d45 1277\r
d8c79a81 1278 if (EFI_ERROR (Status)) {\r
162ed594 1279 DEBUG ((DEBUG_ERROR, "GetInfo from guided section Failed - %r\n", Status));\r
18fd8d65
LG
1280 return Status;\r
1281 }\r
022c6d45 1282\r
de4e6bf9 1283 if (ScratchBufferSize > 0) {\r
d8c79a81 1284 //\r
18fd8d65 1285 // Allocate scratch buffer\r
d8c79a81 1286 //\r
9c4ac31c 1287 ScratchBuffer = AllocatePool (ScratchBufferSize);\r
18fd8d65
LG
1288 if (ScratchBuffer == NULL) {\r
1289 return EFI_OUT_OF_RESOURCES;\r
1290 }\r
d8c79a81
LG
1291 }\r
1292\r
022c6d45 1293 if (OutputBufferSize > 0) {\r
18fd8d65
LG
1294 //\r
1295 // Allocate output buffer\r
1296 //\r
9c4ac31c 1297 AllocatedOutputBuffer = AllocatePool (OutputBufferSize);\r
18fd8d65 1298 if (AllocatedOutputBuffer == NULL) {\r
de4e6bf9 1299 FreePool (ScratchBuffer);\r
18fd8d65
LG
1300 return EFI_OUT_OF_RESOURCES;\r
1301 }\r
1302 *OutputBuffer = AllocatedOutputBuffer;\r
d8c79a81
LG
1303 }\r
1304\r
1305 //\r
18fd8d65 1306 // Call decode function to extract raw data from the guided section.\r
d8c79a81 1307 //\r
18fd8d65 1308 Status = ExtractGuidedSectionDecode (\r
022c6d45 1309 InputSection,\r
18fd8d65
LG
1310 OutputBuffer,\r
1311 ScratchBuffer,\r
1312 AuthenticationStatus\r
e94a9ff7 1313 );\r
d8c79a81
LG
1314 if (EFI_ERROR (Status)) {\r
1315 //\r
18fd8d65 1316 // Decode failed\r
d8c79a81 1317 //\r
18fd8d65
LG
1318 if (AllocatedOutputBuffer != NULL) {\r
1319 CoreFreePool (AllocatedOutputBuffer);\r
1320 }\r
1321 if (ScratchBuffer != NULL) {\r
1322 CoreFreePool (ScratchBuffer);\r
1323 }\r
162ed594 1324 DEBUG ((DEBUG_ERROR, "Extract guided section Failed - %r\n", Status));\r
d8c79a81
LG
1325 return Status;\r
1326 }\r
18fd8d65
LG
1327\r
1328 if (*OutputBuffer != AllocatedOutputBuffer) {\r
1329 //\r
022c6d45 1330 // OutputBuffer was returned as a different value,\r
18fd8d65 1331 // so copy section contents to the allocated memory buffer.\r
022c6d45 1332 //\r
18fd8d65
LG
1333 CopyMem (AllocatedOutputBuffer, *OutputBuffer, OutputBufferSize);\r
1334 *OutputBuffer = AllocatedOutputBuffer;\r
1335 }\r
1336\r
1337 //\r
1338 // Set real size of output buffer.\r
1339 //\r
1340 *OutputSize = (UINTN) OutputBufferSize;\r
1341\r
d8c79a81
LG
1342 //\r
1343 // Free unused scratch buffer.\r
1344 //\r
18fd8d65
LG
1345 if (ScratchBuffer != NULL) {\r
1346 CoreFreePool (ScratchBuffer);\r
1347 }\r
1348\r
d8c79a81 1349 return EFI_SUCCESS;\r
702887db 1350}\r