]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/EmuVariableFvbRuntimeDxe/Fvb.c
OvmfPkg/EmuVariableFvbRuntimeDxe: stop using PcdVariableStoreSize
[mirror_edk2.git] / OvmfPkg / EmuVariableFvbRuntimeDxe / Fvb.c
index 7a6d3153ec8ca6255c2a4168e819c4ba5468c550..9480d879c9353cb518daabeb174242b68108b8ce 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
@@ -120,14 +120,14 @@ FvbVirtualAddressChangeEvent (
   only for memory-mapped firmware volumes.\r
 \r
   @param This     Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.\r
-  \r
+\r
   @param Address  Pointer to a caller-allocated\r
                   EFI_PHYSICAL_ADDRESS that, on successful\r
                   return from GetPhysicalAddress(), contains the\r
                   base address of the firmware volume.\r
-  \r
+\r
   @retval EFI_SUCCESS       The firmware volume base address is returned.\r
-  \r
+\r
   @retval EFI_NOT_SUPPORTED The firmware volume is not memory mapped.\r
 \r
 **/\r
@@ -168,9 +168,9 @@ FvbProtocolGetPhysicalAddress (
                         blocks in this range have a size of\r
                         BlockSize.\r
 \r
-  \r
+\r
   @retval EFI_SUCCESS             The firmware volume base address is returned.\r
-  \r
+\r
   @retval EFI_INVALID_PARAMETER   The requested LBA is out of range.\r
 \r
 **/\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
@@ -246,7 +246,7 @@ FvbProtocolGetAttributes (
                       settings of the firmware volume. Type\r
                       EFI_FVB_ATTRIBUTES_2 is defined in\r
                       EFI_FIRMWARE_VOLUME_HEADER.\r
-  \r
+\r
   @retval EFI_SUCCESS           The firmware volume attributes were returned.\r
 \r
   @retval EFI_INVALID_PARAMETER The attributes requested are in\r
@@ -302,7 +302,7 @@ FvbProtocolSetAttributes (
 \r
   @retval EFI_SUCCESS The erase request was successfully\r
                       completed.\r
-  \r
+\r
   @retval EFI_ACCESS_DENIED   The firmware volume is in the\r
                               WriteDisabled state.\r
   @retval EFI_DEVICE_ERROR  The block device is not functioning\r
@@ -311,7 +311,7 @@ FvbProtocolSetAttributes (
                             partially erased.\r
   @retval EFI_INVALID_PARAMETER One or more of the LBAs listed\r
                                 in the variable argument list do\r
-                                not exist in the firmware volume.  \r
+                                not exist in the firmware volume.\r
 \r
 **/\r
 EFI_STATUS\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, UINT32);\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
@@ -420,29 +410,29 @@ FvbProtocolEraseBlocks (
   returns.\r
 \r
   @param This     Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.\r
-  \r
+\r
   @param Lba      The starting logical block index to write to.\r
-  \r
+\r
   @param Offset   Offset into the block at which to begin writing.\r
-  \r
+\r
   @param NumBytes Pointer to a UINTN. At entry, *NumBytes\r
                   contains the total size of the buffer. At\r
                   exit, *NumBytes contains the total number of\r
                   bytes actually written.\r
-  \r
+\r
   @param Buffer   Pointer to a caller-allocated buffer that\r
                   contains the source for the write.\r
-  \r
+\r
   @retval EFI_SUCCESS         The firmware volume was written successfully.\r
-  \r
+\r
   @retval EFI_BAD_BUFFER_SIZE The write was attempted across an\r
                               LBA boundary. On output, NumBytes\r
                               contains the total number of bytes\r
                               actually written.\r
-  \r
+\r
   @retval EFI_ACCESS_DENIED   The firmware volume is in the\r
                               WriteDisabled state.\r
-  \r
+\r
   @retval EFI_DEVICE_ERROR    The block device is malfunctioning\r
                               and could not be written.\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
@@ -503,7 +492,7 @@ FvbProtocolWrite (
   aware that a read may be partially completed.\r
 \r
   @param This     Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.\r
-  \r
+\r
   @param Lba      The starting logical block index\r
                   from which to read.\r
 \r
@@ -519,15 +508,15 @@ FvbProtocolWrite (
 \r
   @retval EFI_SUCCESS         The firmware volume was read successfully\r
                               and contents are in Buffer.\r
-  \r
+\r
   @retval EFI_BAD_BUFFER_SIZE Read attempted across an LBA\r
                               boundary. On output, NumBytes\r
                               contains the total number of bytes\r
                               returned in Buffer.\r
-  \r
+\r
   @retval EFI_ACCESS_DENIED   The firmware volume is in the\r
                               ReadDisabled state.\r
-  \r
+\r
   @retval EFI_DEVICE_ERROR    The block device is not\r
                               functioning correctly and could\r
                               not be read.\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
-\r
-  if (*NumBytes > 0) {\r
-    CopyMem (Buffer, FvbDataPtr, *NumBytes);\r
-    PlatformFvbDataRead (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 (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
@@ -676,7 +665,7 @@ InitializeFvAndVariableStoreHeaders (
 \r
       // UINT32  Size;\r
       (\r
-        FixedPcdGet32 (PcdVariableStoreSize) -\r
+        FixedPcdGet32 (PcdFlashNvStorageVariableSize) -\r
         OFFSET_OF (FVB_FV_HDR_AND_VARS_TEMPLATE, VarHdr)\r
       ),\r
 \r
@@ -715,9 +704,9 @@ InitializeFvAndVariableStoreHeaders (
 /**\r
   Main entry point.\r
 \r
-  @param[in] ImageHandle    The firmware allocated handle for the EFI image.  \r
+  @param[in] ImageHandle    The firmware allocated handle for the EFI image.\r
   @param[in] SystemTable    A pointer to the EFI System Table.\r
-  \r
+\r
   @retval EFI_SUCCESS       Successfully initialized.\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 (PcdFlashNvStorageVariableSize) +\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
@@ -800,7 +788,7 @@ FvbInitialize (
   //\r
   // Initialize the Fault Tolerant Write data area\r
   //\r
-  SubPtr = (VOID*) ((UINT8*) Ptr + PcdGet32 (PcdVariableStoreSize));\r
+  SubPtr = (VOID*) ((UINT8*) Ptr + PcdGet32 (PcdFlashNvStorageVariableSize));\r
   PcdStatus = PcdSet32S (PcdFlashNvStorageFtwWorkingBase,\r
                 (UINT32)(UINTN) SubPtr);\r
   ASSERT_RETURN_ERROR (PcdStatus);\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