3 Ia32 platform related code to support FtwLite.
5 Copyright (c) 2006 - 2008, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 // MACROs for boot block update
22 #define BOOT_BLOCK_BASE 0xFFFF0000
27 #define LPC_BUS_NUMBER 0x00
28 #define LPC_DEVICE_NUMBER 0x1F
33 #define GEN_STATUS 0xD4
34 #define TOP_SWAP_BIT (1 << 13)
38 Read PCI register value.
39 This is a internal function.
42 @param Offset Offset of the register
44 @return The pci register value.
54 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
57 Status
= gBS
->LocateProtocol (&gEfiPciRootBridgeIoProtocolGuid
, NULL
, (VOID
**) &PciRootBridgeIo
);
58 if (EFI_ERROR (Status
)) {
59 DEBUG ((EFI_D_ERROR
, "FtwLite: Locate PCI root bridge io protocol - %r", Status
));
63 Status
= PciRootBridgeIo
->Pci
.Read (
75 ASSERT_EFI_ERROR (Status
);
84 This is a internal function.
86 @param FtwLiteDevice Calling context
87 @param SwapState Swap state
89 @retval EFI_SUCCESS State successfully got
94 IN EFI_FTW_LITE_DEVICE
*FtwLiteDevice
,
95 OUT BOOLEAN
*SwapState
99 // Top swap status is 13 bit
101 *SwapState
= (BOOLEAN
) ((ReadPciRegister (GEN_STATUS
) & TOP_SWAP_BIT
) != 0);
109 This is a internal function.
111 @param FtwLiteDevice Indicates a pointer to the calling context.
112 @param TopSwap New swap state
114 @retval EFI_SUCCESS The function completed successfully
116 the Top-Swap bit (bit 13, D31: F0, Offset D4h). Note that
117 software will not be able to clear the Top-Swap bit until the system is
118 rebooted without GNT[A]# being pulled down.
123 IN EFI_FTW_LITE_DEVICE
*FtwLiteDevice
,
128 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
132 // Top-Swap bit (bit 13, D31: F0, Offset D4h)
134 GenStatus
= ReadPciRegister (GEN_STATUS
);
137 // Set 13 bit, according to input NewSwapState
140 GenStatus
|= TOP_SWAP_BIT
;
142 GenStatus
&= ~TOP_SWAP_BIT
;
145 Status
= gBS
->LocateProtocol (&gEfiPciRootBridgeIoProtocolGuid
, NULL
, (VOID
**) &PciRootBridgeIo
);
146 if (EFI_ERROR (Status
)) {
147 DEBUG ((EFI_D_ERROR
, "FtwLite: Locate PCI root bridge io protocol - %r", Status
));
151 // Write back the GenStatus register
153 Status
= PciRootBridgeIo
->Pci
.Write (
168 DEBUG ((EFI_D_ERROR
, "SAR: Set top swap\n"));
170 DEBUG ((EFI_D_ERROR
, "SAR: Clear top swap\n"));
179 Check whether the block is a boot block.
182 @param FtwLiteDevice Calling context
183 @param FvBlock Fvb protocol instance
186 @retval FALSE This is a boot block.
187 @retval TRUE This is not a boot block.
192 EFI_FTW_LITE_DEVICE
*FtwLiteDevice
,
193 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
198 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*BootFvb
;
200 Status
= GetFvbByAddress (BOOT_BLOCK_BASE
, &BootFvb
);
201 if (EFI_ERROR (Status
)) {
207 return (BOOLEAN
) (FvBlock
== BootFvb
);
211 Copy the content of spare block to a boot block. Size is FTW_BLOCK_SIZE.
212 Spare block is accessed by FTW backup FVB protocol interface. LBA is
213 FtwLiteDevice->FtwSpareLba.
214 Boot block is accessed by BootFvb protocol interface. LBA is 0.
217 @param FtwLiteDevice The private data of FTW_LITE driver
219 @retval EFI_SUCCESS Spare block content is copied to boot block
220 @retval EFI_INVALID_PARAMETER Input parameter error
221 @retval EFI_OUT_OF_RESOURCES Allocate memory error
222 @retval EFI_ABORTED The function could not complete successfully
224 FTW will do extra work on boot block update.
225 FTW should depend on a protocol of EFI_ADDRESS_RANGE_SWAP_PROTOCOL,
226 which is produced by a chipset driver.
227 FTW updating boot block steps:
228 1. Erase top swap block (0xFFFE-0xFFFEFFFF) and write data to it ready
229 2. Read data from top swap block to memory buffer
230 3. SetSwapState(EFI_SWAPPED)
231 4. Erasing boot block (0xFFFF-0xFFFFFFFF)
232 5. Programming boot block until the boot block is ok.
233 6. SetSwapState(UNSWAPPED)
235 1. Since the SwapState bit is saved in CMOS, FTW can restore and continue
236 even in the scenario of power failure.
237 2. FTW shall not allow to update boot block when battery state is error.
241 FlushSpareBlockToBootBlock (
242 EFI_FTW_LITE_DEVICE
*FtwLiteDevice
252 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*BootFvb
;
256 // Allocate a memory buffer
258 Length
= FtwLiteDevice
->SpareAreaLength
;
259 Buffer
= AllocatePool (Length
);
260 if (Buffer
== NULL
) {
261 return EFI_OUT_OF_RESOURCES
;
264 // Get TopSwap bit state
266 Status
= GetSwapState (FtwLiteDevice
, &TopSwap
);
267 if (EFI_ERROR (Status
)) {
268 DEBUG ((EFI_D_ERROR
, "FtwLite: Get Top Swapped status - %r\n", Status
));
275 // Get FVB of current boot block
277 Status
= GetFvbByAddress (FtwLiteDevice
->SpareAreaAddress
+ FTW_BLOCK_SIZE
, &BootFvb
);
278 if (EFI_ERROR (Status
)) {
283 // Read data from current boot block
287 for (Index
= 0; Index
< FtwLiteDevice
->NumberOfSpareBlock
; Index
+= 1) {
288 Count
= FtwLiteDevice
->SizeOfSpareBlock
;
289 Status
= BootFvb
->Read (
296 if (EFI_ERROR (Status
)) {
306 // Read data from spare block
309 for (Index
= 0; Index
< FtwLiteDevice
->NumberOfSpareBlock
; Index
+= 1) {
310 Count
= FtwLiteDevice
->SizeOfSpareBlock
;
311 Status
= FtwLiteDevice
->FtwBackupFvb
->Read (
312 FtwLiteDevice
->FtwBackupFvb
,
313 FtwLiteDevice
->FtwSpareLba
+ Index
,
318 if (EFI_ERROR (Status
)) {
328 Status
= SetSwapState (FtwLiteDevice
, TRUE
);
329 DEBUG ((EFI_D_ERROR
, "FtwLite: Set Swap State - %r\n", Status
));
330 ASSERT_EFI_ERROR (Status
);
333 // Erase boot block. After setting TopSwap bit, it's spare block now!
335 Status
= FtwEraseSpareBlock (FtwLiteDevice
);
336 if (EFI_ERROR (Status
)) {
341 // Write memory buffer to currenet spare block
344 for (Index
= 0; Index
< FtwLiteDevice
->NumberOfSpareBlock
; Index
+= 1) {
345 Count
= FtwLiteDevice
->SizeOfSpareBlock
;
346 Status
= FtwLiteDevice
->FtwBackupFvb
->Write (
347 FtwLiteDevice
->FtwBackupFvb
,
348 FtwLiteDevice
->FtwSpareLba
+ Index
,
353 if (EFI_ERROR (Status
)) {
354 DEBUG ((EFI_D_FTW_LITE
, "FtwLite: FVB Write boot block - %r\n", Status
));
367 Status
= SetSwapState (FtwLiteDevice
, FALSE
);
368 DEBUG ((EFI_D_ERROR
, "FtwLite: Clear Swap State - %r\n", Status
));
369 ASSERT_EFI_ERROR (Status
);