2 Firmware Volume Block Driver for Lakeport Platform.
4 Firmware volume block driver for FWH or SPI device.
5 It depends on which Flash Device Library to be linked with this driver.
7 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
9 This program and the accompanying materials are licensed and made available under
10 the terms and conditions of the BSD License that accompanies this distribution.
11 The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php.
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 #include "FvbService.h"
23 // Global variable for this FVB driver which contains
24 // the private data of all firmware volume block instances.
26 FWB_GLOBAL mFvbModuleGlobal
;
29 // This platform driver knows there are 3 FVs on
30 // FD, which are FvRecovery, FvMain and FvNvStorage.
32 UINT32 mPlatformFvBaseAddress
[] = {
33 FixedPcdGet32(PcdFlashNvStorageVariableBase
),
36 FV_MEMMAP_DEVICE_PATH mFvMemmapDevicePathTemplate
= {
42 (UINT8
)(sizeof (MEMMAP_DEVICE_PATH
)),
43 (UINT8
)(sizeof (MEMMAP_DEVICE_PATH
) >> 8)
47 (EFI_PHYSICAL_ADDRESS
) 0,
48 (EFI_PHYSICAL_ADDRESS
) 0,
52 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
54 END_DEVICE_PATH_LENGTH
,
60 FV_PIWG_DEVICE_PATH mFvPIWGDevicePathTemplate
= {
66 (UINT8
)(sizeof (MEDIA_FW_VOL_DEVICE_PATH
)),
67 (UINT8
)(sizeof (MEDIA_FW_VOL_DEVICE_PATH
) >> 8)
74 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
76 END_DEVICE_PATH_LENGTH
,
83 // Template structure used when installing FVB protocol.
85 EFI_FW_VOL_BLOCK_DEVICE mFvbDeviceTemplate
= {
90 FvbProtocolGetAttributes
,
91 FvbProtocolSetAttributes
,
92 FvbProtocolGetPhysicalAddress
,
93 FvbProtocolGetBlockSize
,
96 FvbProtocolEraseBlocks
,
98 } // FwVolBlockInstance
103 Get the pointer to EFI_FW_VOL_INSTANCE from the buffer pointed
104 by mFvbModuleGlobal.FvInstance based on a index.
105 Each EFI_FW_VOL_INSTANCE is with variable length as
106 we have a block map at the end of the EFI_FIRMWARE_VOLUME_HEADER.
108 @param[in] Instance The index of the EFI_FW_VOL_INSTANCE.
110 @return A pointer to EFI_FW_VOL_INSTANCE.
113 EFI_FW_VOL_INSTANCE
*
118 EFI_FW_VOL_INSTANCE
*FwhRecord
;
120 if ( Instance
>= mFvbModuleGlobal
.NumFv
) {
121 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER
);
126 // Find the right instance of the FVB private data.
128 FwhRecord
= mFvbModuleGlobal
.FvInstance
;
129 while ( Instance
> 0 ) {
130 FwhRecord
= (EFI_FW_VOL_INSTANCE
*) ((UINTN
)((UINT8
*)FwhRecord
) +
131 FwhRecord
->VolumeHeader
.HeaderLength
+
132 (sizeof (EFI_FW_VOL_INSTANCE
) - sizeof (EFI_FIRMWARE_VOLUME_HEADER
)));
142 Get the EFI_FVB_ATTRIBUTES_2 of a FV.
144 @param[in] The index of the EFI_FW_VOL_INSTANCE.
146 @return EFI_FVB_ATTRIBUTES_2 of the FV identified by Instance.
151 FvbGetVolumeAttributes (
155 EFI_FW_VOL_INSTANCE
* FwInstance
= NULL
;
156 FwInstance
= GetFvbInstance(Instance
);
157 ASSERT (FwInstance
!= NULL
);
159 if ( FwInstance
!= NULL
) {
160 return FwInstance
->VolumeHeader
.Attributes
;
168 Retrieves the starting address of an LBA in an FV. It also
169 return a few other attribut of the FV.
171 @param[in] Instance The index of the EFI_FW_VOL_INSTANCE.
172 @param[in] Lba The logical block address.
173 @param[out] LbaAddress On output, contains the physical starting address
175 @param[out] LbaLength On output, contains the length of the block.
176 @param[out] NumOfBlocks A pointer to a caller allocated UINTN in which the
177 number of consecutive blocks starting with Lba is
178 returned. All blocks in this range have a size of
181 @retval EFI_SUCCESS Successfully returns.
182 @retval EFI_INVALID_PARAMETER Instance not found.
190 OUT UINTN
*LbaAddress
,
191 OUT UINTN
*LbaLength
,
192 OUT UINTN
*NumOfBlocks
195 UINT32 NumBlocks
= 0;
196 UINT32 BlockLength
= 0;
200 EFI_FW_VOL_INSTANCE
*FwhInstance
;
201 EFI_FV_BLOCK_MAP_ENTRY
*BlockMap
= NULL
;
204 // Find the right instance of the FVB private data.
206 FwhInstance
= GetFvbInstance (Instance
);
210 BlockMap
= &(FwhInstance
->VolumeHeader
.BlockMap
[0]);
211 ASSERT (BlockMap
!= NULL
);
214 // Parse the blockmap of the FV to find which map entry the Lba belongs to.
217 if ( BlockMap
!= NULL
) {
218 NumBlocks
= BlockMap
->NumBlocks
;
219 BlockLength
= BlockMap
->Length
;
222 if ( NumBlocks
== 0 || BlockLength
== 0) {
223 return EFI_INVALID_PARAMETER
;
226 NextLba
= StartLba
+ NumBlocks
;
229 // The map entry found.
231 if (Lba
>= StartLba
&& Lba
< NextLba
) {
232 Offset
= Offset
+ (UINTN
)MultU64x32((Lba
- StartLba
), BlockLength
);
233 if ( LbaAddress
&& FwhInstance
) {
234 *LbaAddress
= FwhInstance
->FvBase
+ Offset
;
238 *LbaLength
= BlockLength
;
242 *NumOfBlocks
= (UINTN
)(NextLba
- Lba
);
248 Offset
= Offset
+ NumBlocks
* BlockLength
;
255 Reads specified number of bytes into a buffer from the specified block.
257 @param[in] Instance The FV instance to be read from.
258 @param[in] Lba The logical block address to be read from.
259 @param[in] BlockOffset Offset into the block at which to begin reading.
260 @param[in] NumBytes Pointer that on input contains the total size of
261 the buffer. On output, it contains the total number
263 @param[in] Buffer Pointer to a caller allocated buffer that will be
264 used to hold the data read.
267 @retval EFI_SUCCESS The firmware volume was read successfully and
268 contents are in Buffer.
269 @retval EFI_BAD_BUFFER_SIZE Read attempted across a LBA boundary. On output,
270 NumBytes contains the total number of bytes returned
272 @retval EFI_ACCESS_DENIED The firmware volume is in the ReadDisabled state.
273 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
275 @retval EFI_INVALID_PARAMETER Instance not found, or NumBytes, Buffer are NULL.
283 IN UINTN BlockOffset
,
284 IN OUT UINTN
*NumBytes
,
288 EFI_FVB_ATTRIBUTES_2 Attributes
;
293 if ( (NumBytes
== NULL
) || (Buffer
== NULL
)) {
294 return (EFI_INVALID_PARAMETER
);
296 if (*NumBytes
== 0) {
297 return (EFI_INVALID_PARAMETER
);
300 Status
= FvbGetLbaAddress (Instance
, Lba
, &LbaAddress
, &LbaLength
, NULL
);
301 if (EFI_ERROR(Status
)) {
305 Attributes
= FvbGetVolumeAttributes (Instance
);
307 if ( (Attributes
& EFI_FVB2_READ_STATUS
) == 0) {
308 return (EFI_ACCESS_DENIED
);
311 if (BlockOffset
> LbaLength
) {
312 return (EFI_INVALID_PARAMETER
);
315 if (LbaLength
< ( *NumBytes
+ BlockOffset
) ) {
316 *NumBytes
= (UINT32
) (LbaLength
- BlockOffset
);
317 Status
= EFI_BAD_BUFFER_SIZE
;
320 LibFvbFlashDeviceRead (LbaAddress
+ BlockOffset
, NumBytes
, Buffer
);
327 Writes specified number of bytes from the input buffer to the block.
329 @param[in] Instance The FV instance to be written to.
330 @param[in] Lba The starting logical block index to write to.
331 @param[in] BlockOffset Offset into the block at which to begin writing.
332 @param[in] NumBytes Pointer that on input contains the total size of
333 the buffer. On output, it contains the total number
334 of bytes actually written.
335 @param[in] Buffer Pointer to a caller allocated buffer that contains
336 the source for the write.
337 @retval EFI_SUCCESS The firmware volume was written successfully.
338 @retval EFI_BAD_BUFFER_SIZE Write attempted across a LBA boundary. On output,
339 NumBytes contains the total number of bytes
341 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
342 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
343 could not be written.
344 @retval EFI_INVALID_PARAMETER Instance not found, or NumBytes, Buffer are NULL.
351 IN UINTN BlockOffset
,
352 IN OUT UINTN
*NumBytes
,
356 EFI_FVB_ATTRIBUTES_2 Attributes
;
362 if ( (NumBytes
== NULL
) || (Buffer
== NULL
)) {
363 return (EFI_INVALID_PARAMETER
);
365 if (*NumBytes
== 0) {
366 return (EFI_INVALID_PARAMETER
);
369 Status
= FvbGetLbaAddress (Instance
, Lba
, &LbaAddress
, &LbaLength
, NULL
);
370 if (EFI_ERROR(Status
)) {
375 // Check if the FV is write enabled.
377 Attributes
= FvbGetVolumeAttributes (Instance
);
378 if ( (Attributes
& EFI_FVB2_WRITE_STATUS
) == 0) {
379 return (EFI_ACCESS_DENIED
);
383 // Perform boundary checks and adjust NumBytes.
385 if (BlockOffset
> LbaLength
) {
386 return (EFI_INVALID_PARAMETER
);
389 if ( LbaLength
< ( *NumBytes
+ BlockOffset
) ) {
391 "FvWriteBlock: Reducing Numbytes from 0x%x to 0x%x\n",
393 (UINT32
)(LbaLength
-BlockOffset
))
395 *NumBytes
= (UINT32
) (LbaLength
- BlockOffset
);
396 Status
= EFI_BAD_BUFFER_SIZE
;
399 LibFvbFlashDeviceBlockLock (LbaAddress
, LbaLength
, FALSE
);
401 Status1
= LibFvbFlashDeviceWrite (LbaAddress
+ BlockOffset
, NumBytes
, Buffer
);
403 LibFvbFlashDeviceBlockLock (LbaAddress
, LbaLength
, TRUE
);
404 WriteBackInvalidateDataCacheRange ((VOID
*) (LbaAddress
+ BlockOffset
), *NumBytes
);
406 if ( EFI_ERROR (Status1
) ) {
415 Erases and initializes a firmware volume block.
417 @param[in] Instance The FV instance to be erased.
418 @param[in] Lba The logical block index to be erased.
420 @retval EFI_SUCCESS The erase request was successfully completed.
421 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
422 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
423 could not be written. Firmware device may have been
425 @retval EFI_INVALID_PARAMETER Instance not found.
434 EFI_FVB_ATTRIBUTES_2 Attributes
;
440 // Check if the FV is write enabled.
442 Attributes
= FvbGetVolumeAttributes (Instance
);
444 if( (Attributes
& EFI_FVB2_WRITE_STATUS
) == 0) {
445 return (EFI_ACCESS_DENIED
);
449 // Get the starting address of the block for erase.
451 Status
= FvbGetLbaAddress (Instance
, Lba
, &LbaAddress
, &LbaLength
, NULL
);
452 if (EFI_ERROR(Status
)) {
456 LibFvbFlashDeviceBlockLock (LbaAddress
, LbaLength
, FALSE
);
458 Status
= LibFvbFlashDeviceBlockErase (LbaAddress
, LbaLength
);
460 LibFvbFlashDeviceBlockLock (LbaAddress
, LbaLength
, TRUE
);
462 WriteBackInvalidateDataCacheRange ((VOID
*) LbaAddress
, LbaLength
);
469 Modifies the current settings of the firmware volume according to the
470 input parameter, and returns the new setting of the volume.
472 @param[in] Instance The FV instance whose attributes is going to be
474 @param[in] Attributes On input, it is a pointer to EFI_FVB_ATTRIBUTES_2
475 containing the desired firmware volume settings.
476 On successful return, it contains the new settings
477 of the firmware volume.
479 @retval EFI_SUCCESS Successfully returns.
480 @retval EFI_ACCESS_DENIED The volume setting is locked and cannot be modified.
481 @retval EFI_INVALID_PARAMETER Instance not found, or The attributes requested are
482 in conflict with the capabilities as declared in the
483 firmware volume header.
488 FvbSetVolumeAttributes (
490 IN OUT EFI_FVB_ATTRIBUTES_2
*Attributes
493 EFI_FW_VOL_INSTANCE
*FwhInstance
= NULL
;
494 EFI_FVB_ATTRIBUTES_2 OldAttributes
= 0;
495 EFI_FVB_ATTRIBUTES_2
*AttribPtr
= NULL
;
496 EFI_FVB_ATTRIBUTES_2 UnchangedAttributes
;
498 UINT32 OldStatus
, NewStatus
;
501 // Find the right instance of the FVB private data.
503 FwhInstance
= GetFvbInstance (Instance
);
505 AttribPtr
= (EFI_FVB_ATTRIBUTES_2
*) & (FwhInstance
->VolumeHeader
.Attributes
);
506 ASSERT (AttribPtr
!= NULL
);
508 if ( AttribPtr
!= NULL
) {
509 OldAttributes
= *AttribPtr
;
512 Capabilities
= OldAttributes
& EFI_FVB2_CAPABILITIES
;
513 OldStatus
= OldAttributes
& EFI_FVB2_STATUS
;
514 NewStatus
= *Attributes
& EFI_FVB2_STATUS
;
516 UnchangedAttributes
= EFI_FVB2_READ_DISABLED_CAP
| \
517 EFI_FVB2_READ_ENABLED_CAP
| \
518 EFI_FVB2_WRITE_DISABLED_CAP
| \
519 EFI_FVB2_WRITE_ENABLED_CAP
| \
520 EFI_FVB2_LOCK_CAP
| \
521 EFI_FVB2_STICKY_WRITE
| \
522 EFI_FVB2_MEMORY_MAPPED
| \
523 EFI_FVB2_ERASE_POLARITY
| \
524 EFI_FVB2_READ_LOCK_CAP
| \
525 EFI_FVB2_WRITE_LOCK_CAP
| \
529 // Some attributes of FV is read only can *not* be set.
531 if ((OldAttributes
& UnchangedAttributes
) ^ (*Attributes
& UnchangedAttributes
)) {
532 return EFI_INVALID_PARAMETER
;
536 // If firmware volume is locked, no status bit can be updated.
538 if ( OldAttributes
& EFI_FVB2_LOCK_STATUS
) {
539 if ( OldStatus
^ NewStatus
) {
540 return EFI_ACCESS_DENIED
;
545 // Test read disable.
547 if ((Capabilities
& EFI_FVB2_READ_DISABLED_CAP
) == 0) {
548 if ((NewStatus
& EFI_FVB2_READ_STATUS
) == 0) {
549 return EFI_INVALID_PARAMETER
;
556 if ((Capabilities
& EFI_FVB2_READ_ENABLED_CAP
) == 0) {
557 if (NewStatus
& EFI_FVB2_READ_STATUS
) {
558 return EFI_INVALID_PARAMETER
;
563 // Test write disable.
565 if ((Capabilities
& EFI_FVB2_WRITE_DISABLED_CAP
) == 0) {
566 if ((NewStatus
& EFI_FVB2_WRITE_STATUS
) == 0) {
567 return EFI_INVALID_PARAMETER
;
572 // Test write enable.
574 if ((Capabilities
& EFI_FVB2_WRITE_ENABLED_CAP
) == 0) {
575 if (NewStatus
& EFI_FVB2_WRITE_STATUS
) {
576 return EFI_INVALID_PARAMETER
;
583 if ((Capabilities
& EFI_FVB2_LOCK_CAP
) == 0) {
584 if (NewStatus
& EFI_FVB2_LOCK_STATUS
) {
585 return EFI_INVALID_PARAMETER
;
589 *AttribPtr
= (*AttribPtr
) & (0xFFFFFFFF & (~EFI_FVB2_STATUS
));
590 *AttribPtr
= (*AttribPtr
) | NewStatus
;
591 *Attributes
= *AttribPtr
;
597 // FVB protocol APIs.
600 Retrieves the physical address of the device.
602 @param[in] This A pointer to EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL.
603 @param[out] Address Output buffer containing the address.
605 retval EFI_SUCCESS The function always return successfully.
610 FvbProtocolGetPhysicalAddress (
611 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
612 OUT EFI_PHYSICAL_ADDRESS
*Address
615 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
616 EFI_FW_VOL_INSTANCE
*FvInstance
;
618 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
619 FvInstance
= GetFvbInstance(FvbDevice
->Instance
);
621 if (FvInstance
!= NULL
) {
622 *Address
= FvInstance
->FvBase
;
630 Retrieve the size of a logical block.
632 @param[in] This Calling context.
633 @param[in] Lba Indicates which block to return the size for.
634 @param[out] BlockSize A pointer to a caller allocated UINTN in which
635 the size of the block is returned.
636 @param[out] NumOfBlocks A pointer to a caller allocated UINTN in which the
637 number of consecutive blocks starting with Lba is
638 returned. All blocks in this range have a size of
641 @retval EFI_SUCCESS The function always return successfully.
646 FvbProtocolGetBlockSize (
647 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
649 OUT UINTN
*BlockSize
,
650 OUT UINTN
*NumOfBlocks
653 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
656 "FvbProtocolGetBlockSize: Lba: 0x%lx BlockSize: 0x%x NumOfBlocks: 0x%x\n",
662 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
664 return FvbGetLbaAddress (
675 Retrieves Volume attributes. No polarity translations are done.
677 @param[in] This Calling context.
678 @param[out] Attributes Output buffer which contains attributes.
680 @retval EFI_SUCCESS The function always return successfully.
685 FvbProtocolGetAttributes (
686 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
687 OUT EFI_FVB_ATTRIBUTES_2
*Attributes
690 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
692 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
694 *Attributes
= FvbGetVolumeAttributes (FvbDevice
->Instance
);
697 "FvbProtocolGetAttributes: This: 0x%x Attributes: 0x%x\n",
707 Sets Volume attributes. No polarity translations are done.
709 @param[in] This Calling context.
710 @param[out] Attributes Output buffer which contains attributes.
712 @retval EFI_SUCCESS The function always return successfully.
717 FvbProtocolSetAttributes (
718 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
719 IN OUT EFI_FVB_ATTRIBUTES_2
*Attributes
723 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
726 "FvbProtocolSetAttributes: Before SET - This: 0x%x Attributes: 0x%x\n",
731 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
733 Status
= FvbSetVolumeAttributes (FvbDevice
->Instance
, Attributes
);
736 "FvbProtocolSetAttributes: After SET - This: 0x%x Attributes: 0x%x\n",
746 The EraseBlock() function erases one or more blocks as denoted by the
747 variable argument list. The entire parameter list of blocks must be verified
748 prior to erasing any blocks. If a block is requested that does not exist
749 within the associated firmware volume (it has a larger index than the last
750 block of the firmware volume), the EraseBlock() function must return
751 EFI_INVALID_PARAMETER without modifying the contents of the firmware volume.
753 @param[in] This Calling context.
754 @param[in] ... Starting LBA followed by Number of Lba to erase.
755 a -1 to terminate the list.
757 @retval EFI_SUCCESS The erase request was successfully completed.
758 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
759 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
760 could not be written. Firmware device may have been
766 FvbProtocolEraseBlocks (
767 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
771 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
772 EFI_FW_VOL_INSTANCE
*FwhInstance
;
773 UINTN NumOfBlocks
= 0;
779 DEBUG((EFI_D_INFO
, "FvbProtocolEraseBlocks: \n"));
780 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
782 FwhInstance
= GetFvbInstance (FvbDevice
->Instance
);
784 if (FwhInstance
!= NULL
) {
785 NumOfBlocks
= FwhInstance
->NumOfBlocks
;
788 VA_START (args
, This
);
791 StartingLba
= VA_ARG (args
, EFI_LBA
);
792 if ( StartingLba
== EFI_LBA_LIST_TERMINATOR
) {
796 NumOfLba
= VA_ARG (args
, UINTN
);
799 // Check input parameters.
803 return EFI_INVALID_PARAMETER
;
806 if ( ( StartingLba
+ NumOfLba
) > NumOfBlocks
) {
807 return EFI_INVALID_PARAMETER
;
813 VA_START (args
, This
);
815 StartingLba
= VA_ARG (args
, EFI_LBA
);
816 if (StartingLba
== EFI_LBA_LIST_TERMINATOR
) {
820 NumOfLba
= VA_ARG (args
, UINTN
);
822 while ( NumOfLba
> 0 ) {
823 Status
= FvbEraseBlock (FvbDevice
->Instance
, StartingLba
);
824 if ( EFI_ERROR(Status
)) {
841 Writes data beginning at Lba:Offset from FV. The write terminates either
842 when *NumBytes of data have been written, or when a block boundary is
843 reached. *NumBytes is updated to reflect the actual number of bytes
844 written. The write opertion does not include erase. This routine will
845 attempt to write only the specified bytes. If the writes do not stick,
846 it will return an error.
848 @param[in] This Calling context.
849 @param[in] Lba Block in which to begin write.
850 @param[in] Offset Offset in the block at which to begin write.
851 @param[in,out] NumBytes On input, indicates the requested write size. On
852 output, indicates the actual number of bytes written
853 @param[in] Buffer Buffer containing source data for the write.
855 @retval EFI_SUCCESS The firmware volume was written successfully.
856 @retval EFI_BAD_BUFFER_SIZE Write attempted across a LBA boundary. On output,
857 NumBytes contains the total number of bytes
859 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
860 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
861 could not be written.
862 @retval EFI_INVALID_PARAMETER NumBytes or Buffer are NULL.
868 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
871 IN OUT UINTN
*NumBytes
,
876 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
878 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
881 "FvbProtocolWrite: Lba: 0x%lx Offset: 0x%x NumBytes: 0x%x, Buffer: 0x%x\n",
888 return FvbWriteBlock (FvbDevice
->Instance
, Lba
, Offset
, NumBytes
, Buffer
);
893 Reads data beginning at Lba:Offset from FV. The Read terminates either
894 when *NumBytes of data have been read, or when a block boundary is
895 reached. *NumBytes is updated to reflect the actual number of bytes
896 written. The write opertion does not include erase. This routine will
897 attempt to write only the specified bytes. If the writes do not stick,
898 it will return an error.
900 @param[in] This Calling context.
901 @param[in] Lba Block in which to begin write.
902 @param[in] Offset Offset in the block at which to begin write
903 @param[in,out] NumBytes On input, indicates the requested write size. On
904 output, indicates the actual number of bytes written.
905 @param[in] Buffer Buffer containing source data for the write.
909 @retval EFI_SUCCESS The firmware volume was read successfully and
910 contents are in Buffer.
911 @retval EFI_BAD_BUFFER_SIZE Read attempted across a LBA boundary. On output,
912 NumBytes contains the total number of bytes returned
914 @retval EFI_ACCESS_DENIED The firmware volume is in the ReadDisabled state
915 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
917 @retval EFI_INVALID_PARAMETER NumBytes or Buffer are NULL.
923 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
926 IN OUT UINTN
*NumBytes
,
931 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
934 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
935 Status
= FvbReadBlock (FvbDevice
->Instance
, Lba
, Offset
, NumBytes
, Buffer
);
937 "FvbProtocolRead: Lba: 0x%lx Offset: 0x%x NumBytes: 0x%x, Buffer: 0x%x\n",
949 Check the integrity of firmware volume header.
951 @param[in] FwVolHeader A pointer to a firmware volume header.
953 @retval TRUE The firmware volume is consistent.
954 @retval FALSE The firmware volume has corrupted.
959 IN EFI_PHYSICAL_ADDRESS FvBase
,
960 IN CONST EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
963 if (FvBase
== PcdGet32(PcdFlashNvStorageVariableBase
)) {
964 if (CompareMem (&FwVolHeader
->FileSystemGuid
, &gEfiSystemNvDataFvGuid
, sizeof(EFI_GUID
)) != 0 ) {
968 if (CompareMem (&FwVolHeader
->FileSystemGuid
, &gEfiFirmwareFileSystem2Guid
, sizeof(EFI_GUID
)) != 0 ) {
972 if ( (FwVolHeader
->Revision
!= EFI_FVH_REVISION
) ||
973 (FwVolHeader
->Signature
!= EFI_FVH_SIGNATURE
) ||
974 (FwVolHeader
->FvLength
== ((UINTN
) -1)) ||
975 ((FwVolHeader
->HeaderLength
& 0x01 ) !=0) ) {
979 if (CalculateCheckSum16 ((UINT16
*) FwVolHeader
, FwVolHeader
->HeaderLength
) != 0) {
988 The function does the necessary initialization work for
989 Firmware Volume Block Driver.
991 @retval EFI_SUCCESS This funtion always return EFI_SUCCESS.
992 It will ASSERT on errors.
1000 EFI_FW_VOL_INSTANCE
*FwhInstance
;
1001 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
1002 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
1003 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
1004 EFI_PHYSICAL_ADDRESS BaseAddress
;
1007 UINTN TmpHeaderLength
;
1012 // Calculate the total size for all firmware volume block instances.
1015 for (Idx
= 0; Idx
< 1; Idx
++) {
1016 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) mPlatformFvBaseAddress
[Idx
];
1017 BufferSize
+= (FvHeader
->HeaderLength
+
1018 sizeof (EFI_FW_VOL_INSTANCE
) -
1019 sizeof (EFI_FIRMWARE_VOLUME_HEADER
)
1023 mFvbModuleGlobal
.FvInstance
= (EFI_FW_VOL_INSTANCE
*) AllocateRuntimeZeroPool (BufferSize
);
1024 ASSERT (NULL
!= mFvbModuleGlobal
.FvInstance
);
1028 FwhInstance
= mFvbModuleGlobal
.FvInstance
;
1029 mFvbModuleGlobal
.NumFv
= 0;
1031 for (Idx
= 0; Idx
< 1; Idx
++) {
1032 BaseAddress
= mPlatformFvBaseAddress
[Idx
];
1033 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) BaseAddress
;
1035 if (!IsFvHeaderValid (BaseAddress
, FwVolHeader
)) {
1037 // If not valid, get FvbInfo from the information carried in
1040 DEBUG ((EFI_D_ERROR
, "Fvb: FV header @ 0x%lx invalid\n", BaseAddress
));
1041 Status
= GetFvbInfo (BaseAddress
, &FwVolHeader
);
1042 ASSERT_EFI_ERROR(Status
);
1044 // Write back a healthy FV header.
1046 DEBUG ((EFI_D_ERROR
, "FwBlockService.c: Writing back healthy FV header\n"));
1047 LibFvbFlashDeviceBlockLock ((UINTN
)BaseAddress
, FwVolHeader
->BlockMap
->Length
, FALSE
);
1049 Status
= LibFvbFlashDeviceBlockErase ((UINTN
)BaseAddress
, FwVolHeader
->BlockMap
->Length
);
1051 TmpHeaderLength
= (UINTN
) FwVolHeader
->HeaderLength
;
1052 Status
= LibFvbFlashDeviceWrite (
1055 (UINT8
*) FwVolHeader
1058 LibFvbFlashDeviceBlockLock ((UINTN
)BaseAddress
, FwVolHeader
->BlockMap
->Length
, TRUE
);
1060 WriteBackInvalidateDataCacheRange (
1061 (VOID
*) (UINTN
) BaseAddress
,
1062 FwVolHeader
->BlockMap
->Length
1067 CopyMem (&(FwhInstance
->VolumeHeader
), FwVolHeader
, FwVolHeader
->HeaderLength
);
1069 FwVolHeader
= &(FwhInstance
->VolumeHeader
);
1070 FwhInstance
->FvBase
= (UINTN
)BaseAddress
;
1073 // Process the block map for each FV.
1075 FwhInstance
->NumOfBlocks
= 0;
1076 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
1078 // Get the maximum size of a block.
1080 if (MaxLbaSize
< PtrBlockMapEntry
->Length
) {
1081 MaxLbaSize
= PtrBlockMapEntry
->Length
;
1083 FwhInstance
->NumOfBlocks
+= PtrBlockMapEntry
->NumBlocks
;
1087 // Add a FVB Protocol Instance.
1089 mFvbModuleGlobal
.NumFv
++;
1090 InstallFvbProtocol (FwhInstance
, mFvbModuleGlobal
.NumFv
- 1);
1093 // Move on to the next FwhInstance.
1095 FwhInstance
= (EFI_FW_VOL_INSTANCE
*) ((UINTN
)((UINT8
*)FwhInstance
) +
1096 FwVolHeader
->HeaderLength
+
1097 (sizeof (EFI_FW_VOL_INSTANCE
) - sizeof (EFI_FIRMWARE_VOLUME_HEADER
)));