]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/Ia32/Ia32FtwMisc.c
Use #include "XXX.h" for module internal header files.
[mirror_edk2.git] / MdeModulePkg / Universal / FirmwareVolume / FaultTolerantWriteDxe / Ia32 / Ia32FtwMisc.c
CommitLineData
6cc9ca32
LG
1/** @file\r
2 \r
3 Ia32 platform related code to support FtwLite.\r
4 \r
5Copyright (c) 2006 - 2008, Intel Corporation \r
d7dec593 6All rights reserved. This program and the accompanying materials \r
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
6cc9ca32 14**/\r
d7dec593 15\r
16\r
e87a334f 17#include "FtwLite.h"\r
d7dec593 18\r
19//\r
20// MACROs for boot block update\r
21//\r
22#define BOOT_BLOCK_BASE 0xFFFF0000\r
23\r
24//\r
25// (LPC -- D31:F0)\r
26//\r
27#define LPC_BUS_NUMBER 0x00\r
28#define LPC_DEVICE_NUMBER 0x1F\r
29#define LPC_IF 0xF0\r
30//\r
31// Top swap\r
32//\r
33#define GEN_STATUS 0xD4\r
34#define TOP_SWAP_BIT (1 << 13)\r
35\r
6aab8214 36/**\r
d7dec593 37\r
ca4eb92e 38 Read PCI register value.\r
39 This is a internal function.\r
40\r
41\r
42 @param Offset Offset of the register\r
43\r
44 @return The pci register value.\r
45\r
46**/\r
47UINT32\r
48ReadPciRegister (\r
49 IN UINT32 Offset\r
50 )\r
51{\r
52 EFI_STATUS Status;\r
53 UINT32 Value;\r
54 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRootBridgeIo;\r
55\r
56 Value = 0;\r
57 Status = gBS->LocateProtocol (&gEfiPciRootBridgeIoProtocolGuid, NULL, (VOID **) &PciRootBridgeIo);\r
58 if (EFI_ERROR (Status)) {\r
59 DEBUG ((EFI_D_ERROR, "FtwLite: Locate PCI root bridge io protocol - %r", Status));\r
60 return 0;\r
61 }\r
62\r
63 Status = PciRootBridgeIo->Pci.Read (\r
64 PciRootBridgeIo,\r
65 EfiPciWidthUint32,\r
66 EFI_PCI_ADDRESS (\r
67 LPC_BUS_NUMBER,\r
68 LPC_DEVICE_NUMBER,\r
69 LPC_IF,\r
70 Offset\r
71 ),\r
72 1,\r
73 &Value\r
74 );\r
75 ASSERT_EFI_ERROR (Status);\r
76\r
77 return Value;\r
78}\r
79\r
80/**\r
81\r
82 Get swap state\r
d7dec593 83\r
6aab8214 84 This is a internal function.\r
d7dec593 85\r
6aab8214 86 @param FtwLiteDevice Calling context\r
87 @param SwapState Swap state\r
d7dec593 88\r
6aab8214 89 @retval EFI_SUCCESS State successfully got\r
d7dec593 90\r
6aab8214 91**/\r
92EFI_STATUS\r
93GetSwapState (\r
94 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice,\r
95 OUT BOOLEAN *SwapState\r
96 )\r
d7dec593 97{\r
98 //\r
99 // Top swap status is 13 bit\r
100 //\r
ca4eb92e 101 *SwapState = (BOOLEAN) ((ReadPciRegister (GEN_STATUS) & TOP_SWAP_BIT) != 0);\r
d7dec593 102\r
103 return EFI_SUCCESS;\r
104}\r
105\r
6aab8214 106/**\r
107 Set swap state.\r
108\r
109 This is a internal function.\r
110\r
111 @param FtwLiteDevice Indicates a pointer to the calling context.\r
112 @param TopSwap New swap state\r
113\r
114 @retval EFI_SUCCESS The function completed successfully\r
115 Note:\r
116 the Top-Swap bit (bit 13, D31: F0, Offset D4h). Note that\r
117 software will not be able to clear the Top-Swap bit until the system is\r
118 rebooted without GNT[A]# being pulled down.\r
119\r
120**/\r
d7dec593 121EFI_STATUS\r
122SetSwapState (\r
123 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice,\r
124 IN BOOLEAN TopSwap\r
125 )\r
d7dec593 126{\r
127 UINT32 GenStatus;\r
128 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRootBridgeIo;\r
129 EFI_STATUS Status;\r
130\r
131 //\r
132 // Top-Swap bit (bit 13, D31: F0, Offset D4h)\r
133 //\r
ca4eb92e 134 GenStatus = ReadPciRegister (GEN_STATUS);\r
d7dec593 135\r
136 //\r
137 // Set 13 bit, according to input NewSwapState\r
138 //\r
139 if (TopSwap) {\r
140 GenStatus |= TOP_SWAP_BIT;\r
141 } else {\r
142 GenStatus &= ~TOP_SWAP_BIT;\r
143 }\r
144\r
ca4eb92e 145 Status = gBS->LocateProtocol (&gEfiPciRootBridgeIoProtocolGuid, NULL, (VOID **) &PciRootBridgeIo);\r
146 if (EFI_ERROR (Status)) {\r
147 DEBUG ((EFI_D_ERROR, "FtwLite: Locate PCI root bridge io protocol - %r", Status));\r
148 return Status;\r
149 }\r
d7dec593 150 //\r
151 // Write back the GenStatus register\r
152 //\r
ca4eb92e 153 Status = PciRootBridgeIo->Pci.Write (\r
154 PciRootBridgeIo,\r
155 EfiPciWidthUint32,\r
156 EFI_PCI_ADDRESS (\r
157 LPC_BUS_NUMBER,\r
158 LPC_DEVICE_NUMBER,\r
159 LPC_IF,\r
160 GEN_STATUS\r
161 ),\r
162 1,\r
163 &GenStatus\r
164 );\r
d7dec593 165\r
166 DEBUG_CODE_BEGIN ();\r
167 if (TopSwap) {\r
168 DEBUG ((EFI_D_ERROR, "SAR: Set top swap\n"));\r
169 } else {\r
170 DEBUG ((EFI_D_ERROR, "SAR: Clear top swap\n"));\r
171 }\r
172 DEBUG_CODE_END ();\r
173\r
174 return EFI_SUCCESS;\r
175}\r
176\r
6aab8214 177/**\r
d7dec593 178\r
179 Check whether the block is a boot block.\r
180\r
d7dec593 181\r
6aab8214 182 @param FtwLiteDevice Calling context\r
183 @param FvBlock Fvb protocol instance\r
184 @param Lba Lba value\r
d7dec593 185\r
6aab8214 186 @retval FALSE This is a boot block.\r
187 @retval TRUE This is not a boot block.\r
d7dec593 188\r
6aab8214 189**/\r
190BOOLEAN\r
191IsBootBlock (\r
192 EFI_FTW_LITE_DEVICE *FtwLiteDevice,\r
193 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,\r
194 EFI_LBA Lba\r
195 )\r
d7dec593 196{\r
197 EFI_STATUS Status;\r
198 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *BootFvb;\r
199\r
200 Status = GetFvbByAddress (BOOT_BLOCK_BASE, &BootFvb);\r
201 if (EFI_ERROR (Status)) {\r
202 return FALSE;\r
203 }\r
204 //\r
205 // Compare the Fvb\r
206 //\r
207 return (BOOLEAN) (FvBlock == BootFvb);\r
208}\r
209\r
6aab8214 210/**\r
211 Copy the content of spare block to a boot block. Size is FTW_BLOCK_SIZE.\r
212 Spare block is accessed by FTW backup FVB protocol interface. LBA is\r
213 FtwLiteDevice->FtwSpareLba.\r
214 Boot block is accessed by BootFvb protocol interface. LBA is 0.\r
215\r
216\r
217 @param FtwLiteDevice The private data of FTW_LITE driver\r
218\r
219 @retval EFI_SUCCESS Spare block content is copied to boot block\r
220 @retval EFI_INVALID_PARAMETER Input parameter error\r
221 @retval EFI_OUT_OF_RESOURCES Allocate memory error\r
222 @retval EFI_ABORTED The function could not complete successfully\r
223 Notes:\r
224 FTW will do extra work on boot block update.\r
225 FTW should depend on a protocol of EFI_ADDRESS_RANGE_SWAP_PROTOCOL,\r
226 which is produced by a chipset driver.\r
227 FTW updating boot block steps:\r
228 1. Erase top swap block (0xFFFE-0xFFFEFFFF) and write data to it ready\r
229 2. Read data from top swap block to memory buffer\r
230 3. SetSwapState(EFI_SWAPPED)\r
231 4. Erasing boot block (0xFFFF-0xFFFFFFFF)\r
232 5. Programming boot block until the boot block is ok.\r
233 6. SetSwapState(UNSWAPPED)\r
234 Notes:\r
235 1. Since the SwapState bit is saved in CMOS, FTW can restore and continue\r
236 even in the scenario of power failure.\r
237 2. FTW shall not allow to update boot block when battery state is error.\r
238\r
239**/\r
d7dec593 240EFI_STATUS\r
241FlushSpareBlockToBootBlock (\r
242 EFI_FTW_LITE_DEVICE *FtwLiteDevice\r
243 )\r
d7dec593 244{\r
245 EFI_STATUS Status;\r
246 UINTN Length;\r
247 UINT8 *Buffer;\r
248 UINTN Count;\r
249 UINT8 *Ptr;\r
250 UINTN Index;\r
251 BOOLEAN TopSwap;\r
252 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *BootFvb;\r
253 EFI_LBA BootLba;\r
254\r
255 //\r
256 // Allocate a memory buffer\r
257 //\r
258 Length = FtwLiteDevice->SpareAreaLength;\r
259 Buffer = AllocatePool (Length);\r
260 if (Buffer == NULL) {\r
e11ae3a5 261 return EFI_OUT_OF_RESOURCES;\r
d7dec593 262 }\r
263 //\r
264 // Get TopSwap bit state\r
265 //\r
266 Status = GetSwapState (FtwLiteDevice, &TopSwap);\r
267 if (EFI_ERROR (Status)) {\r
268 DEBUG ((EFI_D_ERROR, "FtwLite: Get Top Swapped status - %r\n", Status));\r
269 FreePool (Buffer);\r
270 return EFI_ABORTED;\r
271 }\r
272\r
273 if (TopSwap) {\r
274 //\r
275 // Get FVB of current boot block\r
276 //\r
277 Status = GetFvbByAddress (FtwLiteDevice->SpareAreaAddress + FTW_BLOCK_SIZE, &BootFvb);\r
278 if (EFI_ERROR (Status)) {\r
279 FreePool (Buffer);\r
280 return Status;\r
281 }\r
282 //\r
283 // Read data from current boot block\r
284 //\r
285 BootLba = 0;\r
286 Ptr = Buffer;\r
287 for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
288 Count = FtwLiteDevice->SizeOfSpareBlock;\r
289 Status = BootFvb->Read (\r
290 BootFvb,\r
291 BootLba + Index,\r
292 0,\r
293 &Count,\r
294 Ptr\r
295 );\r
296 if (EFI_ERROR (Status)) {\r
297 FreePool (Buffer);\r
298 return Status;\r
299 }\r
300\r
301 Ptr += Count;\r
302 }\r
303\r
304 } else {\r
305 //\r
306 // Read data from spare block\r
307 //\r
308 Ptr = Buffer;\r
309 for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
310 Count = FtwLiteDevice->SizeOfSpareBlock;\r
311 Status = FtwLiteDevice->FtwBackupFvb->Read (\r
312 FtwLiteDevice->FtwBackupFvb,\r
313 FtwLiteDevice->FtwSpareLba + Index,\r
314 0,\r
315 &Count,\r
316 Ptr\r
317 );\r
318 if (EFI_ERROR (Status)) {\r
319 FreePool (Buffer);\r
320 return Status;\r
321 }\r
322\r
323 Ptr += Count;\r
324 }\r
325 //\r
326 // Set TopSwap bit\r
327 //\r
328 Status = SetSwapState (FtwLiteDevice, TRUE);\r
329 DEBUG ((EFI_D_ERROR, "FtwLite: Set Swap State - %r\n", Status));\r
330 ASSERT_EFI_ERROR (Status);\r
331 }\r
332 //\r
333 // Erase boot block. After setting TopSwap bit, it's spare block now!\r
334 //\r
335 Status = FtwEraseSpareBlock (FtwLiteDevice);\r
336 if (EFI_ERROR (Status)) {\r
337 FreePool (Buffer);\r
338 return EFI_ABORTED;\r
339 }\r
340 //\r
341 // Write memory buffer to currenet spare block\r
342 //\r
343 Ptr = Buffer;\r
344 for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
345 Count = FtwLiteDevice->SizeOfSpareBlock;\r
346 Status = FtwLiteDevice->FtwBackupFvb->Write (\r
347 FtwLiteDevice->FtwBackupFvb,\r
348 FtwLiteDevice->FtwSpareLba + Index,\r
349 0,\r
350 &Count,\r
351 Ptr\r
352 );\r
353 if (EFI_ERROR (Status)) {\r
354 DEBUG ((EFI_D_FTW_LITE, "FtwLite: FVB Write boot block - %r\n", Status));\r
355 FreePool (Buffer);\r
356 return Status;\r
357 }\r
358\r
359 Ptr += Count;\r
360 }\r
361\r
362 FreePool (Buffer);\r
363\r
364 //\r
365 // Clear TopSwap bit\r
366 //\r
367 Status = SetSwapState (FtwLiteDevice, FALSE);\r
368 DEBUG ((EFI_D_ERROR, "FtwLite: Clear Swap State - %r\n", Status));\r
369 ASSERT_EFI_ERROR (Status);\r
370\r
371 return EFI_SUCCESS;\r
372}\r