]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Dispatcher/Dispatcher.c
MdeModulePkg: Use new added Perf macros
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Dispatcher / Dispatcher.c
index a70e2e45d581aecb65f19f768ad771f9b3a055ff..4c6ca761a6054b7b2605ffd1a4e7ddb7c61accab 100644 (file)
@@ -26,7 +26,7 @@
   Depex - Dependency Expresion.\r
   SOR   - Schedule On Request - Don't schedule if this bit is set.\r
 \r
-Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
 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
@@ -392,25 +392,6 @@ CoreTrust (
   return EFI_NOT_FOUND;\r
 }\r
 \r
-\r
-/**\r
-  An empty function to pass error checking of CreateEventEx ().\r
-\r
-  @param  Event                 Event whose notification function is being invoked.\r
-  @param  Context               Pointer to the notification function's context,\r
-                                which is implementation-dependent.\r
-\r
-**/\r
-VOID\r
-EFIAPI\r
-CoreEmptyCallbackFunction (\r
-  IN EFI_EVENT                Event,\r
-  IN VOID                     *Context\r
-  )\r
-{\r
-  return;\r
-}\r
-\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
@@ -438,6 +419,7 @@ CoreDispatcher (
   BOOLEAN                         ReadyToRun;\r
   EFI_EVENT                       DxeDispatchEvent;\r
   \r
+  PERF_FUNCTION_BEGIN ();\r
 \r
   if (gDispatcherRunning) {\r
     //\r
@@ -451,7 +433,7 @@ CoreDispatcher (
   Status = CoreCreateEventEx (\r
              EVT_NOTIFY_SIGNAL,\r
              TPL_NOTIFY,\r
-             CoreEmptyCallbackFunction,\r
+             EfiEventEmptyFunction,\r
              NULL,\r
              &gEfiEventDxeDispatchGuid,\r
              &DxeDispatchEvent\r
@@ -603,6 +585,8 @@ CoreDispatcher (
 \r
   gDispatcherRunning = FALSE;\r
 \r
+  PERF_FUNCTION_END ();\r
+\r
   return ReturnStatus;\r
 }\r
 \r
@@ -736,6 +720,8 @@ FvIsBeingProcesssed (
   LIST_ENTRY                            *Link;\r
   KNOWN_HANDLE                          *KnownHandle;\r
 \r
+  FwVolHeader = NULL;\r
+\r
   //\r
   // Get the FirmwareVolumeBlock protocol on that handle\r
   //\r
@@ -790,7 +776,7 @@ FvIsBeingProcesssed (
     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 ((EFI_D_ERROR, "FvImage on FvHandle %p and %p has the same FvNameGuid %g.\n", FvHandle, KnownHandle->Handle, FvNameGuid));\r
+        DEBUG ((EFI_D_ERROR, "FvImage on FvHandle %p and %p has the same FvNameGuid %g.\n", FvHandle, KnownHandle->Handle, &FvNameGuid));\r
         return NULL;\r
       }\r
     }\r
@@ -955,7 +941,67 @@ FvFoundInHobFv2 (
   return FALSE;\r
 }\r
 \r
+/**\r
+  Find USED_SIZE FV_EXT_TYPE entry in FV extension header and get the FV used size.\r
+\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
+  @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
+BOOLEAN\r
+GetFvUsedSize (\r
+  IN EFI_FIRMWARE_VOLUME_HEADER     *FvHeader,\r
+  OUT UINT32                        *FvUsedSize,\r
+  OUT UINT8                         *EraseByte\r
+  )\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
+        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
+      ExtEntryList = (EFI_FIRMWARE_VOLUME_EXT_ENTRY *)\r
+                     ((UINT8 *) ExtEntryList + ReadUnaligned16 (&ExtEntryList->ExtEntrySize));\r
+    }\r
+  }\r
+\r
+  //\r
+  // No USED_SIZE FV_EXT_TYPE entry found.\r
+  //\r
+  return FALSE;\r
+}\r
 \r
 /**\r
   Get the driver from the FV through driver name, and produce a FVB protocol on FvHandle.\r
@@ -985,6 +1031,8 @@ CoreProcessFvImageFile (
   EFI_FIRMWARE_VOLUME_HEADER          *FvHeader;\r
   UINT32                              FvAlignment;\r
   EFI_DEVICE_PATH_PROTOCOL            *FvFileDevicePath;\r
+  UINT32                              FvUsedSize;\r
+  UINT8                               EraseByte;\r
 \r
   //\r
   // Read the first (and only the first) firmware volume section\r
@@ -1040,32 +1088,58 @@ CoreProcessFvImageFile (
     // 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 ((FvHeader->Attributes & EFI_FVB2_WEAK_ALIGNMENT) != EFI_FVB2_WEAK_ALIGNMENT) {\r
+    if ((ReadUnaligned32 (&FvHeader->Attributes) & EFI_FVB2_WEAK_ALIGNMENT) != EFI_FVB2_WEAK_ALIGNMENT) {\r
       //\r
       // Get FvHeader alignment\r
       //\r
-      FvAlignment = 1 << ((FvHeader->Attributes & EFI_FVB2_ALIGNMENT) >> 16);\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
+      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
-      // Allocate the aligned buffer for the FvImage.\r
+      // Check FvImage alignment.\r
       //\r
-      AlignedBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES (BufferSize), (UINTN) FvAlignment);\r
-      if (AlignedBuffer == NULL) {\r
-        FreePool (Buffer);\r
-        return EFI_OUT_OF_RESOURCES;\r
-      } else {\r
+      if ((UINTN) FvHeader % FvAlignment != 0) {\r
         //\r
-        // Move FvImage into the aligned buffer and release the original buffer.\r
+        // Allocate the aligned buffer for the FvImage.\r
         //\r
-        CopyMem (AlignedBuffer, Buffer, BufferSize);\r
-        FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) AlignedBuffer;\r
-        CoreFreePool (Buffer);\r
-        Buffer = NULL;\r
+        AlignedBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES (BufferSize), (UINTN) FvAlignment);\r
+        if (AlignedBuffer == NULL) {\r
+          FreePool (Buffer);\r
+          return EFI_OUT_OF_RESOURCES;\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
+          } else {\r
+            CopyMem (AlignedBuffer, Buffer, BufferSize);\r
+          }\r
+          FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) AlignedBuffer;\r
+          CoreFreePool (Buffer);\r
+          Buffer = NULL;\r
+        }\r
       }\r
     }\r
     //\r
@@ -1142,6 +1216,8 @@ CoreFwVolEventProtocolNotify (
   VOID                          *DepexBuffer;\r
   KNOWN_HANDLE                  *KnownHandle;\r
 \r
+  FvHandle = NULL;\r
+\r
   while (TRUE) {\r
     BufferSize = sizeof (EFI_HANDLE);\r
     Status = CoreLocateHandle (\r
@@ -1244,29 +1320,6 @@ CoreFwVolEventProtocolNotify (
               continue;\r
             }\r
 \r
-            //\r
-            // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has PEI depex section.\r
-            //\r
-            DepexBuffer  = NULL;\r
-            SizeOfBuffer = 0;\r
-            Status = Fv->ReadSection (\r
-                           Fv,\r
-                           &NameGuid,\r
-                           EFI_SECTION_PEI_DEPEX,\r
-                           0,\r
-                           &DepexBuffer,\r
-                           &SizeOfBuffer,\r
-                           &AuthenticationStatus\r
-                           );\r
-            if (!EFI_ERROR (Status)) {\r
-              //\r
-              // If PEI depex section is found, this FV image will be ignored in DXE phase.\r
-              // Now, DxeCore doesn't support FV image with more one type DEPEX section.\r
-              //\r
-              FreePool (DepexBuffer);\r
-              continue;\r
-            }\r
-\r
             //\r
             // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has SMM depex section.\r
             //\r
@@ -1283,11 +1336,11 @@ CoreFwVolEventProtocolNotify (
                            );\r
             if (!EFI_ERROR (Status)) {\r
               //\r
-              // If SMM depex section is found, this FV image will be ignored in DXE phase.\r
-              // Now, DxeCore doesn't support FV image with more one type DEPEX section.\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
-              continue;\r
+              ASSERT (FALSE);\r
             }\r
 \r
             //\r
@@ -1387,6 +1440,8 @@ CoreInitializeDispatcher (
   VOID\r
   )\r
 {\r
+  PERF_FUNCTION_BEGIN ();\r
+\r
   mFwVolEvent = EfiCreateProtocolNotifyEvent (\r
                   &gEfiFirmwareVolume2ProtocolGuid,\r
                   TPL_CALLBACK,\r
@@ -1394,6 +1449,8 @@ CoreInitializeDispatcher (
                   NULL,\r
                   &mFwVolEventRegistration\r
                   );\r
+\r
+  PERF_FUNCTION_END ();\r
 }\r
 \r
 //\r