]> git.proxmox.com Git - mirror_edk2.git/blobdiff - IntelFrameworkPkg/Library/PeiHobLibFramework/HobLib.c
IntelFrameworkPkg PeiHobLib: Check FV alignment when building FV HOB
[mirror_edk2.git] / IntelFrameworkPkg / Library / PeiHobLibFramework / HobLib.c
index b41536e2a15adc01d76e2ab6568bdabd75261a1a..223e56a9db120ca6890a0e7c8aa653bf33b04183 100644 (file)
@@ -5,7 +5,7 @@
  This library instance uses EFI_HOB_TYPE_CV defined in Intel framework HOB specification v0.9\r
  to implement HobLib BuildCvHob() API. \r
 \r
-Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2007 - 2016, 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
@@ -475,6 +475,62 @@ BuildGuidDataHob (
   return CopyMem (HobData, Data, DataLength);\r
 }\r
 \r
+/**\r
+  Check FV alignment.\r
+\r
+  @param  BaseAddress   The base address of the Firmware Volume.\r
+  @param  Length        The size of the Firmware Volume in bytes.\r
+\r
+  @retval TRUE          FvImage buffer is at its required alignment.\r
+  @retval FALSE         FvImage buffer is not at its required alignment.\r
+\r
+**/\r
+BOOLEAN\r
+InternalCheckFvAlignment (\r
+  IN EFI_PHYSICAL_ADDRESS       BaseAddress,\r
+  IN UINT64                     Length\r
+  )\r
+{\r
+  EFI_FIRMWARE_VOLUME_HEADER    *FwVolHeader;\r
+  UINT32                        FvAlignment;\r
+\r
+  FvAlignment = 0;\r
+  FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) BaseAddress;\r
+\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 ((FwVolHeader->Attributes & EFI_FVB2_WEAK_ALIGNMENT) != EFI_FVB2_WEAK_ALIGNMENT) {\r
+    //\r
+    // Get FvHeader alignment\r
+    //\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 FALSE;\r
+    }\r
+  }\r
+\r
+  return TRUE;\r
+}\r
+\r
 /**\r
   Builds a Firmware Volume HOB.\r
 \r
@@ -483,6 +539,7 @@ BuildGuidDataHob (
   for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
   \r
   If there is no additional space for HOB creation, then ASSERT().\r
+  If the FvImage buffer is not at its required alignment, then ASSERT().\r
 \r
   @param  BaseAddress   The base address of the Firmware Volume.\r
   @param  Length        The size of the Firmware Volume in bytes.\r
@@ -497,6 +554,11 @@ BuildFvHob (
 {\r
   EFI_HOB_FIRMWARE_VOLUME  *Hob;\r
 \r
+  if (!InternalCheckFvAlignment (BaseAddress, Length)) {\r
+    ASSERT (FALSE);\r
+    return;\r
+  }\r
+\r
   Hob = InternalPeiCreateHob (EFI_HOB_TYPE_FV, (UINT16) sizeof (EFI_HOB_FIRMWARE_VOLUME));\r
   if (Hob == NULL) {\r
     return;\r
@@ -514,6 +576,7 @@ BuildFvHob (
   for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
   \r
   If there is no additional space for HOB creation, then ASSERT().\r
+  If the FvImage buffer is not at its required alignment, then ASSERT().\r
 \r
   @param  BaseAddress   The base address of the Firmware Volume.\r
   @param  Length        The size of the Firmware Volume in bytes.\r
@@ -532,6 +595,11 @@ BuildFv2Hob (
 {\r
   EFI_HOB_FIRMWARE_VOLUME2  *Hob;\r
 \r
+  if (!InternalCheckFvAlignment (BaseAddress, Length)) {\r
+    ASSERT (FALSE);\r
+    return;\r
+  }\r
+\r
   Hob = InternalPeiCreateHob (EFI_HOB_TYPE_FV2, (UINT16) sizeof (EFI_HOB_FIRMWARE_VOLUME2));\r
   if (Hob == NULL) {\r
     return;\r