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 FwVolHeader
= FvDevice
->FwVolHeader
;
327 Status
= Fvb
->GetAttributes (Fvb
, &FvbAttributes
);
328 if (EFI_ERROR (Status
)) {
333 // Size is the size of the FV minus the head. We have already allocated
334 // the header to check to make sure the volume is valid
336 Size
= (UINTN
)(FwVolHeader
->FvLength
- FwVolHeader
->HeaderLength
);
337 if ((FvbAttributes
& EFI_FVB2_MEMORY_MAPPED
) != 0) {
338 FvDevice
->IsMemoryMapped
= TRUE
;
340 Status
= Fvb
->GetPhysicalAddress (Fvb
, &PhysicalAddress
);
341 if (EFI_ERROR (Status
)) {
346 // Don't cache memory mapped FV really.
348 FvDevice
->CachedFv
= (UINT8
*) (UINTN
) (PhysicalAddress
+ FwVolHeader
->HeaderLength
);
350 FvDevice
->IsMemoryMapped
= FALSE
;
351 FvDevice
->CachedFv
= AllocatePool (Size
);
353 if (FvDevice
->CachedFv
== NULL
) {
354 return EFI_OUT_OF_RESOURCES
;
359 // Remember a pointer to the end fo the CachedFv
361 FvDevice
->EndOfCachedFv
= FvDevice
->CachedFv
+ Size
;
363 if (!FvDevice
->IsMemoryMapped
) {
365 // Copy FV minus header into memory using the block map we have all ready
368 BlockMap
= FwVolHeader
->BlockMap
;
369 CacheLocation
= FvDevice
->CachedFv
;
372 HeaderSize
= FwVolHeader
->HeaderLength
;
373 while ((BlockMap
->NumBlocks
!= 0) || (BlockMap
->Length
!= 0)) {
375 Size
= BlockMap
->Length
;
376 if (HeaderSize
> 0) {
380 for (; Index
< BlockMap
->NumBlocks
&& HeaderSize
>= BlockMap
->Length
; Index
++) {
381 HeaderSize
-= BlockMap
->Length
;
386 // Check whether FvHeader is crossing the multi block range.
388 if (Index
>= BlockMap
->NumBlocks
) {
391 } else if (HeaderSize
> 0) {
392 LbaOffset
= HeaderSize
;
393 Size
= BlockMap
->Length
- HeaderSize
;
401 for (; Index
< BlockMap
->NumBlocks
; Index
++) {
402 Status
= Fvb
->Read (Fvb
,
410 // Not check EFI_BAD_BUFFER_SIZE, for Size = BlockMap->Length
412 if (EFI_ERROR (Status
)) {
417 CacheLocation
+= Size
;
420 // After we skip Fv Header always read from start of block
423 Size
= BlockMap
->Length
;
431 // Scan to check the free space & File list
433 if ((FvbAttributes
& EFI_FVB2_ERASE_POLARITY
) != 0) {
434 FvDevice
->ErasePolarity
= 1;
436 FvDevice
->ErasePolarity
= 0;
441 // go through the whole FV cache, check the consistence of the FV.
442 // Make a linked list of all the Ffs file headers
444 Status
= EFI_SUCCESS
;
445 InitializeListHead (&FvDevice
->FfsFileListHeader
);
450 if (FwVolHeader
->ExtHeaderOffset
!= 0) {
452 // Searching for files starts on an 8 byte aligned boundary after the end of the Extended Header if it exists.
454 FwVolExtHeader
= (EFI_FIRMWARE_VOLUME_EXT_HEADER
*) (FvDevice
->CachedFv
+ (FwVolHeader
->ExtHeaderOffset
- FwVolHeader
->HeaderLength
));
455 FfsHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FwVolExtHeader
+ FwVolExtHeader
->ExtHeaderSize
);
456 FfsHeader
= (EFI_FFS_FILE_HEADER
*) ALIGN_POINTER (FfsHeader
, 8);
458 FfsHeader
= (EFI_FFS_FILE_HEADER
*) (FvDevice
->CachedFv
);
460 TopFvAddress
= FvDevice
->EndOfCachedFv
;
461 while ((UINT8
*) FfsHeader
< TopFvAddress
) {
463 TestLength
= TopFvAddress
- ((UINT8
*) FfsHeader
);
464 if (TestLength
> sizeof (EFI_FFS_FILE_HEADER
)) {
465 TestLength
= sizeof (EFI_FFS_FILE_HEADER
);
468 if (IsBufferErased (FvDevice
->ErasePolarity
, FfsHeader
, TestLength
)) {
470 // We have found the free space so we are done!
475 if (!IsValidFfsHeader (FvDevice
->ErasePolarity
, FfsHeader
, &FileState
)) {
476 if ((FileState
== EFI_FILE_HEADER_INVALID
) ||
477 (FileState
== EFI_FILE_HEADER_CONSTRUCTION
)) {
478 if (IS_FFS_FILE2 (FfsHeader
)) {
479 if (!FvDevice
->IsFfs3Fv
) {
480 DEBUG ((EFI_D_ERROR
, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FfsHeader
->Name
));
482 FfsHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FfsHeader
+ sizeof (EFI_FFS_FILE_HEADER2
));
484 FfsHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FfsHeader
+ sizeof (EFI_FFS_FILE_HEADER
));
489 // File system is corrputed
491 Status
= EFI_VOLUME_CORRUPTED
;
496 if (!IsValidFfsFile (FvDevice
->ErasePolarity
, FfsHeader
)) {
498 // File system is corrupted
500 Status
= EFI_VOLUME_CORRUPTED
;
504 if (IS_FFS_FILE2 (FfsHeader
)) {
505 ASSERT (FFS_FILE2_SIZE (FfsHeader
) > 0x00FFFFFF);
506 if (!FvDevice
->IsFfs3Fv
) {
507 DEBUG ((EFI_D_ERROR
, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FfsHeader
->Name
));
508 FfsHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FfsHeader
+ FFS_FILE2_SIZE (FfsHeader
));
510 // Adjust pointer to the next 8-byte aligned boundry.
512 FfsHeader
= (EFI_FFS_FILE_HEADER
*) (((UINTN
) FfsHeader
+ 7) & ~0x07);
517 FileState
= GetFileState (FvDevice
->ErasePolarity
, FfsHeader
);
520 // check for non-deleted file
522 if (FileState
!= EFI_FILE_DELETED
) {
524 // Create a FFS list entry for each non-deleted file
526 FfsFileEntry
= AllocateZeroPool (sizeof (FFS_FILE_LIST_ENTRY
));
527 if (FfsFileEntry
== NULL
) {
528 Status
= EFI_OUT_OF_RESOURCES
;
532 FfsFileEntry
->FfsHeader
= FfsHeader
;
533 InsertTailList (&FvDevice
->FfsFileListHeader
, &FfsFileEntry
->Link
);
536 if (IS_FFS_FILE2 (FfsHeader
)) {
537 FfsHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FfsHeader
+ FFS_FILE2_SIZE (FfsHeader
));
539 FfsHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FfsHeader
+ FFS_FILE_SIZE (FfsHeader
));
543 // Adjust pointer to the next 8-byte aligned boundry.
545 FfsHeader
= (EFI_FFS_FILE_HEADER
*)(((UINTN
)FfsHeader
+ 7) & ~0x07);
550 if (EFI_ERROR (Status
)) {
551 FreeFvDeviceResource (FvDevice
);
560 This notification function is invoked when an instance of the
561 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL is produced. It layers an instance of the
562 EFI_FIRMWARE_VOLUME2_PROTOCOL on the same handle. This is the function where
563 the actual initialization of the EFI_FIRMWARE_VOLUME2_PROTOCOL is done.
565 @param Event The event that occured
566 @param Context For EFI compatiblity. Not used.
579 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
580 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
582 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
584 // Examine all new handles
588 // Get the next handle
590 BufferSize
= sizeof (Handle
);
591 Status
= CoreLocateHandle (
594 gEfiFwVolBlockNotifyReg
,
600 // If not found, we're done
602 if (EFI_NOT_FOUND
== Status
) {
606 if (EFI_ERROR (Status
)) {
611 // Get the FirmwareVolumeBlock protocol on that handle
613 Status
= CoreHandleProtocol (Handle
, &gEfiFirmwareVolumeBlockProtocolGuid
, (VOID
**)&Fvb
);
614 ASSERT_EFI_ERROR (Status
);
615 ASSERT (Fvb
!= NULL
);
618 // Make sure the Fv Header is O.K.
620 Status
= GetFwVolHeader (Fvb
, &FwVolHeader
);
621 if (EFI_ERROR (Status
)) {
624 ASSERT (FwVolHeader
!= NULL
);
626 if (!VerifyFvHeaderChecksum (FwVolHeader
)) {
627 CoreFreePool (FwVolHeader
);
633 // Check to see that the file system is indeed formatted in a way we can
636 if ((!CompareGuid (&FwVolHeader
->FileSystemGuid
, &gEfiFirmwareFileSystem2Guid
)) &&
637 (!CompareGuid (&FwVolHeader
->FileSystemGuid
, &gEfiFirmwareFileSystem3Guid
))) {
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
659 // No FwVol protocol on the handle so create a new one
661 FvDevice
= AllocateCopyPool (sizeof (FV_DEVICE
), &mFvDevice
);
662 if (FvDevice
== NULL
) {
667 FvDevice
->Handle
= Handle
;
668 FvDevice
->FwVolHeader
= FwVolHeader
;
669 FvDevice
->IsFfs3Fv
= CompareGuid (&FwVolHeader
->FileSystemGuid
, &gEfiFirmwareFileSystem3Guid
);
670 FvDevice
->Fv
.ParentHandle
= Fvb
->ParentHandle
;
672 if (Fvb
->ParentHandle
!= NULL
) {
674 // Inherit the authentication status from FVB.
676 FvDevice
->AuthenticationStatus
= GetFvbAuthenticationStatus (Fvb
);
679 if (!EFI_ERROR (FvCheck (FvDevice
))) {
681 // Install an New FV protocol on the existing handle
683 Status
= CoreInstallProtocolInterface (
685 &gEfiFirmwareVolume2ProtocolGuid
,
686 EFI_NATIVE_INTERFACE
,
689 ASSERT_EFI_ERROR (Status
);
692 // Free FvDevice Buffer for the corrupt FV image.
694 CoreFreePool (FvDevice
);
705 This routine is the driver initialization entry point. It registers
706 a notification function. This notification function are responsible
707 for building the FV stack dynamically.
709 @param ImageHandle The image handle.
710 @param SystemTable The system table.
712 @retval EFI_SUCCESS Function successfully returned.
718 IN EFI_HANDLE ImageHandle
,
719 IN EFI_SYSTEM_TABLE
*SystemTable
722 gEfiFwVolBlockEvent
= EfiCreateProtocolNotifyEvent (
723 &gEfiFirmwareVolumeBlockProtocolGuid
,
727 &gEfiFwVolBlockNotifyReg