3 Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
4 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/FirmwareVolumeBlock.h>
32 #include <Protocol/DevicePath.h>
35 // The Library classes this module consumes
37 #include <Library/UefiLib.h>
38 #include <Library/UefiDriverEntryPoint.h>
39 #include <Library/BaseLib.h>
40 #include <Library/DxeServicesTableLib.h>
41 #include <Library/UefiRuntimeLib.h>
42 #include <Library/DebugLib.h>
43 #include <Library/BaseMemoryLib.h>
44 #include <Library/MemoryAllocationLib.h>
45 #include <Library/UefiBootServicesTableLib.h>
46 #include <Library/DevicePathLib.h>
48 #include "FwBlockService.h"
49 #include "QemuFlash.h"
51 #define EFI_FVB2_STATUS (EFI_FVB2_READ_STATUS | EFI_FVB2_WRITE_STATUS | EFI_FVB2_LOCK_STATUS)
53 ESAL_FWB_GLOBAL
*mFvbModuleGlobal
;
55 FV_MEMMAP_DEVICE_PATH mFvMemmapDevicePathTemplate
= {
61 (UINT8
)(sizeof (MEMMAP_DEVICE_PATH
)),
62 (UINT8
)(sizeof (MEMMAP_DEVICE_PATH
) >> 8)
66 (EFI_PHYSICAL_ADDRESS
) 0,
67 (EFI_PHYSICAL_ADDRESS
) 0,
71 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
73 END_DEVICE_PATH_LENGTH
,
79 FV_PIWG_DEVICE_PATH mFvPIWGDevicePathTemplate
= {
85 (UINT8
)(sizeof (MEDIA_FW_VOL_DEVICE_PATH
)),
86 (UINT8
)(sizeof (MEDIA_FW_VOL_DEVICE_PATH
) >> 8)
93 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
95 END_DEVICE_PATH_LENGTH
,
101 EFI_FW_VOL_BLOCK_DEVICE mFvbDeviceTemplate
= {
102 FVB_DEVICE_SIGNATURE
,
106 FvbProtocolGetAttributes
,
107 FvbProtocolSetAttributes
,
108 FvbProtocolGetPhysicalAddress
,
109 FvbProtocolGetBlockSize
,
112 FvbProtocolEraseBlocks
,
121 FvbVirtualddressChangeEvent (
129 Fixup internal data so that EFI and SAL can be call in virtual mode.
130 Call the passed in Child Notify event and convert the mFvbModuleGlobal
131 date items to there virtual address.
133 mFvbModuleGlobal->FvInstance[FVB_PHYSICAL] - Physical copy of instance data
134 mFvbModuleGlobal->FvInstance[FVB_VIRTUAL] - Virtual pointer to common
139 (Standard EFI notify event - EFI_EVENT_NOTIFY)
147 EFI_FW_VOL_INSTANCE
*FwhInstance
;
150 EfiConvertPointer (0x0, (VOID
**) &mFvbModuleGlobal
->FvInstance
[FVB_VIRTUAL
]);
153 // Convert the base address of all the instances
156 FwhInstance
= mFvbModuleGlobal
->FvInstance
[FVB_PHYSICAL
];
157 while (Index
< mFvbModuleGlobal
->NumFv
) {
158 EfiConvertPointer (0x0, (VOID
**) &FwhInstance
->FvBase
[FVB_VIRTUAL
]);
159 FwhInstance
= (EFI_FW_VOL_INSTANCE
*)
161 (UINTN
) ((UINT8
*) FwhInstance
) + FwhInstance
->VolumeHeader
.HeaderLength
+
162 (sizeof (EFI_FW_VOL_INSTANCE
) - sizeof (EFI_FIRMWARE_VOLUME_HEADER
))
167 EfiConvertPointer (0x0, (VOID
**) &mFvbModuleGlobal
->FvbScratchSpace
[FVB_VIRTUAL
]);
168 EfiConvertPointer (0x0, (VOID
**) &mFvbModuleGlobal
);
169 QemuFlashConvertPointers ();
175 IN ESAL_FWB_GLOBAL
*Global
,
176 OUT EFI_FW_VOL_INSTANCE
**FwhInstance
,
182 Retrieves the physical address of a memory mapped FV
185 Instance - The FV instance whose base address is going to be
187 Global - Pointer to ESAL_FWB_GLOBAL that contains all
189 FwhInstance - The EFI_FW_VOL_INSTANCE fimrware instance structure
190 Virtual - Whether CPU is in virtual or physical mode
193 EFI_SUCCESS - Successfully returns
194 EFI_INVALID_PARAMETER - Instance not found
198 EFI_FW_VOL_INSTANCE
*FwhRecord
;
200 if (Instance
>= Global
->NumFv
) {
201 return EFI_INVALID_PARAMETER
;
204 // Find the right instance of the FVB private data
206 FwhRecord
= Global
->FvInstance
[Virtual
];
207 while (Instance
> 0) {
208 FwhRecord
= (EFI_FW_VOL_INSTANCE
*)
210 (UINTN
) ((UINT8
*) FwhRecord
) + FwhRecord
->VolumeHeader
.HeaderLength
+
211 (sizeof (EFI_FW_VOL_INSTANCE
) - sizeof (EFI_FIRMWARE_VOLUME_HEADER
))
216 *FwhInstance
= FwhRecord
;
222 FvbGetPhysicalAddress (
224 OUT EFI_PHYSICAL_ADDRESS
*Address
,
225 IN ESAL_FWB_GLOBAL
*Global
,
231 Retrieves the physical address of a memory mapped FV
234 Instance - The FV instance whose base address is going to be
236 Address - Pointer to a caller allocated EFI_PHYSICAL_ADDRESS
237 that on successful return, contains the base address
238 of the firmware volume.
239 Global - Pointer to ESAL_FWB_GLOBAL that contains all
241 Virtual - Whether CPU is in virtual or physical mode
244 EFI_SUCCESS - Successfully returns
245 EFI_INVALID_PARAMETER - Instance not found
249 EFI_FW_VOL_INSTANCE
*FwhInstance
;
253 // Find the right instance of the FVB private data
255 Status
= GetFvbInstance (Instance
, Global
, &FwhInstance
, Virtual
);
256 ASSERT_EFI_ERROR (Status
);
257 *Address
= FwhInstance
->FvBase
[Virtual
];
263 FvbGetVolumeAttributes (
265 OUT EFI_FVB_ATTRIBUTES_2
*Attributes
,
266 IN ESAL_FWB_GLOBAL
*Global
,
272 Retrieves attributes, insures positive polarity of attribute bits, returns
273 resulting attributes in output parameter
276 Instance - The FV instance whose attributes is going to be
278 Attributes - Output buffer which contains attributes
279 Global - Pointer to ESAL_FWB_GLOBAL that contains all
281 Virtual - Whether CPU is in virtual or physical mode
284 EFI_SUCCESS - Successfully returns
285 EFI_INVALID_PARAMETER - Instance not found
289 EFI_FW_VOL_INSTANCE
*FwhInstance
;
293 // Find the right instance of the FVB private data
295 Status
= GetFvbInstance (Instance
, Global
, &FwhInstance
, Virtual
);
296 ASSERT_EFI_ERROR (Status
);
297 *Attributes
= FwhInstance
->VolumeHeader
.Attributes
;
306 OUT UINTN
*LbaAddress
,
307 OUT UINTN
*LbaLength
,
308 OUT UINTN
*NumOfBlocks
,
309 IN ESAL_FWB_GLOBAL
*Global
,
315 Retrieves the starting address of an LBA in an FV
318 Instance - The FV instance which the Lba belongs to
319 Lba - The logical block address
320 LbaAddress - On output, contains the physical starting address
322 LbaLength - On output, contains the length of the block
323 NumOfBlocks - A pointer to a caller allocated UINTN in which the
324 number of consecutive blocks starting with Lba is
325 returned. All blocks in this range have a size of
327 Global - Pointer to ESAL_FWB_GLOBAL that contains all
329 Virtual - Whether CPU is in virtual or physical mode
332 EFI_SUCCESS - Successfully returns
333 EFI_INVALID_PARAMETER - Instance not found
342 EFI_FW_VOL_INSTANCE
*FwhInstance
;
343 EFI_FV_BLOCK_MAP_ENTRY
*BlockMap
;
347 // Find the right instance of the FVB private data
349 Status
= GetFvbInstance (Instance
, Global
, &FwhInstance
, Virtual
);
350 ASSERT_EFI_ERROR (Status
);
354 BlockMap
= &(FwhInstance
->VolumeHeader
.BlockMap
[0]);
357 // Parse the blockmap of the FV to find which map entry the Lba belongs to
360 NumBlocks
= BlockMap
->NumBlocks
;
361 BlockLength
= BlockMap
->Length
;
363 if (NumBlocks
== 0 || BlockLength
== 0) {
364 return EFI_INVALID_PARAMETER
;
367 NextLba
= StartLba
+ NumBlocks
;
370 // The map entry found
372 if (Lba
>= StartLba
&& Lba
< NextLba
) {
373 Offset
= Offset
+ (UINTN
) MultU64x32 ((Lba
- StartLba
), BlockLength
);
374 if (LbaAddress
!= NULL
) {
375 *LbaAddress
= FwhInstance
->FvBase
[Virtual
] + Offset
;
378 if (LbaLength
!= NULL
) {
379 *LbaLength
= BlockLength
;
382 if (NumOfBlocks
!= NULL
) {
383 *NumOfBlocks
= (UINTN
) (NextLba
- Lba
);
390 Offset
= Offset
+ NumBlocks
* BlockLength
;
396 FvbSetVolumeAttributes (
398 IN OUT EFI_FVB_ATTRIBUTES_2
*Attributes
,
399 IN ESAL_FWB_GLOBAL
*Global
,
405 Modifies the current settings of the firmware volume according to the
406 input parameter, and returns the new setting of the volume
409 Instance - The FV instance whose attributes is going to be
411 Attributes - On input, it is a pointer to EFI_FVB_ATTRIBUTES_2
412 containing the desired firmware volume settings.
413 On successful return, it contains the new settings
414 of the firmware volume
415 Global - Pointer to ESAL_FWB_GLOBAL that contains all
417 Virtual - Whether CPU is in virtual or physical mode
420 EFI_SUCCESS - Successfully returns
421 EFI_ACCESS_DENIED - The volume setting is locked and cannot be modified
422 EFI_INVALID_PARAMETER - Instance not found, or The attributes requested are
423 in conflict with the capabilities as declared in the
424 firmware volume header
428 EFI_FW_VOL_INSTANCE
*FwhInstance
;
429 EFI_FVB_ATTRIBUTES_2 OldAttributes
;
430 EFI_FVB_ATTRIBUTES_2
*AttribPtr
;
435 EFI_FVB_ATTRIBUTES_2 UnchangedAttributes
;
438 // Find the right instance of the FVB private data
440 Status
= GetFvbInstance (Instance
, Global
, &FwhInstance
, Virtual
);
441 ASSERT_EFI_ERROR (Status
);
443 AttribPtr
= (EFI_FVB_ATTRIBUTES_2
*) &(FwhInstance
->VolumeHeader
.Attributes
);
444 OldAttributes
= *AttribPtr
;
445 Capabilities
= OldAttributes
& (EFI_FVB2_READ_DISABLED_CAP
| \
446 EFI_FVB2_READ_ENABLED_CAP
| \
447 EFI_FVB2_WRITE_DISABLED_CAP
| \
448 EFI_FVB2_WRITE_ENABLED_CAP
| \
451 OldStatus
= OldAttributes
& EFI_FVB2_STATUS
;
452 NewStatus
= *Attributes
& EFI_FVB2_STATUS
;
454 UnchangedAttributes
= EFI_FVB2_READ_DISABLED_CAP
| \
455 EFI_FVB2_READ_ENABLED_CAP
| \
456 EFI_FVB2_WRITE_DISABLED_CAP
| \
457 EFI_FVB2_WRITE_ENABLED_CAP
| \
458 EFI_FVB2_LOCK_CAP
| \
459 EFI_FVB2_STICKY_WRITE
| \
460 EFI_FVB2_MEMORY_MAPPED
| \
461 EFI_FVB2_ERASE_POLARITY
| \
462 EFI_FVB2_READ_LOCK_CAP
| \
463 EFI_FVB2_WRITE_LOCK_CAP
| \
467 // Some attributes of FV is read only can *not* be set
469 if ((OldAttributes
& UnchangedAttributes
) ^ (*Attributes
& UnchangedAttributes
)) {
470 return EFI_INVALID_PARAMETER
;
473 // If firmware volume is locked, no status bit can be updated
475 if (OldAttributes
& EFI_FVB2_LOCK_STATUS
) {
476 if (OldStatus
^ NewStatus
) {
477 return EFI_ACCESS_DENIED
;
483 if ((Capabilities
& EFI_FVB2_READ_DISABLED_CAP
) == 0) {
484 if ((NewStatus
& EFI_FVB2_READ_STATUS
) == 0) {
485 return EFI_INVALID_PARAMETER
;
491 if ((Capabilities
& EFI_FVB2_READ_ENABLED_CAP
) == 0) {
492 if (NewStatus
& EFI_FVB2_READ_STATUS
) {
493 return EFI_INVALID_PARAMETER
;
497 // Test write disable
499 if ((Capabilities
& EFI_FVB2_WRITE_DISABLED_CAP
) == 0) {
500 if ((NewStatus
& EFI_FVB2_WRITE_STATUS
) == 0) {
501 return EFI_INVALID_PARAMETER
;
507 if ((Capabilities
& EFI_FVB2_WRITE_ENABLED_CAP
) == 0) {
508 if (NewStatus
& EFI_FVB2_WRITE_STATUS
) {
509 return EFI_INVALID_PARAMETER
;
515 if ((Capabilities
& EFI_FVB2_LOCK_CAP
) == 0) {
516 if (NewStatus
& EFI_FVB2_LOCK_STATUS
) {
517 return EFI_INVALID_PARAMETER
;
521 *AttribPtr
= (*AttribPtr
) & (0xFFFFFFFF & (~EFI_FVB2_STATUS
));
522 *AttribPtr
= (*AttribPtr
) | NewStatus
;
523 *Attributes
= *AttribPtr
;
533 FvbProtocolGetPhysicalAddress (
534 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
535 OUT EFI_PHYSICAL_ADDRESS
*Address
541 Retrieves the physical address of the device.
545 This - Calling context
546 Address - Output buffer containing the address.
551 EFI_SUCCESS - Successfully returns
555 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
557 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
559 return FvbGetPhysicalAddress (FvbDevice
->Instance
, Address
, mFvbModuleGlobal
, EfiGoneVirtual ());
564 FvbProtocolGetBlockSize (
565 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
566 IN CONST EFI_LBA Lba
,
567 OUT UINTN
*BlockSize
,
568 OUT UINTN
*NumOfBlocks
573 Retrieve the size of a logical block
576 This - Calling context
577 Lba - Indicates which block to return the size for.
578 BlockSize - A pointer to a caller allocated UINTN in which
579 the size of the block is returned
580 NumOfBlocks - a pointer to a caller allocated UINTN in which the
581 number of consecutive blocks starting with Lba is
582 returned. All blocks in this range have a size of
586 EFI_SUCCESS - The firmware volume was read successfully and
587 contents are in Buffer
591 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
593 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
595 return FvbGetLbaAddress (
608 FvbProtocolGetAttributes (
609 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
610 OUT EFI_FVB_ATTRIBUTES_2
*Attributes
615 Retrieves Volume attributes. No polarity translations are done.
618 This - Calling context
619 Attributes - output buffer which contains attributes
622 EFI_SUCCESS - Successfully returns
626 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
628 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
630 return FvbGetVolumeAttributes (FvbDevice
->Instance
, Attributes
, mFvbModuleGlobal
, EfiGoneVirtual ());
635 FvbProtocolSetAttributes (
636 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
637 IN OUT EFI_FVB_ATTRIBUTES_2
*Attributes
642 Sets Volume attributes. No polarity translations are done.
645 This - Calling context
646 Attributes - output buffer which contains attributes
649 EFI_SUCCESS - Successfully returns
653 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
655 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
657 return FvbSetVolumeAttributes (FvbDevice
->Instance
, Attributes
, mFvbModuleGlobal
, EfiGoneVirtual ());
662 FvbProtocolEraseBlocks (
663 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
670 The EraseBlock() function erases one or more blocks as denoted by the
671 variable argument list. The entire parameter list of blocks must be verified
672 prior to erasing any blocks. If a block is requested that does not exist
673 within the associated firmware volume (it has a larger index than the last
674 block of the firmware volume), the EraseBlock() function must return
675 EFI_INVALID_PARAMETER without modifying the contents of the firmware volume.
678 This - Calling context
679 ... - Starting LBA followed by Number of Lba to erase.
680 a -1 to terminate the list.
683 EFI_SUCCESS - The erase request was successfully completed
684 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
685 EFI_DEVICE_ERROR - The block device is not functioning correctly and
686 could not be written. Firmware device may have been
691 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
692 EFI_FW_VOL_INSTANCE
*FwhInstance
;
699 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
701 Status
= GetFvbInstance (FvbDevice
->Instance
, mFvbModuleGlobal
, &FwhInstance
, EfiGoneVirtual ());
702 ASSERT_EFI_ERROR (Status
);
704 NumOfBlocks
= FwhInstance
->NumOfBlocks
;
706 VA_START (args
, This
);
709 StartingLba
= VA_ARG (args
, EFI_LBA
);
710 if (StartingLba
== EFI_LBA_LIST_TERMINATOR
) {
714 NumOfLba
= VA_ARG (args
, UINT32
);
717 // Check input parameters
719 if ((NumOfLba
== 0) || ((StartingLba
+ NumOfLba
) > NumOfBlocks
)) {
721 return EFI_INVALID_PARAMETER
;
727 VA_START (args
, This
);
729 StartingLba
= VA_ARG (args
, EFI_LBA
);
730 if (StartingLba
== EFI_LBA_LIST_TERMINATOR
) {
734 NumOfLba
= VA_ARG (args
, UINT32
);
736 while (NumOfLba
> 0) {
737 Status
= QemuFlashEraseBlock (StartingLba
);
738 if (EFI_ERROR (Status
)) {
757 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
760 IN OUT UINTN
*NumBytes
,
767 Writes data beginning at Lba:Offset from FV. The write terminates either
768 when *NumBytes of data have been written, or when a block boundary is
769 reached. *NumBytes is updated to reflect the actual number of bytes
770 written. The write opertion does not include erase. This routine will
771 attempt to write only the specified bytes. If the writes do not stick,
772 it will return an error.
775 This - Calling context
776 Lba - Block in which to begin write
777 Offset - Offset in the block at which to begin write
778 NumBytes - On input, indicates the requested write size. On
779 output, indicates the actual number of bytes written
780 Buffer - Buffer containing source data for the write.
783 EFI_SUCCESS - The firmware volume was written successfully
784 EFI_BAD_BUFFER_SIZE - Write attempted across a LBA boundary. On output,
785 NumBytes contains the total number of bytes
787 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
788 EFI_DEVICE_ERROR - The block device is not functioning correctly and
790 EFI_INVALID_PARAMETER - NumBytes or Buffer are NULL
794 return QemuFlashWrite ((EFI_LBA
)Lba
, (UINTN
)Offset
, NumBytes
, (UINT8
*)Buffer
);
800 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
801 IN CONST EFI_LBA Lba
,
802 IN CONST UINTN Offset
,
803 IN OUT UINTN
*NumBytes
,
810 Reads data beginning at Lba:Offset from FV. The Read terminates either
811 when *NumBytes of data have been read, or when a block boundary is
812 reached. *NumBytes is updated to reflect the actual number of bytes
813 written. The write opertion does not include erase. This routine will
814 attempt to write only the specified bytes. If the writes do not stick,
815 it will return an error.
818 This - Calling context
819 Lba - Block in which to begin Read
820 Offset - Offset in the block at which to begin Read
821 NumBytes - On input, indicates the requested write size. On
822 output, indicates the actual number of bytes Read
823 Buffer - Buffer containing source data for the Read.
826 EFI_SUCCESS - The firmware volume was read successfully and
827 contents are in Buffer
828 EFI_BAD_BUFFER_SIZE - Read attempted across a LBA boundary. On output,
829 NumBytes contains the total number of bytes returned
831 EFI_ACCESS_DENIED - The firmware volume is in the ReadDisabled state
832 EFI_DEVICE_ERROR - The block device is not functioning correctly and
834 EFI_INVALID_PARAMETER - NumBytes or Buffer are NULL
838 return QemuFlashRead ((EFI_LBA
)Lba
, (UINTN
)Offset
, NumBytes
, (UINT8
*)Buffer
);
843 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
848 Check the integrity of firmware volume header
851 FwVolHeader - A pointer to a firmware volume header
854 EFI_SUCCESS - The firmware volume is consistent
855 EFI_NOT_FOUND - The firmware volume has corrupted. So it is not an FV
862 // Verify the header revision, header signature, length
863 // Length of FvBlock cannot be 2**64-1
864 // HeaderLength cannot be an odd number
866 if ((FwVolHeader
->Revision
!= EFI_FVH_REVISION
) ||
867 (FwVolHeader
->Signature
!= EFI_FVH_SIGNATURE
) ||
868 (FwVolHeader
->FvLength
== ((UINTN
) -1)) ||
869 ((FwVolHeader
->HeaderLength
& 0x01) != 0)
871 return EFI_NOT_FOUND
;
875 // Verify the header checksum
878 Checksum
= CalculateSum16 ((UINT16
*) FwVolHeader
, FwVolHeader
->HeaderLength
);
882 Expected
= ((UINTN
) FwVolHeader
->Checksum
+ 0x10000 - Checksum
) & 0xffff;
884 DEBUG ((EFI_D_INFO
, "FV@%p Checksum is 0x%x, expected 0x%x\n",
885 FwVolHeader
, FwVolHeader
->Checksum
, Expected
));
886 return EFI_NOT_FOUND
;
894 MarkMemoryRangeForRuntimeAccess (
895 EFI_PHYSICAL_ADDRESS BaseAddress
,
902 // Mark flash region as runtime memory
904 Status
= gDS
->RemoveMemorySpace (
909 Status
= gDS
->AddMemorySpace (
910 EfiGcdMemoryTypeSystemMemory
,
913 EFI_MEMORY_UC
| EFI_MEMORY_RUNTIME
915 ASSERT_EFI_ERROR (Status
);
917 Status
= gBS
->AllocatePages (
919 EfiRuntimeServicesData
,
920 (UINTN
) EFI_SIZE_TO_PAGES (Length
),
923 ASSERT_EFI_ERROR (Status
);
930 InitializeVariableFvHeader (
935 EFI_FIRMWARE_VOLUME_HEADER
*GoodFwVolHeader
;
936 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
942 (EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
)
943 PcdGet32 (PcdOvmfFlashNvStorageVariableBase
);
946 (FixedPcdGet32 (PcdFlashNvStorageVariableSize
) +
947 FixedPcdGet32 (PcdFlashNvStorageFtwWorkingSize
) +
948 FixedPcdGet32 (PcdFlashNvStorageFtwSpareSize
) +
949 FixedPcdGet32 (PcdOvmfFlashNvStorageEventLogSize
));
951 BlockSize
= PcdGet32 (PcdOvmfFirmwareBlockSize
);
953 Status
= ValidateFvHeader (FwVolHeader
);
954 if (!EFI_ERROR (Status
)) {
955 if (FwVolHeader
->FvLength
!= Length
||
956 FwVolHeader
->BlockMap
[0].Length
!= BlockSize
) {
957 Status
= EFI_VOLUME_CORRUPTED
;
960 if (EFI_ERROR (Status
)) {
964 DEBUG ((EFI_D_INFO
, "Variable FV header is not valid. It will be reinitialized.\n"));
967 // Get FvbInfo to provide in FwhInstance.
969 Status
= GetFvbInfo (Length
, &GoodFwVolHeader
);
970 ASSERT (!EFI_ERROR (Status
));
972 Start
= (UINTN
)(UINT8
*) FwVolHeader
- PcdGet32 (PcdOvmfFdBaseAddress
);
973 ASSERT (Start
% BlockSize
== 0 && Length
% BlockSize
== 0);
974 ASSERT (GoodFwVolHeader
->HeaderLength
<= BlockSize
);
977 // Erase all the blocks
979 for (Offset
= Start
; Offset
< Start
+ Length
; Offset
+= BlockSize
) {
980 Status
= QemuFlashEraseBlock ((EFI_LBA
) Offset
/ BlockSize
);
981 ASSERT_EFI_ERROR (Status
);
985 // Write good FV header
987 WriteLength
= GoodFwVolHeader
->HeaderLength
;
988 Status
= QemuFlashWrite (
989 (EFI_LBA
) Start
/ BlockSize
,
992 (UINT8
*) GoodFwVolHeader
);
993 ASSERT_EFI_ERROR (Status
);
994 ASSERT (WriteLength
== GoodFwVolHeader
->HeaderLength
);
1003 IN EFI_HANDLE ImageHandle
,
1004 IN EFI_SYSTEM_TABLE
*SystemTable
1008 Routine Description:
1009 This function does common initialization for FVB services
1018 EFI_FW_VOL_INSTANCE
*FwhInstance
;
1019 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
1021 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
1022 EFI_HANDLE FwbHandle
;
1023 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
1024 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*OldFwbInterface
;
1026 EFI_PHYSICAL_ADDRESS BaseAddress
;
1029 EFI_EVENT VirtualAddressChangeEvent
;
1031 if (EFI_ERROR (QemuFlashInitialize ())) {
1033 // Return an error so image will be unloaded
1035 DEBUG ((EFI_D_INFO
, "QEMU flash was not detected. Writable FVB is not being installed.\n"));
1036 return EFI_WRITE_PROTECTED
;
1040 // Allocate runtime services data for global variable, which contains
1041 // the private data of all firmware volume block instances
1043 mFvbModuleGlobal
= AllocateRuntimePool (sizeof (ESAL_FWB_GLOBAL
));
1044 ASSERT (mFvbModuleGlobal
!= NULL
);
1046 BaseAddress
= (UINTN
) PcdGet32 (PcdOvmfFdBaseAddress
);
1047 Length
= PcdGet32 (PcdOvmfFirmwareFdSize
);
1049 Status
= InitializeVariableFvHeader ();
1050 if (EFI_ERROR (Status
)) {
1051 DEBUG ((EFI_D_INFO
, "QEMU Flash: Unable to initialize variable FV header\n"));
1052 return EFI_WRITE_PROTECTED
;
1055 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) BaseAddress
;
1056 Status
= ValidateFvHeader (FwVolHeader
);
1057 if (EFI_ERROR (Status
)) {
1061 Status
= GetFvbInfo (Length
, &FwVolHeader
);
1062 if (EFI_ERROR (Status
)) {
1063 DEBUG ((EFI_D_INFO
, "EFI_ERROR (GetFvbInfo (Length, &FwVolHeader))\n"));
1064 return EFI_WRITE_PROTECTED
;
1068 BufferSize
= (sizeof (EFI_FW_VOL_INSTANCE
) + FwVolHeader
->HeaderLength
- sizeof (EFI_FIRMWARE_VOLUME_HEADER
));
1071 // Only need to allocate once. There is only one copy of physical memory for
1072 // the private data of each FV instance. But in virtual mode or in physical
1073 // mode, the address of the the physical memory may be different.
1075 mFvbModuleGlobal
->FvInstance
[FVB_PHYSICAL
] = AllocateRuntimePool (BufferSize
);
1076 ASSERT (mFvbModuleGlobal
->FvInstance
[FVB_PHYSICAL
] != NULL
);
1079 // Make a virtual copy of the FvInstance pointer.
1081 FwhInstance
= mFvbModuleGlobal
->FvInstance
[FVB_PHYSICAL
];
1082 mFvbModuleGlobal
->FvInstance
[FVB_VIRTUAL
] = FwhInstance
;
1084 mFvbModuleGlobal
->NumFv
= 0;
1088 (EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
)
1089 PcdGet32 (PcdOvmfFlashNvStorageVariableBase
);
1091 FwhInstance
->FvBase
[FVB_PHYSICAL
] = (UINTN
) BaseAddress
;
1092 FwhInstance
->FvBase
[FVB_VIRTUAL
] = (UINTN
) BaseAddress
;
1094 CopyMem ((UINTN
*) &(FwhInstance
->VolumeHeader
), (UINTN
*) FwVolHeader
, FwVolHeader
->HeaderLength
);
1095 FwVolHeader
= &(FwhInstance
->VolumeHeader
);
1096 EfiInitializeLock (&(FwhInstance
->FvbDevLock
), TPL_HIGH_LEVEL
);
1100 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
1102 // Get the maximum size of a block.
1104 if (MaxLbaSize
< PtrBlockMapEntry
->Length
) {
1105 MaxLbaSize
= PtrBlockMapEntry
->Length
;
1108 NumOfBlocks
= NumOfBlocks
+ PtrBlockMapEntry
->NumBlocks
;
1112 // The total number of blocks in the FV.
1114 FwhInstance
->NumOfBlocks
= NumOfBlocks
;
1117 // Add a FVB Protocol Instance
1119 FvbDevice
= AllocateRuntimePool (sizeof (EFI_FW_VOL_BLOCK_DEVICE
));
1120 ASSERT (FvbDevice
!= NULL
);
1122 CopyMem (FvbDevice
, &mFvbDeviceTemplate
, sizeof (EFI_FW_VOL_BLOCK_DEVICE
));
1124 FvbDevice
->Instance
= mFvbModuleGlobal
->NumFv
;
1125 mFvbModuleGlobal
->NumFv
++;
1128 // Set up the devicepath
1130 if (FwVolHeader
->ExtHeaderOffset
== 0) {
1132 // FV does not contains extension header, then produce MEMMAP_DEVICE_PATH
1134 FvbDevice
->DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) AllocateCopyPool (sizeof (FV_MEMMAP_DEVICE_PATH
), &mFvMemmapDevicePathTemplate
);
1135 ((FV_MEMMAP_DEVICE_PATH
*) FvbDevice
->DevicePath
)->MemMapDevPath
.StartingAddress
= BaseAddress
;
1136 ((FV_MEMMAP_DEVICE_PATH
*) FvbDevice
->DevicePath
)->MemMapDevPath
.EndingAddress
= BaseAddress
+ FwVolHeader
->FvLength
- 1;
1138 FvbDevice
->DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) AllocateCopyPool (sizeof (FV_PIWG_DEVICE_PATH
), &mFvPIWGDevicePathTemplate
);
1140 &((FV_PIWG_DEVICE_PATH
*)FvbDevice
->DevicePath
)->FvDevPath
.FvName
,
1141 (GUID
*)(UINTN
)(BaseAddress
+ FwVolHeader
->ExtHeaderOffset
)
1146 // Find a handle with a matching device path that has supports FW Block protocol
1148 Status
= gBS
->LocateDevicePath (&gEfiFirmwareVolumeBlockProtocolGuid
, &FvbDevice
->DevicePath
, &FwbHandle
);
1149 if (EFI_ERROR (Status
)) {
1151 // LocateDevicePath fails so install a new interface and device path
1154 DEBUG ((EFI_D_INFO
, "Installing QEMU flash FVB\n"));
1155 Status
= gBS
->InstallMultipleProtocolInterfaces (
1157 &gEfiFirmwareVolumeBlockProtocolGuid
,
1158 &FvbDevice
->FwVolBlockInstance
,
1159 &gEfiDevicePathProtocolGuid
,
1160 FvbDevice
->DevicePath
,
1163 ASSERT_EFI_ERROR (Status
);
1164 } else if (IsDevicePathEnd (FvbDevice
->DevicePath
)) {
1166 // Device already exists, so reinstall the FVB protocol
1168 Status
= gBS
->HandleProtocol (
1170 &gEfiFirmwareVolumeBlockProtocolGuid
,
1171 (VOID
**)&OldFwbInterface
1173 ASSERT_EFI_ERROR (Status
);
1175 DEBUG ((EFI_D_INFO
, "Reinstalling FVB for QEMU flash region\n"));
1176 Status
= gBS
->ReinstallProtocolInterface (
1178 &gEfiFirmwareVolumeBlockProtocolGuid
,
1180 &FvbDevice
->FwVolBlockInstance
1182 ASSERT_EFI_ERROR (Status
);
1185 // There was a FVB protocol on an End Device Path node
1190 MarkMemoryRangeForRuntimeAccess (BaseAddress
, Length
);
1193 // Set several PCD values to point to flash
1196 PcdFlashNvStorageVariableBase64
,
1197 (UINTN
) PcdGet32 (PcdOvmfFlashNvStorageVariableBase
)
1200 PcdFlashNvStorageFtwWorkingBase
,
1201 PcdGet32 (PcdOvmfFlashNvStorageFtwWorkingBase
)
1204 PcdFlashNvStorageFtwSpareBase
,
1205 PcdGet32 (PcdOvmfFlashNvStorageFtwSpareBase
)
1208 FwhInstance
= (EFI_FW_VOL_INSTANCE
*)
1210 (UINTN
) ((UINT8
*) FwhInstance
) + FwVolHeader
->HeaderLength
+
1211 (sizeof (EFI_FW_VOL_INSTANCE
) - sizeof (EFI_FIRMWARE_VOLUME_HEADER
))
1214 VirtualAddressChangeEvent
= NULL
;
1215 Status
= gBS
->CreateEventEx (
1218 FvbVirtualddressChangeEvent
,
1220 &gEfiEventVirtualAddressChangeGuid
,
1221 &VirtualAddressChangeEvent
1223 ASSERT_EFI_ERROR (Status
);
1225 PcdSetBool (PcdOvmfFlashVariablesEnable
, TRUE
);