3 Copyright (c) 2006 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
23 // The package level header files this module uses
28 // The protocols, PPI and GUID defintions for this module
30 #include <Guid/EventGroup.h>
31 #include <Protocol/FvbExtension.h>
32 #include <Protocol/FirmwareVolumeBlock.h>
33 #include <Guid/AlternateFvBlock.h>
34 #include <Protocol/DevicePath.h>
36 // The Library classes this module consumes
38 #include <Library/UefiLib.h>
39 #include <Library/UefiDriverEntryPoint.h>
40 #include <Library/BaseLib.h>
41 #include <Library/DxeServicesTableLib.h>
42 #include <Library/UefiRuntimeLib.h>
43 #include <Library/DebugLib.h>
44 #include <Library/HobLib.h>
45 #include <Library/BaseMemoryLib.h>
46 #include <Library/MemoryAllocationLib.h>
47 #include <Library/UefiBootServicesTableLib.h>
49 #include "FWBlockService.h"
51 #define EFI_FVB2_STATUS (EFI_FVB2_READ_STATUS | EFI_FVB2_WRITE_STATUS | EFI_FVB2_LOCK_STATUS)
53 ESAL_FWB_GLOBAL
*mFvbModuleGlobal
;
55 EFI_FW_VOL_BLOCK_DEVICE mFvbDeviceTemplate
= {
63 sizeof (MEMMAP_DEVICE_PATH
),
73 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
75 sizeof (EFI_DEVICE_PATH_PROTOCOL
),
82 FvbProtocolGetAttributes
,
83 FvbProtocolSetAttributes
,
84 FvbProtocolGetPhysicalAddress
,
85 FvbProtocolGetBlockSize
,
88 FvbProtocolEraseBlocks
,
92 FvbExtendProtocolEraseCustomBlockRange
100 FvbVirtualddressChangeEvent (
108 Fixup internal data so that EFI and SAL can be call in virtual mode.
109 Call the passed in Child Notify event and convert the mFvbModuleGlobal
110 date items to there virtual address.
112 mFvbModuleGlobal->FvInstance[FVB_PHYSICAL] - Physical copy of instance data
113 mFvbModuleGlobal->FvInstance[FVB_VIRTUAL] - Virtual pointer to common
118 (Standard EFI notify event - EFI_EVENT_NOTIFY)
126 EFI_FW_VOL_INSTANCE
*FwhInstance
;
129 EfiConvertPointer (0x0, (VOID
**) &mFvbModuleGlobal
->FvInstance
[FVB_VIRTUAL
]);
132 // Convert the base address of all the instances
135 FwhInstance
= mFvbModuleGlobal
->FvInstance
[FVB_PHYSICAL
];
136 while (Index
< mFvbModuleGlobal
->NumFv
) {
137 EfiConvertPointer (0x0, (VOID
**) &FwhInstance
->FvBase
[FVB_VIRTUAL
]);
138 FwhInstance
= (EFI_FW_VOL_INSTANCE
*)
140 (UINTN
) ((UINT8
*) FwhInstance
) + FwhInstance
->VolumeHeader
.HeaderLength
+
141 (sizeof (EFI_FW_VOL_INSTANCE
) - sizeof (EFI_FIRMWARE_VOLUME_HEADER
))
146 EfiConvertPointer (0x0, (VOID
**) &mFvbModuleGlobal
->FvbScratchSpace
[FVB_VIRTUAL
]);
147 EfiConvertPointer (0x0, (VOID
**) &mFvbModuleGlobal
);
153 IN ESAL_FWB_GLOBAL
*Global
,
154 OUT EFI_FW_VOL_INSTANCE
**FwhInstance
,
160 Retrieves the physical address of a memory mapped FV
163 Instance - The FV instance whose base address is going to be
165 Global - Pointer to ESAL_FWB_GLOBAL that contains all
167 FwhInstance - The EFI_FW_VOL_INSTANCE fimrware instance structure
168 Virtual - Whether CPU is in virtual or physical mode
171 EFI_SUCCESS - Successfully returns
172 EFI_INVALID_PARAMETER - Instance not found
176 EFI_FW_VOL_INSTANCE
*FwhRecord
;
178 if (Instance
>= Global
->NumFv
) {
179 return EFI_INVALID_PARAMETER
;
182 // Find the right instance of the FVB private data
184 FwhRecord
= Global
->FvInstance
[Virtual
];
185 while (Instance
> 0) {
186 FwhRecord
= (EFI_FW_VOL_INSTANCE
*)
188 (UINTN
) ((UINT8
*) FwhRecord
) + FwhRecord
->VolumeHeader
.HeaderLength
+
189 (sizeof (EFI_FW_VOL_INSTANCE
) - sizeof (EFI_FIRMWARE_VOLUME_HEADER
))
194 *FwhInstance
= FwhRecord
;
200 FvbGetPhysicalAddress (
202 OUT EFI_PHYSICAL_ADDRESS
*Address
,
203 IN ESAL_FWB_GLOBAL
*Global
,
209 Retrieves the physical address of a memory mapped FV
212 Instance - The FV instance whose base address is going to be
214 Address - Pointer to a caller allocated EFI_PHYSICAL_ADDRESS
215 that on successful return, contains the base address
216 of the firmware volume.
217 Global - Pointer to ESAL_FWB_GLOBAL that contains all
219 Virtual - Whether CPU is in virtual or physical mode
222 EFI_SUCCESS - Successfully returns
223 EFI_INVALID_PARAMETER - Instance not found
227 EFI_FW_VOL_INSTANCE
*FwhInstance
;
231 // Find the right instance of the FVB private data
233 Status
= GetFvbInstance (Instance
, Global
, &FwhInstance
, Virtual
);
234 ASSERT_EFI_ERROR (Status
);
235 *Address
= FwhInstance
->FvBase
[Virtual
];
241 FvbGetVolumeAttributes (
243 OUT EFI_FVB_ATTRIBUTES
*Attributes
,
244 IN ESAL_FWB_GLOBAL
*Global
,
250 Retrieves attributes, insures positive polarity of attribute bits, returns
251 resulting attributes in output parameter
254 Instance - The FV instance whose attributes is going to be
256 Attributes - Output buffer which contains attributes
257 Global - Pointer to ESAL_FWB_GLOBAL that contains all
259 Virtual - Whether CPU is in virtual or physical mode
262 EFI_SUCCESS - Successfully returns
263 EFI_INVALID_PARAMETER - Instance not found
267 EFI_FW_VOL_INSTANCE
*FwhInstance
;
271 // Find the right instance of the FVB private data
273 Status
= GetFvbInstance (Instance
, Global
, &FwhInstance
, Virtual
);
274 ASSERT_EFI_ERROR (Status
);
275 *Attributes
= FwhInstance
->VolumeHeader
.Attributes
;
284 OUT UINTN
*LbaAddress
,
285 OUT UINTN
*LbaLength
,
286 OUT UINTN
*NumOfBlocks
,
287 IN ESAL_FWB_GLOBAL
*Global
,
293 Retrieves the starting address of an LBA in an FV
296 Instance - The FV instance which the Lba belongs to
297 Lba - The logical block address
298 LbaAddress - On output, contains the physical starting address
300 LbaLength - On output, contains the length of the block
301 NumOfBlocks - A pointer to a caller allocated UINTN in which the
302 number of consecutive blocks starting with Lba is
303 returned. All blocks in this range have a size of
305 Global - Pointer to ESAL_FWB_GLOBAL that contains all
307 Virtual - Whether CPU is in virtual or physical mode
310 EFI_SUCCESS - Successfully returns
311 EFI_INVALID_PARAMETER - Instance not found
320 EFI_FW_VOL_INSTANCE
*FwhInstance
;
321 EFI_FV_BLOCK_MAP_ENTRY
*BlockMap
;
325 // Find the right instance of the FVB private data
327 Status
= GetFvbInstance (Instance
, Global
, &FwhInstance
, Virtual
);
328 ASSERT_EFI_ERROR (Status
);
332 BlockMap
= &(FwhInstance
->VolumeHeader
.BlockMap
[0]);
335 // Parse the blockmap of the FV to find which map entry the Lba belongs to
338 NumBlocks
= BlockMap
->NumBlocks
;
339 BlockLength
= BlockMap
->Length
;
341 if (NumBlocks
== 0 || BlockLength
== 0) {
342 return EFI_INVALID_PARAMETER
;
345 NextLba
= StartLba
+ NumBlocks
;
348 // The map entry found
350 if (Lba
>= StartLba
&& Lba
< NextLba
) {
351 Offset
= Offset
+ (UINTN
) MultU64x32 ((Lba
- StartLba
), BlockLength
);
352 if (LbaAddress
!= NULL
) {
353 *LbaAddress
= FwhInstance
->FvBase
[Virtual
] + Offset
;
356 if (LbaLength
!= NULL
) {
357 *LbaLength
= BlockLength
;
360 if (NumOfBlocks
!= NULL
) {
361 *NumOfBlocks
= (UINTN
) (NextLba
- Lba
);
368 Offset
= Offset
+ NumBlocks
* BlockLength
;
377 IN UINTN BlockOffset
,
378 IN OUT UINTN
*NumBytes
,
380 IN ESAL_FWB_GLOBAL
*Global
,
386 Reads specified number of bytes into a buffer from the specified block
389 Instance - The FV instance to be read from
390 Lba - The logical block address to be read from
391 BlockOffset - Offset into the block at which to begin reading
392 NumBytes - Pointer that on input contains the total size of
393 the buffer. On output, it contains the total number
395 Buffer - Pointer to a caller allocated buffer that will be
396 used to hold the data read
397 Global - Pointer to ESAL_FWB_GLOBAL that contains all
399 Virtual - Whether CPU is in virtual or physical mode
402 EFI_SUCCESS - The firmware volume was read successfully and
403 contents are in Buffer
404 EFI_BAD_BUFFER_SIZE - Read attempted across a LBA boundary. On output,
405 NumBytes contains the total number of bytes returned
407 EFI_ACCESS_DENIED - The firmware volume is in the ReadDisabled state
408 EFI_DEVICE_ERROR - The block device is not functioning correctly and
410 EFI_INVALID_PARAMETER - Instance not found, or NumBytes, Buffer are NULL
414 EFI_FVB_ATTRIBUTES Attributes
;
420 // Check for invalid conditions
422 if ((NumBytes
== NULL
) || (Buffer
== NULL
)) {
423 return EFI_INVALID_PARAMETER
;
426 if (*NumBytes
== 0) {
427 return EFI_INVALID_PARAMETER
;
430 Status
= FvbGetLbaAddress (Instance
, Lba
, &LbaAddress
, &LbaLength
, NULL
, Global
, Virtual
);
431 if (EFI_ERROR (Status
)) {
435 // Check if the FV is read enabled
437 FvbGetVolumeAttributes (Instance
, &Attributes
, Global
, Virtual
);
439 if ((Attributes
& EFI_FVB2_READ_STATUS
) == 0) {
440 return EFI_ACCESS_DENIED
;
443 // Perform boundary checks and adjust NumBytes
445 if (BlockOffset
> LbaLength
) {
446 return EFI_INVALID_PARAMETER
;
449 if (LbaLength
< (*NumBytes
+ BlockOffset
)) {
450 *NumBytes
= (UINT32
) (LbaLength
- BlockOffset
);
451 Status
= EFI_BAD_BUFFER_SIZE
;
454 CopyMem (Buffer
, (UINT8
*) (LbaAddress
+ BlockOffset
), (UINTN
) (*NumBytes
));
463 IN UINTN BlockOffset
,
464 IN OUT UINTN
*NumBytes
,
466 IN ESAL_FWB_GLOBAL
*Global
,
472 Writes specified number of bytes from the input buffer to the block
475 Instance - The FV instance to be written to
476 Lba - The starting logical block index to write to
477 BlockOffset - Offset into the block at which to begin writing
478 NumBytes - Pointer that on input contains the total size of
479 the buffer. On output, it contains the total number
480 of bytes actually written
481 Buffer - Pointer to a caller allocated buffer that contains
482 the source for the write
483 Global - Pointer to ESAL_FWB_GLOBAL that contains all
485 Virtual - Whether CPU is in virtual or physical mode
488 EFI_SUCCESS - The firmware volume was written successfully
489 EFI_BAD_BUFFER_SIZE - Write attempted across a LBA boundary. On output,
490 NumBytes contains the total number of bytes
492 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
493 EFI_DEVICE_ERROR - The block device is not functioning correctly and
495 EFI_INVALID_PARAMETER - Instance not found, or NumBytes, Buffer are NULL
499 EFI_FVB_ATTRIBUTES Attributes
;
505 // Check for invalid conditions
507 if ((NumBytes
== NULL
) || (Buffer
== NULL
)) {
508 return EFI_INVALID_PARAMETER
;
511 if (*NumBytes
== 0) {
512 return EFI_INVALID_PARAMETER
;
515 Status
= FvbGetLbaAddress (Instance
, Lba
, &LbaAddress
, &LbaLength
, NULL
, Global
, Virtual
);
516 if (EFI_ERROR (Status
)) {
520 // Check if the FV is write enabled
522 FvbGetVolumeAttributes (Instance
, &Attributes
, Global
, Virtual
);
524 if ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0) {
525 return EFI_ACCESS_DENIED
;
528 // Perform boundary checks and adjust NumBytes
530 if (BlockOffset
> LbaLength
) {
531 return EFI_INVALID_PARAMETER
;
534 if (LbaLength
< (*NumBytes
+ BlockOffset
)) {
535 *NumBytes
= (UINT32
) (LbaLength
- BlockOffset
);
536 Status
= EFI_BAD_BUFFER_SIZE
;
541 CopyMem ((UINT8
*) (LbaAddress
+ BlockOffset
), Buffer
, (UINTN
) (*NumBytes
));
550 IN ESAL_FWB_GLOBAL
*Global
,
556 Erases and initializes a firmware volume block
559 Instance - The FV instance to be erased
560 Lba - The logical block index to be erased
561 Global - Pointer to ESAL_FWB_GLOBAL that contains all
563 Virtual - Whether CPU is in virtual or physical mode
566 EFI_SUCCESS - The erase request was successfully completed
567 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
568 EFI_DEVICE_ERROR - The block device is not functioning correctly and
569 could not be written. Firmware device may have been
571 EFI_INVALID_PARAMETER - Instance not found
576 EFI_FVB_ATTRIBUTES Attributes
;
583 // Check if the FV is write enabled
585 FvbGetVolumeAttributes (Instance
, &Attributes
, Global
, Virtual
);
587 if ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0) {
588 return EFI_ACCESS_DENIED
;
591 // Get the starting address of the block for erase.
593 Status
= FvbGetLbaAddress (Instance
, Lba
, &LbaAddress
, &LbaLength
, NULL
, Global
, Virtual
);
595 if (EFI_ERROR (Status
)) {
599 if ((Attributes
& EFI_FVB2_ERASE_POLARITY
) != 0) {
605 SetMem ((UINT8
*) LbaAddress
, LbaLength
, Data
);
611 FvbEraseCustomBlockRange (
614 IN UINTN OffsetStartLba
,
616 IN UINTN OffsetLastLba
,
617 IN ESAL_FWB_GLOBAL
*Global
,
623 Erases and initializes a specified range of a firmware volume
626 Instance - The FV instance to be erased
627 StartLba - The starting logical block index to be erased
628 OffsetStartLba - Offset into the starting block at which to
630 LastLba - The last logical block index to be erased
631 OffsetStartLba - Offset into the last block at which to end erasing
632 Global - Pointer to ESAL_FWB_GLOBAL that contains all
634 Virtual - Whether CPU is in virtual or physical mode
637 EFI_SUCCESS - The firmware volume was erased successfully
638 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
639 EFI_DEVICE_ERROR - The block device is not functioning correctly and
640 could not be written. Firmware device may have been
642 EFI_INVALID_PARAMETER - Instance not found
648 UINTN ScratchLbaSizeData
;
653 FvbGetLbaAddress (Instance
, StartLba
, NULL
, &LbaSize
, NULL
, Global
, Virtual
);
656 // Use the scratch space as the intermediate buffer to transfer data
657 // Back up the first LBA in scratch space.
659 FvbReadBlock (Instance
, StartLba
, 0, &LbaSize
, Global
->FvbScratchSpace
[Virtual
], Global
, Virtual
);
664 FvbEraseBlock (Instance
, StartLba
, Global
, Virtual
);
665 ScratchLbaSizeData
= OffsetStartLba
;
668 // write the data back to the first block
670 if (ScratchLbaSizeData
> 0) {
671 FvbWriteBlock (Instance
, StartLba
, 0, &ScratchLbaSizeData
, Global
->FvbScratchSpace
[Virtual
], Global
, Virtual
);
676 if (LastLba
> (StartLba
+ 1)) {
677 for (Index
= (StartLba
+ 1); Index
<= (LastLba
- 1); Index
++) {
678 FvbEraseBlock (Instance
, Index
, Global
, Virtual
);
682 // Last LBAs, the same as first LBAs
684 if (LastLba
> StartLba
) {
685 FvbGetLbaAddress (Instance
, LastLba
, NULL
, &LbaSize
, NULL
, Global
, Virtual
);
686 FvbReadBlock (Instance
, LastLba
, 0, &LbaSize
, Global
->FvbScratchSpace
[Virtual
], Global
, Virtual
);
687 FvbEraseBlock (Instance
, LastLba
, Global
, Virtual
);
690 ScratchLbaSizeData
= LbaSize
- (OffsetStartLba
+ 1);
692 return FvbWriteBlock (
697 Global
->FvbScratchSpace
[Virtual
],
704 FvbSetVolumeAttributes (
706 IN OUT EFI_FVB_ATTRIBUTES
*Attributes
,
707 IN ESAL_FWB_GLOBAL
*Global
,
713 Modifies the current settings of the firmware volume according to the
714 input parameter, and returns the new setting of the volume
717 Instance - The FV instance whose attributes is going to be
719 Attributes - On input, it is a pointer to EFI_FVB_ATTRIBUTES
720 containing the desired firmware volume settings.
721 On successful return, it contains the new settings
722 of the firmware volume
723 Global - Pointer to ESAL_FWB_GLOBAL that contains all
725 Virtual - Whether CPU is in virtual or physical mode
728 EFI_SUCCESS - Successfully returns
729 EFI_ACCESS_DENIED - The volume setting is locked and cannot be modified
730 EFI_INVALID_PARAMETER - Instance not found, or The attributes requested are
731 in conflict with the capabilities as declared in the
732 firmware volume header
736 EFI_FW_VOL_INSTANCE
*FwhInstance
;
737 EFI_FVB_ATTRIBUTES OldAttributes
;
738 EFI_FVB_ATTRIBUTES
*AttribPtr
;
745 // Find the right instance of the FVB private data
747 Status
= GetFvbInstance (Instance
, Global
, &FwhInstance
, Virtual
);
748 ASSERT_EFI_ERROR (Status
);
750 AttribPtr
= (EFI_FVB_ATTRIBUTES
*) &(FwhInstance
->VolumeHeader
.Attributes
);
751 OldAttributes
= *AttribPtr
;
752 Capabilities
= OldAttributes
& (EFI_FVB2_READ_DISABLED_CAP
| \
753 EFI_FVB2_READ_ENABLED_CAP
| \
754 EFI_FVB2_WRITE_DISABLED_CAP
| \
755 EFI_FVB2_WRITE_ENABLED_CAP
| \
758 OldStatus
= OldAttributes
& EFI_FVB2_STATUS
;
759 NewStatus
= *Attributes
& EFI_FVB2_STATUS
;
762 // If firmware volume is locked, no status bit can be updated
764 if (OldAttributes
& EFI_FVB2_LOCK_STATUS
) {
765 if (OldStatus
^ NewStatus
) {
766 return EFI_ACCESS_DENIED
;
772 if ((Capabilities
& EFI_FVB2_READ_DISABLED_CAP
) == 0) {
773 if ((NewStatus
& EFI_FVB2_READ_STATUS
) == 0) {
774 return EFI_INVALID_PARAMETER
;
780 if ((Capabilities
& EFI_FVB2_READ_ENABLED_CAP
) == 0) {
781 if (NewStatus
& EFI_FVB2_READ_STATUS
) {
782 return EFI_INVALID_PARAMETER
;
786 // Test write disable
788 if ((Capabilities
& EFI_FVB2_WRITE_DISABLED_CAP
) == 0) {
789 if ((NewStatus
& EFI_FVB2_WRITE_STATUS
) == 0) {
790 return EFI_INVALID_PARAMETER
;
796 if ((Capabilities
& EFI_FVB2_WRITE_ENABLED_CAP
) == 0) {
797 if (NewStatus
& EFI_FVB2_WRITE_STATUS
) {
798 return EFI_INVALID_PARAMETER
;
804 if ((Capabilities
& EFI_FVB2_LOCK_CAP
) == 0) {
805 if (NewStatus
& EFI_FVB2_LOCK_STATUS
) {
806 return EFI_INVALID_PARAMETER
;
810 *AttribPtr
= (*AttribPtr
) & (0xFFFFFFFF & (~EFI_FVB2_STATUS
));
811 *AttribPtr
= (*AttribPtr
) | NewStatus
;
812 *Attributes
= *AttribPtr
;
821 FvbProtocolGetPhysicalAddress (
822 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
823 OUT EFI_PHYSICAL_ADDRESS
*Address
829 Retrieves the physical address of the device.
833 This - Calling context
834 Address - Output buffer containing the address.
839 EFI_SUCCESS - Successfully returns
843 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
845 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
847 return FvbGetPhysicalAddress (FvbDevice
->Instance
, Address
, mFvbModuleGlobal
, EfiGoneVirtual ());
852 FvbProtocolGetBlockSize (
853 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
854 IN CONST EFI_LBA Lba
,
855 OUT UINTN
*BlockSize
,
856 OUT UINTN
*NumOfBlocks
861 Retrieve the size of a logical block
864 This - Calling context
865 Lba - Indicates which block to return the size for.
866 BlockSize - A pointer to a caller allocated UINTN in which
867 the size of the block is returned
868 NumOfBlocks - a pointer to a caller allocated UINTN in which the
869 number of consecutive blocks starting with Lba is
870 returned. All blocks in this range have a size of
874 EFI_SUCCESS - The firmware volume was read successfully and
875 contents are in Buffer
879 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
881 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
883 return FvbGetLbaAddress (
896 FvbProtocolGetAttributes (
897 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
898 OUT EFI_FVB_ATTRIBUTES
*Attributes
903 Retrieves Volume attributes. No polarity translations are done.
906 This - Calling context
907 Attributes - output buffer which contains attributes
910 EFI_SUCCESS - Successfully returns
914 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
916 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
918 return FvbGetVolumeAttributes (FvbDevice
->Instance
, Attributes
, mFvbModuleGlobal
, EfiGoneVirtual ());
923 FvbProtocolSetAttributes (
924 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
925 IN OUT EFI_FVB_ATTRIBUTES
*Attributes
930 Sets Volume attributes. No polarity translations are done.
933 This - Calling context
934 Attributes - output buffer which contains attributes
937 EFI_SUCCESS - Successfully returns
941 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
943 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
945 return FvbSetVolumeAttributes (FvbDevice
->Instance
, Attributes
, mFvbModuleGlobal
, EfiGoneVirtual ());
950 FvbProtocolEraseBlocks (
951 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
958 The EraseBlock() function erases one or more blocks as denoted by the
959 variable argument list. The entire parameter list of blocks must be verified
960 prior to erasing any blocks. If a block is requested that does not exist
961 within the associated firmware volume (it has a larger index than the last
962 block of the firmware volume), the EraseBlock() function must return
963 EFI_INVALID_PARAMETER without modifying the contents of the firmware volume.
966 This - Calling context
967 ... - Starting LBA followed by Number of Lba to erase.
968 a -1 to terminate the list.
971 EFI_SUCCESS - The erase request was successfully completed
972 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
973 EFI_DEVICE_ERROR - The block device is not functioning correctly and
974 could not be written. Firmware device may have been
979 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
980 EFI_FW_VOL_INSTANCE
*FwhInstance
;
987 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
989 Status
= GetFvbInstance (FvbDevice
->Instance
, mFvbModuleGlobal
, &FwhInstance
, EfiGoneVirtual ());
990 ASSERT_EFI_ERROR (Status
);
992 NumOfBlocks
= FwhInstance
->NumOfBlocks
;
994 VA_START (args
, This
);
997 StartingLba
= VA_ARG (args
, EFI_LBA
);
998 if (StartingLba
== EFI_LBA_LIST_TERMINATOR
) {
1002 NumOfLba
= VA_ARG (args
, UINT32
);
1005 // Check input parameters
1007 if (NumOfLba
== 0) {
1009 return EFI_INVALID_PARAMETER
;
1012 if ((StartingLba
+ NumOfLba
) > NumOfBlocks
) {
1013 return EFI_INVALID_PARAMETER
;
1019 VA_START (args
, This
);
1021 StartingLba
= VA_ARG (args
, EFI_LBA
);
1022 if (StartingLba
== EFI_LBA_LIST_TERMINATOR
) {
1026 NumOfLba
= VA_ARG (args
, UINT32
);
1028 while (NumOfLba
> 0) {
1029 Status
= FvbEraseBlock (FvbDevice
->Instance
, StartingLba
, mFvbModuleGlobal
, EfiGoneVirtual ());
1030 if (EFI_ERROR (Status
)) {
1049 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
1050 IN CONST EFI_LBA Lba
,
1051 IN CONST UINTN Offset
,
1052 IN OUT UINTN
*NumBytes
,
1053 IN CONST UINT8
*Buffer
1057 Routine Description:
1059 Writes data beginning at Lba:Offset from FV. The write terminates either
1060 when *NumBytes of data have been written, or when a block boundary is
1061 reached. *NumBytes is updated to reflect the actual number of bytes
1062 written. The write opertion does not include erase. This routine will
1063 attempt to write only the specified bytes. If the writes do not stick,
1064 it will return an error.
1067 This - Calling context
1068 Lba - Block in which to begin write
1069 Offset - Offset in the block at which to begin write
1070 NumBytes - On input, indicates the requested write size. On
1071 output, indicates the actual number of bytes written
1072 Buffer - Buffer containing source data for the write.
1075 EFI_SUCCESS - The firmware volume was written successfully
1076 EFI_BAD_BUFFER_SIZE - Write attempted across a LBA boundary. On output,
1077 NumBytes contains the total number of bytes
1079 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
1080 EFI_DEVICE_ERROR - The block device is not functioning correctly and
1081 could not be written
1082 EFI_INVALID_PARAMETER - NumBytes or Buffer are NULL
1087 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
1089 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
1091 return FvbWriteBlock (FvbDevice
->Instance
, Lba
, Offset
, NumBytes
, Buffer
, mFvbModuleGlobal
, EfiGoneVirtual ());
1097 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*This
,
1098 IN CONST EFI_LBA Lba
,
1099 IN CONST UINTN Offset
,
1100 IN OUT UINTN
*NumBytes
,
1105 Routine Description:
1107 Reads data beginning at Lba:Offset from FV. The Read terminates either
1108 when *NumBytes of data have been read, or when a block boundary is
1109 reached. *NumBytes is updated to reflect the actual number of bytes
1110 written. The write opertion does not include erase. This routine will
1111 attempt to write only the specified bytes. If the writes do not stick,
1112 it will return an error.
1115 This - Calling context
1116 Lba - Block in which to begin Read
1117 Offset - Offset in the block at which to begin Read
1118 NumBytes - On input, indicates the requested write size. On
1119 output, indicates the actual number of bytes Read
1120 Buffer - Buffer containing source data for the Read.
1123 EFI_SUCCESS - The firmware volume was read successfully and
1124 contents are in Buffer
1125 EFI_BAD_BUFFER_SIZE - Read attempted across a LBA boundary. On output,
1126 NumBytes contains the total number of bytes returned
1128 EFI_ACCESS_DENIED - The firmware volume is in the ReadDisabled state
1129 EFI_DEVICE_ERROR - The block device is not functioning correctly and
1131 EFI_INVALID_PARAMETER - NumBytes or Buffer are NULL
1136 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
1138 FvbDevice
= FVB_DEVICE_FROM_THIS (This
);
1140 return FvbReadBlock (FvbDevice
->Instance
, Lba
, Offset
, NumBytes
, Buffer
, mFvbModuleGlobal
, EfiGoneVirtual ());
1143 // FVB Extension Protocols
1147 FvbExtendProtocolEraseCustomBlockRange (
1148 IN EFI_FVB_EXTENSION_PROTOCOL
*This
,
1149 IN EFI_LBA StartLba
,
1150 IN UINTN OffsetStartLba
,
1152 IN UINTN OffsetLastLba
1156 Routine Description:
1157 Erases and initializes a specified range of a firmware volume
1160 This - Calling context
1161 StartLba - The starting logical block index to be erased
1162 OffsetStartLba - Offset into the starting block at which to
1164 LastLba - The last logical block index to be erased
1165 OffsetStartLba - Offset into the last block at which to end erasing
1168 EFI_SUCCESS - The firmware volume was erased successfully
1169 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
1170 EFI_DEVICE_ERROR - The block device is not functioning correctly and
1171 could not be written. Firmware device may have been
1176 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
1178 FvbDevice
= FVB_EXTEND_DEVICE_FROM_THIS (This
);
1180 return FvbEraseCustomBlockRange (
1181 FvbDevice
->Instance
,
1194 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
1198 Routine Description:
1199 Check the integrity of firmware volume header
1202 FwVolHeader - A pointer to a firmware volume header
1205 EFI_SUCCESS - The firmware volume is consistent
1206 EFI_NOT_FOUND - The firmware volume has corrupted. So it is not an FV
1211 UINT16 HeaderLength
;
1215 // Verify the header revision, header signature, length
1216 // Length of FvBlock cannot be 2**64-1
1217 // HeaderLength cannot be an odd number
1219 if ((FwVolHeader
->Revision
!= EFI_FVH_REVISION
) ||
1220 (FwVolHeader
->Signature
!= EFI_FVH_SIGNATURE
) ||
1221 (FwVolHeader
->FvLength
== ((UINTN
) -1)) ||
1222 ((FwVolHeader
->HeaderLength
& 0x01) != 0)
1224 return EFI_NOT_FOUND
;
1227 // Verify the header checksum
1229 HeaderLength
= (UINT16
) (FwVolHeader
->HeaderLength
/ 2);
1230 Ptr
= (UINT16
*) FwVolHeader
;
1232 while (HeaderLength
> 0) {
1233 Checksum
= Checksum
+ (*Ptr
);
1238 if (Checksum
!= 0) {
1239 return EFI_NOT_FOUND
;
1248 IN EFI_HANDLE ImageHandle
,
1249 IN EFI_SYSTEM_TABLE
*SystemTable
1253 Routine Description:
1254 This function does common initialization for FVB services
1263 EFI_FW_VOL_INSTANCE
*FwhInstance
;
1264 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
1265 EFI_DXE_SERVICES
*DxeServices
;
1266 EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor
;
1268 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
1269 EFI_HANDLE FwbHandle
;
1270 EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
;
1271 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*OldFwbInterface
;
1272 EFI_DEVICE_PATH_PROTOCOL
*TempFwbDevicePath
;
1273 FV_DEVICE_PATH TempFvbDevicePathData
;
1275 EFI_PHYSICAL_ADDRESS BaseAddress
;
1278 EFI_PEI_HOB_POINTERS FvHob
;
1281 // Get the DXE services table
1286 // Allocate runtime services data for global variable, which contains
1287 // the private data of all firmware volume block instances
1289 mFvbModuleGlobal
= AllocateRuntimePool (sizeof (ESAL_FWB_GLOBAL
));
1290 ASSERT (mFvbModuleGlobal
!= NULL
);
1293 // Calculate the total size for all firmware volume block instances
1297 FvHob
.Raw
= GetHobList ();
1298 while ((FvHob
.Raw
= GetNextHob (EFI_HOB_TYPE_FV
, FvHob
.Raw
)) != NULL
) {
1299 BaseAddress
= FvHob
.FirmwareVolume
->BaseAddress
;
1300 Length
= FvHob
.FirmwareVolume
->Length
;
1302 // Check if it is a "real" flash
1304 Status
= DxeServices
->GetMemorySpaceDescriptor (
1308 if (EFI_ERROR (Status
)) {
1312 if (Descriptor
.GcdMemoryType
!= EfiGcdMemoryTypeMemoryMappedIo
) {
1313 FvHob
.Raw
= GET_NEXT_HOB (FvHob
);
1317 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) BaseAddress
;
1318 Status
= ValidateFvHeader (FwVolHeader
);
1319 if (EFI_ERROR (Status
)) {
1323 Status
= GetFvbInfo (Length
, &FwVolHeader
);
1324 if (EFI_ERROR (Status
)) {
1325 FvHob
.Raw
= GET_NEXT_HOB (FvHob
);
1330 BufferSize
+= (sizeof (EFI_FW_VOL_INSTANCE
) + FwVolHeader
->HeaderLength
- sizeof (EFI_FIRMWARE_VOLUME_HEADER
));
1331 FvHob
.Raw
= GET_NEXT_HOB (FvHob
);
1335 // Only need to allocate once. There is only one copy of physical memory for
1336 // the private data of each FV instance. But in virtual mode or in physical
1337 // mode, the address of the the physical memory may be different.
1339 mFvbModuleGlobal
->FvInstance
[FVB_PHYSICAL
] = AllocateRuntimePool (BufferSize
);
1340 ASSERT (mFvbModuleGlobal
->FvInstance
[FVB_PHYSICAL
] != NULL
);
1343 // Make a virtual copy of the FvInstance pointer.
1345 FwhInstance
= mFvbModuleGlobal
->FvInstance
[FVB_PHYSICAL
];
1346 mFvbModuleGlobal
->FvInstance
[FVB_VIRTUAL
] = FwhInstance
;
1348 mFvbModuleGlobal
->NumFv
= 0;
1351 FvHob
.Raw
= GetHobList ();
1352 while (NULL
!= (FvHob
.Raw
= GetNextHob (EFI_HOB_TYPE_FV
, FvHob
.Raw
))) {
1353 BaseAddress
= FvHob
.FirmwareVolume
->BaseAddress
;
1354 Length
= FvHob
.FirmwareVolume
->Length
;
1356 // Check if it is a "real" flash
1358 Status
= DxeServices
->GetMemorySpaceDescriptor (
1362 if (EFI_ERROR (Status
)) {
1366 if (Descriptor
.GcdMemoryType
!= EfiGcdMemoryTypeMemoryMappedIo
) {
1367 FvHob
.Raw
= GET_NEXT_HOB (FvHob
);
1371 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) BaseAddress
;
1372 Status
= ValidateFvHeader (FwVolHeader
);
1373 if (EFI_ERROR (Status
)) {
1375 // Get FvbInfo to provide in FwhInstance.
1377 Status
= GetFvbInfo (Length
, &FwVolHeader
);
1378 if (EFI_ERROR (Status
)) {
1379 FvHob
.Raw
= GET_NEXT_HOB (FvHob
);
1383 // Write healthy FV header back.
1386 (VOID
*) (UINTN
) BaseAddress
,
1387 (VOID
*) FwVolHeader
,
1388 FwVolHeader
->HeaderLength
1392 FwhInstance
->FvBase
[FVB_PHYSICAL
] = (UINTN
) BaseAddress
;
1393 FwhInstance
->FvBase
[FVB_VIRTUAL
] = (UINTN
) BaseAddress
;
1395 CopyMem ((UINTN
*) &(FwhInstance
->VolumeHeader
), (UINTN
*) FwVolHeader
, FwVolHeader
->HeaderLength
);
1396 FwVolHeader
= &(FwhInstance
->VolumeHeader
);
1397 EfiInitializeLock (&(FwhInstance
->FvbDevLock
), TPL_HIGH_LEVEL
);
1401 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
1403 // Get the maximum size of a block. The size will be used to allocate
1404 // buffer for Scratch space, the intermediate buffer for FVB extension
1407 if (MaxLbaSize
< PtrBlockMapEntry
->Length
) {
1408 MaxLbaSize
= PtrBlockMapEntry
->Length
;
1411 NumOfBlocks
= NumOfBlocks
+ PtrBlockMapEntry
->NumBlocks
;
1414 // The total number of blocks in the FV.
1416 FwhInstance
->NumOfBlocks
= NumOfBlocks
;
1419 // Add a FVB Protocol Instance
1421 FvbDevice
= AllocateRuntimePool (sizeof (EFI_FW_VOL_BLOCK_DEVICE
));
1422 ASSERT (FvbDevice
!= NULL
);
1424 CopyMem (FvbDevice
, &mFvbDeviceTemplate
, sizeof (EFI_FW_VOL_BLOCK_DEVICE
));
1426 FvbDevice
->Instance
= mFvbModuleGlobal
->NumFv
;
1427 mFvbModuleGlobal
->NumFv
++;
1430 // Set up the devicepath
1432 FvbDevice
->DevicePath
.MemMapDevPath
.StartingAddress
= BaseAddress
;
1433 FvbDevice
->DevicePath
.MemMapDevPath
.EndingAddress
= BaseAddress
+ (FwVolHeader
->FvLength
- 1);
1436 // Find a handle with a matching device path that has supports FW Block protocol
1438 TempFwbDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) &TempFvbDevicePathData
;
1439 CopyMem (TempFwbDevicePath
, &FvbDevice
->DevicePath
, sizeof (FV_DEVICE_PATH
));
1440 Status
= gBS
->LocateDevicePath (&gEfiFirmwareVolumeBlockProtocolGuid
, &TempFwbDevicePath
, &FwbHandle
);
1441 if (EFI_ERROR (Status
)) {
1443 // LocateDevicePath fails so install a new interface and device path
1446 Status
= gBS
->InstallMultipleProtocolInterfaces (
1448 &gEfiFirmwareVolumeBlockProtocolGuid
,
1449 &FvbDevice
->FwVolBlockInstance
,
1450 &gEfiDevicePathProtocolGuid
,
1451 &FvbDevice
->DevicePath
,
1454 ASSERT_EFI_ERROR (Status
);
1455 } else if (EfiIsDevicePathEnd (TempFwbDevicePath
)) {
1457 // Device allready exists, so reinstall the FVB protocol
1459 Status
= gBS
->HandleProtocol (
1461 &gEfiFirmwareVolumeBlockProtocolGuid
,
1464 ASSERT_EFI_ERROR (Status
);
1466 Status
= gBS
->ReinstallProtocolInterface (
1468 &gEfiFirmwareVolumeBlockProtocolGuid
,
1470 &FvbDevice
->FwVolBlockInstance
1472 ASSERT_EFI_ERROR (Status
);
1476 // There was a FVB protocol on an End Device Path node
1481 // Install FVB Extension Protocol on the same handle
1483 Status
= gBS
->InstallMultipleProtocolInterfaces (
1485 &gEfiFvbExtensionProtocolGuid
,
1486 &FvbDevice
->FvbExtension
,
1487 &gEfiAlternateFvBlockGuid
,
1492 ASSERT_EFI_ERROR (Status
);
1494 FwhInstance
= (EFI_FW_VOL_INSTANCE
*)
1496 (UINTN
) ((UINT8
*) FwhInstance
) + FwVolHeader
->HeaderLength
+
1497 (sizeof (EFI_FW_VOL_INSTANCE
) - sizeof (EFI_FIRMWARE_VOLUME_HEADER
))
1500 FvHob
.Raw
= GET_NEXT_HOB (FvHob
);
1504 // Allocate for scratch space, an intermediate buffer for FVB extention
1506 mFvbModuleGlobal
->FvbScratchSpace
[FVB_PHYSICAL
] = AllocateRuntimePool (MaxLbaSize
);
1507 ASSERT (mFvbModuleGlobal
->FvbScratchSpace
[FVB_PHYSICAL
] != NULL
);
1509 mFvbModuleGlobal
->FvbScratchSpace
[FVB_VIRTUAL
] = mFvbModuleGlobal
->FvbScratchSpace
[FVB_PHYSICAL
];