]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/FwVol/FwVol.c
MdeModulePkg DxeCore: Handle FFS file with FFS_ATTRIB_CHECKSUM set for not cache...
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / FwVol / FwVol.c
index e7a5b44a6605ab181c32a3f966cd95230e50e4fe..4fa177ed7c93359af1adf79ec6423037b3139dbf 100644 (file)
@@ -3,7 +3,7 @@
   Layers on top of Firmware Block protocol to produce a file abstraction\r
   of FV based files.\r
 \r
-Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\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
@@ -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
@@ -252,7 +255,14 @@ FreeFvDeviceResource (
       //\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
@@ -260,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
@@ -294,6 +305,7 @@ FvCheck (
   EFI_STATUS                            Status;\r
   EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL    *Fvb;\r
   EFI_FIRMWARE_VOLUME_HEADER            *FwVolHeader;\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
@@ -304,11 +316,16 @@ FvCheck (
   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
+  EFI_PHYSICAL_ADDRESS                  PhysicalAddress;\r
+  BOOLEAN                               FileCached;\r
+  UINTN                                 WholeFileSize;\r
+  EFI_FFS_FILE_HEADER                   *CacheFfsHeader;\r
 \r
+  FileCached = FALSE;\r
+  CacheFfsHeader = NULL;\r
 \r
   Fvb = FvDevice->Fvb;\r
   FwVolHeader = FvDevice->FwVolHeader;\r
@@ -323,10 +340,25 @@ FvCheck (
   // the header to check to make sure the volume is valid\r
   //\r
   Size = (UINTN)(FwVolHeader->FvLength - FwVolHeader->HeaderLength);\r
-  FvDevice->CachedFv = AllocatePool (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
@@ -334,69 +366,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 = 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
-      //\r
-      // Check whether FvHeader is crossing the multi block range.\r
-      //\r
-      if (HeaderSize > BlockMap->Length) {\r
-        BlockMap++;\r
-        continue;\r
-      } else if (HeaderSize > 0) {\r
-        LbaOffset = HeaderSize;\r
-        Size = BlockMap->Length - HeaderSize;\r
-        HeaderSize = 0;\r
-      }\r
-    }\r
-    \r
+  if (!FvDevice->IsMemoryMapped) {\r
     //\r
-    // read the FV data  \r
+    // Copy FV minus header into memory using the block map we have all ready\r
+    // read into memory.\r
     //\r
-    for (; Index < BlockMap->NumBlocks; Index ++) {\r
-      Status = Fvb->Read (Fvb,\r
-                      LbaIndex,\r
-                      LbaOffset,\r
-                      &Size,\r
-                      CacheLocation\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
+        //\r
+        // Check whether FvHeader is crossing the multi block range.\r
+        //\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
+    \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
-      }\r
+      for (; Index < BlockMap->NumBlocks; Index ++) {\r
+        Status = Fvb->Read (Fvb,\r
+                        LbaIndex,\r
+                        LbaOffset,\r
+                        &Size,\r
+                        CacheLocation\r
+                        );\r
 \r
-      LbaIndex++;\r
-      CacheLocation += Size;\r
+        //\r
+        // Not check EFI_BAD_BUFFER_SIZE, for Size = BlockMap->Length\r
+        //\r
+        if (EFI_ERROR (Status)) {\r
+          goto Done;\r
+        }\r
 \r
-      //\r
-      // After we skip Fv Header always read from start of block\r
-      //\r
-      LbaOffset = 0;\r
-      Size  = BlockMap->Length;\r
-    }\r
+        LbaIndex++;\r
+        CacheLocation += Size;\r
 \r
-    BlockMap++;\r
+        //\r
+        // After we skip Fv Header always read from start of block\r
+        //\r
+        LbaOffset = 0;\r
+        Size  = BlockMap->Length;\r
+      }\r
+\r
+      BlockMap++;\r
+    }\r
   }\r
 \r
   //\r
@@ -411,7 +445,7 @@ FvCheck (
 \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
@@ -419,10 +453,24 @@ 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
+    if (FileCached) {\r
+      CoreFreePool (CacheFfsHeader);\r
+      FileCached = FALSE;\r
+    }\r
+\r
     TestLength = TopFvAddress - ((UINT8 *) FfsHeader);\r
     if (TestLength > sizeof (EFI_FFS_FILE_HEADER)) {\r
       TestLength = sizeof (EFI_FFS_FILE_HEADER);\r
@@ -438,7 +486,14 @@ FvCheck (
     if (!IsValidFfsHeader (FvDevice->ErasePolarity, FfsHeader, &FileState)) {\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
@@ -449,7 +504,25 @@ FvCheck (
       }\r
     }\r
 \r
-    if (!IsValidFfsFile (FvDevice->ErasePolarity, FfsHeader)) {\r
+    CacheFfsHeader = FfsHeader;\r
+    if ((CacheFfsHeader->Attributes & FFS_ATTRIB_CHECKSUM) == FFS_ATTRIB_CHECKSUM) {\r
+      if (FvDevice->IsMemoryMapped) {\r
+        //\r
+        // Memory mapped FV has not been cached.\r
+        // Here is to cache FFS file to memory buffer for following checksum calculating.\r
+        // And then, the cached file buffer can be also used for FvReadFile.\r
+        //\r
+        WholeFileSize = IS_FFS_FILE2 (CacheFfsHeader) ? FFS_FILE2_SIZE (CacheFfsHeader): FFS_FILE_SIZE (CacheFfsHeader);\r
+        CacheFfsHeader = AllocateCopyPool (WholeFileSize, CacheFfsHeader);\r
+        if (CacheFfsHeader == NULL) {\r
+          Status = EFI_OUT_OF_RESOURCES;\r
+          goto Done;\r
+        }\r
+        FileCached = TRUE;\r
+      }\r
+    }\r
+\r
+    if (!IsValidFfsFile (FvDevice->ErasePolarity, CacheFfsHeader)) {\r
       //\r
       // File system is corrupted\r
       //\r
@@ -457,12 +530,20 @@ 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 (CacheFfsHeader)) {\r
+      ASSERT (FFS_FILE2_SIZE (CacheFfsHeader) > 0x00FFFFFF);\r
+      if (!FvDevice->IsFfs3Fv) {\r
+        DEBUG ((EFI_D_ERROR, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &CacheFfsHeader->Name));\r
+        FfsHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsHeader + FFS_FILE2_SIZE (CacheFfsHeader));\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
+    FileState = GetFileState (FvDevice->ErasePolarity, CacheFfsHeader);\r
 \r
     //\r
     // check for non-deleted file\r
@@ -477,11 +558,17 @@ FvCheck (
         goto Done;\r
       }\r
 \r
-      FfsFileEntry->FfsHeader = FfsHeader;\r
+      FfsFileEntry->FfsHeader = CacheFfsHeader;\r
+      FfsFileEntry->FileCached = FileCached;\r
+      FileCached = FALSE;\r
       InsertTailList (&FvDevice->FfsFileListHeader, &FfsFileEntry->Link);\r
     }\r
 \r
-    FfsHeader =  (EFI_FFS_FILE_HEADER *)(((UINT8 *)FfsHeader) + FileLength);\r
+    if (IS_FFS_FILE2 (CacheFfsHeader)) {\r
+      FfsHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsHeader + FFS_FILE2_SIZE (CacheFfsHeader));\r
+    } else {\r
+      FfsHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsHeader + FFS_FILE_SIZE (CacheFfsHeader));\r
+    }\r
 \r
     //\r
     // Adjust pointer to the next 8-byte aligned boundry.\r
@@ -492,6 +579,10 @@ FvCheck (
 \r
 Done:\r
   if (EFI_ERROR (Status)) {\r
+    if (FileCached) {\r
+      CoreFreePool (CacheFfsHeader);\r
+      FileCached = FALSE;\r
+    }\r
     FreeFvDeviceResource (FvDevice);\r
   }\r
 \r
@@ -502,7 +593,7 @@ 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
@@ -577,7 +668,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
@@ -609,18 +701,33 @@ NotifyFwVolBlock (
       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
-      //\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 (Fvb->ParentHandle != NULL) {\r
+        //\r
+        // Inherit the authentication status from FVB.\r
+        //\r
+        FvDevice->AuthenticationStatus = GetFvbAuthenticationStatus (Fvb);\r
+      }\r
+      \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