]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Pei/FwVol/FwVol.c
Source level security checked
[mirror_edk2.git] / MdeModulePkg / Core / Pei / FwVol / FwVol.c
index 689e0953bd342cb4a28a5a91f67e46c987cb408a..7cb2b8d264ad5eb57cd5d18a32f54e79e17ce4b4 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Pei Core Firmware File System service routines.\r
   \r
-Copyright (c) 2006 - 2007, Intel Corporation                                                         \r
+Copyright (c) 2006 - 2008, Intel Corporation                                                         \r
 All rights reserved. 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
@@ -12,9 +12,9 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 **/\r
 \r
-#include <PeiMain.h>\r
+#include "PeiMain.h"\r
 \r
-STATIC EFI_PEI_NOTIFY_DESCRIPTOR mNotifyOnFvInfoList = {\r
+EFI_PEI_NOTIFY_DESCRIPTOR mNotifyOnFvInfoList = {\r
   (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
   &gEfiPeiFirmwareVolumeInfoPpiGuid,\r
   FirmwareVolmeInfoPpiNotifyCallback \r
@@ -22,17 +22,17 @@ STATIC EFI_PEI_NOTIFY_DESCRIPTOR mNotifyOnFvInfoList = {
 \r
 \r
 #define GET_OCCUPIED_SIZE(ActualSize, Alignment) \\r
-  (ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1))) & ((Alignment) - 1))\r
+  ((ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1))) & ((Alignment) - 1)))\r
 \r
 /**\r
-  Returns the highest bit set of the State field\r
+  Returns the file state set by the highest zero bit in the State field\r
 \r
   @param ErasePolarity   Erase Polarity  as defined by EFI_FVB2_ERASE_POLARITY\r
                          in the Attributes field.\r
   @param FfsHeader       Pointer to FFS File Header.\r
 \r
-  @return Returns the highest bit in the State field\r
-\r
+  @retval EFI_FFS_FILE_STATE File state is set by the highest none zero bit \r
+                             in the header State field.\r
 **/\r
 EFI_FFS_FILE_STATE\r
 GetFileState(\r
@@ -48,7 +48,10 @@ GetFileState(
   if (ErasePolarity != 0) {\r
     FileState = (EFI_FFS_FILE_STATE)~FileState;\r
   }\r
-\r
+  \r
+  //\r
+  // Get file state set by its highest none zero bit.\r
+  //\r
   HighestBit = 0x80;\r
   while (HighestBit != 0 && (HighestBit & FileState) == 0) {\r
     HighestBit >>= 1;\r
@@ -63,7 +66,6 @@ GetFileState(
   @param FileHeader      Pointer to FFS File Header.\r
 \r
   @return Checksum of the header.\r
-          The header is zero byte checksum.\r
           Zero means the header is good.\r
           Non-zero means the header is bad.\r
 **/\r
@@ -72,43 +74,26 @@ CalculateHeaderChecksum (
   IN EFI_FFS_FILE_HEADER  *FileHeader\r
   )\r
 {\r
-  UINT8   *Ptr;\r
-  UINTN   Index;\r
-  UINT8   Sum;\r
-  \r
-  Sum = 0;\r
-  Ptr = (UINT8 *)FileHeader;\r
-\r
-  for (Index = 0; Index < sizeof(EFI_FFS_FILE_HEADER) - 3; Index += 4) {\r
-    Sum = (UINT8)(Sum + Ptr[Index]);\r
-    Sum = (UINT8)(Sum + Ptr[Index+1]);\r
-    Sum = (UINT8)(Sum + Ptr[Index+2]);\r
-    Sum = (UINT8)(Sum + Ptr[Index+3]);\r
-  }\r
-\r
-  for (; Index < sizeof(EFI_FFS_FILE_HEADER); Index++) {\r
-    Sum = (UINT8)(Sum + Ptr[Index]);\r
-  }\r
+  EFI_FFS_FILE_HEADER TestFileHeader;\r
   \r
+  CopyMem (&TestFileHeader, FileHeader, sizeof (EFI_FFS_FILE_HEADER));\r
   //\r
-  // State field (since this indicates the different state of file). \r
+  // Ingore State and File field in FFS header.\r
   //\r
-  Sum = (UINT8)(Sum - FileHeader->State);\r
-  //\r
-  // Checksum field of the file is not part of the header checksum.\r
-  //\r
-  Sum = (UINT8)(Sum - FileHeader->IntegrityCheck.Checksum.File);\r
+  TestFileHeader.State = 0;\r
+  TestFileHeader.IntegrityCheck.Checksum.File = 0;\r
 \r
-  return Sum;\r
+  return CalculateSum8 ((CONST UINT8 *) &TestFileHeader, sizeof (EFI_FFS_FILE_HEADER));\r
 }\r
 \r
 /**\r
   Find FV handler according some FileHandle in that FV.\r
 \r
   @param FileHandle      Handle of file image\r
-  @param VolumeHandle    Handle of FV\r
+  @param VolumeHandle    Handle of the found FV, if not found, NULL will be set.\r
 \r
-  @return EDES_TODO: Add description for return value\r
+  @retval TRUE           The corresponding FV handler is found.\r
+  @retval FALSE          The corresponding FV handler is not found.\r
 \r
 **/\r
 BOOLEAN\r
@@ -131,14 +116,17 @@ PeiFileHandleToVolume (
       return TRUE;\r
     }\r
   }\r
+  *VolumeHandle = NULL;\r
   return FALSE;\r
 }\r
 \r
 /**\r
-  Given the input file pointer, search for the next matching file in the\r
+  Given the input file pointer, search for the first matching file in the\r
   FFS volume as defined by SearchType. The search starts from FileHeader inside\r
   the Firmware Volume defined by FwVolHeader.\r
-\r
+  If SearchType is EFI_FV_FILETYPE_ALL, the first FFS file will return without check its file type.\r
+  If SearchType is PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE, \r
+  the first PEIM, or COMBINED PEIM or FV file type FFS file will return.  \r
 \r
   @param FvHandle        Pointer to the FV header of the volume to search\r
   @param FileName        File name\r
@@ -175,7 +163,7 @@ PeiFindFileEx (
   FileHeader  = (EFI_FFS_FILE_HEADER **)FileHandle;\r
 \r
   FvLength = FwVolHeader->FvLength;\r
-  if (FwVolHeader->Attributes & EFI_FVB2_ERASE_POLARITY) {\r
+  if ((FwVolHeader->Attributes & EFI_FVB2_ERASE_POLARITY) != 0) {\r
     ErasePolarity = 1;\r
   } else {\r
     ErasePolarity = 0;\r
@@ -311,13 +299,15 @@ PeiInitializeFv (
 \r
 /**\r
   Process Firmware Volum Information once FvInfoPPI install.\r
+  The FV Info will be registered into PeiCore private data structure.\r
+  And search the inside FV image, if found, the new FV INFO PPI will be installed.\r
 \r
+  @param PeiServices       An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
+  @param NotifyDescriptor  Address of the notification descriptor data structure.\r
+  @param Ppi               Address of the PPI that was installed.\r
 \r
-  @param PeiServices     - General purpose services available to every PEIM.\r
-  @param NotifyDescriptor EDES_TODO: Add parameter description\r
-  @param Ppi             EDES_TODO: Add parameter description\r
-\r
-  @retval EFI_SUCCESS if the interface could be successfully installed\r
+  @retval EFI_SUCCESS    The FV Info is registered into PeiCore private data structure.\r
+  @return if not EFI_SUCESS, fail to verify FV.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -353,6 +343,13 @@ FirmwareVolmeInfoPpiNotifyCallback (
         return EFI_SUCCESS;\r
       }\r
     }\r
+    \r
+    Status = VerifyFv ((EFI_FIRMWARE_VOLUME_HEADER*)Fv->FvInfo);\r
+    if (EFI_ERROR(Status)) {\r
+      DEBUG ((EFI_D_ERROR, "Fail to verify FV which address is 0x%11p", (VOID *) Fv->FvInfo));\r
+      return Status;\r
+    }\r
+    \r
     PrivateData->Fv[PrivateData->FvCount++].FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*)Fv->FvInfo;\r
 \r
     //\r
@@ -360,7 +357,7 @@ FirmwareVolmeInfoPpiNotifyCallback (
     //\r
     PrivateData->AllFv[PrivateData->AllFvCount++] = (EFI_PEI_FV_HANDLE)Fv->FvInfo;\r
     \r
-    DEBUG ((EFI_D_INFO, "The %dth FvImage start address is 0x%10p and size is 0x%08x\n", PrivateData->AllFvCount, (VOID *) Fv->FvInfo, Fv->FvInfoSize));\r
+    DEBUG ((EFI_D_INFO, "The %dth FvImage start address is 0x%11p and size is 0x%08x\n", (UINT32)PrivateData->AllFvCount, (VOID *) Fv->FvInfo, Fv->FvInfoSize));\r
     //\r
     // Preprocess all FV type files in this new FileSystem2 Fv image\r
     //\r
@@ -390,7 +387,7 @@ FirmwareVolmeInfoPpiNotifyCallback (
         //\r
         // Process FvFile to install FvInfo ppi and build FvHob\r
         // \r
-        ProcessFvFile (PeiServices, FileHandle, &AuthenticationStatus);\r
+        ProcessFvFile ((CONST EFI_PEI_SERVICES **) PeiServices, FileHandle, &AuthenticationStatus);\r
       }\r
     } while (FileHandle != NULL);\r
   }\r
@@ -399,19 +396,19 @@ FirmwareVolmeInfoPpiNotifyCallback (
 }\r
 \r
 /**\r
-\r
-  Go through the file to search SectionType section,\r
-  when meeting an encapsuled section.\r
-\r
-\r
-  @param PeiServices     - General purpose services available to every PEIM.\r
-                         SearchType   - Filter to find only section of this type.\r
-  @param SectionType     EDES_TODO: Add parameter description\r
+  Go through the file to search SectionType section. \r
+  Search within encapsulation sections (compression and GUIDed) recursively, \r
+  until the match section is found.\r
+  \r
+  @param PeiServices     - An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
+  @param SectionType     - Filter to find only section of this type.\r
   @param Section         - From where to search.\r
   @param SectionSize     - The file size to search.\r
-  @param OutputBuffer    - Pointer to the section to search.\r
+  @param OutputBuffer    - A pointer to the discovered section, if successful.\r
+                           NULL if section not found\r
 \r
-  @return EFI_STATUS\r
+  @return EFI_NOT_FOUND    The match section is not found.\r
+  @return EFI_SUCCESS      The match section is found.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -534,14 +531,13 @@ PeiFfsProcessSection (
 \r
 \r
 /**\r
-  Given the input file pointer, search for the next matching section in the\r
+  Given the input file pointer, search for the first matching section in the\r
   FFS volume.\r
 \r
-\r
-  @param PeiServices     Pointer to the PEI Core Services Table.\r
+  @param PeiServices     An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
   @param SectionType     Filter to find only sections of this type.\r
   @param FileHandle      Pointer to the current file to search.\r
-  @param SectionData     Pointer to the Section matching SectionType in FfsFileHeader.\r
+  @param SectionData     A pointer to the discovered section, if successful.\r
                          NULL if section not found\r
 \r
   @retval EFI_NOT_FOUND  No files matching the search criteria were found\r
@@ -587,7 +583,7 @@ PeiFfsFindSectionData (
   the Firmware Volume defined by FwVolHeader.\r
 \r
 \r
-  @param PeiServices     Pointer to the PEI Core Services Table.\r
+  @param PeiServices     An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
   @param SearchType      Filter to find only files of this type.\r
                          Type EFI_FV_FILETYPE_ALL causes no filtering to be done.\r
   @param VolumeHandle    Pointer to the FV header of the volume to search.\r
@@ -611,9 +607,9 @@ PeiFfsFindNextFile (
 \r
 \r
 /**\r
-  search the firmware volumes by index\r
+  Search the firmware volumes by index\r
 \r
-  @param PeiServices     The PEI core services table.\r
+  @param PeiServices     An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
   @param Instance        Instance of FV to find\r
   @param VolumeHandle    Pointer to found Volume.\r
 \r
@@ -624,17 +620,51 @@ PeiFfsFindNextFile (
 EFI_STATUS \r
 EFIAPI\r
 PeiFvFindNextVolume (\r
-  IN CONST EFI_PEI_SERVICES           **PeiServices,\r
+  IN CONST EFI_PEI_SERVICES         **PeiServices,\r
   IN     UINTN                      Instance,\r
   IN OUT EFI_PEI_FV_HANDLE          *VolumeHandle\r
   )\r
 {\r
-  PEI_CORE_INSTANCE   *Private;\r
+  PEI_CORE_INSTANCE        *Private;\r
+  UINTN                    Index;\r
+  BOOLEAN                  Match;\r
+  EFI_HOB_FIRMWARE_VOLUME  *FvHob;\r
 \r
   Private = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
   if (VolumeHandle == NULL) {\r
    return EFI_INVALID_PARAMETER;\r
-  } \r
+  }\r
+  \r
+  //\r
+  // Handle Framework FvHob and Install FvInfo Ppi for it.\r
+  //\r
+  if (FeaturePcdGet (PcdFrameworkFvHobCompatibilitySupport)) {\r
+    //\r
+    // Loop to search the wanted FirmwareVolume which supports FFS\r
+    //\r
+    FvHob = (EFI_HOB_FIRMWARE_VOLUME *)GetFirstHob (EFI_HOB_TYPE_FV);\r
+    while (FvHob != NULL) {\r
+      for (Index = 0, Match = FALSE; Index < Private->AllFvCount; Index++) {\r
+        if ((EFI_PEI_FV_HANDLE)(UINTN)FvHob->BaseAddress == Private->AllFv[Index]) {\r
+          Match = TRUE;\r
+          break;\r
+        }\r
+      }\r
+      //\r
+      // If Not Found, Install FvInfo Ppi for it.\r
+      //\r
+      if (!Match) {\r
+        PeiServicesInstallFvInfoPpi (\r
+          NULL,\r
+          (VOID *)(UINTN)FvHob->BaseAddress,\r
+          (UINT32)FvHob->Length,\r
+          NULL,\r
+          NULL\r
+          );\r
+      }\r
+      FvHob = (EFI_HOB_FIRMWARE_VOLUME *)GetNextHob (EFI_HOB_TYPE_FV, (VOID *)((UINTN)FvHob + FvHob->Header.HobLength)); \r
+    }\r
+  }\r
 \r
   if (Instance >= Private->AllFvCount) {\r
    VolumeHandle = NULL;\r
@@ -647,16 +677,16 @@ PeiFvFindNextVolume (
 \r
 \r
 /**\r
+  Find a file within a volume by its name.\r
 \r
-  Given the input VolumeHandle, search for the next matching name file.\r
-\r
+  @param FileName        A pointer to the name of the file to find within the firmware volume.\r
+  @param VolumeHandle    The firmware volume to search\r
+  @param FileHandle      Upon exit, points to the found file's handle \r
+                         or NULL if it could not be found.\r
 \r
-  @param FileName        - File name to search.\r
-  @param VolumeHandle    - The current FV to search.\r
-  @param FileHandle      - Pointer to the file matching name in VolumeHandle.\r
-                         - NULL if file not found\r
-\r
-  @return EFI_STATUS\r
+  @retval EFI_SUCCESS            File was found.\r
+  @retval EFI_NOT_FOUND          File was not found.\r
+  @retval EFI_INVALID_PARAMETER  VolumeHandle or FileHandle or FileName was NULL.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -791,3 +821,126 @@ PeiFfsGetVolumeInfo (
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Get Fv image from the FV type file, then install FV INFO ppi, Build FV hob.\r
+\r
+  @param PeiServices          An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
+  @param FvFileHandle         File handle of a Fv type file.\r
+  @param AuthenticationState  Pointer to attestation authentication state of image.\r
+\r
+\r
+  @retval EFI_NOT_FOUND         FV image can't be found.\r
+  @retval EFI_SUCCESS           Successfully to process it.\r
+  @retval EFI_OUT_OF_RESOURCES  Can not allocate page when aligning FV image\r
+  @retval Others                Can not find EFI_SECTION_FIRMWARE_VOLUME_IMAGE section\r
+  \r
+**/\r
+EFI_STATUS\r
+ProcessFvFile (\r
+  IN  CONST EFI_PEI_SERVICES      **PeiServices,\r
+  IN  EFI_PEI_FILE_HANDLE         FvFileHandle,\r
+  OUT UINT32                      *AuthenticationState\r
+  )\r
+{\r
+  EFI_STATUS            Status;\r
+  EFI_PEI_FV_HANDLE     FvImageHandle;\r
+  EFI_FV_INFO           FvImageInfo;\r
+  UINT32                FvAlignment;\r
+  VOID                  *FvBuffer;\r
+  EFI_PEI_HOB_POINTERS  HobPtr;\r
+\r
+  FvBuffer             = NULL;\r
+  *AuthenticationState = 0;\r
+\r
+  //\r
+  // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has already\r
+  // been extracted.\r
+  //\r
+  HobPtr.Raw = GetHobList ();\r
+  while ((HobPtr.Raw = GetNextHob (EFI_HOB_TYPE_FV2, HobPtr.Raw)) != NULL) {\r
+    if (CompareGuid (&(((EFI_FFS_FILE_HEADER *)FvFileHandle)->Name), &HobPtr.FirmwareVolume2->FileName)) {\r
+      //\r
+      // this FILE has been dispatched, it will not be dispatched again.\r
+      //\r
+      return EFI_SUCCESS;\r
+    }\r
+    HobPtr.Raw = GET_NEXT_HOB (HobPtr);\r
+  }\r
+\r
+  //\r
+  // Find FvImage in FvFile\r
+  //\r
+  Status = PeiFfsFindSectionData (\r
+             PeiServices,\r
+             EFI_SECTION_FIRMWARE_VOLUME_IMAGE,\r
+             FvFileHandle,\r
+             (VOID **)&FvImageHandle\r
+             );\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+  \r
+  //\r
+  // Collect FvImage Info.\r
+  //\r
+  Status = PeiFfsGetVolumeInfo (FvImageHandle, &FvImageInfo);\r
+  ASSERT_EFI_ERROR (Status);\r
+  \r
+  //\r
+  // FvAlignment must be more than 8 bytes required by FvHeader structure.\r
+  //\r
+  FvAlignment = 1 << ((FvImageInfo.FvAttributes & EFI_FVB2_ALIGNMENT) >> 16);\r
+  if (FvAlignment < 8) {\r
+    FvAlignment = 8;\r
+  }\r
+  \r
+  //\r
+  // Check FvImage\r
+  //\r
+  if ((UINTN) FvImageInfo.FvStart % FvAlignment != 0) {\r
+    FvBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES ((UINT32) FvImageInfo.FvSize), FvAlignment);\r
+    if (FvBuffer == NULL) {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+    CopyMem (FvBuffer, FvImageInfo.FvStart, (UINTN) FvImageInfo.FvSize);\r
+    //\r
+    // Update FvImageInfo after reload FvImage to new aligned memory\r
+    //\r
+    PeiFfsGetVolumeInfo ((EFI_PEI_FV_HANDLE) FvBuffer, &FvImageInfo);\r
+  }\r
+\r
+  //\r
+  // Install FvPpi and Build FvHob\r
+  //\r
+  PeiServicesInstallFvInfoPpi (\r
+    NULL,\r
+    FvImageInfo.FvStart,\r
+    (UINT32) FvImageInfo.FvSize,\r
+    &(FvImageInfo.FvName),\r
+    &(((EFI_FFS_FILE_HEADER*)FvFileHandle)->Name)\r
+    );\r
+\r
+  //\r
+  // Inform the extracted FvImage to Fv HOB consumer phase, i.e. DXE phase\r
+  //\r
+  BuildFvHob (\r
+    (EFI_PHYSICAL_ADDRESS) (UINTN) FvImageInfo.FvStart,\r
+    FvImageInfo.FvSize\r
+  );\r
+\r
+  //\r
+  // Makes the encapsulated volume show up in DXE phase to skip processing of\r
+  // encapsulated file again.\r
+  //\r
+  BuildFv2Hob (\r
+    (EFI_PHYSICAL_ADDRESS) (UINTN) FvImageInfo.FvStart,\r
+    FvImageInfo.FvSize,\r
+    &FvImageInfo.FvName,\r
+    &(((EFI_FFS_FILE_HEADER *)FvFileHandle)->Name)\r
+    );\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r