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_EFI_ERROR (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_EFI_ERROR (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
;
359 EFI_FW_VOL_INSTANCE
*FwhInstance
;
363 FwhInstance
= GetFvbInstance (Instance
);
365 if ( (NumBytes
== NULL
) || (Buffer
== NULL
)) {
366 return (EFI_INVALID_PARAMETER
);
368 if (*NumBytes
== 0) {
369 return (EFI_INVALID_PARAMETER
);
372 Status
= FvbGetLbaAddress (Instance
, Lba
, &LbaAddress
, &LbaLength
, NULL
);
373 if (EFI_ERROR(Status
)) {
378 // Check if the FV is write enabled.
380 Attributes
= FvbGetVolumeAttributes (Instance
);
381 if ( (Attributes
& EFI_FVB2_WRITE_STATUS
) == 0) {
382 return (EFI_ACCESS_DENIED
);
386 // Perform boundary checks and adjust NumBytes.
388 if (BlockOffset
> LbaLength
) {
389 return (EFI_INVALID_PARAMETER
);
392 if ( LbaLength
< ( *NumBytes
+ BlockOffset
) ) {
394 "FvWriteBlock: Reducing Numbytes from 0x%x to 0x%x\n",
396 (UINT32
)(LbaLength
-BlockOffset
))
398 *NumBytes
= (UINT32
) (LbaLength
- BlockOffset
);
399 Status
= EFI_BAD_BUFFER_SIZE
;
402 LibFvbFlashDeviceBlockLock (LbaAddress
, LbaLength
, FALSE
);
404 Status1
= LibFvbFlashDeviceWrite (LbaAddress
+ BlockOffset
, NumBytes
, Buffer
);
406 LibFvbFlashDeviceBlockLock (LbaAddress
, LbaLength
, TRUE
);
407 WriteBackInvalidateDataCacheRange ((VOID
*) (LbaAddress
+ BlockOffset
), *NumBytes
);
409 if ( EFI_ERROR (Status1
) ) {
418 Erases and initializes a firmware volume block.
420 @param[in] Instance The FV instance to be erased.
421 @param[in] Lba The logical block index to be erased.
423 @retval EFI_SUCCESS The erase request was successfully completed.
424 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
425 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
426 could not be written. Firmware device may have been
428 @retval EFI_INVALID_PARAMETER Instance not found.
437 EFI_FVB_ATTRIBUTES_2 Attributes
;
439 EFI_FW_VOL_INSTANCE
*FwhInstance
;
444 // Find the right instance of the FVB private data.
446 FwhInstance
= GetFvbInstance (Instance
);
449 // Check if the FV is write enabled.
451 Attributes
= FvbGetVolumeAttributes (Instance
);
453 if( (Attributes
& EFI_FVB2_WRITE_STATUS
) == 0) {
454 return (EFI_ACCESS_DENIED
);
458 // Get the starting address of the block for erase.
460 Status
= FvbGetLbaAddress (Instance
, Lba
, &LbaAddress
, &LbaLength
, NULL
);
461 if (EFI_ERROR(Status
)) {
465 LibFvbFlashDeviceBlockLock (LbaAddress
, LbaLength
, FALSE
);
467 Status
= LibFvbFlashDeviceBlockErase (LbaAddress
, LbaLength
);
469 LibFvbFlashDeviceBlockLock (LbaAddress
, LbaLength
, TRUE
);
471 WriteBackInvalidateDataCacheRange ((VOID
*) LbaAddress
, LbaLength
);
478 Modifies the current settings of the firmware volume according to the
479 input parameter, and returns the new setting of the volume.
481 @param[in] Instance The FV instance whose attributes is going to be
483 @param[in] Attributes On input, it is a pointer to EFI_FVB_ATTRIBUTES_2
484 containing the desired firmware volume settings.
485 On successful return, it contains the new settings
486 of the firmware volume.
488 @retval EFI_SUCCESS Successfully returns.
489 @retval EFI_ACCESS_DENIED The volume setting is locked and cannot be modified.
490 @retval EFI_INVALID_PARAMETER Instance not found, or The attributes requested are
491 in conflict with the capabilities as declared in the
492 firmware volume header.
497 FvbSetVolumeAttributes (
499 IN OUT EFI_FVB_ATTRIBUTES_2
*Attributes
502 EFI_FW_VOL_INSTANCE
*FwhInstance
= NULL
;
503 EFI_FVB_ATTRIBUTES_2 OldAttributes
= 0;
504 EFI_FVB_ATTRIBUTES_2
*AttribPtr
= NULL
;
505 EFI_FVB_ATTRIBUTES_2 UnchangedAttributes
;
507 UINT32 OldStatus
, NewStatus
;
510 // Find the right instance of the FVB private data.
512 FwhInstance
= GetFvbInstance (Instance
);
514 AttribPtr
= (EFI_FVB_ATTRIBUTES_2
*) & (FwhInstance
->VolumeHeader
.Attributes
);
515 ASSERT_EFI_ERROR (AttribPtr
!= NULL
);
517 if ( AttribPtr
!= NULL
) {
518 OldAttributes
= *AttribPtr
;
521 Capabilities
= OldAttributes
& EFI_FVB2_CAPABILITIES
;
522 OldStatus
= OldAttributes
& EFI_FVB2_STATUS
;
523 NewStatus
= *Attributes
& EFI_FVB2_STATUS
;
525 UnchangedAttributes
= EFI_FVB2_READ_DISABLED_CAP
| \
526 EFI_FVB2_READ_ENABLED_CAP
| \
527 EFI_FVB2_WRITE_DISABLED_CAP
| \
528 EFI_FVB2_WRITE_ENABLED_CAP
| \
529 EFI_FVB2_LOCK_CAP
| \
530 EFI_FVB2_STICKY_WRITE
| \
531 EFI_FVB2_MEMORY_MAPPED
| \
532 EFI_FVB2_ERASE_POLARITY
| \
533 EFI_FVB2_READ_LOCK_CAP
| \
534 EFI_FVB2_WRITE_LOCK_CAP
| \
538 // Some attributes of FV is read only can *not* be set.
540 if ((OldAttributes
& UnchangedAttributes
) ^ (*Attributes
& UnchangedAttributes
)) {
541 return EFI_INVALID_PARAMETER
;
545 // If firmware volume is locked, no status bit can be updated.
547 if ( OldAttributes
& EFI_FVB2_LOCK_STATUS
) {
548 if ( OldStatus
^ NewStatus
) {
549 return EFI_ACCESS_DENIED
;
554 // Test read disable.
556 if ((Capabilities
& EFI_FVB2_READ_DISABLED_CAP
) == 0) {
557 if ((NewStatus
& EFI_FVB2_READ_STATUS
) == 0) {
558 return EFI_INVALID_PARAMETER
;
565 if ((Capabilities
& EFI_FVB2_READ_ENABLED_CAP
) == 0) {
566 if (NewStatus
& EFI_FVB2_READ_STATUS
) {
567 return EFI_INVALID_PARAMETER
;
572 // Test write disable.
574 if ((Capabilities
& EFI_FVB2_WRITE_DISABLED_CAP
) == 0) {
575 if ((NewStatus
& EFI_FVB2_WRITE_STATUS
) == 0) {
576 return EFI_INVALID_PARAMETER
;
581 // Test write enable.
583 if ((Capabilities
& EFI_FVB2_WRITE_ENABLED_CAP
) == 0) {
584 if (NewStatus
& EFI_FVB2_WRITE_STATUS
) {
585 return EFI_INVALID_PARAMETER
;
592 if ((Capabilities
& EFI_FVB2_LOCK_CAP
) == 0) {
593 if (NewStatus
& EFI_FVB2_LOCK_STATUS
) {
594 return EFI_INVALID_PARAMETER
;
598 *AttribPtr
= (*AttribPtr
) & (0xFFFFFFFF & (~EFI_FVB2_STATUS
));
599 *AttribPtr
= (*AttribPtr
) | NewStatus
;
600 *Attributes
= *AttribPtr
;
606 // FVB protocol APIs.
609 Retrieves the physical address of the device.
611 @param[in] This A pointer to EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL.
612 @param[out] Address Output buffer containing the address.
614 retval EFI_SUCCESS The function always return successfully.
619 FvbProtocolGetPhysicalAddress (
620 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
621 OUT EFI_PHYSICAL_ADDRESS
*Address
624 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
625 EFI_FW_VOL_INSTANCE
*FvInstance
;
627 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
628 FvInstance
= GetFvbInstance(FvbDevice
->Instance
);
630 if (FvInstance
!= NULL
) {
631 *Address
= FvInstance
->FvBase
;
639 Retrieve the size of a logical block.
641 @param[in] This Calling context.
642 @param[in] Lba Indicates which block to return the size for.
643 @param[out] BlockSize A pointer to a caller allocated UINTN in which
644 the size of the block is returned.
645 @param[out] NumOfBlocks A pointer to a caller allocated UINTN in which the
646 number of consecutive blocks starting with Lba is
647 returned. All blocks in this range have a size of
650 @retval EFI_SUCCESS The function always return successfully.
655 FvbProtocolGetBlockSize (
656 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
658 OUT UINTN
*BlockSize
,
659 OUT UINTN
*NumOfBlocks
662 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
665 "FvbProtocolGetBlockSize: Lba: 0x%lx BlockSize: 0x%x NumOfBlocks: 0x%x\n",
671 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
673 return FvbGetLbaAddress (
684 Retrieves Volume attributes. No polarity translations are done.
686 @param[in] This Calling context.
687 @param[out] Attributes Output buffer which contains attributes.
689 @retval EFI_SUCCESS The function always return successfully.
694 FvbProtocolGetAttributes (
695 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
696 OUT EFI_FVB_ATTRIBUTES_2
*Attributes
699 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
701 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
703 *Attributes
= FvbGetVolumeAttributes (FvbDevice
->Instance
);
706 "FvbProtocolGetAttributes: This: 0x%x Attributes: 0x%x\n",
716 Sets Volume attributes. No polarity translations are done.
718 @param[in] This Calling context.
719 @param[out] Attributes Output buffer which contains attributes.
721 @retval EFI_SUCCESS The function always return successfully.
726 FvbProtocolSetAttributes (
727 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
728 IN OUT EFI_FVB_ATTRIBUTES_2
*Attributes
732 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
735 "FvbProtocolSetAttributes: Before SET - This: 0x%x Attributes: 0x%x\n",
740 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
742 Status
= FvbSetVolumeAttributes (FvbDevice
->Instance
, Attributes
);
745 "FvbProtocolSetAttributes: After SET - This: 0x%x Attributes: 0x%x\n",
755 The EraseBlock() function erases one or more blocks as denoted by the
756 variable argument list. The entire parameter list of blocks must be verified
757 prior to erasing any blocks. If a block is requested that does not exist
758 within the associated firmware volume (it has a larger index than the last
759 block of the firmware volume), the EraseBlock() function must return
760 EFI_INVALID_PARAMETER without modifying the contents of the firmware volume.
762 @param[in] This Calling context.
763 @param[in] ... Starting LBA followed by Number of Lba to erase.
764 a -1 to terminate the list.
766 @retval EFI_SUCCESS The erase request was successfully completed.
767 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
768 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
769 could not be written. Firmware device may have been
775 FvbProtocolEraseBlocks (
776 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
780 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
781 EFI_FW_VOL_INSTANCE
*FwhInstance
;
782 UINTN NumOfBlocks
= 0;
788 DEBUG((EFI_D_INFO
, "FvbProtocolEraseBlocks: \n"));
789 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
791 FwhInstance
= GetFvbInstance (FvbDevice
->Instance
);
793 if (FwhInstance
!= NULL
) {
794 NumOfBlocks
= FwhInstance
->NumOfBlocks
;
797 VA_START (args
, This
);
800 StartingLba
= VA_ARG (args
, EFI_LBA
);
801 if ( StartingLba
== EFI_LBA_LIST_TERMINATOR
) {
805 NumOfLba
= VA_ARG (args
, UINT32
);
808 // Check input parameters.
812 return EFI_INVALID_PARAMETER
;
815 if ( ( StartingLba
+ NumOfLba
) > NumOfBlocks
) {
816 return EFI_INVALID_PARAMETER
;
822 VA_START (args
, This
);
824 StartingLba
= VA_ARG (args
, EFI_LBA
);
825 if (StartingLba
== EFI_LBA_LIST_TERMINATOR
) {
829 NumOfLba
= VA_ARG (args
, UINT32
);
831 while ( NumOfLba
> 0 ) {
832 Status
= FvbEraseBlock (FvbDevice
->Instance
, StartingLba
);
833 if ( EFI_ERROR(Status
)) {
850 Writes data beginning at Lba:Offset from FV. The write terminates either
851 when *NumBytes of data have been written, or when a block boundary is
852 reached. *NumBytes is updated to reflect the actual number of bytes
853 written. The write opertion does not include erase. This routine will
854 attempt to write only the specified bytes. If the writes do not stick,
855 it will return an error.
857 @param[in] This Calling context.
858 @param[in] Lba Block in which to begin write.
859 @param[in] Offset Offset in the block at which to begin write.
860 @param[in,out] NumBytes On input, indicates the requested write size. On
861 output, indicates the actual number of bytes written
862 @param[in] Buffer Buffer containing source data for the write.
864 @retval EFI_SUCCESS The firmware volume was written successfully.
865 @retval EFI_BAD_BUFFER_SIZE Write attempted across a LBA boundary. On output,
866 NumBytes contains the total number of bytes
868 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
869 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
870 could not be written.
871 @retval EFI_INVALID_PARAMETER NumBytes or Buffer are NULL.
877 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
880 IN OUT UINTN
*NumBytes
,
885 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
887 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
890 "FvbProtocolWrite: Lba: 0x%lx Offset: 0x%x NumBytes: 0x%x, Buffer: 0x%x\n",
897 return FvbWriteBlock (FvbDevice
->Instance
, Lba
, Offset
, NumBytes
, Buffer
);
902 Reads data beginning at Lba:Offset from FV. The Read terminates either
903 when *NumBytes of data have been read, or when a block boundary is
904 reached. *NumBytes is updated to reflect the actual number of bytes
905 written. The write opertion does not include erase. This routine will
906 attempt to write only the specified bytes. If the writes do not stick,
907 it will return an error.
909 @param[in] This Calling context.
910 @param[in] Lba Block in which to begin write.
911 @param[in] Offset Offset in the block at which to begin write
912 @param[in,out] NumBytes On input, indicates the requested write size. On
913 output, indicates the actual number of bytes written.
914 @param[in] Buffer Buffer containing source data for the write.
918 @retval EFI_SUCCESS The firmware volume was read successfully and
919 contents are in Buffer.
920 @retval EFI_BAD_BUFFER_SIZE Read attempted across a LBA boundary. On output,
921 NumBytes contains the total number of bytes returned
923 @retval EFI_ACCESS_DENIED The firmware volume is in the ReadDisabled state
924 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
926 @retval EFI_INVALID_PARAMETER NumBytes or Buffer are NULL.
932 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
935 IN OUT UINTN
*NumBytes
,
940 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
943 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
944 Status
= FvbReadBlock (FvbDevice
->Instance
, Lba
, Offset
, NumBytes
, Buffer
);
946 "FvbProtocolRead: Lba: 0x%lx Offset: 0x%x NumBytes: 0x%x, Buffer: 0x%x\n",
958 Check the integrity of firmware volume header.
960 @param[in] FwVolHeader A pointer to a firmware volume header.
962 @retval TRUE The firmware volume is consistent.
963 @retval FALSE The firmware volume has corrupted.
968 IN EFI_PHYSICAL_ADDRESS FvBase
,
969 IN CONST EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
972 if (FvBase
== PcdGet32(PcdFlashNvStorageVariableBase
)) {
973 if (CompareMem (&FwVolHeader
->FileSystemGuid
, &gEfiSystemNvDataFvGuid
, sizeof(EFI_GUID
)) != 0 ) {
977 if (CompareMem (&FwVolHeader
->FileSystemGuid
, &gEfiFirmwareFileSystem2Guid
, sizeof(EFI_GUID
)) != 0 ) {
981 if ( (FwVolHeader
->Revision
!= EFI_FVH_REVISION
) ||
982 (FwVolHeader
->Signature
!= EFI_FVH_SIGNATURE
) ||
983 (FwVolHeader
->FvLength
== ((UINTN
) -1)) ||
984 ((FwVolHeader
->HeaderLength
& 0x01 ) !=0) ) {
988 if (CalculateCheckSum16 ((UINT16
*) FwVolHeader
, FwVolHeader
->HeaderLength
) != 0) {
997 The function does the necessary initialization work for
998 Firmware Volume Block Driver.
1000 @retval EFI_SUCCESS This funtion always return EFI_SUCCESS.
1001 It will ASSERT on errors.
1009 EFI_FW_VOL_INSTANCE
*FwhInstance
;
1010 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
1011 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
1012 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
1013 EFI_PHYSICAL_ADDRESS BaseAddress
;
1016 UINTN TmpHeaderLength
;
1019 BOOLEAN FvHeaderValid
;
1022 // Calculate the total size for all firmware volume block instances.
1025 for (Idx
= 0; Idx
< 1; Idx
++) {
1026 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) mPlatformFvBaseAddress
[Idx
];
1027 BufferSize
+= (FvHeader
->HeaderLength
+
1028 sizeof (EFI_FW_VOL_INSTANCE
) -
1029 sizeof (EFI_FIRMWARE_VOLUME_HEADER
)
1033 mFvbModuleGlobal
.FvInstance
= (EFI_FW_VOL_INSTANCE
*) AllocateRuntimeZeroPool (BufferSize
);
1034 ASSERT (NULL
!= mFvbModuleGlobal
.FvInstance
);
1038 FwhInstance
= mFvbModuleGlobal
.FvInstance
;
1039 mFvbModuleGlobal
.NumFv
= 0;
1041 for (Idx
= 0; Idx
< 1; Idx
++) {
1042 BaseAddress
= mPlatformFvBaseAddress
[Idx
];
1043 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) BaseAddress
;
1045 if (!IsFvHeaderValid (BaseAddress
, FwVolHeader
)) {
1046 FvHeaderValid
= FALSE
;
1048 // If not valid, get FvbInfo from the information carried in
1051 DEBUG ((EFI_D_ERROR
, "Fvb: FV header @ 0x%lx invalid\n", BaseAddress
));
1052 Status
= GetFvbInfo (BaseAddress
, &FwVolHeader
);
1053 ASSERT_EFI_ERROR(Status
);
1055 // Write back a healthy FV header.
1057 DEBUG ((EFI_D_ERROR
, "FwBlockService.c: Writing back healthy FV header\n"));
1058 LibFvbFlashDeviceBlockLock ((UINTN
)BaseAddress
, FwVolHeader
->BlockMap
->Length
, FALSE
);
1060 Status
= LibFvbFlashDeviceBlockErase ((UINTN
)BaseAddress
, FwVolHeader
->BlockMap
->Length
);
1062 TmpHeaderLength
= (UINTN
) FwVolHeader
->HeaderLength
;
1063 Status
= LibFvbFlashDeviceWrite (
1066 (UINT8
*) FwVolHeader
1069 LibFvbFlashDeviceBlockLock ((UINTN
)BaseAddress
, FwVolHeader
->BlockMap
->Length
, TRUE
);
1071 WriteBackInvalidateDataCacheRange (
1072 (VOID
*) (UINTN
) BaseAddress
,
1073 FwVolHeader
->BlockMap
->Length
1078 CopyMem (&(FwhInstance
->VolumeHeader
), FwVolHeader
, FwVolHeader
->HeaderLength
);
1080 FwVolHeader
= &(FwhInstance
->VolumeHeader
);
1081 FwhInstance
->FvBase
= (UINTN
)BaseAddress
;
1084 // Process the block map for each FV.
1086 FwhInstance
->NumOfBlocks
= 0;
1087 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
1089 // Get the maximum size of a block.
1091 if (MaxLbaSize
< PtrBlockMapEntry
->Length
) {
1092 MaxLbaSize
= PtrBlockMapEntry
->Length
;
1094 FwhInstance
->NumOfBlocks
+= PtrBlockMapEntry
->NumBlocks
;
1098 // Add a FVB Protocol Instance.
1100 mFvbModuleGlobal
.NumFv
++;
1101 InstallFvbProtocol (FwhInstance
, mFvbModuleGlobal
.NumFv
- 1);
1104 // Move on to the next FwhInstance.
1106 FwhInstance
= (EFI_FW_VOL_INSTANCE
*) ((UINTN
)((UINT8
*)FwhInstance
) +
1107 FwVolHeader
->HeaderLength
+
1108 (sizeof (EFI_FW_VOL_INSTANCE
) - sizeof (EFI_FIRMWARE_VOLUME_HEADER
)));