]> git.proxmox.com Git - mirror_edk2.git/commitdiff
OvmfPkg/EmuVariableFvbRuntimeDxe: change block size to 4KB
authorLaszlo Ersek <lersek@redhat.com>
Sat, 6 May 2017 11:17:58 +0000 (13:17 +0200)
committerLaszlo Ersek <lersek@redhat.com>
Thu, 18 May 2017 21:38:45 +0000 (23:38 +0200)
EmuVariableFvbRuntimeDxe currently produces a Firmware Volume Block
protocol that is based on a block map of two blocks, each block having
PcdFlashNvStorageFtwSpareSize for size.

(The total size is 2 * PcdFlashNvStorageFtwSpareSize.)

FaultTolerantWriteDxe in turn expects the block size to be a power of two.

In the 4MB build of OVMF, PcdFlashNvStorageFtwSpareSize is 264KB, which is
not a power of two. In order to equip EmuVariableFvbRuntimeDxe for this
build, shrink the block size to 4KB (EFI_PAGE_SIZE), and grow the block
count from 2 to EFI_SIZE_TO_PAGES(2 * PcdFlashNvStorageFtwSpareSize). The
total size remains

  2 * PcdFlashNvStorageFtwSpareSize
  --------------------------------- * EFI_PAGE_SIZE
            EFI_PAGE_SIZE

Right now EmuVariableFvbRuntimeDxe open-codes the block count of 2 in
various limit checks, so introduce a few new macros:
- EMU_FVB_NUM_TOTAL_BLOCKS, for the LHS of the above product,
- EMU_FVB_NUM_SPARE_BLOCKS for the half of that.

Also rework the FVB protocol members to support an arbitrary count of
blocks.

Keep the invariant intact that the first half of the firmware volume hosts
the variable store and the FTW working block, and that the second half
maps the FTW spare area.

Cc: Jordan Justen <jordan.l.justen@intel.com>
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=527
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Regression-tested-by: Gary Lin <glin@suse.com>
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
OvmfPkg/EmuVariableFvbRuntimeDxe/Fvb.c
OvmfPkg/EmuVariableFvbRuntimeDxe/Fvb.h

index 30f69b999ab0b107dd2a2ede39acc93baaeb618c..2d106bb50bed68d2739204c86a0567e37426db60 100644 (file)
@@ -74,8 +74,8 @@ EFI_FW_VOL_BLOCK_DEVICE mEmuVarsFvb = {
     }\r
   },\r
   NULL, // BufferPtr\r
