]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/FwVol/FwVol.c
MdeModulePkg DxeCore: Don't cache memory mapped IO FV.
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / FwVol / FwVol.c
index f14cce05c7c820e1983c84df0409eda9fbe172fd..d3447a57ac2eb3c8486872947896e1646858c1ca 100644 (file)
@@ -1,10 +1,10 @@
 /** @file\r
   Firmware File System driver that produce Firmware Volume protocol.\r
-  Layers on top of Firmware Block protocol to produce a file abstraction \r
+  Layers on top of Firmware Block protocol to produce a file abstraction\r
   of FV based files.\r
 \r
-Copyright (c) 2006 - 2008, Intel Corporation. <BR>\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
 http://opensource.org/licenses/bsd-license.php\r
@@ -14,9 +14,9 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 **/\r
 \r
-#include <DxeMain.h>\r
+#include "DxeMain.h"\r
+#include "FwVolDriver.h"\r
 \r
-#define KEYSIZE       sizeof (UINTN)\r
 \r
 //\r
 // Protocol notify related globals\r
@@ -34,8 +34,8 @@ FV_DEVICE mFvDevice = {
     FvReadFile,\r
     FvReadFileSection,\r
     FvWriteFile,\r
-    FvGetNextFile,\r
-    KEYSIZE,\r
+    FvGetNextFile,   \r
+       sizeof (UINTN),\r
     NULL,\r
     FvGetVolumeInfo,\r
     FvSetVolumeInfo\r
@@ -45,7 +45,10 @@ FV_DEVICE mFvDevice = {
   NULL,\r
   NULL,\r
   { NULL, NULL },\r
-  0\r
+  0,\r
+  0,\r
+  FALSE,\r
+  FALSE\r
 };\r
 \r
 \r
@@ -53,16 +56,122 @@ FV_DEVICE mFvDevice = {
 // FFS helper functions\r
 //\r
 /**\r
-  given the supplied FW_VOL_BLOCK_PROTOCOL, allocate a buffer for output and\r
-  copy the volume header into it.\r
+  Read data from Firmware Block by FVB protocol Read. \r
+  The data may cross the multi block ranges.\r
+\r
+  @param  Fvb                   The FW_VOL_BLOCK_PROTOCOL instance from which to read data.\r
+  @param  StartLba              Pointer to StartLba.\r
+                                On input, the start logical block index from which to read.\r
+                                On output,the end logical block index after reading.\r
+  @param  Offset                Pointer to Offset\r
+                                On input, offset into the block at which to begin reading.\r
+                                On output, offset into the end block after reading.\r
+  @param  DataSize              Size of data to be read.\r
+  @param  Data                  Pointer to Buffer that the data will be read into.\r
+\r
+  @retval EFI_SUCCESS           Successfully read data from firmware block.\r
+  @retval others\r
+**/\r
+EFI_STATUS\r
+ReadFvbData (\r
+  IN     EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL     *Fvb,\r
+  IN OUT EFI_LBA                                *StartLba,\r
+  IN OUT UINTN                                  *Offset,\r
+  IN     UINTN                                  DataSize,\r
+  OUT    UINT8                                  *Data\r
+  )\r
+{\r
+  UINTN                       BlockSize;\r
+  UINTN                       NumberOfBlocks;\r
+  UINTN                       BlockIndex;\r
+  UINTN                       ReadDataSize;\r
+  EFI_STATUS                  Status;\r
+  \r
+  //\r
+  // Try read data in current block\r
+  //\r
+  BlockIndex   = 0;\r
+  ReadDataSize = DataSize;\r
+  Status = Fvb->Read (Fvb, *StartLba, *Offset, &ReadDataSize, Data);\r
+  if (Status == EFI_SUCCESS) {\r
+    *Offset  += DataSize;\r
+    return EFI_SUCCESS;\r
+  } else if (Status != EFI_BAD_BUFFER_SIZE) {\r
+    //\r
+    // other error will direct return\r
+    //\r
+    return Status;\r
+  }\r
+  \r
+  //\r
+  // Data crosses the blocks, read data from next block\r
+  //\r
+  DataSize -= ReadDataSize;\r
+  Data     += ReadDataSize;\r
+  *StartLba = *StartLba + 1;\r
+  while (DataSize > 0) {\r
+    Status = Fvb->GetBlockSize (Fvb, *StartLba, &BlockSize, &NumberOfBlocks);\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
 \r
-  @param  Fvb                   The FW_VOL_BLOCK_PROTOCOL instance from which to \r
-                                read the volume header \r
-  @param  FwVolHeader           Pointer to pointer to allocated buffer in which \r
-                                the volume header is returned. \r
+    //\r
+    // Read data from the crossing blocks\r
+    //\r
+    BlockIndex = 0; \r
+    while (BlockIndex < NumberOfBlocks && DataSize >= BlockSize) {\r
+      Status = Fvb->Read (Fvb, *StartLba + BlockIndex, 0, &BlockSize, Data);\r
+      if (EFI_ERROR (Status)) {\r
+        return Status;\r
+      }\r
+      Data += BlockSize;\r
+      DataSize -= BlockSize;\r
+      BlockIndex ++;\r
+    }\r
+    \r
+    //\r
+    // Data doesn't exceed the current block range.\r
+    //\r
+    if (DataSize < BlockSize) {\r
+      break;\r
+    }\r
+    \r
+    //\r
+    // Data must be got from the next block range.\r
+    //\r
+    *StartLba += NumberOfBlocks;\r
+  }\r
+  \r
+  //\r
+  // read the remaining data\r
+  //\r
+  if (DataSize > 0) {\r
+    Status = Fvb->Read (Fvb, *StartLba + BlockIndex, 0, &DataSize, Data);\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+  }\r
+  \r
+  //\r
+  // Update Lba and Offset used by the following read.\r
+  //\r
+  *StartLba += BlockIndex;\r
+  *Offset   = DataSize;\r
+\r
+  return EFI_SUCCESS;\r
+}\r
 \r
-  @retval EFI_OUT_OF_RESOURCES  No enough buffer could be allocated. \r
-  @retval EFI_SUCCESS           Successfully read volume header to the allocated \r
+/**\r
+  Given the supplied FW_VOL_BLOCK_PROTOCOL, allocate a buffer for output and\r
+  copy the real length volume header into it.\r
+\r
+  @param  Fvb                   The FW_VOL_BLOCK_PROTOCOL instance from which to\r
+                                read the volume header\r
+  @param  FwVolHeader           Pointer to pointer to allocated buffer in which\r
+                                the volume header is returned.\r
+\r
+  @retval EFI_OUT_OF_RESOURCES  No enough buffer could be allocated.\r
+  @retval EFI_SUCCESS           Successfully read volume header to the allocated\r
                                 buffer.\r
 \r
 **/\r
@@ -75,19 +184,25 @@ GetFwVolHeader (
   EFI_STATUS                  Status;\r
   EFI_FIRMWARE_VOLUME_HEADER  TempFvh;\r
   UINTN                       FvhLength;\r
+  EFI_LBA                     StartLba;\r
+  UINTN                       Offset;\r
   UINT8                       *Buffer;\r
-\r
-\r
+  \r
   //\r
-  //Determine the real length of FV header\r
+  // Read the standard FV header\r
   //\r
+  StartLba = 0;\r
+  Offset   = 0;\r
   FvhLength = sizeof (EFI_FIRMWARE_VOLUME_HEADER);\r
-  Status = Fvb->Read (Fvb, 0, 0, &FvhLength, (UINT8 *)&TempFvh);\r
+  Status = ReadFvbData (Fvb, &StartLba, &Offset, FvhLength, (UINT8 *)&TempFvh);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
 \r
   //\r
   // Allocate a buffer for the caller\r
   //\r
-  *FwVolHeader = CoreAllocateBootServicesPool (TempFvh.HeaderLength);\r
+  *FwVolHeader = AllocatePool (TempFvh.HeaderLength);\r
   if (*FwVolHeader == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
@@ -102,14 +217,14 @@ GetFwVolHeader (
   //\r
   FvhLength = TempFvh.HeaderLength - sizeof (EFI_FIRMWARE_VOLUME_HEADER);\r
   Buffer = (UINT8 *)*FwVolHeader + sizeof (EFI_FIRMWARE_VOLUME_HEADER);\r
-  Status = Fvb->Read (Fvb, 0, sizeof (EFI_FIRMWARE_VOLUME_HEADER), &FvhLength, Buffer);\r
+  Status = ReadFvbData (Fvb, &StartLba, &Offset, FvhLength, Buffer);\r
   if (EFI_ERROR (Status)) {\r
     //\r
     // Read failed so free buffer\r
     //\r
     CoreFreePool (*FwVolHeader);\r
   }\r
\r
+\r
   return Status;\r
 }\r
 \r
@@ -118,7 +233,7 @@ GetFwVolHeader (
 /**\r
   Free FvDevice resource when error happens\r
 \r
-  @param  FvDevice              pointer to the FvDevice to be freed. \r
+  @param  FvDevice              pointer to the FvDevice to be freed.\r
 \r
 **/\r
 VOID\r
@@ -135,12 +250,19 @@ FreeFvDeviceResource (
   FfsFileEntry = (FFS_FILE_LIST_ENTRY *)FvDevice->FfsFileListHeader.ForwardLink;\r
   while (&FfsFileEntry->Link != &FvDevice->FfsFileListHeader) {\r
     NextEntry = (&FfsFileEntry->Link)->ForwardLink;\r
-    \r
+\r
     if (FfsFileEntry->StreamHandle != 0) {\r
       //\r
       // Close stream and free resources from SEP\r
       //\r
-      CloseSectionStream (FfsFileEntry->StreamHandle);\r
+      CloseSectionStream (FfsFileEntry->StreamHandle, FALSE);\r
+    }\r
+\r
+    if (FfsFileEntry->FileCached) {\r
+      //\r
+      // Free the cached file buffer.\r
+      //\r
+      CoreFreePool (FfsFileEntry->FfsHeader);\r
     }\r
 \r
     CoreFreePool (FfsFileEntry);\r
@@ -148,11 +270,12 @@ FreeFvDeviceResource (
     FfsFileEntry = (FFS_FILE_LIST_ENTRY *) NextEntry;\r
   }\r
 \r
-\r
-  //\r
-  // Free the cache\r
-  //\r
-  CoreFreePool (FvDevice->CachedFv);\r
+  if (!FvDevice->IsMemoryMapped) {\r
+    //\r
+    // Free the cached FV buffer.\r
+    //\r
+    CoreFreePool (FvDevice->CachedFv);\r
+  }\r
 \r
   //\r
   // Free Volume Header\r
@@ -167,10 +290,10 @@ FreeFvDeviceResource (
 /**\r
   Check if an FV is consistent and allocate cache for it.\r
 \r
-  @param  FvDevice              A pointer to the FvDevice to be checked. \r
+  @param  FvDevice              A pointer to the FvDevice to be checked.\r
 \r
-  @retval EFI_OUT_OF_RESOURCES  No enough buffer could be allocated. \r
-  @retval EFI_SUCCESS           FV is consistent and cache is allocated. \r
+  @retval EFI_OUT_OF_RESOURCES  No enough buffer could be allocated.\r
+  @retval EFI_SUCCESS           FV is consistent and cache is allocated.\r
   @retval EFI_VOLUME_CORRUPTED  File system is corrupted.\r
 \r
 **/\r
@@ -182,24 +305,25 @@ FvCheck (
   EFI_STATUS                            Status;\r
   EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL    *Fvb;\r
   EFI_FIRMWARE_VOLUME_HEADER            *FwVolHeader;\r
-  EFI_FVB_ATTRIBUTES                    FvbAttributes;\r
+  EFI_FIRMWARE_VOLUME_EXT_HEADER        *FwVolExtHeader;\r
+  EFI_FVB_ATTRIBUTES_2                  FvbAttributes;\r
   EFI_FV_BLOCK_MAP_ENTRY                *BlockMap;\r
   FFS_FILE_LIST_ENTRY                   *FfsFileEntry;\r
   EFI_FFS_FILE_HEADER                   *FfsHeader;\r
   UINT8                                 *CacheLocation;\r
   UINTN                                 LbaOffset;\r
+  UINTN                                 HeaderSize;\r
   UINTN                                 Index;\r
   EFI_LBA                               LbaIndex;\r
   UINTN                                 Size;\r
-  UINTN                                 FileLength;\r
   EFI_FFS_FILE_STATE                    FileState;\r
   UINT8                                 *TopFvAddress;\r
   UINTN                                 TestLength;\r
-\r
+  EFI_PHYSICAL_ADDRESS                  PhysicalAddress;\r
 \r
   Fvb = FvDevice->Fvb;\r
   FwVolHeader = FvDevice->FwVolHeader;\r
\r
+\r
   Status = Fvb->GetAttributes (Fvb, &FvbAttributes);\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
@@ -210,10 +334,25 @@ FvCheck (
   // the header to check to make sure the volume is valid\r
   //\r
   Size = (UINTN)(FwVolHeader->FvLength - FwVolHeader->HeaderLength);\r
-  FvDevice->CachedFv = CoreAllocateBootServicesPool (Size);\r
+  if ((FvbAttributes & EFI_FVB2_MEMORY_MAPPED) != 0) {\r
+    FvDevice->IsMemoryMapped = TRUE;\r
 \r
-  if (FvDevice->CachedFv == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
+    Status = Fvb->GetPhysicalAddress (Fvb, &PhysicalAddress);\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+\r
+    //\r
+    // Don't cache memory mapped FV really.\r
+    //\r
+    FvDevice->CachedFv = (UINT8 *) (UINTN) (PhysicalAddress + FwVolHeader->HeaderLength);\r
+  } else {\r
+    FvDevice->IsMemoryMapped = FALSE;\r
+    FvDevice->CachedFv = AllocatePool (Size);\r
+\r
+    if (FvDevice->CachedFv == NULL) {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
   }\r
 \r
   //\r
@@ -221,47 +360,71 @@ FvCheck (
   //\r
   FvDevice->EndOfCachedFv = FvDevice->CachedFv + Size;\r
 \r
-  //\r
-  // Copy FV minus header into memory using the block map we have all ready\r
-  // read into memory.\r
-  //\r
-  BlockMap = FwVolHeader->BlockMap;\r
-  CacheLocation = FvDevice->CachedFv;\r
-  LbaIndex = 0;\r
-  LbaOffset = FwVolHeader->HeaderLength;\r
-  while ((BlockMap->NumBlocks != 0) || (BlockMap->Length != 0)) {\r
-    \r
-    for (Index = 0; Index < BlockMap->NumBlocks; Index ++) {\r
+  if (!FvDevice->IsMemoryMapped) {\r
+    //\r
+    // Copy FV minus header into memory using the block map we have all ready\r
+    // read into memory.\r
+    //\r
+    BlockMap = FwVolHeader->BlockMap;\r
+    CacheLocation = FvDevice->CachedFv;\r
+    LbaIndex = 0;\r
+    LbaOffset = 0;\r
+    HeaderSize = FwVolHeader->HeaderLength;\r
+    while ((BlockMap->NumBlocks != 0) || (BlockMap->Length != 0)) {\r
+      Index = 0;\r
+      Size  = BlockMap->Length;\r
+      if (HeaderSize > 0) {\r
+        //\r
+        // Skip header size\r
+        //\r
+        for (; Index < BlockMap->NumBlocks && HeaderSize >= BlockMap->Length; Index ++) {\r
+          HeaderSize -= BlockMap->Length;\r
+          LbaIndex ++;\r
+        }\r
 \r
-      Size = BlockMap->Length;\r
-      if (Index == 0) {\r
         //\r
-        // Cache does not include FV Header\r
+        // Check whether FvHeader is crossing the multi block range.\r
         //\r
-        Size -= LbaOffset;\r
+        if (Index >= BlockMap->NumBlocks) {\r
+          BlockMap++;\r
+          continue;\r
+        } else if (HeaderSize > 0) {\r
+          LbaOffset = HeaderSize;\r
+          Size = BlockMap->Length - HeaderSize;\r
+          HeaderSize = 0;\r
+        }\r
       }\r
-      Status = Fvb->Read (Fvb,\r
-                      LbaIndex,\r
-                      LbaOffset,\r
-                      &Size,\r
-                      CacheLocation\r
-                      );\r
+    \r
       //\r
-      // Not check EFI_BAD_BUFFER_SIZE, for Size = BlockMap->Length\r
+      // read the FV data  \r
       //\r
-      if (EFI_ERROR (Status)) {\r
-        goto Done;\r
+      for (; Index < BlockMap->NumBlocks; Index ++) {\r
+        Status = Fvb->Read (Fvb,\r
+                        LbaIndex,\r
+                        LbaOffset,\r
+                        &Size,\r
+                        CacheLocation\r
+                        );\r
+\r
+        //\r
+        // Not check EFI_BAD_BUFFER_SIZE, for Size = BlockMap->Length\r
+        //\r
+        if (EFI_ERROR (Status)) {\r
+          goto Done;\r
+        }\r
+\r
+        LbaIndex++;\r
+        CacheLocation += Size;\r
+\r
+        //\r
+        // After we skip Fv Header always read from start of block\r
+        //\r
+        LbaOffset = 0;\r
+        Size  = BlockMap->Length;\r
       }\r
-      \r
-      //\r
-      // After we skip Fv Header always read from start of block\r
-      //\r
-      LbaOffset = 0;\r
 \r
-      LbaIndex++;\r
-      CacheLocation += Size;\r
+      BlockMap++;\r
     }\r
-    BlockMap++;\r
   }\r
 \r
   //\r
@@ -271,12 +434,12 @@ FvCheck (
     FvDevice->ErasePolarity = 1;\r
   } else {\r
     FvDevice->ErasePolarity = 0;\r
-  } \r
+  }\r
 \r
 \r
   //\r
   // go through the whole FV cache, check the consistence of the FV.\r
-  // Make a linked list off all the Ffs file headers\r
+  // Make a linked list of all the Ffs file headers\r
   //\r
   Status = EFI_SUCCESS;\r
   InitializeListHead (&FvDevice->FfsFileListHeader);\r
@@ -284,7 +447,16 @@ FvCheck (
   //\r
   // Build FFS list\r
   //\r
-  FfsHeader = (EFI_FFS_FILE_HEADER *) FvDevice->CachedFv;\r
+  if (FwVolHeader->ExtHeaderOffset != 0) {\r
+    //\r
+    // Searching for files starts on an 8 byte aligned boundary after the end of the Extended Header if it exists.\r
+    //\r
+    FwVolExtHeader = (EFI_FIRMWARE_VOLUME_EXT_HEADER *) (FvDevice->CachedFv + (FwVolHeader->ExtHeaderOffset - FwVolHeader->HeaderLength));\r
+    FfsHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FwVolExtHeader + FwVolExtHeader->ExtHeaderSize);\r
+    FfsHeader = (EFI_FFS_FILE_HEADER *) ALIGN_POINTER (FfsHeader, 8);\r
+  } else {\r
+    FfsHeader = (EFI_FFS_FILE_HEADER *) (FvDevice->CachedFv);\r
+  }\r
   TopFvAddress = FvDevice->EndOfCachedFv;\r
   while ((UINT8 *) FfsHeader < TopFvAddress) {\r
 \r
@@ -301,9 +473,16 @@ FvCheck (
     }\r
 \r
     if (!IsValidFfsHeader (FvDevice->ErasePolarity, FfsHeader, &FileState)) {\r
-      if ((FileState == EFI_FILE_HEADER_INVALID) || \r
+      if ((FileState == EFI_FILE_HEADER_INVALID) ||\r
           (FileState == EFI_FILE_HEADER_CONSTRUCTION)) {\r
-        FfsHeader++;\r
+        if (IS_FFS_FILE2 (FfsHeader)) {\r
+          if (!FvDevice->IsFfs3Fv) {\r
+            DEBUG ((EFI_D_ERROR, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FfsHeader->Name));\r
+          }\r
+          FfsHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsHeader + sizeof (EFI_FFS_FILE_HEADER2));\r
+        } else {\r
+          FfsHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsHeader + sizeof (EFI_FFS_FILE_HEADER));\r
+        }\r
         continue;\r
       } else {\r
         //\r
@@ -322,13 +501,21 @@ FvCheck (
       goto Done;\r
     }\r
 \r
-    //\r
-    // Size[3] is a three byte array, read 4 bytes and throw one away\r
-    //\r
-    FileLength = *(UINT32 *)&FfsHeader->Size[0] & 0x00FFFFFF;\r
+    if (IS_FFS_FILE2 (FfsHeader)) {\r
+      ASSERT (FFS_FILE2_SIZE (FfsHeader) > 0x00FFFFFF);\r
+      if (!FvDevice->IsFfs3Fv) {\r
+        DEBUG ((EFI_D_ERROR, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FfsHeader->Name));\r
+        FfsHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsHeader + FFS_FILE2_SIZE (FfsHeader));\r
+        //\r
+        // Adjust pointer to the next 8-byte aligned boundry.\r
+        //\r
+        FfsHeader = (EFI_FFS_FILE_HEADER *) (((UINTN) FfsHeader + 7) & ~0x07);\r
+        continue;\r
+      }\r
+    }\r
 \r
     FileState = GetFileState (FvDevice->ErasePolarity, FfsHeader);\r
-    \r
+\r
     //\r
     // check for non-deleted file\r
     //\r
@@ -336,23 +523,27 @@ FvCheck (
       //\r
       // Create a FFS list entry for each non-deleted file\r
       //\r
-      FfsFileEntry = CoreAllocateZeroBootServicesPool (sizeof (FFS_FILE_LIST_ENTRY));\r
+      FfsFileEntry = AllocateZeroPool (sizeof (FFS_FILE_LIST_ENTRY));\r
       if (FfsFileEntry == NULL) {\r
         Status = EFI_OUT_OF_RESOURCES;\r
         goto Done;\r
       }\r
-    \r
+\r
       FfsFileEntry->FfsHeader = FfsHeader;\r
       InsertTailList (&FvDevice->FfsFileListHeader, &FfsFileEntry->Link);\r
     }\r
 \r
-    FfsHeader =  (EFI_FFS_FILE_HEADER *)(((UINT8 *)FfsHeader) + FileLength);\r
-    \r
+    if (IS_FFS_FILE2 (FfsHeader)) {\r
+      FfsHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsHeader + FFS_FILE2_SIZE (FfsHeader));\r
+    } else {\r
+      FfsHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsHeader + FFS_FILE_SIZE (FfsHeader));\r
+    }\r
+\r
     //\r
     // Adjust pointer to the next 8-byte aligned boundry.\r
     //\r
     FfsHeader = (EFI_FFS_FILE_HEADER *)(((UINTN)FfsHeader + 7) & ~0x07);\r
-    \r
+\r
   }\r
 \r
 Done:\r
@@ -367,11 +558,11 @@ Done:
 \r
 /**\r
   This notification function is invoked when an instance of the\r
-  EFI_FW_VOLUME_BLOCK_PROTOCOL is produced.  It layers an instance of the\r
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL is produced.  It layers an instance of the\r
   EFI_FIRMWARE_VOLUME2_PROTOCOL on the same handle.  This is the function where\r
   the actual initialization of the EFI_FIRMWARE_VOLUME2_PROTOCOL is done.\r
 \r
-  @param  Event                 The event that occured \r
+  @param  Event                 The event that occured\r
   @param  Context               For EFI compatiblity.  Not used.\r
 \r
 **/\r
@@ -415,13 +606,13 @@ NotifyFwVolBlock (
     if (EFI_ERROR (Status)) {\r
       continue;\r
     }\r
-    \r
+\r
     //\r
     // Get the FirmwareVolumeBlock protocol on that handle\r
     //\r
-    Status = CoreHandleProtocol (Handle, &gEfiFirmwareVolumeBlockProtocolGuid, (VOID **)&Fvb); \r
+    Status = CoreHandleProtocol (Handle, &gEfiFirmwareVolumeBlockProtocolGuid, (VOID **)&Fvb);\r
     ASSERT_EFI_ERROR (Status);\r
-    \r
+    ASSERT (Fvb != NULL);\r
 \r
     //\r
     // Make sure the Fv Header is O.K.\r
@@ -430,6 +621,7 @@ NotifyFwVolBlock (
     if (EFI_ERROR (Status)) {\r
       return;\r
     }\r
+    ASSERT (FwVolHeader != NULL);\r
 \r
     if (!VerifyFvHeaderChecksum (FwVolHeader)) {\r
       CoreFreePool (FwVolHeader);\r
@@ -441,7 +633,8 @@ NotifyFwVolBlock (
     // Check to see that the file system is indeed formatted in a way we can\r
     // understand it...\r
     //\r
-    if (!CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiFirmwareFileSystem2Guid)) {\r
+    if ((!CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiFirmwareFileSystem2Guid)) &&\r
+        (!CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiFirmwareFileSystem3Guid))) {\r
       continue;\r
     }\r
 \r
@@ -453,7 +646,7 @@ NotifyFwVolBlock (
       //\r
       // Update Fv to use a new Fvb\r
       //\r
-      FvDevice = _CR (Fv, FV_DEVICE, Fv);\r
+      FvDevice = BASE_CR (Fv, FV_DEVICE, Fv);\r
       if (FvDevice->Signature == FV2_DEVICE_SIGNATURE) {\r
         //\r
         // Only write into our device structure if it's our device structure\r
@@ -465,41 +658,56 @@ NotifyFwVolBlock (
       //\r
       // No FwVol protocol on the handle so create a new one\r
       //\r
-      FvDevice = CoreAllocateCopyPool (sizeof (FV_DEVICE), &mFvDevice);\r
+      FvDevice = AllocateCopyPool (sizeof (FV_DEVICE), &mFvDevice);\r
       if (FvDevice == NULL) {\r
         return;\r
       }\r
-      \r
+\r
       FvDevice->Fvb             = Fvb;\r
       FvDevice->Handle          = Handle;\r
       FvDevice->FwVolHeader     = FwVolHeader;\r
+      FvDevice->IsFfs3Fv        = CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiFirmwareFileSystem3Guid);\r
       FvDevice->Fv.ParentHandle = Fvb->ParentHandle;\r
+\r
+      if (Fvb->ParentHandle != NULL) {\r
+        //\r
+        // Inherit the authentication status from FVB.\r
+        //\r
+        FvDevice->AuthenticationStatus = GetFvbAuthenticationStatus (Fvb);\r
+      }\r
       \r
-      //\r
-      // Install an New FV protocol on the existing handle\r
-      //\r
-      Status = CoreInstallProtocolInterface (\r
-                  &Handle,\r
-                  &gEfiFirmwareVolume2ProtocolGuid,\r
-                  EFI_NATIVE_INTERFACE,\r
-                  &FvDevice->Fv\r
-                  );\r
-      ASSERT_EFI_ERROR (Status);\r
+      if (!EFI_ERROR (FvCheck (FvDevice))) {\r
+        //\r
+        // Install an New FV protocol on the existing handle\r
+        //\r
+        Status = CoreInstallProtocolInterface (\r
+                    &Handle,\r
+                    &gEfiFirmwareVolume2ProtocolGuid,\r
+                    EFI_NATIVE_INTERFACE,\r
+                    &FvDevice->Fv\r
+                    );\r
+        ASSERT_EFI_ERROR (Status);\r
+      } else {\r
+        //\r
+        // Free FvDevice Buffer for the corrupt FV image.\r
+        //\r
+        CoreFreePool (FvDevice);\r
+      }\r
     }\r
   }\r
-  \r
+\r
   return;\r
 }\r
 \r
 \r
 \r
 /**\r
-  This routine is the driver initialization entry point.  It initializes the\r
-  libraries, and registers two notification functions.  These notification\r
-  functions are responsible for building the FV stack dynamically.\r
+  This routine is the driver initialization entry point.  It registers\r
+  a notification function.  This notification function are responsible\r
+  for building the FV stack dynamically.\r
 \r
-  @param  ImageHandle           The image handle. \r
-  @param  SystemTable           The system table. \r
+  @param  ImageHandle           The image handle.\r
+  @param  SystemTable           The system table.\r
 \r
   @retval EFI_SUCCESS           Function successfully returned.\r
 \r
@@ -511,13 +719,12 @@ FwVolDriverInit (
   IN EFI_SYSTEM_TABLE             *SystemTable\r
   )\r
 {\r
-  gEfiFwVolBlockEvent = CoreCreateProtocolNotifyEvent (\r
+  gEfiFwVolBlockEvent = EfiCreateProtocolNotifyEvent (\r
                           &gEfiFirmwareVolumeBlockProtocolGuid,\r
                           TPL_CALLBACK,\r
                           NotifyFwVolBlock,\r
                           NULL,\r
-                          &gEfiFwVolBlockNotifyReg,\r
-                          TRUE\r
+                          &gEfiFwVolBlockNotifyReg\r
                           );\r
   return EFI_SUCCESS;\r
 }\r