3 Copyright (c) 2006 - 2007, 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.
23 // The package level header files this module uses
28 // The protocols, PPI and GUID defintions for this module
30 #include <Guid/EventGroup.h>
31 #include <Protocol/FvbExtension.h>
32 #include <Protocol/FirmwareVolumeBlock.h>
33 #include <Guid/AlternateFvBlock.h>
34 #include <Protocol/DevicePath.h>
36 // The Library classes this module consumes
38 #include <Library/UefiLib.h>
39 #include <Library/UefiDriverEntryPoint.h>
40 #include <Library/BaseLib.h>
41 #include <Library/DxeServicesTableLib.h>
42 #include <Library/UefiRuntimeLib.h>
43 #include <Library/DebugLib.h>
44 #include <Library/HobLib.h>
45 #include <Library/BaseMemoryLib.h>
46 #include <Library/MemoryAllocationLib.h>
47 #include <Library/UefiBootServicesTableLib.h>
49 #include "FWBlockService.h"
51 #define EFI_FVB2_STATUS (EFI_FVB2_READ_STATUS | EFI_FVB2_WRITE_STATUS | EFI_FVB2_LOCK_STATUS)
53 ESAL_FWB_GLOBAL
*mFvbModuleGlobal
;
55 EFI_FW_VOL_BLOCK_DEVICE mFvbDeviceTemplate
= {
63 sizeof (MEMMAP_DEVICE_PATH
),
73 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
75 sizeof (EFI_DEVICE_PATH_PROTOCOL
),
82 FvbProtocolGetAttributes
,
83 FvbProtocolSetAttributes
,
84 FvbProtocolGetPhysicalAddress
,
85 FvbProtocolGetBlockSize
,
88 FvbProtocolEraseBlocks
,
92 FvbExtendProtocolEraseCustomBlockRange
100 FvbVirtualddressChangeEvent (
108 Fixup internal data so that EFI and SAL can be call in virtual mode.
109 Call the passed in Child Notify event and convert the mFvbModuleGlobal
110 date items to there virtual address.
112 mFvbModuleGlobal->FvInstance[FVB_PHYSICAL] - Physical copy of instance data
113 mFvbModuleGlobal->FvInstance[FVB_VIRTUAL] - Virtual pointer to common
118 (Standard EFI notify event - EFI_EVENT_NOTIFY)
126 EFI_FW_VOL_INSTANCE
*FwhInstance
;
129 EfiConvertPointer (0x0, (VOID
**) &mFvbModuleGlobal
->FvInstance
[FVB_VIRTUAL
]);
132 // Convert the base address of all the instances
135 FwhInstance
= mFvbModuleGlobal
->FvInstance
[FVB_PHYSICAL
];
136 while (Index
< mFvbModuleGlobal
->NumFv
) {
137 EfiConvertPointer (0x0, (VOID
**) &FwhInstance
->FvBase
[FVB_VIRTUAL
]);
138 FwhInstance
= (EFI_FW_VOL_INSTANCE
*)
140 (UINTN
) ((UINT8
*) FwhInstance
) + FwhInstance
->VolumeHeader
.HeaderLength
+
141 (sizeof (EFI_FW_VOL_INSTANCE
) - sizeof (EFI_FIRMWARE_VOLUME_HEADER
))
146 EfiConvertPointer (0x0, (VOID
**) &mFvbModuleGlobal
->FvbScratchSpace
[FVB_VIRTUAL
]);
147 EfiConvertPointer (0x0, (VOID
**) &mFvbModuleGlobal
);
153 IN ESAL_FWB_GLOBAL
*Global
,
154 OUT EFI_FW_VOL_INSTANCE
**FwhInstance
,
160 Retrieves the physical address of a memory mapped FV
163 Instance - The FV instance whose base address is going to be
165 Global - Pointer to ESAL_FWB_GLOBAL that contains all
167 FwhInstance - The EFI_FW_VOL_INSTANCE fimrware instance structure
168 Virtual - Whether CPU is in virtual or physical mode
171 EFI_SUCCESS - Successfully returns
172 EFI_INVALID_PARAMETER - Instance not found
176 EFI_FW_VOL_INSTANCE
*FwhRecord
;
178 if (Instance
>= Global
->NumFv
) {
179 return EFI_INVALID_PARAMETER
;
182 // Find the right instance of the FVB private data
184 FwhRecord
= Global
->FvInstance
[Virtual
];
185 while (Instance
> 0) {
186 FwhRecord
= (EFI_FW_VOL_INSTANCE
*)
188 (UINTN
) ((UINT8
*) FwhRecord
) + FwhRecord
->VolumeHeader
.HeaderLength
+
189 (sizeof (EFI_FW_VOL_INSTANCE
) - sizeof (EFI_FIRMWARE_VOLUME_HEADER
))
194 *FwhInstance
= FwhRecord
;
200 FvbGetPhysicalAddress (
202 OUT EFI_PHYSICAL_ADDRESS
*Address
,
203 IN ESAL_FWB_GLOBAL
*Global
,
209 Retrieves the physical address of a memory mapped FV
212 Instance - The FV instance whose base address is going to be
214 Address - Pointer to a caller allocated EFI_PHYSICAL_ADDRESS
215 that on successful return, contains the base address
216 of the firmware volume.
217 Global - Pointer to ESAL_FWB_GLOBAL that contains all
219 Virtual - Whether CPU is in virtual or physical mode
222 EFI_SUCCESS - Successfully returns
223 EFI_INVALID_PARAMETER - Instance not found
227 EFI_FW_VOL_INSTANCE
*FwhInstance
;
231 // Find the right instance of the FVB private data
233 Status
= GetFvbInstance (Instance
, Global
, &FwhInstance
, Virtual
);
234 ASSERT_EFI_ERROR (Status
);
235 *Address
= FwhInstance
->FvBase
[Virtual
];
241 FvbGetVolumeAttributes (
243 OUT EFI_FVB_ATTRIBUTES_2
*Attributes
,
244 IN ESAL_FWB_GLOBAL
*Global
,
250 Retrieves attributes, insures positive polarity of attribute bits, returns
251 resulting attributes in output parameter
254 Instance - The FV instance whose attributes is going to be
256 Attributes - Output buffer which contains attributes
257 Global - Pointer to ESAL_FWB_GLOBAL that contains all
259 Virtual - Whether CPU is in virtual or physical mode
262 EFI_SUCCESS - Successfully returns
263 EFI_INVALID_PARAMETER - Instance not found
267 EFI_FW_VOL_INSTANCE
*FwhInstance
;
271 // Find the right instance of the FVB private data
273 Status
= GetFvbInstance (Instance
, Global
, &FwhInstance
, Virtual
);
274 ASSERT_EFI_ERROR (Status
);
275 *Attributes
= FwhInstance
->VolumeHeader
.Attributes
;
284 OUT UINTN
*LbaAddress
,
285 OUT UINTN
*LbaLength
,
286 OUT UINTN
*NumOfBlocks
,
287 IN ESAL_FWB_GLOBAL
*Global
,
293 Retrieves the starting address of an LBA in an FV
296 Instance - The FV instance which the Lba belongs to
297 Lba - The logical block address
298 LbaAddress - On output, contains the physical starting address
300 LbaLength - On output, contains the length of the block
301 NumOfBlocks - A pointer to a caller allocated UINTN in which the
302 number of consecutive blocks starting with Lba is
303 returned. All blocks in this range have a size of
305 Global - Pointer to ESAL_FWB_GLOBAL that contains all
307 Virtual - Whether CPU is in virtual or physical mode
310 EFI_SUCCESS - Successfully returns
311 EFI_INVALID_PARAMETER - Instance not found
320 EFI_FW_VOL_INSTANCE
*FwhInstance
;
321 EFI_FV_BLOCK_MAP_ENTRY
*BlockMap
;
325 // Find the right instance of the FVB private data
327 Status
= GetFvbInstance (Instance
, Global
, &FwhInstance
, Virtual
);
328 ASSERT_EFI_ERROR (Status
);
332 BlockMap
= &(FwhInstance
->VolumeHeader
.BlockMap
[0]);
335 // Parse the blockmap of the FV to find which map entry the Lba belongs to
338 NumBlocks
= BlockMap
->NumBlocks
;
339 BlockLength
= BlockMap
->Length
;
341 if (NumBlocks
== 0 || BlockLength
== 0) {
342 return EFI_INVALID_PARAMETER
;
345 NextLba
= StartLba
+ NumBlocks
;
348 // The map entry found
350 if (Lba
>= StartLba
&& Lba
< NextLba
) {
351 Offset
= Offset
+ (UINTN
) MultU64x32 ((Lba
- StartLba
), BlockLength
);
352 if (LbaAddress
!= NULL
) {
353 *LbaAddress
= FwhInstance
->FvBase
[Virtual
] + Offset
;
356 if (LbaLength
!= NULL
) {
357 *LbaLength
= BlockLength
;
360 if (NumOfBlocks
!= NULL
) {
361 *NumOfBlocks
= (UINTN
) (NextLba
- Lba
);
368 Offset
= Offset
+ NumBlocks
* BlockLength
;
377 IN UINTN BlockOffset
,
378 IN OUT UINTN
*NumBytes
,
380 IN ESAL_FWB_GLOBAL
*Global
,
386 Reads specified number of bytes into a buffer from the specified block
389 Instance - The FV instance to be read from
390 Lba - The logical block address to be read from
391 BlockOffset - Offset into the block at which to begin reading
392 NumBytes - Pointer that on input contains the total size of
393 the buffer. On output, it contains the total number
395 Buffer - Pointer to a caller allocated buffer that will be
396 used to hold the data read
397 Global - Pointer to ESAL_FWB_GLOBAL that contains all
399 Virtual - Whether CPU is in virtual or physical mode
402 EFI_SUCCESS - The firmware volume was read successfully and
403 contents are in Buffer
404 EFI_BAD_BUFFER_SIZE - Read attempted across a LBA boundary. On output,
405 NumBytes contains the total number of bytes returned
407 EFI_ACCESS_DENIED - The firmware volume is in the ReadDisabled state
408 EFI_DEVICE_ERROR - The block device is not functioning correctly and
410 EFI_INVALID_PARAMETER - Instance not found, or NumBytes, Buffer are NULL
414 EFI_FVB_ATTRIBUTES_2 Attributes
;
420 // Check for invalid conditions
422 if ((NumBytes
== NULL
) || (Buffer
== NULL
)) {
423 return EFI_INVALID_PARAMETER
;
426 if (*NumBytes
== 0) {
427 return EFI_INVALID_PARAMETER
;
430 Status
= FvbGetLbaAddress (Instance
, Lba
, &LbaAddress
, &LbaLength
, NULL
, Global
, Virtual
);
431 if (EFI_ERROR (Status
)) {
435 // Check if the FV is read enabled
437 FvbGetVolumeAttributes (Instance
, &Attributes
, Global
, Virtual
);
439 if ((Attributes
& EFI_FVB2_READ_STATUS
) == 0) {
440 return EFI_ACCESS_DENIED
;
443 // Perform boundary checks and adjust NumBytes
445 if (BlockOffset
> LbaLength
) {
446 return EFI_INVALID_PARAMETER
;
449 if (LbaLength
< (*NumBytes
+ BlockOffset
)) {
450 *NumBytes
= (UINT32
) (LbaLength
- BlockOffset
);
451 Status
= EFI_BAD_BUFFER_SIZE
;
454 CopyMem (Buffer
, (UINT8
*) (LbaAddress
+ BlockOffset
), (UINTN
) (*NumBytes
));
463 IN UINTN BlockOffset
,
464 IN OUT UINTN
*NumBytes
,
466 IN ESAL_FWB_GLOBAL
*Global
,
472 Writes specified number of bytes from the input buffer to the block
475 Instance - The FV instance to be written to
476 Lba - The starting logical block index to write to
477 BlockOffset - Offset into the block at which to begin writing
478 NumBytes - Pointer that on input contains the total size of
479 the buffer. On output, it contains the total number
480 of bytes actually written
481 Buffer - Pointer to a caller allocated buffer that contains
482 the source for the write
483 Global - Pointer to ESAL_FWB_GLOBAL that contains all
485 Virtual - Whether CPU is in virtual or physical mode
488 EFI_SUCCESS - The firmware volume was written successfully
489 EFI_BAD_BUFFER_SIZE - Write attempted across a LBA boundary. On output,
490 NumBytes contains the total number of bytes
492 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
493 EFI_DEVICE_ERROR - The block device is not functioning correctly and
495 EFI_INVALID_PARAMETER - Instance not found, or NumBytes, Buffer are NULL
499 EFI_FVB_ATTRIBUTES_2 Attributes
;
505 // Check for invalid conditions
507 if ((NumBytes
== NULL
) || (Buffer
== NULL
)) {
508 return EFI_INVALID_PARAMETER
;
511 if (*NumBytes
== 0) {
512 return EFI_INVALID_PARAMETER
;
515 Status
= FvbGetLbaAddress (Instance
, Lba
, &LbaAddress
, &LbaLength
, NULL
, Global
, Virtual
);
516 if (EFI_ERROR (Status
)) {
520 // Check if the FV is write enabled
522 FvbGetVolumeAttributes (Instance
, &Attributes
, Global
, Virtual
);
524 if ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0) {
525 return EFI_ACCESS_DENIED
;
528 // Perform boundary checks and adjust NumBytes
530 if (BlockOffset
> LbaLength
) {
531 return EFI_INVALID_PARAMETER
;
534 if (LbaLength
< (*NumBytes
+ BlockOffset
)) {
535 *NumBytes
= (UINT32
) (LbaLength
- BlockOffset
);
536 Status
= EFI_BAD_BUFFER_SIZE
;
541 CopyMem ((UINT8
*) (LbaAddress
+ BlockOffset
), Buffer
, (UINTN
) (*NumBytes
));
550 IN ESAL_FWB_GLOBAL
*Global
,
556 Erases and initializes a firmware volume block
559 Instance - The FV instance to be erased
560 Lba - The logical block index to be erased
561 Global - Pointer to ESAL_FWB_GLOBAL that contains all
563 Virtual - Whether CPU is in virtual or physical mode
566 EFI_SUCCESS - The erase request was successfully completed
567 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
568 EFI_DEVICE_ERROR - The block device is not functioning correctly and
569 could not be written. Firmware device may have been
571 EFI_INVALID_PARAMETER - Instance not found
576 EFI_FVB_ATTRIBUTES_2 Attributes
;
583 // Check if the FV is write enabled
585 FvbGetVolumeAttributes (Instance
, &Attributes
, Global
, Virtual
);
587 if ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0) {
588 return EFI_ACCESS_DENIED
;
591 // Get the starting address of the block for erase.
593 Status
= FvbGetLbaAddress (Instance
, Lba
, &LbaAddress
, &LbaLength
, NULL
, Global
, Virtual
);
595 if (EFI_ERROR (Status
)) {
599 if ((Attributes
& EFI_FVB2_ERASE_POLARITY
) != 0) {
605 SetMem ((UINT8
*) LbaAddress
, LbaLength
, Data
);
611 FvbEraseCustomBlockRange (
614 IN UINTN OffsetStartLba
,
616 IN UINTN OffsetLastLba
,
617 IN ESAL_FWB_GLOBAL
*Global
,
623 Erases and initializes a specified range of a firmware volume
626 Instance - The FV instance to be erased
627 StartLba - The starting logical block index to be erased
628 OffsetStartLba - Offset into the starting block at which to
630 LastLba - The last logical block index to be erased
631 OffsetStartLba - Offset into the last block at which to end erasing
632 Global - Pointer to ESAL_FWB_GLOBAL that contains all
634 Virtual - Whether CPU is in virtual or physical mode
637 EFI_SUCCESS - The firmware volume was erased successfully
638 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
639 EFI_DEVICE_ERROR - The block device is not functioning correctly and
640 could not be written. Firmware device may have been
642 EFI_INVALID_PARAMETER - Instance not found
648 UINTN ScratchLbaSizeData
;
654 Status
= FvbGetLbaAddress (Instance
, StartLba
, NULL
, &LbaSize
, NULL
, Global
, Virtual
);
655 if (EFI_ERROR (Status
)) {
660 // Use the scratch space as the intermediate buffer to transfer data
661 // Back up the first LBA in scratch space.
663 FvbReadBlock (Instance
, StartLba
, 0, &LbaSize
, Global
->FvbScratchSpace
[Virtual
], Global
, Virtual
);
668 FvbEraseBlock (Instance
, StartLba
, Global
, Virtual
);
669 ScratchLbaSizeData
= OffsetStartLba
;
672 // write the data back to the first block
674 if (ScratchLbaSizeData
> 0) {
675 Status
= FvbWriteBlock (Instance
, StartLba
, 0, &ScratchLbaSizeData
, Global
->FvbScratchSpace
[Virtual
], Global
, Virtual
);
676 if (EFI_ERROR (Status
)) {
683 if (LastLba
> (StartLba
+ 1)) {
684 for (Index
= (StartLba
+ 1); Index
<= (LastLba
- 1); Index
++) {
685 FvbEraseBlock (Instance
, Index
, Global
, Virtual
);
689 // Last LBAs, the same as first LBAs
691 if (LastLba
> StartLba
) {
692 Status
= FvbGetLbaAddress (Instance
, LastLba
, NULL
, &LbaSize
, NULL
, Global
, Virtual
);
693 if (EFI_ERROR (Status
)) {
696 FvbReadBlock (Instance
, LastLba
, 0, &LbaSize
, Global
->FvbScratchSpace
[Virtual
], Global
, Virtual
);
697 FvbEraseBlock (Instance
, LastLba
, Global
, Virtual
);
700 ScratchLbaSizeData
= LbaSize
- (OffsetLastLba
+ 1);
702 if (ScratchLbaSizeData
> 0) {
703 Status
= FvbWriteBlock (
708 Global
->FvbScratchSpace
[Virtual
] + OffsetLastLba
+ 1,
718 FvbSetVolumeAttributes (
720 IN OUT EFI_FVB_ATTRIBUTES_2
*Attributes
,
721 IN ESAL_FWB_GLOBAL
*Global
,
727 Modifies the current settings of the firmware volume according to the
728 input parameter, and returns the new setting of the volume
731 Instance - The FV instance whose attributes is going to be
733 Attributes - On input, it is a pointer to EFI_FVB_ATTRIBUTES_2
734 containing the desired firmware volume settings.
735 On successful return, it contains the new settings
736 of the firmware volume
737 Global - Pointer to ESAL_FWB_GLOBAL that contains all
739 Virtual - Whether CPU is in virtual or physical mode
742 EFI_SUCCESS - Successfully returns
743 EFI_ACCESS_DENIED - The volume setting is locked and cannot be modified
744 EFI_INVALID_PARAMETER - Instance not found, or The attributes requested are
745 in conflict with the capabilities as declared in the
746 firmware volume header
750 EFI_FW_VOL_INSTANCE
*FwhInstance
;
751 EFI_FVB_ATTRIBUTES_2 OldAttributes
;
752 EFI_FVB_ATTRIBUTES_2
*AttribPtr
;
757 EFI_FVB_ATTRIBUTES_2 UnchangedAttributes
;
760 // Find the right instance of the FVB private data
762 Status
= GetFvbInstance (Instance
, Global
, &FwhInstance
, Virtual
);
763 ASSERT_EFI_ERROR (Status
);
765 AttribPtr
= (EFI_FVB_ATTRIBUTES_2
*) &(FwhInstance
->VolumeHeader
.Attributes
);
766 OldAttributes
= *AttribPtr
;
767 Capabilities
= OldAttributes
& (EFI_FVB2_READ_DISABLED_CAP
| \
768 EFI_FVB2_READ_ENABLED_CAP
| \
769 EFI_FVB2_WRITE_DISABLED_CAP
| \
770 EFI_FVB2_WRITE_ENABLED_CAP
| \
773 OldStatus
= OldAttributes
& EFI_FVB2_STATUS
;
774 NewStatus
= *Attributes
& EFI_FVB2_STATUS
;
776 UnchangedAttributes
= EFI_FVB2_READ_DISABLED_CAP
| \
777 EFI_FVB2_READ_ENABLED_CAP
| \
778 EFI_FVB2_WRITE_DISABLED_CAP
| \
779 EFI_FVB2_WRITE_ENABLED_CAP
| \
780 EFI_FVB2_LOCK_CAP
| \
781 EFI_FVB2_STICKY_WRITE
| \
782 EFI_FVB2_MEMORY_MAPPED
| \
783 EFI_FVB2_ERASE_POLARITY
| \
784 EFI_FVB2_READ_LOCK_CAP
| \
785 EFI_FVB2_WRITE_LOCK_CAP
| \
789 // Some attributes of FV is read only can *not* be set
791 if ((OldAttributes
& UnchangedAttributes
) ^ (*Attributes
& UnchangedAttributes
)) {
792 return EFI_INVALID_PARAMETER
;
795 // If firmware volume is locked, no status bit can be updated
797 if (OldAttributes
& EFI_FVB2_LOCK_STATUS
) {
798 if (OldStatus
^ NewStatus
) {
799 return EFI_ACCESS_DENIED
;
805 if ((Capabilities
& EFI_FVB2_READ_DISABLED_CAP
) == 0) {
806 if ((NewStatus
& EFI_FVB2_READ_STATUS
) == 0) {
807 return EFI_INVALID_PARAMETER
;
813 if ((Capabilities
& EFI_FVB2_READ_ENABLED_CAP
) == 0) {
814 if (NewStatus
& EFI_FVB2_READ_STATUS
) {
815 return EFI_INVALID_PARAMETER
;
819 // Test write disable
821 if ((Capabilities
& EFI_FVB2_WRITE_DISABLED_CAP
) == 0) {
822 if ((NewStatus
& EFI_FVB2_WRITE_STATUS
) == 0) {
823 return EFI_INVALID_PARAMETER
;
829 if ((Capabilities
& EFI_FVB2_WRITE_ENABLED_CAP
) == 0) {
830 if (NewStatus
& EFI_FVB2_WRITE_STATUS
) {
831 return EFI_INVALID_PARAMETER
;
837 if ((Capabilities
& EFI_FVB2_LOCK_CAP
) == 0) {
838 if (NewStatus
& EFI_FVB2_LOCK_STATUS
) {
839 return EFI_INVALID_PARAMETER
;
843 *AttribPtr
= (*AttribPtr
) & (0xFFFFFFFF & (~EFI_FVB2_STATUS
));
844 *AttribPtr
= (*AttribPtr
) | NewStatus
;
845 *Attributes
= *AttribPtr
;
854 FvbProtocolGetPhysicalAddress (
855 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
856 OUT EFI_PHYSICAL_ADDRESS
*Address
862 Retrieves the physical address of the device.
866 This - Calling context
867 Address - Output buffer containing the address.
872 EFI_SUCCESS - Successfully returns
876 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
878 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
880 return FvbGetPhysicalAddress (FvbDevice
->Instance
, Address
, mFvbModuleGlobal
, EfiGoneVirtual ());
885 FvbProtocolGetBlockSize (
886 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
887 IN CONST EFI_LBA Lba
,
888 OUT UINTN
*BlockSize
,
889 OUT UINTN
*NumOfBlocks
894 Retrieve the size of a logical block
897 This - Calling context
898 Lba - Indicates which block to return the size for.
899 BlockSize - A pointer to a caller allocated UINTN in which
900 the size of the block is returned
901 NumOfBlocks - a pointer to a caller allocated UINTN in which the
902 number of consecutive blocks starting with Lba is
903 returned. All blocks in this range have a size of
907 EFI_SUCCESS - The firmware volume was read successfully and
908 contents are in Buffer
912 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
914 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
916 return FvbGetLbaAddress (
929 FvbProtocolGetAttributes (
930 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
931 OUT EFI_FVB_ATTRIBUTES_2
*Attributes
936 Retrieves Volume attributes. No polarity translations are done.
939 This - Calling context
940 Attributes - output buffer which contains attributes
943 EFI_SUCCESS - Successfully returns
947 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
949 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
951 return FvbGetVolumeAttributes (FvbDevice
->Instance
, Attributes
, mFvbModuleGlobal
, EfiGoneVirtual ());
956 FvbProtocolSetAttributes (
957 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
958 IN OUT EFI_FVB_ATTRIBUTES_2
*Attributes
963 Sets Volume attributes. No polarity translations are done.
966 This - Calling context
967 Attributes - output buffer which contains attributes
970 EFI_SUCCESS - Successfully returns
974 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
976 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
978 return FvbSetVolumeAttributes (FvbDevice
->Instance
, Attributes
, mFvbModuleGlobal
, EfiGoneVirtual ());
983 FvbProtocolEraseBlocks (
984 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
991 The EraseBlock() function erases one or more blocks as denoted by the
992 variable argument list. The entire parameter list of blocks must be verified
993 prior to erasing any blocks. If a block is requested that does not exist
994 within the associated firmware volume (it has a larger index than the last
995 block of the firmware volume), the EraseBlock() function must return
996 EFI_INVALID_PARAMETER without modifying the contents of the firmware volume.
999 This - Calling context
1000 ... - Starting LBA followed by Number of Lba to erase.
1001 a -1 to terminate the list.
1004 EFI_SUCCESS - The erase request was successfully completed
1005 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
1006 EFI_DEVICE_ERROR - The block device is not functioning correctly and
1007 could not be written. Firmware device may have been
1012 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
1013 EFI_FW_VOL_INSTANCE
*FwhInstance
;
1016 EFI_LBA StartingLba
;
1020 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
1022 Status
= GetFvbInstance (FvbDevice
->Instance
, mFvbModuleGlobal
, &FwhInstance
, EfiGoneVirtual ());
1023 ASSERT_EFI_ERROR (Status
);
1025 NumOfBlocks
= FwhInstance
->NumOfBlocks
;
1027 VA_START (args
, This
);
1030 StartingLba
= VA_ARG (args
, EFI_LBA
);
1031 if (StartingLba
== EFI_LBA_LIST_TERMINATOR
) {
1035 NumOfLba
= VA_ARG (args
, UINT32
);
1038 // Check input parameters
1040 if (NumOfLba
== 0) {
1042 return EFI_INVALID_PARAMETER
;
1045 if ((StartingLba
+ NumOfLba
) > NumOfBlocks
) {
1046 return EFI_INVALID_PARAMETER
;
1052 VA_START (args
, This
);
1054 StartingLba
= VA_ARG (args
, EFI_LBA
);
1055 if (StartingLba
== EFI_LBA_LIST_TERMINATOR
) {
1059 NumOfLba
= VA_ARG (args
, UINT32
);
1061 while (NumOfLba
> 0) {
1062 Status
= FvbEraseBlock (FvbDevice
->Instance
, StartingLba
, mFvbModuleGlobal
, EfiGoneVirtual ());
1063 if (EFI_ERROR (Status
)) {
1082 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
1083 IN CONST EFI_LBA Lba
,
1084 IN CONST UINTN Offset
,
1085 IN OUT UINTN
*NumBytes
,
1086 IN CONST UINT8
*Buffer
1090 Routine Description:
1092 Writes data beginning at Lba:Offset from FV. The write terminates either
1093 when *NumBytes of data have been written, or when a block boundary is
1094 reached. *NumBytes is updated to reflect the actual number of bytes
1095 written. The write opertion does not include erase. This routine will
1096 attempt to write only the specified bytes. If the writes do not stick,
1097 it will return an error.
1100 This - Calling context
1101 Lba - Block in which to begin write
1102 Offset - Offset in the block at which to begin write
1103 NumBytes - On input, indicates the requested write size. On
1104 output, indicates the actual number of bytes written
1105 Buffer - Buffer containing source data for the write.
1108 EFI_SUCCESS - The firmware volume was written successfully
1109 EFI_BAD_BUFFER_SIZE - Write attempted across a LBA boundary. On output,
1110 NumBytes contains the total number of bytes
1112 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
1113 EFI_DEVICE_ERROR - The block device is not functioning correctly and
1114 could not be written
1115 EFI_INVALID_PARAMETER - NumBytes or Buffer are NULL
1120 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
1122 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
1124 return FvbWriteBlock (FvbDevice
->Instance
, (EFI_LBA
)Lba
, (UINTN
)Offset
, NumBytes
, (UINT8
*)Buffer
, mFvbModuleGlobal
, EfiGoneVirtual ());
1130 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
1131 IN CONST EFI_LBA Lba
,
1132 IN CONST UINTN Offset
,
1133 IN OUT UINTN
*NumBytes
,
1138 Routine Description:
1140 Reads data beginning at Lba:Offset from FV. The Read terminates either
1141 when *NumBytes of data have been read, or when a block boundary is
1142 reached. *NumBytes is updated to reflect the actual number of bytes
1143 written. The write opertion does not include erase. This routine will
1144 attempt to write only the specified bytes. If the writes do not stick,
1145 it will return an error.
1148 This - Calling context
1149 Lba - Block in which to begin Read
1150 Offset - Offset in the block at which to begin Read
1151 NumBytes - On input, indicates the requested write size. On
1152 output, indicates the actual number of bytes Read
1153 Buffer - Buffer containing source data for the Read.
1156 EFI_SUCCESS - The firmware volume was read successfully and
1157 contents are in Buffer
1158 EFI_BAD_BUFFER_SIZE - Read attempted across a LBA boundary. On output,
1159 NumBytes contains the total number of bytes returned
1161 EFI_ACCESS_DENIED - The firmware volume is in the ReadDisabled state
1162 EFI_DEVICE_ERROR - The block device is not functioning correctly and
1164 EFI_INVALID_PARAMETER - NumBytes or Buffer are NULL
1169 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
1171 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
1173 return FvbReadBlock (FvbDevice
->Instance
, Lba
, Offset
, NumBytes
, Buffer
, mFvbModuleGlobal
, EfiGoneVirtual ());
1176 // FVB Extension Protocols
1180 FvbExtendProtocolEraseCustomBlockRange (
1181 IN EFI_FVB_EXTENSION_PROTOCOL
*This
,
1182 IN EFI_LBA StartLba
,
1183 IN UINTN OffsetStartLba
,
1185 IN UINTN OffsetLastLba
1189 Routine Description:
1190 Erases and initializes a specified range of a firmware volume
1193 This - Calling context
1194 StartLba - The starting logical block index to be erased
1195 OffsetStartLba - Offset into the starting block at which to
1197 LastLba - The last logical block index to be erased
1198 OffsetStartLba - Offset into the last block at which to end erasing
1201 EFI_SUCCESS - The firmware volume was erased successfully
1202 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
1203 EFI_DEVICE_ERROR - The block device is not functioning correctly and
1204 could not be written. Firmware device may have been
1209 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
1211 FvbDevice
= FVB_EXTEND_DEVICE_FROM_THIS (This
);
1213 return FvbEraseCustomBlockRange (
1214 FvbDevice
->Instance
,
1227 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
1231 Routine Description:
1232 Check the integrity of firmware volume header
1235 FwVolHeader - A pointer to a firmware volume header
1238 EFI_SUCCESS - The firmware volume is consistent
1239 EFI_NOT_FOUND - The firmware volume has corrupted. So it is not an FV
1244 UINT16 HeaderLength
;
1248 // Verify the header revision, header signature, length
1249 // Length of FvBlock cannot be 2**64-1
1250 // HeaderLength cannot be an odd number
1252 if ((FwVolHeader
->Revision
!= EFI_FVH_REVISION
) ||
1253 (FwVolHeader
->Signature
!= EFI_FVH_SIGNATURE
) ||
1254 (FwVolHeader
->FvLength
== ((UINTN
) -1)) ||
1255 ((FwVolHeader
->HeaderLength
& 0x01) != 0)
1257 return EFI_NOT_FOUND
;
1260 // Verify the header checksum
1262 HeaderLength
= (UINT16
) (FwVolHeader
->HeaderLength
/ 2);
1263 Ptr
= (UINT16
*) FwVolHeader
;
1265 while (HeaderLength
> 0) {
1266 Checksum
= Checksum
+ (*Ptr
);
1271 if (Checksum
!= 0) {
1272 return EFI_NOT_FOUND
;
1281 IN EFI_HANDLE ImageHandle
,
1282 IN EFI_SYSTEM_TABLE
*SystemTable
1286 Routine Description:
1287 This function does common initialization for FVB services
1296 EFI_FW_VOL_INSTANCE
*FwhInstance
;
1297 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
1298 EFI_DXE_SERVICES
*DxeServices
;
1299 EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor
;
1301 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
1302 EFI_HANDLE FwbHandle
;
1303 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
1304 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*OldFwbInterface
;
1305 EFI_DEVICE_PATH_PROTOCOL
*TempFwbDevicePath
;
1306 FV_DEVICE_PATH TempFvbDevicePathData
;
1308 EFI_PHYSICAL_ADDRESS BaseAddress
;
1311 EFI_PEI_HOB_POINTERS FvHob
;
1314 // Get the DXE services table
1319 // Allocate runtime services data for global variable, which contains
1320 // the private data of all firmware volume block instances
1322 mFvbModuleGlobal
= AllocateRuntimePool (sizeof (ESAL_FWB_GLOBAL
));
1323 ASSERT (mFvbModuleGlobal
!= NULL
);
1326 // Calculate the total size for all firmware volume block instances
1330 FvHob
.Raw
= GetHobList ();
1331 while ((FvHob
.Raw
= GetNextHob (EFI_HOB_TYPE_FV
, FvHob
.Raw
)) != NULL
) {
1332 BaseAddress
= FvHob
.FirmwareVolume
->BaseAddress
;
1333 Length
= FvHob
.FirmwareVolume
->Length
;
1335 // Check if it is a "real" flash
1337 Status
= DxeServices
->GetMemorySpaceDescriptor (
1341 if (EFI_ERROR (Status
)) {
1345 if (Descriptor
.GcdMemoryType
!= EfiGcdMemoryTypeMemoryMappedIo
) {
1346 FvHob
.Raw
= GET_NEXT_HOB (FvHob
);
1350 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) BaseAddress
;
1351 Status
= ValidateFvHeader (FwVolHeader
);
1352 if (EFI_ERROR (Status
)) {
1356 Status
= GetFvbInfo (Length
, &FwVolHeader
);
1357 if (EFI_ERROR (Status
)) {
1358 FvHob
.Raw
= GET_NEXT_HOB (FvHob
);
1363 BufferSize
+= (sizeof (EFI_FW_VOL_INSTANCE
) + FwVolHeader
->HeaderLength
- sizeof (EFI_FIRMWARE_VOLUME_HEADER
));
1364 FvHob
.Raw
= GET_NEXT_HOB (FvHob
);
1368 // Only need to allocate once. There is only one copy of physical memory for
1369 // the private data of each FV instance. But in virtual mode or in physical
1370 // mode, the address of the the physical memory may be different.
1372 mFvbModuleGlobal
->FvInstance
[FVB_PHYSICAL
] = AllocateRuntimePool (BufferSize
);
1373 ASSERT (mFvbModuleGlobal
->FvInstance
[FVB_PHYSICAL
] != NULL
);
1376 // Make a virtual copy of the FvInstance pointer.
1378 FwhInstance
= mFvbModuleGlobal
->FvInstance
[FVB_PHYSICAL
];
1379 mFvbModuleGlobal
->FvInstance
[FVB_VIRTUAL
] = FwhInstance
;
1381 mFvbModuleGlobal
->NumFv
= 0;
1384 FvHob
.Raw
= GetHobList ();
1385 while (NULL
!= (FvHob
.Raw
= GetNextHob (EFI_HOB_TYPE_FV
, FvHob
.Raw
))) {
1386 BaseAddress
= FvHob
.FirmwareVolume
->BaseAddress
;
1387 Length
= FvHob
.FirmwareVolume
->Length
;
1389 // Check if it is a "real" flash
1391 Status
= DxeServices
->GetMemorySpaceDescriptor (
1395 if (EFI_ERROR (Status
)) {
1399 if (Descriptor
.GcdMemoryType
!= EfiGcdMemoryTypeMemoryMappedIo
) {
1400 FvHob
.Raw
= GET_NEXT_HOB (FvHob
);
1404 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) BaseAddress
;
1405 Status
= ValidateFvHeader (FwVolHeader
);
1406 if (EFI_ERROR (Status
)) {
1408 // Get FvbInfo to provide in FwhInstance.
1410 Status
= GetFvbInfo (Length
, &FwVolHeader
);
1411 if (EFI_ERROR (Status
)) {
1412 FvHob
.Raw
= GET_NEXT_HOB (FvHob
);
1416 // Write healthy FV header back.
1419 (VOID
*) (UINTN
) BaseAddress
,
1420 (VOID
*) FwVolHeader
,
1421 FwVolHeader
->HeaderLength
1425 FwhInstance
->FvBase
[FVB_PHYSICAL
] = (UINTN
) BaseAddress
;
1426 FwhInstance
->FvBase
[FVB_VIRTUAL
] = (UINTN
) BaseAddress
;
1428 CopyMem ((UINTN
*) &(FwhInstance
->VolumeHeader
), (UINTN
*) FwVolHeader
, FwVolHeader
->HeaderLength
);
1429 FwVolHeader
= &(FwhInstance
->VolumeHeader
);
1430 EfiInitializeLock (&(FwhInstance
->FvbDevLock
), TPL_HIGH_LEVEL
);
1434 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
1436 // Get the maximum size of a block. The size will be used to allocate
1437 // buffer for Scratch space, the intermediate buffer for FVB extension
1440 if (MaxLbaSize
< PtrBlockMapEntry
->Length
) {
1441 MaxLbaSize
= PtrBlockMapEntry
->Length
;
1444 NumOfBlocks
= NumOfBlocks
+ PtrBlockMapEntry
->NumBlocks
;
1447 // The total number of blocks in the FV.
1449 FwhInstance
->NumOfBlocks
= NumOfBlocks
;
1452 // Add a FVB Protocol Instance
1454 FvbDevice
= AllocateRuntimePool (sizeof (EFI_FW_VOL_BLOCK_DEVICE
));
1455 ASSERT (FvbDevice
!= NULL
);
1457 CopyMem (FvbDevice
, &mFvbDeviceTemplate
, sizeof (EFI_FW_VOL_BLOCK_DEVICE
));
1459 FvbDevice
->Instance
= mFvbModuleGlobal
->NumFv
;
1460 mFvbModuleGlobal
->NumFv
++;
1463 // Set up the devicepath
1465 FvbDevice
->DevicePath
.MemMapDevPath
.StartingAddress
= BaseAddress
;
1466 FvbDevice
->DevicePath
.MemMapDevPath
.EndingAddress
= BaseAddress
+ (FwVolHeader
->FvLength
- 1);
1469 // Find a handle with a matching device path that has supports FW Block protocol
1471 TempFwbDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) &TempFvbDevicePathData
;
1472 CopyMem (TempFwbDevicePath
, &FvbDevice
->DevicePath
, sizeof (FV_DEVICE_PATH
));
1473 Status
= gBS
->LocateDevicePath (&gEfiFirmwareVolumeBlockProtocolGuid
, &TempFwbDevicePath
, &FwbHandle
);
1474 if (EFI_ERROR (Status
)) {
1476 // LocateDevicePath fails so install a new interface and device path
1479 Status
= gBS
->InstallMultipleProtocolInterfaces (
1481 &gEfiFirmwareVolumeBlockProtocolGuid
,
1482 &FvbDevice
->FwVolBlockInstance
,
1483 &gEfiDevicePathProtocolGuid
,
1484 &FvbDevice
->DevicePath
,
1487 ASSERT_EFI_ERROR (Status
);
1488 } else if (EfiIsDevicePathEnd (TempFwbDevicePath
)) {
1490 // Device allready exists, so reinstall the FVB protocol
1492 Status
= gBS
->HandleProtocol (
1494 &gEfiFirmwareVolumeBlockProtocolGuid
,
1497 ASSERT_EFI_ERROR (Status
);
1499 Status
= gBS
->ReinstallProtocolInterface (
1501 &gEfiFirmwareVolumeBlockProtocolGuid
,
1503 &FvbDevice
->FwVolBlockInstance
1505 ASSERT_EFI_ERROR (Status
);
1509 // There was a FVB protocol on an End Device Path node
1514 // Install FVB Extension Protocol on the same handle
1516 Status
= gBS
->InstallMultipleProtocolInterfaces (
1518 &gEfiFvbExtensionProtocolGuid
,
1519 &FvbDevice
->FvbExtension
,
1520 &gEfiAlternateFvBlockGuid
,
1525 ASSERT_EFI_ERROR (Status
);
1527 FwhInstance
= (EFI_FW_VOL_INSTANCE
*)
1529 (UINTN
) ((UINT8
*) FwhInstance
) + FwVolHeader
->HeaderLength
+
1530 (sizeof (EFI_FW_VOL_INSTANCE
) - sizeof (EFI_FIRMWARE_VOLUME_HEADER
))
1533 FvHob
.Raw
= GET_NEXT_HOB (FvHob
);
1537 // Allocate for scratch space, an intermediate buffer for FVB extention
1539 mFvbModuleGlobal
->FvbScratchSpace
[FVB_PHYSICAL
] = AllocateRuntimePool (MaxLbaSize
);
1540 ASSERT (mFvbModuleGlobal
->FvbScratchSpace
[FVB_PHYSICAL
] != NULL
);
1542 mFvbModuleGlobal
->FvbScratchSpace
[FVB_VIRTUAL
] = mFvbModuleGlobal
->FvbScratchSpace
[FVB_PHYSICAL
];