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 - 2012, 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
,
55 // FFS helper functions
58 Read data from Firmware Block by FVB protocol Read.
59 The data may cross the multi block ranges.
61 @param Fvb The FW_VOL_BLOCK_PROTOCOL instance from which to read data.
62 @param StartLba Pointer to StartLba.
63 On input, the start logical block index from which to read.
64 On output,the end logical block index after reading.
65 @param Offset Pointer to Offset
66 On input, offset into the block at which to begin reading.
67 On output, offset into the end block after reading.
68 @param DataSize Size of data to be read.
69 @param Data Pointer to Buffer that the data will be read into.
71 @retval EFI_SUCCESS Successfully read data from firmware block.
76 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
77 IN OUT EFI_LBA
*StartLba
,
90 // Try read data in current block
93 ReadDataSize
= DataSize
;
94 Status
= Fvb
->Read (Fvb
, *StartLba
, *Offset
, &ReadDataSize
, Data
);
95 if (Status
== EFI_SUCCESS
) {
98 } else if (Status
!= EFI_BAD_BUFFER_SIZE
) {
100 // other error will direct return
106 // Data crosses the blocks, read data from next block
108 DataSize
-= ReadDataSize
;
109 Data
+= ReadDataSize
;
110 *StartLba
= *StartLba
+ 1;
111 while (DataSize
> 0) {
112 Status
= Fvb
->GetBlockSize (Fvb
, *StartLba
, &BlockSize
, &NumberOfBlocks
);
113 if (EFI_ERROR (Status
)) {
118 // Read data from the crossing blocks
121 while (BlockIndex
< NumberOfBlocks
&& DataSize
>= BlockSize
) {
122 Status
= Fvb
->Read (Fvb
, *StartLba
+ BlockIndex
, 0, &BlockSize
, Data
);
123 if (EFI_ERROR (Status
)) {
127 DataSize
-= BlockSize
;
132 // Data doesn't exceed the current block range.
134 if (DataSize
< BlockSize
) {
139 // Data must be got from the next block range.
141 *StartLba
+= NumberOfBlocks
;
145 // read the remaining data
148 Status
= Fvb
->Read (Fvb
, *StartLba
+ BlockIndex
, 0, &DataSize
, Data
);
149 if (EFI_ERROR (Status
)) {
155 // Update Lba and Offset used by the following read.
157 *StartLba
+= BlockIndex
;
164 Given the supplied FW_VOL_BLOCK_PROTOCOL, allocate a buffer for output and
165 copy the real length volume header into it.
167 @param Fvb The FW_VOL_BLOCK_PROTOCOL instance from which to
168 read the volume header
169 @param FwVolHeader Pointer to pointer to allocated buffer in which
170 the volume header is returned.
172 @retval EFI_OUT_OF_RESOURCES No enough buffer could be allocated.
173 @retval EFI_SUCCESS Successfully read volume header to the allocated
179 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
180 OUT EFI_FIRMWARE_VOLUME_HEADER
**FwVolHeader
184 EFI_FIRMWARE_VOLUME_HEADER TempFvh
;
191 // Read the standard FV header
195 FvhLength
= sizeof (EFI_FIRMWARE_VOLUME_HEADER
);
196 Status
= ReadFvbData (Fvb
, &StartLba
, &Offset
, FvhLength
, (UINT8
*)&TempFvh
);
197 if (EFI_ERROR (Status
)) {
202 // Allocate a buffer for the caller
204 *FwVolHeader
= AllocatePool (TempFvh
.HeaderLength
);
205 if (*FwVolHeader
== NULL
) {
206 return EFI_OUT_OF_RESOURCES
;
210 // Copy the standard header into the buffer
212 CopyMem (*FwVolHeader
, &TempFvh
, sizeof (EFI_FIRMWARE_VOLUME_HEADER
));
215 // Read the rest of the header
217 FvhLength
= TempFvh
.HeaderLength
- sizeof (EFI_FIRMWARE_VOLUME_HEADER
);
218 Buffer
= (UINT8
*)*FwVolHeader
+ sizeof (EFI_FIRMWARE_VOLUME_HEADER
);
219 Status
= ReadFvbData (Fvb
, &StartLba
, &Offset
, FvhLength
, Buffer
);
220 if (EFI_ERROR (Status
)) {
222 // Read failed so free buffer
224 CoreFreePool (*FwVolHeader
);
233 Free FvDevice resource when error happens
235 @param FvDevice pointer to the FvDevice to be freed.
239 FreeFvDeviceResource (
240 IN FV_DEVICE
*FvDevice
243 FFS_FILE_LIST_ENTRY
*FfsFileEntry
;
244 LIST_ENTRY
*NextEntry
;
247 // Free File List Entry
249 FfsFileEntry
= (FFS_FILE_LIST_ENTRY
*)FvDevice
->FfsFileListHeader
.ForwardLink
;
250 while (&FfsFileEntry
->Link
!= &FvDevice
->FfsFileListHeader
) {
251 NextEntry
= (&FfsFileEntry
->Link
)->ForwardLink
;
253 if (FfsFileEntry
->StreamHandle
!= 0) {
255 // Close stream and free resources from SEP
257 CloseSectionStream (FfsFileEntry
->StreamHandle
);
260 CoreFreePool (FfsFileEntry
);
262 FfsFileEntry
= (FFS_FILE_LIST_ENTRY
*) NextEntry
;
269 CoreFreePool (FvDevice
->CachedFv
);
272 // Free Volume Header
274 CoreFreePool (FvDevice
->FwVolHeader
);
282 Check if an FV is consistent and allocate cache for it.
284 @param FvDevice A pointer to the FvDevice to be checked.
286 @retval EFI_OUT_OF_RESOURCES No enough buffer could be allocated.
287 @retval EFI_SUCCESS FV is consistent and cache is allocated.
288 @retval EFI_VOLUME_CORRUPTED File system is corrupted.
293 IN OUT FV_DEVICE
*FvDevice
297 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
298 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
299 EFI_FIRMWARE_VOLUME_EXT_HEADER
*FwVolExtHeader
;
300 EFI_FVB_ATTRIBUTES_2 FvbAttributes
;
301 EFI_FV_BLOCK_MAP_ENTRY
*BlockMap
;
302 FFS_FILE_LIST_ENTRY
*FfsFileEntry
;
303 EFI_FFS_FILE_HEADER
*FfsHeader
;
304 UINT8
*CacheLocation
;
310 EFI_FFS_FILE_STATE FileState
;
316 FwVolHeader
= FvDevice
->FwVolHeader
;
318 Status
= Fvb
->GetAttributes (Fvb
, &FvbAttributes
);
319 if (EFI_ERROR (Status
)) {
324 // Size is the size of the FV minus the head. We have already allocated
325 // the header to check to make sure the volume is valid
327 Size
= (UINTN
)(FwVolHeader
->FvLength
- FwVolHeader
->HeaderLength
);
328 FvDevice
->CachedFv
= AllocatePool (Size
);
330 if (FvDevice
->CachedFv
== NULL
) {
331 return EFI_OUT_OF_RESOURCES
;
335 // Remember a pointer to the end fo the CachedFv
337 FvDevice
->EndOfCachedFv
= FvDevice
->CachedFv
+ Size
;
340 // Copy FV minus header into memory using the block map we have all ready
343 BlockMap
= FwVolHeader
->BlockMap
;
344 CacheLocation
= FvDevice
->CachedFv
;
347 HeaderSize
= FwVolHeader
->HeaderLength
;
348 while ((BlockMap
->NumBlocks
!= 0) || (BlockMap
->Length
!= 0)) {
350 Size
= BlockMap
->Length
;
351 if (HeaderSize
> 0) {
355 for (; Index
< BlockMap
->NumBlocks
&& HeaderSize
>= BlockMap
->Length
; Index
++) {
356 HeaderSize
-= BlockMap
->Length
;
361 // Check whether FvHeader is crossing the multi block range.
363 if (Index
>= BlockMap
->NumBlocks
) {
366 } else if (HeaderSize
> 0) {
367 LbaOffset
= HeaderSize
;
368 Size
= BlockMap
->Length
- HeaderSize
;
376 for (; Index
< BlockMap
->NumBlocks
; Index
++) {
377 Status
= Fvb
->Read (Fvb
,
385 // Not check EFI_BAD_BUFFER_SIZE, for Size = BlockMap->Length
387 if (EFI_ERROR (Status
)) {
392 CacheLocation
+= Size
;
395 // After we skip Fv Header always read from start of block
398 Size
= BlockMap
->Length
;
405 // Scan to check the free space & File list
407 if ((FvbAttributes
& EFI_FVB2_ERASE_POLARITY
) != 0) {
408 FvDevice
->ErasePolarity
= 1;
410 FvDevice
->ErasePolarity
= 0;
415 // go through the whole FV cache, check the consistence of the FV.
416 // Make a linked list of all the Ffs file headers
418 Status
= EFI_SUCCESS
;
419 InitializeListHead (&FvDevice
->FfsFileListHeader
);
424 if (FwVolHeader
->ExtHeaderOffset
!= 0) {
426 // Searching for files starts on an 8 byte aligned boundary after the end of the Extended Header if it exists.
428 FwVolExtHeader
= (EFI_FIRMWARE_VOLUME_EXT_HEADER
*) (FvDevice
->CachedFv
+ (FwVolHeader
->ExtHeaderOffset
- FwVolHeader
->HeaderLength
));
429 FfsHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FwVolExtHeader
+ FwVolExtHeader
->ExtHeaderSize
);
430 FfsHeader
= (EFI_FFS_FILE_HEADER
*) ALIGN_POINTER (FfsHeader
, 8);
432 FfsHeader
= (EFI_FFS_FILE_HEADER
*) (FvDevice
->CachedFv
);
434 TopFvAddress
= FvDevice
->EndOfCachedFv
;
435 while ((UINT8
*) FfsHeader
< TopFvAddress
) {
437 TestLength
= TopFvAddress
- ((UINT8
*) FfsHeader
);
438 if (TestLength
> sizeof (EFI_FFS_FILE_HEADER
)) {
439 TestLength
= sizeof (EFI_FFS_FILE_HEADER
);
442 if (IsBufferErased (FvDevice
->ErasePolarity
, FfsHeader
, TestLength
)) {
444 // We have found the free space so we are done!
449 if (!IsValidFfsHeader (FvDevice
->ErasePolarity
, FfsHeader
, &FileState
)) {
450 if ((FileState
== EFI_FILE_HEADER_INVALID
) ||
451 (FileState
== EFI_FILE_HEADER_CONSTRUCTION
)) {
452 if (IS_FFS_FILE2 (FfsHeader
)) {
453 if (!FvDevice
->IsFfs3Fv
) {
454 DEBUG ((EFI_D_ERROR
, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FfsHeader
->Name
));
456 FfsHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FfsHeader
+ sizeof (EFI_FFS_FILE_HEADER2
));
458 FfsHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FfsHeader
+ sizeof (EFI_FFS_FILE_HEADER
));
463 // File system is corrputed
465 Status
= EFI_VOLUME_CORRUPTED
;
470 if (!IsValidFfsFile (FvDevice
->ErasePolarity
, FfsHeader
)) {
472 // File system is corrupted
474 Status
= EFI_VOLUME_CORRUPTED
;
478 if (IS_FFS_FILE2 (FfsHeader
)) {
479 ASSERT (FFS_FILE2_SIZE (FfsHeader
) > 0x00FFFFFF);
480 if (!FvDevice
->IsFfs3Fv
) {
481 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
+ FFS_FILE2_SIZE (FfsHeader
));
484 // Adjust pointer to the next 8-byte aligned boundry.
486 FfsHeader
= (EFI_FFS_FILE_HEADER
*) (((UINTN
) FfsHeader
+ 7) & ~0x07);
491 FileState
= GetFileState (FvDevice
->ErasePolarity
, FfsHeader
);
494 // check for non-deleted file
496 if (FileState
!= EFI_FILE_DELETED
) {
498 // Create a FFS list entry for each non-deleted file
500 FfsFileEntry
= AllocateZeroPool (sizeof (FFS_FILE_LIST_ENTRY
));
501 if (FfsFileEntry
== NULL
) {
502 Status
= EFI_OUT_OF_RESOURCES
;
506 FfsFileEntry
->FfsHeader
= FfsHeader
;
507 InsertTailList (&FvDevice
->FfsFileListHeader
, &FfsFileEntry
->Link
);
510 if (IS_FFS_FILE2 (FfsHeader
)) {
511 FfsHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FfsHeader
+ FFS_FILE2_SIZE (FfsHeader
));
513 FfsHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FfsHeader
+ FFS_FILE_SIZE (FfsHeader
));
517 // Adjust pointer to the next 8-byte aligned boundry.
519 FfsHeader
= (EFI_FFS_FILE_HEADER
*)(((UINTN
)FfsHeader
+ 7) & ~0x07);
524 if (EFI_ERROR (Status
)) {
525 FreeFvDeviceResource (FvDevice
);
534 This notification function is invoked when an instance of the
535 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL is produced. It layers an instance of the
536 EFI_FIRMWARE_VOLUME2_PROTOCOL on the same handle. This is the function where
537 the actual initialization of the EFI_FIRMWARE_VOLUME2_PROTOCOL is done.
539 @param Event The event that occured
540 @param Context For EFI compatiblity. Not used.
553 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
554 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
556 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
558 // Examine all new handles
562 // Get the next handle
564 BufferSize
= sizeof (Handle
);
565 Status
= CoreLocateHandle (
568 gEfiFwVolBlockNotifyReg
,
574 // If not found, we're done
576 if (EFI_NOT_FOUND
== Status
) {
580 if (EFI_ERROR (Status
)) {
585 // Get the FirmwareVolumeBlock protocol on that handle
587 Status
= CoreHandleProtocol (Handle
, &gEfiFirmwareVolumeBlockProtocolGuid
, (VOID
**)&Fvb
);
588 ASSERT_EFI_ERROR (Status
);
589 ASSERT (Fvb
!= NULL
);
592 // Make sure the Fv Header is O.K.
594 Status
= GetFwVolHeader (Fvb
, &FwVolHeader
);
595 if (EFI_ERROR (Status
)) {
598 ASSERT (FwVolHeader
!= NULL
);
600 if (!VerifyFvHeaderChecksum (FwVolHeader
)) {
601 CoreFreePool (FwVolHeader
);
607 // Check to see that the file system is indeed formatted in a way we can
610 if ((!CompareGuid (&FwVolHeader
->FileSystemGuid
, &gEfiFirmwareFileSystem2Guid
)) &&
611 (!CompareGuid (&FwVolHeader
->FileSystemGuid
, &gEfiFirmwareFileSystem3Guid
))) {
616 // Check if there is an FV protocol already installed in that handle
618 Status
= CoreHandleProtocol (Handle
, &gEfiFirmwareVolume2ProtocolGuid
, (VOID
**)&Fv
);
619 if (!EFI_ERROR (Status
)) {
621 // Update Fv to use a new Fvb
623 FvDevice
= BASE_CR (Fv
, FV_DEVICE
, Fv
);
624 if (FvDevice
->Signature
== FV2_DEVICE_SIGNATURE
) {
626 // Only write into our device structure if it's our device structure
633 // No FwVol protocol on the handle so create a new one
635 FvDevice
= AllocateCopyPool (sizeof (FV_DEVICE
), &mFvDevice
);
636 if (FvDevice
== NULL
) {
641 FvDevice
->Handle
= Handle
;
642 FvDevice
->FwVolHeader
= FwVolHeader
;
643 FvDevice
->IsFfs3Fv
= CompareGuid (&FwVolHeader
->FileSystemGuid
, &gEfiFirmwareFileSystem3Guid
);
644 FvDevice
->Fv
.ParentHandle
= Fvb
->ParentHandle
;
646 if (Fvb
->ParentHandle
!= NULL
) {
648 // Inherit the authentication status from FVB.
650 FvDevice
->AuthenticationStatus
= GetFvbAuthenticationStatus (Fvb
);
653 if (!EFI_ERROR (FvCheck (FvDevice
))) {
655 // Install an New FV protocol on the existing handle
657 Status
= CoreInstallProtocolInterface (
659 &gEfiFirmwareVolume2ProtocolGuid
,
660 EFI_NATIVE_INTERFACE
,
663 ASSERT_EFI_ERROR (Status
);
666 // Free FvDevice Buffer for the corrupt FV image.
668 CoreFreePool (FvDevice
);
679 This routine is the driver initialization entry point. It registers
680 a notification function. This notification function are responsible
681 for building the FV stack dynamically.
683 @param ImageHandle The image handle.
684 @param SystemTable The system table.
686 @retval EFI_SUCCESS Function successfully returned.
692 IN EFI_HANDLE ImageHandle
,
693 IN EFI_SYSTEM_TABLE
*SystemTable
696 gEfiFwVolBlockEvent
= EfiCreateProtocolNotifyEvent (
697 &gEfiFirmwareVolumeBlockProtocolGuid
,
701 &gEfiFwVolBlockNotifyReg