]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Dispatcher/Dispatcher.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Dispatcher / Dispatcher.c
index 3de4edf00ce78a6dd913bb3735ad0c0a0099adad..fb9ae3ce78dacacead90ed30f2fcc3c527f26670 100644 (file)
@@ -1,56 +1,43 @@
-/*++\r
-\r
-Copyright (c) 2006, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
-are licensed and made available under the terms and conditions of the BSD License         \r
-which accompanies this distribution.  The full text of the license may be found at        \r
-http://opensource.org/licenses/bsd-license.php                                            \r
-                                                                                          \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
-\r
-Module Name:\r
-\r
-  Dispatcher.c\r
-\r
-Abstract:\r
-\r
-  Tiano DXE Dispatcher.\r
+/** @file\r
+  DXE Dispatcher.\r
 \r
   Step #1 - When a FV protocol is added to the system every driver in the FV\r
-            is added to the mDiscoveredList. The SOR, Before, and After Depex are \r
-            pre-processed as drivers are added to the mDiscoveredList. If an Apriori \r
-            file exists in the FV those drivers are addeded to the \r
-            mScheduledQueue. The mFvHandleList is used to make sure a \r
+            is added to the mDiscoveredList. The SOR, Before, and After Depex are\r
+            pre-processed as drivers are added to the mDiscoveredList. If an Apriori\r
+            file exists in the FV those drivers are addeded to the\r
+            mScheduledQueue. The mFvHandleList is used to make sure a\r
             FV is only processed once.\r
 \r
   Step #2 - Dispatch. Remove driver from the mScheduledQueue and load and\r
-            start it. After mScheduledQueue is drained check the \r
-            mDiscoveredList to see if any item has a Depex that is ready to \r
+            start it. After mScheduledQueue is drained check the\r
+            mDiscoveredList to see if any item has a Depex that is ready to\r
             be placed on the mScheduledQueue.\r
 \r
-  Step #3 - Adding to the mScheduledQueue requires that you process Before \r
+  Step #3 - Adding to the mScheduledQueue requires that you process Before\r
             and After dependencies. This is done recursively as the call to add\r
-            to the mScheduledQueue checks for Before and recursively adds \r
-            all Befores. It then addes the item that was passed in and then \r
+            to the mScheduledQueue checks for Before and recursively adds\r
+            all Befores. It then addes the item that was passed in and then\r
             processess the After dependecies by recursively calling the routine.\r
 \r
   Dispatcher Rules:\r
-  The rules for the dispatcher are in chapter 10 of the DXE CIS. Figure 10-3 \r
+  The rules for the dispatcher are in chapter 10 of the DXE CIS. Figure 10-3\r
   is the state diagram for the DXE dispatcher\r
 \r
   Depex - Dependency Expresion.\r
   SOR   - Schedule On Request - Don't schedule if this bit is set.\r
 \r
---*/\r
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
 \r
-#include <DxeMain.h>\r
+#include "DxeMain.h"\r
 \r
 //\r
 // The Driver List contains one copy of every driver that has been discovered.\r
 // Items are never removed from the driver list. List of EFI_CORE_DRIVER_ENTRY\r
 //\r
-LIST_ENTRY  mDiscoveredList = INITIALIZE_LIST_HEAD_VARIABLE (mDiscoveredList);  \r
+LIST_ENTRY  mDiscoveredList = INITIALIZE_LIST_HEAD_VARIABLE (mDiscoveredList);\r
 \r
 //\r
 // Queue of drivers that are ready to dispatch. This queue is a subset of the\r
@@ -68,7 +55,6 @@ LIST_ENTRY  mFvHandleList = INITIALIZE_LIST_HEAD_VARIABLE (mFvHandleList);
 //\r
 EFI_LOCK  mDispatcherLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_HIGH_LEVEL);\r
 \r
-\r
 //\r
 // Flag for the DXE Dispacher.  TRUE if dispatcher is execuing.\r
 //\r
@@ -77,165 +63,199 @@ BOOLEAN  gDispatcherRunning = FALSE;
 //\r
 // Module globals to manage the FwVol registration notification event\r
 //\r
-EFI_EVENT       mFwVolEvent;\r
-VOID            *mFwVolEventRegistration;\r
+EFI_EVENT  mFwVolEvent;\r
+VOID       *mFwVolEventRegistration;\r
 \r
 //\r
 // List of file types supported by dispatcher\r
 //\r