-  FixedPcdGet32 (PcdFlashNvStorageFtwSpareSize), // BlockSize\r
-  2 * FixedPcdGet32 (PcdFlashNvStorageFtwSpareSize), // Size\r
+  EMU_FVB_BLOCK_SIZE, // BlockSize\r
+  EMU_FVB_SIZE, // Size\r
   {     // FwVolBlockInstance\r
     FvbProtocolGetAttributes,\r
     FvbProtocolSetAttributes,\r
@@ -185,14 +185,14 @@ FvbProtocolGetBlockSize (
 {\r
   EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
 \r
-  if (Lba > 1) {\r
+  if (Lba >= EMU_FVB_NUM_TOTAL_BLOCKS) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
   FvbDevice = FVB_DEVICE_FROM_THIS (This);\r
 \r
   *BlockSize = FvbDevice->BlockSize;\r
-  *NumberOfBlocks = (UINTN) (2 - (UINTN) Lba);\r
+  *NumberOfBlocks = (UINTN)(EMU_FVB_NUM_TOTAL_BLOCKS - Lba);\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -322,68 +322,58 @@ FvbProtocolEraseBlocks (
   )\r
 {\r
   EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
-  VA_LIST                 args;\r
+  VA_LIST                 Args;\r
   EFI_LBA                 StartingLba;\r
   UINTN                   NumOfLba;\r
-  UINT8                   Erase;\r
-  VOID                    *ErasePtr;\r
+  UINT8                   *ErasePtr;\r
   UINTN                   EraseSize;\r
 \r
   FvbDevice = FVB_DEVICE_FROM_THIS (This);\r
-  Erase = 0;\r
-\r
-  VA_START (args, This);\r
 \r
+  //\r
+  // Check input parameters\r
+  //\r
+  VA_START (Args, This);\r
   do {\r
-    StartingLba = VA_ARG (args, EFI_LBA);\r
+    StartingLba = VA_ARG (Args, EFI_LBA);\r
     if (StartingLba == EFI_LBA_LIST_TERMINATOR) {\r
       break;\r
     }\r
+    NumOfLba = VA_ARG (Args, UINTN);\r
 \r
-    NumOfLba = VA_ARG (args, UINTN);\r
-\r
-    //\r
-    // Check input parameters\r
-    //\r
-    if ((NumOfLba == 0) || (StartingLba > 1) || ((StartingLba + NumOfLba) > 2)) {\r
-      VA_END (args);\r
+    if (StartingLba > EMU_FVB_NUM_TOTAL_BLOCKS ||\r
+        NumOfLba > EMU_FVB_NUM_TOTAL_BLOCKS - StartingLba) {\r
+      VA_END (Args);\r
       return EFI_INVALID_PARAMETER;\r
     }\r
-\r
-    if (StartingLba == 0) {\r
-      Erase = (UINT8) (Erase | BIT0);\r
-    }\r
-    if ((StartingLba + NumOfLba) == 2) {\r
-      Erase = (UINT8) (Erase | BIT1);\r
-    }\r
-\r
   } while (1);\r
+  VA_END (Args);\r
 \r
-  VA_END (args);\r
-\r
-  ErasePtr = (UINT8*) FvbDevice->BufferPtr;\r
-  EraseSize = 0;\r
+  //\r
+  // Erase blocks\r
+  //\r
+  VA_START (Args, This);\r
+  do {\r
+    StartingLba = VA_ARG (Args, EFI_LBA);\r
+    if (StartingLba == EFI_LBA_LIST_TERMINATOR) {\r
+      break;\r
+    }\r
+    NumOfLba = VA_ARG (Args, UINTN);\r
 \r
-  if ((Erase & BIT0) != 0) {\r
-    EraseSize = EraseSize + FvbDevice->BlockSize;\r
-  } else {\r
-    ErasePtr = (VOID*) ((UINT8*)ErasePtr + FvbDevice->BlockSize);\r
-  }\r
+    ErasePtr = FvbDevice->BufferPtr;\r
+    ErasePtr += (UINTN)StartingLba * FvbDevice->BlockSize;\r
+    EraseSize = NumOfLba * FvbDevice->BlockSize;\r
 \r
-  if ((Erase & BIT1) != 0) {\r
-    EraseSize = EraseSize + FvbDevice->BlockSize;\r
-  }\r
+    SetMem (ErasePtr, EraseSize, ERASED_UINT8);\r
+  } while (1);\r
+  VA_END (Args);\r
 \r
-  if (EraseSize != 0) {\r
-    SetMem (\r
-      (VOID*) ErasePtr,\r
-      EraseSize,\r
-      ERASED_UINT8\r
-      );\r
-    VA_START (args, This);\r
-    PlatformFvbBlocksErased (This, args);\r
-    VA_END (args);\r
-  }\r
+  //\r
+  // Call platform hook\r
+  //\r
+  VA_START (Args, This);\r
+  PlatformFvbBlocksErased (This, Args);\r
+  VA_END (Args);\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -458,31 +448,30 @@ FvbProtocolWrite (
   IN        UINT8                               *Buffer\r
   )\r
 {\r
-\r
   EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
   UINT8                   *FvbDataPtr;\r
+  EFI_STATUS              Status;\r
 \r
   FvbDevice = FVB_DEVICE_FROM_THIS (This);\r
 \r
-  if ((Lba > 1) || (Offset > FvbDevice->BlockSize)) {\r
+  if (Lba >= EMU_FVB_NUM_TOTAL_BLOCKS ||\r
+      Offset > FvbDevice->BlockSize) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if ((Offset + *NumBytes) > FvbDevice->BlockSize) {\r
+  Status = EFI_SUCCESS;\r
+  if (*NumBytes > FvbDevice->BlockSize - Offset) {\r
     *NumBytes = FvbDevice->BlockSize - Offset;\r
+    Status = EFI_BAD_BUFFER_SIZE;\r
   }\r
 \r
-  FvbDataPtr =\r
-    (UINT8*) FvbDevice->BufferPtr +\r
-    MultU64x32 (Lba, (UINT32) FvbDevice->BlockSize) +\r
-    Offset;\r
-\r
-  if (*NumBytes > 0) {\r
-    CopyMem (FvbDataPtr, Buffer, *NumBytes);\r
-    PlatformFvbDataWritten (This, Lba, Offset, *NumBytes, Buffer);\r
-  }\r
+  FvbDataPtr = FvbDevice->BufferPtr;\r
+  FvbDataPtr += (UINTN)Lba * FvbDevice->BlockSize;\r
+  FvbDataPtr += Offset;\r
 \r
-  return EFI_SUCCESS;\r
+  CopyMem (FvbDataPtr, Buffer, *NumBytes);\r
+  PlatformFvbDataWritten (This, Lba, Offset, *NumBytes, Buffer);\r
+  return Status;\r
 }\r
 \r
 \r
@@ -545,28 +534,28 @@ FvbProtocolRead (
 {\r
   EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
   UINT8                   *FvbDataPtr;\r
+  EFI_STATUS              Status;\r
 \r
   FvbDevice = FVB_DEVICE_FROM_THIS (This);\r
 \r
-  if ((Lba > 1) || (Offset > FvbDevice->BlockSize)) {\r
+  if (Lba >= EMU_FVB_NUM_TOTAL_BLOCKS ||\r
+      Offset > FvbDevice->BlockSize) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if ((Offset + *NumBytes) > FvbDevice->BlockSize) {\r
+  Status = EFI_SUCCESS;\r
+  if (*NumBytes > FvbDevice->BlockSize - Offset) {\r
     *NumBytes = FvbDevice->BlockSize - Offset;\r
+    Status = EFI_BAD_BUFFER_SIZE;\r
   }\r
 \r
-  FvbDataPtr =\r
-    (UINT8*) FvbDevice->BufferPtr +\r
-    MultU64x32 (Lba, (UINT32) FvbDevice->BlockSize) +\r
-    Offset;\r
+  FvbDataPtr = FvbDevice->BufferPtr;\r
+  FvbDataPtr += (UINTN)Lba * FvbDevice->BlockSize;\r
+  FvbDataPtr += Offset;\r
 \r
-  if (*NumBytes > 0) {\r
-    CopyMem (Buffer, FvbDataPtr, *NumBytes);\r
-    PlatformFvbDataRead (This, Lba, Offset, *NumBytes, Buffer);\r
-  }\r
-\r
-  return EFI_SUCCESS;\r
+  CopyMem (Buffer, FvbDataPtr, *NumBytes);\r
+  PlatformFvbDataRead (This, Lba, Offset, *NumBytes, Buffer);\r
+  return Status;\r
 }\r
 \r
 \r
@@ -663,7 +652,7 @@ InitializeFvAndVariableStoreHeaders (
       // EFI_FV_BLOCK_MAP_ENTRY    BlockMap[1];\r
       {\r
         {\r
-          2, // UINT32 NumBlocks;\r
+          EMU_FVB_NUM_TOTAL_BLOCKS, // UINT32 NumBlocks;\r
           EMU_FVB_BLOCK_SIZE  // UINT32 Length;\r
         }\r
       }\r
@@ -741,11 +730,13 @@ FvbInitialize (
   //\r
   // Verify that the PCD's are set correctly.\r
   //\r
+  ASSERT (FixedPcdGet32 (PcdFlashNvStorageFtwSpareSize) %\r
+          EMU_FVB_BLOCK_SIZE == 0);\r
   if (\r
        (PcdGet32 (PcdVariableStoreSize) +\r
         PcdGet32 (PcdFlashNvStorageFtwWorkingSize)\r
        ) >\r
-       EMU_FVB_BLOCK_SIZE\r
+       EMU_FVB_NUM_SPARE_BLOCKS * EMU_FVB_BLOCK_SIZE\r
      ) {\r
     DEBUG ((EFI_D_ERROR, "EMU Variable invalid PCD sizes\n"));\r
     return EFI_INVALID_PARAMETER;\r
@@ -779,10 +770,7 @@ FvbInitialize (
       Initialize = FALSE;\r
     }\r
   } else {\r
-    Ptr = AllocateAlignedRuntimePages (\r
-            EFI_SIZE_TO_PAGES (EMU_FVB_SIZE),\r
-            SIZE_64KB\r
-            );\r
+    Ptr = AllocateRuntimePages (EFI_SIZE_TO_PAGES (EMU_FVB_SIZE));\r
   }\r
 \r
   mEmuVarsFvb.BufferPtr = Ptr;\r
@@ -808,7 +796,8 @@ FvbInitialize (
   //\r
   // Initialize the Fault Tolerant Write spare block\r
   //\r
-  SubPtr = (VOID*) ((UINT8*) Ptr + EMU_FVB_BLOCK_SIZE);\r
+  SubPtr = (VOID*) ((UINT8*) Ptr +\r
+                    EMU_FVB_NUM_SPARE_BLOCKS * EMU_FVB_BLOCK_SIZE);\r
   PcdStatus = PcdSet32S (PcdFlashNvStorageFtwSpareBase,\r
                 (UINT32)(UINTN) SubPtr);\r
   ASSERT_RETURN_ERROR (PcdStatus);\r
index 4247d21d72f8109facabae7c2052c224c4d228e4..beb11e3f9a90a95354827d88e5e70be402d0ae96 100644 (file)
@@ -58,8 +58,14 @@ typedef struct {
 //\r
 // Constants\r
 //\r
-#define EMU_FVB_BLOCK_SIZE (FixedPcdGet32 (PcdFlashNvStorageFtwSpareSize))\r
-#define EMU_FVB_SIZE (2 * FixedPcdGet32 (PcdFlashNvStorageFtwSpareSize))\r
+#define EMU_FVB_BLOCK_SIZE \\r
+  EFI_PAGE_SIZE\r
+#define EMU_FVB_NUM_SPARE_BLOCKS \\r
+  EFI_SIZE_TO_PAGES ((UINTN)FixedPcdGet32 (PcdFlashNvStorageFtwSpareSize))\r
+#define EMU_FVB_NUM_TOTAL_BLOCKS \\r
+  (2 * EMU_FVB_NUM_SPARE_BLOCKS)\r
+#define EMU_FVB_SIZE \\r
+  (EMU_FVB_NUM_TOTAL_BLOCKS * EMU_FVB_BLOCK_SIZE)\r
 #define FTW_WRITE_QUEUE_SIZE \\r
   (FixedPcdGet32 (PcdFlashNvStorageFtwWorkingSize) - \\r
    sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER))\r