]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/FwVolBlock/FwVolBlock.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / FwVolBlock / FwVolBlock.c
index 35926d3ee2a5cb1161d773bdaa1c949774ec400b..100aa28dffa4d3b94f3eee78a3d918e0ceab351d 100644 (file)
@@ -1,11 +1,11 @@
 /** @file\r
   Implementations for Firmware Volume Block protocol.\r
 \r
-  It consumes FV HOBs and creates read-lonly Firmare Volume Block protocol\r
+  It consumes FV HOBs and creates read-only Firmare Volume Block protocol\r
   instances for each of them.\r
 \r
-Copyright (c) 2006 - 2008, Intel Corporation. <BR>\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2006 - 2018, 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
 http://opensource.org/licenses/bsd-license.php\r
@@ -15,35 +15,59 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 **/\r
 \r
-#include <DxeMain.h>\r
+#include "DxeMain.h"\r
+#include "FwVolBlock.h"\r
 \r
-\r
-EFI_FW_VOL_BLOCK_DEVICE  mFwVolBlock = {\r
-  FVB_DEVICE_SIGNATURE,\r
-  NULL,\r
+FV_MEMMAP_DEVICE_PATH mFvMemmapDevicePathTemplate = {\r
   {\r
     {\r
+      HARDWARE_DEVICE_PATH,\r
+      HW_MEMMAP_DP,\r
       {\r
-        HARDWARE_DEVICE_PATH,\r
-        HW_MEMMAP_DP,\r
-        {\r
-          (UINT8)(sizeof (MEMMAP_DEVICE_PATH)),\r
-          (UINT8)(sizeof (MEMMAP_DEVICE_PATH) >> 8)\r
-        }\r
-      },\r
-      EfiMemoryMappedIO,\r
-      (EFI_PHYSICAL_ADDRESS) 0,\r
-      (EFI_PHYSICAL_ADDRESS) 0,\r
+        (UINT8)(sizeof (MEMMAP_DEVICE_PATH)),\r
+        (UINT8)(sizeof (MEMMAP_DEVICE_PATH) >> 8)\r
+      }\r
     },\r
+    EfiMemoryMappedIO,\r
+    (EFI_PHYSICAL_ADDRESS) 0,\r
+    (EFI_PHYSICAL_ADDRESS) 0,\r
+  },\r
+  {\r
+    END_DEVICE_PATH_TYPE,\r
+    END_ENTIRE_DEVICE_PATH_SUBTYPE,\r
+    {\r
+      END_DEVICE_PATH_LENGTH,\r
+      0\r
+    }\r
+  }\r
+};\r
+\r
+FV_PIWG_DEVICE_PATH mFvPIWGDevicePathTemplate = {\r
+  {\r
     {\r
-      END_DEVICE_PATH_TYPE,\r
-      END_ENTIRE_DEVICE_PATH_SUBTYPE,\r
+      MEDIA_DEVICE_PATH,\r
+      MEDIA_PIWG_FW_VOL_DP,\r
       {\r
-        END_DEVICE_PATH_LENGTH,\r
-        0\r
+        (UINT8)(sizeof (MEDIA_FW_VOL_DEVICE_PATH)),\r
+        (UINT8)(sizeof (MEDIA_FW_VOL_DEVICE_PATH) >> 8)\r
       }\r
     },\r
+    { 0 }\r
   },\r
+  {\r
+    END_DEVICE_PATH_TYPE,\r
+    END_ENTIRE_DEVICE_PATH_SUBTYPE,\r
+    {\r
+      END_DEVICE_PATH_LENGTH,\r
+      0\r
+    }\r
+  }\r
+};\r
+\r
+EFI_FW_VOL_BLOCK_DEVICE  mFwVolBlock = {\r
+  FVB_DEVICE_SIGNATURE,\r
+  NULL,\r
+  NULL,\r
   {\r
     FwVolBlockGetAttributes,\r
     (EFI_FVB_SET_ATTRIBUTES)FwVolBlockSetAttributes,\r
@@ -57,6 +81,7 @@ EFI_FW_VOL_BLOCK_DEVICE  mFwVolBlock = {
   0,\r
   NULL,\r
   0,\r
+  0,\r
   0\r
 };\r
 \r
@@ -75,7 +100,7 @@ EFI_STATUS
 EFIAPI\r
 FwVolBlockGetAttributes (\r
   IN CONST  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *This,\r
-  OUT       EFI_FVB_ATTRIBUTES                  *Attributes\r
+  OUT       EFI_FVB_ATTRIBUTES_2                *Attributes\r
   )\r
 {\r
   EFI_FW_VOL_BLOCK_DEVICE               *FvbDevice;\r
@@ -85,7 +110,7 @@ FwVolBlockGetAttributes (
   //\r
   // Since we are read only, it's safe to get attributes data from our in-memory copy.\r
   //\r
-  *Attributes = FvbDevice->FvbAttributes;\r
+  *Attributes = FvbDevice->FvbAttributes & ~EFI_FVB2_WRITE_STATUS;\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -109,7 +134,7 @@ EFI_STATUS
 EFIAPI\r
 FwVolBlockSetAttributes (\r
   IN CONST  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *This,\r
-  IN CONST  EFI_FVB_ATTRIBUTES                  *Attributes\r
+  IN CONST  EFI_FVB_ATTRIBUTES_2                *Attributes\r
   )\r
 {\r
   return EFI_UNSUPPORTED;\r
@@ -209,7 +234,7 @@ FwVolBlockReadBlock (
 \r
   if (Offset > FvbDevice->LbaCache[LbaIndex].Length) {\r
     //\r
-    // all exceed boundry, read nothing.\r
+    // all exceed boundary, read nothing.\r
     //\r
     *NumBytes = 0;\r
     return EFI_BAD_BUFFER_SIZE;\r
@@ -218,7 +243,7 @@ FwVolBlockReadBlock (
   NumOfBytesRead = *NumBytes;\r
   if (Offset + NumOfBytesRead > FvbDevice->LbaCache[LbaIndex].Length) {\r
     //\r
-    // partial exceed boundry, read data from current postion to end.\r
+    // partial exceed boundary, read data from current postion to end.\r
     //\r
     NumOfBytesRead = FvbDevice->LbaCache[LbaIndex].Length - Offset;\r
   }\r
@@ -302,7 +327,7 @@ FwVolBlockGetPhysicalAddress (
 \r
   FvbDevice = FVB_DEVICE_FROM_THIS (This);\r
 \r
-  if (FvbDevice->FvbAttributes & EFI_FVB2_MEMORY_MAPPED) {\r
+  if ((FvbDevice->FvbAttributes & EFI_FVB2_MEMORY_MAPPED) != 0) {\r
     *Address = FvbDevice->BaseAddress;\r
     return EFI_SUCCESS;\r
   }\r
@@ -378,7 +403,31 @@ FwVolBlockGetBlockSize (
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+\r
+  Get FVB authentication status\r
+\r
+  @param FvbProtocol    FVB protocol.\r
+\r
+  @return Authentication status.\r
+\r
+**/\r
+UINT32\r
+GetFvbAuthenticationStatus (\r
+  IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL     *FvbProtocol\r
+  )\r
+{\r
+  EFI_FW_VOL_BLOCK_DEVICE   *FvbDevice;\r
+  UINT32                    AuthenticationStatus;\r
+\r
+  AuthenticationStatus = 0;\r
+  FvbDevice = BASE_CR (FvbProtocol, EFI_FW_VOL_BLOCK_DEVICE, FwVolBlockInstance);\r
+  if (FvbDevice->Signature == FVB_DEVICE_SIGNATURE) {\r
+    AuthenticationStatus = FvbDevice->AuthenticationStatus;\r
+  }\r
 \r
+  return AuthenticationStatus;\r
+}\r
 \r
 /**\r
   This routine produces a firmware volume block protocol on a given\r
@@ -387,8 +436,10 @@ FwVolBlockGetBlockSize (
   @param  BaseAddress            base address of the firmware volume image\r
   @param  Length                 length of the firmware volume image\r
   @param  ParentHandle           handle of parent firmware volume, if this image\r
-                                 came from an FV image file in another firmware\r
+                                 came from an FV image file and section in another firmware\r
                                  volume (ala capsules)\r
+  @param  AuthenticationStatus   Authentication status inherited, if this image\r
+                                 came from an FV image file and section in another firmware volume.\r
   @param  FvProtocol             Firmware volume block protocol produced.\r
 \r
   @retval EFI_VOLUME_CORRUPTED   Volume corrupted.\r
@@ -402,6 +453,7 @@ ProduceFVBProtocolOnBuffer (
   IN EFI_PHYSICAL_ADDRESS   BaseAddress,\r
   IN UINT64                 Length,\r
   IN EFI_HANDLE             ParentHandle,\r
+  IN UINT32                 AuthenticationStatus,\r
   OUT EFI_HANDLE            *FvProtocol  OPTIONAL\r
   )\r
 {\r
@@ -422,23 +474,42 @@ ProduceFVBProtocolOnBuffer (
   if (FwVolHeader->Signature != EFI_FVH_SIGNATURE) {\r
     return EFI_VOLUME_CORRUPTED;\r
   }\r
+\r
   //\r
-  // Get FvHeader alignment\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
-  FvAlignment = 1 << ((FwVolHeader->Attributes & EFI_FVB2_ALIGNMENT) >> 16);\r
-  if (FvAlignment < 8) {\r
-    FvAlignment = 8;\r
-  }\r
-  if ((UINTN)BaseAddress % FvAlignment != 0) {\r
+  if ((FwVolHeader->Attributes & EFI_FVB2_WEAK_ALIGNMENT) != EFI_FVB2_WEAK_ALIGNMENT) {\r
     //\r
-    // FvImage buffer is not at its required alignment.\r
+    // Get FvHeader alignment\r
     //\r
-    return EFI_VOLUME_CORRUPTED;\r
+    FvAlignment = 1 << ((FwVolHeader->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
+    if ((UINTN)BaseAddress % FvAlignment != 0) {\r
+      //\r
+      // FvImage buffer is not at its required alignment.\r
+      //\r
+      DEBUG ((\r
+        DEBUG_ERROR,\r
+        "Unaligned FvImage found at 0x%lx:0x%lx, the required alignment is 0x%x\n",\r
+        BaseAddress,\r
+        Length,\r
+        FvAlignment\r
+        ));\r
+      return EFI_VOLUME_CORRUPTED;\r
+    }\r
   }\r
+\r
   //\r
   // Allocate EFI_FW_VOL_BLOCK_DEVICE\r
   //\r
-  FvbDev = CoreAllocateCopyPool (sizeof (EFI_FW_VOL_BLOCK_DEVICE), &mFwVolBlock);\r
+  FvbDev = AllocateCopyPool (sizeof (EFI_FW_VOL_BLOCK_DEVICE), &mFwVolBlock);\r
   if (FvbDev == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
@@ -446,6 +517,7 @@ ProduceFVBProtocolOnBuffer (
   FvbDev->BaseAddress   = BaseAddress;\r
   FvbDev->FvbAttributes = FwVolHeader->Attributes;\r
   FvbDev->FwVolBlockInstance.ParentHandle = ParentHandle;\r
+  FvbDev->AuthenticationStatus = AuthenticationStatus;\r
 \r
   //\r
   // Init the block caching fields of the device\r
@@ -457,14 +529,20 @@ ProduceFVBProtocolOnBuffer (
        PtrBlockMapEntry++) {\r
     FvbDev->NumBlocks += PtrBlockMapEntry->NumBlocks;\r
   }\r
+\r
   //\r
   // Second, allocate the cache\r
   //\r
-  FvbDev->LbaCache = CoreAllocateBootServicesPool (FvbDev->NumBlocks * sizeof (LBA_CACHE));\r
+  if (FvbDev->NumBlocks >= (MAX_ADDRESS / sizeof (LBA_CACHE))) {\r
+    CoreFreePool (FvbDev);\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  FvbDev->LbaCache = AllocatePool (FvbDev->NumBlocks * sizeof (LBA_CACHE));\r
   if (FvbDev->LbaCache == NULL) {\r
     CoreFreePool (FvbDev);\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
+\r
   //\r
   // Last, fill in the cache with the linear address of the blocks\r
   //\r
@@ -481,10 +559,33 @@ ProduceFVBProtocolOnBuffer (
   }\r
 \r
   //\r
-  // Set up the devicepath\r
+  // Judget whether FV name guid is produced in Fv extension header\r
   //\r
-  FvbDev->DevicePath.MemMapDevPath.StartingAddress = BaseAddress;\r
-  FvbDev->DevicePath.MemMapDevPath.EndingAddress = BaseAddress + FwVolHeader->FvLength - 1;\r
+  if (FwVolHeader->ExtHeaderOffset == 0) {\r
+    //\r
+    // FV does not contains extension header, then produce MEMMAP_DEVICE_PATH\r
+    //\r
+    FvbDev->DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) AllocateCopyPool (sizeof (FV_MEMMAP_DEVICE_PATH), &mFvMemmapDevicePathTemplate);\r
+    if (FvbDev->DevicePath == NULL) {\r
+      FreePool (FvbDev);\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+    ((FV_MEMMAP_DEVICE_PATH *) FvbDev->DevicePath)->MemMapDevPath.StartingAddress = BaseAddress;\r
+    ((FV_MEMMAP_DEVICE_PATH *) FvbDev->DevicePath)->MemMapDevPath.EndingAddress   = BaseAddress + FwVolHeader->FvLength - 1;\r
+  } else {\r
+    //\r
+    // FV contains extension header, then produce MEDIA_FW_VOL_DEVICE_PATH\r
+    //\r
+    FvbDev->DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) AllocateCopyPool (sizeof (FV_PIWG_DEVICE_PATH), &mFvPIWGDevicePathTemplate);\r
+    if (FvbDev->DevicePath == NULL) {\r
+      FreePool (FvbDev);\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+    CopyGuid (\r
+      &((FV_PIWG_DEVICE_PATH *)FvbDev->DevicePath)->FvDevPath.FvName,\r
+      (GUID *)(UINTN)(BaseAddress + FwVolHeader->ExtHeaderOffset)\r
+      );\r
+  }\r
 \r
   //\r
   //\r
@@ -493,8 +594,7 @@ ProduceFVBProtocolOnBuffer (
   Status = CoreInstallMultipleProtocolInterfaces (\r
              &FvbDev->Handle,\r
              &gEfiFirmwareVolumeBlockProtocolGuid,     &FvbDev->FwVolBlockInstance,\r
-             &gEfiDevicePathProtocolGuid,              &FvbDev->DevicePath,\r
-             &gEfiFirmwareVolumeDispatchProtocolGuid,  NULL,\r
+             &gEfiDevicePathProtocolGuid,              FvbDev->DevicePath,\r
              NULL\r
              );\r
 \r
@@ -511,9 +611,7 @@ ProduceFVBProtocolOnBuffer (
 \r
 \r
 /**\r
-  This routine is the driver initialization entry point.  It initializes the\r
-  libraries, consumes FV hobs and NT_NON_MM_FV environment variable and\r
-  produces instances of FW_VOL_BLOCK_PROTOCOL as appropriate.\r
+  This routine consumes FV hobs and produces instances of FW_VOL_BLOCK_PROTOCOL as appropriate.\r
 \r
   @param  ImageHandle            The image handle.\r
   @param  SystemTable            The system table.\r
@@ -530,16 +628,31 @@ FwVolBlockDriverInit (
   )\r
 {\r
   EFI_PEI_HOB_POINTERS          FvHob;\r
+  EFI_PEI_HOB_POINTERS          Fv3Hob;\r
+  UINT32                        AuthenticationStatus;\r
 \r
   //\r
   // Core Needs Firmware Volumes to function\r
   //\r
   FvHob.Raw = GetHobList ();\r
   while ((FvHob.Raw = GetNextHob (EFI_HOB_TYPE_FV, FvHob.Raw)) != NULL) {\r
+    AuthenticationStatus = 0;\r
+    //\r
+    // Get the authentication status propagated from PEI-phase to DXE.\r
+    //\r
+    Fv3Hob.Raw = GetHobList ();\r
+    while ((Fv3Hob.Raw = GetNextHob (EFI_HOB_TYPE_FV3, Fv3Hob.Raw)) != NULL) {\r
+      if ((Fv3Hob.FirmwareVolume3->BaseAddress == FvHob.FirmwareVolume->BaseAddress) &&\r
+          (Fv3Hob.FirmwareVolume3->Length == FvHob.FirmwareVolume->Length)) {\r
+        AuthenticationStatus = Fv3Hob.FirmwareVolume3->AuthenticationStatus;\r
+        break;\r
+      }\r
+      Fv3Hob.Raw = GET_NEXT_HOB (Fv3Hob);\r
+    }\r
     //\r
     // Produce an FVB protocol for it\r
     //\r
-    ProduceFVBProtocolOnBuffer (FvHob.FirmwareVolume->BaseAddress, FvHob.FirmwareVolume->Length, NULL, NULL);\r
+    ProduceFVBProtocolOnBuffer (FvHob.FirmwareVolume->BaseAddress, FvHob.FirmwareVolume->Length, NULL, AuthenticationStatus, NULL);\r
     FvHob.Raw = GET_NEXT_HOB (FvHob);\r
   }\r
 \r
@@ -553,6 +666,10 @@ FwVolBlockDriverInit (
   particular, it can be called by BDS to process a single firmware\r
   volume found in a capsule.\r
 \r
+  Caution: The caller need validate the input firmware volume to follow\r
+  PI specification.\r
+  DxeCore will trust the input data and process firmware volume directly.\r
+\r
   @param  FvHeader               pointer to a firmware volume header\r
   @param  Size                   the size of the buffer pointed to by FvHeader\r
   @param  FVProtocolHandle       the handle on which a firmware volume protocol\r
@@ -566,6 +683,7 @@ FwVolBlockDriverInit (
 \r
 **/\r
 EFI_STATUS\r
+EFIAPI\r
 CoreProcessFirmwareVolume (\r
   IN VOID                             *FvHeader,\r
   IN UINTN                            Size,\r
@@ -580,6 +698,7 @@ CoreProcessFirmwareVolume (
             (EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader,\r
             (UINT64)Size,\r
             NULL,\r
+            0,\r
             FVProtocolHandle\r
             );\r
   //\r
@@ -591,6 +710,7 @@ CoreProcessFirmwareVolume (
   // somehow.\r
   //\r
   if (!EFI_ERROR(Status)) {\r
+    ASSERT (*FVProtocolHandle != NULL);\r
     Ptr = NULL;\r
     Status = CoreHandleProtocol (*FVProtocolHandle, &gEfiFirmwareVolume2ProtocolGuid, (VOID **) &Ptr);\r
     if (EFI_ERROR(Status) || (Ptr == NULL)) {\r