3 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available
6 under the terms and conditions of the BSD License which accompanies this
7 distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
24 // The package level header files this module uses
29 // The protocols, PPI and GUID defintions for this module
31 #include <Guid/EventGroup.h>
32 #include <Protocol/FirmwareVolumeBlock.h>
33 #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/BaseMemoryLib.h>
45 #include <Library/MemoryAllocationLib.h>
46 #include <Library/UefiBootServicesTableLib.h>
47 #include <Library/DevicePathLib.h>
49 #include "FwBlockService.h"
50 #include "QemuFlash.h"
52 #define EFI_FVB2_STATUS \
53 (EFI_FVB2_READ_STATUS | EFI_FVB2_WRITE_STATUS | EFI_FVB2_LOCK_STATUS)
55 ESAL_FWB_GLOBAL
*mFvbModuleGlobal
;
57 FV_MEMMAP_DEVICE_PATH mFvMemmapDevicePathTemplate
= {
63 (UINT8
)(sizeof (MEMMAP_DEVICE_PATH
)),
64 (UINT8
)(sizeof (MEMMAP_DEVICE_PATH
) >> 8)
68 (EFI_PHYSICAL_ADDRESS
) 0,
69 (EFI_PHYSICAL_ADDRESS
) 0,
73 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
75 END_DEVICE_PATH_LENGTH
,
81 FV_PIWG_DEVICE_PATH mFvPIWGDevicePathTemplate
= {
87 (UINT8
)(sizeof (MEDIA_FW_VOL_DEVICE_PATH
)),
88 (UINT8
)(sizeof (MEDIA_FW_VOL_DEVICE_PATH
) >> 8)
95 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
97 END_DEVICE_PATH_LENGTH
,
103 EFI_FW_VOL_BLOCK_DEVICE mFvbDeviceTemplate
= {
104 FVB_DEVICE_SIGNATURE
,
108 FvbProtocolGetAttributes
,
109 FvbProtocolSetAttributes
,
110 FvbProtocolGetPhysicalAddress
,
111 FvbProtocolGetBlockSize
,
114 FvbProtocolEraseBlocks
,
123 FvbVirtualddressChangeEvent (
131 Fixup internal data so that EFI and SAL can be call in virtual mode.
132 Call the passed in Child Notify event and convert the mFvbModuleGlobal
133 date items to there virtual address.
135 mFvbModuleGlobal->FvInstance[FVB_PHYSICAL] - Physical copy of instance
137 mFvbModuleGlobal->FvInstance[FVB_VIRTUAL] - Virtual pointer to common
142 (Standard EFI notify event - EFI_EVENT_NOTIFY)
150 EFI_FW_VOL_INSTANCE
*FwhInstance
;
153 EfiConvertPointer (0x0,
154 (VOID
**) &mFvbModuleGlobal
->FvInstance
[FVB_VIRTUAL
]);
157 // Convert the base address of all the instances
160 FwhInstance
= mFvbModuleGlobal
->FvInstance
[FVB_PHYSICAL
];
161 while (Index
< mFvbModuleGlobal
->NumFv
) {
162 EfiConvertPointer (0x0, (VOID
**) &FwhInstance
->FvBase
[FVB_VIRTUAL
]);
163 FwhInstance
= (EFI_FW_VOL_INSTANCE
*)
165 (UINTN
) ((UINT8
*) FwhInstance
) +
166 FwhInstance
->VolumeHeader
.HeaderLength
+
167 (sizeof (EFI_FW_VOL_INSTANCE
) - sizeof (EFI_FIRMWARE_VOLUME_HEADER
))
172 EfiConvertPointer (0x0, (VOID
**) &mFvbModuleGlobal
);
173 QemuFlashConvertPointers ();
179 IN ESAL_FWB_GLOBAL
*Global
,
180 OUT EFI_FW_VOL_INSTANCE
**FwhInstance
,
186 Retrieves the physical address of a memory mapped FV
189 Instance - The FV instance whose base address is going to be
191 Global - Pointer to ESAL_FWB_GLOBAL that contains all
193 FwhInstance - The EFI_FW_VOL_INSTANCE fimrware instance structure
194 Virtual - Whether CPU is in virtual or physical mode
197 EFI_SUCCESS - Successfully returns
198 EFI_INVALID_PARAMETER - Instance not found
202 EFI_FW_VOL_INSTANCE
*FwhRecord
;
205 if (Instance
>= Global
->NumFv
) {
206 return EFI_INVALID_PARAMETER
;
209 // Find the right instance of the FVB private data
211 FwhRecord
= Global
->FvInstance
[Virtual
];
212 while (Instance
> 0) {
213 FwhRecord
= (EFI_FW_VOL_INSTANCE
*)
215 (UINTN
) ((UINT8
*) FwhRecord
) + FwhRecord
->VolumeHeader
.HeaderLength
+
216 (sizeof (EFI_FW_VOL_INSTANCE
) - sizeof (EFI_FIRMWARE_VOLUME_HEADER
))
221 *FwhInstance
= FwhRecord
;
227 FvbGetPhysicalAddress (
229 OUT EFI_PHYSICAL_ADDRESS
*Address
,
230 IN ESAL_FWB_GLOBAL
*Global
,
236 Retrieves the physical address of a memory mapped FV
239 Instance - The FV instance whose base address is going to be
241 Address - Pointer to a caller allocated EFI_PHYSICAL_ADDRESS
242 that on successful return, contains the base
243 address of the firmware volume.
244 Global - Pointer to ESAL_FWB_GLOBAL that contains all
246 Virtual - Whether CPU is in virtual or physical mode
249 EFI_SUCCESS - Successfully returns
250 EFI_INVALID_PARAMETER - Instance not found
254 EFI_FW_VOL_INSTANCE
*FwhInstance
;
258 // Find the right instance of the FVB private data
260 Status
= GetFvbInstance (Instance
, Global
, &FwhInstance
, Virtual
);
261 ASSERT_EFI_ERROR (Status
);
262 *Address
= FwhInstance
->FvBase
[Virtual
];
268 FvbGetVolumeAttributes (
270 OUT EFI_FVB_ATTRIBUTES_2
*Attributes
,
271 IN ESAL_FWB_GLOBAL
*Global
,
277 Retrieves attributes, insures positive polarity of attribute bits, returns
278 resulting attributes in output parameter
281 Instance - The FV instance whose attributes is going to be
283 Attributes - Output buffer which contains attributes
284 Global - Pointer to ESAL_FWB_GLOBAL that contains all
286 Virtual - Whether CPU is in virtual or physical mode
289 EFI_SUCCESS - Successfully returns
290 EFI_INVALID_PARAMETER - Instance not found
294 EFI_FW_VOL_INSTANCE
*FwhInstance
;
298 // Find the right instance of the FVB private data
300 Status
= GetFvbInstance (Instance
, Global
, &FwhInstance
, Virtual
);
301 ASSERT_EFI_ERROR (Status
);
302 *Attributes
= FwhInstance
->VolumeHeader
.Attributes
;
311 OUT UINTN
*LbaAddress
,
312 OUT UINTN
*LbaLength
,
313 OUT UINTN
*NumOfBlocks
,
314 IN ESAL_FWB_GLOBAL
*Global
,
320 Retrieves the starting address of an LBA in an FV
323 Instance - The FV instance which the Lba belongs to
324 Lba - The logical block address
325 LbaAddress - On output, contains the physical starting address
327 LbaLength - On output, contains the length of the block
328 NumOfBlocks - A pointer to a caller allocated UINTN in which the
329 number of consecutive blocks starting with Lba is
330 returned. All blocks in this range have a size of
332 Global - Pointer to ESAL_FWB_GLOBAL that contains all
334 Virtual - Whether CPU is in virtual or physical mode
337 EFI_SUCCESS - Successfully returns
338 EFI_INVALID_PARAMETER - Instance not found
347 EFI_FW_VOL_INSTANCE
*FwhInstance
;
348 EFI_FV_BLOCK_MAP_ENTRY
*BlockMap
;
352 // Find the right instance of the FVB private data
354 Status
= GetFvbInstance (Instance
, Global
, &FwhInstance
, Virtual
);
355 ASSERT_EFI_ERROR (Status
);
359 BlockMap
= &(FwhInstance
->VolumeHeader
.BlockMap
[0]);
362 // Parse the blockmap of the FV to find which map entry the Lba belongs to
365 NumBlocks
= BlockMap
->NumBlocks
;
366 BlockLength
= BlockMap
->Length
;
368 if (NumBlocks
== 0 || BlockLength
== 0) {
369 return EFI_INVALID_PARAMETER
;
372 NextLba
= StartLba
+ NumBlocks
;
375 // The map entry found
377 if (Lba
>= StartLba
&& Lba
< NextLba
) {
378 Offset
= Offset
+ (UINTN
) MultU64x32 ((Lba
- StartLba
), BlockLength
);
379 if (LbaAddress
!= NULL
) {
380 *LbaAddress
= FwhInstance
->FvBase
[Virtual
] + Offset
;
383 if (LbaLength
!= NULL
) {
384 *LbaLength
= BlockLength
;
387 if (NumOfBlocks
!= NULL
) {
388 *NumOfBlocks
= (UINTN
) (NextLba
- Lba
);
395 Offset
= Offset
+ NumBlocks
* BlockLength
;
401 FvbSetVolumeAttributes (
403 IN OUT EFI_FVB_ATTRIBUTES_2
*Attributes
,
404 IN ESAL_FWB_GLOBAL
*Global
,
410 Modifies the current settings of the firmware volume according to the
411 input parameter, and returns the new setting of the volume
414 Instance - The FV instance whose attributes is going to be
416 Attributes - On input, it is a pointer to EFI_FVB_ATTRIBUTES_2
417 containing the desired firmware volume settings.
418 On successful return, it contains the new settings
419 of the firmware volume
420 Global - Pointer to ESAL_FWB_GLOBAL that contains all
422 Virtual - Whether CPU is in virtual or physical mode
425 EFI_SUCCESS - Successfully returns
426 EFI_ACCESS_DENIED - The volume setting is locked and cannot be modified
427 EFI_INVALID_PARAMETER - Instance not found, or The attributes requested are
428 in conflict with the capabilities as declared in
429 the firmware volume header
433 EFI_FW_VOL_INSTANCE
*FwhInstance
;
434 EFI_FVB_ATTRIBUTES_2 OldAttributes
;
435 EFI_FVB_ATTRIBUTES_2
*AttribPtr
;
440 EFI_FVB_ATTRIBUTES_2 UnchangedAttributes
;
443 // Find the right instance of the FVB private data
445 Status
= GetFvbInstance (Instance
, Global
, &FwhInstance
, Virtual
);
446 ASSERT_EFI_ERROR (Status
);
449 (EFI_FVB_ATTRIBUTES_2
*) &(FwhInstance
->VolumeHeader
.Attributes
);
450 OldAttributes
= *AttribPtr
;
451 Capabilities
= OldAttributes
& (EFI_FVB2_READ_DISABLED_CAP
| \
452 EFI_FVB2_READ_ENABLED_CAP
| \
453 EFI_FVB2_WRITE_DISABLED_CAP
| \
454 EFI_FVB2_WRITE_ENABLED_CAP
| \
457 OldStatus
= OldAttributes
& EFI_FVB2_STATUS
;
458 NewStatus
= *Attributes
& EFI_FVB2_STATUS
;
460 UnchangedAttributes
= EFI_FVB2_READ_DISABLED_CAP
| \
461 EFI_FVB2_READ_ENABLED_CAP
| \
462 EFI_FVB2_WRITE_DISABLED_CAP
| \
463 EFI_FVB2_WRITE_ENABLED_CAP
| \
464 EFI_FVB2_LOCK_CAP
| \
465 EFI_FVB2_STICKY_WRITE
| \
466 EFI_FVB2_MEMORY_MAPPED
| \
467 EFI_FVB2_ERASE_POLARITY
| \
468 EFI_FVB2_READ_LOCK_CAP
| \
469 EFI_FVB2_WRITE_LOCK_CAP
| \
473 // Some attributes of FV is read only can *not* be set
475 if ((OldAttributes
& UnchangedAttributes
) ^
476 (*Attributes
& UnchangedAttributes
)) {
477 return EFI_INVALID_PARAMETER
;
480 // If firmware volume is locked, no status bit can be updated
482 if (OldAttributes
& EFI_FVB2_LOCK_STATUS
) {
483 if (OldStatus
^ NewStatus
) {
484 return EFI_ACCESS_DENIED
;
490 if ((Capabilities
& EFI_FVB2_READ_DISABLED_CAP
) == 0) {
491 if ((NewStatus
& EFI_FVB2_READ_STATUS
) == 0) {
492 return EFI_INVALID_PARAMETER
;
498 if ((Capabilities
& EFI_FVB2_READ_ENABLED_CAP
) == 0) {
499 if (NewStatus
& EFI_FVB2_READ_STATUS
) {
500 return EFI_INVALID_PARAMETER
;
504 // Test write disable
506 if ((Capabilities
& EFI_FVB2_WRITE_DISABLED_CAP
) == 0) {
507 if ((NewStatus
& EFI_FVB2_WRITE_STATUS
) == 0) {
508 return EFI_INVALID_PARAMETER
;
514 if ((Capabilities
& EFI_FVB2_WRITE_ENABLED_CAP
) == 0) {
515 if (NewStatus
& EFI_FVB2_WRITE_STATUS
) {
516 return EFI_INVALID_PARAMETER
;
522 if ((Capabilities
& EFI_FVB2_LOCK_CAP
) == 0) {
523 if (NewStatus
& EFI_FVB2_LOCK_STATUS
) {
524 return EFI_INVALID_PARAMETER
;
528 *AttribPtr
= (*AttribPtr
) & (0xFFFFFFFF & (~EFI_FVB2_STATUS
));
529 *AttribPtr
= (*AttribPtr
) | NewStatus
;
530 *Attributes
= *AttribPtr
;
540 FvbProtocolGetPhysicalAddress (
541 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
542 OUT EFI_PHYSICAL_ADDRESS
*Address
548 Retrieves the physical address of the device.
552 This - Calling context
553 Address - Output buffer containing the address.
556 EFI_SUCCESS - Successfully returns
560 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
562 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
564 return FvbGetPhysicalAddress (FvbDevice
->Instance
, Address
,
565 mFvbModuleGlobal
, EfiGoneVirtual ());
570 FvbProtocolGetBlockSize (
571 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
572 IN CONST EFI_LBA Lba
,
573 OUT UINTN
*BlockSize
,
574 OUT UINTN
*NumOfBlocks
579 Retrieve the size of a logical block
582 This - Calling context
583 Lba - Indicates which block to return the size for.
584 BlockSize - A pointer to a caller allocated UINTN in which
585 the size of the block is returned
586 NumOfBlocks - a pointer to a caller allocated UINTN in which the
587 number of consecutive blocks starting with Lba is
588 returned. All blocks in this range have a size of
592 EFI_SUCCESS - The firmware volume was read successfully and
593 contents are in Buffer
597 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
599 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
601 return FvbGetLbaAddress (
614 FvbProtocolGetAttributes (
615 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
616 OUT EFI_FVB_ATTRIBUTES_2
*Attributes
621 Retrieves Volume attributes. No polarity translations are done.
624 This - Calling context
625 Attributes - output buffer which contains attributes
628 EFI_SUCCESS - Successfully returns
632 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
634 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
636 return FvbGetVolumeAttributes (FvbDevice
->Instance
, Attributes
,
637 mFvbModuleGlobal
, EfiGoneVirtual ());
642 FvbProtocolSetAttributes (
643 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
644 IN OUT EFI_FVB_ATTRIBUTES_2
*Attributes
649 Sets Volume attributes. No polarity translations are done.
652 This - Calling context
653 Attributes - output buffer which contains attributes
656 EFI_SUCCESS - Successfully returns
660 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
662 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
664 return FvbSetVolumeAttributes (FvbDevice
->Instance
, Attributes
,
665 mFvbModuleGlobal
, EfiGoneVirtual ());
670 FvbProtocolEraseBlocks (
671 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
678 The EraseBlock() function erases one or more blocks as denoted by the
679 variable argument list. The entire parameter list of blocks must be
680 verified prior to erasing any blocks. If a block is requested that does
681 not exist within the associated firmware volume (it has a larger index than
682 the last block of the firmware volume), the EraseBlock() function must
683 return EFI_INVALID_PARAMETER without modifying the contents of the firmware
687 This - Calling context
688 ... - Starting LBA followed by Number of Lba to erase.
689 a -1 to terminate the list.
692 EFI_SUCCESS - The erase request was successfully completed
693 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
694 EFI_DEVICE_ERROR - The block device is not functioning correctly and
695 could not be written. Firmware device may have been
700 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
701 EFI_FW_VOL_INSTANCE
*FwhInstance
;
708 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
710 Status
= GetFvbInstance (FvbDevice
->Instance
, mFvbModuleGlobal
,
711 &FwhInstance
, EfiGoneVirtual ());
712 ASSERT_EFI_ERROR (Status
);
714 NumOfBlocks
= FwhInstance
->NumOfBlocks
;
716 VA_START (args
, This
);
719 StartingLba
= VA_ARG (args
, EFI_LBA
);
720 if (StartingLba
== EFI_LBA_LIST_TERMINATOR
) {
724 NumOfLba
= VA_ARG (args
, UINT32
);
727 // Check input parameters
729 if ((NumOfLba
== 0) || ((StartingLba
+ NumOfLba
) > NumOfBlocks
)) {
731 return EFI_INVALID_PARAMETER
;
737 VA_START (args
, This
);
739 StartingLba
= VA_ARG (args
, EFI_LBA
);
740 if (StartingLba
== EFI_LBA_LIST_TERMINATOR
) {
744 NumOfLba
= VA_ARG (args
, UINT32
);
746 while (NumOfLba
> 0) {
747 Status
= QemuFlashEraseBlock (StartingLba
);
748 if (EFI_ERROR (Status
)) {
767 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
770 IN OUT UINTN
*NumBytes
,
777 Writes data beginning at Lba:Offset from FV. The write terminates either
778 when *NumBytes of data have been written, or when a block boundary is
779 reached. *NumBytes is updated to reflect the actual number of bytes
780 written. The write opertion does not include erase. This routine will
781 attempt to write only the specified bytes. If the writes do not stick,
782 it will return an error.
785 This - Calling context
786 Lba - Block in which to begin write
787 Offset - Offset in the block at which to begin write
788 NumBytes - On input, indicates the requested write size. On
789 output, indicates the actual number of bytes
791 Buffer - Buffer containing source data for the write.
794 EFI_SUCCESS - The firmware volume was written successfully
795 EFI_BAD_BUFFER_SIZE - Write attempted across a LBA boundary. On output,
796 NumBytes contains the total number of bytes
798 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
799 EFI_DEVICE_ERROR - The block device is not functioning correctly and
801 EFI_INVALID_PARAMETER - NumBytes or Buffer are NULL
805 return QemuFlashWrite ((EFI_LBA
)Lba
, (UINTN
)Offset
, NumBytes
,
812 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
813 IN CONST EFI_LBA Lba
,
814 IN CONST UINTN Offset
,
815 IN OUT UINTN
*NumBytes
,
822 Reads data beginning at Lba:Offset from FV. The Read terminates either
823 when *NumBytes of data have been read, or when a block boundary is
824 reached. *NumBytes is updated to reflect the actual number of bytes
825 written. The write opertion does not include erase. This routine will
826 attempt to write only the specified bytes. If the writes do not stick,
827 it will return an error.
830 This - Calling context
831 Lba - Block in which to begin Read
832 Offset - Offset in the block at which to begin Read
833 NumBytes - On input, indicates the requested write size. On
834 output, indicates the actual number of bytes Read
835 Buffer - Buffer containing source data for the Read.
838 EFI_SUCCESS - The firmware volume was read successfully and
839 contents are in Buffer
840 EFI_BAD_BUFFER_SIZE - Read attempted across a LBA boundary. On output,
841 NumBytes contains the total number of bytes
843 EFI_ACCESS_DENIED - The firmware volume is in the ReadDisabled state
844 EFI_DEVICE_ERROR - The block device is not functioning correctly and
846 EFI_INVALID_PARAMETER - NumBytes or Buffer are NULL
850 return QemuFlashRead ((EFI_LBA
)Lba
, (UINTN
)Offset
, NumBytes
,
856 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
861 Check the integrity of firmware volume header
864 FwVolHeader - A pointer to a firmware volume header
867 EFI_SUCCESS - The firmware volume is consistent
868 EFI_NOT_FOUND - The firmware volume has corrupted. So it is not an
876 // Verify the header revision, header signature, length
877 // Length of FvBlock cannot be 2**64-1
878 // HeaderLength cannot be an odd number
880 if ((FwVolHeader
->Revision
!= EFI_FVH_REVISION
) ||
881 (FwVolHeader
->Signature
!= EFI_FVH_SIGNATURE
) ||
882 (FwVolHeader
->FvLength
== ((UINTN
) -1)) ||
883 ((FwVolHeader
->HeaderLength
& 0x01) != 0)
885 return EFI_NOT_FOUND
;
889 // Verify the header checksum
892 Checksum
= CalculateSum16 ((UINT16
*) FwVolHeader
,
893 FwVolHeader
->HeaderLength
);
898 (UINT16
) (((UINTN
) FwVolHeader
->Checksum
+ 0x10000 - Checksum
) & 0xffff);
900 DEBUG ((EFI_D_INFO
, "FV@%p Checksum is 0x%x, expected 0x%x\n",
901 FwVolHeader
, FwVolHeader
->Checksum
, Expected
));
902 return EFI_NOT_FOUND
;
910 MarkMemoryRangeForRuntimeAccess (
911 EFI_PHYSICAL_ADDRESS BaseAddress
,
918 // Mark flash region as runtime memory
920 Status
= gDS
->RemoveMemorySpace (
925 Status
= gDS
->AddMemorySpace (
926 EfiGcdMemoryTypeSystemMemory
,
929 EFI_MEMORY_UC
| EFI_MEMORY_RUNTIME
931 ASSERT_EFI_ERROR (Status
);
933 Status
= gBS
->AllocatePages (
935 EfiRuntimeServicesData
,
936 EFI_SIZE_TO_PAGES (Length
),
939 ASSERT_EFI_ERROR (Status
);
946 InitializeVariableFvHeader (
951 EFI_FIRMWARE_VOLUME_HEADER
*GoodFwVolHeader
;
952 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
958 (EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
)
959 PcdGet32 (PcdOvmfFlashNvStorageVariableBase
);
962 (FixedPcdGet32 (PcdFlashNvStorageVariableSize
) +
963 FixedPcdGet32 (PcdFlashNvStorageFtwWorkingSize
) +
964 FixedPcdGet32 (PcdFlashNvStorageFtwSpareSize
) +
965 FixedPcdGet32 (PcdOvmfFlashNvStorageEventLogSize
));
967 BlockSize
= PcdGet32 (PcdOvmfFirmwareBlockSize
);
969 Status
= ValidateFvHeader (FwVolHeader
);
970 if (!EFI_ERROR (Status
)) {
971 if (FwVolHeader
->FvLength
!= Length
||
972 FwVolHeader
->BlockMap
[0].Length
!= BlockSize
) {
973 Status
= EFI_VOLUME_CORRUPTED
;
976 if (EFI_ERROR (Status
)) {
981 "Variable FV header is not valid. It will be reinitialized.\n"));
984 // Get FvbInfo to provide in FwhInstance.
986 Status
= GetFvbInfo (Length
, &GoodFwVolHeader
);
987 ASSERT (!EFI_ERROR (Status
));
989 Start
= (UINTN
)(UINT8
*) FwVolHeader
- PcdGet32 (PcdOvmfFdBaseAddress
);
990 ASSERT (Start
% BlockSize
== 0 && Length
% BlockSize
== 0);
991 ASSERT (GoodFwVolHeader
->HeaderLength
<= BlockSize
);
994 // Erase all the blocks
996 for (Offset
= Start
; Offset
< Start
+ Length
; Offset
+= BlockSize
) {
997 Status
= QemuFlashEraseBlock (Offset
/ BlockSize
);
998 ASSERT_EFI_ERROR (Status
);
1002 // Write good FV header
1004 WriteLength
= GoodFwVolHeader
->HeaderLength
;
1005 Status
= QemuFlashWrite (
1009 (UINT8
*) GoodFwVolHeader
);
1010 ASSERT_EFI_ERROR (Status
);
1011 ASSERT (WriteLength
== GoodFwVolHeader
->HeaderLength
);
1020 IN EFI_HANDLE ImageHandle
,
1021 IN EFI_SYSTEM_TABLE
*SystemTable
1025 Routine Description:
1026 This function does common initialization for FVB services
1035 EFI_FW_VOL_INSTANCE
*FwhInstance
;
1036 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
1038 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
1039 EFI_HANDLE FwbHandle
;
1040 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
1041 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*OldFwbInterface
;
1043 EFI_PHYSICAL_ADDRESS BaseAddress
;
1046 EFI_EVENT VirtualAddressChangeEvent
;
1048 if (EFI_ERROR (QemuFlashInitialize ())) {
1050 // Return an error so image will be unloaded
1053 "QEMU flash was not detected. Writable FVB is not being installed.\n"));
1054 return EFI_WRITE_PROTECTED
;
1058 // Allocate runtime services data for global variable, which contains
1059 // the private data of all firmware volume block instances
1061 mFvbModuleGlobal
= AllocateRuntimePool (sizeof (ESAL_FWB_GLOBAL
));
1062 ASSERT (mFvbModuleGlobal
!= NULL
);
1064 BaseAddress
= (UINTN
) PcdGet32 (PcdOvmfFdBaseAddress
);
1065 Length
= PcdGet32 (PcdOvmfFirmwareFdSize
);
1067 Status
= InitializeVariableFvHeader ();
1068 if (EFI_ERROR (Status
)) {
1070 "QEMU Flash: Unable to initialize variable FV header\n"));
1071 return EFI_WRITE_PROTECTED
;
1074 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) BaseAddress
;
1075 Status
= ValidateFvHeader (FwVolHeader
);
1076 if (EFI_ERROR (Status
)) {
1080 Status
= GetFvbInfo (Length
, &FwVolHeader
);
1081 if (EFI_ERROR (Status
)) {
1082 DEBUG ((EFI_D_INFO
, "EFI_ERROR (GetFvbInfo (Length, &FwVolHeader))\n"));
1083 return EFI_WRITE_PROTECTED
;
1087 BufferSize
= (sizeof (EFI_FW_VOL_INSTANCE
) +
1088 FwVolHeader
->HeaderLength
-
1089 sizeof (EFI_FIRMWARE_VOLUME_HEADER
)
1093 // Only need to allocate once. There is only one copy of physical memory for
1094 // the private data of each FV instance. But in virtual mode or in physical
1095 // mode, the address of the the physical memory may be different.
1097 mFvbModuleGlobal
->FvInstance
[FVB_PHYSICAL
] = AllocateRuntimePool (
1099 ASSERT (mFvbModuleGlobal
->FvInstance
[FVB_PHYSICAL
] != NULL
);
1102 // Make a virtual copy of the FvInstance pointer.
1104 FwhInstance
= mFvbModuleGlobal
->FvInstance
[FVB_PHYSICAL
];
1105 mFvbModuleGlobal
->FvInstance
[FVB_VIRTUAL
] = FwhInstance
;
1107 mFvbModuleGlobal
->NumFv
= 0;
1111 (EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
)
1112 PcdGet32 (PcdOvmfFlashNvStorageVariableBase
);
1114 FwhInstance
->FvBase
[FVB_PHYSICAL
] = (UINTN
) BaseAddress
;
1115 FwhInstance
->FvBase
[FVB_VIRTUAL
] = (UINTN
) BaseAddress
;
1117 CopyMem ((UINTN
*) &(FwhInstance
->VolumeHeader
), (UINTN
*) FwVolHeader
,
1118 FwVolHeader
->HeaderLength
);
1119 FwVolHeader
= &(FwhInstance
->VolumeHeader
);
1123 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
;
1124 PtrBlockMapEntry
->NumBlocks
!= 0;
1125 PtrBlockMapEntry
++) {
1127 // Get the maximum size of a block.
1129 if (MaxLbaSize
< PtrBlockMapEntry
->Length
) {
1130 MaxLbaSize
= PtrBlockMapEntry
->Length
;
1133 NumOfBlocks
= NumOfBlocks
+ PtrBlockMapEntry
->NumBlocks
;
1137 // The total number of blocks in the FV.
1139 FwhInstance
->NumOfBlocks
= NumOfBlocks
;
1142 // Add a FVB Protocol Instance
1144 FvbDevice
= AllocateRuntimePool (sizeof (EFI_FW_VOL_BLOCK_DEVICE
));
1145 ASSERT (FvbDevice
!= NULL
);
1147 CopyMem (FvbDevice
, &mFvbDeviceTemplate
, sizeof (EFI_FW_VOL_BLOCK_DEVICE
));
1149 FvbDevice
->Instance
= mFvbModuleGlobal
->NumFv
;
1150 mFvbModuleGlobal
->NumFv
++;
1153 // Set up the devicepath
1155 if (FwVolHeader
->ExtHeaderOffset
== 0) {
1156 FV_MEMMAP_DEVICE_PATH
*FvMemmapDevicePath
;
1159 // FV does not contains extension header, then produce MEMMAP_DEVICE_PATH
1161 FvMemmapDevicePath
= AllocateCopyPool (sizeof (FV_MEMMAP_DEVICE_PATH
),
1162 &mFvMemmapDevicePathTemplate
);
1163 FvMemmapDevicePath
->MemMapDevPath
.StartingAddress
= BaseAddress
;
1164 FvMemmapDevicePath
->MemMapDevPath
.EndingAddress
=
1165 BaseAddress
+ FwVolHeader
->FvLength
- 1;
1166 FvbDevice
->DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*)FvMemmapDevicePath
;
1168 FV_PIWG_DEVICE_PATH
*FvPiwgDevicePath
;
1170 FvPiwgDevicePath
= AllocateCopyPool (sizeof (FV_PIWG_DEVICE_PATH
),
1171 &mFvPIWGDevicePathTemplate
);
1173 &FvPiwgDevicePath
->FvDevPath
.FvName
,
1174 (GUID
*)(UINTN
)(BaseAddress
+ FwVolHeader
->ExtHeaderOffset
)
1176 FvbDevice
->DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*)FvPiwgDevicePath
;
1180 // Find a handle with a matching device path that has supports FW Block
1183 Status
= gBS
->LocateDevicePath (&gEfiFirmwareVolumeBlockProtocolGuid
,
1184 &FvbDevice
->DevicePath
, &FwbHandle
);
1185 if (EFI_ERROR (Status
)) {
1187 // LocateDevicePath fails so install a new interface and device path
1190 DEBUG ((EFI_D_INFO
, "Installing QEMU flash FVB\n"));
1191 Status
= gBS
->InstallMultipleProtocolInterfaces (
1193 &gEfiFirmwareVolumeBlockProtocolGuid
,
1194 &FvbDevice
->FwVolBlockInstance
,
1195 &gEfiDevicePathProtocolGuid
,
1196 FvbDevice
->DevicePath
,
1199 ASSERT_EFI_ERROR (Status
);
1200 } else if (IsDevicePathEnd (FvbDevice
->DevicePath
)) {
1202 // Device already exists, so reinstall the FVB protocol
1204 Status
= gBS
->HandleProtocol (
1206 &gEfiFirmwareVolumeBlockProtocolGuid
,
1207 (VOID
**)&OldFwbInterface
1209 ASSERT_EFI_ERROR (Status
);
1211 DEBUG ((EFI_D_INFO
, "Reinstalling FVB for QEMU flash region\n"));
1212 Status
= gBS
->ReinstallProtocolInterface (
1214 &gEfiFirmwareVolumeBlockProtocolGuid
,
1216 &FvbDevice
->FwVolBlockInstance
1218 ASSERT_EFI_ERROR (Status
);
1221 // There was a FVB protocol on an End Device Path node
1226 MarkMemoryRangeForRuntimeAccess (BaseAddress
, Length
);
1229 // Set several PCD values to point to flash
1232 PcdFlashNvStorageVariableBase64
,
1233 (UINTN
) PcdGet32 (PcdOvmfFlashNvStorageVariableBase
)
1236 PcdFlashNvStorageFtwWorkingBase
,
1237 PcdGet32 (PcdOvmfFlashNvStorageFtwWorkingBase
)
1240 PcdFlashNvStorageFtwSpareBase
,
1241 PcdGet32 (PcdOvmfFlashNvStorageFtwSpareBase
)
1244 FwhInstance
= (EFI_FW_VOL_INSTANCE
*)
1246 (UINTN
) ((UINT8
*) FwhInstance
) + FwVolHeader
->HeaderLength
+
1247 (sizeof (EFI_FW_VOL_INSTANCE
) - sizeof (EFI_FIRMWARE_VOLUME_HEADER
))
1250 VirtualAddressChangeEvent
= NULL
;
1251 Status
= gBS
->CreateEventEx (
1254 FvbVirtualddressChangeEvent
,
1256 &gEfiEventVirtualAddressChangeGuid
,
1257 &VirtualAddressChangeEvent
1259 ASSERT_EFI_ERROR (Status
);
1261 PcdSetBool (PcdOvmfFlashVariablesEnable
, TRUE
);