-static EFI_FV_FILETYPE mDxeFileTypes[] = { \r
-  EFI_FV_FILETYPE_DRIVER, \r
-  EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER, \r
+EFI_FV_FILETYPE  mDxeFileTypes[] = {\r
+  EFI_FV_FILETYPE_DRIVER,\r
+  EFI_FV_FILETYPE_COMBINED_SMM_DXE,\r
+  EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER,\r
   EFI_FV_FILETYPE_DXE_CORE,\r
   EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE\r
 };\r
 \r
 typedef struct {\r
-  MEDIA_FW_VOL_FILEPATH_DEVICE_PATH   File;\r
-  EFI_DEVICE_PATH_PROTOCOL            End;\r
+  MEDIA_FW_VOL_FILEPATH_DEVICE_PATH    File;\r
+  EFI_DEVICE_PATH_PROTOCOL             End;\r
 } FV_FILEPATH_DEVICE_PATH;\r
 \r
-FV_FILEPATH_DEVICE_PATH mFvDevicePath;\r
-\r
+FV_FILEPATH_DEVICE_PATH  mFvDevicePath;\r
 \r
 //\r
 // Function Prototypes\r
 //\r
-STATIC\r
+\r
+/**\r
+  Insert InsertedDriverEntry onto the mScheduledQueue. To do this you\r
+  must add any driver with a before dependency on InsertedDriverEntry first.\r
+  You do this by recursively calling this routine. After all the Befores are\r
+  processed you can add InsertedDriverEntry to the mScheduledQueue.\r
+  Then you can add any driver with an After dependency on InsertedDriverEntry\r
+  by recursively calling this routine.\r
+\r
+  @param  InsertedDriverEntry   The driver to insert on the ScheduledLink Queue\r
+\r
+**/\r
 VOID\r
 CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (\r
-  IN  EFI_CORE_DRIVER_ENTRY   *InsertedDriverEntry\r
+  IN  EFI_CORE_DRIVER_ENTRY  *InsertedDriverEntry\r
   );\r
\r
-STATIC\r
+\r
+/**\r
+  Event notification that is fired every time a FV dispatch protocol is added.\r
+  More than one protocol may have been added when this event is fired, so you\r
+  must loop on CoreLocateHandle () to see how many protocols were added and\r
+  do the following to each FV:\r
+  If the Fv has already been processed, skip it. If the Fv has not been\r
+  processed then mark it as being processed, as we are about to process it.\r
+  Read the Fv and add any driver in the Fv to the mDiscoveredList.The\r
+  mDiscoveredList is never free'ed and contains variables that define\r
+  the other states the DXE driver transitions to..\r
+  While you are at it read the A Priori file into memory.\r
+  Place drivers in the A Priori list onto the mScheduledQueue.\r
+\r
+  @param  Event                 The Event that is being processed, not used.\r
+  @param  Context               Event Context, not used.\r
+\r
+**/\r
 VOID\r
 EFIAPI\r
 CoreFwVolEventProtocolNotify (\r
-  IN  EFI_EVENT       Event,\r
-  IN  VOID            *Context\r
+  IN  EFI_EVENT  Event,\r
+  IN  VOID       *Context\r
   );\r
 \r
-STATIC\r
+/**\r
+  Convert FvHandle and DriverName into an EFI device path\r
+\r
+  @param  Fv                    Fv protocol, needed to read Depex info out of\r
+                                FLASH.\r
+  @param  FvHandle              Handle for Fv, needed in the\r
+                                EFI_CORE_DRIVER_ENTRY so that the PE image can be\r
+                                read out of the FV at a later time.\r
+  @param  DriverName            Name of driver to add to mDiscoveredList.\r
+\r
+  @return Pointer to device path constructed from FvHandle and DriverName\r
+\r
+**/\r
 EFI_DEVICE_PATH_PROTOCOL *\r
 CoreFvToDevicePath (\r
-  IN  EFI_FIRMWARE_VOLUME_PROTOCOL    *Fv,\r
-  IN  EFI_HANDLE                      FvHandle,\r
-  IN  EFI_GUID                        *DriverName\r
+  IN  EFI_FIRMWARE_VOLUME2_PROTOCOL  *Fv,\r
+  IN  EFI_HANDLE                     FvHandle,\r
+  IN  EFI_GUID                       *DriverName\r
   );\r
 \r
-STATIC \r
+/**\r
+  Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,\r
+  and initilize any state variables. Read the Depex from the FV and store it\r
+  in DriverEntry. Pre-process the Depex to set the SOR, Before and After state.\r
+  The Discovered list is never free'ed and contains booleans that represent the\r
+  other possible DXE driver states.\r
+\r
+  @param  Fv                    Fv protocol, needed to read Depex info out of\r
+                                FLASH.\r
+  @param  FvHandle              Handle for Fv, needed in the\r
+                                EFI_CORE_DRIVER_ENTRY so that the PE image can be\r
+                                read out of the FV at a later time.\r
+  @param  DriverName            Name of driver to add to mDiscoveredList.\r
+  @param  Type                  Fv File Type of file to add to mDiscoveredList.\r
+\r
+  @retval EFI_SUCCESS           If driver was added to the mDiscoveredList.\r
+  @retval EFI_ALREADY_STARTED   The driver has already been started. Only one\r
+                                DriverName may be active in the system at any one\r
+                                time.\r
+\r
+**/\r
 EFI_STATUS\r
 CoreAddToDriverList (\r
-  IN  EFI_FIRMWARE_VOLUME_PROTOCOL  *Fv,\r
-  IN  EFI_HANDLE                    FvHandle,\r
-  IN  EFI_GUID                      *DriverName\r
+  IN  EFI_FIRMWARE_VOLUME2_PROTOCOL  *Fv,\r
+  IN  EFI_HANDLE                     FvHandle,\r
+  IN  EFI_GUID                       *DriverName,\r
+  IN  EFI_FV_FILETYPE                Type\r
   );\r
 \r
-STATIC\r
-EFI_STATUS \r
+/**\r
+  Get Fv image(s) from the FV through file name, and produce FVB protocol for every Fv image(s).\r
+\r
+  @param  Fv                    The FIRMWARE_VOLUME protocol installed on the FV.\r
+  @param  FvHandle              The handle which FVB protocol installed on.\r
+  @param  FileName              The file name guid specified.\r
+\r
+  @retval EFI_OUT_OF_RESOURCES  No enough memory or other resource.\r
+  @retval EFI_SUCCESS           Function successfully returned.\r
+\r
+**/\r
+EFI_STATUS\r
 CoreProcessFvImageFile (\r
-  IN  EFI_FIRMWARE_VOLUME_PROTOCOL    *Fv,\r
-  IN  EFI_HANDLE                      FvHandle,\r
-  IN  EFI_GUID                        *DriverName\r
+  IN  EFI_FIRMWARE_VOLUME2_PROTOCOL  *Fv,\r
+  IN  EFI_HANDLE                     FvHandle,\r
+  IN  EFI_GUID                       *FileName\r
   );\r
 \r
-STATIC\r
+/**\r
+  Enter critical section by gaining lock on mDispatcherLock.\r
+\r
+**/\r
 VOID\r
 CoreAcquireDispatcherLock (\r
   VOID\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Enter critical section by gaining lock on mDispatcherLock\r
-\r
-Arguments:\r
-\r
-  None\r
-\r
-Returns:\r
-\r
-  None\r
-\r
---*/\r
-\r
 {\r
   CoreAcquireLock (&mDispatcherLock);\r
 }\r
 \r
-STATIC\r
+/**\r
+  Exit critical section by releasing lock on mDispatcherLock.\r
+\r
+**/\r
 VOID\r
 CoreReleaseDispatcherLock (\r
   VOID\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Exit critical section by releasing lock on mDispatcherLock\r
-\r
-Arguments:\r
-\r
-  None\r
-\r
-Returns:\r
-\r
-  None\r
-\r
---*/\r
 {\r
   CoreReleaseLock (&mDispatcherLock);\r
 }\r
 \r
-STATIC\r
-EFI_STATUS\r
-CoreGetDepexSectionAndPreProccess (\r
-  IN  EFI_CORE_DRIVER_ENTRY   *DriverEntry\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
+/**\r
   Read Depex and pre-process the Depex for Before and After. If Section Extraction\r
   protocol returns an error via ReadSection defer the reading of the Depex.\r
 \r
-Arguments:\r
-\r
-  DriverEntry - Driver to work on.\r
-  \r
-Returns:\r
-\r
-  EFI_SUCCESS - Depex read and preprossesed \r
-\r
-  EFI_PROTOCOL_ERROR - The section extraction protocol returned an error and \r
-                        Depex reading needs to be retried.\r
+  @param  DriverEntry           Driver to work on.\r
 \r
-  Other Error - DEPEX not found.\r
+  @retval EFI_SUCCESS           Depex read and preprossesed\r
+  @retval EFI_PROTOCOL_ERROR    The section extraction protocol returned an error\r
+                                and  Depex reading needs to be retried.\r
+  @retval Error                 DEPEX not found.\r
 \r
---*/\r
+**/\r
+EFI_STATUS\r
+CoreGetDepexSectionAndPreProccess (\r
+  IN  EFI_CORE_DRIVER_ENTRY  *DriverEntry\r
+  )\r
 {\r
-  EFI_STATUS                    Status;\r
-  EFI_SECTION_TYPE              SectionType;\r
-  UINT32                        AuthenticationStatus;\r
-  EFI_FIRMWARE_VOLUME_PROTOCOL  *Fv;\r
+  EFI_STATUS                     Status;\r
+  EFI_SECTION_TYPE               SectionType;\r
+  UINT32                         AuthenticationStatus;\r
+  EFI_FIRMWARE_VOLUME2_PROTOCOL  *Fv;\r
 \r
-  \r
   Fv = DriverEntry->Fv;\r
 \r
   //\r
   // Grab Depex info, it will never be free'ed.\r
   //\r
-  SectionType         = EFI_SECTION_DXE_DEPEX;\r
-  Status = Fv->ReadSection (\r
-                DriverEntry->Fv, \r
-                &DriverEntry->FileName,\r
-                SectionType, \r
-                0, \r
-                &DriverEntry->Depex, \r
-                (UINTN *)&DriverEntry->DepexSize,\r
-                &AuthenticationStatus\r
-                );\r
+  SectionType = EFI_SECTION_DXE_DEPEX;\r
+  Status      = Fv->ReadSection (\r
+                      DriverEntry->Fv,\r
+                      &DriverEntry->FileName,\r
+                      SectionType,\r
+                      0,\r
+                      &DriverEntry->Depex,\r
+                      (UINTN *)&DriverEntry->DepexSize,\r
+                      &AuthenticationStatus\r
+                      );\r
   if (EFI_ERROR (Status)) {\r
     if (Status == EFI_PROTOCOL_ERROR) {\r
       //\r
@@ -244,10 +264,10 @@ Returns:
       DriverEntry->DepexProtocolError = TRUE;\r
     } else {\r
       //\r
-      // If no Depex assume EFI 1.1 driver model\r
+      // If no Depex assume UEFI 2.0 driver model\r
       //\r
-      DriverEntry->Depex = NULL;\r
-      DriverEntry->Dependent = TRUE;\r
+      DriverEntry->Depex              = NULL;\r
+      DriverEntry->Dependent          = TRUE;\r
       DriverEntry->DepexProtocolError = FALSE;\r
     }\r
   } else {\r
@@ -257,106 +277,94 @@ Returns:
     //\r
     CorePreProcessDepex (DriverEntry);\r
     DriverEntry->DepexProtocolError = FALSE;\r
-  }  \r
+  }\r
 \r
   return Status;\r
 }\r
 \r
-EFI_DXESERVICE\r
+/**\r
+  Check every driver and locate a matching one. If the driver is found, the Unrequested\r
+  state flag is cleared.\r
+\r
+  @param  FirmwareVolumeHandle  The handle of the Firmware Volume that contains\r
+                                the firmware  file specified by DriverName.\r
+  @param  DriverName            The Driver name to put in the Dependent state.\r
+\r
+  @retval EFI_SUCCESS           The DriverName was found and it's SOR bit was\r
+                                cleared\r
+  @retval EFI_NOT_FOUND         The DriverName does not exist or it's SOR bit was\r
+                                not set.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 CoreSchedule (\r
   IN  EFI_HANDLE  FirmwareVolumeHandle,\r
   IN  EFI_GUID    *DriverName\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Check every driver and locate a matching one. If the driver is found, the Unrequested\r
-  state flag is cleared.\r
-\r
-Arguments:\r
-\r
-  FirmwareVolumeHandle - The handle of the Firmware Volume that contains the firmware \r
-                         file specified by DriverName.\r
-\r
-  DriverName           - The Driver name to put in the Dependent state.\r
-\r
-Returns:\r
-\r
-  EFI_SUCCESS   - The DriverName was found and it's SOR bit was cleared\r
-\r
-  EFI_NOT_FOUND - The DriverName does not exist or it's SOR bit was not set.\r
-\r
---*/\r
 {\r
-  LIST_ENTRY            *Link;\r
-  EFI_CORE_DRIVER_ENTRY *DriverEntry;\r
+  LIST_ENTRY             *Link;\r
+  EFI_CORE_DRIVER_ENTRY  *DriverEntry;\r
 \r
   //\r
   // Check every driver\r
   //\r
   for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
-    DriverEntry = CR(Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);\r
-    if (DriverEntry->FvHandle == FirmwareVolumeHandle &&\r
-        DriverEntry->Unrequested && \r
-        CompareGuid (DriverName, &DriverEntry->FileName)) {\r
+    DriverEntry = CR (Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);\r
+    if ((DriverEntry->FvHandle == FirmwareVolumeHandle) &&\r
+        DriverEntry->Unrequested &&\r
+        CompareGuid (DriverName, &DriverEntry->FileName))\r
+    {\r
       //\r
       // Move the driver from the Unrequested to the Dependent state\r
       //\r
       CoreAcquireDispatcherLock ();\r
-      DriverEntry->Unrequested  = FALSE;\r
-      DriverEntry->Dependent    = TRUE;\r
+      DriverEntry->Unrequested = FALSE;\r
+      DriverEntry->Dependent   = TRUE;\r
       CoreReleaseDispatcherLock ();\r
-    \r
+\r
+      DEBUG ((DEBUG_DISPATCH, "Schedule FFS(%g) - EFI_SUCCESS\n", DriverName));\r
+\r
       return EFI_SUCCESS;\r
     }\r
   }\r
-  return EFI_NOT_FOUND;  \r
+\r
+  DEBUG ((DEBUG_DISPATCH, "Schedule FFS(%g) - EFI_NOT_FOUND\n", DriverName));\r
+\r
+  return EFI_NOT_FOUND;\r
 }\r
 \r
+/**\r
+  Convert a driver from the Untrused back to the Scheduled state.\r
+\r
+  @param  FirmwareVolumeHandle  The handle of the Firmware Volume that contains\r
+                                the firmware  file specified by DriverName.\r
+  @param  DriverName            The Driver name to put in the Scheduled state\r
+\r
+  @retval EFI_SUCCESS           The file was found in the untrusted state, and it\r
+                                was promoted  to the trusted state.\r
+  @retval EFI_NOT_FOUND         The file was not found in the untrusted state.\r
 \r
-EFI_DXESERVICE\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 CoreTrust (\r
   IN  EFI_HANDLE  FirmwareVolumeHandle,\r
   IN  EFI_GUID    *DriverName\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Convert a driver from the Untrused back to the Scheduled state\r
-\r
-Arguments:\r
-\r
-  FirmwareVolumeHandle - The handle of the Firmware Volume that contains the firmware \r
-                         file specified by DriverName.\r
-\r
-  DriverName           - The Driver name to put in the Scheduled state\r
-\r
-Returns:\r
-\r
-  EFI_SUCCESS   - The file was found in the untrusted state, and it was promoted \r
-                  to the trusted state.\r
-\r
-  EFI_NOT_FOUND - The file was not found in the untrusted state.\r
-\r
---*/\r
 {\r
-  LIST_ENTRY            *Link;\r
-  EFI_CORE_DRIVER_ENTRY *DriverEntry;\r
+  LIST_ENTRY             *Link;\r
+  EFI_CORE_DRIVER_ENTRY  *DriverEntry;\r
 \r
   //\r
   // Check every driver\r
   //\r
   for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
-    DriverEntry = CR(Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);\r
-    if (DriverEntry->FvHandle == FirmwareVolumeHandle &&\r
-        DriverEntry->Untrusted && \r
-        CompareGuid (DriverName, &DriverEntry->FileName)) {\r
+    DriverEntry = CR (Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);\r
+    if ((DriverEntry->FvHandle == FirmwareVolumeHandle) &&\r
+        DriverEntry->Untrusted &&\r
+        CompareGuid (DriverName, &DriverEntry->FileName))\r
+    {\r
       //\r
       // Transition driver from Untrusted to Scheduled state.\r
       //\r
@@ -365,51 +373,42 @@ Returns:
       DriverEntry->Scheduled = TRUE;\r
       InsertTailList (&mScheduledQueue, &DriverEntry->ScheduledLink);\r
       CoreReleaseDispatcherLock ();\r
-    \r
+\r
       return EFI_SUCCESS;\r
     }\r
   }\r
-  return EFI_NOT_FOUND;  \r
-}\r
 \r
+  return EFI_NOT_FOUND;\r
+}\r
 \r
-EFI_DXESERVICE\r
-EFI_STATUS\r
-EFIAPI\r
-CoreDispatcher (\r
-  VOID\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  This is the main Dispatcher for DXE and it exits when there are no more \r
+/**\r
+  This is the main Dispatcher for DXE and it exits when there are no more\r
   drivers to run. Drain the mScheduledQueue and load and start a PE\r
-  image for each driver. Search the mDiscoveredList to see if any driver can \r
+  image for each driver. Search the mDiscoveredList to see if any driver can\r
   be placed on the mScheduledQueue. If no drivers are placed on the\r
   mScheduledQueue exit the function. On exit it is assumed the Bds()\r
-  will be called, and when the Bds() exits the Dispatcher will be called \r
+  will be called, and when the Bds() exits the Dispatcher will be called\r
   again.\r
 \r
-Arguments:\r
-\r
-  NONE\r
-\r
-Returns:\r
-\r
-  EFI_ALREADY_STARTED - The DXE Dispatcher is already running\r
+  @retval EFI_ALREADY_STARTED   The DXE Dispatcher is already running\r
+  @retval EFI_NOT_FOUND         No DXE Drivers were dispatched\r
+  @retval EFI_SUCCESS           One or more DXE Drivers were dispatched\r
 \r
-  EFI_NOT_FOUND       - No DXE Drivers were dispatched\r
-\r
-  EFI_SUCCESS         - One or more DXE Drivers were dispatched\r
-\r
---*/\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+CoreDispatcher (\r
+  VOID\r
+  )\r
 {\r
-  EFI_STATUS                      Status;\r
-  EFI_STATUS                      ReturnStatus;\r
-  LIST_ENTRY                      *Link;\r
-  EFI_CORE_DRIVER_ENTRY           *DriverEntry;\r
-  BOOLEAN                         ReadyToRun;\r
+  EFI_STATUS             Status;\r
+  EFI_STATUS             ReturnStatus;\r
+  LIST_ENTRY             *Link;\r
+  EFI_CORE_DRIVER_ENTRY  *DriverEntry;\r
+  BOOLEAN                ReadyToRun;\r
+  EFI_EVENT              DxeDispatchEvent;\r
+\r
+  PERF_FUNCTION_BEGIN ();\r
 \r
   if (gDispatcherRunning) {\r
     //\r
@@ -420,6 +419,17 @@ Returns:
 \r
   gDispatcherRunning = TRUE;\r
 \r
+  Status = CoreCreateEventEx (\r
+             EVT_NOTIFY_SIGNAL,\r
+             TPL_NOTIFY,\r
+             EfiEventEmptyFunction,\r
+             NULL,\r
+             &gEfiEventDxeDispatchGuid,\r
+             &DxeDispatchEvent\r
+             );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
 \r
   ReturnStatus = EFI_NOT_FOUND;\r
   do {\r
@@ -439,15 +449,16 @@ Returns:
       // Untrused to Scheduled it would have already been loaded so we may need to\r
       // skip the LoadImage\r
       //\r
-      if (DriverEntry->ImageHandle == NULL) {\r
+      if ((DriverEntry->ImageHandle == NULL) && !DriverEntry->IsFvImage) {\r
+        DEBUG ((DEBUG_INFO, "Loading driver %g\n", &DriverEntry->FileName));\r
         Status = CoreLoadImage (\r
-                        FALSE, \r
-                        gDxeCoreImageHandle, \r
-                        DriverEntry->FvFileDevicePath,\r
-                        NULL, \r
-                        0, \r
-                        &DriverEntry->ImageHandle\r
-                        );\r
+                   FALSE,\r
+                   gDxeCoreImageHandle,\r
+                   DriverEntry->FvFileDevicePath,\r
+                   NULL,\r
+                   0,\r
+                   &DriverEntry->ImageHandle\r
+                   );\r
 \r
         //\r
         // Update the driver state to reflect that it's been loaded\r
@@ -463,18 +474,18 @@ Returns:
           } else {\r
             //\r
             // The DXE Driver could not be loaded, and do not attempt to load or start it again.\r
-            // Take driver from Scheduled to Initialized. \r
+            // Take driver from Scheduled to Initialized.\r
             //\r
             // This case include the Never Trusted state if EFI_ACCESS_DENIED is returned\r
             //\r
-            DriverEntry->Initialized  = TRUE;\r
+            DriverEntry->Initialized = TRUE;\r
           }\r
 \r
           DriverEntry->Scheduled = FALSE;\r
           RemoveEntryList (&DriverEntry->ScheduledLink);\r
-          \r
+\r
           CoreReleaseDispatcherLock ();\r
-        \r
+\r
           //\r
           // If it's an error don't try the StartImage\r
           //\r
@@ -484,20 +495,48 @@ Returns:
 \r
       CoreAcquireDispatcherLock ();\r
 \r
-      DriverEntry->Scheduled    = FALSE;\r
-      DriverEntry->Initialized  = TRUE;\r
+      DriverEntry->Scheduled   = FALSE;\r
+      DriverEntry->Initialized = TRUE;\r
       RemoveEntryList (&DriverEntry->ScheduledLink);\r
-      \r
-      CoreReleaseDispatcherLock ();\r
 \r
+      CoreReleaseDispatcherLock ();\r
 \r
-      CoreReportProgressCodeSpecific (EFI_SOFTWARE_DXE_CORE | EFI_SW_PC_INIT_BEGIN, DriverEntry->ImageHandle);\r
-      Status = CoreStartImage (DriverEntry->ImageHandle, NULL, NULL);\r
-      CoreReportProgressCodeSpecific (EFI_SOFTWARE_DXE_CORE | EFI_SW_PC_INIT_END, DriverEntry->ImageHandle);\r
+      if (DriverEntry->IsFvImage) {\r
+        //\r
+        // Produce a firmware volume block protocol for FvImage so it gets dispatched from.\r
+        //\r
+        Status = CoreProcessFvImageFile (DriverEntry->Fv, DriverEntry->FvHandle, &DriverEntry->FileName);\r
+      } else {\r
+        REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
+          EFI_PROGRESS_CODE,\r
+          (EFI_SOFTWARE_DXE_CORE | EFI_SW_PC_INIT_BEGIN),\r
+          &DriverEntry->ImageHandle,\r
+          sizeof (DriverEntry->ImageHandle)\r
+          );\r
+        ASSERT (DriverEntry->ImageHandle != NULL);\r
+\r
+        Status = CoreStartImage (DriverEntry->ImageHandle, NULL, NULL);\r
+\r
+        REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
+          EFI_PROGRESS_CODE,\r
+          (EFI_SOFTWARE_DXE_CORE | EFI_SW_PC_INIT_END),\r
+          &DriverEntry->ImageHandle,\r
+          sizeof (DriverEntry->ImageHandle)\r
+          );\r
+      }\r
 \r
       ReturnStatus = EFI_SUCCESS;\r
     }\r
 \r
+    //\r
+    // Now DXE Dispatcher finished one round of dispatch, signal an event group\r
+    // so that SMM Dispatcher get chance to dispatch SMM Drivers which depend\r
+    // on UEFI protocols\r
+    //\r
+    if (!EFI_ERROR (ReturnStatus)) {\r
+      CoreSignalEvent (DxeDispatchEvent);\r
+    }\r
+\r
     //\r
     // Search DriverList for items to place on Scheduled Queue\r
     //\r
@@ -505,67 +544,75 @@ Returns:
     for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
       DriverEntry = CR (Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);\r
 \r
-      if (DriverEntry->DepexProtocolError){\r
+      if (DriverEntry->DepexProtocolError) {\r
         //\r
         // If Section Extraction Protocol did not let the Depex be read before retry the read\r
         //\r
         Status = CoreGetDepexSectionAndPreProccess (DriverEntry);\r
-      } \r
+      }\r
 \r
       if (DriverEntry->Dependent) {\r
         if (CoreIsSchedulable (DriverEntry)) {\r
-          CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry); \r
+          CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);\r
           ReadyToRun = TRUE;\r
-        } \r
+        }\r
+      } else {\r
+        if (DriverEntry->Unrequested) {\r
+          DEBUG ((DEBUG_DISPATCH, "Evaluate DXE DEPEX for FFS(%g)\n", &DriverEntry->FileName));\r
+          DEBUG ((DEBUG_DISPATCH, "  SOR                                             = Not Requested\n"));\r
+          DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE\n"));\r
+        }\r
       }\r
     }\r
   } while (ReadyToRun);\r
 \r
