]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Universal/Variable/RuntimeDxe/Reclaim.c
Fix the issue that RuntimeServiceQueryVariableInfo() in VariableSmmRuntimeDxe always...
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / Reclaim.c
... / ...
CommitLineData
1/** @file\r
2 \r
3 Handles non-volatile variable store garbage collection, using FTW\r
4 (Fault Tolerant Write) protocol.\r
5\r
6Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
7This program and the accompanying materials\r
8are licensed and made available under the terms and conditions of the BSD License\r
9which accompanies this distribution. The full text of the license may be found at\r
10http://opensource.org/licenses/bsd-license.php\r
11\r
12THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15**/\r
16\r
17#include "Variable.h"\r
18\r
19/**\r
20 Gets LBA of block and offset by given address.\r
21\r
22 This function gets the Logical Block Address (LBA) of a firmware\r
23 volume block containing the given address, and the offset of the\r
24 address on the block.\r
25\r
26 @param Address Address which should be contained\r
27 by returned FVB handle.\r
28 @param Lba Pointer to LBA for output.\r
29 @param Offset Pointer to offset for output.\r
30\r
31 @retval EFI_SUCCESS LBA and offset successfully returned.\r
32 @retval EFI_NOT_FOUND Fail to find FVB handle by address.\r
33 @retval EFI_ABORTED Fail to find valid LBA and offset.\r
34\r
35**/\r
36EFI_STATUS\r
37GetLbaAndOffsetByAddress (\r
38 IN EFI_PHYSICAL_ADDRESS Address,\r
39 OUT EFI_LBA *Lba,\r
40 OUT UINTN *Offset\r
41 )\r
42{\r
43 EFI_STATUS Status;\r
44 EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r
45 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
46 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
47 EFI_FV_BLOCK_MAP_ENTRY *FvbMapEntry;\r
48 UINT32 LbaIndex;\r
49\r
50 *Lba = (EFI_LBA) (-1);\r
51 *Offset = 0;\r
52 \r
53 //\r
54 // Get the proper FVB protocol.\r
55 //\r
56 Status = GetFvbInfoByAddress (Address, NULL, &Fvb);\r
57 if (EFI_ERROR (Status)) {\r
58 return Status;\r
59 }\r
60\r
61 //\r
62 // Get the Base Address of FV.\r
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
72 // Get the (LBA, Offset) of Address.\r
73 //\r
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
88 }\r
89 }\r
90\r
91 return EFI_ABORTED;\r
92}\r
93\r
94/**\r
95 Writes a buffer to variable storage space, in the working block.\r
96\r
97 This function writes a buffer to variable storage space into a firmware\r
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
102 @param Buffer Point to the data buffer.\r
103 @param BufferSize The number of bytes of the data Buffer.\r
104\r
105 @retval EFI_SUCCESS The function completed successfully.\r
106 @retval EFI_NOT_FOUND Fail to locate Fault Tolerant Write protocol.\r
107 @retval EFI_ABORTED The function could not complete successfully.\r
108\r
109**/\r
110EFI_STATUS\r
111FtwVariableSpace (\r
112 IN EFI_PHYSICAL_ADDRESS VariableBase,\r
113 IN UINT8 *Buffer,\r
114 IN UINTN BufferSize\r
115 )\r
116{\r
117 EFI_STATUS Status;\r
118 EFI_HANDLE FvbHandle;\r
119 EFI_LBA VarLba;\r
120 UINTN VarOffset;\r
121 UINT8 *FtwBuffer;\r
122 UINTN FtwBufferSize;\r
123 EFI_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r
124\r
125 //\r
126 // Locate fault tolerant write protocol.\r
127 //\r
128 Status = GetFtwProtocol((VOID **) &FtwProtocol);\r
129 if (EFI_ERROR (Status)) {\r
130 return EFI_NOT_FOUND;\r
131 }\r
132 //\r
133 // Locate Fvb handle by address.\r
134 //\r
135 Status = GetFvbInfoByAddress (VariableBase, &FvbHandle, NULL);\r
136 if (EFI_ERROR (Status)) {\r
137 return Status;\r
138 }\r
139 //\r
140 // Get LBA and Offset by address.\r
141 //\r
142 Status = GetLbaAndOffsetByAddress (VariableBase, &VarLba, &VarOffset);\r
143 if (EFI_ERROR (Status)) {\r
144 return EFI_ABORTED;\r
145 }\r
146 //\r
147 // Prepare for the variable data.\r
148 //\r
149 FtwBufferSize = ((VARIABLE_STORE_HEADER *) ((UINTN) VariableBase))->Size;\r
150 FtwBuffer = AllocatePool (FtwBufferSize);\r
151 if (FtwBuffer == NULL) {\r
152 return EFI_OUT_OF_RESOURCES;\r
153 }\r
154\r
155 SetMem (FtwBuffer, FtwBufferSize, (UINT8) 0xff);\r
156 CopyMem (FtwBuffer, Buffer, BufferSize);\r
157\r
158 //\r
159 // FTW write record.\r
160 //\r
161 Status = FtwProtocol->Write (\r
162 FtwProtocol,\r
163 VarLba, // LBA\r
164 VarOffset, // Offset\r
165 FtwBufferSize, // NumBytes\r
166 NULL, // PrivateData NULL\r
167 FvbHandle, // Fvb Handle\r
168 FtwBuffer // write buffer\r
169 );\r
170\r
171 FreePool (FtwBuffer);\r
172 return Status;\r
173}\r