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 - 2014, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include "FwVolDriver.h"
22 // Protocol notify related globals
24 VOID
*gEfiFwVolBlockNotifyReg
;
25 EFI_EVENT gEfiFwVolBlockEvent
;
27 FV_DEVICE mFvDevice
= {
32 FvGetVolumeAttributes
,
33 FvSetVolumeAttributes
,
56 // FFS helper functions
59 Read data from Firmware Block by FVB protocol Read.
60 The data may cross the multi block ranges.
62 @param Fvb The FW_VOL_BLOCK_PROTOCOL instance from which to read data.
63 @param StartLba Pointer to StartLba.
64 On input, the start logical block index from which to read.
65 On output,the end logical block index after reading.
66 @param Offset Pointer to Offset
67 On input, offset into the block at which to begin reading.
68 On output, offset into the end block after reading.
69 @param DataSize Size of data to be read.
70 @param Data Pointer to Buffer that the data will be read into.
72 @retval EFI_SUCCESS Successfully read data from firmware block.
77 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
78 IN OUT EFI_LBA
*StartLba
,
91 // Try read data in current block
94 ReadDataSize
= DataSize
;
95 Status
= Fvb
->Read (Fvb
, *StartLba
, *Offset
, &ReadDataSize
, Data
);
96 if (Status
== EFI_SUCCESS
) {
99 } else if (Status
!= EFI_BAD_BUFFER_SIZE
) {
101 // other error will direct return
107 // Data crosses the blocks, read data from next block
109 DataSize
-= ReadDataSize
;
110 Data
+= ReadDataSize
;
111 *StartLba
= *StartLba
+ 1;
112 while (DataSize
> 0) {
113 Status
= Fvb
->GetBlockSize (Fvb
, *StartLba
, &BlockSize
, &NumberOfBlocks
);
114 if (EFI_ERROR (Status
)) {
119 // Read data from the crossing blocks
122 while (BlockIndex
< NumberOfBlocks
&& DataSize
>= BlockSize
) {
123 Status
= Fvb
->Read (Fvb
, *StartLba
+ BlockIndex
, 0, &BlockSize
, Data
);
124 if (EFI_ERROR (Status
)) {
128 DataSize
-= BlockSize
;
133 // Data doesn't exceed the current block range.
135 if (DataSize
< BlockSize
) {
140 // Data must be got from the next block range.
142 *StartLba
+= NumberOfBlocks
;
146 // read the remaining data
149 Status
= Fvb
->Read (Fvb
, *StartLba
+ BlockIndex
, 0, &DataSize
, Data
);
150 if (EFI_ERROR (Status
)) {
156 // Update Lba and Offset used by the following read.
158 *StartLba
+= BlockIndex
;
165 Given the supplied FW_VOL_BLOCK_PROTOCOL, allocate a buffer for output and
166 copy the real length volume header into it.
168 @param Fvb The FW_VOL_BLOCK_PROTOCOL instance from which to
169 read the volume header
170 @param FwVolHeader Pointer to pointer to allocated buffer in which
171 the volume header is returned.
173 @retval EFI_OUT_OF_RESOURCES No enough buffer could be allocated.
174 @retval EFI_SUCCESS Successfully read volume header to the allocated
180 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
181 OUT EFI_FIRMWARE_VOLUME_HEADER
**FwVolHeader
185 EFI_FIRMWARE_VOLUME_HEADER TempFvh
;
192 // Read the standard FV header
196 FvhLength
= sizeof (EFI_FIRMWARE_VOLUME_HEADER
);
197 Status
= ReadFvbData (Fvb
, &StartLba
, &Offset
, FvhLength
, (UINT8
*)&TempFvh
);
198 if (EFI_ERROR (Status
)) {
203 // Allocate a buffer for the caller
205 *FwVolHeader
= AllocatePool (TempFvh
.HeaderLength
);
206 if (*FwVolHeader
== NULL
) {
207 return EFI_OUT_OF_RESOURCES
;
211 // Copy the standard header into the buffer
213 CopyMem (*FwVolHeader
, &TempFvh
, sizeof (EFI_FIRMWARE_VOLUME_HEADER
));
216 // Read the rest of the header
218 FvhLength
= TempFvh
.HeaderLength
- sizeof (EFI_FIRMWARE_VOLUME_HEADER
);
219 Buffer
= (UINT8
*)*FwVolHeader
+ sizeof (EFI_FIRMWARE_VOLUME_HEADER
);
220 Status
= ReadFvbData (Fvb
, &StartLba
, &Offset
, FvhLength
, Buffer
);
221 if (EFI_ERROR (Status
)) {
223 // Read failed so free buffer
225 CoreFreePool (*FwVolHeader
);
234 Free FvDevice resource when error happens
236 @param FvDevice pointer to the FvDevice to be freed.
240 FreeFvDeviceResource (
241 IN FV_DEVICE
*FvDevice
244 FFS_FILE_LIST_ENTRY
*FfsFileEntry
;
245 LIST_ENTRY
*NextEntry
;
248 // Free File List Entry
250 FfsFileEntry
= (FFS_FILE_LIST_ENTRY
*)FvDevice
->FfsFileListHeader
.ForwardLink
;
251 while (&FfsFileEntry
->Link
!= &FvDevice
->FfsFileListHeader
) {
252 NextEntry
= (&FfsFileEntry
->Link
)->ForwardLink
;
254 if (FfsFileEntry
->StreamHandle
!= 0) {
256 // Close stream and free resources from SEP
258 CloseSectionStream (FfsFileEntry
->StreamHandle
, FALSE
);
261 if (FfsFileEntry
->FileCached
) {
263 // Free the cached file buffer.
265 CoreFreePool (FfsFileEntry
->FfsHeader
);
268 CoreFreePool (FfsFileEntry
);
270 FfsFileEntry
= (FFS_FILE_LIST_ENTRY
*) NextEntry
;
273 if (!FvDevice
->IsMemoryMapped
) {
275 // Free the cached FV buffer.
277 CoreFreePool (FvDevice
->CachedFv
);
281 // Free Volume Header
283 CoreFreePool (FvDevice
->FwVolHeader
);
291 Check if an FV is consistent and allocate cache for it.
293 @param FvDevice A pointer to the FvDevice to be checked.
295 @retval EFI_OUT_OF_RESOURCES No enough buffer could be allocated.
296 @retval EFI_SUCCESS FV is consistent and cache is allocated.
297 @retval EFI_VOLUME_CORRUPTED File system is corrupted.
302 IN OUT FV_DEVICE
*FvDevice
306 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
307 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
308 EFI_FIRMWARE_VOLUME_EXT_HEADER
*FwVolExtHeader
;
309 EFI_FVB_ATTRIBUTES_2 FvbAttributes
;
310 EFI_FV_BLOCK_MAP_ENTRY
*BlockMap
;
311 FFS_FILE_LIST_ENTRY
*FfsFileEntry
;
312 EFI_FFS_FILE_HEADER
*FfsHeader
;
313 UINT8
*CacheLocation
;
319 EFI_FFS_FILE_STATE FileState
;
322 EFI_PHYSICAL_ADDRESS PhysicalAddress
;
325 EFI_FFS_FILE_HEADER
*CacheFfsHeader
;
328 CacheFfsHeader
= NULL
;
331 FwVolHeader
= FvDevice
->FwVolHeader
;
333 Status
= Fvb
->GetAttributes (Fvb
, &FvbAttributes
);
334 if (EFI_ERROR (Status
)) {
339 // Size is the size of the FV minus the head. We have already allocated
340 // the header to check to make sure the volume is valid
342 Size
= (UINTN
)(FwVolHeader
->FvLength
- FwVolHeader
->HeaderLength
);
343 if ((FvbAttributes
& EFI_FVB2_MEMORY_MAPPED
) != 0) {
344 FvDevice
->IsMemoryMapped
= TRUE
;
346 Status
= Fvb
->GetPhysicalAddress (Fvb
, &PhysicalAddress
);
347 if (EFI_ERROR (Status
)) {
352 // Don't cache memory mapped FV really.
354 FvDevice
->CachedFv
= (UINT8
*) (UINTN
) (PhysicalAddress
+ FwVolHeader
->HeaderLength
);
356 FvDevice
->IsMemoryMapped
= FALSE
;
357 FvDevice
->CachedFv
= AllocatePool (Size
);
359 if (FvDevice
->CachedFv
== NULL
) {
360 return EFI_OUT_OF_RESOURCES
;
365 // Remember a pointer to the end fo the CachedFv
367 FvDevice
->EndOfCachedFv
= FvDevice
->CachedFv
+ Size
;
369 if (!FvDevice
->IsMemoryMapped
) {
371 // Copy FV minus header into memory using the block map we have all ready
374 BlockMap
= FwVolHeader
->BlockMap
;
375 CacheLocation
= FvDevice
->CachedFv
;
378 HeaderSize
= FwVolHeader
->HeaderLength
;
379 while ((BlockMap
->NumBlocks
!= 0) || (BlockMap
->Length
!= 0)) {
381 Size
= BlockMap
->Length
;
382 if (HeaderSize
> 0) {
386 for (; Index
< BlockMap
->NumBlocks
&& HeaderSize
>= BlockMap
->Length
; Index
++) {
387 HeaderSize
-= BlockMap
->Length
;
392 // Check whether FvHeader is crossing the multi block range.
394 if (Index
>= BlockMap
->NumBlocks
) {
397 } else if (HeaderSize
> 0) {
398 LbaOffset
= HeaderSize
;
399 Size
= BlockMap
->Length
- HeaderSize
;
407 for (; Index
< BlockMap
->NumBlocks
; Index
++) {
408 Status
= Fvb
->Read (Fvb
,
416 // Not check EFI_BAD_BUFFER_SIZE, for Size = BlockMap->Length
418 if (EFI_ERROR (Status
)) {
423 CacheLocation
+= Size
;
426 // After we skip Fv Header always read from start of block
429 Size
= BlockMap
->Length
;
437 // Scan to check the free space & File list
439 if ((FvbAttributes
& EFI_FVB2_ERASE_POLARITY
) != 0) {
440 FvDevice
->ErasePolarity
= 1;
442 FvDevice
->ErasePolarity
= 0;
447 // go through the whole FV cache, check the consistence of the FV.
448 // Make a linked list of all the Ffs file headers
450 Status
= EFI_SUCCESS
;
451 InitializeListHead (&FvDevice
->FfsFileListHeader
);
456 if (FwVolHeader
->ExtHeaderOffset
!= 0) {
458 // Searching for files starts on an 8 byte aligned boundary after the end of the Extended Header if it exists.
460 FwVolExtHeader
= (EFI_FIRMWARE_VOLUME_EXT_HEADER
*) (FvDevice
->CachedFv
+ (FwVolHeader
->ExtHeaderOffset
- FwVolHeader
->HeaderLength
));
461 FfsHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FwVolExtHeader
+ FwVolExtHeader
->ExtHeaderSize
);
462 FfsHeader
= (EFI_FFS_FILE_HEADER
*) ALIGN_POINTER (FfsHeader
, 8);
464 FfsHeader
= (EFI_FFS_FILE_HEADER
*) (FvDevice
->CachedFv
);
466 TopFvAddress
= FvDevice
->EndOfCachedFv
;
467 while ((UINT8
*) FfsHeader
< TopFvAddress
) {
470 CoreFreePool (CacheFfsHeader
);
474 TestLength
= TopFvAddress
- ((UINT8
*) FfsHeader
);
475 if (TestLength
> sizeof (EFI_FFS_FILE_HEADER
)) {
476 TestLength
= sizeof (EFI_FFS_FILE_HEADER
);
479 if (IsBufferErased (FvDevice
->ErasePolarity
, FfsHeader
, TestLength
)) {
481 // We have found the free space so we are done!
486 if (!IsValidFfsHeader (FvDevice
->ErasePolarity
, FfsHeader
, &FileState
)) {
487 if ((FileState
== EFI_FILE_HEADER_INVALID
) ||
488 (FileState
== EFI_FILE_HEADER_CONSTRUCTION
)) {
489 if (IS_FFS_FILE2 (FfsHeader
)) {
490 if (!FvDevice
->IsFfs3Fv
) {
491 DEBUG ((EFI_D_ERROR
, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FfsHeader
->Name
));
493 FfsHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FfsHeader
+ sizeof (EFI_FFS_FILE_HEADER2
));
495 FfsHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FfsHeader
+ sizeof (EFI_FFS_FILE_HEADER
));
500 // File system is corrputed
502 Status
= EFI_VOLUME_CORRUPTED
;
507 CacheFfsHeader
= FfsHeader
;
508 if ((CacheFfsHeader
->Attributes
& FFS_ATTRIB_CHECKSUM
) == FFS_ATTRIB_CHECKSUM
) {
509 if (FvDevice
->IsMemoryMapped
) {
511 // Memory mapped FV has not been cached.
512 // Here is to cache FFS file to memory buffer for following checksum calculating.
513 // And then, the cached file buffer can be also used for FvReadFile.
515 WholeFileSize
= IS_FFS_FILE2 (CacheFfsHeader
) ? FFS_FILE2_SIZE (CacheFfsHeader
): FFS_FILE_SIZE (CacheFfsHeader
);
516 CacheFfsHeader
= AllocateCopyPool (WholeFileSize
, CacheFfsHeader
);
517 if (CacheFfsHeader
== NULL
) {
518 Status
= EFI_OUT_OF_RESOURCES
;
525 if (!IsValidFfsFile (FvDevice
->ErasePolarity
, CacheFfsHeader
)) {
527 // File system is corrupted
529 Status
= EFI_VOLUME_CORRUPTED
;
533 if (IS_FFS_FILE2 (CacheFfsHeader
)) {
534 ASSERT (FFS_FILE2_SIZE (CacheFfsHeader
) > 0x00FFFFFF);
535 if (!FvDevice
->IsFfs3Fv
) {
536 DEBUG ((EFI_D_ERROR
, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &CacheFfsHeader
->Name
));
537 FfsHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FfsHeader
+ FFS_FILE2_SIZE (CacheFfsHeader
));
539 // Adjust pointer to the next 8-byte aligned boundry.
541 FfsHeader
= (EFI_FFS_FILE_HEADER
*) (((UINTN
) FfsHeader
+ 7) & ~0x07);
546 FileState
= GetFileState (FvDevice
->ErasePolarity
, CacheFfsHeader
);
549 // check for non-deleted file
551 if (FileState
!= EFI_FILE_DELETED
) {
553 // Create a FFS list entry for each non-deleted file
555 FfsFileEntry
= AllocateZeroPool (sizeof (FFS_FILE_LIST_ENTRY
));
556 if (FfsFileEntry
== NULL
) {
557 Status
= EFI_OUT_OF_RESOURCES
;
561 FfsFileEntry
->FfsHeader
= CacheFfsHeader
;
562 FfsFileEntry
->FileCached
= FileCached
;
564 InsertTailList (&FvDevice
->FfsFileListHeader
, &FfsFileEntry
->Link
);
567 if (IS_FFS_FILE2 (CacheFfsHeader
)) {
568 FfsHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FfsHeader
+ FFS_FILE2_SIZE (CacheFfsHeader
));
570 FfsHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FfsHeader
+ FFS_FILE_SIZE (CacheFfsHeader
));
574 // Adjust pointer to the next 8-byte aligned boundry.
576 FfsHeader
= (EFI_FFS_FILE_HEADER
*)(((UINTN
)FfsHeader
+ 7) & ~0x07);
581 if (EFI_ERROR (Status
)) {
583 CoreFreePool (CacheFfsHeader
);
586 FreeFvDeviceResource (FvDevice
);
595 This notification function is invoked when an instance of the
596 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL is produced. It layers an instance of the
597 EFI_FIRMWARE_VOLUME2_PROTOCOL on the same handle. This is the function where
598 the actual initialization of the EFI_FIRMWARE_VOLUME2_PROTOCOL is done.
600 @param Event The event that occured
601 @param Context For EFI compatiblity. Not used.
614 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
615 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
617 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
619 // Examine all new handles
623 // Get the next handle
625 BufferSize
= sizeof (Handle
);
626 Status
= CoreLocateHandle (
629 gEfiFwVolBlockNotifyReg
,
635 // If not found, we're done
637 if (EFI_NOT_FOUND
== Status
) {
641 if (EFI_ERROR (Status
)) {
646 // Get the FirmwareVolumeBlock protocol on that handle
648 Status
= CoreHandleProtocol (Handle
, &gEfiFirmwareVolumeBlockProtocolGuid
, (VOID
**)&Fvb
);
649 ASSERT_EFI_ERROR (Status
);
650 ASSERT (Fvb
!= NULL
);
653 // Make sure the Fv Header is O.K.
655 Status
= GetFwVolHeader (Fvb
, &FwVolHeader
);
656 if (EFI_ERROR (Status
)) {
659 ASSERT (FwVolHeader
!= NULL
);
661 if (!VerifyFvHeaderChecksum (FwVolHeader
)) {
662 CoreFreePool (FwVolHeader
);
668 // Check to see that the file system is indeed formatted in a way we can
671 if ((!CompareGuid (&FwVolHeader
->FileSystemGuid
, &gEfiFirmwareFileSystem2Guid
)) &&
672 (!CompareGuid (&FwVolHeader
->FileSystemGuid
, &gEfiFirmwareFileSystem3Guid
))) {
677 // Check if there is an FV protocol already installed in that handle
679 Status
= CoreHandleProtocol (Handle
, &gEfiFirmwareVolume2ProtocolGuid
, (VOID
**)&Fv
);
680 if (!EFI_ERROR (Status
)) {
682 // Update Fv to use a new Fvb
684 FvDevice
= BASE_CR (Fv
, FV_DEVICE
, Fv
);
685 if (FvDevice
->Signature
== FV2_DEVICE_SIGNATURE
) {
687 // Only write into our device structure if it's our device structure
694 // No FwVol protocol on the handle so create a new one
696 FvDevice
= AllocateCopyPool (sizeof (FV_DEVICE
), &mFvDevice
);
697 if (FvDevice
== NULL
) {
702 FvDevice
->Handle
= Handle
;
703 FvDevice
->FwVolHeader
= FwVolHeader
;
704 FvDevice
->IsFfs3Fv
= CompareGuid (&FwVolHeader
->FileSystemGuid
, &gEfiFirmwareFileSystem3Guid
);
705 FvDevice
->Fv
.ParentHandle
= Fvb
->ParentHandle
;
707 if (Fvb
->ParentHandle
!= NULL
) {
709 // Inherit the authentication status from FVB.
711 FvDevice
->AuthenticationStatus
= GetFvbAuthenticationStatus (Fvb
);
714 if (!EFI_ERROR (FvCheck (FvDevice
))) {
716 // Install an New FV protocol on the existing handle
718 Status
= CoreInstallProtocolInterface (
720 &gEfiFirmwareVolume2ProtocolGuid
,
721 EFI_NATIVE_INTERFACE
,
724 ASSERT_EFI_ERROR (Status
);
727 // Free FvDevice Buffer for the corrupt FV image.
729 CoreFreePool (FvDevice
);
740 This routine is the driver initialization entry point. It registers
741 a notification function. This notification function are responsible
742 for building the FV stack dynamically.
744 @param ImageHandle The image handle.
745 @param SystemTable The system table.
747 @retval EFI_SUCCESS Function successfully returned.
753 IN EFI_HANDLE ImageHandle
,
754 IN EFI_SYSTEM_TABLE
*SystemTable
757 gEfiFwVolBlockEvent
= EfiCreateProtocolNotifyEvent (
758 &gEfiFirmwareVolumeBlockProtocolGuid
,
762 &gEfiFwVolBlockNotifyReg