+  //\r
+  // Close DXE dispatch Event\r
+  //\r
+  CoreCloseEvent (DxeDispatchEvent);\r
+\r
   gDispatcherRunning = FALSE;\r
 \r
+  PERF_FUNCTION_END ();\r
+\r
   return ReturnStatus;\r
 }\r
 \r
-STATIC\r
-VOID\r
-CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (\r
-  IN  EFI_CORE_DRIVER_ENTRY   *InsertedDriverEntry\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Insert InsertedDriverEntry onto the mScheduledQueue. To do this you \r
+/**\r
+  Insert InsertedDriverEntry onto the mScheduledQueue. To do this you\r
   must add any driver with a before dependency on InsertedDriverEntry first.\r
   You do this by recursively calling this routine. After all the Befores are\r
-  processed you can add InsertedDriverEntry to the mScheduledQueue. \r
-  Then you can add any driver with an After dependency on InsertedDriverEntry \r
+  processed you can add InsertedDriverEntry to the mScheduledQueue.\r
+  Then you can add any driver with an After dependency on InsertedDriverEntry\r
   by recursively calling this routine.\r
 \r
-Arguments:\r
-\r
-  InsertedDriverEntry - The driver to insert on the ScheduledLink Queue\r
+  @param  InsertedDriverEntry   The driver to insert on the ScheduledLink Queue\r
 \r
-Returns:\r
-\r
-  NONE \r
-\r
---*/\r
+**/\r
+VOID\r
+CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (\r
+  IN  EFI_CORE_DRIVER_ENTRY  *InsertedDriverEntry\r
+  )\r
 {\r
-  LIST_ENTRY            *Link;\r
-  EFI_CORE_DRIVER_ENTRY *DriverEntry;\r
+  LIST_ENTRY             *Link;\r
+  EFI_CORE_DRIVER_ENTRY  *DriverEntry;\r
 \r
   //\r
   // Process Before Dependency\r
   //\r
   for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
-    DriverEntry = CR(Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);\r
-    if (DriverEntry->Before && DriverEntry->Dependent) {\r
+    DriverEntry = CR (Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);\r
+    if (DriverEntry->Before && DriverEntry->Dependent && (DriverEntry != InsertedDriverEntry)) {\r
+      DEBUG ((DEBUG_DISPATCH, "Evaluate DXE DEPEX for FFS(%g)\n", &DriverEntry->FileName));\r
+      DEBUG ((DEBUG_DISPATCH, "  BEFORE FFS(%g) = ", &DriverEntry->BeforeAfterGuid));\r
       if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {\r
         //\r
         // Recursively process BEFORE\r
         //\r
+        DEBUG ((DEBUG_DISPATCH, "TRUE\n  END\n  RESULT = TRUE\n"));\r
         CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);\r
+      } else {\r
+        DEBUG ((DEBUG_DISPATCH, "FALSE\n  END\n  RESULT = FALSE\n"));\r
       }\r
     }\r
   }\r
@@ -578,126 +625,189 @@ Returns:
   InsertedDriverEntry->Dependent = FALSE;\r
   InsertedDriverEntry->Scheduled = TRUE;\r
   InsertTailList (&mScheduledQueue, &InsertedDriverEntry->ScheduledLink);\r
-  \r
+\r
   CoreReleaseDispatcherLock ();\r
 \r
   //\r
   // Process After Dependency\r
   //\r
   for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
-    DriverEntry = CR(Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);\r
-    if (DriverEntry->After && DriverEntry->Dependent) {\r
+    DriverEntry = CR (Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);\r
+    if (DriverEntry->After && DriverEntry->Dependent && (DriverEntry != InsertedDriverEntry)) {\r
+      DEBUG ((DEBUG_DISPATCH, "Evaluate DXE DEPEX for FFS(%g)\n", &DriverEntry->FileName));\r
+      DEBUG ((DEBUG_DISPATCH, "  AFTER FFS(%g) = ", &DriverEntry->BeforeAfterGuid));\r
       if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {\r
         //\r
         // Recursively process AFTER\r
         //\r
+        DEBUG ((DEBUG_DISPATCH, "TRUE\n  END\n  RESULT = TRUE\n"));\r
         CoreInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);\r
+      } else {\r
+        DEBUG ((DEBUG_DISPATCH, "FALSE\n  END\n  RESULT = FALSE\n"));\r
       }\r
     }\r
   }\r
 }\r
 \r
