]>
Commit | Line | Data |
---|---|---|
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) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r | |
6 | SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
7 | \r | |
8 | **/\r | |
9 | \r | |
10 | #include "Variable.h"\r | |
11 | \r | |
12 | /**\r | |
13 | Gets LBA of block and offset by given address.\r | |
14 | \r | |
15 | This function gets the Logical Block Address (LBA) of a firmware\r | |
16 | volume block containing the given address, and the offset of the\r | |
17 | address on the block.\r | |
18 | \r | |
19 | @param Address Address which should be contained\r | |
20 | by returned FVB handle.\r | |
21 | @param Lba Pointer to LBA for output.\r | |
22 | @param Offset Pointer to offset for output.\r | |
23 | \r | |
24 | @retval EFI_SUCCESS LBA and offset successfully returned.\r | |
25 | @retval EFI_NOT_FOUND Fail to find FVB handle by address.\r | |
26 | @retval EFI_ABORTED Fail to find valid LBA and offset.\r | |
27 | \r | |
28 | **/\r | |
29 | EFI_STATUS\r | |
30 | GetLbaAndOffsetByAddress (\r | |
31 | IN EFI_PHYSICAL_ADDRESS Address,\r | |
32 | OUT EFI_LBA *Lba,\r | |
33 | OUT UINTN *Offset\r | |
34 | )\r | |
35 | {\r | |
36 | EFI_STATUS Status;\r | |
37 | EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r | |
38 | EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r | |
39 | EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r | |
40 | EFI_FV_BLOCK_MAP_ENTRY *FvbMapEntry;\r | |
41 | UINT32 LbaIndex;\r | |
42 | \r | |
43 | Fvb = NULL;\r | |
44 | *Lba = (EFI_LBA)(-1);\r | |
45 | *Offset = 0;\r | |
46 | \r | |
47 | //\r | |
48 | // Get the proper FVB protocol.\r | |
49 | //\r | |
50 | Status = GetFvbInfoByAddress (Address, NULL, &Fvb);\r | |
51 | if (EFI_ERROR (Status)) {\r | |
52 | return Status;\r | |
53 | }\r | |
54 | \r | |
55 | //\r | |
56 | // Get the Base Address of FV.\r | |
57 | //\r | |
58 | Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r | |
59 | if (EFI_ERROR (Status)) {\r | |
60 | return Status;\r | |
61 | }\r | |
62 | \r | |
63 | FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)((UINTN)FvbBaseAddress);\r | |
64 | \r | |
65 | //\r | |
66 | // Get the (LBA, Offset) of Address.\r | |
67 | //\r | |
68 | if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) {\r | |
69 | //\r | |
70 | // BUGBUG: Assume one FV has one type of BlockLength.\r | |
71 | //\r | |
72 | FvbMapEntry = &FwVolHeader->BlockMap[0];\r | |
73 | for (LbaIndex = 1; LbaIndex <= FvbMapEntry->NumBlocks; LbaIndex += 1) {\r | |
74 | if (Address < (FvbBaseAddress + FvbMapEntry->Length * LbaIndex)) {\r | |
75 | //\r | |
76 | // Found the (Lba, Offset).\r | |
77 | //\r | |
78 | *Lba = LbaIndex - 1;\r | |
79 | *Offset = (UINTN)(Address - (FvbBaseAddress + FvbMapEntry->Length * (LbaIndex - 1)));\r | |
80 | return EFI_SUCCESS;\r | |
81 | }\r | |
82 | }\r | |
83 | }\r | |
84 | \r | |
85 | return EFI_ABORTED;\r | |
86 | }\r | |
87 | \r | |
88 | /**\r | |
89 | Writes a buffer to variable storage space, in the working block.\r | |
90 | \r | |
91 | This function writes a buffer to variable storage space into a firmware\r | |
92 | volume block device. The destination is specified by parameter\r | |
93 | VariableBase. Fault Tolerant Write protocol is used for writing.\r | |
94 | \r | |
95 | @param VariableBase Base address of variable to write\r | |
96 | @param VariableBuffer Point to the variable data buffer.\r | |
97 | \r | |
98 | @retval EFI_SUCCESS The function completed successfully.\r | |
99 | @retval EFI_NOT_FOUND Fail to locate Fault Tolerant Write protocol.\r | |
100 | @retval EFI_ABORTED The function could not complete successfully.\r | |
101 | \r | |
102 | **/\r | |
103 | EFI_STATUS\r | |
104 | FtwVariableSpace (\r | |
105 | IN EFI_PHYSICAL_ADDRESS VariableBase,\r | |
106 | IN VARIABLE_STORE_HEADER *VariableBuffer\r | |
107 | )\r | |
108 | {\r | |
109 | EFI_STATUS Status;\r | |
110 | EFI_HANDLE FvbHandle;\r | |
111 | EFI_LBA VarLba;\r | |
112 | UINTN VarOffset;\r | |
113 | UINTN FtwBufferSize;\r | |
114 | EFI_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r | |
115 | \r | |
116 | //\r | |
117 | // Locate fault tolerant write protocol.\r | |
118 | //\r | |
119 | Status = GetFtwProtocol ((VOID **)&FtwProtocol);\r | |
120 | if (EFI_ERROR (Status)) {\r | |
121 | return EFI_NOT_FOUND;\r | |
122 | }\r | |
123 | \r | |
124 | //\r | |
125 | // Locate Fvb handle by address.\r | |
126 | //\r | |
127 | Status = GetFvbInfoByAddress (VariableBase, &FvbHandle, NULL);\r | |
128 | if (EFI_ERROR (Status)) {\r | |
129 | return Status;\r | |
130 | }\r | |
131 | \r | |
132 | //\r | |
133 | // Get LBA and Offset by address.\r | |
134 | //\r | |
135 | Status = GetLbaAndOffsetByAddress (VariableBase, &VarLba, &VarOffset);\r | |
136 | if (EFI_ERROR (Status)) {\r | |
137 | return EFI_ABORTED;\r | |
138 | }\r | |
139 | \r | |
140 | FtwBufferSize = ((VARIABLE_STORE_HEADER *)((UINTN)VariableBase))->Size;\r | |
141 | ASSERT (FtwBufferSize == VariableBuffer->Size);\r | |
142 | \r | |
143 | //\r | |
144 | // FTW write record.\r | |
145 | //\r | |
146 | Status = FtwProtocol->Write (\r | |
147 | FtwProtocol,\r | |
148 | VarLba, // LBA\r | |
149 | VarOffset, // Offset\r | |
150 | FtwBufferSize, // NumBytes\r | |
151 | NULL, // PrivateData NULL\r | |
152 | FvbHandle, // Fvb Handle\r | |
153 | (VOID *)VariableBuffer // write buffer\r | |
154 | );\r | |
155 | \r | |
156 | return Status;\r | |
157 | }\r |