]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/PiSmmCore/Dispatcher.c
Patch from open source community for CryptoPkg to allow it to build for ARM using...
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / Dispatcher.c
1 /** @file
2 SMM Driver Dispatcher.
3
4 Step #1 - When a FV protocol is added to the system every driver in the FV
5 is added to the mDiscoveredList. The Before, and After Depex are
6 pre-processed as drivers are added to the mDiscoveredList. If an Apriori
7 file exists in the FV those drivers are addeded to the
8 mScheduledQueue. The mFvHandleList is used to make sure a
9 FV is only processed once.
10
11 Step #2 - Dispatch. Remove driver from the mScheduledQueue and load and
12 start it. After mScheduledQueue is drained check the
13 mDiscoveredList to see if any item has a Depex that is ready to
14 be placed on the mScheduledQueue.
15
16 Step #3 - Adding to the mScheduledQueue requires that you process Before
17 and After dependencies. This is done recursively as the call to add
18 to the mScheduledQueue checks for Before and recursively adds
19 all Befores. It then addes the item that was passed in and then
20 processess the After dependecies by recursively calling the routine.
21
22 Dispatcher Rules:
23 The rules for the dispatcher are similar to the DXE dispatcher.
24
25 The rules for DXE dispatcher are in chapter 10 of the DXE CIS. Figure 10-3
26 is the state diagram for the DXE dispatcher
27
28 Depex - Dependency Expresion.
29
30 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
31 This program and the accompanying materials are licensed and made available
32 under the terms and conditions of the BSD License which accompanies this
33 distribution. The full text of the license may be found at
34 http://opensource.org/licenses/bsd-license.php
35
36 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
37 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
38
39 **/
40
41 #include "PiSmmCore.h"
42
43 //
44 // SMM Dispatcher Data structures
45 //
46 #define KNOWN_HANDLE_SIGNATURE SIGNATURE_32('k','n','o','w')
47 typedef struct {
48 UINTN Signature;
49 LIST_ENTRY Link; // mFvHandleList
50 EFI_HANDLE Handle;
51 } KNOWN_HANDLE;
52
53 //
54 // Function Prototypes
55 //
56
57 /**
58 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
59 must add any driver with a before dependency on InsertedDriverEntry first.
60 You do this by recursively calling this routine. After all the Befores are
61 processed you can add InsertedDriverEntry to the mScheduledQueue.
62 Then you can add any driver with an After dependency on InsertedDriverEntry
63 by recursively calling this routine.
64
65 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue
66
67 **/
68 VOID
69 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
70 IN EFI_SMM_DRIVER_ENTRY *InsertedDriverEntry
71 );
72
73 //
74 // The Driver List contains one copy of every driver that has been discovered.
75 // Items are never removed from the driver list. List of EFI_SMM_DRIVER_ENTRY
76 //
77 LIST_ENTRY mDiscoveredList = INITIALIZE_LIST_HEAD_VARIABLE (mDiscoveredList);
78
79 //
80 // Queue of drivers that are ready to dispatch. This queue is a subset of the
81 // mDiscoveredList.list of EFI_SMM_DRIVER_ENTRY.
82 //
83 LIST_ENTRY mScheduledQueue = INITIALIZE_LIST_HEAD_VARIABLE (mScheduledQueue);
84
85 //
86 // List of handles who's Fv's have been parsed and added to the mFwDriverList.
87 //
88 LIST_ENTRY mFvHandleList = INITIALIZE_LIST_HEAD_VARIABLE (mFvHandleList);
89
90 //
91 // Flag for the SMM Dispacher. TRUE if dispatcher is execuing.
92 //
93 BOOLEAN gDispatcherRunning = FALSE;
94
95 //
96 // Flag for the SMM Dispacher. TRUE if there is one or more SMM drivers ready to be dispatched
97 //
98 BOOLEAN gRequestDispatch = FALSE;
99
100 //
101 // List of file types supported by dispatcher
102 //
103 EFI_FV_FILETYPE mSmmFileTypes[] = {
104 EFI_FV_FILETYPE_SMM,
105 EFI_FV_FILETYPE_COMBINED_SMM_DXE
106 //
107 // Note: DXE core will process the FV image file, so skip it in SMM core
108 // EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
109 //
110 };
111
112 typedef struct {
113 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH File;
114 EFI_DEVICE_PATH_PROTOCOL End;
115 } FV_FILEPATH_DEVICE_PATH;
116
117 FV_FILEPATH_DEVICE_PATH mFvDevicePath;
118
119 //
120 // DXE Architecture Protocols
121 //
122 EFI_SECURITY_ARCH_PROTOCOL *mSecurity = NULL;
123
124 //
125 // The global variable is defined for Loading modules at fixed address feature to track the SMM code
126 // memory range usage. It is a bit mapped array in which every bit indicates the correspoding
127 // memory page available or not.
128 //
129 GLOBAL_REMOVE_IF_UNREFERENCED UINT64 *mSmmCodeMemoryRangeUsageBitMap=NULL;
130
131 /**
132 To check memory usage bit map array to figure out if the memory range in which the image will be loaded is available or not. If
133 memory range is avaliable, the function will mark the correponding bits to 1 which indicates the memory range is used.
134 The function is only invoked when load modules at fixed address feature is enabled.
135
136 @param ImageBase The base addres the image will be loaded at.
137 @param ImageSize The size of the image
138
139 @retval EFI_SUCCESS The memory range the image will be loaded in is available
140 @retval EFI_NOT_FOUND The memory range the image will be loaded in is not available
141 **/
142 EFI_STATUS
143 CheckAndMarkFixLoadingMemoryUsageBitMap (
144 IN EFI_PHYSICAL_ADDRESS ImageBase,
145 IN UINTN ImageSize
146 )
147 {
148 UINT32 SmmCodePageNumber;
149 UINT64 SmmCodeSize;
150 EFI_PHYSICAL_ADDRESS SmmCodeBase;
151 UINTN BaseOffsetPageNumber;
152 UINTN TopOffsetPageNumber;
153 UINTN Index;
154 //
155 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
156 //
157 SmmCodePageNumber = PcdGet32(PcdLoadFixAddressSmmCodePageNumber);
158 SmmCodeSize = EFI_PAGES_TO_SIZE (SmmCodePageNumber);
159 SmmCodeBase = gLoadModuleAtFixAddressSmramBase;
160
161 //
162 // If the memory usage bit map is not initialized, do it. Every bit in the array
163 // indicate the status of the corresponding memory page, available or not
164 //
165 if (mSmmCodeMemoryRangeUsageBitMap == NULL) {
166 mSmmCodeMemoryRangeUsageBitMap = AllocateZeroPool(((SmmCodePageNumber / 64) + 1)*sizeof(UINT64));
167 }
168 //
169 // If the Dxe code memory range is not allocated or the bit map array allocation failed, return EFI_NOT_FOUND
170 //
171 if (mSmmCodeMemoryRangeUsageBitMap == NULL) {
172 return EFI_NOT_FOUND;
173 }
174 //
175 // see if the memory range for loading the image is in the SMM code range.
176 //
177 if (SmmCodeBase + SmmCodeSize < ImageBase + ImageSize || SmmCodeBase > ImageBase) {
178 return EFI_NOT_FOUND;
179 }
180 //
181 // Test if the memory is avalaible or not.
182 //
183 BaseOffsetPageNumber = (UINTN)EFI_SIZE_TO_PAGES((UINT32)(ImageBase - SmmCodeBase));
184 TopOffsetPageNumber = (UINTN)EFI_SIZE_TO_PAGES((UINT32)(ImageBase + ImageSize - SmmCodeBase));
185 for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {
186 if ((mSmmCodeMemoryRangeUsageBitMap[Index / 64] & LShiftU64(1, (Index % 64))) != 0) {
187 //
188 // This page is already used.
189 //
190 return EFI_NOT_FOUND;
191 }
192 }
193
194 //
195 // Being here means the memory range is available. So mark the bits for the memory range
196 //
197 for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {
198 mSmmCodeMemoryRangeUsageBitMap[Index / 64] |= LShiftU64(1, (Index % 64));
199 }
200 return EFI_SUCCESS;
201 }
202 /**
203 Get the fixed loadding address from image header assigned by build tool. This function only be called
204 when Loading module at Fixed address feature enabled.
205
206 @param ImageContext Pointer to the image context structure that describes the PE/COFF
207 image that needs to be examined by this function.
208 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
209 @retval EFI_NOT_FOUND The image has no assigned fixed loadding address.
210
211 **/
212 EFI_STATUS
213 GetPeCoffImageFixLoadingAssignedAddress(
214 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
215 )
216 {
217 UINTN SectionHeaderOffset;
218 EFI_STATUS Status;
219 EFI_IMAGE_SECTION_HEADER SectionHeader;
220 EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;
221 EFI_PHYSICAL_ADDRESS FixLoaddingAddress;
222 UINT16 Index;
223 UINTN Size;
224 UINT16 NumberOfSections;
225 UINT64 ValueInSectionHeader;
226
227 FixLoaddingAddress = 0;
228 Status = EFI_NOT_FOUND;
229
230 //
231 // Get PeHeader pointer
232 //
233 ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);
234 SectionHeaderOffset = (UINTN)(
235 ImageContext->PeCoffHeaderOffset +
236 sizeof (UINT32) +
237 sizeof (EFI_IMAGE_FILE_HEADER) +
238 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader
239 );
240 NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;
241
242 //
243 // Get base address from the first section header that doesn't point to code section.
244 //
245 for (Index = 0; Index < NumberOfSections; Index++) {
246 //
247 // Read section header from file
248 //
249 Size = sizeof (EFI_IMAGE_SECTION_HEADER);
250 Status = ImageContext->ImageRead (
251 ImageContext->Handle,
252 SectionHeaderOffset,
253 &Size,
254 &SectionHeader
255 );
256 if (EFI_ERROR (Status)) {
257 return Status;
258 }
259
260 Status = EFI_NOT_FOUND;
261
262 if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {
263 //
264 // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header
265 // that doesn't point to code section in image header.So there is an assumption that when the feature is enabled,
266 // if a module with a loading address assigned by tools, the PointerToRelocations & PointerToLineNumbers fields
267 // should not be Zero, or else, these 2 fileds should be set to Zero
268 //
269 ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);
270 if (ValueInSectionHeader != 0) {
271 //
272 // Found first section header that doesn't point to code section in which uild tool saves the
273 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
274 //
275 FixLoaddingAddress = (EFI_PHYSICAL_ADDRESS)(gLoadModuleAtFixAddressSmramBase + (INT64)ValueInSectionHeader);
276 //
277 // Check if the memory range is avaliable.
278 //
279 Status = CheckAndMarkFixLoadingMemoryUsageBitMap (FixLoaddingAddress, (UINTN)(ImageContext->ImageSize + ImageContext->SectionAlignment));
280 if (!EFI_ERROR(Status)) {
281 //
282 // The assigned address is valid. Return the specified loadding address
283 //
284 ImageContext->ImageAddress = FixLoaddingAddress;
285 }
286 }
287 break;
288 }
289 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
290 }
291 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r\n", FixLoaddingAddress, Status));
292 return Status;
293 }
294 /**
295 Loads an EFI image into SMRAM.
296
297 @param DriverEntry EFI_SMM_DRIVER_ENTRY instance
298
299 @return EFI_STATUS
300
301 **/
302 EFI_STATUS
303 EFIAPI
304 SmmLoadImage (
305 IN OUT EFI_SMM_DRIVER_ENTRY *DriverEntry
306 )
307 {
308 UINT32 AuthenticationStatus;
309 UINTN FilePathSize;
310 VOID *Buffer;
311 UINTN Size;
312 UINTN PageCount;
313 EFI_GUID *NameGuid;
314 EFI_STATUS Status;
315 EFI_STATUS SecurityStatus;
316 EFI_HANDLE DeviceHandle;
317 EFI_PHYSICAL_ADDRESS DstBuffer;
318 EFI_DEVICE_PATH_PROTOCOL *FilePath;
319 EFI_DEVICE_PATH_PROTOCOL *OriginalFilePath;
320 EFI_DEVICE_PATH_PROTOCOL *HandleFilePath;
321 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
322 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
323
324 Buffer = NULL;
325 Size = 0;
326 Fv = DriverEntry->Fv;
327 NameGuid = &DriverEntry->FileName;
328 FilePath = DriverEntry->FvFileDevicePath;
329
330 OriginalFilePath = FilePath;
331 HandleFilePath = FilePath;
332 DeviceHandle = NULL;
333 SecurityStatus = EFI_SUCCESS;
334 Status = EFI_SUCCESS;
335 AuthenticationStatus = 0;
336
337 //
338 // Try to get the image device handle by checking the match protocol.
339 //
340 Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &HandleFilePath, &DeviceHandle);
341 if (EFI_ERROR(Status)) {
342 return Status;
343 }
344
345 //
346 // If the Security Architectural Protocol has not been located yet, then attempt to locate it
347 //
348 if (mSecurity == NULL) {
349 gBS->LocateProtocol (&gEfiSecurityArchProtocolGuid, NULL, (VOID**)&mSecurity);
350 }
351
352 //
353 // Verify the Authentication Status through the Security Architectural Protocol
354 //
355 if ((mSecurity != NULL) && (OriginalFilePath != NULL)) {
356 SecurityStatus = mSecurity->FileAuthenticationState (
357 mSecurity,
358 AuthenticationStatus,
359 OriginalFilePath
360 );
361 if (EFI_ERROR (SecurityStatus) && SecurityStatus != EFI_SECURITY_VIOLATION) {
362 Status = SecurityStatus;
363 return Status;
364 }
365 }
366
367 //
368 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath
369 //
370 FilePath = OriginalFilePath;
371 Status = gBS->HandleProtocol (DeviceHandle, &gEfiDevicePathProtocolGuid, (VOID **)&HandleFilePath);
372 if (!EFI_ERROR (Status)) {
373 FilePathSize = GetDevicePathSize (HandleFilePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL);
374 FilePath = (EFI_DEVICE_PATH_PROTOCOL *) (((UINT8 *)FilePath) + FilePathSize );
375 }
376
377 //
378 // Try reading PE32 section firstly
379 //
380 Status = Fv->ReadSection (
381 Fv,
382 NameGuid,
383 EFI_SECTION_PE32,
384 0,
385 &Buffer,
386 &Size,
387 &AuthenticationStatus
388 );
389
390 if (EFI_ERROR (Status)) {
391 //
392 // Try reading TE section secondly
393 //
394 Buffer = NULL;
395 Size = 0;
396 Status = Fv->ReadSection (
397 Fv,
398 NameGuid,
399 EFI_SECTION_TE,
400 0,
401 &Buffer,
402 &Size,
403 &AuthenticationStatus
404 );
405 }
406
407 if (EFI_ERROR (Status)) {
408 if (Buffer != NULL) {
409 Status = gBS->FreePool (Buffer);
410 }
411 return Status;
412 }
413
414 //
415 // Initialize ImageContext
416 //
417 ImageContext.Handle = Buffer;
418 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;
419
420 //
421 // Get information about the image being loaded
422 //
423 Status = PeCoffLoaderGetImageInfo (&ImageContext);
424 if (EFI_ERROR (Status)) {
425 if (Buffer != NULL) {
426 Status = gBS->FreePool (Buffer);
427 }
428 return Status;
429 }
430 //
431 // if Loading module at Fixed Address feature is enabled, then cut out a memory range started from TESG BASE
432 // to hold the Smm driver code
433 //
434 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {
435 //
436 // Get the fixed loading address assigned by Build tool
437 //
438 Status = GetPeCoffImageFixLoadingAssignedAddress (&ImageContext);
439 if (!EFI_ERROR (Status)) {
440 //
441 // Since the memory range to load Smm core alreay been cut out, so no need to allocate and free this range
442 // following statements is to bypass SmmFreePages
443 //
444 PageCount = 0;
445 DstBuffer = (UINTN)gLoadModuleAtFixAddressSmramBase;
446 } else {
447 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED ERROR: Failed to load module at fixed address. \n"));
448 //
449 // allocate the memory to load the SMM driver
450 //
451 PageCount = (UINTN)EFI_SIZE_TO_PAGES(ImageContext.ImageSize + ImageContext.SectionAlignment);
452 DstBuffer = (UINTN)(-1);
453
454 Status = SmmAllocatePages (
455 AllocateMaxAddress,
456 EfiRuntimeServicesCode,
457 PageCount,
458 &DstBuffer
459 );
460 if (EFI_ERROR (Status)) {
461 if (Buffer != NULL) {
462 Status = gBS->FreePool (Buffer);
463 }
464 return Status;
465 }
466 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)DstBuffer;
467 }
468 } else {
469 PageCount = (UINTN)EFI_SIZE_TO_PAGES(ImageContext.ImageSize + ImageContext.SectionAlignment);
470 DstBuffer = (UINTN)(-1);
471
472 Status = SmmAllocatePages (
473 AllocateMaxAddress,
474 EfiRuntimeServicesCode,
475 PageCount,
476 &DstBuffer
477 );
478 if (EFI_ERROR (Status)) {
479 if (Buffer != NULL) {
480 Status = gBS->FreePool (Buffer);
481 }
482 return Status;
483 }
484
485 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)DstBuffer;
486 }
487 //
488 // Align buffer on section boundry
489 //
490 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;
491 ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);
492
493 //
494 // Load the image to our new buffer
495 //
496 Status = PeCoffLoaderLoadImage (&ImageContext);
497 if (EFI_ERROR (Status)) {
498 if (Buffer != NULL) {
499 Status = gBS->FreePool (Buffer);
500 }
501 SmmFreePages (DstBuffer, PageCount);
502 return Status;
503 }
504
505 //
506 // Relocate the image in our new buffer
507 //
508 Status = PeCoffLoaderRelocateImage (&ImageContext);
509 if (EFI_ERROR (Status)) {
510 if (Buffer != NULL) {
511 Status = gBS->FreePool (Buffer);
512 }
513 SmmFreePages (DstBuffer, PageCount);
514 return Status;
515 }
516
517 //
518 // Flush the instruction cache so the image data are written before we execute it
519 //
520 InvalidateInstructionCacheRange ((VOID *)(UINTN) ImageContext.ImageAddress, (UINTN) ImageContext.ImageSize);
521
522 //
523 // Save Image EntryPoint in DriverEntry
524 //
525 DriverEntry->ImageEntryPoint = ImageContext.EntryPoint;
526 DriverEntry->ImageBuffer = DstBuffer;
527 DriverEntry->NumberOfPage = PageCount;
528
529 //
530 // Allocate a Loaded Image Protocol in EfiBootServicesData
531 //
532 Status = gBS->AllocatePool (EfiBootServicesData, sizeof (EFI_LOADED_IMAGE_PROTOCOL), (VOID **)&DriverEntry->LoadedImage);
533 if (EFI_ERROR (Status)) {
534 if (Buffer != NULL) {
535 Status = gBS->FreePool (Buffer);
536 }
537 SmmFreePages (DstBuffer, PageCount);
538 return Status;
539 }
540
541 //
542 // Fill in the remaining fields of the Loaded Image Protocol instance.
543 // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.
544 //
545 DriverEntry->LoadedImage->Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
546 DriverEntry->LoadedImage->ParentHandle = gSmmCorePrivate->SmmIplImageHandle;
547 DriverEntry->LoadedImage->SystemTable = gST;
548 DriverEntry->LoadedImage->DeviceHandle = DeviceHandle;
549
550 //
551 // Make an EfiBootServicesData buffer copy of FilePath
552 //
553 Status = gBS->AllocatePool (EfiBootServicesData, GetDevicePathSize (FilePath), (VOID **)&DriverEntry->LoadedImage->FilePath);
554 if (EFI_ERROR (Status)) {
555 if (Buffer != NULL) {
556 Status = gBS->FreePool (Buffer);
557 }
558 SmmFreePages (DstBuffer, PageCount);
559 return Status;
560 }
561 CopyMem (DriverEntry->LoadedImage->FilePath, FilePath, GetDevicePathSize (FilePath));
562
563 DriverEntry->LoadedImage->ImageBase = (VOID *)(UINTN)DriverEntry->ImageBuffer;
564 DriverEntry->LoadedImage->ImageSize = ImageContext.ImageSize;
565 DriverEntry->LoadedImage->ImageCodeType = EfiRuntimeServicesCode;
566 DriverEntry->LoadedImage->ImageDataType = EfiRuntimeServicesData;
567
568 //
569 // Create a new image handle in the UEFI handle database for the SMM Driver
570 //
571 DriverEntry->ImageHandle = NULL;
572 Status = gBS->InstallMultipleProtocolInterfaces (
573 &DriverEntry->ImageHandle,
574 &gEfiLoadedImageProtocolGuid, DriverEntry->LoadedImage,
575 NULL
576 );
577
578 //
579 // Print the load address and the PDB file name if it is available
580 //
581
582 DEBUG_CODE_BEGIN ();
583
584 UINTN Index;
585 UINTN StartIndex;
586 CHAR8 EfiFileName[256];
587
588
589 DEBUG ((DEBUG_INFO | DEBUG_LOAD,
590 "Loading SMM driver at 0x%11p EntryPoint=0x%11p ",
591 (VOID *)(UINTN) ImageContext.ImageAddress,
592 FUNCTION_ENTRY_POINT (ImageContext.EntryPoint)));
593
594
595 //
596 // Print Module Name by Pdb file path.
597 // Windows and Unix style file path are all trimmed correctly.
598 //
599 if (ImageContext.PdbPointer != NULL) {
600 StartIndex = 0;
601 for (Index = 0; ImageContext.PdbPointer[Index] != 0; Index++) {
602 if ((ImageContext.PdbPointer[Index] == '\\') || (ImageContext.PdbPointer[Index] == '/')) {
603 StartIndex = Index + 1;
604 }
605 }
606 //
607 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
608 // The PDB file name is limited in the range of 0~255.
609 // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.
610 //
611 for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {
612 EfiFileName[Index] = ImageContext.PdbPointer[Index + StartIndex];
613 if (EfiFileName[Index] == 0) {
614 EfiFileName[Index] = '.';
615 }
616 if (EfiFileName[Index] == '.') {
617 EfiFileName[Index + 1] = 'e';
618 EfiFileName[Index + 2] = 'f';
619 EfiFileName[Index + 3] = 'i';
620 EfiFileName[Index + 4] = 0;
621 break;
622 }
623 }
624
625 if (Index == sizeof (EfiFileName) - 4) {
626 EfiFileName[Index] = 0;
627 }
628 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "%a", EfiFileName)); // &Image->ImageContext.PdbPointer[StartIndex]));
629 }
630 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "\n"));
631
632 DEBUG_CODE_END ();
633
634 //
635 // Free buffer allocated by Fv->ReadSection.
636 //
637 // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection
638 // used the UEFI Boot Services AllocatePool() function
639 //
640 Status = gBS->FreePool(Buffer);
641 return Status;
642 }
643
644 /**
645 Preprocess dependency expression and update DriverEntry to reflect the
646 state of Before and After dependencies. If DriverEntry->Before
647 or DriverEntry->After is set it will never be cleared.
648
649 @param DriverEntry DriverEntry element to update .
650
651 @retval EFI_SUCCESS It always works.
652
653 **/
654 EFI_STATUS
655 SmmPreProcessDepex (
656 IN EFI_SMM_DRIVER_ENTRY *DriverEntry
657 )
658 {
659 UINT8 *Iterator;
660
661 Iterator = DriverEntry->Depex;
662 DriverEntry->Dependent = TRUE;
663
664 if (*Iterator == EFI_DEP_BEFORE) {
665 DriverEntry->Before = TRUE;
666 } else if (*Iterator == EFI_DEP_AFTER) {
667 DriverEntry->After = TRUE;
668 }
669
670 if (DriverEntry->Before || DriverEntry->After) {
671 CopyMem (&DriverEntry->BeforeAfterGuid, Iterator + 1, sizeof (EFI_GUID));
672 }
673
674 return EFI_SUCCESS;
675 }
676
677 /**
678 Read Depex and pre-process the Depex for Before and After. If Section Extraction
679 protocol returns an error via ReadSection defer the reading of the Depex.
680
681 @param DriverEntry Driver to work on.
682
683 @retval EFI_SUCCESS Depex read and preprossesed
684 @retval EFI_PROTOCOL_ERROR The section extraction protocol returned an error
685 and Depex reading needs to be retried.
686 @retval Error DEPEX not found.
687
688 **/
689 EFI_STATUS
690 SmmGetDepexSectionAndPreProccess (
691 IN EFI_SMM_DRIVER_ENTRY *DriverEntry
692 )
693 {
694 EFI_STATUS Status;
695 EFI_SECTION_TYPE SectionType;
696 UINT32 AuthenticationStatus;
697 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
698
699 Fv = DriverEntry->Fv;
700
701 //
702 // Grab Depex info, it will never be free'ed.
703 // (Note: DriverEntry->Depex is in DXE memory)
704 //
705 SectionType = EFI_SECTION_SMM_DEPEX;
706 Status = Fv->ReadSection (
707 DriverEntry->Fv,
708 &DriverEntry->FileName,
709 SectionType,
710 0,
711 &DriverEntry->Depex,
712 (UINTN *)&DriverEntry->DepexSize,
713 &AuthenticationStatus
714 );
715 if (EFI_ERROR (Status)) {
716 if (Status == EFI_PROTOCOL_ERROR) {
717 //
718 // The section extraction protocol failed so set protocol error flag
719 //
720 DriverEntry->DepexProtocolError = TRUE;
721 } else {
722 //
723 // If no Depex assume depend on all architectural protocols
724 //
725 DriverEntry->Depex = NULL;
726 DriverEntry->Dependent = TRUE;
727 DriverEntry->DepexProtocolError = FALSE;
728 }
729 } else {
730 //
731 // Set Before and After state information based on Depex
732 // Driver will be put in Dependent state
733 //
734 SmmPreProcessDepex (DriverEntry);
735 DriverEntry->DepexProtocolError = FALSE;
736 }
737
738 return Status;
739 }
740
741 /**
742 This is the main Dispatcher for SMM and it exits when there are no more
743 drivers to run. Drain the mScheduledQueue and load and start a PE
744 image for each driver. Search the mDiscoveredList to see if any driver can
745 be placed on the mScheduledQueue. If no drivers are placed on the
746 mScheduledQueue exit the function. On exit it is assumed the Bds()
747 will be called, and when the Bds() exits the Dispatcher will be called
748 again.
749
750 @retval EFI_ALREADY_STARTED The SMM Dispatcher is already running
751 @retval EFI_NOT_FOUND No SMM Drivers were dispatched
752 @retval EFI_SUCCESS One or more SMM Drivers were dispatched
753
754 **/
755 EFI_STATUS
756 SmmDispatcher (
757 VOID
758 )
759 {
760 EFI_STATUS Status;
761 EFI_STATUS ReturnStatus;
762 LIST_ENTRY *Link;
763 EFI_SMM_DRIVER_ENTRY *DriverEntry;
764 BOOLEAN ReadyToRun;
765
766 if (!gRequestDispatch) {
767 return EFI_NOT_FOUND;
768 }
769
770 if (gDispatcherRunning) {
771 //
772 // If the dispatcher is running don't let it be restarted.
773 //
774 return EFI_ALREADY_STARTED;
775 }
776
777 gDispatcherRunning = TRUE;
778
779 ReturnStatus = EFI_NOT_FOUND;
780 do {
781 //
782 // Drain the Scheduled Queue
783 //
784 while (!IsListEmpty (&mScheduledQueue)) {
785 DriverEntry = CR (
786 mScheduledQueue.ForwardLink,
787 EFI_SMM_DRIVER_ENTRY,
788 ScheduledLink,
789 EFI_SMM_DRIVER_ENTRY_SIGNATURE
790 );
791
792 //
793 // Load the SMM Driver image into memory. If the Driver was transitioned from
794 // Untrused to Scheduled it would have already been loaded so we may need to
795 // skip the LoadImage
796 //
797 if (DriverEntry->ImageHandle == NULL) {
798 Status = SmmLoadImage (DriverEntry);
799
800 //
801 // Update the driver state to reflect that it's been loaded
802 //
803 if (EFI_ERROR (Status)) {
804 //
805 // The SMM Driver could not be loaded, and do not attempt to load or start it again.
806 // Take driver from Scheduled to Initialized.
807 //
808 DriverEntry->Initialized = TRUE;
809 DriverEntry->Scheduled = FALSE;
810 RemoveEntryList (&DriverEntry->ScheduledLink);
811
812 //
813 // If it's an error don't try the StartImage
814 //
815 continue;
816 }
817 }
818
819 DriverEntry->Scheduled = FALSE;
820 DriverEntry->Initialized = TRUE;
821 RemoveEntryList (&DriverEntry->ScheduledLink);
822
823 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
824 EFI_PROGRESS_CODE,
825 EFI_SOFTWARE_SMM_DRIVER | EFI_SW_PC_INIT_BEGIN,
826 &DriverEntry->ImageHandle,
827 sizeof (DriverEntry->ImageHandle)
828 );
829
830 //
831 // For each SMM driver, pass NULL as ImageHandle
832 //
833 Status = ((EFI_IMAGE_ENTRY_POINT)(UINTN)DriverEntry->ImageEntryPoint)(DriverEntry->ImageHandle, gST);
834 if (EFI_ERROR(Status)){
835 SmmFreePages(DriverEntry->ImageBuffer, DriverEntry->NumberOfPage);
836 }
837
838 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
839 EFI_PROGRESS_CODE,
840 EFI_SOFTWARE_SMM_DRIVER | EFI_SW_PC_INIT_END,
841 &DriverEntry->ImageHandle,
842 sizeof (DriverEntry->ImageHandle)
843 );
844
845 ReturnStatus = EFI_SUCCESS;
846 }
847
848 //
849 // Search DriverList for items to place on Scheduled Queue
850 //
851 ReadyToRun = FALSE;
852 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
853 DriverEntry = CR (Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);
854
855 if (DriverEntry->DepexProtocolError){
856 //
857 // If Section Extraction Protocol did not let the Depex be read before retry the read
858 //
859 Status = SmmGetDepexSectionAndPreProccess (DriverEntry);
860 }
861
862 if (DriverEntry->Dependent) {
863 if (SmmIsSchedulable (DriverEntry)) {
864 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
865 ReadyToRun = TRUE;
866 }
867 }
868 }
869 } while (ReadyToRun);
870
871 //
872 // If there is no more SMM driver to dispatch, stop the dispatch request
873 //
874 gRequestDispatch = FALSE;
875 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
876 DriverEntry = CR (Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);
877
878 if (!DriverEntry->Initialized){
879 //
880 // We have SMM driver pending to dispatch
881 //
882 gRequestDispatch = TRUE;
883 break;
884 }
885 }
886
887 gDispatcherRunning = FALSE;
888
889 return ReturnStatus;
890 }
891
892 /**
893 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
894 must add any driver with a before dependency on InsertedDriverEntry first.
895 You do this by recursively calling this routine. After all the Befores are
896 processed you can add InsertedDriverEntry to the mScheduledQueue.
897 Then you can add any driver with an After dependency on InsertedDriverEntry
898 by recursively calling this routine.
899
900 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue
901
902 **/
903 VOID
904 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
905 IN EFI_SMM_DRIVER_ENTRY *InsertedDriverEntry
906 )
907 {
908 LIST_ENTRY *Link;
909 EFI_SMM_DRIVER_ENTRY *DriverEntry;
910
911 //
912 // Process Before Dependency
913 //
914 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
915 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);
916 if (DriverEntry->Before && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {
917 DEBUG ((DEBUG_DISPATCH, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
918 DEBUG ((DEBUG_DISPATCH, " BEFORE FFS(%g) = ", &DriverEntry->BeforeAfterGuid));
919 if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {
920 //
921 // Recursively process BEFORE
922 //
923 DEBUG ((DEBUG_DISPATCH, "TRUE\n END\n RESULT = TRUE\n"));
924 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
925 } else {
926 DEBUG ((DEBUG_DISPATCH, "FALSE\n END\n RESULT = FALSE\n"));
927 }
928 }
929 }
930
931 //
932 // Convert driver from Dependent to Scheduled state
933 //
934
935 InsertedDriverEntry->Dependent = FALSE;
936 InsertedDriverEntry->Scheduled = TRUE;
937 InsertTailList (&mScheduledQueue, &InsertedDriverEntry->ScheduledLink);
938
939
940 //
941 // Process After Dependency
942 //
943 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
944 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);
945 if (DriverEntry->After && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {
946 DEBUG ((DEBUG_DISPATCH, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
947 DEBUG ((DEBUG_DISPATCH, " AFTER FFS(%g) = ", &DriverEntry->BeforeAfterGuid));
948 if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {
949 //
950 // Recursively process AFTER
951 //
952 DEBUG ((DEBUG_DISPATCH, "TRUE\n END\n RESULT = TRUE\n"));
953 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
954 } else {
955 DEBUG ((DEBUG_DISPATCH, "FALSE\n END\n RESULT = FALSE\n"));
956 }
957 }
958 }
959 }
960
961 /**
962 Return TRUE if the Fv has been processed, FALSE if not.
963
964 @param FvHandle The handle of a FV that's being tested
965
966 @retval TRUE Fv protocol on FvHandle has been processed
967 @retval FALSE Fv protocol on FvHandle has not yet been
968 processed
969
970 **/
971 BOOLEAN
972 FvHasBeenProcessed (
973 IN EFI_HANDLE FvHandle
974 )
975 {
976 LIST_ENTRY *Link;
977 KNOWN_HANDLE *KnownHandle;
978
979 for (Link = mFvHandleList.ForwardLink; Link != &mFvHandleList; Link = Link->ForwardLink) {
980 KnownHandle = CR(Link, KNOWN_HANDLE, Link, KNOWN_HANDLE_SIGNATURE);
981 if (KnownHandle->Handle == FvHandle) {
982 return TRUE;
983 }
984 }
985 return FALSE;
986 }
987
988 /**
989 Remember that Fv protocol on FvHandle has had it's drivers placed on the
990 mDiscoveredList. This fucntion adds entries on the mFvHandleList. Items are
991 never removed/freed from the mFvHandleList.
992
993 @param FvHandle The handle of a FV that has been processed
994
995 **/
996 VOID
997 FvIsBeingProcesssed (
998 IN EFI_HANDLE FvHandle
999 )
1000 {
1001 KNOWN_HANDLE *KnownHandle;
1002
1003 KnownHandle = AllocatePool (sizeof (KNOWN_HANDLE));
1004 ASSERT (KnownHandle != NULL);
1005
1006 KnownHandle->Signature = KNOWN_HANDLE_SIGNATURE;
1007 KnownHandle->Handle = FvHandle;
1008 InsertTailList (&mFvHandleList, &KnownHandle->Link);
1009 }
1010
1011 /**
1012 Convert FvHandle and DriverName into an EFI device path
1013
1014 @param Fv Fv protocol, needed to read Depex info out of
1015 FLASH.
1016 @param FvHandle Handle for Fv, needed in the
1017 EFI_SMM_DRIVER_ENTRY so that the PE image can be
1018 read out of the FV at a later time.
1019 @param DriverName Name of driver to add to mDiscoveredList.
1020
1021 @return Pointer to device path constructed from FvHandle and DriverName
1022
1023 **/
1024 EFI_DEVICE_PATH_PROTOCOL *
1025 SmmFvToDevicePath (
1026 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,
1027 IN EFI_HANDLE FvHandle,
1028 IN EFI_GUID *DriverName
1029 )
1030 {
1031 EFI_STATUS Status;
1032 EFI_DEVICE_PATH_PROTOCOL *FvDevicePath;
1033 EFI_DEVICE_PATH_PROTOCOL *FileNameDevicePath;
1034
1035 //
1036 // Remember the device path of the FV
1037 //
1038 Status = gBS->HandleProtocol (FvHandle, &gEfiDevicePathProtocolGuid, (VOID **)&FvDevicePath);
1039 if (EFI_ERROR (Status)) {
1040 FileNameDevicePath = NULL;
1041 } else {
1042 //
1043 // Build a device path to the file in the FV to pass into gBS->LoadImage
1044 //
1045 EfiInitializeFwVolDevicepathNode (&mFvDevicePath.File, DriverName);
1046 SetDevicePathEndNode (&mFvDevicePath.End);
1047
1048 //
1049 // Note: FileNameDevicePath is in DXE memory
1050 //
1051 FileNameDevicePath = AppendDevicePath (
1052 FvDevicePath,
1053 (EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath
1054 );
1055 }
1056 return FileNameDevicePath;
1057 }
1058
1059 /**
1060 Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,
1061 and initilize any state variables. Read the Depex from the FV and store it
1062 in DriverEntry. Pre-process the Depex to set the Before and After state.
1063 The Discovered list is never free'ed and contains booleans that represent the
1064 other possible SMM driver states.
1065
1066 @param Fv Fv protocol, needed to read Depex info out of
1067 FLASH.
1068 @param FvHandle Handle for Fv, needed in the
1069 EFI_SMM_DRIVER_ENTRY so that the PE image can be
1070 read out of the FV at a later time.
1071 @param DriverName Name of driver to add to mDiscoveredList.
1072
1073 @retval EFI_SUCCESS If driver was added to the mDiscoveredList.
1074 @retval EFI_ALREADY_STARTED The driver has already been started. Only one
1075 DriverName may be active in the system at any one
1076 time.
1077
1078 **/
1079 EFI_STATUS
1080 SmmAddToDriverList (
1081 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,
1082 IN EFI_HANDLE FvHandle,
1083 IN EFI_GUID *DriverName
1084 )
1085 {
1086 EFI_SMM_DRIVER_ENTRY *DriverEntry;
1087
1088 //
1089 // Create the Driver Entry for the list. ZeroPool initializes lots of variables to
1090 // NULL or FALSE.
1091 //
1092 DriverEntry = AllocateZeroPool (sizeof (EFI_SMM_DRIVER_ENTRY));
1093 ASSERT (DriverEntry != NULL);
1094
1095 DriverEntry->Signature = EFI_SMM_DRIVER_ENTRY_SIGNATURE;
1096 CopyGuid (&DriverEntry->FileName, DriverName);
1097 DriverEntry->FvHandle = FvHandle;
1098 DriverEntry->Fv = Fv;
1099 DriverEntry->FvFileDevicePath = SmmFvToDevicePath (Fv, FvHandle, DriverName);
1100
1101 SmmGetDepexSectionAndPreProccess (DriverEntry);
1102
1103 InsertTailList (&mDiscoveredList, &DriverEntry->Link);
1104 gRequestDispatch = TRUE;
1105
1106 return EFI_SUCCESS;
1107 }
1108
1109 /**
1110 This function is the main entry point for an SMM handler dispatch
1111 or communicate-based callback.
1112
1113 Event notification that is fired every time a FV dispatch protocol is added.
1114 More than one protocol may have been added when this event is fired, so you
1115 must loop on SmmLocateHandle () to see how many protocols were added and
1116 do the following to each FV:
1117 If the Fv has already been processed, skip it. If the Fv has not been
1118 processed then mark it as being processed, as we are about to process it.
1119 Read the Fv and add any driver in the Fv to the mDiscoveredList.The
1120 mDiscoveredList is never free'ed and contains variables that define
1121 the other states the SMM driver transitions to..
1122 While you are at it read the A Priori file into memory.
1123 Place drivers in the A Priori list onto the mScheduledQueue.
1124
1125 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
1126 @param Context Points to an optional handler context which was specified when the handler was registered.
1127 @param CommBuffer A pointer to a collection of data in memory that will
1128 be conveyed from a non-SMM environment into an SMM environment.
1129 @param CommBufferSize The size of the CommBuffer.
1130
1131 @return Status Code
1132
1133 **/
1134 EFI_STATUS
1135 EFIAPI
1136 SmmDriverDispatchHandler (
1137 IN EFI_HANDLE DispatchHandle,
1138 IN CONST VOID *Context, OPTIONAL
1139 IN OUT VOID *CommBuffer, OPTIONAL
1140 IN OUT UINTN *CommBufferSize OPTIONAL
1141 )
1142 {
1143 EFI_STATUS Status;
1144 UINTN HandleCount;
1145 EFI_HANDLE *HandleBuffer;
1146 EFI_STATUS GetNextFileStatus;
1147 EFI_STATUS SecurityStatus;
1148 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
1149 EFI_DEVICE_PATH_PROTOCOL *FvDevicePath;
1150 EFI_HANDLE FvHandle;
1151 EFI_GUID NameGuid;
1152 UINTN Key;
1153 EFI_FV_FILETYPE Type;
1154 EFI_FV_FILE_ATTRIBUTES Attributes;
1155 UINTN Size;
1156 EFI_SMM_DRIVER_ENTRY *DriverEntry;
1157 EFI_GUID *AprioriFile;
1158 UINTN AprioriEntryCount;
1159 UINTN Index;
1160 LIST_ENTRY *Link;
1161 UINT32 AuthenticationStatus;
1162 UINTN SizeOfBuffer;
1163
1164 HandleBuffer = NULL;
1165 Status = gBS->LocateHandleBuffer (
1166 ByProtocol,
1167 &gEfiFirmwareVolume2ProtocolGuid,
1168 NULL,
1169 &HandleCount,
1170 &HandleBuffer
1171 );
1172 if (EFI_ERROR (Status)) {
1173 return EFI_NOT_FOUND;
1174 }
1175
1176 for (Index = 0; Index < HandleCount; Index++) {
1177 FvHandle = HandleBuffer[Index];
1178
1179 if (FvHasBeenProcessed (FvHandle)) {
1180 //
1181 // This Fv has already been processed so lets skip it!
1182 //
1183 continue;
1184 }
1185
1186 //
1187 // Since we are about to process this Fv mark it as processed.
1188 //
1189 FvIsBeingProcesssed (FvHandle);
1190
1191 Status = gBS->HandleProtocol (FvHandle, &gEfiFirmwareVolume2ProtocolGuid, (VOID **)&Fv);
1192 if (EFI_ERROR (Status)) {
1193 //
1194 // FvHandle must have a Firmware Volume2 Protocol thus we should never get here.
1195 //
1196 ASSERT (FALSE);
1197 continue;
1198 }
1199
1200 Status = gBS->HandleProtocol (FvHandle, &gEfiDevicePathProtocolGuid, (VOID **)&FvDevicePath);
1201 if (EFI_ERROR (Status)) {
1202 //
1203 // The Firmware volume doesn't have device path, can't be dispatched.
1204 //
1205 continue;
1206 }
1207
1208 //
1209 // If the Security Architectural Protocol has not been located yet, then attempt to locate it
1210 //
1211 if (mSecurity == NULL) {
1212 gBS->LocateProtocol (&gEfiSecurityArchProtocolGuid, NULL, (VOID**)&mSecurity);
1213 }
1214
1215 //
1216 // Evaluate the authentication status of the Firmware Volume through
1217 // Security Architectural Protocol
1218 //
1219 if (mSecurity != NULL) {
1220 SecurityStatus = mSecurity->FileAuthenticationState (
1221 mSecurity,
1222 0,
1223 FvDevicePath
1224 );
1225 if (SecurityStatus != EFI_SUCCESS) {
1226 //
1227 // Security check failed. The firmware volume should not be used for any purpose.
1228 //
1229 continue;
1230 }
1231 }
1232
1233 //
1234 // Discover Drivers in FV and add them to the Discovered Driver List.
1235 // Process EFI_FV_FILETYPE_SMM type and then EFI_FV_FILETYPE_COMBINED_SMM_DXE
1236 //
1237 for (Index = 0; Index < sizeof (mSmmFileTypes)/sizeof (EFI_FV_FILETYPE); Index++) {
1238 //
1239 // Initialize the search key
1240 //
1241 Key = 0;
1242 do {
1243 Type = mSmmFileTypes[Index];
1244 GetNextFileStatus = Fv->GetNextFile (
1245 Fv,
1246 &Key,
1247 &Type,
1248 &NameGuid,
1249 &Attributes,
1250 &Size
1251 );
1252 if (!EFI_ERROR (GetNextFileStatus)) {
1253 SmmAddToDriverList (Fv, FvHandle, &NameGuid);
1254 }
1255 } while (!EFI_ERROR (GetNextFileStatus));
1256 }
1257
1258 //
1259 // Read the array of GUIDs from the Apriori file if it is present in the firmware volume
1260 // (Note: AprioriFile is in DXE memory)
1261 //
1262 AprioriFile = NULL;
1263 Status = Fv->ReadSection (
1264 Fv,
1265 &gAprioriGuid,
1266 EFI_SECTION_RAW,
1267 0,
1268 (VOID **)&AprioriFile,
1269 &SizeOfBuffer,
1270 &AuthenticationStatus
1271 );
1272 if (!EFI_ERROR (Status)) {
1273 AprioriEntryCount = SizeOfBuffer / sizeof (EFI_GUID);
1274 } else {
1275 AprioriEntryCount = 0;
1276 }
1277
1278 //
1279 // Put drivers on Apriori List on the Scheduled queue. The Discovered List includes
1280 // drivers not in the current FV and these must be skipped since the a priori list
1281 // is only valid for the FV that it resided in.
1282 //
1283
1284 for (Index = 0; Index < AprioriEntryCount; Index++) {
1285 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
1286 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);
1287 if (CompareGuid (&DriverEntry->FileName, &AprioriFile[Index]) &&
1288 (FvHandle == DriverEntry->FvHandle)) {
1289 DriverEntry->Dependent = FALSE;
1290 DriverEntry->Scheduled = TRUE;
1291 InsertTailList (&mScheduledQueue, &DriverEntry->ScheduledLink);
1292 DEBUG ((DEBUG_DISPATCH, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
1293 DEBUG ((DEBUG_DISPATCH, " RESULT = TRUE (Apriori)\n"));
1294 break;
1295 }
1296 }
1297 }
1298
1299 //
1300 // Free data allocated by Fv->ReadSection ()
1301 //
1302 // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection
1303 // used the UEFI Boot Services AllocatePool() function
1304 //
1305 gBS->FreePool (AprioriFile);
1306 }
1307
1308 //
1309 // Execute the SMM Dispatcher on any newly discovered FVs and previously
1310 // discovered SMM drivers that have been discovered but not dispatched.
1311 //
1312 return SmmDispatcher ();
1313 }
1314
1315 /**
1316 Traverse the discovered list for any drivers that were discovered but not loaded
1317 because the dependency experessions evaluated to false.
1318
1319 **/
1320 VOID
1321 SmmDisplayDiscoveredNotDispatched (
1322 VOID
1323 )
1324 {
1325 LIST_ENTRY *Link;
1326 EFI_SMM_DRIVER_ENTRY *DriverEntry;
1327
1328 for (Link = mDiscoveredList.ForwardLink;Link !=&mDiscoveredList; Link = Link->ForwardLink) {
1329 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);
1330 if (DriverEntry->Dependent) {
1331 DEBUG ((DEBUG_LOAD, "SMM Driver %g was discovered but not loaded!!\n", &DriverEntry->FileName));
1332 }
1333 }
1334 }