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>
48 #include <Library/DevicePathLib.h>
50 #include "FWBlockService.h"
52 #define EFI_FVB2_STATUS (EFI_FVB2_READ_STATUS | EFI_FVB2_WRITE_STATUS | EFI_FVB2_LOCK_STATUS)
54 ESAL_FWB_GLOBAL
*mFvbModuleGlobal
;
56 EFI_FW_VOL_BLOCK_DEVICE mFvbDeviceTemplate
= {
64 sizeof (MEMMAP_DEVICE_PATH
),
74 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
76 sizeof (EFI_DEVICE_PATH_PROTOCOL
),
83 FvbProtocolGetAttributes
,
84 FvbProtocolSetAttributes
,
85 FvbProtocolGetPhysicalAddress
,
86 FvbProtocolGetBlockSize
,
89 FvbProtocolEraseBlocks
,
93 FvbExtendProtocolEraseCustomBlockRange
101 FvbVirtualddressChangeEvent (
109 Fixup internal data so that EFI and SAL can be call in virtual mode.
110 Call the passed in Child Notify event and convert the mFvbModuleGlobal
111 date items to there virtual address.
113 mFvbModuleGlobal->FvInstance[FVB_PHYSICAL] - Physical copy of instance data
114 mFvbModuleGlobal->FvInstance[FVB_VIRTUAL] - Virtual pointer to common
119 (Standard EFI notify event - EFI_EVENT_NOTIFY)
127 EFI_FW_VOL_INSTANCE
*FwhInstance
;
130 EfiConvertPointer (0x0, (VOID
**) &mFvbModuleGlobal
->FvInstance
[FVB_VIRTUAL
]);
133 // Convert the base address of all the instances
136 FwhInstance
= mFvbModuleGlobal
->FvInstance
[FVB_PHYSICAL
];
137 while (Index
< mFvbModuleGlobal
->NumFv
) {
138 EfiConvertPointer (0x0, (VOID
**) &FwhInstance
->FvBase
[FVB_VIRTUAL
]);
139 FwhInstance
= (EFI_FW_VOL_INSTANCE
*)
141 (UINTN
) ((UINT8
*) FwhInstance
) + FwhInstance
->VolumeHeader
.HeaderLength
+
142 (sizeof (EFI_FW_VOL_INSTANCE
) - sizeof (EFI_FIRMWARE_VOLUME_HEADER
))
147 EfiConvertPointer (0x0, (VOID
**) &mFvbModuleGlobal
->FvbScratchSpace
[FVB_VIRTUAL
]);
148 EfiConvertPointer (0x0, (VOID
**) &mFvbModuleGlobal
);
154 IN ESAL_FWB_GLOBAL
*Global
,
155 OUT EFI_FW_VOL_INSTANCE
**FwhInstance
,
161 Retrieves the physical address of a memory mapped FV
164 Instance - The FV instance whose base address is going to be
166 Global - Pointer to ESAL_FWB_GLOBAL that contains all
168 FwhInstance - The EFI_FW_VOL_INSTANCE fimrware instance structure
169 Virtual - Whether CPU is in virtual or physical mode
172 EFI_SUCCESS - Successfully returns
173 EFI_INVALID_PARAMETER - Instance not found
177 EFI_FW_VOL_INSTANCE
*FwhRecord
;
179 if (Instance
>= Global
->NumFv
) {
180 return EFI_INVALID_PARAMETER
;
183 // Find the right instance of the FVB private data
185 FwhRecord
= Global
->FvInstance
[Virtual
];
186 while (Instance
> 0) {
187 FwhRecord
= (EFI_FW_VOL_INSTANCE
*)
189 (UINTN
) ((UINT8
*) FwhRecord
) + FwhRecord
->VolumeHeader
.HeaderLength
+
190 (sizeof (EFI_FW_VOL_INSTANCE
) - sizeof (EFI_FIRMWARE_VOLUME_HEADER
))
195 *FwhInstance
= FwhRecord
;
201 FvbGetPhysicalAddress (
203 OUT EFI_PHYSICAL_ADDRESS
*Address
,
204 IN ESAL_FWB_GLOBAL
*Global
,
210 Retrieves the physical address of a memory mapped FV
213 Instance - The FV instance whose base address is going to be
215 Address - Pointer to a caller allocated EFI_PHYSICAL_ADDRESS
216 that on successful return, contains the base address
217 of the firmware volume.
218 Global - Pointer to ESAL_FWB_GLOBAL that contains all
220 Virtual - Whether CPU is in virtual or physical mode
223 EFI_SUCCESS - Successfully returns
224 EFI_INVALID_PARAMETER - Instance not found
228 EFI_FW_VOL_INSTANCE
*FwhInstance
;
232 // Find the right instance of the FVB private data
234 Status
= GetFvbInstance (Instance
, Global
, &FwhInstance
, Virtual
);
235 ASSERT_EFI_ERROR (Status
);
236 *Address
= FwhInstance
->FvBase
[Virtual
];
242 FvbGetVolumeAttributes (
244 OUT EFI_FVB_ATTRIBUTES_2
*Attributes
,
245 IN ESAL_FWB_GLOBAL
*Global
,
251 Retrieves attributes, insures positive polarity of attribute bits, returns
252 resulting attributes in output parameter
255 Instance - The FV instance whose attributes is going to be
257 Attributes - Output buffer which contains attributes
258 Global - Pointer to ESAL_FWB_GLOBAL that contains all
260 Virtual - Whether CPU is in virtual or physical mode
263 EFI_SUCCESS - Successfully returns
264 EFI_INVALID_PARAMETER - Instance not found
268 EFI_FW_VOL_INSTANCE
*FwhInstance
;
272 // Find the right instance of the FVB private data
274 Status
= GetFvbInstance (Instance
, Global
, &FwhInstance
, Virtual
);
275 ASSERT_EFI_ERROR (Status
);
276 *Attributes
= FwhInstance
->VolumeHeader
.Attributes
;
285 OUT UINTN
*LbaAddress
,
286 OUT UINTN
*LbaLength
,
287 OUT UINTN
*NumOfBlocks
,
288 IN ESAL_FWB_GLOBAL
*Global
,
294 Retrieves the starting address of an LBA in an FV
297 Instance - The FV instance which the Lba belongs to
298 Lba - The logical block address
299 LbaAddress - On output, contains the physical starting address
301 LbaLength - On output, contains the length of the block
302 NumOfBlocks - A pointer to a caller allocated UINTN in which the
303 number of consecutive blocks starting with Lba is
304 returned. All blocks in this range have a size of
306 Global - Pointer to ESAL_FWB_GLOBAL that contains all
308 Virtual - Whether CPU is in virtual or physical mode
311 EFI_SUCCESS - Successfully returns
312 EFI_INVALID_PARAMETER - Instance not found
321 EFI_FW_VOL_INSTANCE
*FwhInstance
;
322 EFI_FV_BLOCK_MAP_ENTRY
*BlockMap
;
326 // Find the right instance of the FVB private data
328 Status
= GetFvbInstance (Instance
, Global
, &FwhInstance
, Virtual
);
329 ASSERT_EFI_ERROR (Status
);
333 BlockMap
= &(FwhInstance
->VolumeHeader
.BlockMap
[0]);
336 // Parse the blockmap of the FV to find which map entry the Lba belongs to
339 NumBlocks
= BlockMap
->NumBlocks
;
340 BlockLength
= BlockMap
->Length
;
342 if (NumBlocks
== 0 || BlockLength
== 0) {
343 return EFI_INVALID_PARAMETER
;
346 NextLba
= StartLba
+ NumBlocks
;
349 // The map entry found
351 if (Lba
>= StartLba
&& Lba
< NextLba
) {
352 Offset
= Offset
+ (UINTN
) MultU64x32 ((Lba
- StartLba
), BlockLength
);
353 if (LbaAddress
!= NULL
) {
354 *LbaAddress
= FwhInstance
->FvBase
[Virtual
] + Offset
;
357 if (LbaLength
!= NULL
) {
358 *LbaLength
= BlockLength
;
361 if (NumOfBlocks
!= NULL
) {
362 *NumOfBlocks
= (UINTN
) (NextLba
- Lba
);
369 Offset
= Offset
+ NumBlocks
* BlockLength
;
378 IN UINTN BlockOffset
,
379 IN OUT UINTN
*NumBytes
,
381 IN ESAL_FWB_GLOBAL
*Global
,
387 Reads specified number of bytes into a buffer from the specified block
390 Instance - The FV instance to be read from
391 Lba - The logical block address to be read from
392 BlockOffset - Offset into the block at which to begin reading
393 NumBytes - Pointer that on input contains the total size of
394 the buffer. On output, it contains the total number
396 Buffer - Pointer to a caller allocated buffer that will be
397 used to hold the data read
398 Global - Pointer to ESAL_FWB_GLOBAL that contains all
400 Virtual - Whether CPU is in virtual or physical mode
403 EFI_SUCCESS - The firmware volume was read successfully and
404 contents are in Buffer
405 EFI_BAD_BUFFER_SIZE - Read attempted across a LBA boundary. On output,
406 NumBytes contains the total number of bytes returned
408 EFI_ACCESS_DENIED - The firmware volume is in the ReadDisabled state
409 EFI_DEVICE_ERROR - The block device is not functioning correctly and
411 EFI_INVALID_PARAMETER - Instance not found, or NumBytes, Buffer are NULL
415 EFI_FVB_ATTRIBUTES_2 Attributes
;
421 // Check for invalid conditions
423 if ((NumBytes
== NULL
) || (Buffer
== NULL
)) {
424 return EFI_INVALID_PARAMETER
;
427 if (*NumBytes
== 0) {
428 return EFI_INVALID_PARAMETER
;
431 Status
= FvbGetLbaAddress (Instance
, Lba
, &LbaAddress
, &LbaLength
, NULL
, Global
, Virtual
);
432 if (EFI_ERROR (Status
)) {
436 // Check if the FV is read enabled
438 FvbGetVolumeAttributes (Instance
, &Attributes
, Global
, Virtual
);
440 if ((Attributes
& EFI_FVB2_READ_STATUS
) == 0) {
441 return EFI_ACCESS_DENIED
;
444 // Perform boundary checks and adjust NumBytes
446 if (BlockOffset
> LbaLength
) {
447 return EFI_INVALID_PARAMETER
;
450 if (LbaLength
< (*NumBytes
+ BlockOffset
)) {
451 *NumBytes
= (UINT32
) (LbaLength
- BlockOffset
);
452 Status
= EFI_BAD_BUFFER_SIZE
;
455 CopyMem (Buffer
, (UINT8
*) (LbaAddress
+ BlockOffset
), (UINTN
) (*NumBytes
));
464 IN UINTN BlockOffset
,
465 IN OUT UINTN
*NumBytes
,
467 IN ESAL_FWB_GLOBAL
*Global
,
473 Writes specified number of bytes from the input buffer to the block
476 Instance - The FV instance to be written to
477 Lba - The starting logical block index to write to
478 BlockOffset - Offset into the block at which to begin writing
479 NumBytes - Pointer that on input contains the total size of
480 the buffer. On output, it contains the total number
481 of bytes actually written
482 Buffer - Pointer to a caller allocated buffer that contains
483 the source for the write
484 Global - Pointer to ESAL_FWB_GLOBAL that contains all
486 Virtual - Whether CPU is in virtual or physical mode
489 EFI_SUCCESS - The firmware volume was written successfully
490 EFI_BAD_BUFFER_SIZE - Write attempted across a LBA boundary. On output,
491 NumBytes contains the total number of bytes
493 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
494 EFI_DEVICE_ERROR - The block device is not functioning correctly and
496 EFI_INVALID_PARAMETER - Instance not found, or NumBytes, Buffer are NULL
500 EFI_FVB_ATTRIBUTES_2 Attributes
;
506 // Check for invalid conditions
508 if ((NumBytes
== NULL
) || (Buffer
== NULL
)) {
509 return EFI_INVALID_PARAMETER
;
512 if (*NumBytes
== 0) {
513 return EFI_INVALID_PARAMETER
;
516 Status
= FvbGetLbaAddress (Instance
, Lba
, &LbaAddress
, &LbaLength
, NULL
, Global
, Virtual
);
517 if (EFI_ERROR (Status
)) {
521 // Check if the FV is write enabled
523 FvbGetVolumeAttributes (Instance
, &Attributes
, Global
, Virtual
);
525 if ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0) {
526 return EFI_ACCESS_DENIED
;
529 // Perform boundary checks and adjust NumBytes
531 if (BlockOffset
> LbaLength
) {
532 return EFI_INVALID_PARAMETER
;
535 if (LbaLength
< (*NumBytes
+ BlockOffset
)) {
536 *NumBytes
= (UINT32
) (LbaLength
- BlockOffset
);
537 Status
= EFI_BAD_BUFFER_SIZE
;
542 CopyMem ((UINT8
*) (LbaAddress
+ BlockOffset
), Buffer
, (UINTN
) (*NumBytes
));
551 IN ESAL_FWB_GLOBAL
*Global
,
557 Erases and initializes a firmware volume block
560 Instance - The FV instance to be erased
561 Lba - The logical block index to be erased
562 Global - Pointer to ESAL_FWB_GLOBAL that contains all
564 Virtual - Whether CPU is in virtual or physical mode
567 EFI_SUCCESS - The erase request was successfully completed
568 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
569 EFI_DEVICE_ERROR - The block device is not functioning correctly and
570 could not be written. Firmware device may have been
572 EFI_INVALID_PARAMETER - Instance not found
577 EFI_FVB_ATTRIBUTES_2 Attributes
;
584 // Check if the FV is write enabled
586 FvbGetVolumeAttributes (Instance
, &Attributes
, Global
, Virtual
);
588 if ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0) {
589 return EFI_ACCESS_DENIED
;
592 // Get the starting address of the block for erase.
594 Status
= FvbGetLbaAddress (Instance
, Lba
, &LbaAddress
, &LbaLength
, NULL
, Global
, Virtual
);
596 if (EFI_ERROR (Status
)) {
600 if ((Attributes
& EFI_FVB2_ERASE_POLARITY
) != 0) {
606 SetMem ((UINT8
*) LbaAddress
, LbaLength
, Data
);
612 FvbEraseCustomBlockRange (
615 IN UINTN OffsetStartLba
,
617 IN UINTN OffsetLastLba
,
618 IN ESAL_FWB_GLOBAL
*Global
,
624 Erases and initializes a specified range of a firmware volume
627 Instance - The FV instance to be erased
628 StartLba - The starting logical block index to be erased
629 OffsetStartLba - Offset into the starting block at which to
631 LastLba - The last logical block index to be erased
632 OffsetStartLba - Offset into the last block at which to end erasing
633 Global - Pointer to ESAL_FWB_GLOBAL that contains all
635 Virtual - Whether CPU is in virtual or physical mode
638 EFI_SUCCESS - The firmware volume was erased successfully
639 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
640 EFI_DEVICE_ERROR - The block device is not functioning correctly and
641 could not be written. Firmware device may have been
643 EFI_INVALID_PARAMETER - Instance not found
649 UINTN ScratchLbaSizeData
;
655 Status
= FvbGetLbaAddress (Instance
, StartLba
, NULL
, &LbaSize
, NULL
, Global
, Virtual
);
656 if (EFI_ERROR (Status
)) {
661 // Use the scratch space as the intermediate buffer to transfer data
662 // Back up the first LBA in scratch space.
664 FvbReadBlock (Instance
, StartLba
, 0, &LbaSize
, Global
->FvbScratchSpace
[Virtual
], Global
, Virtual
);
669 FvbEraseBlock (Instance
, StartLba
, Global
, Virtual
);
670 ScratchLbaSizeData
= OffsetStartLba
;
673 // write the data back to the first block
675 if (ScratchLbaSizeData
> 0) {
676 Status
= FvbWriteBlock (Instance
, StartLba
, 0, &ScratchLbaSizeData
, Global
->FvbScratchSpace
[Virtual
], Global
, Virtual
);
677 if (EFI_ERROR (Status
)) {
684 if (LastLba
> (StartLba
+ 1)) {
685 for (Index
= (StartLba
+ 1); Index
<= (LastLba
- 1); Index
++) {
686 FvbEraseBlock (Instance
, Index
, Global
, Virtual
);
690 // Last LBAs, the same as first LBAs
692 if (LastLba
> StartLba
) {
693 Status
= FvbGetLbaAddress (Instance
, LastLba
, NULL
, &LbaSize
, NULL
, Global
, Virtual
);
694 if (EFI_ERROR (Status
)) {
697 FvbReadBlock (Instance
, LastLba
, 0, &LbaSize
, Global
->FvbScratchSpace
[Virtual
], Global
, Virtual
);
698 FvbEraseBlock (Instance
, LastLba
, Global
, Virtual
);
701 ScratchLbaSizeData
= LbaSize
- (OffsetLastLba
+ 1);
703 if (ScratchLbaSizeData
> 0) {
704 Status
= FvbWriteBlock (
709 Global
->FvbScratchSpace
[Virtual
] + OffsetLastLba
+ 1,
719 FvbSetVolumeAttributes (
721 IN OUT EFI_FVB_ATTRIBUTES_2
*Attributes
,
722 IN ESAL_FWB_GLOBAL
*Global
,
728 Modifies the current settings of the firmware volume according to the
729 input parameter, and returns the new setting of the volume
732 Instance - The FV instance whose attributes is going to be
734 Attributes - On input, it is a pointer to EFI_FVB_ATTRIBUTES_2
735 containing the desired firmware volume settings.
736 On successful return, it contains the new settings
737 of the firmware volume
738 Global - Pointer to ESAL_FWB_GLOBAL that contains all
740 Virtual - Whether CPU is in virtual or physical mode
743 EFI_SUCCESS - Successfully returns
744 EFI_ACCESS_DENIED - The volume setting is locked and cannot be modified
745 EFI_INVALID_PARAMETER - Instance not found, or The attributes requested are
746 in conflict with the capabilities as declared in the
747 firmware volume header
751 EFI_FW_VOL_INSTANCE
*FwhInstance
;
752 EFI_FVB_ATTRIBUTES_2 OldAttributes
;
753 EFI_FVB_ATTRIBUTES_2
*AttribPtr
;
758 EFI_FVB_ATTRIBUTES_2 UnchangedAttributes
;
761 // Find the right instance of the FVB private data
763 Status
= GetFvbInstance (Instance
, Global
, &FwhInstance
, Virtual
);
764 ASSERT_EFI_ERROR (Status
);
766 AttribPtr
= (EFI_FVB_ATTRIBUTES_2
*) &(FwhInstance
->VolumeHeader
.Attributes
);
767 OldAttributes
= *AttribPtr
;
768 Capabilities
= OldAttributes
& (EFI_FVB2_READ_DISABLED_CAP
| \
769 EFI_FVB2_READ_ENABLED_CAP
| \
770 EFI_FVB2_WRITE_DISABLED_CAP
| \
771 EFI_FVB2_WRITE_ENABLED_CAP
| \
774 OldStatus
= OldAttributes
& EFI_FVB2_STATUS
;
775 NewStatus
= *Attributes
& EFI_FVB2_STATUS
;
777 UnchangedAttributes
= EFI_FVB2_READ_DISABLED_CAP
| \
778 EFI_FVB2_READ_ENABLED_CAP
| \
779 EFI_FVB2_WRITE_DISABLED_CAP
| \
780 EFI_FVB2_WRITE_ENABLED_CAP
| \
781 EFI_FVB2_LOCK_CAP
| \
782 EFI_FVB2_STICKY_WRITE
| \
783 EFI_FVB2_MEMORY_MAPPED
| \
784 EFI_FVB2_ERASE_POLARITY
| \
785 EFI_FVB2_READ_LOCK_CAP
| \
786 EFI_FVB2_WRITE_LOCK_CAP
| \
790 // Some attributes of FV is read only can *not* be set
792 if ((OldAttributes
& UnchangedAttributes
) ^ (*Attributes
& UnchangedAttributes
)) {
793 return EFI_INVALID_PARAMETER
;
796 // If firmware volume is locked, no status bit can be updated
798 if (OldAttributes
& EFI_FVB2_LOCK_STATUS
) {
799 if (OldStatus
^ NewStatus
) {
800 return EFI_ACCESS_DENIED
;
806 if ((Capabilities
& EFI_FVB2_READ_DISABLED_CAP
) == 0) {
807 if ((NewStatus
& EFI_FVB2_READ_STATUS
) == 0) {
808 return EFI_INVALID_PARAMETER
;
814 if ((Capabilities
& EFI_FVB2_READ_ENABLED_CAP
) == 0) {
815 if (NewStatus
& EFI_FVB2_READ_STATUS
) {
816 return EFI_INVALID_PARAMETER
;
820 // Test write disable
822 if ((Capabilities
& EFI_FVB2_WRITE_DISABLED_CAP
) == 0) {
823 if ((NewStatus
& EFI_FVB2_WRITE_STATUS
) == 0) {
824 return EFI_INVALID_PARAMETER
;
830 if ((Capabilities
& EFI_FVB2_WRITE_ENABLED_CAP
) == 0) {
831 if (NewStatus
& EFI_FVB2_WRITE_STATUS
) {
832 return EFI_INVALID_PARAMETER
;
838 if ((Capabilities
& EFI_FVB2_LOCK_CAP
) == 0) {
839 if (NewStatus
& EFI_FVB2_LOCK_STATUS
) {
840 return EFI_INVALID_PARAMETER
;
844 *AttribPtr
= (*AttribPtr
) & (0xFFFFFFFF & (~EFI_FVB2_STATUS
));
845 *AttribPtr
= (*AttribPtr
) | NewStatus
;
846 *Attributes
= *AttribPtr
;
855 FvbProtocolGetPhysicalAddress (
856 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
857 OUT EFI_PHYSICAL_ADDRESS
*Address
863 Retrieves the physical address of the device.
867 This - Calling context
868 Address - Output buffer containing the address.
873 EFI_SUCCESS - Successfully returns
877 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
879 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
881 return FvbGetPhysicalAddress (FvbDevice
->Instance
, Address
, mFvbModuleGlobal
, EfiGoneVirtual ());
886 FvbProtocolGetBlockSize (
887 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
888 IN CONST EFI_LBA Lba
,
889 OUT UINTN
*BlockSize
,
890 OUT UINTN
*NumOfBlocks
895 Retrieve the size of a logical block
898 This - Calling context
899 Lba - Indicates which block to return the size for.
900 BlockSize - A pointer to a caller allocated UINTN in which
901 the size of the block is returned
902 NumOfBlocks - a pointer to a caller allocated UINTN in which the
903 number of consecutive blocks starting with Lba is
904 returned. All blocks in this range have a size of
908 EFI_SUCCESS - The firmware volume was read successfully and
909 contents are in Buffer
913 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
915 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
917 return FvbGetLbaAddress (
930 FvbProtocolGetAttributes (
931 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
932 OUT EFI_FVB_ATTRIBUTES_2
*Attributes
937 Retrieves Volume attributes. No polarity translations are done.
940 This - Calling context
941 Attributes - output buffer which contains attributes
944 EFI_SUCCESS - Successfully returns
948 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
950 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
952 return FvbGetVolumeAttributes (FvbDevice
->Instance
, Attributes
, mFvbModuleGlobal
, EfiGoneVirtual ());
957 FvbProtocolSetAttributes (
958 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
959 IN OUT EFI_FVB_ATTRIBUTES_2
*Attributes
964 Sets Volume attributes. No polarity translations are done.
967 This - Calling context
968 Attributes - output buffer which contains attributes
971 EFI_SUCCESS - Successfully returns
975 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
977 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
979 return FvbSetVolumeAttributes (FvbDevice
->Instance
, Attributes
, mFvbModuleGlobal
, EfiGoneVirtual ());
984 FvbProtocolEraseBlocks (
985 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
992 The EraseBlock() function erases one or more blocks as denoted by the
993 variable argument list. The entire parameter list of blocks must be verified
994 prior to erasing any blocks. If a block is requested that does not exist
995 within the associated firmware volume (it has a larger index than the last
996 block of the firmware volume), the EraseBlock() function must return
997 EFI_INVALID_PARAMETER without modifying the contents of the firmware volume.
1000 This - Calling context
1001 ... - Starting LBA followed by Number of Lba to erase.
1002 a -1 to terminate the list.
1005 EFI_SUCCESS - The erase request was successfully completed
1006 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
1007 EFI_DEVICE_ERROR - The block device is not functioning correctly and
1008 could not be written. Firmware device may have been
1013 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
1014 EFI_FW_VOL_INSTANCE
*FwhInstance
;
1017 EFI_LBA StartingLba
;
1021 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
1023 Status
= GetFvbInstance (FvbDevice
->Instance
, mFvbModuleGlobal
, &FwhInstance
, EfiGoneVirtual ());
1024 ASSERT_EFI_ERROR (Status
);
1026 NumOfBlocks
= FwhInstance
->NumOfBlocks
;
1028 VA_START (args
, This
);
1031 StartingLba
= VA_ARG (args
, EFI_LBA
);
1032 if (StartingLba
== EFI_LBA_LIST_TERMINATOR
) {
1036 NumOfLba
= VA_ARG (args
, UINT32
);
1039 // Check input parameters
1041 if (NumOfLba
== 0) {
1043 return EFI_INVALID_PARAMETER
;
1046 if ((StartingLba
+ NumOfLba
) > NumOfBlocks
) {
1047 return EFI_INVALID_PARAMETER
;
1053 VA_START (args
, This
);
1055 StartingLba
= VA_ARG (args
, EFI_LBA
);
1056 if (StartingLba
== EFI_LBA_LIST_TERMINATOR
) {
1060 NumOfLba
= VA_ARG (args
, UINT32
);
1062 while (NumOfLba
> 0) {
1063 Status
= FvbEraseBlock (FvbDevice
->Instance
, StartingLba
, mFvbModuleGlobal
, EfiGoneVirtual ());
1064 if (EFI_ERROR (Status
)) {
1083 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
1086 IN OUT UINTN
*NumBytes
,
1091 Routine Description:
1093 Writes data beginning at Lba:Offset from FV. The write terminates either
1094 when *NumBytes of data have been written, or when a block boundary is
1095 reached. *NumBytes is updated to reflect the actual number of bytes
1096 written. The write opertion does not include erase. This routine will
1097 attempt to write only the specified bytes. If the writes do not stick,
1098 it will return an error.
1101 This - Calling context
1102 Lba - Block in which to begin write
1103 Offset - Offset in the block at which to begin write
1104 NumBytes - On input, indicates the requested write size. On
1105 output, indicates the actual number of bytes written
1106 Buffer - Buffer containing source data for the write.
1109 EFI_SUCCESS - The firmware volume was written successfully
1110 EFI_BAD_BUFFER_SIZE - Write attempted across a LBA boundary. On output,
1111 NumBytes contains the total number of bytes
1113 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
1114 EFI_DEVICE_ERROR - The block device is not functioning correctly and
1115 could not be written
1116 EFI_INVALID_PARAMETER - NumBytes or Buffer are NULL
1121 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
1123 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
1125 return FvbWriteBlock (FvbDevice
->Instance
, (EFI_LBA
)Lba
, (UINTN
)Offset
, NumBytes
, (UINT8
*)Buffer
, mFvbModuleGlobal
, EfiGoneVirtual ());
1131 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
1132 IN CONST EFI_LBA Lba
,
1133 IN CONST UINTN Offset
,
1134 IN OUT UINTN
*NumBytes
,
1139 Routine Description:
1141 Reads data beginning at Lba:Offset from FV. The Read terminates either
1142 when *NumBytes of data have been read, or when a block boundary is
1143 reached. *NumBytes is updated to reflect the actual number of bytes
1144 written. The write opertion does not include erase. This routine will
1145 attempt to write only the specified bytes. If the writes do not stick,
1146 it will return an error.
1149 This - Calling context
1150 Lba - Block in which to begin Read
1151 Offset - Offset in the block at which to begin Read
1152 NumBytes - On input, indicates the requested write size. On
1153 output, indicates the actual number of bytes Read
1154 Buffer - Buffer containing source data for the Read.
1157 EFI_SUCCESS - The firmware volume was read successfully and
1158 contents are in Buffer
1159 EFI_BAD_BUFFER_SIZE - Read attempted across a LBA boundary. On output,
1160 NumBytes contains the total number of bytes returned
1162 EFI_ACCESS_DENIED - The firmware volume is in the ReadDisabled state
1163 EFI_DEVICE_ERROR - The block device is not functioning correctly and
1165 EFI_INVALID_PARAMETER - NumBytes or Buffer are NULL
1170 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
1172 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
1174 return FvbReadBlock (FvbDevice
->Instance
, Lba
, Offset
, NumBytes
, Buffer
, mFvbModuleGlobal
, EfiGoneVirtual ());
1177 // FVB Extension Protocols
1181 FvbExtendProtocolEraseCustomBlockRange (
1182 IN EFI_FVB_EXTENSION_PROTOCOL
*This
,
1183 IN EFI_LBA StartLba
,
1184 IN UINTN OffsetStartLba
,
1186 IN UINTN OffsetLastLba
1190 Routine Description:
1191 Erases and initializes a specified range of a firmware volume
1194 This - Calling context
1195 StartLba - The starting logical block index to be erased
1196 OffsetStartLba - Offset into the starting block at which to
1198 LastLba - The last logical block index to be erased
1199 OffsetStartLba - Offset into the last block at which to end erasing
1202 EFI_SUCCESS - The firmware volume was erased successfully
1203 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
1204 EFI_DEVICE_ERROR - The block device is not functioning correctly and
1205 could not be written. Firmware device may have been
1210 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
1212 FvbDevice
= FVB_EXTEND_DEVICE_FROM_THIS (This
);
1214 return FvbEraseCustomBlockRange (
1215 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
= (UINT16
)(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 (IsDevicePathEnd (TempFwbDevicePath
)) {
1490 // Device allready exists, so reinstall the FVB protocol
1492 Status
= gBS
->HandleProtocol (
1494 &gEfiFirmwareVolumeBlockProtocolGuid
,
1495 (VOID
**)&OldFwbInterface
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
];