3 Internal functions to operate Working Block Space.
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 Check to see if it is a valid work space.
23 @param WorkingHeader Pointer of working block header
25 @retval EFI_SUCCESS The function completed successfully
26 @retval EFI_ABORTED The function could not complete successfully.
31 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*WorkingHeader
35 EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER WorkingBlockHeader
;
37 ASSERT (WorkingHeader
!= NULL
);
38 if (WorkingHeader
->WorkingBlockValid
!= FTW_VALID_STATE
) {
42 // Check signature with gEfiSystemNvDataFvGuid
44 if (!CompareGuid (&gEfiSystemNvDataFvGuid
, &WorkingHeader
->Signature
)) {
48 // Check the CRC of header
53 sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
)
57 // Filter out the Crc and State fields
60 &WorkingBlockHeader
.Crc
,
64 WorkingBlockHeader
.WorkingBlockValid
= FTW_ERASE_POLARITY
;
65 WorkingBlockHeader
.WorkingBlockInvalid
= FTW_ERASE_POLARITY
;
68 // Calculate the Crc of woking block header
70 Status
= gBS
->CalculateCrc32 (
71 (UINT8
*) &WorkingBlockHeader
,
72 sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
),
73 &WorkingBlockHeader
.Crc
75 ASSERT_EFI_ERROR (Status
);
77 if (WorkingBlockHeader
.Crc
!= WorkingHeader
->Crc
) {
78 DEBUG ((EFI_D_FTW_LITE
, "FtwLite: Work block header CRC check error\n"));
86 Initialize a work space when there is no work space.
89 @param WorkingHeader Pointer of working block header
91 @retval EFI_SUCCESS The function completed successfully
92 @retval EFI_ABORTED The function could not complete successfully.
97 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*WorkingHeader
102 ASSERT (WorkingHeader
!= NULL
);
105 // Here using gEfiSystemNvDataFvGuid as the signature.
108 &WorkingHeader
->Signature
,
109 &gEfiSystemNvDataFvGuid
,
112 WorkingHeader
->WriteQueueSize
= FTW_WORKING_QUEUE_SIZE
;
115 // Crc is calculated with all the fields except Crc and STATE
117 WorkingHeader
->WorkingBlockValid
= FTW_ERASE_POLARITY
;
118 WorkingHeader
->WorkingBlockInvalid
= FTW_ERASE_POLARITY
;
119 SetMem (&WorkingHeader
->Crc
, sizeof (UINT32
), FTW_ERASED_BYTE
);
122 // Calculate the CRC value
124 Status
= gBS
->CalculateCrc32 (
125 (UINT8
*) WorkingHeader
,
126 sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
),
129 ASSERT_EFI_ERROR (Status
);
132 // Restore the WorkingBlockValid flag to VALID state
134 WorkingHeader
->WorkingBlockValid
= FTW_VALID_STATE
;
135 WorkingHeader
->WorkingBlockInvalid
= FTW_INVALID_STATE
;
141 Update a bit of state on a block device. The location of the bit is
142 calculated by the (Lba, Offset, bit). Here bit is determined by the
143 the name of a certain bit.
146 @param FvBlock FVB Protocol interface to access SrcBlock and DestBlock
147 @param Lba Lba of a block
148 @param Offset Offset on the Lba
149 @param NewBit New value that will override the old value if it can be change
151 @retval EFI_SUCCESS A state bit has been updated successfully
152 @retval Others Access block device error.
154 Assume all bits of State are inside the same BYTE.
155 @retval EFI_ABORTED Read block fail
160 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
171 // Read state from device, assume State is only one byte.
173 Length
= sizeof (UINT8
);
174 Status
= FvBlock
->Read (FvBlock
, Lba
, Offset
, &Length
, &State
);
175 if (EFI_ERROR (Status
)) {
179 State
^= FTW_POLARITY_REVERT
;
180 State
= (UINT8
) (State
| NewBit
);
181 State
^= FTW_POLARITY_REVERT
;
184 // Write state back to device
186 Length
= sizeof (UINT8
);
187 Status
= FvBlock
->Write (FvBlock
, Lba
, Offset
, &Length
, &State
);
193 Get the last Write record pointer.
194 The last record is the record whose 'complete' state hasn't been set.
195 After all, this header may be a EMPTY header entry for next Allocate.
198 @param FtwLiteDevice Private data of this driver
199 @param FtwLastRecord Pointer to retrieve the last write record
201 @retval EFI_SUCCESS Get the last write record successfully
202 @retval EFI_ABORTED The FTW work space is damaged
207 IN EFI_FTW_LITE_DEVICE
*FtwLiteDevice
,
208 OUT EFI_FTW_LITE_RECORD
**FtwLastRecord
211 EFI_FTW_LITE_RECORD
*Record
;
213 Record
= (EFI_FTW_LITE_RECORD
*) (FtwLiteDevice
->FtwWorkSpaceHeader
+ 1);
214 while (Record
->WriteCompleted
== FTW_VALID_STATE
) {
216 // If Offset exceed the FTW work space boudary, return error.
218 if ((UINTN
) ((UINT8
*) Record
- FtwLiteDevice
->FtwWorkSpace
) > FtwLiteDevice
->FtwWorkSpaceSize
) {
225 // Last write record is found
227 *FtwLastRecord
= Record
;
232 Read from working block to refresh the work space in memory.
235 @param FtwLiteDevice Point to private data of FTW driver
237 @retval EFI_SUCCESS The function completed successfully
238 @retval EFI_ABORTED The function could not complete successfully.
243 IN EFI_FTW_LITE_DEVICE
*FtwLiteDevice
249 EFI_FTW_LITE_RECORD
*Record
;
252 // Initialize WorkSpace as FTW_ERASED_BYTE
255 FtwLiteDevice
->FtwWorkSpace
,
256 FtwLiteDevice
->FtwWorkSpaceSize
,
261 // Read from working block
263 Length
= FtwLiteDevice
->FtwWorkSpaceSize
;
264 Status
= FtwLiteDevice
->FtwFvBlock
->Read (
265 FtwLiteDevice
->FtwFvBlock
,
266 FtwLiteDevice
->FtwWorkSpaceLba
,
267 FtwLiteDevice
->FtwWorkSpaceBase
,
269 FtwLiteDevice
->FtwWorkSpace
271 if (EFI_ERROR (Status
)) {
275 // Refresh the FtwLastRecord
277 Status
= FtwGetLastRecord (FtwLiteDevice
, &FtwLiteDevice
->FtwLastRecord
);
279 Record
= FtwLiteDevice
->FtwLastRecord
;
280 Offset
= (UINTN
) (UINT8
*) Record
- (UINTN
) FtwLiteDevice
->FtwWorkSpace
;
283 // IF work space has error or Record is out of the workspace limit, THEN
286 if (EFI_ERROR (Status
) || (Offset
+ WRITE_TOTAL_SIZE
>= FtwLiteDevice
->FtwWorkSpaceSize
)) {
288 // reclaim work space in working block.
290 Status
= FtwReclaimWorkSpace (FtwLiteDevice
);
291 if (EFI_ERROR (Status
)) {
292 DEBUG ((EFI_D_FTW_LITE
, "FtwLite: Reclaim workspace - %r\n", Status
));
301 Reclaim the work space. Get rid of all the completed write records
302 and write records in the Fault Tolerant work space.
305 @param FtwLiteDevice Point to private data of FTW driver
306 @param FtwSpaceBuffer Buffer to contain the reclaimed clean data
307 @param BufferSize Size of the FtwSpaceBuffer
309 @retval EFI_SUCCESS The function completed successfully
310 @retval EFI_BUFFER_TOO_SMALL The FtwSpaceBuffer is too small
311 @retval EFI_ABORTED The function could not complete successfully.
316 IN EFI_FTW_LITE_DEVICE
*FtwLiteDevice
,
317 IN OUT UINT8
*FtwSpaceBuffer
,
322 EFI_FTW_LITE_RECORD
*Record
;
325 // To check if the buffer is large enough
327 Length
= FtwLiteDevice
->FtwWorkSpaceSize
;
328 if (BufferSize
< Length
) {
329 return EFI_BUFFER_TOO_SMALL
;
332 // Clear the content of buffer that will save the new work space data
334 SetMem (FtwSpaceBuffer
, Length
, FTW_ERASED_BYTE
);
337 // Copy EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER to buffer
341 FtwLiteDevice
->FtwWorkSpaceHeader
,
342 sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
)
346 // Get the last record
348 Record
= FtwLiteDevice
->FtwLastRecord
;
349 if ((Record
!= NULL
) && (Record
->WriteAllocated
== FTW_VALID_STATE
) && (Record
->WriteCompleted
!= FTW_VALID_STATE
)) {
351 (UINT8
*) FtwSpaceBuffer
+ sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
),
361 Reclaim the work space on the working block.
364 @param FtwLiteDevice Point to private data of FTW driver
366 @retval EFI_SUCCESS The function completed successfully
367 @retval EFI_OUT_OF_RESOURCES Allocate memory error
368 @retval EFI_ABORTED The function could not complete successfully
372 FtwReclaimWorkSpace (
373 IN EFI_FTW_LITE_DEVICE
*FtwLiteDevice
378 UINTN TempBufferSize
;
382 UINTN SpareBufferSize
;
384 EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*WorkingBlockHeader
;
386 DEBUG ((EFI_D_FTW_LITE
, "FtwLite: start to reclaim work space\n"));
389 // Read all original data from working block to a memory buffer
391 TempBufferSize
= FtwLiteDevice
->SpareAreaLength
;
392 TempBuffer
= AllocateZeroPool (TempBufferSize
);
393 if (TempBuffer
!= NULL
) {
394 return EFI_OUT_OF_RESOURCES
;
398 for (Index
= 0; Index
< FtwLiteDevice
->NumberOfSpareBlock
; Index
+= 1) {
399 Length
= FtwLiteDevice
->SizeOfSpareBlock
;
400 Status
= FtwLiteDevice
->FtwFvBlock
->Read (
401 FtwLiteDevice
->FtwFvBlock
,
402 FtwLiteDevice
->FtwWorkBlockLba
+ Index
,
407 if (EFI_ERROR (Status
)) {
408 FreePool (TempBuffer
);
415 // Clean up the workspace, remove all the completed records.
418 ((UINTN
) (FtwLiteDevice
->FtwWorkSpaceLba
- FtwLiteDevice
->FtwWorkBlockLba
)) *
419 FtwLiteDevice
->SizeOfSpareBlock
+ FtwLiteDevice
->FtwWorkSpaceBase
;
421 Status
= CleanupWorkSpace (
424 FtwLiteDevice
->FtwWorkSpaceSize
428 FtwLiteDevice
->FtwWorkSpace
,
430 FtwLiteDevice
->FtwWorkSpaceSize
433 Status
= FtwGetLastRecord (FtwLiteDevice
, &FtwLiteDevice
->FtwLastRecord
);
436 // Set the WorkingBlockValid and WorkingBlockInvalid as INVALID
438 WorkingBlockHeader
= (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*) Ptr
;
439 WorkingBlockHeader
->WorkingBlockValid
= FTW_INVALID_STATE
;
440 WorkingBlockHeader
->WorkingBlockInvalid
= FTW_INVALID_STATE
;
443 // Try to keep the content of spare block
444 // Save spare block into a spare backup memory buffer (Sparebuffer)
446 SpareBufferSize
= FtwLiteDevice
->SpareAreaLength
;
447 SpareBuffer
= AllocatePool (SpareBufferSize
);
448 if (SpareBuffer
== NULL
) {
449 FreePool (TempBuffer
);
450 return EFI_OUT_OF_RESOURCES
;
454 for (Index
= 0; Index
< FtwLiteDevice
->NumberOfSpareBlock
; Index
+= 1) {
455 Length
= FtwLiteDevice
->SizeOfSpareBlock
;
456 Status
= FtwLiteDevice
->FtwBackupFvb
->Read (
457 FtwLiteDevice
->FtwBackupFvb
,
458 FtwLiteDevice
->FtwSpareLba
+ Index
,
463 if (EFI_ERROR (Status
)) {
464 FreePool (TempBuffer
);
465 FreePool (SpareBuffer
);
472 // Write the memory buffer to spare block
474 Status
= FtwEraseSpareBlock (FtwLiteDevice
);
476 for (Index
= 0; Index
< FtwLiteDevice
->NumberOfSpareBlock
; Index
+= 1) {
477 Length
= FtwLiteDevice
->SizeOfSpareBlock
;
478 Status
= FtwLiteDevice
->FtwBackupFvb
->Write (
479 FtwLiteDevice
->FtwBackupFvb
,
480 FtwLiteDevice
->FtwSpareLba
+ Index
,
485 if (EFI_ERROR (Status
)) {
486 FreePool (TempBuffer
);
487 FreePool (SpareBuffer
);
496 FreePool (TempBuffer
);
499 // Write the spare block to working block
501 Status
= FlushSpareBlockToWorkingBlock (FtwLiteDevice
);
502 if (EFI_ERROR (Status
)) {
503 FreePool (SpareBuffer
);
507 // Restore spare backup buffer into spare block , if no failure happened during FtwWrite.
509 Status
= FtwEraseSpareBlock (FtwLiteDevice
);
511 for (Index
= 0; Index
< FtwLiteDevice
->NumberOfSpareBlock
; Index
+= 1) {
512 Length
= FtwLiteDevice
->SizeOfSpareBlock
;
513 Status
= FtwLiteDevice
->FtwBackupFvb
->Write (
514 FtwLiteDevice
->FtwBackupFvb
,
515 FtwLiteDevice
->FtwSpareLba
+ Index
,
520 if (EFI_ERROR (Status
)) {
521 FreePool (SpareBuffer
);
528 FreePool (SpareBuffer
);
530 DEBUG ((EFI_D_FTW_LITE
, "FtwLite: reclaim work space success\n"));