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