]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/FwVol/FwVolRead.c
MdeModulePkg/Core/Dxe: log informative memprotect msgs at DEBUG_INFO level
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / FwVol / FwVolRead.c
index fa4d6444594ee87af19d191212a05838f940f252..c59c384f5c7598cffce878f43e690b0abcbe20f6 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Implements functions to read firmware file\r
 \r
-Copyright (c) 2006 - 2009, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2017, 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
@@ -16,23 +16,27 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include "FwVolDriver.h"\r
 \r
 /**\r
-Required Alignment             Alignment Value in FFS         Alignment Value in\r
-(bytes)                        Attributes Field               Firmware Volume Interfaces\r
-1                                    0                                     0\r
-2                                    0                                     1\r
-4                                    0                                     2\r
-8                                    0                                     3\r
-16                                   1                                     4\r
-128                                  2                                     7\r
-512                                  3                                     9\r
-1 KB                                 4                                     10\r
-4 KB                                 5                                     12\r
-32 KB                                6                                     15\r
-64 KB                                7                                     16\r
+Required Alignment   Alignment Value in FFS   FFS_ATTRIB_DATA_ALIGNMENT2   Alignment Value in\r
+(bytes)              Attributes Field         in FFS Attributes Field      Firmware Volume Interfaces\r
+1                               0                          0                            0\r
+16                              1                          0                            4\r
+128                             2                          0                            7\r
+512                             3                          0                            9\r
+1 KB                            4                          0                            10\r
+4 KB                            5                          0                            12\r
+32 KB                           6                          0                            15\r
+64 KB                           7                          0                            16\r
+128 KB                          0                          1                            17\r
+256 KB                          1                          1                            18\r
+512 KB                          2                          1                            19\r
+1 MB                            3                          1                            20\r
+2 MB                            4                          1                            21\r
+4 MB                            5                          1                            22\r
+8 MB                            6                          1                            23\r
+16 MB                           7                          1                            24\r
 **/\r
 UINT8 mFvAttributes[] = {0, 4, 7, 9, 10, 12, 15, 16};\r
