]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/FaultTolerantWriteDxe/FtwMisc.c
Update the copyright notice format
[mirror_edk2.git] / MdeModulePkg / Universal / FaultTolerantWriteDxe / FtwMisc.c
1 /** @file
2
3 Internal generic functions to operate flash block.
4
5 Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>
6 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
10
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.
13
14 **/
15
16 #include "FaultTolerantWrite.h"
17
18 /**
19
20 Check whether a flash buffer is erased.
21
22 @param Buffer Buffer to check
23 @param BufferSize Size of the buffer
24
25 @return A BOOLEAN value indicating erased or not.
26
27 **/
28 BOOLEAN
29 IsErasedFlashBuffer (
30 IN UINT8 *Buffer,
31 IN UINTN BufferSize
32 )
33 {
34 BOOLEAN IsEmpty;
35 UINT8 *Ptr;
36 UINTN Index;
37
38 Ptr = Buffer;
39 IsEmpty = TRUE;
40 for (Index = 0; Index < BufferSize; Index += 1) {
41 if (*Ptr++ != FTW_ERASED_BYTE) {
42 IsEmpty = FALSE;
43 break;
44 }
45 }
46
47 return IsEmpty;
48 }
49
50 /**
51 To erase the block with the spare block size.
52
53
54 @param FtwDevice The private data of FTW driver
55 @param FvBlock FVB Protocol interface
56 @param Lba Lba of the firmware block
57
58 @retval EFI_SUCCESS Block LBA is Erased successfully
59 @retval Others Error occurs
60
61 **/
62 EFI_STATUS
63 FtwEraseBlock (
64 IN EFI_FTW_DEVICE *FtwDevice,
65 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
66 EFI_LBA Lba
67 )
68 {
69 return FvBlock->EraseBlocks (
70 FvBlock,
71 Lba,
72 FtwDevice->NumberOfSpareBlock,
73 EFI_LBA_LIST_TERMINATOR
74 );
75 }
76
77 /**
78 Erase spare block.
79
80 @param FtwDevice The private data of FTW driver
81
82 @retval EFI_SUCCESS The erase request was successfully completed.
83 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
84 @retval EFI_DEVICE_ERROR The block device is not functioning
85 correctly and could not be written.
86 The firmware device may have been
87 partially erased.
88 @retval EFI_INVALID_PARAMETER One or more of the LBAs listed
89 in the variable argument list do
90 not exist in the firmware volume.
91
92
93 **/
94 EFI_STATUS
95 FtwEraseSpareBlock (
96 IN EFI_FTW_DEVICE *FtwDevice
97 )
98 {
99 return FtwDevice->FtwBackupFvb->EraseBlocks (
100 FtwDevice->FtwBackupFvb,
101 FtwDevice->FtwSpareLba,
102 FtwDevice->NumberOfSpareBlock,
103 EFI_LBA_LIST_TERMINATOR
104 );
105 }
106
107 /**
108 Retrive the proper FVB protocol interface by HANDLE.
109
110
111 @param FvBlockHandle The handle of FVB protocol that provides services for
112 reading, writing, and erasing the target block.
113 @param FvBlock The interface of FVB protocol
114
115 @retval EFI_SUCCESS The function completed successfully
116 @retval EFI_ABORTED The function could not complete successfully
117
118 **/
119 EFI_STATUS
120 FtwGetFvbByHandle (
121 IN EFI_HANDLE FvBlockHandle,
122 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock
123 )
124 {
125 //
126 // To get the FVB protocol interface on the handle
127 //
128 return gBS->HandleProtocol (
129 FvBlockHandle,
130 &gEfiFirmwareVolumeBlockProtocolGuid,
131 (VOID **) FvBlock
132 );
133 }
134
135 /**
136
137 Is it in working block?
138
139 @param FtwDevice The private data of FTW driver
140 @param FvBlock Fvb protocol instance
141 @param Lba The block specified
142
143 @return A BOOLEAN value indicating in working block or not.
144
145 **/
146 BOOLEAN
147 IsWorkingBlock (
148 EFI_FTW_DEVICE *FtwDevice,
149 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
150 EFI_LBA Lba
151 )
152 {
153 //
154 // If matching the following condition, the target block is in working block.
155 // 1. Target block is on the FV of working block (Using the same FVB protocol instance).
156 // 2. Lba falls into the range of working block.
157 //
158 return (BOOLEAN)
159 (
160 (FvBlock == FtwDevice->FtwFvBlock) &&
161 (Lba >= FtwDevice->FtwWorkBlockLba) &&
162 (Lba <= FtwDevice->FtwWorkSpaceLba)
163 );
164 }
165
166 /**
167
168 Get firmware block by address.
169
170
171 @param Address Address specified the block
172 @param FvBlock The block caller wanted
173
174 @retval EFI_SUCCESS The protocol instance if found.
175 @retval EFI_NOT_FOUND Block not found
176
177 **/
178 EFI_HANDLE
179 GetFvbByAddress (
180 IN EFI_PHYSICAL_ADDRESS Address,
181 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock
182 )
183 {
184 EFI_STATUS Status;
185 EFI_HANDLE *HandleBuffer;
186 UINTN HandleCount;
187 UINTN Index;
188 EFI_PHYSICAL_ADDRESS FvbBaseAddress;
189 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
190 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
191 EFI_HANDLE FvbHandle;
192
193 *FvBlock = NULL;
194 FvbHandle = NULL;
195 //
196 // Locate all handles of Fvb protocol
197 //
198 Status = gBS->LocateHandleBuffer (
199 ByProtocol,
200 &gEfiFirmwareVolumeBlockProtocolGuid,
201 NULL,
202 &HandleCount,
203 &HandleBuffer
204 );
205 if (EFI_ERROR (Status)) {
206 return NULL;
207 }
208 //
209 // Get the FVB to access variable store
210 //
211 for (Index = 0; Index < HandleCount; Index += 1) {
212 Status = gBS->HandleProtocol (
213 HandleBuffer[Index],
214 &gEfiFirmwareVolumeBlockProtocolGuid,
215 (VOID **) &Fvb
216 );
217 if (EFI_ERROR (Status)) {
218 break;
219 }
220 //
221 // Compare the address and select the right one
222 //
223 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);
224 if (EFI_ERROR (Status)) {
225 continue;
226 }
227
228 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);
229 if ((Address >= FvbBaseAddress) && (Address <= (FvbBaseAddress + (FwVolHeader->FvLength - 1)))) {
230 *FvBlock = Fvb;
231 FvbHandle = HandleBuffer[Index];
232 break;
233 }
234 }
235
236 FreePool (HandleBuffer);
237 return FvbHandle;
238 }
239
240 /**
241
242 Is it in boot block?
243
244 @param FtwDevice The private data of FTW driver
245 @param FvBlock Fvb protocol instance
246 @param Lba The block specified
247
248 @return A BOOLEAN value indicating in boot block or not.
249
250 **/
251 BOOLEAN
252 IsBootBlock (
253 EFI_FTW_DEVICE *FtwDevice,
254 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
255 EFI_LBA Lba
256 )
257 {
258 EFI_STATUS Status;
259 EFI_SWAP_ADDRESS_RANGE_PROTOCOL *SarProtocol;
260 EFI_PHYSICAL_ADDRESS BootBlockBase;
261 UINTN BootBlockSize;
262 EFI_PHYSICAL_ADDRESS BackupBlockBase;
263 UINTN BackupBlockSize;
264 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *BootFvb;
265 BOOLEAN IsSwapped;
266 EFI_HANDLE FvbHandle;
267
268 if (!FeaturePcdGet(PcdFullFtwServiceEnable)) {
269 return FALSE;
270 }
271
272 Status = gBS->LocateProtocol (&gEfiSwapAddressRangeProtocolGuid, NULL, (VOID **) &SarProtocol);
273 if (EFI_ERROR (Status)) {
274 return FALSE;
275 }
276 //
277 // Get the boot block range
278 //
279 Status = SarProtocol->GetRangeLocation (
280 SarProtocol,
281 &BootBlockBase,
282 &BootBlockSize,
283 &BackupBlockBase,
284 &BackupBlockSize
285 );
286 if (EFI_ERROR (Status)) {
287 return FALSE;
288 }
289
290 Status = SarProtocol->GetSwapState (SarProtocol, &IsSwapped);
291 if (EFI_ERROR (Status)) {
292 return FALSE;
293 }
294 //
295 // Get FVB by address
296 //
297 if (!IsSwapped) {
298 FvbHandle = GetFvbByAddress (BootBlockBase, &BootFvb);
299 } else {
300 FvbHandle = GetFvbByAddress (BackupBlockBase, &BootFvb);
301 }
302
303 if (FvbHandle == NULL) {
304 return FALSE;
305 }
306 //
307 // Compare the Fvb
308 //
309 return (BOOLEAN) (FvBlock == BootFvb);
310 }
311
312 /**
313 Copy the content of spare block to a boot block. Size is FTW_BLOCK_SIZE.
314 Spare block is accessed by FTW working FVB protocol interface. LBA is 1.
315 Target block is accessed by FvbBlock protocol interface. LBA is Lba.
316
317 FTW will do extra work on boot block update.
318 FTW should depend on a protocol of EFI_ADDRESS_RANGE_SWAP_PROTOCOL,
319 which is produced by a chipset driver.
320 FTW updating boot block steps may be:
321 1. GetRangeLocation(), if the Range is inside the boot block, FTW know
322 that boot block will be update. It shall add a FLAG in the working block.
323 2. When spare block is ready,
324 3. SetSwapState(EFI_SWAPPED)
325 4. erasing boot block,
326 5. programming boot block until the boot block is ok.
327 6. SetSwapState(UNSWAPPED)
328 FTW shall not allow to update boot block when battery state is error.
329
330 @param FtwDevice The private data of FTW driver
331
332 @retval EFI_SUCCESS Spare block content is copied to boot block
333 @retval EFI_INVALID_PARAMETER Input parameter error
334 @retval EFI_OUT_OF_RESOURCES Allocate memory error
335 @retval EFI_ABORTED The function could not complete successfully
336
337 **/
338 EFI_STATUS
339 FlushSpareBlockToBootBlock (
340 EFI_FTW_DEVICE *FtwDevice
341 )
342 {
343 EFI_STATUS Status;
344 UINTN Length;
345 UINT8 *Buffer;
346 UINTN Count;
347 UINT8 *Ptr;
348 UINTN Index;
349 BOOLEAN TopSwap;
350 EFI_SWAP_ADDRESS_RANGE_PROTOCOL *SarProtocol;
351 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *BootFvb;
352 EFI_LBA BootLba;
353
354 if (!FeaturePcdGet(PcdFullFtwServiceEnable)) {
355 return EFI_UNSUPPORTED;
356 }
357
358 //
359 // Locate swap address range protocol
360 //
361 Status = gBS->LocateProtocol (&gEfiSwapAddressRangeProtocolGuid, NULL, (VOID **) &SarProtocol);
362 if (EFI_ERROR (Status)) {
363 return Status;
364 }
365 //
366 // Allocate a memory buffer
367 //
368 Length = FtwDevice->SpareAreaLength;
369 Buffer = AllocatePool (Length);
370 if (Buffer == NULL) {
371 return EFI_OUT_OF_RESOURCES;
372 }
373 //
374 // Get TopSwap bit state
375 //
376 Status = SarProtocol->GetSwapState (SarProtocol, &TopSwap);
377 if (EFI_ERROR (Status)) {
378 DEBUG ((EFI_D_ERROR, "Ftw: Get Top Swapped status - %r\n", Status));
379 FreePool (Buffer);
380 return EFI_ABORTED;
381 }
382
383 if (TopSwap) {
384 //
385 // Get FVB of current boot block
386 //
387 if (GetFvbByAddress (FtwDevice->SpareAreaAddress + FtwDevice->SpareAreaLength, &BootFvb) == NULL) {
388 FreePool (Buffer);
389 return EFI_ABORTED;
390 }
391 //
392 // Read data from current boot block
393 //
394 BootLba = 0;
395 Ptr = Buffer;
396 for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
397 Count = FtwDevice->BlockSize;
398 Status = BootFvb->Read (
399 BootFvb,
400 BootLba + Index,
401 0,
402 &Count,
403 Ptr
404 );
405 if (EFI_ERROR (Status)) {
406 FreePool (Buffer);
407 return Status;
408 }
409
410 Ptr += Count;
411 }
412 } else {
413 //
414 // Read data from spare block
415 //
416 Ptr = Buffer;
417 for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
418 Count = FtwDevice->BlockSize;
419 Status = FtwDevice->FtwBackupFvb->Read (
420 FtwDevice->FtwBackupFvb,
421 FtwDevice->FtwSpareLba + Index,
422 0,
423 &Count,
424 Ptr
425 );
426 if (EFI_ERROR (Status)) {
427 FreePool (Buffer);
428 return Status;
429 }
430
431 Ptr += Count;
432 }
433 //
434 // Set TopSwap bit
435 //
436 Status = SarProtocol->SetSwapState (SarProtocol, TRUE);
437 if (EFI_ERROR (Status)) {
438 FreePool (Buffer);
439 return Status;
440 }
441 }
442 //
443 // Erase current spare block
444 // Because TopSwap is set, this actually erase the top block (boot block)!
445 //
446 Status = FtwEraseSpareBlock (FtwDevice);
447 if (EFI_ERROR (Status)) {
448 FreePool (Buffer);
449 return EFI_ABORTED;
450 }
451 //
452 // Write memory buffer currenet spare block. Still top block.
453 //
454 Ptr = Buffer;
455 for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
456 Count = FtwDevice->BlockSize;
457 Status = FtwDevice->FtwBackupFvb->Write (
458 FtwDevice->FtwBackupFvb,
459 FtwDevice->FtwSpareLba + Index,
460 0,
461 &Count,
462 Ptr
463 );
464 if (EFI_ERROR (Status)) {
465 DEBUG ((EFI_D_ERROR, "Ftw: FVB Write boot block - %r\n", Status));
466 FreePool (Buffer);
467 return Status;
468 }
469
470 Ptr += Count;
471 }
472
473 FreePool (Buffer);
474
475 //
476 // Clear TopSwap bit
477 //
478 Status = SarProtocol->SetSwapState (SarProtocol, FALSE);
479
480 return Status;
481 }
482
483 /**
484 Copy the content of spare block to a target block. Size is FTW_BLOCK_SIZE.
485 Spare block is accessed by FTW backup FVB protocol interface. LBA is 1.
486 Target block is accessed by FvbBlock protocol interface. LBA is Lba.
487
488
489 @param FtwDevice The private data of FTW driver
490 @param FvBlock FVB Protocol interface to access target block
491 @param Lba Lba of the target block
492
493 @retval EFI_SUCCESS Spare block content is copied to target block
494 @retval EFI_INVALID_PARAMETER Input parameter error
495 @retval EFI_OUT_OF_RESOURCES Allocate memory error
496 @retval EFI_ABORTED The function could not complete successfully
497
498 **/
499 EFI_STATUS
500 FlushSpareBlockToTargetBlock (
501 EFI_FTW_DEVICE *FtwDevice,
502 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
503 EFI_LBA Lba
504 )
505 {
506 EFI_STATUS Status;
507 UINTN Length;
508 UINT8 *Buffer;
509 UINTN Count;
510 UINT8 *Ptr;
511 UINTN Index;
512
513 if ((FtwDevice == NULL) || (FvBlock == NULL)) {
514 return EFI_INVALID_PARAMETER;
515 }
516 //
517 // Allocate a memory buffer
518 //
519 Length = FtwDevice->SpareAreaLength;
520 Buffer = AllocatePool (Length);
521 if (Buffer == NULL) {
522 return EFI_OUT_OF_RESOURCES;
523 }
524 //
525 // Read all content of spare block to memory buffer
526 //
527 Ptr = Buffer;
528 for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
529 Count = FtwDevice->BlockSize;
530 Status = FtwDevice->FtwBackupFvb->Read (
531 FtwDevice->FtwBackupFvb,
532 FtwDevice->FtwSpareLba + Index,
533 0,
534 &Count,
535 Ptr
536 );
537 if (EFI_ERROR (Status)) {
538 FreePool (Buffer);
539 return Status;
540 }
541
542 Ptr += Count;
543 }
544 //
545 // Erase the target block
546 //
547 Status = FtwEraseBlock (FtwDevice, FvBlock, Lba);
548 if (EFI_ERROR (Status)) {
549 FreePool (Buffer);
550 return EFI_ABORTED;
551 }
552 //
553 // Write memory buffer to block, using the FvbBlock protocol interface
554 //
555 Ptr = Buffer;
556 for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
557 Count = FtwDevice->BlockSize;
558 Status = FvBlock->Write (FvBlock, Lba + Index, 0, &Count, Ptr);
559 if (EFI_ERROR (Status)) {
560 DEBUG ((EFI_D_ERROR, "Ftw: FVB Write block - %r\n", Status));
561 FreePool (Buffer);
562 return Status;
563 }
564
565 Ptr += Count;
566 }
567
568 FreePool (Buffer);
569
570 return Status;
571 }
572
573 /**
574 Copy the content of spare block to working block. Size is FTW_BLOCK_SIZE.
575 Spare block is accessed by FTW backup FVB protocol interface. LBA is
576 FtwDevice->FtwSpareLba.
577 Working block is accessed by FTW working FVB protocol interface. LBA is
578 FtwDevice->FtwWorkBlockLba.
579
580 Since the working block header is important when FTW initializes, the
581 state of the operation should be handled carefully. The Crc value is
582 calculated without STATE element.
583
584 @param FtwDevice The private data of FTW driver
585
586 @retval EFI_SUCCESS Spare block content is copied to target block
587 @retval EFI_OUT_OF_RESOURCES Allocate memory error
588 @retval EFI_ABORTED The function could not complete successfully
589
590 **/
591 EFI_STATUS
592 FlushSpareBlockToWorkingBlock (
593 EFI_FTW_DEVICE *FtwDevice
594 )
595 {
596 EFI_STATUS Status;
597 UINTN Length;
598 UINT8 *Buffer;
599 EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingBlockHeader;
600 UINTN Count;
601 UINT8 *Ptr;
602 UINTN Index;
603 EFI_LBA WorkSpaceLbaOffset;
604
605 //
606 // Allocate a memory buffer
607 //
608 Length = FtwDevice->SpareAreaLength;
609 Buffer = AllocatePool (Length);
610 if (Buffer == NULL) {
611 return EFI_OUT_OF_RESOURCES;
612 }
613 //
614 // To guarantee that the WorkingBlockValid is set on spare block
615 //
616 // Offset = OFFSET_OF(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER,
617 // WorkingBlockValid);
618 // To skip Signature and Crc: sizeof(EFI_GUID)+sizeof(UINT32).
619 //
620 FtwUpdateFvState (
621 FtwDevice->FtwBackupFvb,
622 FtwDevice->FtwWorkSpaceLba,
623 FtwDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32),
624 WORKING_BLOCK_VALID
625 );
626 //
627 // Read from spare block to memory buffer
628 //
629 Ptr = Buffer;
630 for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
631 Count = FtwDevice->BlockSize;
632 Status = FtwDevice->FtwBackupFvb->Read (
633 FtwDevice->FtwBackupFvb,
634 FtwDevice->FtwSpareLba + Index,
635 0,
636 &Count,
637 Ptr
638 );
639 if (EFI_ERROR (Status)) {
640 FreePool (Buffer);
641 return Status;
642 }
643
644 Ptr += Count;
645 }
646 //
647 // Clear the CRC and STATE, copy data from spare to working block.
648 //
649 WorkSpaceLbaOffset = FtwDevice->FtwWorkSpaceLba - FtwDevice->FtwWorkBlockLba;
650 WorkingBlockHeader = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *) (Buffer + (UINTN) WorkSpaceLbaOffset * FtwDevice->BlockSize + FtwDevice->FtwWorkSpaceBase);
651 InitWorkSpaceHeader (WorkingBlockHeader);
652 WorkingBlockHeader->WorkingBlockValid = FTW_ERASE_POLARITY;
653 WorkingBlockHeader->WorkingBlockInvalid = FTW_ERASE_POLARITY;
654
655 //
656 // target block is working block, then
657 // Set WorkingBlockInvalid in EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
658 // before erase the working block.
659 //
660 // Offset = OFFSET_OF(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER,
661 // WorkingBlockInvalid);
662 // So hardcode offset as sizeof(EFI_GUID)+sizeof(UINT32) to
663 // skip Signature and Crc.
664 //
665 Status = FtwUpdateFvState (
666 FtwDevice->FtwFvBlock,
667 FtwDevice->FtwWorkSpaceLba,
668 FtwDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32),
669 WORKING_BLOCK_INVALID
670 );
671 if (EFI_ERROR (Status)) {
672 FreePool (Buffer);
673 return EFI_ABORTED;
674 }
675
676 FtwDevice->FtwWorkSpaceHeader->WorkingBlockInvalid = FTW_VALID_STATE;
677
678 //
679 // Erase the working block
680 //
681 Status = FtwEraseBlock (FtwDevice, FtwDevice->FtwFvBlock, FtwDevice->FtwWorkBlockLba);
682 if (EFI_ERROR (Status)) {
683 FreePool (Buffer);
684 return EFI_ABORTED;
685 }
686 //
687 // Write memory buffer to working block, using the FvbBlock protocol interface
688 //
689 Ptr = Buffer;
690 for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
691 Count = FtwDevice->BlockSize;
692 Status = FtwDevice->FtwFvBlock->Write (
693 FtwDevice->FtwFvBlock,
694 FtwDevice->FtwWorkBlockLba + Index,
695 0,
696 &Count,
697 Ptr
698 );
699 if (EFI_ERROR (Status)) {
700 DEBUG ((EFI_D_ERROR, "Ftw: FVB Write block - %r\n", Status));
701 FreePool (Buffer);
702 return Status;
703 }
704
705 Ptr += Count;
706 }
707 //
708 // Since the memory buffer will not be used, free memory Buffer.
709 //
710 FreePool (Buffer);
711
712 //
713 // Update the VALID of the working block
714 //
715 // Offset = OFFSET_OF(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER, WorkingBlockValid);
716 // So hardcode offset as sizeof(EFI_GUID)+sizeof(UINT32) to skip Signature and Crc.
717 //
718 Status = FtwUpdateFvState (
719 FtwDevice->FtwFvBlock,
720 FtwDevice->FtwWorkSpaceLba,
721 FtwDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32),
722 WORKING_BLOCK_VALID
723 );
724 if (EFI_ERROR (Status)) {
725 return EFI_ABORTED;
726 }
727
728 FtwDevice->FtwWorkSpaceHeader->WorkingBlockValid = FTW_VALID_STATE;
729
730 return EFI_SUCCESS;
731 }
732
733 /**
734 Update a bit of state on a block device. The location of the bit is
735 calculated by the (Lba, Offset, bit). Here bit is determined by the
736 the name of a certain bit.
737
738
739 @param FvBlock FVB Protocol interface to access SrcBlock and DestBlock
740 @param Lba Lba of a block
741 @param Offset Offset on the Lba
742 @param NewBit New value that will override the old value if it can be change
743
744 @retval EFI_SUCCESS A state bit has been updated successfully
745 @retval Others Access block device error.
746 Notes:
747 Assume all bits of State are inside the same BYTE.
748 @retval EFI_ABORTED Read block fail
749
750 **/
751 EFI_STATUS
752 FtwUpdateFvState (
753 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
754 IN EFI_LBA Lba,
755 IN UINTN Offset,
756 IN UINT8 NewBit
757 )
758 {
759 EFI_STATUS Status;
760 UINT8 State;
761 UINTN Length;
762
763 //
764 // Read state from device, assume State is only one byte.
765 //
766 Length = sizeof (UINT8);
767 Status = FvBlock->Read (FvBlock, Lba, Offset, &Length, &State);
768 if (EFI_ERROR (Status)) {
769 return EFI_ABORTED;
770 }
771
772 State ^= FTW_POLARITY_REVERT;
773 State = (UINT8) (State | NewBit);
774 State ^= FTW_POLARITY_REVERT;
775
776 //
777 // Write state back to device
778 //
779 Length = sizeof (UINT8);
780 Status = FvBlock->Write (FvBlock, Lba, Offset, &Length, &State);
781
782 return Status;
783 }
784
785 /**
786 Get the last Write Header pointer.
787 The last write header is the header whose 'complete' state hasn't been set.
788 After all, this header may be a EMPTY header entry for next Allocate.
789
790
791 @param FtwWorkSpaceHeader Pointer of the working block header
792 @param FtwWorkSpaceSize Size of the work space
793 @param FtwWriteHeader Pointer to retrieve the last write header
794
795 @retval EFI_SUCCESS Get the last write record successfully
796 @retval EFI_ABORTED The FTW work space is damaged
797
798 **/
799 EFI_STATUS
800 FtwGetLastWriteHeader (
801 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *FtwWorkSpaceHeader,
802 IN UINTN FtwWorkSpaceSize,
803 OUT EFI_FAULT_TOLERANT_WRITE_HEADER **FtwWriteHeader
804 )
805 {
806 UINTN Offset;
807 EFI_FAULT_TOLERANT_WRITE_HEADER *FtwHeader;
808
809 *FtwWriteHeader = NULL;
810 FtwHeader = (EFI_FAULT_TOLERANT_WRITE_HEADER *) (FtwWorkSpaceHeader + 1);
811 Offset = sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER);
812
813 while (FtwHeader->Complete == FTW_VALID_STATE) {
814 Offset += WRITE_TOTAL_SIZE (FtwHeader->NumberOfWrites, FtwHeader->PrivateDataSize);
815 //
816 // If Offset exceed the FTW work space boudary, return error.
817 //
818 if (Offset > FtwWorkSpaceSize) {
819 *FtwWriteHeader = FtwHeader;
820 return EFI_ABORTED;
821 }
822
823 FtwHeader = (EFI_FAULT_TOLERANT_WRITE_HEADER *) ((UINT8 *) FtwWorkSpaceHeader + Offset);
824 }
825 //
826 // Last write header is found
827 //
828 *FtwWriteHeader = FtwHeader;
829
830 return EFI_SUCCESS;
831 }
832
833 /**
834 Get the last Write Record pointer. The last write Record is the Record
835 whose DestinationCompleted state hasn't been set. After all, this Record
836 may be a EMPTY record entry for next write.
837
838
839 @param FtwWriteHeader Pointer to the write record header
840 @param FtwWriteRecord Pointer to retrieve the last write record
841
842 @retval EFI_SUCCESS Get the last write record successfully
843 @retval EFI_ABORTED The FTW work space is damaged
844
845 **/
846 EFI_STATUS
847 FtwGetLastWriteRecord (
848 IN EFI_FAULT_TOLERANT_WRITE_HEADER *FtwWriteHeader,
849 OUT EFI_FAULT_TOLERANT_WRITE_RECORD **FtwWriteRecord
850 )
851 {
852 UINTN Index;
853 EFI_FAULT_TOLERANT_WRITE_RECORD *FtwRecord;
854
855 *FtwWriteRecord = NULL;
856 FtwRecord = (EFI_FAULT_TOLERANT_WRITE_RECORD *) (FtwWriteHeader + 1);
857
858 //
859 // Try to find the last write record "that has not completed"
860 //
861 for (Index = 0; Index < FtwWriteHeader->NumberOfWrites; Index += 1) {
862 if (FtwRecord->DestinationComplete != FTW_VALID_STATE) {
863 //
864 // The last write record is found
865 //
866 *FtwWriteRecord = FtwRecord;
867 return EFI_SUCCESS;
868 }
869
870 FtwRecord++;
871
872 if (FtwWriteHeader->PrivateDataSize != 0) {
873 FtwRecord = (EFI_FAULT_TOLERANT_WRITE_RECORD *) ((UINTN) FtwRecord + FtwWriteHeader->PrivateDataSize);
874 }
875 }
876 //
877 // if Index == NumberOfWrites, then
878 // the last record has been written successfully,
879 // but the Header->Complete Flag has not been set.
880 // also return the last record.
881 //
882 if (Index == FtwWriteHeader->NumberOfWrites) {
883 *FtwWriteRecord = (EFI_FAULT_TOLERANT_WRITE_RECORD *) ((UINTN) FtwRecord - RECORD_SIZE (FtwWriteHeader->PrivateDataSize));
884 return EFI_SUCCESS;
885 }
886
887 return EFI_ABORTED;
888 }
889
890 /**
891 To check if FtwRecord is the first record of FtwHeader.
892
893 @param FtwHeader Pointer to the write record header
894 @param FtwRecord Pointer to the write record
895
896 @retval TRUE FtwRecord is the first Record of the FtwHeader
897 @retval FALSE FtwRecord is not the first Record of the FtwHeader
898
899 **/
900 BOOLEAN
901 IsFirstRecordOfWrites (
902 IN EFI_FAULT_TOLERANT_WRITE_HEADER *FtwHeader,
903 IN EFI_FAULT_TOLERANT_WRITE_RECORD *FtwRecord
904 )
905 {
906 UINT8 *Head;
907 UINT8 *Ptr;
908
909 Head = (UINT8 *) FtwHeader;
910 Ptr = (UINT8 *) FtwRecord;
911
912 Head += sizeof (EFI_FAULT_TOLERANT_WRITE_HEADER);
913 return (BOOLEAN) (Head == Ptr);
914 }
915
916 /**
917 To check if FtwRecord is the last record of FtwHeader. Because the
918 FtwHeader has NumberOfWrites & PrivateDataSize, the FtwRecord can be
919 determined if it is the last record of FtwHeader.
920
921 @param FtwHeader Pointer to the write record header
922 @param FtwRecord Pointer to the write record
923
924 @retval TRUE FtwRecord is the last Record of the FtwHeader
925 @retval FALSE FtwRecord is not the last Record of the FtwHeader
926
927 **/
928 BOOLEAN
929 IsLastRecordOfWrites (
930 IN EFI_FAULT_TOLERANT_WRITE_HEADER *FtwHeader,
931 IN EFI_FAULT_TOLERANT_WRITE_RECORD *FtwRecord
932 )
933 {
934 UINT8 *Head;
935 UINT8 *Ptr;
936
937 Head = (UINT8 *) FtwHeader;
938 Ptr = (UINT8 *) FtwRecord;
939
940 Head += WRITE_TOTAL_SIZE (FtwHeader->NumberOfWrites - 1, FtwHeader->PrivateDataSize);
941 return (BOOLEAN) (Head == Ptr);
942 }
943
944 /**
945 To check if FtwRecord is the first record of FtwHeader.
946
947 @param FtwHeader Pointer to the write record header
948 @param FtwRecord Pointer to retrieve the previous write record
949
950 @retval EFI_ACCESS_DENIED Input record is the first record, no previous record is return.
951 @retval EFI_SUCCESS The previous write record is found.
952
953 **/
954 EFI_STATUS
955 GetPreviousRecordOfWrites (
956 IN EFI_FAULT_TOLERANT_WRITE_HEADER *FtwHeader,
957 IN OUT EFI_FAULT_TOLERANT_WRITE_RECORD **FtwRecord
958 )
959 {
960 UINT8 *Ptr;
961
962 if (IsFirstRecordOfWrites (FtwHeader, *FtwRecord)) {
963 *FtwRecord = NULL;
964 return EFI_ACCESS_DENIED;
965 }
966
967 Ptr = (UINT8 *) (*FtwRecord);
968 Ptr -= RECORD_SIZE (FtwHeader->PrivateDataSize);
969 *FtwRecord = (EFI_FAULT_TOLERANT_WRITE_RECORD *) Ptr;
970 return EFI_SUCCESS;
971 }