]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/FwVol/FwVolRead.c
Refine code to make it more safely.
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / FwVol / FwVolRead.c
index 0b53af27d1fd8902b7caea6c8fed7891a43feaa4..f2c0dd5de42e0593211750d299de93d39f17d680 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 - 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
@@ -19,9 +19,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 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
@@ -32,8 +29,6 @@ Required Alignment             Alignment Value in FFS         Alignment Value in
 **/\r
 UINT8 mFvAttributes[] = {0, 4, 7, 9, 10, 12, 15, 16};\r
 \r
-\r
-\r
 /**\r
   Convert the FFS File Attributes to FV File Attributes\r
 \r
@@ -47,13 +42,20 @@ 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
+  FileAttribute = (EFI_FV_FILE_ATTRIBUTES) mFvAttributes[DataAlignment];\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 +121,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 +201,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
@@ -320,6 +323,9 @@ FvReadFile (
   //\r
   *FoundType = FfsHeader->Type;\r
   *FileAttributes = FfsAttributes2FvFileAttributes (FfsHeader->Attributes);\r
+   if ((FvDevice->FwVolHeader->Attributes & EFI_FVB2_MEMORY_MAPPED) == EFI_FVB2_MEMORY_MAPPED) {\r
+     *FileAttributes |= EFI_FV_FILE_ATTRIB_MEMORY_MAPPED;\r
+   }\r
   *AuthenticationStatus = 0;\r
   *BufferSize = FileSize;\r
 \r
@@ -333,7 +339,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
@@ -437,6 +447,7 @@ FvReadFileSection (
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
+  ASSERT (FileBuffer != NULL);\r
 \r
   //\r
   // Check to see that the file actually HAS sections before we go any further.\r
@@ -447,7 +458,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,9 +481,17 @@ 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