]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/PiSmmCore/Dispatcher.c
Add Perf log for SMM driver Load and Start Image.
[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 - 2012, 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 UINT64 Tick;
324
325 Tick = 0;
326 PERF_CODE (
327 Tick = GetPerformanceCounter ();
328 );
329
330 Buffer = NULL;
331 Size = 0;
332 Fv = DriverEntry->Fv;
333 NameGuid = &DriverEntry->FileName;
334 FilePath = DriverEntry->FvFileDevicePath;
335
336 OriginalFilePath = FilePath;
337 HandleFilePath = FilePath;
338 DeviceHandle = NULL;
339 SecurityStatus = EFI_SUCCESS;
340 Status = EFI_SUCCESS;
341 AuthenticationStatus = 0;
342
343 //
344 // Try to get the image device handle by checking the match protocol.
345 //
346 Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &HandleFilePath, &DeviceHandle);
347 if (EFI_ERROR(Status)) {
348 return Status;
349 }
350
351 //
352 // If the Security Architectural Protocol has not been located yet, then attempt to locate it
353 //
354 if (mSecurity == NULL) {
355 gBS->LocateProtocol (&gEfiSecurityArchProtocolGuid, NULL, (VOID**)&mSecurity);
356 }
357
358 //
359 // Verify the Authentication Status through the Security Architectural Protocol
360 //
361 if ((mSecurity != NULL) && (OriginalFilePath != NULL)) {
362 SecurityStatus = mSecurity->FileAuthenticationState (
363 mSecurity,
364 AuthenticationStatus,
365 OriginalFilePath
366 );
367 if (EFI_ERROR (SecurityStatus) && SecurityStatus != EFI_SECURITY_VIOLATION) {
368 Status = SecurityStatus;
369 return Status;
370 }
371 }
372
373 //
374 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath
375 //
376 FilePath = OriginalFilePath;
377 Status = gBS->HandleProtocol (DeviceHandle, &gEfiDevicePathProtocolGuid, (VOID **)&HandleFilePath);
378 if (!EFI_ERROR (Status)) {
379 FilePathSize = GetDevicePathSize (HandleFilePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL);
380 FilePath = (EFI_DEVICE_PATH_PROTOCOL *) (((UINT8 *)FilePath) + FilePathSize );
381 }
382
383 //
384 // Try reading PE32 section firstly
385 //
386 Status = Fv->ReadSection (
387 Fv,
388 NameGuid,
389 EFI_SECTION_PE32,
390 0,
391 &Buffer,
392 &Size,
393 &AuthenticationStatus
394 );
395
396 if (EFI_ERROR (Status)) {
397 //
398 // Try reading TE section secondly
399 //
400 Buffer = NULL;
401 Size = 0;
402 Status = Fv->ReadSection (
403 Fv,
404 NameGuid,
405 EFI_SECTION_TE,
406 0,
407 &Buffer,
408 &Size,
409 &AuthenticationStatus
410 );
411 }
412
413 if (EFI_ERROR (Status)) {
414 if (Buffer != NULL) {
415 gBS->FreePool (Buffer);
416 }
417 return Status;
418 }
419
420 //
421 // Initialize ImageContext
422 //
423 ImageContext.Handle = Buffer;
424 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;
425
426 //
427 // Get information about the image being loaded
428 //
429 Status = PeCoffLoaderGetImageInfo (&ImageContext);
430 if (EFI_ERROR (Status)) {
431 if (Buffer != NULL) {
432 gBS->FreePool (Buffer);
433 }
434 return Status;
435 }
436 //
437 // if Loading module at Fixed Address feature is enabled, then cut out a memory range started from TESG BASE
438 // to hold the Smm driver code
439 //
440 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {
441 //
442 // Get the fixed loading address assigned by Build tool
443 //
444 Status = GetPeCoffImageFixLoadingAssignedAddress (&ImageContext);
445 if (!EFI_ERROR (Status)) {
446 //
447 // Since the memory range to load Smm core alreay been cut out, so no need to allocate and free this range
448 // following statements is to bypass SmmFreePages
449 //
450 PageCount = 0;
451 DstBuffer = (UINTN)gLoadModuleAtFixAddressSmramBase;
452 } else {
453 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED ERROR: Failed to load module at fixed address. \n"));
454 //
455 // allocate the memory to load the SMM driver
456 //
457 PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);
458 DstBuffer = (UINTN)(-1);
459
460 Status = SmmAllocatePages (
461 AllocateMaxAddress,
462 EfiRuntimeServicesCode,
463 PageCount,
464 &DstBuffer
465 );
466 if (EFI_ERROR (Status)) {
467 if (Buffer != NULL) {
468 gBS->FreePool (Buffer);
469 }
470 return Status;
471 }
472 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)DstBuffer;
473 }
474 } else {
475 PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);
476 DstBuffer = (UINTN)(-1);
477
478 Status = SmmAllocatePages (
479 AllocateMaxAddress,
480 EfiRuntimeServicesCode,
481 PageCount,
482 &DstBuffer
483 );
484 if (EFI_ERROR (Status)) {
485 if (Buffer != NULL) {
486 gBS->FreePool (Buffer);
487 }
488 return Status;
489 }
490
491 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)DstBuffer;
492 }
493 //
494 // Align buffer on section boundry
495 //
496 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;
497 ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);
498
499 //
500 // Load the image to our new buffer
501 //
502 Status = PeCoffLoaderLoadImage (&ImageContext);
503 if (EFI_ERROR (Status)) {
504 if (Buffer != NULL) {
505 gBS->FreePool (Buffer);
506 }
507 SmmFreePages (DstBuffer, PageCount);
508 return Status;
509 }
510
511 //
512 // Relocate the image in our new buffer
513 //
514 Status = PeCoffLoaderRelocateImage (&ImageContext);
515 if (EFI_ERROR (Status)) {
516 if (Buffer != NULL) {
517 gBS->FreePool (Buffer);
518 }
519 SmmFreePages (DstBuffer, PageCount);
520 return Status;
521 }
522
523 //
524 // Flush the instruction cache so the image data are written before we execute it
525 //
526 InvalidateInstructionCacheRange ((VOID *)(UINTN) ImageContext.ImageAddress, (UINTN) ImageContext.ImageSize);
527
528 //
529 // Save Image EntryPoint in DriverEntry
530 //
531 DriverEntry->ImageEntryPoint = ImageContext.EntryPoint;
532 DriverEntry->ImageBuffer = DstBuffer;
533 DriverEntry->NumberOfPage = PageCount;
534
535 //
536 // Allocate a Loaded Image Protocol in EfiBootServicesData
537 //
538 Status = gBS->AllocatePool (EfiBootServicesData, sizeof (EFI_LOADED_IMAGE_PROTOCOL), (VOID **)&DriverEntry->LoadedImage);
539 if (EFI_ERROR (Status)) {
540 if (Buffer != NULL) {
541 gBS->FreePool (Buffer);
542 }
543 SmmFreePages (DstBuffer, PageCount);
544 return Status;
545 }
546
547 //
548 // Fill in the remaining fields of the Loaded Image Protocol instance.
549 // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.
550 //
551 DriverEntry->LoadedImage->Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
552 DriverEntry->LoadedImage->ParentHandle = gSmmCorePrivate->SmmIplImageHandle;
553 DriverEntry->LoadedImage->SystemTable = gST;
554 DriverEntry->LoadedImage->DeviceHandle = DeviceHandle;
555
556 //
557 // Make an EfiBootServicesData buffer copy of FilePath
558 //
559 Status = gBS->AllocatePool (EfiBootServicesData, GetDevicePathSize (FilePath), (VOID **)&DriverEntry->LoadedImage->FilePath);
560 if (EFI_ERROR (Status)) {
561 if (Buffer != NULL) {
562 gBS->FreePool (Buffer);
563 }
564 SmmFreePages (DstBuffer, PageCount);
565 return Status;
566 }
567 CopyMem (DriverEntry->LoadedImage->FilePath, FilePath, GetDevicePathSize (FilePath));
568
569 DriverEntry->LoadedImage->ImageBase = (VOID *)(UINTN)DriverEntry->ImageBuffer;
570 DriverEntry->LoadedImage->ImageSize = ImageContext.ImageSize;
571 DriverEntry->LoadedImage->ImageCodeType = EfiRuntimeServicesCode;
572 DriverEntry->LoadedImage->ImageDataType = EfiRuntimeServicesData;
573
574 //
575 // Create a new image handle in the UEFI handle database for the SMM Driver
576 //
577 DriverEntry->ImageHandle = NULL;
578 Status = gBS->InstallMultipleProtocolInterfaces (
579 &DriverEntry->ImageHandle,
580 &gEfiLoadedImageProtocolGuid, DriverEntry->LoadedImage,
581 NULL
582 );
583
584 PERF_START (DriverEntry->ImageHandle, "LoadImage:", NULL, Tick);
585 PERF_END (DriverEntry->ImageHandle, "LoadImage:", NULL, 0);
586
587 //
588 // Print the load address and the PDB file name if it is available
589 //
590
591 DEBUG_CODE_BEGIN ();
592
593 UINTN Index;
594 UINTN StartIndex;
595 CHAR8 EfiFileName[256];
596
597
598 DEBUG ((DEBUG_INFO | DEBUG_LOAD,
599 "Loading SMM driver at 0x%11p EntryPoint=0x%11p ",
600 (VOID *)(UINTN) ImageContext.ImageAddress,
601 FUNCTION_ENTRY_POINT (ImageContext.EntryPoint)));
602
603
604 //
605 // Print Module Name by Pdb file path.
606 // Windows and Unix style file path are all trimmed correctly.
607 //
608 if (ImageContext.PdbPointer != NULL) {
609 StartIndex = 0;
610 for (Index = 0; ImageContext.PdbPointer[Index] != 0; Index++) {
611 if ((ImageContext.PdbPointer[Index] == '\\') || (ImageContext.PdbPointer[Index] == '/')) {
612 StartIndex = Index + 1;
613 }
614 }
615 //
616 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
617 // The PDB file name is limited in the range of 0~255.
618 // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.
619 //
620 for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {
621 EfiFileName[Index] = ImageContext.PdbPointer[Index + StartIndex];
622 if (EfiFileName[Index] == 0) {
623 EfiFileName[Index] = '.';
624 }
625 if (EfiFileName[Index] == '.') {
626 EfiFileName[Index + 1] = 'e';
627 EfiFileName[Index + 2] = 'f';
628 EfiFileName[Index + 3] = 'i';
629 EfiFileName[Index + 4] = 0;
630 break;
631 }
632 }
633
634 if (Index == sizeof (EfiFileName) - 4) {
635 EfiFileName[Index] = 0;
636 }
637 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "%a", EfiFileName)); // &Image->ImageContext.PdbPointer[StartIndex]));
638 }
639 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "\n"));
640
641 DEBUG_CODE_END ();
642
643 //
644 // Free buffer allocated by Fv->ReadSection.
645 //
646 // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection
647 // used the UEFI Boot Services AllocatePool() function
648 //
649 Status = gBS->FreePool(Buffer);
650 return Status;
651 }
652
653 /**
654 Preprocess dependency expression and update DriverEntry to reflect the
655 state of Before and After dependencies. If DriverEntry->Before
656 or DriverEntry->After is set it will never be cleared.
657
658 @param DriverEntry DriverEntry element to update .
659
660 @retval EFI_SUCCESS It always works.
661
662 **/
663 EFI_STATUS
664 SmmPreProcessDepex (
665 IN EFI_SMM_DRIVER_ENTRY *DriverEntry
666 )
667 {
668 UINT8 *Iterator;
669
670 Iterator = DriverEntry->Depex;
671 DriverEntry->Dependent = TRUE;
672
673 if (*Iterator == EFI_DEP_BEFORE) {
674 DriverEntry->Before = TRUE;
675 } else if (*Iterator == EFI_DEP_AFTER) {
676 DriverEntry->After = TRUE;
677 }
678
679 if (DriverEntry->Before || DriverEntry->After) {
680 CopyMem (&DriverEntry->BeforeAfterGuid, Iterator + 1, sizeof (EFI_GUID));
681 }
682
683 return EFI_SUCCESS;
684 }
685
686 /**
687 Read Depex and pre-process the Depex for Before and After. If Section Extraction
688 protocol returns an error via ReadSection defer the reading of the Depex.
689
690 @param DriverEntry Driver to work on.
691
692 @retval EFI_SUCCESS Depex read and preprossesed
693 @retval EFI_PROTOCOL_ERROR The section extraction protocol returned an error
694 and Depex reading needs to be retried.
695 @retval Error DEPEX not found.
696
697 **/
698 EFI_STATUS
699 SmmGetDepexSectionAndPreProccess (
700 IN EFI_SMM_DRIVER_ENTRY *DriverEntry
701 )
702 {
703 EFI_STATUS Status;
704 EFI_SECTION_TYPE SectionType;
705 UINT32 AuthenticationStatus;
706 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
707
708 Fv = DriverEntry->Fv;
709
710 //
711 // Grab Depex info, it will never be free'ed.
712 // (Note: DriverEntry->Depex is in DXE memory)
713 //
714 SectionType = EFI_SECTION_SMM_DEPEX;
715 Status = Fv->ReadSection (
716 DriverEntry->Fv,
717 &DriverEntry->FileName,
718 SectionType,
719 0,
720 &DriverEntry->Depex,
721 (UINTN *)&DriverEntry->DepexSize,
722 &AuthenticationStatus
723 );
724 if (EFI_ERROR (Status)) {
725 if (Status == EFI_PROTOCOL_ERROR) {
726 //
727 // The section extraction protocol failed so set protocol error flag
728 //
729 DriverEntry->DepexProtocolError = TRUE;
730 } else {
731 //
732 // If no Depex assume depend on all architectural protocols
733 //
734 DriverEntry->Depex = NULL;
735 DriverEntry->Dependent = TRUE;
736 DriverEntry->DepexProtocolError = FALSE;
737 }
738 } else {
739 //
740 // Set Before and After state information based on Depex
741 // Driver will be put in Dependent state
742 //
743 SmmPreProcessDepex (DriverEntry);
744 DriverEntry->DepexProtocolError = FALSE;
745 }
746
747 return Status;
748 }
749
750 /**
751 This is the main Dispatcher for SMM and it exits when there are no more
752 drivers to run. Drain the mScheduledQueue and load and start a PE
753 image for each driver. Search the mDiscoveredList to see if any driver can
754 be placed on the mScheduledQueue. If no drivers are placed on the
755 mScheduledQueue exit the function.
756
757 @retval EFI_SUCCESS All of the SMM Drivers that could be dispatched
758 have been run and the SMM Entry Point has been
759 registered.
760 @retval EFI_NOT_READY The SMM Driver that registered the SMM Entry Point
761 was just dispatched.
762 @retval EFI_NOT_FOUND There are no SMM Drivers available to be dispatched.
763 @retval EFI_ALREADY_STARTED The SMM Dispatcher is already running
764
765 **/
766 EFI_STATUS
767 SmmDispatcher (
768 VOID
769 )
770 {
771 EFI_STATUS Status;
772 LIST_ENTRY *Link;
773 EFI_SMM_DRIVER_ENTRY *DriverEntry;
774 BOOLEAN ReadyToRun;
775 BOOLEAN PreviousSmmEntryPointRegistered;
776
777 if (!gRequestDispatch) {
778 return EFI_NOT_FOUND;
779 }
780
781 if (gDispatcherRunning) {
782 //
783 // If the dispatcher is running don't let it be restarted.
784 //
785 return EFI_ALREADY_STARTED;
786 }
787
788 gDispatcherRunning = TRUE;
789
790 do {
791 //
792 // Drain the Scheduled Queue
793 //
794 while (!IsListEmpty (&mScheduledQueue)) {
795 DriverEntry = CR (
796 mScheduledQueue.ForwardLink,
797 EFI_SMM_DRIVER_ENTRY,
798 ScheduledLink,
799 EFI_SMM_DRIVER_ENTRY_SIGNATURE
800 );
801
802 //
803 // Load the SMM Driver image into memory. If the Driver was transitioned from
804 // Untrused to Scheduled it would have already been loaded so we may need to
805 // skip the LoadImage
806 //
807 if (DriverEntry->ImageHandle == NULL) {
808 Status = SmmLoadImage (DriverEntry);
809
810 //
811 // Update the driver state to reflect that it's been loaded
812 //
813 if (EFI_ERROR (Status)) {
814 //
815 // The SMM Driver could not be loaded, and do not attempt to load or start it again.
816 // Take driver from Scheduled to Initialized.
817 //
818 DriverEntry->Initialized = TRUE;
819 DriverEntry->Scheduled = FALSE;
820 RemoveEntryList (&DriverEntry->ScheduledLink);
821
822 //
823 // If it's an error don't try the StartImage
824 //
825 continue;
826 }
827 }
828
829 DriverEntry->Scheduled = FALSE;
830 DriverEntry->Initialized = TRUE;
831 RemoveEntryList (&DriverEntry->ScheduledLink);
832
833 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
834 EFI_PROGRESS_CODE,
835 EFI_SOFTWARE_SMM_DRIVER | EFI_SW_PC_INIT_BEGIN,
836 &DriverEntry->ImageHandle,
837 sizeof (DriverEntry->ImageHandle)
838 );
839
840 //
841 // Cache state of SmmEntryPointRegistered before calling entry point
842 //
843 PreviousSmmEntryPointRegistered = gSmmCorePrivate->SmmEntryPointRegistered;
844
845 //
846 // For each SMM driver, pass NULL as ImageHandle
847 //
848 PERF_START (DriverEntry->ImageHandle, "StartImage:", NULL, 0);
849 Status = ((EFI_IMAGE_ENTRY_POINT)(UINTN)DriverEntry->ImageEntryPoint)(DriverEntry->ImageHandle, gST);
850 PERF_END (DriverEntry->ImageHandle, "StartImage:", NULL, 0);
851 if (EFI_ERROR(Status)){
852 SmmFreePages(DriverEntry->ImageBuffer, DriverEntry->NumberOfPage);
853 }
854
855 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
856 EFI_PROGRESS_CODE,
857 EFI_SOFTWARE_SMM_DRIVER | EFI_SW_PC_INIT_END,
858 &DriverEntry->ImageHandle,
859 sizeof (DriverEntry->ImageHandle)
860 );
861
862 if (!PreviousSmmEntryPointRegistered && gSmmCorePrivate->SmmEntryPointRegistered) {
863 //
864 // Return immediately if the SMM Entry Point was registered by the SMM
865 // Driver that was just dispatched. The SMM IPL will reinvoke the SMM
866 // Core Dispatcher. This is required so SMM Mode may be enabled as soon
867 // as all the dependent SMM Drivers for SMM Mode have been dispatched.
868 // Once the SMM Entry Point has been registered, then SMM Mode will be
869 // used.
870 //
871 gRequestDispatch = TRUE;
872 gDispatcherRunning = FALSE;
873 return EFI_NOT_READY;
874 }
875 }
876
877 //
878 // Search DriverList for items to place on Scheduled Queue
879 //
880 ReadyToRun = FALSE;
881 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
882 DriverEntry = CR (Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);
883
884 if (DriverEntry->DepexProtocolError){
885 //
886 // If Section Extraction Protocol did not let the Depex be read before retry the read
887 //
888 Status = SmmGetDepexSectionAndPreProccess (DriverEntry);
889 }
890
891 if (DriverEntry->Dependent) {
892 if (SmmIsSchedulable (DriverEntry)) {
893 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
894 ReadyToRun = TRUE;
895 }
896 }
897 }
898 } while (ReadyToRun);
899
900 //
901 // If there is no more SMM driver to dispatch, stop the dispatch request
902 //
903 gRequestDispatch = FALSE;
904 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
905 DriverEntry = CR (Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);
906
907 if (!DriverEntry->Initialized){
908 //
909 // We have SMM driver pending to dispatch
910 //
911 gRequestDispatch = TRUE;
912 break;
913 }
914 }
915
916 gDispatcherRunning = FALSE;
917
918 return EFI_SUCCESS;
919 }
920
921 /**
922 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
923 must add any driver with a before dependency on InsertedDriverEntry first.
924 You do this by recursively calling this routine. After all the Befores are
925 processed you can add InsertedDriverEntry to the mScheduledQueue.
926 Then you can add any driver with an After dependency on InsertedDriverEntry
927 by recursively calling this routine.
928
929 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue
930
931 **/
932 VOID
933 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
934 IN EFI_SMM_DRIVER_ENTRY *InsertedDriverEntry
935 )
936 {
937 LIST_ENTRY *Link;
938 EFI_SMM_DRIVER_ENTRY *DriverEntry;
939
940 //
941 // Process Before 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->Before && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {
946 DEBUG ((DEBUG_DISPATCH, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
947 DEBUG ((DEBUG_DISPATCH, " BEFORE FFS(%g) = ", &DriverEntry->BeforeAfterGuid));
948 if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {
949 //
950 // Recursively process BEFORE
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 // Convert driver from Dependent to Scheduled state
962 //
963
964 InsertedDriverEntry->Dependent = FALSE;
965 InsertedDriverEntry->Scheduled = TRUE;
966 InsertTailList (&mScheduledQueue, &InsertedDriverEntry->ScheduledLink);
967
968
969 //
970 // Process After Dependency
971 //
972 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
973 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);
974 if (DriverEntry->After && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {
975 DEBUG ((DEBUG_DISPATCH, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
976 DEBUG ((DEBUG_DISPATCH, " AFTER FFS(%g) = ", &DriverEntry->BeforeAfterGuid));
977 if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {
978 //
979 // Recursively process AFTER
980 //
981 DEBUG ((DEBUG_DISPATCH, "TRUE\n END\n RESULT = TRUE\n"));
982 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
983 } else {
984 DEBUG ((DEBUG_DISPATCH, "FALSE\n END\n RESULT = FALSE\n"));
985 }
986 }
987 }
988 }
989
990 /**
991 Return TRUE if the Fv has been processed, FALSE if not.
992
993 @param FvHandle The handle of a FV that's being tested
994
995 @retval TRUE Fv protocol on FvHandle has been processed
996 @retval FALSE Fv protocol on FvHandle has not yet been
997 processed
998
999 **/
1000 BOOLEAN
1001 FvHasBeenProcessed (
1002 IN EFI_HANDLE FvHandle
1003 )
1004 {
1005 LIST_ENTRY *Link;
1006 KNOWN_HANDLE *KnownHandle;
1007
1008 for (Link = mFvHandleList.ForwardLink; Link != &mFvHandleList; Link = Link->ForwardLink) {
1009 KnownHandle = CR(Link, KNOWN_HANDLE, Link, KNOWN_HANDLE_SIGNATURE);
1010 if (KnownHandle->Handle == FvHandle) {
1011 return TRUE;
1012 }
1013 }
1014 return FALSE;
1015 }
1016
1017 /**
1018 Remember that Fv protocol on FvHandle has had it's drivers placed on the
1019 mDiscoveredList. This fucntion adds entries on the mFvHandleList. Items are
1020 never removed/freed from the mFvHandleList.
1021
1022 @param FvHandle The handle of a FV that has been processed
1023
1024 **/
1025 VOID
1026 FvIsBeingProcesssed (
1027 IN EFI_HANDLE FvHandle
1028 )
1029 {
1030 KNOWN_HANDLE *KnownHandle;
1031
1032 KnownHandle = AllocatePool (sizeof (KNOWN_HANDLE));
1033 ASSERT (KnownHandle != NULL);
1034
1035 KnownHandle->Signature = KNOWN_HANDLE_SIGNATURE;
1036 KnownHandle->Handle = FvHandle;
1037 InsertTailList (&mFvHandleList, &KnownHandle->Link);
1038 }
1039
1040 /**
1041 Convert FvHandle and DriverName into an EFI device path
1042
1043 @param Fv Fv protocol, needed to read Depex info out of
1044 FLASH.
1045 @param FvHandle Handle for Fv, needed in the
1046 EFI_SMM_DRIVER_ENTRY so that the PE image can be
1047 read out of the FV at a later time.
1048 @param DriverName Name of driver to add to mDiscoveredList.
1049
1050 @return Pointer to device path constructed from FvHandle and DriverName
1051
1052 **/
1053 EFI_DEVICE_PATH_PROTOCOL *
1054 SmmFvToDevicePath (
1055 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,
1056 IN EFI_HANDLE FvHandle,
1057 IN EFI_GUID *DriverName
1058 )
1059 {
1060 EFI_STATUS Status;
1061 EFI_DEVICE_PATH_PROTOCOL *FvDevicePath;
1062 EFI_DEVICE_PATH_PROTOCOL *FileNameDevicePath;
1063
1064 //
1065 // Remember the device path of the FV
1066 //
1067 Status = gBS->HandleProtocol (FvHandle, &gEfiDevicePathProtocolGuid, (VOID **)&FvDevicePath);
1068 if (EFI_ERROR (Status)) {
1069 FileNameDevicePath = NULL;
1070 } else {
1071 //
1072 // Build a device path to the file in the FV to pass into gBS->LoadImage
1073 //
1074 EfiInitializeFwVolDevicepathNode (&mFvDevicePath.File, DriverName);
1075 SetDevicePathEndNode (&mFvDevicePath.End);
1076
1077 //
1078 // Note: FileNameDevicePath is in DXE memory
1079 //
1080 FileNameDevicePath = AppendDevicePath (
1081 FvDevicePath,
1082 (EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath
1083 );
1084 }
1085 return FileNameDevicePath;
1086 }
1087
1088 /**
1089 Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,
1090 and initilize any state variables. Read the Depex from the FV and store it
1091 in DriverEntry. Pre-process the Depex to set the Before and After state.
1092 The Discovered list is never free'ed and contains booleans that represent the
1093 other possible SMM driver states.
1094
1095 @param Fv Fv protocol, needed to read Depex info out of
1096 FLASH.
1097 @param FvHandle Handle for Fv, needed in the
1098 EFI_SMM_DRIVER_ENTRY so that the PE image can be
1099 read out of the FV at a later time.
1100 @param DriverName Name of driver to add to mDiscoveredList.
1101
1102 @retval EFI_SUCCESS If driver was added to the mDiscoveredList.
1103 @retval EFI_ALREADY_STARTED The driver has already been started. Only one
1104 DriverName may be active in the system at any one
1105 time.
1106
1107 **/
1108 EFI_STATUS
1109 SmmAddToDriverList (
1110 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,
1111 IN EFI_HANDLE FvHandle,
1112 IN EFI_GUID *DriverName
1113 )
1114 {
1115 EFI_SMM_DRIVER_ENTRY *DriverEntry;
1116
1117 //
1118 // Create the Driver Entry for the list. ZeroPool initializes lots of variables to
1119 // NULL or FALSE.
1120 //
1121 DriverEntry = AllocateZeroPool (sizeof (EFI_SMM_DRIVER_ENTRY));
1122 ASSERT (DriverEntry != NULL);
1123
1124 DriverEntry->Signature = EFI_SMM_DRIVER_ENTRY_SIGNATURE;
1125 CopyGuid (&DriverEntry->FileName, DriverName);
1126 DriverEntry->FvHandle = FvHandle;
1127 DriverEntry->Fv = Fv;
1128 DriverEntry->FvFileDevicePath = SmmFvToDevicePath (Fv, FvHandle, DriverName);
1129
1130 SmmGetDepexSectionAndPreProccess (DriverEntry);
1131
1132 InsertTailList (&mDiscoveredList, &DriverEntry->Link);
1133 gRequestDispatch = TRUE;
1134
1135 return EFI_SUCCESS;
1136 }
1137
1138 /**
1139 This function is the main entry point for an SMM handler dispatch
1140 or communicate-based callback.
1141
1142 Event notification that is fired every time a FV dispatch protocol is added.
1143 More than one protocol may have been added when this event is fired, so you
1144 must loop on SmmLocateHandle () to see how many protocols were added and
1145 do the following to each FV:
1146 If the Fv has already been processed, skip it. If the Fv has not been
1147 processed then mark it as being processed, as we are about to process it.
1148 Read the Fv and add any driver in the Fv to the mDiscoveredList.The
1149 mDiscoveredList is never free'ed and contains variables that define
1150 the other states the SMM driver transitions to..
1151 While you are at it read the A Priori file into memory.
1152 Place drivers in the A Priori list onto the mScheduledQueue.
1153
1154 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
1155 @param Context Points to an optional handler context which was specified when the handler was registered.
1156 @param CommBuffer A pointer to a collection of data in memory that will
1157 be conveyed from a non-SMM environment into an SMM environment.
1158 @param CommBufferSize The size of the CommBuffer.
1159
1160 @return Status Code
1161
1162 **/
1163 EFI_STATUS
1164 EFIAPI
1165 SmmDriverDispatchHandler (
1166 IN EFI_HANDLE DispatchHandle,
1167 IN CONST VOID *Context, OPTIONAL
1168 IN OUT VOID *CommBuffer, OPTIONAL
1169 IN OUT UINTN *CommBufferSize OPTIONAL
1170 )
1171 {
1172 EFI_STATUS Status;
1173 UINTN HandleCount;
1174 EFI_HANDLE *HandleBuffer;
1175 EFI_STATUS GetNextFileStatus;
1176 EFI_STATUS SecurityStatus;
1177 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
1178 EFI_DEVICE_PATH_PROTOCOL *FvDevicePath;
1179 EFI_HANDLE FvHandle;
1180 EFI_GUID NameGuid;
1181 UINTN Key;
1182 EFI_FV_FILETYPE Type;
1183 EFI_FV_FILE_ATTRIBUTES Attributes;
1184 UINTN Size;
1185 EFI_SMM_DRIVER_ENTRY *DriverEntry;
1186 EFI_GUID *AprioriFile;
1187 UINTN AprioriEntryCount;
1188 UINTN Index;
1189 LIST_ENTRY *Link;
1190 UINT32 AuthenticationStatus;
1191 UINTN SizeOfBuffer;
1192
1193 HandleBuffer = NULL;
1194 Status = gBS->LocateHandleBuffer (
1195 ByProtocol,
1196 &gEfiFirmwareVolume2ProtocolGuid,
1197 NULL,
1198 &HandleCount,
1199 &HandleBuffer
1200 );
1201 if (EFI_ERROR (Status)) {
1202 return EFI_NOT_FOUND;
1203 }
1204
1205 for (Index = 0; Index < HandleCount; Index++) {
1206 FvHandle = HandleBuffer[Index];
1207
1208 if (FvHasBeenProcessed (FvHandle)) {
1209 //
1210 // This Fv has already been processed so lets skip it!
1211 //
1212 continue;
1213 }
1214
1215 //
1216 // Since we are about to process this Fv mark it as processed.
1217 //
1218 FvIsBeingProcesssed (FvHandle);
1219
1220 Status = gBS->HandleProtocol (FvHandle, &gEfiFirmwareVolume2ProtocolGuid, (VOID **)&Fv);
1221 if (EFI_ERROR (Status)) {
1222 //
1223 // FvHandle must have a Firmware Volume2 Protocol thus we should never get here.
1224 //
1225 ASSERT (FALSE);
1226 continue;
1227 }
1228
1229 Status = gBS->HandleProtocol (FvHandle, &gEfiDevicePathProtocolGuid, (VOID **)&FvDevicePath);
1230 if (EFI_ERROR (Status)) {
1231 //
1232 // The Firmware volume doesn't have device path, can't be dispatched.
1233 //
1234 continue;
1235 }
1236
1237 //
1238 // If the Security Architectural Protocol has not been located yet, then attempt to locate it
1239 //
1240 if (mSecurity == NULL) {
1241 gBS->LocateProtocol (&gEfiSecurityArchProtocolGuid, NULL, (VOID**)&mSecurity);
1242 }
1243
1244 //
1245 // Evaluate the authentication status of the Firmware Volume through
1246 // Security Architectural Protocol
1247 //
1248 if (mSecurity != NULL) {
1249 SecurityStatus = mSecurity->FileAuthenticationState (
1250 mSecurity,
1251 0,
1252 FvDevicePath
1253 );
1254 if (SecurityStatus != EFI_SUCCESS) {
1255 //
1256 // Security check failed. The firmware volume should not be used for any purpose.
1257 //
1258 continue;
1259 }
1260 }
1261
1262 //
1263 // Discover Drivers in FV and add them to the Discovered Driver List.
1264 // Process EFI_FV_FILETYPE_SMM type and then EFI_FV_FILETYPE_COMBINED_SMM_DXE
1265 //
1266 for (Index = 0; Index < sizeof (mSmmFileTypes)/sizeof (EFI_FV_FILETYPE); Index++) {
1267 //
1268 // Initialize the search key
1269 //
1270 Key = 0;
1271 do {
1272 Type = mSmmFileTypes[Index];
1273 GetNextFileStatus = Fv->GetNextFile (
1274 Fv,
1275 &Key,
1276 &Type,
1277 &NameGuid,
1278 &Attributes,
1279 &Size
1280 );
1281 if (!EFI_ERROR (GetNextFileStatus)) {
1282 SmmAddToDriverList (Fv, FvHandle, &NameGuid);
1283 }
1284 } while (!EFI_ERROR (GetNextFileStatus));
1285 }
1286
1287 //
1288 // Read the array of GUIDs from the Apriori file if it is present in the firmware volume
1289 // (Note: AprioriFile is in DXE memory)
1290 //
1291 AprioriFile = NULL;
1292 Status = Fv->ReadSection (
1293 Fv,
1294 &gAprioriGuid,
1295 EFI_SECTION_RAW,
1296 0,
1297 (VOID **)&AprioriFile,
1298 &SizeOfBuffer,
1299 &AuthenticationStatus
1300 );
1301 if (!EFI_ERROR (Status)) {
1302 AprioriEntryCount = SizeOfBuffer / sizeof (EFI_GUID);
1303 } else {
1304 AprioriEntryCount = 0;
1305 }
1306
1307 //
1308 // Put drivers on Apriori List on the Scheduled queue. The Discovered List includes
1309 // drivers not in the current FV and these must be skipped since the a priori list
1310 // is only valid for the FV that it resided in.
1311 //
1312
1313 for (Index = 0; Index < AprioriEntryCount; Index++) {
1314 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
1315 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);
1316 if (CompareGuid (&DriverEntry->FileName, &AprioriFile[Index]) &&
1317 (FvHandle == DriverEntry->FvHandle)) {
1318 DriverEntry->Dependent = FALSE;
1319 DriverEntry->Scheduled = TRUE;
1320 InsertTailList (&mScheduledQueue, &DriverEntry->ScheduledLink);
1321 DEBUG ((DEBUG_DISPATCH, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
1322 DEBUG ((DEBUG_DISPATCH, " RESULT = TRUE (Apriori)\n"));
1323 break;
1324 }
1325 }
1326 }
1327
1328 //
1329 // Free data allocated by Fv->ReadSection ()
1330 //
1331 // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection
1332 // used the UEFI Boot Services AllocatePool() function
1333 //
1334 gBS->FreePool (AprioriFile);
1335 }
1336
1337 //
1338 // Execute the SMM Dispatcher on any newly discovered FVs and previously
1339 // discovered SMM drivers that have been discovered but not dispatched.
1340 //
1341 Status = SmmDispatcher ();
1342
1343 //
1344 // Check to see if CommBuffer and CommBufferSize are valid
1345 //
1346 if (CommBuffer != NULL && CommBufferSize != NULL) {
1347 if (*CommBufferSize > 0) {
1348 if (Status == EFI_NOT_READY) {
1349 //
1350 // If a the SMM Core Entry Point was just registered, then set flag to
1351 // request the SMM Dispatcher to be restarted.
1352 //
1353 *(UINT8 *)CommBuffer = COMM_BUFFER_SMM_DISPATCH_RESTART;
1354 } else if (!EFI_ERROR (Status)) {
1355 //
1356 // Set the flag to show that the SMM Dispatcher executed without errors
1357 //
1358 *(UINT8 *)CommBuffer = COMM_BUFFER_SMM_DISPATCH_SUCCESS;
1359 } else {
1360 //
1361 // Set the flag to show that the SMM Dispatcher encountered an error
1362 //
1363 *(UINT8 *)CommBuffer = COMM_BUFFER_SMM_DISPATCH_ERROR;
1364 }
1365 }
1366 }
1367
1368 return EFI_SUCCESS;
1369 }
1370
1371 /**
1372 Traverse the discovered list for any drivers that were discovered but not loaded
1373 because the dependency experessions evaluated to false.
1374
1375 **/
1376 VOID
1377 SmmDisplayDiscoveredNotDispatched (
1378 VOID
1379 )
1380 {
1381 LIST_ENTRY *Link;
1382 EFI_SMM_DRIVER_ENTRY *DriverEntry;
1383
1384 for (Link = mDiscoveredList.ForwardLink;Link !=&mDiscoveredList; Link = Link->ForwardLink) {
1385 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);
1386 if (DriverEntry->Dependent) {
1387 DEBUG ((DEBUG_LOAD, "SMM Driver %g was discovered but not loaded!!\n", &DriverEntry->FileName));
1388 }
1389 }
1390 }