]>
Commit | Line | Data |
---|---|---|
0c18794e | 1 | /** @file\r |
2 | Handles non-volatile variable store garbage collection, using FTW\r | |
3 | (Fault Tolerant Write) protocol.\r | |
4 | \r | |
5 | Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>\r | |
6 | This program and the accompanying materials \r | |
7 | are licensed and made available under the terms and conditions of the BSD License \r | |
8 | which accompanies this distribution. The full text of the license may be found at \r | |
9 | http://opensource.org/licenses/bsd-license.php\r | |
10 | \r | |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | \r | |
14 | **/\r | |
15 | \r | |
16 | #include "Variable.h"\r | |
17 | \r | |
18 | /**\r | |
19 | Gets LBA of block and offset by given address.\r | |
20 | \r | |
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 | |
23 | address on the block.\r | |
24 | \r | |
25 | @param Address Address which should be contained\r | |
26 | by returned FVB handle.\r | |
27 | @param Lba Pointer to LBA for output.\r | |
28 | @param Offset Pointer to offset for output.\r | |
29 | \r | |
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 | |
33 | \r | |
34 | **/\r | |
35 | EFI_STATUS\r | |
36 | GetLbaAndOffsetByAddress (\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 | |
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 | |
49 | *Lba = (EFI_LBA) (-1);\r | |
50 | *Offset = 0;\r | |
51 | \r | |
52 | //\r | |
53 | // Get the proper FVB protocol.\r | |
54 | //\r | |
55 | Status = GetFvbInfoByAddress (Address, NULL, &Fvb);\r | |
56 | if (EFI_ERROR (Status)) {\r | |
57 | return Status;\r | |
58 | }\r | |
59 | \r | |
60 | //\r | |
61 | // Get the Base Address of FV.\r | |
62 | //\r | |
63 | Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r | |
64 | if (EFI_ERROR (Status)) {\r | |
65 | return Status;\r | |
66 | }\r | |
67 | \r | |
68 | FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);\r | |
69 | \r | |
70 | //\r | |
71 | // Get the (LBA, Offset) of Address.\r | |
72 | //\r | |
73 | if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) {\r | |
74 | //\r | |
75 | // BUGBUG: Assume one FV has one type of BlockLength.\r | |
76 | //\r | |
77 | FvbMapEntry = &FwVolHeader->BlockMap[0];\r | |
78 | for (LbaIndex = 1; LbaIndex <= FvbMapEntry->NumBlocks; LbaIndex += 1) {\r | |
79 | if (Address < (FvbBaseAddress + FvbMapEntry->Length * LbaIndex)) {\r | |
80 | //\r | |
81 | // Found the (Lba, Offset).\r | |
82 | //\r | |
83 | *Lba = LbaIndex - 1;\r | |
84 | *Offset = (UINTN) (Address - (FvbBaseAddress + FvbMapEntry->Length * (LbaIndex - 1)));\r | |
85 | return EFI_SUCCESS;\r | |
86 | }\r | |
87 | }\r | |
88 | }\r | |
89 | \r | |
90 | return EFI_ABORTED;\r | |
91 | }\r | |
92 | \r | |
93 | /**\r | |
94 | Writes a buffer to variable storage space, in the working block.\r | |
95 | \r | |
96 | This function writes a buffer to variable storage space into a firmware\r | |
97 | volume block device. The destination is specified by parameter\r | |
98 | VariableBase. Fault Tolerant Write protocol is used for writing.\r | |
99 | \r | |
100 | @param VariableBase Base address of variable to write\r | |
101 | @param Buffer Point to the data buffer.\r | |
102 | @param BufferSize The number of bytes of the data Buffer.\r | |
103 | \r | |
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 | |
107 | \r | |
108 | **/\r | |
109 | EFI_STATUS\r | |
110 | FtwVariableSpace (\r | |
111 | IN EFI_PHYSICAL_ADDRESS VariableBase,\r | |
112 | IN UINT8 *Buffer,\r | |
113 | IN UINTN BufferSize\r | |
114 | )\r | |
115 | {\r | |
116 | EFI_STATUS Status;\r | |
117 | EFI_HANDLE FvbHandle;\r | |
118 | EFI_LBA VarLba;\r | |
119 | UINTN VarOffset;\r | |
120 | UINT8 *FtwBuffer;\r | |
121 | UINTN FtwBufferSize;\r | |
122 | EFI_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r | |
123 | \r | |
124 | //\r | |
125 | // Locate fault tolerant write protocol.\r | |
126 | //\r | |
127 | Status = GetFtwProtocol((VOID **) &FtwProtocol);\r | |
128 | if (EFI_ERROR (Status)) {\r | |
129 | return EFI_NOT_FOUND;\r | |
130 | }\r | |
131 | //\r | |
132 | // Locate Fvb handle by address.\r | |
133 | //\r | |
134 | Status = GetFvbInfoByAddress (VariableBase, &FvbHandle, NULL);\r | |
135 | if (EFI_ERROR (Status)) {\r | |
136 | return Status;\r | |
137 | }\r | |
138 | //\r | |
139 | // Get LBA and Offset by address.\r | |
140 | //\r | |
141 | Status = GetLbaAndOffsetByAddress (VariableBase, &VarLba, &VarOffset);\r | |
142 | if (EFI_ERROR (Status)) {\r | |
143 | return EFI_ABORTED;\r | |
144 | }\r | |
145 | //\r | |
146 | // Prepare for the variable data.\r | |
147 | //\r | |
148 | FtwBufferSize = ((VARIABLE_STORE_HEADER *) ((UINTN) VariableBase))->Size;\r | |
149 | FtwBuffer = AllocatePool (FtwBufferSize);\r | |
150 | if (FtwBuffer == NULL) {\r | |
151 | return EFI_OUT_OF_RESOURCES;\r | |
152 | }\r | |
153 | \r | |
154 | SetMem (FtwBuffer, FtwBufferSize, (UINT8) 0xff);\r | |
155 | CopyMem (FtwBuffer, Buffer, BufferSize);\r | |
156 | \r | |
157 | //\r | |
158 | // FTW write record.\r | |
159 | //\r | |
160 | Status = FtwProtocol->Write (\r | |
161 | FtwProtocol,\r | |
162 | VarLba, // LBA\r | |
163 | VarOffset, // Offset\r | |
164 | FtwBufferSize, // NumBytes\r | |
165 | NULL, // PrivateData NULL\r | |
166 | FvbHandle, // Fvb Handle\r | |
167 | FtwBuffer // write buffer\r | |
168 | );\r | |
169 | \r | |
170 | FreePool (FtwBuffer);\r | |
171 | return Status;\r | |
172 | }\r |