]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/BdsDxe/BdsEntry.c
MdeModulePkg/BdsDxe: Avoid overwriting PlatformRecovery####
[mirror_edk2.git] / MdeModulePkg / Universal / BdsDxe / BdsEntry.c
index f06684c96ae4d1ba00edc2d5e75ce6ad26e06395..98b393109389d74a1fe824562afe845acc6535a0 100644 (file)
@@ -5,8 +5,9 @@
   After DxeCore finish DXE phase, gEfiBdsArchProtocolGuid->BdsEntry will be invoked\r
   to enter BDS phase.\r
 \r
+Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>\r
+(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>\r
-Copyright (c) 2004 - 2015, 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
@@ -85,6 +86,81 @@ BdsDxeOnConnectConInCallBack (
     DEBUG ((EFI_D_WARN, "[Bds] Connect ConIn failed - %r!!!\n", Status));\r
   }\r
 }\r
+/**\r
+  Notify function for event group EFI_EVENT_GROUP_READY_TO_BOOT. This is used to\r
+  check whether there is remaining deferred load images.\r
+\r
+  @param[in]  Event   The Event that is being processed.\r
+  @param[in]  Context The Event Context.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+CheckDeferredLoadImageOnReadyToBoot (\r
+  IN EFI_EVENT        Event,\r
+  IN VOID             *Context\r
+  )\r
+{\r
+  EFI_STATUS                         Status;\r
+  EFI_DEFERRED_IMAGE_LOAD_PROTOCOL   *DeferredImage;\r
+  UINTN                              HandleCount;\r
+  EFI_HANDLE                         *Handles;\r
+  UINTN                              Index;\r
+  UINTN                              ImageIndex;\r
+  EFI_DEVICE_PATH_PROTOCOL           *ImageDevicePath;\r
+  VOID                               *Image;\r
+  UINTN                              ImageSize;\r
+  BOOLEAN                            BootOption;\r
+  CHAR16                             *DevicePathStr;\r
+\r
+  //\r
+  // Find all the deferred image load protocols.\r
+  //\r
+  HandleCount = 0;\r
+  Handles = NULL;\r
+  Status = gBS->LocateHandleBuffer (\r
+    ByProtocol,\r
+    &gEfiDeferredImageLoadProtocolGuid,\r
+    NULL,\r
+    &HandleCount,\r
+    &Handles\r
+  );\r
+  if (EFI_ERROR (Status)) {\r
+    return;\r
+  }\r
+\r
+  for (Index = 0; Index < HandleCount; Index++) {\r
+    Status = gBS->HandleProtocol (Handles[Index], &gEfiDeferredImageLoadProtocolGuid, (VOID **) &DeferredImage);\r
+    if (EFI_ERROR (Status)) {\r
+      continue;\r
+    }\r
+\r
+    for (ImageIndex = 0; ; ImageIndex++) {\r
+      //\r
+      // Load all the deferred images in this protocol instance.\r
+      //\r
+      Status = DeferredImage->GetImageInfo (\r
+        DeferredImage,\r
+        ImageIndex,\r
+        &ImageDevicePath,\r
+        (VOID **) &Image,\r
+        &ImageSize,\r
+        &BootOption\r
+      );\r
+      if (EFI_ERROR (Status)) {\r
+        break;\r
+      }\r
+      DevicePathStr = ConvertDevicePathToText (ImageDevicePath, FALSE, FALSE);\r
+      DEBUG ((DEBUG_LOAD, "[Bds] Image was deferred but not loaded: %s.\n", DevicePathStr));\r
+      if (DevicePathStr != NULL) {\r
+        FreePool (DevicePathStr);\r
+      }\r
+    }\r
+  }\r
+  if (Handles != NULL) {\r
+    FreePool (Handles);\r
+  }\r
+}\r
 \r
 /**\r
 \r
@@ -118,6 +194,21 @@ BdsInitialize (
                   );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
+  DEBUG_CODE (\r
+    EFI_EVENT   Event;\r
+    //\r
+    // Register notify function to check deferred images on ReadyToBoot Event.\r
+    //\r
+    Status = gBS->CreateEventEx (\r
+                    EVT_NOTIFY_SIGNAL,\r
+                    TPL_CALLBACK,\r
+                    CheckDeferredLoadImageOnReadyToBoot,\r
+                    NULL,\r
+                    &gEfiEventReadyToBootGuid,\r
+                    &Event\r
+                    );\r
+    ASSERT_EFI_ERROR (Status);\r
+  );\r
   return Status;\r
 }\r
 \r
@@ -273,7 +364,7 @@ BOOLEAN
 BootBootOptions (\r
   IN EFI_BOOT_MANAGER_LOAD_OPTION    *BootOptions,\r
   IN UINTN                           BootOptionCount,\r
-  IN EFI_BOOT_MANAGER_LOAD_OPTION    *BootManagerMenu\r
+  IN EFI_BOOT_MANAGER_LOAD_OPTION    *BootManagerMenu OPTIONAL\r
   )\r
 {\r
   UINTN                              Index;\r
@@ -307,9 +398,12 @@ BootBootOptions (
     EfiBootManagerBoot (&BootOptions[Index]);\r
 \r
     //\r
-    // If the boot via Boot#### returns with a status of EFI_SUCCESS, platform firmware\r    // supports boot manager menu, and if firmware is configured to boot in an\r    // interactive mode, the boot manager will stop processing the BootOrder variable and\r    // present a boot manager menu to the user.\r
+    // If the boot via Boot#### returns with a status of EFI_SUCCESS, platform firmware\r
+    // supports boot manager menu, and if firmware is configured to boot in an\r
+    // interactive mode, the boot manager will stop processing the BootOrder variable and\r
+    // present a boot manager menu to the user.\r
     //\r
-    if (BootOptions[Index].Status == EFI_SUCCESS) {\r
+    if ((BootManagerMenu != NULL) && (BootOptions[Index].Status == EFI_SUCCESS)) {\r
       EfiBootManagerBoot (BootManagerMenu);\r
       break;\r
     }\r
@@ -349,17 +443,28 @@ ProcessLoadOptions (
       LoadOptionType = LoadOptions[Index].OptionType;\r
     }\r
     ASSERT (LoadOptionType == LoadOptions[Index].OptionType);\r
+    ASSERT (LoadOptionType != LoadOptionTypeBoot);\r
 \r
     Status = EfiBootManagerProcessLoadOption (&LoadOptions[Index]);\r
 \r
+    //\r
+    // Status indicates whether the load option is loaded and executed\r
+    // LoadOptions[Index].Status is what the load option returns\r
+    //\r
     if (!EFI_ERROR (Status)) {\r
-      if (LoadOptionType == LoadOptionTypePlatformRecovery) {\r
-        //\r
-        // Stop processing if any entry is successful\r
-        //\r
+      //\r
+      // Stop processing if any PlatformRecovery#### returns success.\r
+      //\r
+      if ((LoadOptions[Index].Status == EFI_SUCCESS) &&\r
+          (LoadOptionType == LoadOptionTypePlatformRecovery)) {\r
         break;\r
       }\r
-      if ((LoadOptions[Index].Attributes & LOAD_OPTION_FORCE_RECONNECT) != 0) {\r
+\r
+      //\r
+      // Only set ReconnectAll flag when the load option executes successfully.\r
+      //\r
+      if (!EFI_ERROR (LoadOptions[Index].Status) &&\r
+          (LoadOptions[Index].Attributes & LOAD_OPTION_FORCE_RECONNECT) != 0) {\r
         ReconnectAll = TRUE;\r
       }\r
     }\r
@@ -422,22 +527,32 @@ BdsFormalizeConsoleVariable (
   \r
   Item 3 is used to solve case when OS corrupts OsIndications. Here simply delete this NV variable.\r
 \r
+  Create a boot option for BootManagerMenu if it hasn't been created yet\r
+\r
 **/\r
 VOID \r
 BdsFormalizeOSIndicationVariable (\r
   VOID\r
   )\r
 {\r
-  EFI_STATUS Status;\r
-  UINT64     OsIndicationSupport;\r
-  UINT64     OsIndication;\r
-  UINTN      DataSize;\r
-  UINT32     Attributes;\r
+  EFI_STATUS                      Status;\r
+  UINT64                          OsIndicationSupport;\r
+  UINT64                          OsIndication;\r
+  UINTN                           DataSize;\r
+  UINT32                          Attributes;\r
+  EFI_BOOT_MANAGER_LOAD_OPTION    BootManagerMenu;\r
 \r
   //\r
   // OS indicater support variable\r
   //\r
-  OsIndicationSupport = EFI_OS_INDICATIONS_BOOT_TO_FW_UI | EFI_OS_INDICATIONS_START_PLATFORM_RECOVERY;\r
+  Status = EfiBootManagerGetBootManagerMenu (&BootManagerMenu);\r
+  if (Status != EFI_NOT_FOUND) {\r
+    OsIndicationSupport = EFI_OS_INDICATIONS_BOOT_TO_FW_UI | EFI_OS_INDICATIONS_START_PLATFORM_RECOVERY;\r
+    EfiBootManagerFreeLoadOption (&BootManagerMenu);\r
+  } else {\r
+    OsIndicationSupport = EFI_OS_INDICATIONS_START_PLATFORM_RECOVERY;\r
+  }\r
+\r
   Status = gRT->SetVariable (\r
                   EFI_OS_INDICATIONS_SUPPORT_VARIABLE_NAME,\r
                   &gEfiGlobalVariableGuid,\r
@@ -598,6 +713,7 @@ BdsEntry (
   BOOLEAN                         PlatformRecovery;\r
   BOOLEAN                         BootSuccess;\r
   EFI_DEVICE_PATH_PROTOCOL        *FilePath;\r
+  EFI_STATUS                      BootManagerMenuStatus;\r
 \r
   HotkeyTriggered = NULL;\r
   Status          = EFI_SUCCESS;\r
@@ -639,7 +755,7 @@ BdsEntry (
   Status = gBS->LocateProtocol (&gEdkiiVariableLockProtocolGuid, NULL, (VOID **) &VariableLock);\r
   DEBUG ((EFI_D_INFO, "[BdsDxe] Locate Variable Lock protocol - %r\n", Status));\r
   if (!EFI_ERROR (Status)) {\r
-    for (Index = 0; Index < sizeof (mReadOnlyVariables) / sizeof (mReadOnlyVariables[0]); Index++) {\r
+    for (Index = 0; Index < ARRAY_SIZE (mReadOnlyVariables); Index++) {\r
       Status = VariableLock->RequestToLock (VariableLock, mReadOnlyVariables[Index], &gEfiGlobalVariableGuid);\r
       ASSERT_EFI_ERROR (Status);\r
     }\r
@@ -721,7 +837,7 @@ BdsEntry (
   FilePath = FileDevicePath (NULL, EFI_REMOVABLE_MEDIA_FILE_NAME);\r
   Status = EfiBootManagerInitializeLoadOption (\r
              &LoadOption,\r
-             0,\r
+             LoadOptionNumberUnassigned,\r
              LoadOptionTypePlatformRecovery,\r
              LOAD_OPTION_ACTIVE,\r
              L"Default PlatformRecovery",\r
@@ -730,9 +846,24 @@ BdsEntry (
              0\r
              );\r
   ASSERT_EFI_ERROR (Status);\r
-  EfiBootManagerLoadOptionToVariable (&LoadOption);\r
+  LoadOptions = EfiBootManagerGetLoadOptions (&LoadOptionCount, LoadOptionTypePlatformRecovery);\r
+  if (EfiBootManagerFindLoadOption (&LoadOption, LoadOptions, LoadOptionCount) == -1) {\r
+    for (Index = 0; Index < LoadOptionCount; Index++) {\r
+      //\r
+      // The PlatformRecovery#### options are sorted by OptionNumber.\r
+      // Find the the smallest unused number as the new OptionNumber.\r
+      //\r
+      if (LoadOptions[Index].OptionNumber != Index) {\r
+        break;\r
+      }\r
+    }\r
+    LoadOption.OptionNumber = Index;\r
+    Status = EfiBootManagerLoadOptionToVariable (&LoadOption);\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
   EfiBootManagerFreeLoadOption (&LoadOption);\r
   FreePool (FilePath);\r
+  EfiBootManagerFreeLoadOptions (LoadOptions, LoadOptionCount);\r
 \r
   //\r
   // Report Status Code to indicate connecting drivers will happen\r
@@ -848,9 +979,9 @@ BdsEntry (
   );\r
 \r
   //\r
-  // BootManagerMenu always contains the correct information even call fails.\r
+  // BootManagerMenu doesn't contain the correct information when return status is EFI_NOT_FOUND.\r
   //\r
-  EfiBootManagerGetBootManagerMenu (&BootManagerMenu);\r
+  BootManagerMenuStatus = EfiBootManagerGetBootManagerMenu (&BootManagerMenu);\r
 \r
   BootFwUi         = (BOOLEAN) ((OsIndication & EFI_OS_INDICATIONS_BOOT_TO_FW_UI) != 0);\r
   PlatformRecovery = (BOOLEAN) ((OsIndication & EFI_OS_INDICATIONS_START_PLATFORM_RECOVERY) != 0);\r
@@ -875,7 +1006,7 @@ BdsEntry (
   //\r
   // Launch Boot Manager Menu directly when EFI_OS_INDICATIONS_BOOT_TO_FW_UI is set. Skip HotkeyBoot\r
   //\r
-  if (BootFwUi) {\r
+  if (BootFwUi && (BootManagerMenuStatus != EFI_NOT_FOUND)) {\r
     //\r
     // Follow generic rule, Call BdsDxeOnConnectConInCallBack to connect ConIn before enter UI\r
     //\r
@@ -920,9 +1051,12 @@ BdsEntry (
       if (!EFI_ERROR (Status)) {\r
         EfiBootManagerBoot (&LoadOption);\r
         EfiBootManagerFreeLoadOption (&LoadOption);\r
-        if (LoadOption.Status == EFI_SUCCESS) {\r
+        if ((LoadOption.Status == EFI_SUCCESS) && \r
+            (BootManagerMenuStatus != EFI_NOT_FOUND) &&\r
+            (LoadOption.OptionNumber != BootManagerMenu.OptionNumber)) {\r
           //\r
           // Boot to Boot Manager Menu upon EFI_SUCCESS\r
+          // Exception: Do not boot again when the BootNext points to Boot Manager Menu.\r
           //\r
           EfiBootManagerBoot (&BootManagerMenu);\r
         }\r
@@ -934,12 +1068,14 @@ BdsEntry (
       // Retry to boot if any of the boot succeeds\r
       //\r
       LoadOptions = EfiBootManagerGetLoadOptions (&LoadOptionCount, LoadOptionTypeBoot);\r
-      BootSuccess = BootBootOptions (LoadOptions, LoadOptionCount, &BootManagerMenu);\r
+      BootSuccess = BootBootOptions (LoadOptions, LoadOptionCount, (BootManagerMenuStatus != EFI_NOT_FOUND) ? &BootManagerMenu : NULL);\r
       EfiBootManagerFreeLoadOptions (LoadOptions, LoadOptionCount);\r
     } while (BootSuccess);\r
   }\r
 \r
-  EfiBootManagerFreeLoadOption (&BootManagerMenu);\r
+  if (BootManagerMenuStatus != EFI_NOT_FOUND) {\r
+    EfiBootManagerFreeLoadOption (&BootManagerMenu);\r
+  }\r
 \r
   if (!BootSuccess) {\r
     LoadOptions = EfiBootManagerGetLoadOptions (&LoadOptionCount, LoadOptionTypePlatformRecovery);\r