]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Variable/RuntimeDxe/Reclaim.c
Add SMM Variable implementation.
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / Reclaim.c
index ed68f12017f737270d28ee2949e14b4a075134b7..11bf1a02b6f6a5299479d9d08dfb1f9694f4817e 100644 (file)
@@ -3,8 +3,8 @@
   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 - 2010, 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,71 +14,25 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 **/\r
 \r
-\r
 #include "Variable.h"\r
-#include <VariableFormat.h>\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
+  Gets LBA of block and offset by given address.\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
+  This function gets the Logical Block Address (LBA) of a firmware\r
+  volume block containing the given address, and the offset of the\r
+  address on the block.\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
+  @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
 \r
-  FreePool (HandleBuffer);\r
-  return Status;\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
 \r
+**/\r
 EFI_STATUS\r
 GetLbaAndOffsetByAddress (\r
   IN  EFI_PHYSICAL_ADDRESS   Address,\r
@@ -87,7 +41,6 @@ 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
@@ -96,25 +49,17 @@ GetLbaAndOffsetByAddress (
 \r
   *Lba    = (EFI_LBA) (-1);\r
   *Offset = 0;\r
-\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
@@ -124,91 +69,85 @@ GetLbaAndOffsetByAddress (
   FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);\r
 \r
   //\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
+  // Get the (LBA, Offset) of Address.\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
   return EFI_ABORTED;\r
 }\r
 \r
+/**\r
+  Writes a buffer to variable storage space, in the working block.\r
+\r
+  This function writes a buffer to variable storage space into a 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
+\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
   )\r
-/*++\r
-\r
-Routine Description:\r
-    Write a buffer to Variable space, in the working block.\r
-\r
-Arguments:\r
-    FvbHandle        - Indicates a handle to FVB to access variable store\r
-    Buffer           - Point to the input buffer\r
-    BufferSize       - The number of bytes of the input Buffer\r
-\r
-Returns:\r
-    EFI_SUCCESS            - The function completed successfully\r
-    EFI_ABORTED            - The function could not complete successfully\r
-    EFI_NOT_FOUND          - Locate FVB protocol by handle fails\r
-\r
---*/\r
 {\r
-  EFI_STATUS            Status;\r
-  EFI_HANDLE            FvbHandle;\r
-  EFI_FTW_LITE_PROTOCOL *FtwLiteProtocol;\r
-  EFI_LBA               VarLba;\r
-  UINTN                 VarOffset;\r
-  UINT8                 *FtwBuffer;\r
-  UINTN                 FtwBufferSize;\r
-\r
-  //\r
-  // Locate fault tolerant write protocol\r
-  //\r
-  Status = gBS->LocateProtocol (\r
-                  &gEfiFaultTolerantWriteLiteProtocolGuid,\r
-                  NULL,\r
-                  (VOID **) &FtwLiteProtocol\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
+  //\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
+  // Prepare for the variable data.\r
   //\r
   FtwBufferSize = ((VARIABLE_STORE_HEADER *) ((UINTN) VariableBase))->Size;\r
-  FtwBuffer     = AllocateRuntimePool (FtwBufferSize);\r
+  FtwBuffer     = AllocatePool (FtwBufferSize);\r
   if (FtwBuffer == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
@@ -217,16 +156,17 @@ Returns:
   CopyMem (FtwBuffer, Buffer, BufferSize);\r
 \r
   //\r
-  // FTW write record\r
+  // FTW write record.\r
   //\r
-  Status = FtwLiteProtocol->Write (\r
-                              FtwLiteProtocol,\r
-                              FvbHandle,\r
-                              VarLba,         // LBA\r
-                              VarOffset,      // Offset\r
-                              &FtwBufferSize, // NumBytes,\r
-                              FtwBuffer\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
 \r
   FreePool (FtwBuffer);\r
   return Status;\r