3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Ia32 platform related code to support FtwLite..
28 // MACROs for boot block update
30 #define BOOT_BLOCK_BASE 0xFFFF0000
35 #define LPC_BUS_NUMBER 0x00
36 #define LPC_DEVICE_NUMBER 0x1F
41 #define GEN_STATUS 0xD4
42 #define TOP_SWAP_BIT (1 << 13)
53 Read PCI register value.
57 Offset - Offset of the register
67 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
70 Status
= gBS
->LocateProtocol (&gEfiPciRootBridgeIoProtocolGuid
, NULL
, (VOID
**) &PciRootBridgeIo
);
71 if (EFI_ERROR (Status
)) {
72 DEBUG ((EFI_D_ERROR
, "FtwLite: Locate PCI root bridge io protocol - %r", Status
));
76 Status
= PciRootBridgeIo
->Pci
.Read (
79 EFI_PCI_ADDRESS (LPC_BUS_NUMBER
,
86 ASSERT_EFI_ERROR (Status
);
94 IN EFI_FTW_LITE_DEVICE
*FtwLiteDevice
,
95 OUT BOOLEAN
*SwapState
105 FtwLiteDevice - Calling context
106 SwapState - Swap state
110 EFI_SUCCESS - State successfully got
115 // Top swap status is 13 bit
117 *SwapState
= (BOOLEAN
) ((ReadPciRegister (GEN_STATUS
) & TOP_SWAP_BIT
) != 0);
125 IN EFI_FTW_LITE_DEVICE
*FtwLiteDevice
,
134 FtwLiteDevice - Indicates a pointer to the calling context.
135 TopSwap - New swap state
138 EFI_SUCCESS - The function completed successfully
141 the Top-Swap bit (bit 13, D31: F0, Offset D4h). Note that
142 software will not be able to clear the Top-Swap bit until the system is
143 rebooted without GNT[A]# being pulled down.
148 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
152 // Top-Swap bit (bit 13, D31: F0, Offset D4h)
154 GenStatus
= ReadPciRegister (GEN_STATUS
);
157 // Set 13 bit, according to input NewSwapState
160 GenStatus
|= TOP_SWAP_BIT
;
162 GenStatus
&= ~TOP_SWAP_BIT
;
165 Status
= gBS
->LocateProtocol (&gEfiPciRootBridgeIoProtocolGuid
, NULL
, (VOID
**) &PciRootBridgeIo
);
166 if (EFI_ERROR (Status
)) {
167 DEBUG ((EFI_D_ERROR
, "FtwLite: Locate PCI root bridge io protocol - %r", Status
));
171 // Write back the GenStatus register
173 Status
= PciRootBridgeIo
->Pci
.Write (
176 EFI_PCI_ADDRESS (LPC_BUS_NUMBER
,
186 DEBUG ((EFI_D_ERROR
, "SAR: Set top swap\n"));
188 DEBUG ((EFI_D_ERROR
, "SAR: Clear top swap\n"));
197 EFI_FTW_LITE_DEVICE
*FtwLiteDevice
,
198 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
205 Check whether the block is a boot block.
209 FtwLiteDevice - Calling context
210 FvBlock - Fvb protocol instance
215 Is a boot block or not
220 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*BootFvb
;
222 Status
= GetFvbByAddress (BOOT_BLOCK_BASE
, &BootFvb
);
223 if (EFI_ERROR (Status
)) {
229 return (BOOLEAN
) (FvBlock
== BootFvb
);
233 FlushSpareBlockToBootBlock (
234 EFI_FTW_LITE_DEVICE
*FtwLiteDevice
239 Copy the content of spare block to a boot block. Size is FTW_BLOCK_SIZE.
240 Spare block is accessed by FTW backup FVB protocol interface. LBA is
241 FtwLiteDevice->FtwSpareLba.
242 Boot block is accessed by BootFvb protocol interface. LBA is 0.
245 FtwLiteDevice - The private data of FTW_LITE driver
248 EFI_SUCCESS - Spare block content is copied to boot block
249 EFI_INVALID_PARAMETER - Input parameter error
250 EFI_OUT_OF_RESOURCES - Allocate memory error
251 EFI_ABORTED - The function could not complete successfully
254 FTW will do extra work on boot block update.
255 FTW should depend on a protocol of EFI_ADDRESS_RANGE_SWAP_PROTOCOL,
256 which is produced by a chipset driver.
258 FTW updating boot block steps:
259 1. Erase top swap block (0xFFFE-0xFFFEFFFF) and write data to it ready
260 2. Read data from top swap block to memory buffer
261 3. SetSwapState(EFI_SWAPPED)
262 4. Erasing boot block (0xFFFF-0xFFFFFFFF)
263 5. Programming boot block until the boot block is ok.
264 6. SetSwapState(UNSWAPPED)
267 1. Since the SwapState bit is saved in CMOS, FTW can restore and continue
268 even in the scenario of power failure.
269 2. FTW shall not allow to update boot block when battery state is error.
280 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*BootFvb
;
284 // Allocate a memory buffer
286 Length
= FtwLiteDevice
->SpareAreaLength
;
287 Buffer
= AllocatePool (Length
);
288 if (Buffer
== NULL
) {
291 // Get TopSwap bit state
293 Status
= GetSwapState (FtwLiteDevice
, &TopSwap
);
294 if (EFI_ERROR (Status
)) {
295 DEBUG ((EFI_D_ERROR
, "FtwLite: Get Top Swapped status - %r\n", Status
));
296 gBS
->FreePool (Buffer
);
302 // Get FVB of current boot block
304 Status
= GetFvbByAddress (FtwLiteDevice
->SpareAreaAddress
+ FTW_BLOCK_SIZE
, &BootFvb
);
305 if (EFI_ERROR (Status
)) {
306 gBS
->FreePool (Buffer
);
310 // Read data from current boot block
314 for (Index
= 0; Index
< FtwLiteDevice
->NumberOfSpareBlock
; Index
+= 1) {
315 Count
= FtwLiteDevice
->SizeOfSpareBlock
;
316 Status
= BootFvb
->Read (
323 if (EFI_ERROR (Status
)) {
324 gBS
->FreePool (Buffer
);
333 // Read data from spare block
336 for (Index
= 0; Index
< FtwLiteDevice
->NumberOfSpareBlock
; Index
+= 1) {
337 Count
= FtwLiteDevice
->SizeOfSpareBlock
;
338 Status
= FtwLiteDevice
->FtwBackupFvb
->Read (
339 FtwLiteDevice
->FtwBackupFvb
,
340 FtwLiteDevice
->FtwSpareLba
+ Index
,
345 if (EFI_ERROR (Status
)) {
346 gBS
->FreePool (Buffer
);
355 Status
= SetSwapState (FtwLiteDevice
, TRUE
);
356 DEBUG ((EFI_D_ERROR
, "FtwLite: Set Swap State - %r\n", Status
));
357 ASSERT_EFI_ERROR (Status
);
360 // Erase boot block. After setting TopSwap bit, it's spare block now!
362 Status
= FtwEraseSpareBlock (FtwLiteDevice
);
363 if (EFI_ERROR (Status
)) {
364 gBS
->FreePool (Buffer
);
368 // Write memory buffer to currenet spare block
371 for (Index
= 0; Index
< FtwLiteDevice
->NumberOfSpareBlock
; Index
+= 1) {
372 Count
= FtwLiteDevice
->SizeOfSpareBlock
;
373 Status
= FtwLiteDevice
->FtwBackupFvb
->Write (
374 FtwLiteDevice
->FtwBackupFvb
,
375 FtwLiteDevice
->FtwSpareLba
+ Index
,
380 if (EFI_ERROR (Status
)) {
381 DEBUG ((EFI_D_FTW_LITE
, "FtwLite: FVB Write boot block - %r\n", Status
));
382 gBS
->FreePool (Buffer
);
389 gBS
->FreePool (Buffer
);
394 Status
= SetSwapState (FtwLiteDevice
, FALSE
);
395 DEBUG ((EFI_D_ERROR
, "FtwLite: Clear Swap State - %r\n", Status
));
396 ASSERT_EFI_ERROR (Status
);