]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Vlv2TbltDevicePkg/Library/PlatformBdsLib/BdsPlatform.c
Vlv2TbltDevicePkg/Bds: use Tcg2 instead of TrEE.
[mirror_edk2.git] / Vlv2TbltDevicePkg / Library / PlatformBdsLib / BdsPlatform.c
index e7e4a919253304b0a970ebfe4ecd97f51bf07841..e42e82b678b592eb956480a600682b5b62e347ab 100644 (file)
@@ -1,15 +1,15 @@
 /** @file\r
 \r
-  Copyright (c) 2004  - 2014, Intel Corporation. All rights reserved.<BR>\r
-                                                                                   \r\r
-  This program and the accompanying materials are licensed and made available under\r\r
-  the terms and conditions of the BSD License that accompanies this distribution.  \r\r
-  The full text of the license may be found at                                     \r\r
-  http://opensource.org/licenses/bsd-license.php.                                  \r\r
-                                                                                   \r\r
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,            \r\r
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.    \r\r
-                                                                                   \r\r
+  Copyright (c) 2004  - 2016, Intel Corporation. All rights reserved.<BR>\r
+                                                                                   \r
+  This program and the accompanying materials are licensed and made available under\r
+  the terms and conditions of the BSD License that accompanies this distribution.  \r
+  The full text of the license may be found at                                     \r
+  http://opensource.org/licenses/bsd-license.php.                                  \r
+                                                                                   \r
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,            \r
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.    \r
+                                                                                   \r
 \r
 \r
 Module Name:\r
@@ -27,7 +27,7 @@ Abstract:
 #include "SetupMode.h"\r
 #include <Guid/SetupVariable.h>\r
 #include <Library/TcgPhysicalPresenceLib.h>\r
-#include <Library/TrEEPhysicalPresenceLib.h>\r
+#include <Library/Tcg2PhysicalPresenceLib.h>\r
 #include <Protocol/I2cMasterMcg.h>\r
 #include <TianoApi.h>\r
 #include <PlatformBaseAddresses.h>\r
@@ -41,6 +41,12 @@ Abstract:
 #include <Library/SerialPortLib.h>\r
 #include <Library/DebugLib.h>\r
 \r
+#include <Library/GenericBdsLib/InternalBdsLib.h>\r
+#include <Library/GenericBdsLib/String.h>\r
+#include <Library/NetLib.h>\r
+\r
+#include <Library/CapsuleLib.h>\r
+#include <Protocol/EsrtManagement.h>\r
 \r
 EFI_GUID *ConnectDriverTable[] = {\r
   &gEfiMmioDeviceProtocolGuid,\r
@@ -178,6 +184,11 @@ InstallReadyToLock (
                     );\r
     ASSERT_EFI_ERROR (Status);\r
 \r
+    //\r
+    // Signal EndOfDxe PI Event\r
+    //\r
+    EfiEventGroupSignal (&gEfiEndOfDxeEventGroupGuid);\r
+\r
     Handle = NULL;\r
     Status = gBS->InstallProtocolInterface (\r
                     &Handle,\r
@@ -206,7 +217,7 @@ ShellImageCallback (
 // BDS Platform Functions\r
 //\r
 /**\r
-  Platform Bds init. Incude the platform firmware vendor, revision\r
+  Platform Bds init. Include the platform firmware vendor, revision\r
   and so crc check.\r
 \r
   @param VOID\r
@@ -302,7 +313,7 @@ GetGopDevicePath (
   }\r
 \r
   //\r
-  // Try to connect this handle, so that GOP dirver could start on this\r
+  // Try to connect this handle, so that GOP driver could start on this\r
   // device and create child handles with GraphicsOutput Protocol installed\r
   // on them, then we get device paths of these child handles and select\r
   // them as possible console device.\r
@@ -319,7 +330,18 @@ GetGopDevicePath (
                   &VarSize,\r
                   &mSystemConfiguration\r
                   );\r
-  ASSERT_EFI_ERROR (Status);\r
+  if (EFI_ERROR (Status) || VarSize != sizeof(SYSTEM_CONFIGURATION)) {\r
+    //The setup variable is corrupted\r
+    VarSize = sizeof(SYSTEM_CONFIGURATION);\r
+    Status = gRT->GetVariable(\r
+              L"SetupRecovery",\r
+              &gEfiNormalSetupGuid,\r
+              NULL,\r
+              &VarSize,\r
+              &mSystemConfiguration\r
+              );\r
+    ASSERT_EFI_ERROR (Status);\r
+  }    \r
 \r
   if(mSystemConfiguration.BootDisplayDevice != 0x0)\r
   {\r
@@ -408,7 +430,7 @@ GetGopDevicePath (
         // In current implementation, we only enable one of the child handles\r
         // as console device, i.e. sotre one of the child handle's device\r
         // path to variable "ConOut"\r
-        // In futhure, we could select all child handles to be console device\r
+        // In future, we could select all child handles to be console device\r
         //\r
         *GopDevicePath = TempDevicePath;\r
       }\r
@@ -621,7 +643,18 @@ PlatformBdsForceActiveVga (
                   &VarSize,\r
                   &mSystemConfiguration\r
                   );\r
-  ASSERT_EFI_ERROR (Status);\r
+  if (EFI_ERROR (Status) || VarSize != sizeof(SYSTEM_CONFIGURATION)) {\r
+    //The setup variable is corrupted\r
+    VarSize = sizeof(SYSTEM_CONFIGURATION);\r
+    Status = gRT->GetVariable(\r
+              L"SetupRecovery",\r
+              &gEfiNormalSetupGuid,\r
+              NULL,\r
+              &VarSize,\r
+              &mSystemConfiguration\r
+              );\r
+    ASSERT_EFI_ERROR (Status);\r
+  }    \r
 \r
 \r
   if ((PlugInPciVgaDevicePath == NULL && OnboardPciVgaDevicePath != NULL) ) {\r
@@ -677,7 +710,18 @@ UpdateConsoleResolution(
                   &VarSize,\r
                   &SystemConfiguration\r
                   );\r
-  ASSERT_EFI_ERROR (Status);\r
+  if (EFI_ERROR (Status) || VarSize != sizeof(SYSTEM_CONFIGURATION)) {\r
+    //The setup variable is corrupted\r
+    VarSize = sizeof(SYSTEM_CONFIGURATION);\r
+    Status = gRT->GetVariable(\r
+              L"SetupRecovery",\r
+              &gEfiNormalSetupGuid,\r
+              NULL,\r
+              &VarSize,\r
+              &SystemConfiguration\r
+              );\r
+    ASSERT_EFI_ERROR (Status);\r
+  }  \r
 \r
   switch (SystemConfiguration.IgdFlatPanel) {\r
 \r
@@ -739,7 +783,7 @@ UpdateConsoleResolution(
   Connect the predefined platform default console device. Always try to find\r
   and enable the vga device if have.\r
 \r
-  @param PlatformConsole    Predfined platform default console device array.\r
+  @param PlatformConsole    Predefined platform default console device array.\r
 \r
   @retval EFI_SUCCESS       Success connect at least one ConIn and ConOut\r
                             device, there must have one ConOut device is\r
@@ -778,7 +822,7 @@ PlatformBdsConnectConsole (
   if (VarConout == NULL || VarConin == NULL) {\r
     //\r
     // Have chance to connect the platform default console,\r
-    // the platform default console is the minimue device group\r
+    // the platform default console is the minimum device group\r
     // the platform should support\r
     //\r
     while (PlatformConsole[Index].DevicePath != NULL) {\r
@@ -825,7 +869,7 @@ PlatformBdsConnectConsole (
 }\r
 \r
 /**\r
-  Connect with predeined platform connect sequence,\r
+  Connect with predefined platform connect sequence,\r
   the OEM/IBV can customize with their own connect sequence.\r
 \r
   @param None.\r
@@ -983,8 +1027,551 @@ PlatformBdsDiagnostics (
 \r
 \r
 /**\r
+  For EFI boot option, BDS separate them as six types:\r
+  1. Network - The boot option points to the SimpleNetworkProtocol device.\r
+               Bds will try to automatically create this type boot option when enumerate.\r
+  2. Shell   - The boot option points to internal flash shell.\r
+               Bds will try to automatically create this type boot option when enumerate.\r
+  3. Removable BlockIo      - The boot option only points to the removable media\r
+                              device, like USB flash disk, DVD, Floppy etc.\r
+                              These device should contain a *removable* blockIo\r
+                              protocol in their device handle.\r
+                              Bds will try to automatically create this type boot option\r
+                              when enumerate.\r
+  4. Fixed BlockIo          - The boot option only points to a Fixed blockIo device,\r
+                              like HardDisk.\r
+                              These device should contain a *fixed* blockIo\r
+                              protocol in their device handle.\r
+                              BDS will skip fixed blockIo devices, and NOT\r
+                              automatically create boot option for them. But BDS\r
+                              will help to delete those fixed blockIo boot option,\r
+                              whose description rule conflict with other auto-created\r
+                              boot options.\r
+  5. Non-BlockIo Simplefile - The boot option points to a device whose handle\r
+                              has SimpleFileSystem Protocol, but has no blockio\r
+                              protocol. These devices do not offer blockIo\r
+                              protocol, but BDS still can get the\r
+                              \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem\r
+                              Protocol.\r
+  6. File    - The boot option points to a file. These boot options are usually\r
+               created by user manually or OS loader. BDS will not delete or modify\r
+               these boot options.\r
+\r
+  This function will enumerate all possible boot device in the system, and\r
+  automatically create boot options for Network, Shell, Removable BlockIo,\r
+  and Non-BlockIo Simplefile devices.\r
+  It will only execute once of every boot.\r
+\r
+  @param  BdsBootOptionList      The header of the link list which indexed all\r
+                                 current boot options\r
+\r
+  @retval EFI_SUCCESS            Finished all the boot device enumerate and create\r
+                                 the boot option base on that boot device\r
+\r
+  @retval EFI_OUT_OF_RESOURCES   Failed to enumerate the boot device and create the boot option list\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+PlatformBdsLibEnumerateAllBootOption (\r
+  IN OUT LIST_ENTRY          *BdsBootOptionList\r
+  )\r
+{\r
+  EFI_STATUS                    Status;\r
+  UINT16                        FloppyNumber;\r
+  UINT16                        HarddriveNumber;\r
+  UINT16                        CdromNumber;\r
+  UINT16                        UsbNumber;\r
+  UINT16                        MiscNumber;\r
+  UINT16                        ScsiNumber;\r
+  UINT16                        NonBlockNumber;\r
+  UINTN                         NumberBlockIoHandles;\r
+  EFI_HANDLE                    *BlockIoHandles;\r
+  EFI_BLOCK_IO_PROTOCOL         *BlkIo;\r
+  BOOLEAN                       Removable[2];\r
+  UINTN                         RemovableIndex;\r
+  UINTN                         Index;\r
+  UINTN                         NumOfLoadFileHandles;\r
+  EFI_HANDLE                    *LoadFileHandles;\r
+  UINTN                         FvHandleCount;\r
+  EFI_HANDLE                    *FvHandleBuffer;\r
+  EFI_FV_FILETYPE               Type;\r
+  UINTN                         Size;\r
+  EFI_FV_FILE_ATTRIBUTES        Attributes;\r
+  UINT32                        AuthenticationStatus;\r
+  EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
+  EFI_DEVICE_PATH_PROTOCOL      *DevicePath;\r
+  UINTN                         DevicePathType;\r
+  CHAR16                        Buffer[40];\r
+  EFI_HANDLE                    *FileSystemHandles;\r
+  UINTN                         NumberFileSystemHandles;\r
+  BOOLEAN                       NeedDelete;\r
+  EFI_IMAGE_DOS_HEADER          DosHeader;\r
+  CHAR8                         *PlatLang;\r
+  CHAR8                         *LastLang;\r
+  EFI_IMAGE_OPTIONAL_HEADER_UNION       HdrData;\r
+  EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION   Hdr;\r
+  CHAR16                        *MacStr;\r
+  CHAR16                        *IPverStr;\r
+  EFI_HANDLE                    *NetworkHandles;\r
+  UINTN                         BufferSize;\r
+\r
+  FloppyNumber    = 0;\r
+  HarddriveNumber = 0;\r
+  CdromNumber     = 0;\r
+  UsbNumber       = 0;\r
+  MiscNumber      = 0;\r
+  ScsiNumber      = 0;\r
+  PlatLang        = NULL;\r
+  LastLang        = NULL;\r
+  ZeroMem (Buffer, sizeof (Buffer));\r
+\r
+  //\r
+  // If the boot device enumerate happened, just get the boot\r
+  // device from the boot order variable\r
+  //\r
+  if (mEnumBootDevice) {\r
+    GetVariable2 (LAST_ENUM_LANGUAGE_VARIABLE_NAME, &gLastEnumLangGuid, (VOID**)&LastLang, NULL);\r
+    GetEfiGlobalVariable2 (L"PlatformLang", (VOID**)&PlatLang, NULL);\r
+    ASSERT (PlatLang != NULL);\r
+    if ((LastLang != NULL) && (AsciiStrCmp (LastLang, PlatLang) == 0)) {\r
+      Status = BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");\r
+      FreePool (LastLang);\r
+      FreePool (PlatLang);\r
+      return Status;\r
+    } else {\r
+      Status = gRT->SetVariable (\r
+        LAST_ENUM_LANGUAGE_VARIABLE_NAME,\r
+        &gLastEnumLangGuid,\r
+        EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
+        AsciiStrSize (PlatLang),\r
+        PlatLang\r
+        );\r
+      //\r
+      // Failure to set the variable only impacts the performance next time enumerating the boot options.\r
+      //\r
+\r
+      if (LastLang != NULL) {\r
+        FreePool (LastLang);\r
+      }\r
+      FreePool (PlatLang);\r
+    }\r
+  }\r
 \r
-  The function will excute with as the platform policy, current policy\r
+  //\r
+  // Notes: this dirty code is to get the legacy boot option from the\r
+  // BBS table and create to variable as the EFI boot option, it should\r
+  // be removed after the CSM can provide legacy boot option directly\r
+  //\r
+  REFRESH_LEGACY_BOOT_OPTIONS;\r
+\r
+  //\r
+  // Delete invalid boot option\r
+  //\r
+  BdsDeleteAllInvalidEfiBootOption ();\r
+\r
+  //\r
+  // Parse removable media followed by fixed media.\r
+  // The Removable[] array is used by the for-loop below to create removable media boot options \r
+  // at first, and then to create fixed media boot options.\r
+  //\r
+  Removable[0]  = FALSE;\r
+  Removable[1]  = TRUE;\r
+\r
+  gBS->LocateHandleBuffer (\r
+        ByProtocol,\r
+        &gEfiBlockIoProtocolGuid,\r
+        NULL,\r
+        &NumberBlockIoHandles,\r
+        &BlockIoHandles\r
+        );\r
+\r
+  for (RemovableIndex = 0; RemovableIndex < 2; RemovableIndex++) {\r
+    for (Index = 0; Index < NumberBlockIoHandles; Index++) {\r
+      Status = gBS->HandleProtocol (\r
+                      BlockIoHandles[Index],\r
+                      &gEfiBlockIoProtocolGuid,\r
+                      (VOID **) &BlkIo\r
+                      );\r
+      //\r
+      // skip the logical partition\r
+      //\r
+      if (EFI_ERROR (Status) || BlkIo->Media->LogicalPartition) {\r
+        continue;\r
+      }\r
+\r
+      //\r
+      // firstly fixed block io then the removable block io\r
+      //\r
+      if (BlkIo->Media->RemovableMedia == Removable[RemovableIndex]) {\r
+        continue;\r
+      }\r
+      DevicePath  = DevicePathFromHandle (BlockIoHandles[Index]);\r
+      DevicePathType = BdsGetBootTypeFromDevicePath (DevicePath);\r
+\r
+      switch (DevicePathType) {\r
+      case BDS_EFI_ACPI_FLOPPY_BOOT:\r
+        if (FloppyNumber != 0) {\r
+          UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY)), FloppyNumber);\r
+        } else {\r
+          UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY)));\r
+        }\r
+        BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
+        FloppyNumber++;\r
+        break;\r
+\r
+      //\r
+      // Assume a removable SATA device should be the DVD/CD device, a fixed SATA device should be the Hard Drive device.\r
+      //\r
+      case BDS_EFI_MESSAGE_ATAPI_BOOT:\r
+      case BDS_EFI_MESSAGE_SATA_BOOT:\r
+        if (BlkIo->Media->RemovableMedia) {\r
+          if (CdromNumber != 0) {\r
+            UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD)), CdromNumber);\r
+          } else {\r
+            UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD)));\r
+          }\r
+          CdromNumber++;\r
+        } else {\r
+          if (HarddriveNumber != 0) {\r
+            UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE)), HarddriveNumber);\r
+          } else {\r
+            UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE)));\r
+          }\r
+          HarddriveNumber++;\r
+        }\r
+        DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Buffer: %S\n", Buffer));\r
+        BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
+        break;\r
+\r
+      case BDS_EFI_MESSAGE_USB_DEVICE_BOOT:\r
+        if (UsbNumber != 0) {\r
+          UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB)), UsbNumber);\r
+        } else {\r
+          UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB)));\r
+        }\r
+        BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
+        UsbNumber++;\r
+        break;\r
+\r
+      case BDS_EFI_MESSAGE_SCSI_BOOT:\r
+        if (ScsiNumber != 0) {\r
+          UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI)), ScsiNumber);\r
+        } else {\r
+          UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI)));\r
+        }\r
+        BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
+        ScsiNumber++;\r
+        break;\r
+\r
+      case BDS_EFI_MESSAGE_MISC_BOOT:\r
+      default:\r
+        if (MiscNumber != 0) {\r
+          UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC)), MiscNumber);\r
+        } else {\r
+          UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC)));\r
+        }\r
+        BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
+        MiscNumber++;\r
+        break;\r
+      }\r
+    }\r
+  }\r
+\r
+  if (NumberBlockIoHandles != 0) {\r
+    FreePool (BlockIoHandles);\r
+  }\r
+\r
+  //\r
+  // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.\r
+  //\r
+  NonBlockNumber = 0;\r
+  gBS->LocateHandleBuffer (\r
+        ByProtocol,\r
+        &gEfiSimpleFileSystemProtocolGuid,\r
+        NULL,\r
+        &NumberFileSystemHandles,\r
+        &FileSystemHandles\r
+        );\r
+  for (Index = 0; Index < NumberFileSystemHandles; Index++) {\r
+    Status = gBS->HandleProtocol (\r
+                    FileSystemHandles[Index],\r
+                    &gEfiBlockIoProtocolGuid,\r
+                    (VOID **) &BlkIo\r
+                    );\r
+     if (!EFI_ERROR (Status)) {\r
+      //\r
+      //  Skip if the file system handle supports a BlkIo protocol,\r
+      //\r
+      continue;\r
+    }\r
+\r
+    //\r
+    // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI\r
+    //  machinename is ia32, ia64, x64, ...\r
+    //\r
+    Hdr.Union  = &HdrData;\r
+    NeedDelete = TRUE;\r
+    Status     = BdsLibGetImageHeader (\r
+                   FileSystemHandles[Index],\r
+                   EFI_REMOVABLE_MEDIA_FILE_NAME,\r
+                   &DosHeader,\r
+                   Hdr\r
+                   );\r
+    if (!EFI_ERROR (Status) &&\r
+        EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr.Pe32->FileHeader.Machine) &&\r
+        Hdr.Pe32->OptionalHeader.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {\r
+      NeedDelete = FALSE;\r
+    }\r
+\r
+    if (NeedDelete) {\r
+      //\r
+      // No such file or the file is not a EFI application, delete this boot option\r
+      //\r
+      BdsLibDeleteOptionFromHandle (FileSystemHandles[Index]);\r
+    } else {\r
+      if (NonBlockNumber != 0) {\r
+        UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK)), NonBlockNumber);\r
+      } else {\r
+        UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK)));\r
+      }\r
+      BdsLibBuildOptionFromHandle (FileSystemHandles[Index], BdsBootOptionList, Buffer);\r
+      NonBlockNumber++;\r
+    }\r
+  }\r
+\r
+  if (NumberFileSystemHandles != 0) {\r
+    FreePool (FileSystemHandles);\r
+  }\r
+\r
+  //\r
+  // Check if we have on flash shell\r
+  //\r
+  gBS->LocateHandleBuffer (\r
+        ByProtocol,\r
+        &gEfiFirmwareVolume2ProtocolGuid,\r
+        NULL,\r
+        &FvHandleCount,\r
+        &FvHandleBuffer\r
+        );\r
+  for (Index = 0; Index < FvHandleCount; Index++) {\r
+    gBS->HandleProtocol (\r
+          FvHandleBuffer[Index],\r
+          &gEfiFirmwareVolume2ProtocolGuid,\r
+          (VOID **) &Fv\r
+          );\r
+\r
+    Status = Fv->ReadFile (\r
+                  Fv,\r
+                  PcdGetPtr(PcdShellFile),\r
+                  NULL,\r
+                  &Size,\r
+                  &Type,\r
+                  &Attributes,\r
+                  &AuthenticationStatus\r
+                  );\r
+    if (EFI_ERROR (Status)) {\r
+      //\r
+      // Skip if no shell file in the FV\r
+      //\r
+      continue;\r
+    }\r
+    //\r
+    // Build the shell boot option\r
+    //\r
+    BdsLibBuildOptionFromShell (FvHandleBuffer[Index], BdsBootOptionList);\r
+  }\r
+\r
+  if (FvHandleCount != 0) {\r
+    FreePool (FvHandleBuffer);\r
+  }\r
+\r
+  //\r
+  // Parse Network Boot Device\r
+  //\r
+  NumOfLoadFileHandles = 0;\r
+  //\r
+  // Search Load File protocol for PXE boot option.\r
+  //\r
+  gBS->LocateHandleBuffer (\r
+        ByProtocol,\r
+        &gEfiLoadFileProtocolGuid,\r
+        NULL,\r
+        &NumOfLoadFileHandles,\r
+        &LoadFileHandles\r
+        );\r
+\r
+  for (Index = 0; Index < NumOfLoadFileHandles; Index++) {\r
+\r
+//\r
+//Locate EFI_DEVICE_PATH_PROTOCOL to dynamically get IPv4/IPv6 protocol information.\r
+//\r
+\r
+ Status = gBS->HandleProtocol (\r
+                  LoadFileHandles[Index],\r
+                  &gEfiDevicePathProtocolGuid,\r
+                  (VOID **) &DevicePath\r
+                  );\r
+  \r
+ ASSERT_EFI_ERROR (Status);\r
+\r
+  while (!IsDevicePathEnd (DevicePath)) {\r
+    if ((DevicePath->Type == MESSAGING_DEVICE_PATH) &&\r
+        (DevicePath->SubType == MSG_IPv4_DP)) {\r
+\r
+  //\r
+  //Get handle infomation\r
+  //\r
+  BufferSize = 0;\r
+  NetworkHandles = NULL;\r
+  Status = gBS->LocateHandle (\r
+                  ByProtocol, \r
+                  &gEfiSimpleNetworkProtocolGuid,\r
+                  NULL,\r
+                  &BufferSize,\r
+                  NetworkHandles\r
+                  );\r
+\r
+  if (Status == EFI_BUFFER_TOO_SMALL) {\r
+    NetworkHandles = AllocateZeroPool(BufferSize);\r
+    if (NetworkHandles == NULL) {\r
+      return (EFI_OUT_OF_RESOURCES);\r
+    }\r
+    Status = gBS->LocateHandle(\r
+                    ByProtocol,\r
+                    &gEfiSimpleNetworkProtocolGuid,\r
+                    NULL,\r
+                    &BufferSize,\r
+                    NetworkHandles\r
+                    );\r
+ }\r
+               \r
+  //\r
+  //Get the MAC string\r
+  //\r
+  Status = NetLibGetMacString (\r
+             *NetworkHandles,\r
+             NULL,\r
+             &MacStr\r
+             );\r
+  if (EFI_ERROR (Status)) {    \r
+    return Status;\r
+  }\r
+  IPverStr = L" IPv4";\r
+  UnicodeSPrint (Buffer, sizeof (Buffer), L"%s%s%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK)),MacStr,IPverStr);\r
+  break;\r
+  }\r
+    if((DevicePath->Type == MESSAGING_DEVICE_PATH) &&\r
+        (DevicePath->SubType == MSG_IPv6_DP)) {\r
+\r
+  //\r
+  //Get handle infomation\r
+  //\r
+  BufferSize = 0;\r
+  NetworkHandles = NULL;\r
+  Status = gBS->LocateHandle (\r
+                  ByProtocol, \r
+                  &gEfiSimpleNetworkProtocolGuid,\r
+                  NULL,\r
+                  &BufferSize,\r
+                  NetworkHandles\r
+                  );\r
+\r
+  if (Status == EFI_BUFFER_TOO_SMALL) {\r
+    NetworkHandles = AllocateZeroPool(BufferSize);\r
+    if (NetworkHandles == NULL) {\r
+       return (EFI_OUT_OF_RESOURCES);\r
+    }\r
+    Status = gBS->LocateHandle(\r
+                    ByProtocol,\r
+                    &gEfiSimpleNetworkProtocolGuid,\r
+                    NULL,\r
+                    &BufferSize,\r
+                    NetworkHandles\r
+                    );\r
+ }\r
+                    \r
+  //\r
+  //Get the MAC string\r
+  //\r
+  Status = NetLibGetMacString (\r
+             *NetworkHandles,\r
+             NULL,\r
+             &MacStr\r
+             );\r
+  if (EFI_ERROR (Status)) {    \r
+    return Status;\r
+  }\r
+      IPverStr = L" IPv6";\r
+      UnicodeSPrint (Buffer, sizeof (Buffer), L"%s%s%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK)),MacStr,IPverStr);\r
+      break;\r
+    }\r
+    DevicePath = NextDevicePathNode (DevicePath);\r
+  }\r
+  \r
+    BdsLibBuildOptionFromHandle (LoadFileHandles[Index], BdsBootOptionList, Buffer);\r
+  }\r
+\r
+  if (NumOfLoadFileHandles != 0) {\r
+    FreePool (LoadFileHandles);\r
+  }\r
+\r
+  //\r
+  // Check if we have on flash shell\r
+  //\r
+ /* gBS->LocateHandleBuffer (\r
+        ByProtocol,\r
+        &gEfiFirmwareVolume2ProtocolGuid,\r
+        NULL,\r
+        &FvHandleCount,\r
+        &FvHandleBuffer\r
+        );\r
+  for (Index = 0; Index < FvHandleCount; Index++) {\r
+    gBS->HandleProtocol (\r
+          FvHandleBuffer[Index],\r
+          &gEfiFirmwareVolume2ProtocolGuid,\r
+          (VOID **) &Fv\r
+          );\r
+\r
+    Status = Fv->ReadFile (\r
+                  Fv,\r
+                  PcdGetPtr(PcdShellFile),\r
+                  NULL,\r
+                  &Size,\r
+                  &Type,\r
+                  &Attributes,\r
+                  &AuthenticationStatus\r
+                  );\r
+    if (EFI_ERROR (Status)) {\r
+      //\r
+      // Skip if no shell file in the FV\r
+      //\r
+      continue;\r
+    }\r
+    //\r
+    // Build the shell boot option\r
+    //\r
+    BdsLibBuildOptionFromShell (FvHandleBuffer[Index], BdsBootOptionList);\r
+  }\r
+\r
+  if (FvHandleCount != 0) {\r
+    FreePool (FvHandleBuffer);\r
+  } */\r
+  \r
+  //\r
+  // Make sure every boot only have one time\r
+  // boot device enumerate\r
+  //\r
+  Status = BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");\r
+  mEnumBootDevice = TRUE;\r
+\r
+  return Status;\r
+} \r
+\r
+\r
+\r
+/**\r
+\r
+  The function will execute with as the platform policy, current policy\r
   is driven by boot mode. IBV/OEM can customize this code for their specific\r
   policy action.\r
 \r
@@ -1001,7 +1588,7 @@ EFIAPI
 PlatformBdsPolicyBehavior (\r
   IN OUT LIST_ENTRY                  *DriverOptionList,\r
   IN OUT LIST_ENTRY                  *BootOptionList,\r
-  IN PROCESS_CAPSULES                ProcessCapsules,\r
+  IN PROCESS_CAPSULES                BdsProcessCapsules,\r
   IN BASEM_MEMORY_TEST               BaseMemoryTest\r
   )\r
 {\r
@@ -1010,11 +1597,8 @@ PlatformBdsPolicyBehavior (
   EFI_BOOT_MODE                      BootMode;\r
   BOOLEAN                            DeferredImageExist;\r
   UINTN                              Index;\r
-  CHAR16                             CapsuleVarName[36];\r
-  CHAR16                             *TempVarName;\r
   SYSTEM_CONFIGURATION               SystemConfiguration;\r
   UINTN                              VarSize;\r
-  BOOLEAN                            SetVariableFlag;\r
   PLATFORM_PCI_DEVICE_PATH           *EmmcBootDevPath;\r
   EFI_GLOBAL_NVS_AREA_PROTOCOL       *GlobalNvsArea;\r
   EFI_HANDLE                         FvProtocolHandle;\r
@@ -1028,8 +1612,14 @@ PlatformBdsPolicyBehavior (
   BOOLEAN                            IsFirstBoot;\r
   UINT16                             *BootOrder;\r
   UINTN                              BootOrderSize;\r
+  ESRT_MANAGEMENT_PROTOCOL           *EsrtManagement;\r
 \r
   Timeout = PcdGet16 (PcdPlatformBootTimeOut);\r
+  if (Timeout > 10 ) {\r
+    //we think the Timeout variable is corrupted\r
+    Timeout = 10;\r
+  }\r
+\r
   VarSize = sizeof(SYSTEM_CONFIGURATION);\r
   Status = gRT->GetVariable(\r
                   NORMAL_SETUP_NAME,\r
@@ -1038,8 +1628,18 @@ PlatformBdsPolicyBehavior (
                   &VarSize,\r
                   &SystemConfiguration\r
                   );\r
-  if (EFI_ERROR (Status)) {\r
-    return;\r
+\r
+  if (EFI_ERROR (Status) || VarSize != sizeof(SYSTEM_CONFIGURATION)) {\r
+    //The setup variable is corrupted\r
+    VarSize = sizeof(SYSTEM_CONFIGURATION);\r
+    Status = gRT->GetVariable(\r
+              L"SetupRecovery",\r
+              &gEfiNormalSetupGuid,\r
+              NULL,\r
+              &VarSize,\r
+              &SystemConfiguration\r
+              );\r
+    ASSERT_EFI_ERROR (Status);\r
   }\r
 \r
   //\r
@@ -1052,37 +1652,6 @@ PlatformBdsPolicyBehavior (
   //\r
   BootMode = GetBootModeHob();\r
 \r
-  //\r
-  // Clear all the capsule variables CapsuleUpdateData, CapsuleUpdateData1, CapsuleUpdateData2...\r
-  // as early as possible which will avoid the next time boot after the capsule update\r
-  // will still into the capsule loop\r
-  //\r
-  StrCpy (CapsuleVarName, EFI_CAPSULE_VARIABLE_NAME);\r
-  TempVarName = CapsuleVarName + StrLen (CapsuleVarName);\r
-  Index = 0;\r
-  SetVariableFlag = TRUE;\r
-  while (SetVariableFlag) {\r
-    if (Index > 0) {\r
-      UnicodeValueToString (TempVarName, 0, Index, 0);\r
-    }\r
-    Status = gRT->SetVariable (\r
-                    CapsuleVarName,\r
-                    &gEfiCapsuleVendorGuid,\r
-                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS |\r
-                    EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
-                    0,\r
-                    (VOID *)NULL\r
-                    );\r
-    if (EFI_ERROR (Status)) {\r
-      //\r
-      // There is no capsule variables, quit\r
-      //\r
-      SetVariableFlag = FALSE;\r
-      continue;\r
-    }\r
-    Index++;\r
-  }\r
-\r
   //\r
   // No deferred images exist by default\r
   //\r
@@ -1134,6 +1703,11 @@ PlatformBdsPolicyBehavior (
     }\r
   }\r
 \r
+  Status = gBS->LocateProtocol(&gEsrtManagementProtocolGuid, NULL, (VOID **)&EsrtManagement);\r
+  if (EFI_ERROR(Status)) {\r
+    EsrtManagement = NULL;\r
+  }\r
+\r
   switch (BootMode) {\r
 \r
   case BOOT_WITH_MINIMAL_CONFIGURATION:\r
@@ -1217,9 +1791,16 @@ PlatformBdsPolicyBehavior (
     }\r
 \r
 \r
-#ifdef TPM_ENABLED\r
-       TcgPhysicalPresenceLibProcessRequest();\r
-#endif\r
+    #ifdef TPM_ENABLED\r
+    TcgPhysicalPresenceLibProcessRequest();\r
+    #endif\r
+    #ifdef FTPM_ENABLE\r
+    Tcg2PhysicalPresenceLibProcessRequest(NULL);\r
+    #endif\r
+\r
+    if (EsrtManagement != NULL) {\r
+      EsrtManagement->LockEsrtRepository();\r
+    }\r
 \r
     //\r
     // Close boot script and install ready to lock\r
@@ -1227,7 +1808,7 @@ PlatformBdsPolicyBehavior (
     InstallReadyToLock ();\r
 \r
     //\r
-    // Give one chance to enter the setup if we \r
+    // Give one chance to enter the setup if we\r
     // select Gummiboot "Reboot Into Firmware Interface" and Fast Boot is enabled.\r
     //\r
     BootIntoFirmwareInterface();\r
@@ -1262,6 +1843,10 @@ PlatformBdsPolicyBehavior (
       }\r
     }\r
 \r
+    if (EsrtManagement != NULL) {\r
+      EsrtManagement->LockEsrtRepository();\r
+    }\r
+\r
     //\r
     // Close boot script and install ready to lock\r
     //\r
@@ -1286,6 +1871,16 @@ PlatformBdsPolicyBehavior (
     //\r
     PlatformBdsConnectConsole (gPlatformConsole);\r
     PlatformBdsDiagnostics (EXTENSIVE, FALSE, BaseMemoryTest);\r
+\r
+    DEBUG((DEBUG_INFO, "ProcessCapsules Before EndOfDxe......\n"));\r
+    ProcessCapsules ();\r
+    DEBUG((DEBUG_INFO, "ProcessCapsules Done\n"));\r
+\r
+    //\r
+    // Close boot script and install ready to lock\r
+    //\r
+    InstallReadyToLock ();\r
+\r
     BdsLibConnectAll ();\r
 \r
     //\r
@@ -1302,12 +1897,13 @@ PlatformBdsPolicyBehavior (
       }\r
     }\r
 \r
-    //\r
-    // Close boot script and install ready to lock\r
-    //\r
-    InstallReadyToLock ();\r
+    if (EsrtManagement != NULL) {\r
+      EsrtManagement->SyncEsrtFmp();\r
+    }\r
 \r
-    ProcessCapsules (BOOT_ON_FLASH_UPDATE);\r
+    DEBUG((DEBUG_INFO, "ProcessCapsules After ConnectAll......\n"));\r
+    ProcessCapsules();\r
+    DEBUG((DEBUG_INFO, "ProcessCapsules Done\n"));\r
     break;\r
 \r
   case BOOT_IN_RECOVERY_MODE:\r
@@ -1405,15 +2001,26 @@ FULL_CONFIGURATION:
         PlatformBdsConnectSequence ();\r
       }\r
     }\r
-#ifdef TPM_ENABLED\r
+   #ifdef TPM_ENABLED\r
    TcgPhysicalPresenceLibProcessRequest();\r
-#endif\r
+   #endif\r
+   #ifdef FTPM_ENABLE\r
+   Tcg2PhysicalPresenceLibProcessRequest(NULL);\r
+   #endif\r
 \r
+    if (EsrtManagement != NULL) {\r
+      EsrtManagement->SyncEsrtFmp();\r
+    }\r
     //\r
     // Close boot script and install ready to lock\r
     //\r
     InstallReadyToLock ();\r
 \r
+    //\r
+    // Here we have enough time to do the enumeration of boot device\r
+    //\r
+    PlatformBdsLibEnumerateAllBootOption (BootOptionList);\r
+\r
     //\r
     // Give one chance to enter the setup if we\r
     // have the time out\r
@@ -1421,7 +2028,7 @@ FULL_CONFIGURATION:
     PlatformBdsEnterFrontPageWithHotKey (Timeout, FALSE);\r
 \r
        //\r
-       // Give one chance to enter the setup if we \r
+       // Give one chance to enter the setup if we\r
        // select Gummiboot "Reboot Into Firmware Interface"\r
        //\r
        BootIntoFirmwareInterface();\r
@@ -1439,10 +2046,7 @@ FULL_CONFIGURATION:
       return;\r
     }\r
 \r
-    //\r
-    // Here we have enough time to do the enumeration of boot device\r
-    //\r
-    BdsLibEnumerateAllBootOption (BootOptionList);\r
+\r
     break;\r
   }\r
 \r
@@ -1558,7 +2162,6 @@ BdsLockFv (
   EFI_FV_BLOCK_MAP_ENTRY      *BlockMap;\r
   EFI_FIRMWARE_VOLUME_HEADER  *FvHeader;\r
   EFI_PHYSICAL_ADDRESS        BaseAddress;\r
-  UINT8                       Data;\r
   UINT32                      BlockLength;\r
   UINTN                       Index;\r
 \r
@@ -1569,7 +2172,7 @@ BdsLockFv (
   while ((BlockMap->NumBlocks != 0) && (BlockMap->Length != 0)) {\r
     BlockLength = BlockMap->Length;\r
     for (Index = 0; Index < BlockMap->NumBlocks; Index++) {\r
-      Data = MmioOr8 ((UINTN) BaseAddress, 0x03);\r
+      MmioOr8 ((UINTN) BaseAddress, 0x03);\r
       BaseAddress += BlockLength;\r
     }\r
     BlockMap++;\r
@@ -1808,11 +2411,19 @@ ShowProgressHotKey (
   EFI_GRAPHICS_OUTPUT_BLT_PIXEL Background;\r
   EFI_GRAPHICS_OUTPUT_BLT_PIXEL Color;\r
   UINT32                        GpioValue;\r
+  CHAR16                        *TmpStr1;\r
+  CHAR16                        *TmpStr2;\r
+  CHAR16                        *TmpStr3;\r
+  UINTN                         TmpStrSize;\r
+  VOID                          *Buffer;\r
+  UINTN                         Size;\r
 \r
   if (TimeoutDefault == 0) {\r
     return EFI_TIMEOUT;\r
   }\r
 \r
+  gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR (EFI_LIGHTGRAY, EFI_BLACK));\r
+    \r
   if (DebugAssertEnabled())\r
   {\r
     DEBUG ((EFI_D_INFO, "\n\nStart showing progress bar... Press any key to stop it, or press <F2> or <DEL> to enter setup page! ...Zzz....\n"));\r
@@ -1829,10 +2440,76 @@ ShowProgressHotKey (
   SetMem (&Background, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL), 0x0);\r
   SetMem (&Color, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL), 0xff);\r
 \r
+  TmpStr2 = NULL;\r
+  TmpStr3 = NULL;\r
+\r
+  //\r
+  // Check if the platform is using test key.\r
+  //\r
+  Status = GetSectionFromAnyFv(\r
+             PcdGetPtr(PcdEdkiiRsa2048Sha256TestPublicKeyFileGuid),\r
+             EFI_SECTION_RAW,\r
+             0,\r
+             &Buffer,\r
+             &Size\r
+             );\r
+  if (!EFI_ERROR(Status)) {\r
+    if ((Size == PcdGetSize(PcdRsa2048Sha256PublicKeyBuffer)) &&\r
+        (CompareMem(Buffer, PcdGetPtr(PcdRsa2048Sha256PublicKeyBuffer), Size) == 0)) {\r
+      TmpStr2 = L"WARNING: Recovery Test Key is used.\r\n";\r
+      if (DebugAssertEnabled()) {\r
+        DEBUG ((DEBUG_INFO, "\n\nWARNING: Recovery Test Key is used.\n"));\r
+      } else {\r
+        SerialPortWrite((UINT8 *)"\n\nWARNING: Recovery Test Key is used.", sizeof("\n\nWARNING: Recovery Test Key is used."));\r
+      }\r
+      PcdSetBoolS(PcdTestKeyUsed, TRUE);\r
+    }\r
+    FreePool(Buffer);\r
+  }\r
+  Status = GetSectionFromAnyFv(\r
+             PcdGetPtr(PcdEdkiiPkcs7TestPublicKeyFileGuid),\r
+             EFI_SECTION_RAW,\r
+             0,\r
+             &Buffer,\r
+             &Size\r
+             );\r
+  if (!EFI_ERROR(Status)) {\r
+    if ((Size == PcdGetSize(PcdPkcs7CertBuffer)) &&\r
+        (CompareMem(Buffer, PcdGetPtr(PcdPkcs7CertBuffer), Size) == 0)) {\r
+      TmpStr3 = L"WARNING: Capsule Test Key is used.\r\n";\r
+      if (DebugAssertEnabled()) {\r
+        DEBUG ((DEBUG_INFO, "\n\nWARNING: Capsule Test Key is used.\r\n"));\r
+      } else {\r
+        SerialPortWrite((UINT8 *)"\n\nWARNING: Capsule Test Key is used.", sizeof("\n\nWARNING: Capsule Test Key is used."));\r
+      }\r
+      PcdSetBoolS(PcdTestKeyUsed, TRUE);\r
+    }\r
+    FreePool(Buffer);\r
+  }\r
+\r
   //\r
   // Clear the progress status bar first\r
   //\r
-  TmpStr = L"Start boot option, Press <F2> or <DEL> to enter setup page.";\r
+  TmpStr1 = L"Start boot option, Press <F2> or <DEL> to enter setup page.\r\n";\r
+  TmpStrSize = StrSize(TmpStr1);\r
+  if (TmpStr2 != NULL) {\r
+    TmpStrSize += StrSize(TmpStr2);\r
+  }\r
+  if (TmpStr3 != NULL) {\r
+    TmpStrSize += StrSize(TmpStr3);\r
+  }\r
+  TmpStr = AllocatePool (TmpStrSize);\r
+  if (TmpStr == NULL) {\r
+    TmpStr = TmpStr1;\r
+  } else {\r
+    StrCpyS(TmpStr, TmpStrSize/sizeof(CHAR16), TmpStr1);\r
+    if (TmpStr2 != NULL) {\r
+      StrCatS(TmpStr, TmpStrSize/sizeof(CHAR16), TmpStr2);\r
+    }\r
+    if (TmpStr3 != NULL) {\r
+      StrCatS(TmpStr, TmpStrSize/sizeof(CHAR16), TmpStr3);\r
+    }\r
+  }\r
   PlatformBdsShowProgress (Foreground, Background, TmpStr, Color, 0, 0);\r
 \r
   TimeoutRemain = TimeoutDefault;\r
@@ -2233,7 +2910,7 @@ PlatformBdsConnectSimpleConsole (
   if (VarConout == NULL || VarConin == NULL) {\r
     //\r
     // Have chance to connect the platform default console,\r
-    // the platform default console is the minimue device group\r
+    // the platform default console is the minimum device group\r
     // the platform should support\r
     //\r
     while (PlatformConsole[Index].DevicePath != NULL) {\r