2 Firmware File System driver that produce Firmware Volume protocol.
3 Layers on top of Firmware Block protocol to produce a file abstraction
6 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include "FwVolDriver.h"
15 // Protocol notify related globals
17 VOID
*gEfiFwVolBlockNotifyReg
;
18 EFI_EVENT gEfiFwVolBlockEvent
;
20 FV_DEVICE mFvDevice
= {
25 FvGetVolumeAttributes
,
26 FvSetVolumeAttributes
,
48 // FFS helper functions
52 Read data from Firmware Block by FVB protocol Read.
53 The data may cross the multi block ranges.
55 @param Fvb The FW_VOL_BLOCK_PROTOCOL instance from which to read data.
56 @param StartLba Pointer to StartLba.
57 On input, the start logical block index from which to read.
58 On output,the end logical block index after reading.
59 @param Offset Pointer to Offset
60 On input, offset into the block at which to begin reading.
61 On output, offset into the end block after reading.
62 @param DataSize Size of data to be read.
63 @param Data Pointer to Buffer that the data will be read into.
65 @retval EFI_SUCCESS Successfully read data from firmware block.
70 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
71 IN OUT EFI_LBA
*StartLba
,
84 // Try read data in current block
87 ReadDataSize
= DataSize
;
88 Status
= Fvb
->Read (Fvb
, *StartLba
, *Offset
, &ReadDataSize
, Data
);
89 if (Status
== EFI_SUCCESS
) {
92 } else if (Status
!= EFI_BAD_BUFFER_SIZE
) {
94 // other error will direct return
100 // Data crosses the blocks, read data from next block
102 DataSize
-= ReadDataSize
;
103 Data
+= ReadDataSize
;
104 *StartLba
= *StartLba
+ 1;
105 while (DataSize
> 0) {
106 Status
= Fvb
->GetBlockSize (Fvb
, *StartLba
, &BlockSize
, &NumberOfBlocks
);
107 if (EFI_ERROR (Status
)) {
112 // Read data from the crossing blocks
115 while (BlockIndex
< NumberOfBlocks
&& DataSize
>= BlockSize
) {
116 Status
= Fvb
->Read (Fvb
, *StartLba
+ BlockIndex
, 0, &BlockSize
, Data
);
117 if (EFI_ERROR (Status
)) {
122 DataSize
-= BlockSize
;
127 // Data doesn't exceed the current block range.
129 if (DataSize
< BlockSize
) {
134 // Data must be got from the next block range.
136 *StartLba
+= NumberOfBlocks
;
140 // read the remaining data
143 Status
= Fvb
->Read (Fvb
, *StartLba
+ BlockIndex
, 0, &DataSize
, Data
);
144 if (EFI_ERROR (Status
)) {
150 // Update Lba and Offset used by the following read.
152 *StartLba
+= BlockIndex
;
159 Given the supplied FW_VOL_BLOCK_PROTOCOL, allocate a buffer for output and
160 copy the real length volume header into it.
162 @param Fvb The FW_VOL_BLOCK_PROTOCOL instance from which to
163 read the volume header
164 @param FwVolHeader Pointer to pointer to allocated buffer in which
165 the volume header is returned.
167 @retval EFI_OUT_OF_RESOURCES No enough buffer could be allocated.
168 @retval EFI_SUCCESS Successfully read volume header to the allocated
170 @retval EFI_INVALID_PARAMETER The FV Header signature is not as expected or
171 the file system could not be understood.
176 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
177 OUT EFI_FIRMWARE_VOLUME_HEADER
**FwVolHeader
181 EFI_FIRMWARE_VOLUME_HEADER TempFvh
;
188 // Read the standard FV header
192 FvhLength
= sizeof (EFI_FIRMWARE_VOLUME_HEADER
);
193 Status
= ReadFvbData (Fvb
, &StartLba
, &Offset
, FvhLength
, (UINT8
*)&TempFvh
);
194 if (EFI_ERROR (Status
)) {
199 // Validate FV Header signature, if not as expected, continue.
201 if (TempFvh
.Signature
!= EFI_FVH_SIGNATURE
) {
202 return EFI_INVALID_PARAMETER
;
206 // Check to see that the file system is indeed formatted in a way we can
209 if ((!CompareGuid (&TempFvh
.FileSystemGuid
, &gEfiFirmwareFileSystem2Guid
)) &&
210 (!CompareGuid (&TempFvh
.FileSystemGuid
, &gEfiFirmwareFileSystem3Guid
)))
212 return EFI_INVALID_PARAMETER
;
216 // Allocate a buffer for the caller
218 *FwVolHeader
= AllocatePool (TempFvh
.HeaderLength
);
219 if (*FwVolHeader
== NULL
) {
220 return EFI_OUT_OF_RESOURCES
;
224 // Copy the standard header into the buffer
226 CopyMem (*FwVolHeader
, &TempFvh
, sizeof (EFI_FIRMWARE_VOLUME_HEADER
));
229 // Read the rest of the header
231 FvhLength
= TempFvh
.HeaderLength
- sizeof (EFI_FIRMWARE_VOLUME_HEADER
);
232 Buffer
= (UINT8
*)*FwVolHeader
+ sizeof (EFI_FIRMWARE_VOLUME_HEADER
);
233 Status
= ReadFvbData (Fvb
, &StartLba
, &Offset
, FvhLength
, Buffer
);
234 if (EFI_ERROR (Status
)) {
236 // Read failed so free buffer
238 CoreFreePool (*FwVolHeader
);
245 Free FvDevice resource when error happens
247 @param FvDevice pointer to the FvDevice to be freed.
251 FreeFvDeviceResource (
252 IN FV_DEVICE
*FvDevice
255 FFS_FILE_LIST_ENTRY
*FfsFileEntry
;
256 LIST_ENTRY
*NextEntry
;
259 // Free File List Entry
261 FfsFileEntry
= (FFS_FILE_LIST_ENTRY
*)FvDevice
->FfsFileListHeader
.ForwardLink
;
262 while (&FfsFileEntry
->Link
!= &FvDevice
->FfsFileListHeader
) {
263 NextEntry
= (&FfsFileEntry
->Link
)->ForwardLink
;
265 if (FfsFileEntry
->StreamHandle
!= 0) {
267 // Close stream and free resources from SEP
269 CloseSectionStream (FfsFileEntry
->StreamHandle
, FALSE
);
272 if (FfsFileEntry
->FileCached
) {
274 // Free the cached file buffer.
276 CoreFreePool (FfsFileEntry
->FfsHeader
);
279 CoreFreePool (FfsFileEntry
);
281 FfsFileEntry
= (FFS_FILE_LIST_ENTRY
*)NextEntry
;
284 if (!FvDevice
->IsMemoryMapped
) {
286 // Free the cached FV buffer.
288 CoreFreePool (FvDevice
->CachedFv
);
292 // Free Volume Header
294 CoreFreePool (FvDevice
->FwVolHeader
);
300 Check if an FV is consistent and allocate cache for it.
302 @param FvDevice A pointer to the FvDevice to be checked.
304 @retval EFI_OUT_OF_RESOURCES No enough buffer could be allocated.
305 @retval EFI_SUCCESS FV is consistent and cache is allocated.
306 @retval EFI_VOLUME_CORRUPTED File system is corrupted.
311 IN OUT FV_DEVICE
*FvDevice
315 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
316 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
317 EFI_FIRMWARE_VOLUME_EXT_HEADER
*FwVolExtHeader
;
318 EFI_FVB_ATTRIBUTES_2 FvbAttributes
;
319 EFI_FV_BLOCK_MAP_ENTRY
*BlockMap
;
320 FFS_FILE_LIST_ENTRY
*FfsFileEntry
;
321 EFI_FFS_FILE_HEADER
*FfsHeader
;
322 UINT8
*CacheLocation
;
326 EFI_FFS_FILE_STATE FileState
;
329 EFI_PHYSICAL_ADDRESS PhysicalAddress
;
332 EFI_FFS_FILE_HEADER
*CacheFfsHeader
;
335 CacheFfsHeader
= NULL
;
338 FwVolHeader
= FvDevice
->FwVolHeader
;
340 Status
= Fvb
->GetAttributes (Fvb
, &FvbAttributes
);
341 if (EFI_ERROR (Status
)) {
345 Size
= (UINTN
)FwVolHeader
->FvLength
;
346 if ((FvbAttributes
& EFI_FVB2_MEMORY_MAPPED
) != 0) {
347 FvDevice
->IsMemoryMapped
= TRUE
;
349 Status
= Fvb
->GetPhysicalAddress (Fvb
, &PhysicalAddress
);
350 if (EFI_ERROR (Status
)) {
355 // Don't cache memory mapped FV really.
357 FvDevice
->CachedFv
= (UINT8
*)(UINTN
)PhysicalAddress
;
359 FvDevice
->IsMemoryMapped
= FALSE
;
360 FvDevice
->CachedFv
= AllocatePool (Size
);
362 if (FvDevice
->CachedFv
== NULL
) {
363 return EFI_OUT_OF_RESOURCES
;
368 // Remember a pointer to the end of the CachedFv
370 FvDevice
->EndOfCachedFv
= FvDevice
->CachedFv
+ Size
;
372 if (!FvDevice
->IsMemoryMapped
) {
374 // Copy FV into memory using the block map.
376 BlockMap
= FwVolHeader
->BlockMap
;
377 CacheLocation
= FvDevice
->CachedFv
;
379 while ((BlockMap
->NumBlocks
!= 0) || (BlockMap
->Length
!= 0)) {
383 Size
= BlockMap
->Length
;
384 for (Index
= 0; Index
< BlockMap
->NumBlocks
; Index
++) {
394 // Not check EFI_BAD_BUFFER_SIZE, for Size = BlockMap->Length
396 if (EFI_ERROR (Status
)) {
401 CacheLocation
+= BlockMap
->Length
;
409 // Scan to check the free space & File list
411 if ((FvbAttributes
& EFI_FVB2_ERASE_POLARITY
) != 0) {
412 FvDevice
->ErasePolarity
= 1;
414 FvDevice
->ErasePolarity
= 0;
418 // go through the whole FV cache, check the consistence of the FV.
419 // Make a linked list of all the Ffs file headers
421 Status
= EFI_SUCCESS
;
422 InitializeListHead (&FvDevice
->FfsFileListHeader
);
427 if (FwVolHeader
->ExtHeaderOffset
!= 0) {
429 // Searching for files starts on an 8 byte aligned boundary after the end of the Extended Header if it exists.
431 FwVolExtHeader
= (EFI_FIRMWARE_VOLUME_EXT_HEADER
*)(FvDevice
->CachedFv
+ FwVolHeader
->ExtHeaderOffset
);
432 FfsHeader
= (EFI_FFS_FILE_HEADER
*)((UINT8
*)FwVolExtHeader
+ FwVolExtHeader
->ExtHeaderSize
);
434 FfsHeader
= (EFI_FFS_FILE_HEADER
*)(FvDevice
->CachedFv
+ FwVolHeader
->HeaderLength
);
437 FfsHeader
= (EFI_FFS_FILE_HEADER
*)ALIGN_POINTER (FfsHeader
, 8);
438 TopFvAddress
= FvDevice
->EndOfCachedFv
;
439 while (((UINTN
)FfsHeader
>= (UINTN
)FvDevice
->CachedFv
) && ((UINTN
)FfsHeader
<= (UINTN
)((UINTN
)TopFvAddress
- sizeof (EFI_FFS_FILE_HEADER
)))) {
441 CoreFreePool (CacheFfsHeader
);
445 TestLength
= TopFvAddress
- ((UINT8
*)FfsHeader
);
446 if (TestLength
> sizeof (EFI_FFS_FILE_HEADER
)) {
447 TestLength
= sizeof (EFI_FFS_FILE_HEADER
);
450 if (IsBufferErased (FvDevice
->ErasePolarity
, FfsHeader
, TestLength
)) {
452 // We have found the free space so we are done!
457 if (!IsValidFfsHeader (FvDevice
->ErasePolarity
, FfsHeader
, &FileState
)) {
458 if ((FileState
== EFI_FILE_HEADER_INVALID
) ||
459 (FileState
== EFI_FILE_HEADER_CONSTRUCTION
))
461 if (IS_FFS_FILE2 (FfsHeader
)) {
462 if (!FvDevice
->IsFfs3Fv
) {
463 DEBUG ((DEBUG_ERROR
, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FfsHeader
->Name
));
466 FfsHeader
= (EFI_FFS_FILE_HEADER
*)((UINT8
*)FfsHeader
+ sizeof (EFI_FFS_FILE_HEADER2
));
468 FfsHeader
= (EFI_FFS_FILE_HEADER
*)((UINT8
*)FfsHeader
+ sizeof (EFI_FFS_FILE_HEADER
));
474 // File system is corrputed
476 Status
= EFI_VOLUME_CORRUPTED
;
481 CacheFfsHeader
= FfsHeader
;
482 if ((CacheFfsHeader
->Attributes
& FFS_ATTRIB_CHECKSUM
) == FFS_ATTRIB_CHECKSUM
) {
483 if (FvDevice
->IsMemoryMapped
) {
485 // Memory mapped FV has not been cached.
486 // Here is to cache FFS file to memory buffer for following checksum calculating.
487 // And then, the cached file buffer can be also used for FvReadFile.
489 WholeFileSize
= IS_FFS_FILE2 (CacheFfsHeader
) ? FFS_FILE2_SIZE (CacheFfsHeader
) : FFS_FILE_SIZE (CacheFfsHeader
);
490 CacheFfsHeader
= AllocateCopyPool (WholeFileSize
, CacheFfsHeader
);
491 if (CacheFfsHeader
== NULL
) {
492 Status
= EFI_OUT_OF_RESOURCES
;
500 if (!IsValidFfsFile (FvDevice
->ErasePolarity
, CacheFfsHeader
)) {
502 // File system is corrupted
504 Status
= EFI_VOLUME_CORRUPTED
;
508 if (IS_FFS_FILE2 (CacheFfsHeader
)) {
509 ASSERT (FFS_FILE2_SIZE (CacheFfsHeader
) > 0x00FFFFFF);
510 if (!FvDevice
->IsFfs3Fv
) {
511 DEBUG ((DEBUG_ERROR
, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &CacheFfsHeader
->Name
));
512 FfsHeader
= (EFI_FFS_FILE_HEADER
*)((UINT8
*)FfsHeader
+ FFS_FILE2_SIZE (CacheFfsHeader
));
514 // Adjust pointer to the next 8-byte aligned boundary.
516 FfsHeader
= (EFI_FFS_FILE_HEADER
*)(((UINTN
)FfsHeader
+ 7) & ~0x07);
521 FileState
= GetFileState (FvDevice
->ErasePolarity
, CacheFfsHeader
);
524 // check for non-deleted file
526 if (FileState
!= EFI_FILE_DELETED
) {
528 // Create a FFS list entry for each non-deleted file
530 FfsFileEntry
= AllocateZeroPool (sizeof (FFS_FILE_LIST_ENTRY
));
531 if (FfsFileEntry
== NULL
) {
532 Status
= EFI_OUT_OF_RESOURCES
;
536 FfsFileEntry
->FfsHeader
= CacheFfsHeader
;
537 FfsFileEntry
->FileCached
= FileCached
;
539 InsertTailList (&FvDevice
->FfsFileListHeader
, &FfsFileEntry
->Link
);
542 if (IS_FFS_FILE2 (CacheFfsHeader
)) {
543 FfsHeader
= (EFI_FFS_FILE_HEADER
*)((UINT8
*)FfsHeader
+ FFS_FILE2_SIZE (CacheFfsHeader
));
545 FfsHeader
= (EFI_FFS_FILE_HEADER
*)((UINT8
*)FfsHeader
+ FFS_FILE_SIZE (CacheFfsHeader
));
549 // Adjust pointer to the next 8-byte aligned boundary.
551 FfsHeader
= (EFI_FFS_FILE_HEADER
*)(((UINTN
)FfsHeader
+ 7) & ~0x07);
555 if (EFI_ERROR (Status
)) {
557 CoreFreePool (CacheFfsHeader
);
561 FreeFvDeviceResource (FvDevice
);
568 This notification function is invoked when an instance of the
569 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL is produced. It layers an instance of the
570 EFI_FIRMWARE_VOLUME2_PROTOCOL on the same handle. This is the function where
571 the actual initialization of the EFI_FIRMWARE_VOLUME2_PROTOCOL is done.
573 @param Event The event that occurred
574 @param Context For EFI compatiblity. Not used.
587 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
588 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
590 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
593 // Examine all new handles
597 // Get the next handle
599 BufferSize
= sizeof (Handle
);
600 Status
= CoreLocateHandle (
603 gEfiFwVolBlockNotifyReg
,
609 // If not found, we're done
611 if (EFI_NOT_FOUND
== Status
) {
615 if (EFI_ERROR (Status
)) {
620 // Get the FirmwareVolumeBlock protocol on that handle
622 Status
= CoreHandleProtocol (Handle
, &gEfiFirmwareVolumeBlockProtocolGuid
, (VOID
**)&Fvb
);
623 ASSERT_EFI_ERROR (Status
);
624 ASSERT (Fvb
!= NULL
);
627 // Make sure the Fv Header is O.K.
629 Status
= GetFwVolHeader (Fvb
, &FwVolHeader
);
630 if (EFI_ERROR (Status
)) {
634 ASSERT (FwVolHeader
!= NULL
);
636 if (!VerifyFvHeaderChecksum (FwVolHeader
)) {
637 CoreFreePool (FwVolHeader
);
642 // Check if there is an FV protocol already installed in that handle
644 Status
= CoreHandleProtocol (Handle
, &gEfiFirmwareVolume2ProtocolGuid
, (VOID
**)&Fv
);
645 if (!EFI_ERROR (Status
)) {
647 // Update Fv to use a new Fvb
649 FvDevice
= BASE_CR (Fv
, FV_DEVICE
, Fv
);
650 if (FvDevice
->Signature
== FV2_DEVICE_SIGNATURE
) {
652 // Only write into our device structure if it's our device structure
658 // No FwVol protocol on the handle so create a new one
660 FvDevice
= AllocateCopyPool (sizeof (FV_DEVICE
), &mFvDevice
);
661 if (FvDevice
== NULL
) {
666 FvDevice
->Handle
= Handle
;
667 FvDevice
->FwVolHeader
= FwVolHeader
;
668 FvDevice
->IsFfs3Fv
= CompareGuid (&FwVolHeader
->FileSystemGuid
, &gEfiFirmwareFileSystem3Guid
);
669 FvDevice
->Fv
.ParentHandle
= Fvb
->ParentHandle
;
671 // Inherit the authentication status from FVB.
673 FvDevice
->AuthenticationStatus
= GetFvbAuthenticationStatus (Fvb
);
675 if (!EFI_ERROR (FvCheck (FvDevice
))) {
677 // Install an New FV protocol on the existing handle
679 Status
= CoreInstallProtocolInterface (
681 &gEfiFirmwareVolume2ProtocolGuid
,
682 EFI_NATIVE_INTERFACE
,
685 ASSERT_EFI_ERROR (Status
);
688 // Free FvDevice Buffer for the corrupt FV image.
690 CoreFreePool (FvDevice
);
699 This routine is the driver initialization entry point. It registers
700 a notification function. This notification function are responsible
701 for building the FV stack dynamically.
703 @param ImageHandle The image handle.
704 @param SystemTable The system table.
706 @retval EFI_SUCCESS Function successfully returned.
712 IN EFI_HANDLE ImageHandle
,
713 IN EFI_SYSTEM_TABLE
*SystemTable
716 gEfiFwVolBlockEvent
= EfiCreateProtocolNotifyEvent (
717 &gEfiFirmwareVolumeBlockProtocolGuid
,
721 &gEfiFwVolBlockNotifyReg