-\r
-\r
+UINT8 mFvAttributes2[] = {17, 18, 19, 20, 21, 22, 23, 24};\r
 \r
 /**\r
   Convert the FFS File Attributes to FV File Attributes\r
@@ -47,13 +51,24 @@ FfsAttributes2FvFileAttributes (
   IN EFI_FFS_FILE_ATTRIBUTES FfsAttributes\r
   )\r
 {\r
-  FfsAttributes = (EFI_FFS_FILE_ATTRIBUTES)((FfsAttributes & FFS_ATTRIB_DATA_ALIGNMENT) >> 3);\r
-  ASSERT (FfsAttributes < 8);\r
+  UINT8                     DataAlignment;\r
+  EFI_FV_FILE_ATTRIBUTES    FileAttribute;\r
 \r
-  return (EFI_FV_FILE_ATTRIBUTES) mFvAttributes[FfsAttributes];\r
-}\r
+  DataAlignment = (UINT8) ((FfsAttributes & FFS_ATTRIB_DATA_ALIGNMENT) >> 3);\r
+  ASSERT (DataAlignment < 8);\r
+\r
+  if ((FfsAttributes & FFS_ATTRIB_DATA_ALIGNMENT_2) != 0) {\r
+    FileAttribute = (EFI_FV_FILE_ATTRIBUTES) mFvAttributes2[DataAlignment];\r
+  } else {\r
+    FileAttribute = (EFI_FV_FILE_ATTRIBUTES) mFvAttributes[DataAlignment];\r
+  }\r
 \r
+  if ((FfsAttributes & FFS_ATTRIB_FIXED) == FFS_ATTRIB_FIXED) {\r
+    FileAttribute |= EFI_FV_FILE_ATTRIB_FIXED;\r
+  }\r
 \r
+  return FileAttribute;\r
+}\r
 \r
 /**\r
   Given the input key, search for the next matching file in the volume.\r
@@ -119,7 +134,6 @@ FvGetNextFile (
   UINTN                                       *KeyValue;\r
   LIST_ENTRY                                  *Link;\r
   FFS_FILE_LIST_ENTRY                         *FfsFileEntry;\r
-  UINTN                                       FileLength;\r
 \r
   FvDevice = FV_DEVICE_FROM_THIS (This);\r
 \r
@@ -200,16 +214,18 @@ FvGetNextFile (
   *FileType = FfsFileHeader->Type;\r
   CopyGuid (NameGuid, &FfsFileHeader->Name);\r
   *Attributes = FfsAttributes2FvFileAttributes (FfsFileHeader->Attributes);\r
-\r
-  //\r
-  // Read four bytes out of the 3 byte array and throw out extra data\r
-  //\r
-  FileLength = *(UINT32 *)&FfsFileHeader->Size[0] & 0x00FFFFFF;\r
+  if ((FvDevice->FwVolHeader->Attributes & EFI_FVB2_MEMORY_MAPPED) == EFI_FVB2_MEMORY_MAPPED) {\r
+    *Attributes |= EFI_FV_FILE_ATTRIB_MEMORY_MAPPED;\r
+  }\r
 \r
   //\r
   // we need to substract the header size\r
   //\r
-  *Size = FileLength - sizeof(EFI_FFS_FILE_HEADER);\r
+  if (IS_FFS_FILE2 (FfsFileHeader)) {\r
+    *Size = FFS_FILE2_SIZE (FfsFileHeader) - sizeof (EFI_FFS_FILE_HEADER2);\r
+  } else {\r
+    *Size = FFS_FILE_SIZE (FfsFileHeader) - sizeof (EFI_FFS_FILE_HEADER);\r
+  }\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -277,6 +293,7 @@ FvReadFile (
   UINT8                             *SrcPtr;\r
   EFI_FFS_FILE_HEADER               *FfsHeader;\r
   UINTN                             InputBufferSize;\r
+  UINTN                             WholeFileSize;\r
 \r
   if (NameGuid == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -287,7 +304,7 @@ FvReadFile (
 \r
   //\r
   // Keep looking until we find the matching NameGuid.\r
-  // The Key is really an FfsFileEntry\r
+  // The Key is really a FfsFileEntry\r
   //\r
   FvDevice->LastKey = 0;\r
   do {\r
@@ -309,6 +326,26 @@ FvReadFile (
   // Get a pointer to the header\r
   //\r
   FfsHeader = FvDevice->LastKey->FfsHeader;\r
+  if (FvDevice->IsMemoryMapped) {\r
+    //\r
+    // Memory mapped FV has not been cached, so here is to cache by file.\r
+    //\r
+    if (!FvDevice->LastKey->FileCached) {\r
+      //\r
+      // Cache FFS file to memory buffer.\r
+      //\r
+      WholeFileSize = IS_FFS_FILE2 (FfsHeader) ? FFS_FILE2_SIZE (FfsHeader): FFS_FILE_SIZE (FfsHeader);\r
+      FfsHeader = AllocateCopyPool (WholeFileSize, FfsHeader);\r
+      if (FfsHeader == NULL) {\r
+        return EFI_OUT_OF_RESOURCES;\r
+      }\r
+      //\r
+      // Let FfsHeader in FfsFileEntry point to the cached file buffer.\r
+      //\r
+      FvDevice->LastKey->FfsHeader = FfsHeader;\r
+      FvDevice->LastKey->FileCached = TRUE;\r
+    }\r
+  }\r
 \r
   //\r
   // Remember callers buffer size\r
@@ -320,12 +357,18 @@ FvReadFile (
   //\r
   *FoundType = FfsHeader->Type;\r
   *FileAttributes = FfsAttributes2FvFileAttributes (FfsHeader->Attributes);\r
-  *AuthenticationStatus = 0;\r
+   if ((FvDevice->FwVolHeader->Attributes & EFI_FVB2_MEMORY_MAPPED) == EFI_FVB2_MEMORY_MAPPED) {\r
+     *FileAttributes |= EFI_FV_FILE_ATTRIB_MEMORY_MAPPED;\r
+   }\r
+  //\r
+  // Inherit the authentication status.\r
+  //\r
+  *AuthenticationStatus = FvDevice->AuthenticationStatus;\r
   *BufferSize = FileSize;\r
 \r
   if (Buffer == NULL) {\r
     //\r
-    // If Buffer is NULL, we only want to get the information colected so far\r
+    // If Buffer is NULL, we only want to get the information collected so far\r
     //\r
     return EFI_SUCCESS;\r
   }\r
@@ -333,7 +376,11 @@ FvReadFile (
   //\r
   // Skip over file header\r
   //\r
-  SrcPtr = ((UINT8 *)FfsHeader) + sizeof (EFI_FFS_FILE_HEADER);\r
+  if (IS_FFS_FILE2 (FfsHeader)) {\r
+    SrcPtr = ((UINT8 *) FfsHeader) + sizeof (EFI_FFS_FILE_HEADER2);\r
+  } else {\r
+    SrcPtr = ((UINT8 *) FfsHeader) + sizeof (EFI_FFS_FILE_HEADER);\r
+  }\r
 \r
   Status = EFI_SUCCESS;\r
   if (*Buffer == NULL) {\r
@@ -417,13 +464,12 @@ FvReadFileSection (
   FvDevice = FV_DEVICE_FROM_THIS (This);\r
 \r
   //\r
-  // Read the whole file into buffer\r
+  // Read the file\r
   //\r
-  FileBuffer = NULL;\r
   Status = FvReadFile (\r
             This,\r
             NameGuid,\r
-            (VOID **)&FileBuffer,\r
+            NULL,\r
             &FileSize,\r
             &FileType,\r
             &FileAttributes,\r
@@ -437,7 +483,11 @@ FvReadFileSection (
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
-\r
+  if (IS_FFS_FILE2 (FfsEntry->FfsHeader)) {\r
+    FileBuffer = ((UINT8 *) FfsEntry->FfsHeader) + sizeof (EFI_FFS_FILE_HEADER2);\r
+  } else {\r
+    FileBuffer = ((UINT8 *) FfsEntry->FfsHeader) + sizeof (EFI_FFS_FILE_HEADER);\r
+  }\r
   //\r
   // Check to see that the file actually HAS sections before we go any further.\r
   //\r
@@ -447,7 +497,7 @@ FvReadFileSection (
   }\r
 \r
   //\r
-  // Use FfsEntry to cache Section Extraction Protocol Inforomation\r
+  // Use FfsEntry to cache Section Extraction Protocol Information\r
   //\r
   if (FfsEntry->StreamHandle == 0) {\r
     Status = OpenSectionStream (\r
@@ -470,16 +520,22 @@ FvReadFileSection (
              (SectionType == 0) ? 0 : SectionInstance,\r
              Buffer,\r
              BufferSize,\r
-             AuthenticationStatus\r
+             AuthenticationStatus,\r
+             FvDevice->IsFfs3Fv\r
              );\r
 \r
+  if (!EFI_ERROR (Status)) {\r
+    //\r
+    // Inherit the authentication status.\r
+    //\r
+    *AuthenticationStatus |= FvDevice->AuthenticationStatus;\r
+  }\r
+\r
   //\r
   // Close of stream defered to close of FfsHeader list to allow SEP to cache data\r
   //\r
 \r
 Done:\r
-  CoreFreePool (FileBuffer);\r
-\r
   return Status;\r
 }\r
 \r