3 Firmware File System driver that produce full Firmware Volume2 protocol.
4 Layers on top of Firmware Block protocol to produce a file abstraction
7 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
9 This program and the accompanying materials
10 are licensed and made available under the terms and conditions
11 of the BSD License which accompanies this distribution. The
12 full text of the license may be found at
13 http://opensource.org/licenses/bsd-license.php
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
16 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 #include "FwVolDriver.h"
22 #define KEYSIZE sizeof (UINTN)
25 Given the supplied FW_VOL_BLOCK_PROTOCOL, allocate a buffer for output and
26 copy the real length volume header into it.
28 @param Fvb The FW_VOL_BLOCK_PROTOCOL instance from which to
29 read the volume header
30 @param FwVolHeader Pointer to pointer to allocated buffer in which
31 the volume header is returned.
33 @retval EFI_OUT_OF_RESOURCES No enough buffer could be allocated.
34 @retval EFI_SUCCESS Successfully read volume header to the allocated
36 @retval EFI_ACCESS_DENIED Read status of FV is not enabled.
40 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
41 OUT EFI_FIRMWARE_VOLUME_HEADER
**FwVolHeader
45 EFI_FIRMWARE_VOLUME_HEADER TempFvh
;
46 EFI_FVB_ATTRIBUTES_2 FvbAttributes
;
48 EFI_PHYSICAL_ADDRESS BaseAddress
;
51 // Determine the real length of FV header
53 Status
= Fvb
->GetAttributes (
57 if (EFI_ERROR (Status
)) {
61 if ((FvbAttributes
& EFI_FVB2_READ_STATUS
) == 0) {
62 return EFI_ACCESS_DENIED
;
66 // Just avoid compiling warning
69 FvhLength
= sizeof (EFI_FIRMWARE_VOLUME_HEADER
);
72 // memory-mapped FV and non memory-mapped has different ways to read
74 if ((FvbAttributes
& EFI_FVB2_MEMORY_MAPPED
) != 0) {
75 Status
= Fvb
->GetPhysicalAddress (
79 if (EFI_ERROR (Status
)) {
82 CopyMem (&TempFvh
, (VOID
*) (UINTN
) BaseAddress
, FvhLength
);
93 *FwVolHeader
= AllocatePool (TempFvh
.HeaderLength
);
94 if (*FwVolHeader
== NULL
) {
95 return EFI_OUT_OF_RESOURCES
;
98 // Read the whole header
100 if ((FvbAttributes
& EFI_FVB2_MEMORY_MAPPED
) != 0) {
101 CopyMem (*FwVolHeader
, (VOID
*) (UINTN
) BaseAddress
, TempFvh
.HeaderLength
);
104 // Assumed the first block is bigger than the length of Fv headder
106 FvhLength
= TempFvh
.HeaderLength
;
112 (UINT8
*) *FwVolHeader
115 // Check whether Read successes.
117 if (EFI_ERROR (Status
)) {
118 FreePool (*FwVolHeader
);
128 Free FvDevice resource when error happens.
130 @param FvDevice Pointer to the FvDevice to be freed.
133 FreeFvDeviceResource (
134 IN FV_DEVICE
*FvDevice
138 FREE_SPACE_ENTRY
*FreeSpaceEntry
;
139 FFS_FILE_LIST_ENTRY
*FfsFileEntry
;
140 LIST_ENTRY
*NextEntry
;
145 LbaEntry
= (LBA_ENTRY
*) FvDevice
->LbaHeader
.ForwardLink
;
146 while (&LbaEntry
->Link
!= &FvDevice
->LbaHeader
) {
147 NextEntry
= (&LbaEntry
->Link
)->ForwardLink
;
149 LbaEntry
= (LBA_ENTRY
*) NextEntry
;
152 // Free File List Entry
154 FfsFileEntry
= (FFS_FILE_LIST_ENTRY
*) FvDevice
->FfsFileListHeader
.ForwardLink
;
155 while (&FfsFileEntry
->Link
!= &FvDevice
->FfsFileListHeader
) {
156 NextEntry
= (&FfsFileEntry
->Link
)->ForwardLink
;
157 FreePool (FfsFileEntry
);
158 FfsFileEntry
= (FFS_FILE_LIST_ENTRY
*) NextEntry
;
163 FreeSpaceEntry
= (FREE_SPACE_ENTRY
*) FvDevice
->FreeSpaceHeader
.ForwardLink
;
164 while (&FreeSpaceEntry
->Link
!= &FvDevice
->FreeSpaceHeader
) {
165 NextEntry
= (&FreeSpaceEntry
->Link
)->ForwardLink
;
166 FreePool (FreeSpaceEntry
);
167 FreeSpaceEntry
= (FREE_SPACE_ENTRY
*) NextEntry
;
172 FreePool ((UINT8
*) (UINTN
) FvDevice
->CachedFv
);
178 Check if an FV is consistent and allocate cache for it.
180 @param FvDevice A pointer to the FvDevice to be checked.
182 @retval EFI_OUT_OF_RESOURCES No enough buffer could be allocated.
183 @retval EFI_VOLUME_CORRUPTED File system is corrupted.
184 @retval EFI_SUCCESS FV is consistent and cache is allocated.
189 IN FV_DEVICE
*FvDevice
193 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
194 EFI_FVB_ATTRIBUTES_2 FvbAttributes
;
195 EFI_FV_BLOCK_MAP_ENTRY
*BlockMap
;
196 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
197 EFI_FIRMWARE_VOLUME_EXT_HEADER
*FwVolExtHeader
;
200 FREE_SPACE_ENTRY
*FreeSpaceEntry
;
201 FFS_FILE_LIST_ENTRY
*FfsFileEntry
;
210 EFI_FFS_FILE_STATE FileState
;
213 EFI_PHYSICAL_ADDRESS BaseAddress
;
217 Status
= Fvb
->GetAttributes (Fvb
, &FvbAttributes
);
218 if (EFI_ERROR (Status
)) {
222 InitializeListHead (&FvDevice
->LbaHeader
);
223 InitializeListHead (&FvDevice
->FreeSpaceHeader
);
224 InitializeListHead (&FvDevice
->FfsFileListHeader
);
227 Status
= GetFwVolHeader (Fvb
, &FwVolHeader
);
228 if (EFI_ERROR (Status
)) {
231 ASSERT (FwVolHeader
!= NULL
);
233 FvDevice
->IsFfs3Fv
= CompareGuid (&FwVolHeader
->FileSystemGuid
, &gEfiFirmwareFileSystem3Guid
);
236 // Double Check firmware volume header here
238 if (!VerifyFvHeaderChecksum (FwVolHeader
)) {
239 FreePool (FwVolHeader
);
240 return EFI_VOLUME_CORRUPTED
;
243 BlockMap
= FwVolHeader
->BlockMap
;
246 // FwVolHeader->FvLength is the whole FV length including FV header
248 FwCache
= AllocateZeroPool ((UINTN
) FwVolHeader
->FvLength
);
249 if (FwCache
== NULL
) {
250 FreePool (FwVolHeader
);
251 return EFI_OUT_OF_RESOURCES
;
254 FvDevice
->CachedFv
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) FwCache
;
263 if ((FvbAttributes
& EFI_FVB2_MEMORY_MAPPED
) != 0) {
265 // Get volume base address
267 Status
= Fvb
->GetPhysicalAddress (Fvb
, &BaseAddress
);
268 if (EFI_ERROR (Status
)) {
269 FreePool (FwVolHeader
);
273 Ptr
= (UINT8
*) ((UINTN
) BaseAddress
);
275 DEBUG((EFI_D_INFO
, "Fv Base Address is 0x%LX\n", BaseAddress
));
278 // Copy whole FV into the memory
280 while ((BlockMap
->NumBlocks
!= 0) || (BlockMap
->Length
!= 0)) {
282 for (Index
= 0; Index
< BlockMap
->NumBlocks
; Index
++) {
283 LbaEntry
= AllocatePool (sizeof (LBA_ENTRY
));
284 if (LbaEntry
== NULL
) {
285 FreePool (FwVolHeader
);
286 FreeFvDeviceResource (FvDevice
);
287 return EFI_OUT_OF_RESOURCES
;
290 LbaEntry
->LbaIndex
= LbaIndex
;
291 LbaEntry
->StartingAddress
= LbaStart
;
292 LbaEntry
->BlockLength
= BlockMap
->Length
;
295 // Copy each LBA into memory
297 if ((FvbAttributes
& EFI_FVB2_MEMORY_MAPPED
) != 0) {
299 CopyMem (LbaStart
, Ptr
, BlockMap
->Length
);
300 Ptr
+= BlockMap
->Length
;
304 Size
= BlockMap
->Length
;
313 // Not check EFI_BAD_BUFFER_SIZE, for Size = BlockMap->Length
315 if (EFI_ERROR (Status
)) {
316 FreePool (FwVolHeader
);
317 FreeFvDeviceResource (FvDevice
);
324 LbaStart
+= BlockMap
->Length
;
326 InsertTailList (&FvDevice
->LbaHeader
, &LbaEntry
->Link
);
332 FvDevice
->FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) FwCache
;
335 // it is not used any more, so free FwVolHeader
337 FreePool (FwVolHeader
);
340 // Scan to check the free space & File list
342 if ((FvbAttributes
& EFI_FVB2_ERASE_POLARITY
) != 0) {
348 FvDevice
->ErasePolarity
= ErasePolarity
;
351 // go through the whole FV cache, check the consistence of the FV
353 if (FvDevice
->FwVolHeader
->ExtHeaderOffset
!= 0) {
355 // Searching for files starts on an 8 byte aligned boundary after the end of the Extended Header if it exists.
357 FwVolExtHeader
= (EFI_FIRMWARE_VOLUME_EXT_HEADER
*) (UINTN
) (FvDevice
->CachedFv
+ FvDevice
->FwVolHeader
->ExtHeaderOffset
);
358 Ptr
= (UINT8
*) FwVolExtHeader
+ FwVolExtHeader
->ExtHeaderSize
;
359 Ptr
= (UINT8
*) ALIGN_POINTER (Ptr
, 8);
361 Ptr
= (UINT8
*) (UINTN
) (FvDevice
->CachedFv
+ FvDevice
->FwVolHeader
->HeaderLength
);
363 TopFvAddress
= (UINT8
*) (UINTN
) (FvDevice
->CachedFv
+ FvDevice
->FwVolHeader
->FvLength
);
366 // Build FFS list & Free Space List here
368 while (Ptr
< TopFvAddress
) {
369 TestLength
= TopFvAddress
- Ptr
;
371 if (TestLength
> sizeof (EFI_FFS_FILE_HEADER
)) {
372 TestLength
= sizeof (EFI_FFS_FILE_HEADER
);
375 if (IsBufferErased (ErasePolarity
, Ptr
, TestLength
)) {
377 // We found free space
383 TestLength
= TopFvAddress
- Ptr
;
385 if (TestLength
> sizeof (EFI_FFS_FILE_HEADER
)) {
386 TestLength
= sizeof (EFI_FFS_FILE_HEADER
);
389 if (!IsBufferErased (ErasePolarity
, Ptr
, TestLength
)) {
393 FreeSize
+= TestLength
;
395 } while (Ptr
< TopFvAddress
);
397 FreeSpaceEntry
= AllocateZeroPool (sizeof (FREE_SPACE_ENTRY
));
398 if (FreeSpaceEntry
== NULL
) {
399 FreeFvDeviceResource (FvDevice
);
400 return EFI_OUT_OF_RESOURCES
;
403 // Create a Free space entry
405 FreeSpaceEntry
->StartingAddress
= FreeStart
;
406 FreeSpaceEntry
->Length
= FreeSize
;
407 InsertTailList (&FvDevice
->FreeSpaceHeader
, &FreeSpaceEntry
->Link
);
411 // double check boundry
413 if (TestLength
< sizeof (EFI_FFS_FILE_HEADER
)) {
417 if (!IsValidFFSHeader (
418 FvDevice
->ErasePolarity
,
419 (EFI_FFS_FILE_HEADER
*) Ptr
421 FileState
= GetFileState (
422 FvDevice
->ErasePolarity
,
423 (EFI_FFS_FILE_HEADER
*) Ptr
425 if ((FileState
== EFI_FILE_HEADER_INVALID
) || (FileState
== EFI_FILE_HEADER_CONSTRUCTION
)) {
426 if (IS_FFS_FILE2 (Ptr
)) {
427 if (!FvDevice
->IsFfs3Fv
) {
428 DEBUG ((EFI_D_ERROR
, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &((EFI_FFS_FILE_HEADER
*) Ptr
)->Name
));
430 Ptr
= Ptr
+ sizeof (EFI_FFS_FILE_HEADER2
);
432 Ptr
= Ptr
+ sizeof (EFI_FFS_FILE_HEADER
);
439 // File system is corrputed, return
441 FreeFvDeviceResource (FvDevice
);
442 return EFI_VOLUME_CORRUPTED
;
446 if (IS_FFS_FILE2 (Ptr
)) {
447 ASSERT (FFS_FILE2_SIZE (Ptr
) > 0x00FFFFFF);
448 if (!FvDevice
->IsFfs3Fv
) {
449 DEBUG ((EFI_D_ERROR
, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &((EFI_FFS_FILE_HEADER
*) Ptr
)->Name
));
450 Ptr
= Ptr
+ FFS_FILE2_SIZE (Ptr
);
452 // Adjust Ptr to the next 8-byte aligned boundry.
454 while (((UINTN
) Ptr
& 0x07) != 0) {
461 if (IsValidFFSFile (FvDevice
, (EFI_FFS_FILE_HEADER
*) Ptr
)) {
462 FileState
= GetFileState (
463 FvDevice
->ErasePolarity
,
464 (EFI_FFS_FILE_HEADER
*) Ptr
468 // check for non-deleted file
470 if (FileState
!= EFI_FILE_DELETED
) {
472 // Create a FFS list entry for each non-deleted file
474 FfsFileEntry
= AllocateZeroPool (sizeof (FFS_FILE_LIST_ENTRY
));
475 if (FfsFileEntry
== NULL
) {
476 FreeFvDeviceResource (FvDevice
);
477 return EFI_OUT_OF_RESOURCES
;
480 FfsFileEntry
->FfsHeader
= Ptr
;
481 InsertTailList (&FvDevice
->FfsFileListHeader
, &FfsFileEntry
->Link
);
484 if (IS_FFS_FILE2 (Ptr
)) {
485 Ptr
= Ptr
+ FFS_FILE2_SIZE (Ptr
);
487 Ptr
= Ptr
+ FFS_FILE_SIZE (Ptr
);
491 // Adjust Ptr to the next 8-byte aligned boundry.
493 while (((UINTN
) Ptr
& 0x07) != 0) {
498 // File system is corrupted, return
500 FreeFvDeviceResource (FvDevice
);
501 return EFI_VOLUME_CORRUPTED
;
505 FvDevice
->CurrentFfsFile
= NULL
;
511 Entry point function does install/reinstall FV2 protocol with full functionality.
513 @param ImageHandle A handle for the image that is initializing this driver
514 @param SystemTable A pointer to the EFI system table
516 @retval EFI_SUCCESS At least one Fv protocol install/reinstall successfully.
517 @retval EFI_NOT_FOUND No FV protocol install/reinstall successfully.
522 IN EFI_HANDLE ImageHandle
,
523 IN EFI_SYSTEM_TABLE
*SystemTable
527 EFI_HANDLE
*HandleBuffer
;
530 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
531 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
533 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
537 DEBUG ((EFI_D_INFO
, "=========FwVol writable driver installed\n"));
540 // Locate all handles of Fvb protocol
542 Status
= gBS
->LocateHandleBuffer (
544 &gEfiFirmwareVolumeBlockProtocolGuid
,
549 if (EFI_ERROR (Status
)) {
550 return EFI_NOT_FOUND
;
553 for (Index
= 0; Index
< HandleCount
; Index
+= 1) {
554 Status
= gBS
->HandleProtocol (
556 &gEfiFirmwareVolumeBlockProtocolGuid
,
559 if (EFI_ERROR (Status
)) {
564 Status
= GetFwVolHeader (Fvb
, &FwVolHeader
);
565 if (EFI_ERROR (Status
)) {
568 ASSERT (FwVolHeader
!= NULL
);
570 // Check to see that the file system is indeed formatted in a way we can
573 if ((!CompareGuid (&FwVolHeader
->FileSystemGuid
, &gEfiFirmwareFileSystem2Guid
)) &&
574 (!CompareGuid (&FwVolHeader
->FileSystemGuid
, &gEfiFirmwareFileSystem3Guid
))) {
575 FreePool (FwVolHeader
);
578 FreePool (FwVolHeader
);
582 // Check if there is an FV protocol already installed in that handle
584 Status
= gBS
->HandleProtocol (
586 &gEfiFirmwareVolume2ProtocolGuid
,
589 if (!EFI_ERROR (Status
)) {
593 // FwVol protocol on the handle so create a new one
595 FvDevice
= AllocateZeroPool (sizeof (FV_DEVICE
));
596 if (FvDevice
== NULL
) {
600 FvDevice
->Signature
= FV_DEVICE_SIGNATURE
;
604 // Firmware Volume Protocol interface
606 FvDevice
->Fv
.GetVolumeAttributes
= FvGetVolumeAttributes
;
607 FvDevice
->Fv
.SetVolumeAttributes
= FvSetVolumeAttributes
;
608 FvDevice
->Fv
.ReadFile
= FvReadFile
;
609 FvDevice
->Fv
.ReadSection
= FvReadFileSection
;
610 FvDevice
->Fv
.WriteFile
= FvWriteFile
;
611 FvDevice
->Fv
.GetNextFile
= FvGetNextFile
;
612 FvDevice
->Fv
.KeySize
= KEYSIZE
;
613 FvDevice
->Fv
.GetInfo
= FvGetVolumeInfo
;
614 FvDevice
->Fv
.SetInfo
= FvSetVolumeInfo
;
616 Status
= FvCheck (FvDevice
);
617 if (EFI_ERROR (Status
)) {
619 // The file system is not consistence
627 // Reinstall an New FV protocol
629 // FvDevice = FV_DEVICE_FROM_THIS (Fv);
630 // FvDevice->Fvb = Fvb;
631 // FreeFvDeviceResource (FvDevice);
633 Status
= gBS
->ReinstallProtocolInterface (
635 &gEfiFirmwareVolume2ProtocolGuid
,
639 if (!EFI_ERROR (Status
)) {
645 DEBUG ((EFI_D_INFO
, "Reinstall FV protocol as writable - %r\n", Status
));
646 ASSERT_EFI_ERROR (Status
);
649 // Install an New FV protocol
651 Status
= gBS
->InstallProtocolInterface (
653 &gEfiFirmwareVolume2ProtocolGuid
,
654 EFI_NATIVE_INTERFACE
,
657 if (!EFI_ERROR (Status
)) {
663 DEBUG ((EFI_D_INFO
, "Install FV protocol as writable - %r\n", Status
));
664 ASSERT_EFI_ERROR (Status
);
670 // As long as one Fv protocol install/reinstall successfully,
671 // success should return to ensure this image will be not unloaded.
672 // Otherwise, new Fv protocols are corrupted by other loaded driver.
679 // No FV protocol install/reinstall successfully.
680 // EFI_NOT_FOUND should return to ensure this image will be unloaded.
682 return EFI_NOT_FOUND
;