]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Variable/RuntimeDxe/Reclaim.c
MdeModulePkg VariableSmm: Check InfoSize correctly
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / Reclaim.c
index 9c351a3547e748b1178bb71f4c5aae60bbd5065b..616812ccd8d8f9ddcbdd6f9882545c30ba8c0a8a 100644 (file)
@@ -1,10 +1,9 @@
 /** @file\r
-  \r
   Handles non-volatile variable store garbage collection, using FTW\r
   (Fault Tolerant Write) protocol.\r
 \r
-Copyright (c) 2006 - 2008, Intel Corporation\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2006 - 2015, 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
@@ -14,99 +13,23 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 **/\r
 \r
-\r
 #include "Variable.h"\r
 \r
-/**\r
-  Gets firmware volume block handle by given address.\r
-\r
-  This function gets firmware volume block handle whose\r
-  address range contains the parameter Address.\r
-\r
-  @param  Address        Address which should be contained\r
-                         by returned FVB handle\r
-  @param  FvbHandle      Pointer to FVB handle for output\r
-\r
-  @retval EFI_SUCCESS    FVB handle successfully returned\r
-  @retval EFI_NOT_FOUND  Fail to find FVB handle by address\r
-\r
-**/\r
-EFI_STATUS\r
-GetFvbHandleByAddress (\r
-  IN  EFI_PHYSICAL_ADDRESS   Address,\r
-  OUT EFI_HANDLE             *FvbHandle\r
-  )\r
-{\r
-  EFI_STATUS                          Status;\r
-  EFI_HANDLE                          *HandleBuffer;\r
-  UINTN                               HandleCount;\r
-  UINTN                               Index;\r
-  EFI_PHYSICAL_ADDRESS                FvbBaseAddress;\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *Fvb;\r
-  EFI_FIRMWARE_VOLUME_HEADER          *FwVolHeader;\r
-\r
-  *FvbHandle = NULL;\r
-  //\r
-  // Locate all handles of Fvb protocol\r
-  //\r
-  Status = gBS->LocateHandleBuffer (\r
-                  ByProtocol,\r
-                  &gEfiFirmwareVolumeBlockProtocolGuid,\r
-                  NULL,\r
-                  &HandleCount,\r
-                  &HandleBuffer\r
-                  );\r
-  if (EFI_ERROR (Status)) {\r
-    return EFI_NOT_FOUND;\r
-  }\r
-  //\r
-  // Get the FVB to access variable store\r
-  //\r
-  for (Index = 0; Index < HandleCount; Index += 1) {\r
-    Status = gBS->HandleProtocol (\r
-                    HandleBuffer[Index],\r
-                    &gEfiFirmwareVolumeBlockProtocolGuid,\r
-                    (VOID **) &Fvb\r
-                    );\r
-    if (EFI_ERROR (Status)) {\r
-      Status = EFI_NOT_FOUND;\r
-      break;\r
-    }\r
-    //\r
-    // Compare the address and select the right one\r
-    //\r
-    Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
-    if (EFI_ERROR (Status)) {\r
-      continue;\r
-    }\r
-\r
-    FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);\r
-    if ((Address >= FvbBaseAddress) && (Address <= (FvbBaseAddress + FwVolHeader->FvLength))) {\r
-      *FvbHandle  = HandleBuffer[Index];\r
-      Status      = EFI_SUCCESS;\r
-      break;\r
-    }\r
-  }\r
-\r
-  FreePool (HandleBuffer);\r
-  return Status;\r
-}\r
-\r
 /**\r
   Gets LBA of block and offset by given address.\r
 \r
-  This function gets the Logical Block Address (LBA) of firmware\r
-  volume block containing the given address, and the offset of\r
+  This function gets the Logical Block Address (LBA) of firmware\r
+  volume block containing the given address, and the offset of the\r
   address on the block.\r
 \r
   @param  Address        Address which should be contained\r
-                         by returned FVB handle\r
-  @param  Lba            Pointer to LBA for output\r
-  @param  Offset         Pointer to offset for output\r
+                         by returned FVB handle.\r
+  @param  Lba            Pointer to LBA for output.\r
+  @param  Offset         Pointer to offset for output.\r
 \r
-  @retval EFI_SUCCESS    LBA and offset successfully returned\r
-  @retval EFI_NOT_FOUND  Fail to find FVB handle by address\r
-  @retval EFI_ABORTED    Fail to find valid LBA and offset\r
+  @retval EFI_SUCCESS    LBA and offset successfully returned.\r
+  @retval EFI_NOT_FOUND  Fail to find FVB handle by address.\r
+  @retval EFI_ABORTED    Fail to find valid LBA and offset.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -117,34 +40,26 @@ GetLbaAndOffsetByAddress (
   )\r
 {\r
   EFI_STATUS                          Status;\r
-  EFI_HANDLE                          FvbHandle;\r
   EFI_PHYSICAL_ADDRESS                FvbBaseAddress;\r
   EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *Fvb;\r
   EFI_FIRMWARE_VOLUME_HEADER          *FwVolHeader;\r
   EFI_FV_BLOCK_MAP_ENTRY              *FvbMapEntry;\r
   UINT32                              LbaIndex;\r
 \r
+  Fvb     = NULL;\r
   *Lba    = (EFI_LBA) (-1);\r
   *Offset = 0;\r
 \r
   //\r
-  // Get the proper FVB\r
+  // Get the proper FVB protocol.\r
   //\r
-  Status = GetFvbHandleByAddress (Address, &FvbHandle);\r
+  Status = GetFvbInfoByAddress (Address, NULL, &Fvb);\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
 \r
-  Status = gBS->HandleProtocol (\r
-                  FvbHandle,\r
-                  &gEfiFirmwareVolumeBlockProtocolGuid,\r
-                  (VOID **) &Fvb\r
-                  );\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
   //\r
-  // Get the Base Address of FV\r
+  // Get the Base Address of FV.\r
   //\r
   Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
   if (EFI_ERROR (Status)) {\r
@@ -154,24 +69,22 @@ GetLbaAndOffsetByAddress (
   FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);\r
 \r
   //\r
-  // Get the (LBA, Offset) of Address\r
+  // Get the (LBA, Offset) of Address.\r
   //\r
-  if ((Address >= FvbBaseAddress) && (Address <= (FvbBaseAddress + FwVolHeader->FvLength))) {\r
-    if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) {\r
-      //\r
-      // BUGBUG: Assume one FV has one type of BlockLength\r
-      //\r
-      FvbMapEntry = &FwVolHeader->BlockMap[0];\r
-      for (LbaIndex = 1; LbaIndex <= FvbMapEntry->NumBlocks; LbaIndex += 1) {\r
-        if (Address < (FvbBaseAddress + FvbMapEntry->Length * LbaIndex)) {\r
-          //\r
-          // Found the (Lba, Offset)\r
-          //\r
-          *Lba    = LbaIndex - 1;\r
-          *Offset = (UINTN) (Address - (FvbBaseAddress + FvbMapEntry->Length * (LbaIndex - 1)));\r
-          return EFI_SUCCESS;\r
-       }\r
-      }\r
+  if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) {\r
+    //\r
+    // BUGBUG: Assume one FV has one type of BlockLength.\r
+    //\r
+    FvbMapEntry = &FwVolHeader->BlockMap[0];\r
+    for (LbaIndex = 1; LbaIndex <= FvbMapEntry->NumBlocks; LbaIndex += 1) {\r
+      if (Address < (FvbBaseAddress + FvbMapEntry->Length * LbaIndex)) {\r
+        //\r
+        // Found the (Lba, Offset).\r
+        //\r
+        *Lba    = LbaIndex - 1;\r
+        *Offset = (UINTN) (Address - (FvbBaseAddress + FvbMapEntry->Length * (LbaIndex - 1)));\r
+        return EFI_SUCCESS;\r
+     }\r
     }\r
   }\r
 \r
@@ -181,84 +94,68 @@ GetLbaAndOffsetByAddress (
 /**\r
   Writes a buffer to variable storage space, in the working block.\r
 \r
-  This function writes a buffer to variable storage space into firmware\r
+  This function writes a buffer to variable storage space into firmware\r
   volume block device. The destination is specified by parameter\r
   VariableBase. Fault Tolerant Write protocol is used for writing.\r
 \r
   @param  VariableBase   Base address of variable to write\r
-  @param  Buffer         Point to the data buffer\r
-  @param  BufferSize     The number of bytes of the data Buffer\r
+  @param  VariableBuffer Point to the variable data buffer.\r
 \r
-  @retval EFI_SUCCESS    The function completed successfully\r
-  @retval EFI_NOT_FOUND  Fail to locate Fault Tolerant Write protocol\r
-  @retval EFI_ABORTED    The function could not complete successfully\r
+  @retval EFI_SUCCESS    The function completed successfully.\r
+  @retval EFI_NOT_FOUND  Fail to locate Fault Tolerant Write protocol.\r
+  @retval EFI_ABORTED    The function could not complete successfully.\r
 \r
 **/\r
 EFI_STATUS\r
 FtwVariableSpace (\r
   IN EFI_PHYSICAL_ADDRESS   VariableBase,\r
-  IN UINT8                  *Buffer,\r
-  IN UINTN                  BufferSize\r
+  IN VARIABLE_STORE_HEADER  *VariableBuffer\r
   )\r
 {\r
   EFI_STATUS                         Status;\r
   EFI_HANDLE                         FvbHandle;\r
   EFI_LBA                            VarLba;\r
   UINTN                              VarOffset;\r
-  UINT8                              *FtwBuffer;\r
   UINTN                              FtwBufferSize;\r
   EFI_FAULT_TOLERANT_WRITE_PROTOCOL  *FtwProtocol;\r
 \r
   //\r
-  // Locate fault tolerant write protocol\r
+  // Locate fault tolerant write protocol.\r
   //\r
-  Status = gBS->LocateProtocol (\r
-                  &gEfiFaultTolerantWriteProtocolGuid,\r
-                  NULL,\r
-                  (VOID **) &FtwProtocol\r
-                  );\r
+  Status = GetFtwProtocol((VOID **) &FtwProtocol);\r
   if (EFI_ERROR (Status)) {\r
     return EFI_NOT_FOUND;\r
   }\r
   //\r
-  // Locate Fvb handle by address\r
+  // Locate Fvb handle by address.\r
   //\r
-  Status = GetFvbHandleByAddress (VariableBase, &FvbHandle);\r
+  Status = GetFvbInfoByAddress (VariableBase, &FvbHandle, NULL);\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
   //\r
-  // Get LBA and Offset by address\r
+  // Get LBA and Offset by address.\r
   //\r
   Status = GetLbaAndOffsetByAddress (VariableBase, &VarLba, &VarOffset);\r
   if (EFI_ERROR (Status)) {\r
     return EFI_ABORTED;\r
   }\r
-  //\r
-  // Prepare for the variable data\r
-  //\r
-  FtwBufferSize = ((VARIABLE_STORE_HEADER *) ((UINTN) VariableBase))->Size;\r
-  FtwBuffer     = AllocateRuntimePool (FtwBufferSize);\r
-  if (FtwBuffer == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
 \r
-  SetMem (FtwBuffer, FtwBufferSize, (UINT8) 0xff);\r
-  CopyMem (FtwBuffer, Buffer, BufferSize);\r
+  FtwBufferSize = ((VARIABLE_STORE_HEADER *) ((UINTN) VariableBase))->Size;\r
+  ASSERT (FtwBufferSize == VariableBuffer->Size);\r
 \r
   //\r
-  // FTW write record\r
+  // FTW write record.\r
   //\r
   Status = FtwProtocol->Write (\r
-                              FtwProtocol,\r
-                              VarLba,         // LBA\r
-                              VarOffset,      // Offset\r
-                              FtwBufferSize,  // NumBytes\r
-                              NULL,           // PrivateData NULL\r
-                              FvbHandle,      // Fvb Handle\r
-                              FtwBuffer       // write buffer\r
-                              );\r
+                          FtwProtocol,\r
+                          VarLba,         // LBA\r
+                          VarOffset,      // Offset\r
+                          FtwBufferSize,  // NumBytes\r
+                          NULL,           // PrivateData NULL\r
+                          FvbHandle,      // Fvb Handle\r
+                          (VOID *) VariableBuffer // write buffer\r
+                          );\r
 \r
-  FreePool (FtwBuffer);\r
   return Status;\r
 }\r