]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/FwVol/FwVol.c
Refine code to make it more safely.
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / FwVol / FwVol.c
index e7a5b44a6605ab181c32a3f966cd95230e50e4fe..9355e52ab068179760e6e19bcbc2ec6b7564cb8e 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 - 2012, 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,6 +45,8 @@ FV_DEVICE mFvDevice = {
   NULL,\r
   NULL,\r
   { NULL, NULL },\r
+  0,\r
+  FALSE,\r
   0\r
 };\r
 \r
@@ -294,6 +296,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,7 +307,6 @@ 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
@@ -358,7 +360,7 @@ FvCheck (
       //\r
       // Check whether FvHeader is crossing the multi block range.\r
       //\r
-      if (HeaderSize > BlockMap->Length) {\r
+      if (Index >= BlockMap->NumBlocks) {\r
         BlockMap++;\r
         continue;\r
       } else if (HeaderSize > 0) {\r
@@ -411,7 +413,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,7 +421,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
@@ -438,7 +449,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
@@ -457,10 +475,18 @@ 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
@@ -481,7 +507,11 @@ FvCheck (
       InsertTailList (&FvDevice->FfsFileListHeader, &FfsFileEntry->Link);\r
     }\r
 \r
-    FfsHeader =  (EFI_FFS_FILE_HEADER *)(((UINT8 *)FfsHeader) + FileLength);\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
@@ -502,7 +532,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 +607,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 +640,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