-STATIC\r
-BOOLEAN\r
-FvHasBeenProcessed (\r
-  IN  EFI_HANDLE      FvHandle\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
+/**\r
   Return TRUE if the Fv has been processed, FALSE if not.\r
 \r
-Arguments:\r
-\r
-  FvHandle - The handle of a FV that's being tested\r
-\r
-Returns:\r
+  @param  FvHandle              The handle of a FV that's being tested\r
 \r
-  TRUE  - Fv protocol on FvHandle has been processed\r
+  @retval TRUE                  Fv protocol on FvHandle has been processed\r
+  @retval FALSE                 Fv protocol on FvHandle has not yet been processed\r
 \r
-  FALSE - Fv protocol on FvHandle has not yet been processed\r
-\r
---*/\r
+**/\r
+BOOLEAN\r
+FvHasBeenProcessed (\r
+  IN  EFI_HANDLE  FvHandle\r
+  )\r
 {\r
-  LIST_ENTRY      *Link;\r
-  KNOWN_HANDLE    *KnownHandle;\r
+  LIST_ENTRY    *Link;\r
+  KNOWN_HANDLE  *KnownHandle;\r
 \r
   for (Link = mFvHandleList.ForwardLink; Link != &mFvHandleList; Link = Link->ForwardLink) {\r
-    KnownHandle = CR(Link, KNOWN_HANDLE, Link, KNOWN_HANDLE_SIGNATURE);\r
+    KnownHandle = CR (Link, KNOWN_HANDLE, Link, KNOWN_HANDLE_SIGNATURE);\r
     if (KnownHandle->Handle == FvHandle) {\r
       return TRUE;\r
     }\r
   }\r
+\r
   return FALSE;\r
 }\r
 \r
-STATIC\r
-VOID\r
-FvIsBeingProcesssed (\r
-  IN  EFI_HANDLE    FvHandle\r
+/**\r
+  Remember that Fv protocol on FvHandle has had it's drivers placed on the\r
+  mDiscoveredList. This fucntion adds entries on the mFvHandleList if new\r
+  entry is different from one in mFvHandleList by checking FvImage Guid.\r
+  Items are never removed/freed from the mFvHandleList.\r
+\r
+  @param  FvHandle              The handle of a FV that has been processed\r
+\r
+  @return A point to new added FvHandle entry. If FvHandle with the same FvImage guid\r
+          has been added, NULL will return.\r
+\r
+**/\r
+KNOWN_HANDLE *\r
+FvIsBeingProcessed (\r
+  IN  EFI_HANDLE  FvHandle\r
   )\r
-/*++\r
+{\r
+  EFI_STATUS                          Status;\r
+  EFI_GUID                            FvNameGuid;\r
+  BOOLEAN                             FvNameGuidIsFound;\r
+  UINT32                              ExtHeaderOffset;\r
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *Fvb;\r
+  EFI_FIRMWARE_VOLUME_HEADER          *FwVolHeader;\r
+  EFI_FV_BLOCK_MAP_ENTRY              *BlockMap;\r
+  UINTN                               LbaOffset;\r
+  UINTN                               Index;\r
+  EFI_LBA                             LbaIndex;\r
+  LIST_ENTRY                          *Link;\r
+  KNOWN_HANDLE                        *KnownHandle;\r
 \r
-Routine Description:\r
+  FwVolHeader = NULL;\r
 \r
-  Remember that Fv protocol on FvHandle has had it's drivers placed on the\r
-  mDiscoveredList. This fucntion adds entries on the mFvHandleList. Items are\r
-  never removed/freed from the mFvHandleList.\r
+  //\r
+  // Get the FirmwareVolumeBlock protocol on that handle\r
+  //\r
+  FvNameGuidIsFound = FALSE;\r
+  Status            = CoreHandleProtocol (FvHandle, &gEfiFirmwareVolumeBlockProtocolGuid, (VOID **)&Fvb);\r
+  if (!EFI_ERROR (Status)) {\r
+    //\r
+    // Get the full FV header based on FVB protocol.\r
+    //\r
+    ASSERT (Fvb != NULL);\r
+    Status = GetFwVolHeader (Fvb, &FwVolHeader);\r
+    if (!EFI_ERROR (Status)) {\r
+      ASSERT (FwVolHeader != NULL);\r
+      if (VerifyFvHeaderChecksum (FwVolHeader) && (FwVolHeader->ExtHeaderOffset != 0)) {\r
+        ExtHeaderOffset = (UINT32)FwVolHeader->ExtHeaderOffset;\r
+        BlockMap        = FwVolHeader->BlockMap;\r
+        LbaIndex        = 0;\r
+        LbaOffset       = 0;\r
+        //\r
+        // Find LbaIndex and LbaOffset for FV extension header based on BlockMap.\r
+        //\r
+        while ((BlockMap->NumBlocks != 0) || (BlockMap->Length != 0)) {\r
+          for (Index = 0; Index < BlockMap->NumBlocks && ExtHeaderOffset >= BlockMap->Length; Index++) {\r
+            ExtHeaderOffset -= BlockMap->Length;\r
+            LbaIndex++;\r
+          }\r
 \r
-Arguments:\r
+          //\r
+          // Check whether FvExtHeader is crossing the multi block range.\r
+          //\r
+          if (Index < BlockMap->NumBlocks) {\r
+            LbaOffset = ExtHeaderOffset;\r
+            break;\r
+          }\r
 \r
-  FvHandle - The handle of a FV that has been processed\r
+          BlockMap++;\r
+        }\r
 \r
-Returns:\r
+        //\r
+        // Read FvNameGuid from FV extension header.\r
+        //\r
+        Status = ReadFvbData (Fvb, &LbaIndex, &LbaOffset, sizeof (FvNameGuid), (UINT8 *)&FvNameGuid);\r
+        if (!EFI_ERROR (Status)) {\r
+          FvNameGuidIsFound = TRUE;\r
+        }\r
+      }\r
 \r
-  None\r
+      CoreFreePool (FwVolHeader);\r
+    }\r
+  }\r
 \r
---*/\r
-{\r
-  KNOWN_HANDLE  *KnownHandle;\r
+  if (FvNameGuidIsFound) {\r
+    //\r
+    // Check whether the FV image with the found FvNameGuid has been processed.\r
+    //\r
+    for (Link = mFvHandleList.ForwardLink; Link != &mFvHandleList; Link = Link->ForwardLink) {\r
+      KnownHandle = CR (Link, KNOWN_HANDLE, Link, KNOWN_HANDLE_SIGNATURE);\r
+      if (CompareGuid (&FvNameGuid, &KnownHandle->FvNameGuid)) {\r
+        DEBUG ((DEBUG_ERROR, "FvImage on FvHandle %p and %p has the same FvNameGuid %g.\n", FvHandle, KnownHandle->Handle, &FvNameGuid));\r
+        return NULL;\r
+      }\r
+    }\r
+  }\r
 \r
-  KnownHandle = CoreAllocateBootServicesPool (sizeof (KNOWN_HANDLE));\r
+  KnownHandle = AllocateZeroPool (sizeof (KNOWN_HANDLE));\r
   ASSERT (KnownHandle != NULL);\r
 \r
   KnownHandle->Signature = KNOWN_HANDLE_SIGNATURE;\r
-  KnownHandle->Handle = FvHandle;\r
+  KnownHandle->Handle    = FvHandle;\r
+  if (FvNameGuidIsFound) {\r
+    CopyGuid (&KnownHandle->FvNameGuid, &FvNameGuid);\r
+  }\r
+\r
   InsertTailList (&mFvHandleList, &KnownHandle->Link);\r
+  return KnownHandle;\r
 }\r
 \r
+/**\r
+  Convert FvHandle and DriverName into an EFI device path\r
+\r
+  @param  Fv                    Fv protocol, needed to read Depex info out of\r
+                                FLASH.\r
+  @param  FvHandle              Handle for Fv, needed in the\r
+                                EFI_CORE_DRIVER_ENTRY so that the PE image can be\r
+                                read out of the FV at a later time.\r
+  @param  DriverName            Name of driver to add to mDiscoveredList.\r
 \r
+  @return Pointer to device path constructed from FvHandle and DriverName\r
 \r
-STATIC\r
+**/\r
 EFI_DEVICE_PATH_PROTOCOL *\r
 CoreFvToDevicePath (\r
-  IN  EFI_FIRMWARE_VOLUME_PROTOCOL    *Fv,\r
-  IN  EFI_HANDLE                      FvHandle,\r
-  IN  EFI_GUID                        *DriverName\r
+  IN  EFI_FIRMWARE_VOLUME2_PROTOCOL  *Fv,\r
+  IN  EFI_HANDLE                     FvHandle,\r
+  IN  EFI_GUID                       *DriverName\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Convert FvHandle and DriverName into an EFI device path\r
-\r
-Arguments:\r
-\r
-  Fv         - Fv protocol, needed to read Depex info out of FLASH.\r
-  \r
-  FvHandle   - Handle for Fv, needed in the EFI_CORE_DRIVER_ENTRY so that the\r
-               PE image can be read out of the FV at a later time.\r
-\r
-  DriverName - Name of driver to add to mDiscoveredList.\r
-\r
-Returns:\r
-\r
-  Pointer to device path constructed from FvHandle and DriverName\r
-\r
---*/\r
 {\r
-  EFI_STATUS                          Status;\r
-  EFI_DEVICE_PATH_PROTOCOL            *FvDevicePath;\r
-  EFI_DEVICE_PATH_PROTOCOL            *FileNameDevicePath;\r
+  EFI_STATUS                Status;\r
+  EFI_DEVICE_PATH_PROTOCOL  *FvDevicePath;\r
+  EFI_DEVICE_PATH_PROTOCOL  *FileNameDevicePath;\r
 \r
   //\r
   // Remember the device path of the FV\r
@@ -710,75 +820,68 @@ Returns:
     // Build a device path to the file in the FV to pass into gBS->LoadImage\r
     //\r
     EfiInitializeFwVolDevicepathNode (&mFvDevicePath.File, DriverName);\r
-    mFvDevicePath.End.Type = EFI_END_ENTIRE_DEVICE_PATH;\r
-    mFvDevicePath.End.SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;\r
-    SetDevicePathNodeLength (&mFvDevicePath.End, sizeof (EFI_DEVICE_PATH_PROTOCOL));\r
-\r
-    FileNameDevicePath = CoreAppendDevicePath (\r
-                            FvDevicePath, \r
-                            (EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath\r
-                            );\r
+    SetDevicePathEndNode (&mFvDevicePath.End);\r
+\r
+    FileNameDevicePath = AppendDevicePath (\r
+                           FvDevicePath,\r
+                           (EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath\r
+                           );\r
   }\r
 \r
   return FileNameDevicePath;\r
 }\r
 \r
-\r
-\r
-EFI_STATUS\r
-CoreAddToDriverList (\r
-  IN  EFI_FIRMWARE_VOLUME_PROTOCOL    *Fv,\r
-  IN  EFI_HANDLE                      FvHandle,\r
-  IN  EFI_GUID                        *DriverName\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry, \r
+/**\r
+  Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,\r
   and initilize any state variables. Read the Depex from the FV and store it\r
   in DriverEntry. Pre-process the Depex to set the SOR, Before and After state.\r
   The Discovered list is never free'ed and contains booleans that represent the\r
   other possible DXE driver states.\r
 \r
-Arguments:\r
-\r
-  Fv         - Fv protocol, needed to read Depex info out of FLASH.\r
-  \r
-  FvHandle   - Handle for Fv, needed in the EFI_CORE_DRIVER_ENTRY so that the\r
-               PE image can be read out of the FV at a later time.\r
-\r
-  DriverName - Name of driver to add to mDiscoveredList.\r
+  @param  Fv                    Fv protocol, needed to read Depex info out of\r
+                                FLASH.\r
+  @param  FvHandle              Handle for Fv, needed in the\r
+                                EFI_CORE_DRIVER_ENTRY so that the PE image can be\r
+                                read out of the FV at a later time.\r
+  @param  DriverName            Name of driver to add to mDiscoveredList.\r
+  @param  Type                  Fv File Type of file to add to mDiscoveredList.\r
 \r
-Returns:\r
+  @retval EFI_SUCCESS           If driver was added to the mDiscoveredList.\r
+  @retval EFI_ALREADY_STARTED   The driver has already been started. Only one\r
+                                DriverName may be active in the system at any one\r
+                                time.\r
 \r
-  EFI_SUCCESS - If driver was added to the mDiscoveredList.\r
-\r
-  EFI_ALREADY_STARTED - The driver has already been started. Only one DriverName\r
-                        may be active in the system at any one time.\r
-\r
---*/\r
+**/\r
+EFI_STATUS\r
+CoreAddToDriverList (\r
+  IN  EFI_FIRMWARE_VOLUME2_PROTOCOL  *Fv,\r
+  IN  EFI_HANDLE                     FvHandle,\r
+  IN  EFI_GUID                       *DriverName,\r
+  IN  EFI_FV_FILETYPE                Type\r
+  )\r
 {\r
-  EFI_CORE_DRIVER_ENTRY               *DriverEntry;\r
+  EFI_CORE_DRIVER_ENTRY  *DriverEntry;\r
 \r
\r
   //\r
-  // Create the Driver Entry for the list. ZeroPool initializes lots of variables to \r
+  // Create the Driver Entry for the list. ZeroPool initializes lots of variables to\r
   // NULL or FALSE.\r
   //\r
-  DriverEntry = CoreAllocateZeroBootServicesPool (sizeof (EFI_CORE_DRIVER_ENTRY));\r
+  DriverEntry = AllocateZeroPool (sizeof (EFI_CORE_DRIVER_ENTRY));\r
   ASSERT (DriverEntry != NULL);\r
+  if (Type == EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {\r
+    DriverEntry->IsFvImage = TRUE;\r
+  }\r
 \r
-  DriverEntry->Signature        = EFI_CORE_DRIVER_ENTRY_SIGNATURE;\r
-  CopyMem (&DriverEntry->FileName, DriverName, sizeof (EFI_GUID));\r
+  DriverEntry->Signature = EFI_CORE_DRIVER_ENTRY_SIGNATURE;\r
+  CopyGuid (&DriverEntry->FileName, DriverName);\r
   DriverEntry->FvHandle         = FvHandle;\r
   DriverEntry->Fv               = Fv;\r
   DriverEntry->FvFileDevicePath = CoreFvToDevicePath (Fv, FvHandle, DriverName);\r
 \r
   CoreGetDepexSectionAndPreProccess (DriverEntry);\r
-  \r
+\r
   CoreAcquireDispatcherLock ();\r
-  \r
+\r
   InsertTailList (&mDiscoveredList, &DriverEntry->Link);\r
 \r
   CoreReleaseDispatcherLock ();\r
@@ -786,202 +889,350 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
-STATIC\r
+/**\r
+  Check if a FV Image type file (EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) is\r
+  described by a EFI_HOB_FIRMWARE_VOLUME2 Hob.\r
+\r
+  @param  FvNameGuid            The FV image guid specified.\r
+  @param  DriverName            The driver guid specified.\r
+\r
+  @retval TRUE                  This file is found in a EFI_HOB_FIRMWARE_VOLUME2\r
+                                Hob.\r
+  @retval FALSE                 Not found.\r
+\r
+**/\r
 BOOLEAN\r
 FvFoundInHobFv2 (\r
-  IN  EFI_HANDLE                      FvHandle,\r
-  IN  CONST EFI_GUID                  *DriverName\r
+  IN  CONST EFI_GUID  *FvNameGuid,\r
+  IN  CONST EFI_GUID  *DriverName\r
   )\r
-/*++\r
+{\r
+  EFI_PEI_HOB_POINTERS  HobFv2;\r
 \r
-Routine Description:\r
+  HobFv2.Raw = GetHobList ();\r
 \r
-  Check if a FV Image type file (EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) is\r
-  described by a EFI_HOB_FIRMWARE_VOLUME2 Hob.\r
+  while ((HobFv2.Raw = GetNextHob (EFI_HOB_TYPE_FV2, HobFv2.Raw)) != NULL) {\r
+    //\r
+    // Compare parent FvNameGuid and FileGuid both.\r
+    //\r
+    if (CompareGuid (DriverName, &HobFv2.FirmwareVolume2->FileName) &&\r
+        CompareGuid (FvNameGuid, &HobFv2.FirmwareVolume2->FvName))\r
+    {\r
+      return TRUE;\r
+    }\r
 \r
-Arguments:\r
+    HobFv2.Raw = GET_NEXT_HOB (HobFv2);\r
+  }\r
 \r
-  FvHandle    - The handle which FVB protocol installed on.\r
-  DriverName  - The driver guid specified.\r
+  return FALSE;\r
+}\r
 \r
-Returns:\r
+/**\r
+  Find USED_SIZE FV_EXT_TYPE entry in FV extension header and get the FV used size.\r
 \r
-  TRUE    - This file is found in a EFI_HOB_FIRMWARE_VOLUME2 Hob.\r
+  @param[in]  FvHeader      Pointer to FV header.\r
+  @param[out] FvUsedSize    Pointer to FV used size returned,\r
+                            only valid if USED_SIZE FV_EXT_TYPE entry is found.\r
+  @param[out] EraseByte     Pointer to erase byte returned,\r
+                            only valid if USED_SIZE FV_EXT_TYPE entry is found.\r
 \r
-  FALSE   - Not found.\r
-  \r
+  @retval TRUE              USED_SIZE FV_EXT_TYPE entry is found,\r
+                            FV used size and erase byte are returned.\r
+  @retval FALSE             No USED_SIZE FV_EXT_TYPE entry found.\r
 \r
---*/\r
+**/\r
+BOOLEAN\r
+GetFvUsedSize (\r
+  IN EFI_FIRMWARE_VOLUME_HEADER  *FvHeader,\r
+  OUT UINT32                     *FvUsedSize,\r
+  OUT UINT8                      *EraseByte\r
+  )\r
 {\r
-  EFI_PEI_HOB_POINTERS                HobFv2;\r
-  EFI_STATUS                          Status;\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *Fvb;\r
-  EFI_FIRMWARE_VOLUME_HEADER          *FvHeader;\r
-  EFI_PHYSICAL_ADDRESS                FvHeaderAddr;\r
-  \r
-  HobFv2.Raw = GetHobList ();\r
-  \r
-  while ((HobFv2.Raw = GetNextHob (EFI_HOB_TYPE_FV2, HobFv2.Raw)) != NULL) {\r
-    if (CompareGuid (DriverName, &HobFv2.FirmwareVolume2->FileName)) {\r
-      Status = CoreHandleProtocol (FvHandle, &gEfiFirmwareVolumeBlockProtocolGuid, (VOID **) &Fvb);\r
-      if (!EFI_ERROR (Status)) {\r
-        Status = Fvb->GetPhysicalAddress (Fvb, &FvHeaderAddr);\r
-        if (!EFI_ERROR (Status)) {\r
-          FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) FvHeaderAddr;\r
-          if (CompareGuid (&FvHeader->FileSystemGuid, &HobFv2.FirmwareVolume2->FvName)) {\r
-            return TRUE;\r
-          }\r
+  UINT16                                        ExtHeaderOffset;\r
+  EFI_FIRMWARE_VOLUME_EXT_HEADER                *ExtHeader;\r
+  EFI_FIRMWARE_VOLUME_EXT_ENTRY                 *ExtEntryList;\r
+  EFI_FIRMWARE_VOLUME_EXT_ENTRY_USED_SIZE_TYPE  *ExtEntryUsedSize;\r
+\r
+  ExtHeaderOffset = ReadUnaligned16 (&FvHeader->ExtHeaderOffset);\r
+  if (ExtHeaderOffset != 0) {\r
+    ExtHeader    = (EFI_FIRMWARE_VOLUME_EXT_HEADER *)((UINT8 *)FvHeader + ExtHeaderOffset);\r
+    ExtEntryList = (EFI_FIRMWARE_VOLUME_EXT_ENTRY *)(ExtHeader + 1);\r
+    while ((UINTN)ExtEntryList < ((UINTN)ExtHeader + ReadUnaligned32 (&ExtHeader->ExtHeaderSize))) {\r
+      if (ReadUnaligned16 (&ExtEntryList->ExtEntryType) == EFI_FV_EXT_TYPE_USED_SIZE_TYPE) {\r
+        //\r
+        // USED_SIZE FV_EXT_TYPE entry is found.\r
+        //\r
+        ExtEntryUsedSize = (EFI_FIRMWARE_VOLUME_EXT_ENTRY_USED_SIZE_TYPE *)ExtEntryList;\r
+        *FvUsedSize      = ReadUnaligned32 (&ExtEntryUsedSize->UsedSize);\r
+        if ((ReadUnaligned32 (&FvHeader->Attributes) & EFI_FVB2_ERASE_POLARITY) != 0) {\r
+          *EraseByte = 0xFF;\r
+        } else {\r
+          *EraseByte = 0;\r
         }\r
+\r
+        DEBUG ((\r
+          DEBUG_INFO,\r
+          "FV at 0x%x has 0x%x used size, and erase byte is 0x%02x\n",\r
+          FvHeader,\r
+          *FvUsedSize,\r
+          *EraseByte\r
+          ));\r
+        return TRUE;\r
       }\r
+\r
+      ExtEntryList = (EFI_FIRMWARE_VOLUME_EXT_ENTRY *)\r
+                     ((UINT8 *)ExtEntryList + ReadUnaligned16 (&ExtEntryList->ExtEntrySize));\r
     }\r
-    HobFv2.Raw = GET_NEXT_HOB (HobFv2);\r
   }\r
 \r
+  //\r
+  // No USED_SIZE FV_EXT_TYPE entry found.\r
+  //\r
   return FALSE;\r
 }\r
 \r
+/**\r
+  Get Fv image(s) from the FV through file name, and produce FVB protocol for every Fv image(s).\r
 \r
-EFI_STATUS \r
+  @param  Fv                    The FIRMWARE_VOLUME protocol installed on the FV.\r
+  @param  FvHandle              The handle which FVB protocol installed on.\r
+  @param  FileName              The file name guid specified.\r
+\r
+  @retval EFI_OUT_OF_RESOURCES  No enough memory or other resource.\r
+  @retval EFI_SUCCESS           Function successfully returned.\r
+\r
+**/\r
+EFI_STATUS\r
 CoreProcessFvImageFile (\r
-  IN  EFI_FIRMWARE_VOLUME_PROTOCOL    *Fv,\r
-  IN  EFI_HANDLE                      FvHandle,\r
-  IN  EFI_GUID                        *DriverName\r
+  IN  EFI_FIRMWARE_VOLUME2_PROTOCOL  *Fv,\r
+  IN  EFI_HANDLE                     FvHandle,\r
+  IN  EFI_GUID                       *FileName\r
   )\r
-/*++\r
-\r
-Routine Description:\r
+{\r
+  EFI_STATUS                  Status;\r
+  EFI_SECTION_TYPE            SectionType;\r
+  UINT32                      AuthenticationStatus;\r
+  VOID                        *Buffer;\r
+  VOID                        *AlignedBuffer;\r
+  UINTN                       BufferSize;\r
+  EFI_FIRMWARE_VOLUME_HEADER  *FvHeader;\r
+  UINT32                      FvAlignment;\r
+  EFI_DEVICE_PATH_PROTOCOL    *FvFileDevicePath;\r
+  UINT32                      FvUsedSize;\r
+  UINT8                       EraseByte;\r
+  UINTN                       Index;\r
 \r
-  Get the driver from the FV through driver name, and produce a FVB protocol on FvHandle.\r
+  //\r
+  // Read firmware volume section(s)\r
+  //\r
+  SectionType = EFI_SECTION_FIRMWARE_VOLUME_IMAGE;\r
 \r
-Arguments:\r
+  Index = 0;\r
+  do {\r
+    FvHeader      = NULL;\r
+    FvAlignment   = 0;\r
+    Buffer        = NULL;\r
+    BufferSize    = 0;\r
+    AlignedBuffer = NULL;\r
+    Status        = Fv->ReadSection (\r
+                          Fv,\r
+                          FileName,\r
+                          SectionType,\r
+                          Index,\r
+                          &Buffer,\r
+                          &BufferSize,\r
+                          &AuthenticationStatus\r
+                          );\r
+    if (!EFI_ERROR (Status)) {\r
+      //\r
+      // Evaluate the authentication status of the Firmware Volume through\r
+      // Security Architectural Protocol\r
+      //\r
+      if (gSecurity != NULL) {\r
+        FvFileDevicePath = CoreFvToDevicePath (Fv, FvHandle, FileName);\r
+        Status           = gSecurity->FileAuthenticationState (\r
+                                        gSecurity,\r
+                                        AuthenticationStatus,\r
+                                        FvFileDevicePath\r
+                                        );\r
+        if (FvFileDevicePath != NULL) {\r
+          FreePool (FvFileDevicePath);\r
+        }\r
 \r
-  Fv          - The FIRMWARE_VOLUME protocol installed on the FV.\r
-  FvHandle    - The handle which FVB protocol installed on.\r
-  DriverName  - The driver guid specified.\r
+        if (Status != EFI_SUCCESS) {\r
+          //\r
+          // Security check failed. The firmware volume should not be used for any purpose.\r
+          //\r
+          if (Buffer != NULL) {\r
+            FreePool (Buffer);\r
+          }\r
 \r
-Returns:\r
+          break;\r
+        }\r
+      }\r
 \r
-  EFI_OUT_OF_RESOURCES    - No enough memory or other resource.\r
-  \r
-  EFI_VOLUME_CORRUPTED    - Corrupted volume.\r
-  \r
-  EFI_SUCCESS             - Function successfully returned.\r
-  \r
+      //\r
+      // FvImage should be at its required alignment.\r
+      //\r
+      FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)Buffer;\r
+      //\r
+      // If EFI_FVB2_WEAK_ALIGNMENT is set in the volume header then the first byte of the volume\r
+      // can be aligned on any power-of-two boundary. A weakly aligned volume can not be moved from\r
+      // its initial linked location and maintain its alignment.\r
+      //\r
+      if ((ReadUnaligned32 (&FvHeader->Attributes) & EFI_FVB2_WEAK_ALIGNMENT) != EFI_FVB2_WEAK_ALIGNMENT) {\r
+        //\r
+        // Get FvHeader alignment\r
+        //\r
+        FvAlignment = 1 << ((ReadUnaligned32 (&FvHeader->Attributes) & EFI_FVB2_ALIGNMENT) >> 16);\r
+        //\r
+        // FvAlignment must be greater than or equal to 8 bytes of the minimum FFS alignment value.\r
+        //\r
+        if (FvAlignment < 8) {\r
+          FvAlignment = 8;\r
+        }\r
 \r
---*/\r
-{\r
-  EFI_STATUS                          Status;\r
-  EFI_SECTION_TYPE                    SectionType;\r
-  UINT32                              AuthenticationStatus;\r
-  VOID                                *Buffer;\r
-  UINTN                               BufferSize;\r
+        DEBUG ((\r
+          DEBUG_INFO,\r
+          "%a() FV at 0x%x, FvAlignment required is 0x%x\n",\r
+          __FUNCTION__,\r
+          FvHeader,\r
+          FvAlignment\r
+          ));\r
 \r
-  //\r
-  // Read the first (and only the first) firmware volume section\r
-  //\r
-  SectionType = EFI_SECTION_FIRMWARE_VOLUME_IMAGE;\r
-  Buffer      = NULL;\r
-  BufferSize  = 0;\r
-  Status = Fv->ReadSection (\r
-                Fv, \r
-                DriverName, \r
-                SectionType, \r
-                0, \r
-                &Buffer, \r
-                &BufferSize,\r
-                &AuthenticationStatus\r
+        //\r
+        // Check FvImage alignment.\r
+        //\r
+        if ((UINTN)FvHeader % FvAlignment != 0) {\r
+          //\r
+          // Allocate the aligned buffer for the FvImage.\r
+          //\r
+          AlignedBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES (BufferSize), (UINTN)FvAlignment);\r
+          if (AlignedBuffer == NULL) {\r
+            FreePool (Buffer);\r
+            Status = EFI_OUT_OF_RESOURCES;\r
+            break;\r
+          } else {\r
+            //\r
+            // Move FvImage into the aligned buffer and release the original buffer.\r
+            //\r
+            if (GetFvUsedSize (FvHeader, &FvUsedSize, &EraseByte)) {\r
+              //\r
+              // Copy the used bytes and fill the rest with the erase value.\r
+              //\r
+              CopyMem (AlignedBuffer, FvHeader, (UINTN)FvUsedSize);\r
+              SetMem (\r
+                (UINT8 *)AlignedBuffer + FvUsedSize,\r
+                (UINTN)(BufferSize - FvUsedSize),\r
+                EraseByte\r
                 );\r
-  if (!EFI_ERROR (Status)) {\r
-    //\r
-    // Produce a FVB protocol for the file\r
-    //\r
-    Status = ProduceFVBProtocolOnBuffer (\r
-              (EFI_PHYSICAL_ADDRESS) (UINTN) Buffer,\r
-              (UINT64)BufferSize,\r
-              FvHandle,\r
-              NULL\r
-              );\r
-  }\r
+            } else {\r
+              CopyMem (AlignedBuffer, Buffer, BufferSize);\r
+            }\r
 \r
-  if (EFI_ERROR (Status) && (Buffer != NULL)) {    \r
-  //\r
-  // ReadSection or Produce FVB failed, Free data buffer\r
-  //\r
-  CoreFreePool (Buffer); \r
+            FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)AlignedBuffer;\r
+            FreePool (Buffer);\r
+            Buffer = NULL;\r
+          }\r
+        }\r
+      }\r
 \r
-  }\r
+      //\r
+      // Produce a FVB protocol for the file\r
+      //\r
+      Status = ProduceFVBProtocolOnBuffer (\r
+                 (EFI_PHYSICAL_ADDRESS)(UINTN)FvHeader,\r
+                 (UINT64)BufferSize,\r
+                 FvHandle,\r
+                 AuthenticationStatus,\r
+                 NULL\r
+                 );\r
+    }\r
 \r
-  return Status;\r
-}\r
+    if (EFI_ERROR (Status)) {\r
+      //\r
+      // ReadSection or Produce FVB failed, Free data buffer\r
+      //\r
+      if (Buffer != NULL) {\r
+        FreePool (Buffer);\r
+      }\r
 \r
-STATIC\r
-VOID\r
-EFIAPI\r
-CoreFwVolEventProtocolNotify (\r
-  IN  EFI_EVENT       Event,\r
-  IN  VOID            *Context\r
-  )\r
-/*++\r
+      if (AlignedBuffer != NULL) {\r
+        FreeAlignedPages (AlignedBuffer, EFI_SIZE_TO_PAGES (BufferSize));\r
+      }\r
 \r
-Routine Description:\r
+      break;\r
+    } else {\r
+      Index++;\r
+    }\r
+  } while (TRUE);\r
 \r
-  Event notification that is fired every time a FV dispatch protocol is added. \r
+  if (Index > 0) {\r
+    //\r
+    // At least one FvImage has been processed successfully.\r
+    //\r
+    return EFI_SUCCESS;\r
+  } else {\r
+    return Status;\r
+  }\r
+}\r
+\r
+/**\r
+  Event notification that is fired every time a FV dispatch protocol is added.\r
   More than one protocol may have been added when this event is fired, so you\r
   must loop on CoreLocateHandle () to see how many protocols were added and\r
   do the following to each FV:\r
-\r
-  If the Fv has already been processed, skip it. If the Fv has not been \r
+  If the Fv has already been processed, skip it. If the Fv has not been\r
   processed then mark it as being processed, as we are about to process it.\r
-\r
-  Read the Fv and add any driver in the Fv to the mDiscoveredList.The \r
+  Read the Fv and add any driver in the Fv to the mDiscoveredList.The\r
   mDiscoveredList is never free'ed and contains variables that define\r
-  the other states the DXE driver transitions to.. \r
-  \r
+  the other states the DXE driver transitions to..\r
   While you are at it read the A Priori file into memory.\r
   Place drivers in the A Priori list onto the mScheduledQueue.\r
 \r
-Arguments:\r
-\r
-  Event   - The Event that is being processed, not used.\r
-  \r
-  Context - Event Context, not used.\r
-\r
-Returns:\r
-\r
-  None\r
+  @param  Event                 The Event that is being processed, not used.\r
+  @param  Context               Event Context, not used.\r
 \r
---*/\r
+**/\r
+VOID\r
+EFIAPI\r
+CoreFwVolEventProtocolNotify (\r
+  IN  EFI_EVENT  Event,\r
+  IN  VOID       *Context\r
+  )\r
 {\r
-  EFI_STATUS                    Status;\r
-  EFI_STATUS                    GetNextFileStatus;\r
-  EFI_STATUS                    SecurityStatus;\r
-  EFI_FIRMWARE_VOLUME_PROTOCOL  *Fv;\r
-  EFI_DEVICE_PATH_PROTOCOL      *FvDevicePath;\r
-  EFI_HANDLE                    FvHandle;\r
-  UINTN                         BufferSize;\r
-  EFI_GUID                      NameGuid;\r
-  UINTN                         Key;\r
-  EFI_FV_FILETYPE               Type;\r
-  EFI_FV_FILE_ATTRIBUTES        Attributes;\r
-  UINTN                         Size;\r
-  EFI_CORE_DRIVER_ENTRY         *DriverEntry;\r
-  EFI_GUID                      *AprioriFile;\r
-  UINTN                         AprioriEntryCount;\r
-  UINTN                         Index;\r
-  LIST_ENTRY                    *Link;\r
-  UINT32                        AuthenticationStatus;\r
-  UINTN                         SizeOfBuffer;\r
-\r
+  EFI_STATUS                     Status;\r
+  EFI_STATUS                     GetNextFileStatus;\r
+  EFI_FIRMWARE_VOLUME2_PROTOCOL  *Fv;\r
+  EFI_DEVICE_PATH_PROTOCOL       *FvDevicePath;\r
+  EFI_HANDLE                     FvHandle;\r
+  UINTN                          BufferSize;\r
+  EFI_GUID                       NameGuid;\r
+  UINTN                          Key;\r
+  EFI_FV_FILETYPE                Type;\r
+  EFI_FV_FILE_ATTRIBUTES         Attributes;\r
+  UINTN                          Size;\r
+  EFI_CORE_DRIVER_ENTRY          *DriverEntry;\r
+  EFI_GUID                       *AprioriFile;\r
+  UINTN                          AprioriEntryCount;\r
+  UINTN                          Index;\r
+  LIST_ENTRY                     *Link;\r
+  UINT32                         AuthenticationStatus;\r
+  UINTN                          SizeOfBuffer;\r
+  VOID                           *DepexBuffer;\r
+  KNOWN_HANDLE                   *KnownHandle;\r
+\r
+  FvHandle = NULL;\r
 \r
   while (TRUE) {\r
     BufferSize = sizeof (EFI_HANDLE);\r
-    Status = CoreLocateHandle (\r
-                    ByRegisterNotify,\r
-                    NULL,\r
-                    mFwVolEventRegistration,\r
-                    &BufferSize,\r
-                    &FvHandle\r
-                    );\r
+    Status     = CoreLocateHandle (\r
+                   ByRegisterNotify,\r
+                   NULL,\r
+                   mFwVolEventRegistration,\r
+                   &BufferSize,\r
+                   &FvHandle\r
+                   );\r
     if (EFI_ERROR (Status)) {\r
       //\r
       // If no more notification events exit\r
@@ -996,31 +1247,27 @@ Returns:
       continue;\r
     }\r
 \r
-    Status = CoreHandleProtocol (FvHandle, &gEfiFirmwareVolumeDispatchProtocolGuid, (VOID **)&Fv);\r
-    if (EFI_ERROR (Status)) {\r
+    //\r
+    // Since we are about to process this Fv mark it as processed.\r
+    //\r
+    KnownHandle = FvIsBeingProcessed (FvHandle);\r
+    if (KnownHandle == NULL) {\r
       //\r
-      // If no dispatch protocol then skip, but do not marked as being processed as it\r
-      // may show up later.\r
+      // The FV with the same FV name guid has already been processed.\r
+      // So lets skip it!\r
       //\r
       continue;\r
     }\r
 \r
-    //\r
-    // Since we are about to process this Fv mark it as processed.\r
-    //\r
-    FvIsBeingProcesssed (FvHandle);\r
-\r
-\r
-    Status = CoreHandleProtocol (FvHandle, &gEfiFirmwareVolumeProtocolGuid, (VOID **)&Fv);\r
-    if (EFI_ERROR (Status)) {\r
+    Status = CoreHandleProtocol (FvHandle, &gEfiFirmwareVolume2ProtocolGuid, (VOID **)&Fv);\r
+    if (EFI_ERROR (Status) || (Fv == NULL)) {\r
       //\r
-      // The Handle has a FirmwareVolumeDispatch protocol and should also contiain\r
-      // a FirmwareVolume protocol thus we should never get here.\r
+      // FvHandle must have Firmware Volume2 protocol thus we should never get here.\r
       //\r
       ASSERT (FALSE);\r
       continue;\r
     }\r
-    \r
+\r
     Status = CoreHandleProtocol (FvHandle, &gEfiDevicePathProtocolGuid, (VOID **)&FvDevicePath);\r
     if (EFI_ERROR (Status)) {\r
       //\r
@@ -1028,44 +1275,26 @@ Returns:
       //\r
       continue;\r
     }\r
-    \r
-    //\r
-    // Evaluate the authentication status of the Firmware Volume through \r
-    // Security Architectural Protocol\r
-    //\r
-    if (gSecurity != NULL) {\r
-      SecurityStatus = gSecurity->FileAuthenticationState (\r
-                                    gSecurity, \r
-                                    0, \r
-                                    FvDevicePath\r
-                                    );\r
-      if (SecurityStatus != EFI_SUCCESS) {\r
-        //\r
-        // Security check failed. The firmware volume should not be used for any purpose.\r
-        //\r
-        continue;\r
-      }\r
-    }   \r
-    \r
+\r
     //\r
-    // Discover Drivers in FV and add them to the Discovered Driver List. \r
-    // Process EFI_FV_FILETYPE_DRIVER type and then EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER \r
+    // Discover Drivers in FV and add them to the Discovered Driver List.\r
+    // Process EFI_FV_FILETYPE_DRIVER type and then EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER\r
     //  EFI_FV_FILETYPE_DXE_CORE is processed to produce a Loaded Image protocol for the core\r
     //  EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE is processed to create a Fvb\r
     //\r
-    for (Index = 0; Index < sizeof (mDxeFileTypes)/sizeof (EFI_FV_FILETYPE); Index++) {\r
+    for (Index = 0; Index < sizeof (mDxeFileTypes) / sizeof (EFI_FV_FILETYPE); Index++) {\r
       //\r
       // Initialize the search key\r
       //\r
       Key = 0;\r
       do {\r
-        Type = mDxeFileTypes[Index];\r
+        Type              = mDxeFileTypes[Index];\r
         GetNextFileStatus = Fv->GetNextFile (\r
-                                  Fv, \r
+                                  Fv,\r
                                   &Key,\r
-                                  &Type,  \r
-                                  &NameGuid, \r
-                                  &Attributes, \r
+                                  &Type,\r
+                                  &NameGuid,\r
+                                  &Attributes,\r
                                   &Size\r
                                   );\r
         if (!EFI_ERROR (GetNextFileStatus)) {\r
@@ -1080,11 +1309,9 @@ Returns:
                 // be initialized completely.\r
                 //\r
                 EfiInitializeFwVolDevicepathNode (&mFvDevicePath.File, &NameGuid);\r
-                mFvDevicePath.End.Type = EFI_END_ENTIRE_DEVICE_PATH;\r
-                mFvDevicePath.End.SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;\r
-                SetDevicePathNodeLength (&mFvDevicePath.End, sizeof (EFI_DEVICE_PATH_PROTOCOL));\r
+                SetDevicePathEndNode (&mFvDevicePath.End);\r
 \r
-                gDxeCoreLoadedImage->FilePath = CoreDuplicateDevicePath (\r
+                gDxeCoreLoadedImage->FilePath = DuplicateDevicePath (\r
                                                   (EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath\r
                                                   );\r
                 gDxeCoreLoadedImage->DeviceHandle = FvHandle;\r
@@ -1092,42 +1319,85 @@ Returns:
             }\r
           } else if (Type == EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {\r
             //\r
-            // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has already \r
+            // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has already\r
             // been extracted.\r
             //\r
-            if (FvFoundInHobFv2 (FvHandle, &NameGuid)) {\r
+            if (FvFoundInHobFv2 (&KnownHandle->FvNameGuid, &NameGuid)) {\r
               continue;\r
             }\r
-            \r
+\r
+            //\r
+            // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has SMM depex section.\r
+            //\r
+            DepexBuffer  = NULL;\r
+            SizeOfBuffer = 0;\r
+            Status       = Fv->ReadSection (\r
+                                 Fv,\r
+                                 &NameGuid,\r
+                                 EFI_SECTION_SMM_DEPEX,\r
+                                 0,\r
+                                 &DepexBuffer,\r
+                                 &SizeOfBuffer,\r
+                                 &AuthenticationStatus\r
+                                 );\r
+            if (!EFI_ERROR (Status)) {\r
+              //\r
+              // If SMM depex section is found, this FV image is invalid to be supported.\r
+              // ASSERT FALSE to report this FV image.\r
+              //\r
+              FreePool (DepexBuffer);\r
+              ASSERT (FALSE);\r
+            }\r
+\r
             //\r
-            // Found a firmware volume image. Produce a firmware volume block\r
-            // protocol for it so it gets dispatched from. This is usually a \r
-            // capsule.\r
+            // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has DXE depex section.\r
             //\r
-            CoreProcessFvImageFile (Fv, FvHandle, &NameGuid);\r
+            DepexBuffer  = NULL;\r
+            SizeOfBuffer = 0;\r
+            Status       = Fv->ReadSection (\r
+                                 Fv,\r
+                                 &NameGuid,\r
+                                 EFI_SECTION_DXE_DEPEX,\r
+                                 0,\r
+                                 &DepexBuffer,\r
+                                 &SizeOfBuffer,\r
+                                 &AuthenticationStatus\r
+                                 );\r
+            if (EFI_ERROR (Status)) {\r
+              //\r
+              // If no depex section, produce a firmware volume block protocol for it so it gets dispatched from.\r
+              //\r
+              CoreProcessFvImageFile (Fv, FvHandle, &NameGuid);\r
+            } else {\r
+              //\r
+              // If depex section is found, this FV image will be dispatched until its depex is evaluated to TRUE.\r
+              //\r
+              FreePool (DepexBuffer);\r
+              CoreAddToDriverList (Fv, FvHandle, &NameGuid, Type);\r
+            }\r
           } else {\r
             //\r
             // Transition driver from Undiscovered to Discovered state\r
             //\r
-            CoreAddToDriverList (Fv, FvHandle, &NameGuid);\r
+            CoreAddToDriverList (Fv, FvHandle, &NameGuid, Type);\r
           }\r
         }\r
       } while (!EFI_ERROR (GetNextFileStatus));\r
     }\r
-    \r
+\r
     //\r
     // Read the array of GUIDs from the Apriori file if it is present in the firmware volume\r
     //\r
     AprioriFile = NULL;\r
-    Status = Fv->ReadSection (\r
-                  Fv,\r
-                  &gAprioriGuid,\r
-                  EFI_SECTION_RAW,\r
-                  0,\r
-                  (VOID **)&AprioriFile,\r
-                  &SizeOfBuffer,\r
-                  &AuthenticationStatus\r
-                  );\r
+    Status      = Fv->ReadSection (\r
+                        Fv,\r
+                        &gAprioriGuid,\r
+                        EFI_SECTION_RAW,\r
+                        0,\r
+                        (VOID **)&AprioriFile,\r
+                        &SizeOfBuffer,\r
+                        &AuthenticationStatus\r
+                        );\r
     if (!EFI_ERROR (Status)) {\r
       AprioriEntryCount = SizeOfBuffer / sizeof (EFI_GUID);\r
     } else {\r
@@ -1139,93 +1409,76 @@ Returns:
     // drivers not in the current FV and these must be skipped since the a priori list\r
     // is only valid for the FV that it resided in.\r
     //\r
-    CoreAcquireDispatcherLock ();\r
-    \r
+\r
     for (Index = 0; Index < AprioriEntryCount; Index++) {\r
       for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
-        DriverEntry = CR(Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);\r
+        DriverEntry = CR (Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);\r
         if (CompareGuid (&DriverEntry->FileName, &AprioriFile[Index]) &&\r
-            (FvHandle == DriverEntry->FvHandle)) {\r
+            (FvHandle == DriverEntry->FvHandle))\r
+        {\r
+          CoreAcquireDispatcherLock ();\r
           DriverEntry->Dependent = FALSE;\r
           DriverEntry->Scheduled = TRUE;\r
           InsertTailList (&mScheduledQueue, &DriverEntry->ScheduledLink);\r
+          CoreReleaseDispatcherLock ();\r
+          DEBUG ((DEBUG_DISPATCH, "Evaluate DXE DEPEX for FFS(%g)\n", &DriverEntry->FileName));\r
+          DEBUG ((DEBUG_DISPATCH, "  RESULT = TRUE (Apriori)\n"));\r
           break;\r
         }\r
       }\r
     }\r
 \r
-    CoreReleaseDispatcherLock ();\r
-\r
     //\r
-    // Free data allocated by Fv->ReadSection () \r
+    // Free data allocated by Fv->ReadSection ()\r
     //\r
-    CoreFreePool (AprioriFile);  \r
+    CoreFreePool (AprioriFile);\r
   }\r
 }\r
 \r
+/**\r
+  Initialize the dispatcher. Initialize the notification function that runs when\r
+  an FV2 protocol is added to the system.\r
 \r
+**/\r
 VOID\r
 CoreInitializeDispatcher (\r
   VOID\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Initialize the dispatcher. Initialize the notification function that runs when\r
-  a FV protocol is added to the system.\r
-\r
-Arguments:\r
-\r
-  NONE\r
-\r
-Returns:\r
-\r
-  NONE \r
-\r
---*/\r
 {\r
-  mFwVolEvent = CoreCreateProtocolNotifyEvent (\r
-                  &gEfiFirmwareVolumeProtocolGuid, \r
+  PERF_FUNCTION_BEGIN ();\r
+\r
+  mFwVolEvent = EfiCreateProtocolNotifyEvent (\r
+                  &gEfiFirmwareVolume2ProtocolGuid,\r
                   TPL_CALLBACK,\r
                   CoreFwVolEventProtocolNotify,\r
                   NULL,\r
-                  &mFwVolEventRegistration,\r
-                  TRUE\r
+                  &mFwVolEventRegistration\r
                   );\r
+\r
+  PERF_FUNCTION_END ();\r
 }\r
 \r
 //\r
 // Function only used in debug builds\r
 //\r
+\r
+/**\r
+  Traverse the discovered list for any drivers that were discovered but not loaded\r
+  because the dependency experessions evaluated to false.\r
+\r
+**/\r
 VOID\r
 CoreDisplayDiscoveredNotDispatched (\r
   VOID\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Traverse the discovered list for any drivers that were discovered but not loaded \r
-  because the dependency experessions evaluated to false\r
-\r
-Arguments:\r
-\r
-  NONE\r
-\r
-Returns:\r
-\r
-  NONE \r
-\r
---*/\r
 {\r
-  LIST_ENTRY                    *Link;\r
-  EFI_CORE_DRIVER_ENTRY         *DriverEntry;\r
+  LIST_ENTRY             *Link;\r
+  EFI_CORE_DRIVER_ENTRY  *DriverEntry;\r
 \r
-  for (Link = mDiscoveredList.ForwardLink;Link !=&mDiscoveredList; Link = Link->ForwardLink) {\r
-    DriverEntry = CR(Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);\r
+  for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
+    DriverEntry = CR (Link, EFI_CORE_DRIVER_ENTRY, Link, EFI_CORE_DRIVER_ENTRY_SIGNATURE);\r
     if (DriverEntry->Dependent) {\r
-      DEBUG ((EFI_D_LOAD, "Driver %g was discovered but not loaded!!\n", &DriverEntry->FileName));\r
+      DEBUG ((DEBUG_LOAD, "Driver %g was discovered but not loaded!!\n", &DriverEntry->FileName));\r
     }\r
   }\r
 }\r