]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Variable/RuntimeDxe/Reclaim.c
MdeModulePkg Variable: Handle ftw driver executes prior to variable driver
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / Reclaim.c
CommitLineData
504214c4 1/** @file\r
504214c4
LG
2 Handles non-volatile variable store garbage collection, using FTW\r
3 (Fault Tolerant Write) protocol.\r
8d3a5c82 4\r
fa0737a8 5Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
e5eed7d3 6This program and the accompanying materials\r
8d3a5c82 7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
504214c4 14**/\r
8d3a5c82 15\r
3cfb790c 16#include "Variable.h"\r
8d3a5c82 17\r
7c80e839 18/**\r
19 Gets LBA of block and offset by given address.\r
20\r
8a2d4996 21 This function gets the Logical Block Address (LBA) of a firmware\r
22 volume block containing the given address, and the offset of the\r
7c80e839 23 address on the block.\r
24\r
25 @param Address Address which should be contained\r
8a2d4996 26 by returned FVB handle.\r
27 @param Lba Pointer to LBA for output.\r
28 @param Offset Pointer to offset for output.\r
7c80e839 29\r
8a2d4996 30 @retval EFI_SUCCESS LBA and offset successfully returned.\r
31 @retval EFI_NOT_FOUND Fail to find FVB handle by address.\r
32 @retval EFI_ABORTED Fail to find valid LBA and offset.\r
7c80e839 33\r
34**/\r
8d3a5c82 35EFI_STATUS\r
36GetLbaAndOffsetByAddress (\r
37 IN EFI_PHYSICAL_ADDRESS Address,\r
38 OUT EFI_LBA *Lba,\r
39 OUT UINTN *Offset\r
40 )\r
41{\r
42 EFI_STATUS Status;\r
8d3a5c82 43 EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r
44 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
45 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
46 EFI_FV_BLOCK_MAP_ENTRY *FvbMapEntry;\r
47 UINT32 LbaIndex;\r
48\r
4e1005ec 49 Fvb = NULL;\r
8d3a5c82 50 *Lba = (EFI_LBA) (-1);\r
51 *Offset = 0;\r
fa0737a8 52\r
8d3a5c82 53 //\r
8a2d4996 54 // Get the proper FVB protocol.\r
8d3a5c82 55 //\r
8a2d4996 56 Status = GetFvbInfoByAddress (Address, NULL, &Fvb);\r
8d3a5c82 57 if (EFI_ERROR (Status)) {\r
58 return Status;\r
59 }\r
60\r
8d3a5c82 61 //\r
8a2d4996 62 // Get the Base Address of FV.\r
8d3a5c82 63 //\r
64 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
65 if (EFI_ERROR (Status)) {\r
66 return Status;\r
67 }\r
68\r
69 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);\r
70\r
71 //\r
8a2d4996 72 // Get the (LBA, Offset) of Address.\r
8d3a5c82 73 //\r
8a2d4996 74 if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) {\r
75 //\r
76 // BUGBUG: Assume one FV has one type of BlockLength.\r
77 //\r
78 FvbMapEntry = &FwVolHeader->BlockMap[0];\r
79 for (LbaIndex = 1; LbaIndex <= FvbMapEntry->NumBlocks; LbaIndex += 1) {\r
80 if (Address < (FvbBaseAddress + FvbMapEntry->Length * LbaIndex)) {\r
81 //\r
82 // Found the (Lba, Offset).\r
83 //\r
84 *Lba = LbaIndex - 1;\r
85 *Offset = (UINTN) (Address - (FvbBaseAddress + FvbMapEntry->Length * (LbaIndex - 1)));\r
86 return EFI_SUCCESS;\r
87 }\r
8d3a5c82 88 }\r
89 }\r
90\r
91 return EFI_ABORTED;\r
92}\r
93\r
7c80e839 94/**\r
95 Writes a buffer to variable storage space, in the working block.\r
96\r
8a2d4996 97 This function writes a buffer to variable storage space into a firmware\r
7c80e839 98 volume block device. The destination is specified by parameter\r
99 VariableBase. Fault Tolerant Write protocol is used for writing.\r
100\r
101 @param VariableBase Base address of variable to write\r
128ef095 102 @param VariableBuffer Point to the variable data buffer.\r
7c80e839 103\r
8a2d4996 104 @retval EFI_SUCCESS The function completed successfully.\r
105 @retval EFI_NOT_FOUND Fail to locate Fault Tolerant Write protocol.\r
106 @retval EFI_ABORTED The function could not complete successfully.\r
7c80e839 107\r
108**/\r
8d3a5c82 109EFI_STATUS\r
110FtwVariableSpace (\r
111 IN EFI_PHYSICAL_ADDRESS VariableBase,\r
128ef095 112 IN VARIABLE_STORE_HEADER *VariableBuffer\r
8d3a5c82 113 )\r
8d3a5c82 114{\r
88a5561c
LG
115 EFI_STATUS Status;\r
116 EFI_HANDLE FvbHandle;\r
117 EFI_LBA VarLba;\r
118 UINTN VarOffset;\r
88a5561c
LG
119 UINTN FtwBufferSize;\r
120 EFI_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r
8d3a5c82 121\r
122 //\r
8a2d4996 123 // Locate fault tolerant write protocol.\r
8d3a5c82 124 //\r
8a2d4996 125 Status = GetFtwProtocol((VOID **) &FtwProtocol);\r
8d3a5c82 126 if (EFI_ERROR (Status)) {\r
127 return EFI_NOT_FOUND;\r
128 }\r
129 //\r
8a2d4996 130 // Locate Fvb handle by address.\r
8d3a5c82 131 //\r
8a2d4996 132 Status = GetFvbInfoByAddress (VariableBase, &FvbHandle, NULL);\r
8d3a5c82 133 if (EFI_ERROR (Status)) {\r
134 return Status;\r
135 }\r
136 //\r
8a2d4996 137 // Get LBA and Offset by address.\r
8d3a5c82 138 //\r
139 Status = GetLbaAndOffsetByAddress (VariableBase, &VarLba, &VarOffset);\r
140 if (EFI_ERROR (Status)) {\r
141 return EFI_ABORTED;\r
142 }\r
8d3a5c82 143\r
128ef095
SZ
144 FtwBufferSize = ((VARIABLE_STORE_HEADER *) ((UINTN) VariableBase))->Size;\r
145 ASSERT (FtwBufferSize == VariableBuffer->Size);\r
8d3a5c82 146\r
147 //\r
8a2d4996 148 // FTW write record.\r
8d3a5c82 149 //\r
88a5561c 150 Status = FtwProtocol->Write (\r
8a2d4996 151 FtwProtocol,\r
152 VarLba, // LBA\r
153 VarOffset, // Offset\r
154 FtwBufferSize, // NumBytes\r
155 NULL, // PrivateData NULL\r
156 FvbHandle, // Fvb Handle\r
128ef095 157 (VOID *) VariableBuffer // write buffer\r
8a2d4996 158 );\r
8d3a5c82 159\r
8d3a5c82 160 return Status;\r
161}\r