]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/VariableRuntimeDxe/reclaim.c
add English.inf, EdkFvbServiceLib.inf and Variable.inf
[mirror_edk2.git] / MdeModulePkg / Universal / VariableRuntimeDxe / reclaim.c
diff --git a/MdeModulePkg/Universal/VariableRuntimeDxe/reclaim.c b/MdeModulePkg/Universal/VariableRuntimeDxe/reclaim.c
new file mode 100644 (file)
index 0000000..309d285
--- /dev/null
@@ -0,0 +1,242 @@
+/*++\r
+\r
+Copyright (c) 2006 - 2007, Intel Corporation\r
+All rights reserved. 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
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+Module Name:\r
+\r
+  reclaim.c\r
+\r
+Abstract:\r
+\r
+  Handles non-volatile variable store garbage collection, using FTW\r
+  (Fault Tolerant Write) protocol.\r
+\r
+Revision History\r
+\r
+--*/\r
+\r
+\r
+#include "reclaim.h"\r
+#include "Common/Variable.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
+  *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
+STATIC\r
+EFI_STATUS\r
+GetLbaAndOffsetByAddress (\r
+  IN  EFI_PHYSICAL_ADDRESS   Address,\r
+  OUT EFI_LBA                *Lba,\r
+  OUT UINTN                  *Offset\r
+  )\r
+{\r
+  EFI_STATUS                            Status;\r
+  EFI_HANDLE                            FvbHandle;\r
+  EFI_PHYSICAL_ADDRESS                  FvbBaseAddress;\r
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL    *Fvb;\r
+  FRAMEWORK_EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader;\r
+  EFI_FV_BLOCK_MAP_ENTRY                *FvbMapEntry;\r
+  UINT32                                LbaIndex;\r
+\r
+  *Lba    = (EFI_LBA) (-1);\r
+  *Offset = 0;\r
+\r
+  //\r
+  // Get the proper FVB\r
+  //\r
+  Status = GetFvbHandleByAddress (Address, &FvbHandle);\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
+  //\r
+  Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  FwVolHeader = (FRAMEWORK_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->FvBlockMap[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
+\r
+  return EFI_ABORTED;\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
+  if (EFI_ERROR (Status)) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+  //\r
+  // Locate Fvb handle by address\r
+  //\r
+  Status = GetFvbHandleByAddress (VariableBase, &FvbHandle);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+  //\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
+\r
+  //\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
+\r
+  FreePool (FtwBuffer);\r
+  return Status;\r
+}\r