]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c
MdeModulePkg/DxeCapsuleLibFmp: Use new Variable Lock interface
[mirror_edk2.git] / MdeModulePkg / Library / UefiBootManagerLib / BmMisc.c
index c6ac242094692d7690c18c392ea6933ab02b55ef..89595747af3d87f4ed8dfdb7a94bb71010d58154 100644 (file)
@@ -1,14 +1,9 @@
 /** @file\r
   Misc library functions.\r
 \r
-Copyright (c) 2011 - 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
-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
+Copyright (c) 2011 - 2019, Intel Corporation. All rights reserved.<BR>\r
+(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
@@ -121,7 +116,7 @@ BmMatchDevicePaths (
 }\r
 \r
 /**\r
-  This routine adjust the memory information for different memory type and \r
+  This routine adjust the memory information for different memory type and\r
   save them into the variables for next boot. It resets the system when\r
   memory information is updated and the current boot option belongs to\r
   boot category instead of application category. It doesn't count the\r
@@ -129,13 +124,10 @@ BmMatchDevicePaths (
 \r
   @param Boot               TRUE if current boot option belongs to boot\r
                             category instead of application category.\r
-  @param RamDiskSizeInPages Reserved memory size in pages occupied by\r
-                            RAM Disk.\r
 **/\r
 VOID\r
 BmSetMemoryTypeInformationVariable (\r
-  IN BOOLEAN                    Boot,\r
-  IN UINTN                      RamDiskSizeInPages\r
+  IN BOOLEAN                    Boot\r
   )\r
 {\r
   EFI_STATUS                   Status;\r
@@ -165,7 +157,7 @@ BmSetMemoryTypeInformationVariable (
   }\r
 \r
   //\r
-  // Only check the the Memory Type Information variable in the boot mode \r
+  // Only check the the Memory Type Information variable in the boot mode\r
   // other than BOOT_WITH_DEFAULT_SETTINGS because the Memory Type\r
   // Information is not valid in this boot mode.\r
   //\r
@@ -174,8 +166,8 @@ BmSetMemoryTypeInformationVariable (
     Status = gRT->GetVariable (\r
                     EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r
                     &gEfiMemoryTypeInformationGuid,\r
-                    NULL, \r
-                    &VariableSize, \r
+                    NULL,\r
+                    &VariableSize,\r
                     NULL\r
                     );\r
     if (Status == EFI_BUFFER_TOO_SMALL) {\r
@@ -207,8 +199,11 @@ BmSetMemoryTypeInformationVariable (
     //\r
     return;\r
   }\r
-  PreviousMemoryTypeInformation = GET_GUID_HOB_DATA (GuidHob);\r
-  VariableSize = GET_GUID_HOB_DATA_SIZE (GuidHob);\r
+  VariableSize                  = GET_GUID_HOB_DATA_SIZE (GuidHob);\r
+  PreviousMemoryTypeInformation = AllocateCopyPool (VariableSize, GET_GUID_HOB_DATA (GuidHob));\r
+  if (PreviousMemoryTypeInformation == NULL) {\r
+    return;\r
+  }\r
 \r
   //\r
   // Use a heuristic to adjust the Memory Type Information for the next boot\r
@@ -228,13 +223,6 @@ BmSetMemoryTypeInformationVariable (
       continue;\r
     }\r
 \r
-    //\r
-    // Do not count the reserved memory occupied by RAM Disk.\r
-    //\r
-    if (CurrentMemoryTypeInformation[Index1].Type == EfiReservedMemoryType) {\r
-      CurrentMemoryTypeInformation[Index1].NumberOfPages -= RamDiskSizeInPages;\r
-    }\r
-\r
     //\r
     // Previous is the number of pages pre-allocated\r
     // Current is the number of pages actually needed\r
@@ -287,14 +275,18 @@ BmSetMemoryTypeInformationVariable (
       // then reset the platform so the new Memory Type Information setting will be used to guarantee that an S4\r
       // entry/resume cycle will not fail.\r
       //\r
-      if (MemoryTypeInformationModified && Boot && PcdGetBool (PcdResetOnMemoryTypeInformationChange)) {\r
-        DEBUG ((EFI_D_INFO, "Memory Type Information settings change. Warm Reset!!!\n"));\r
-        gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);\r
+      if (MemoryTypeInformationModified) {\r
+        DEBUG ((EFI_D_INFO, "Memory Type Information settings change.\n"));\r
+        if (Boot && PcdGetBool (PcdResetOnMemoryTypeInformationChange)) {\r
+          DEBUG ((EFI_D_INFO, "...Warm Reset!!!\n"));\r
+          gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);\r
+        }\r
       }\r
     } else {\r
       DEBUG ((EFI_D_ERROR, "Memory Type Information settings cannot be saved. OS S4 may fail!\n"));\r
     }\r
   }\r
+  FreePool (PreviousMemoryTypeInformation);\r
 }\r
 \r
 /**\r
@@ -306,15 +298,14 @@ BmSetMemoryTypeInformationVariable (
                                  then EFI_INVALID_PARAMETER is returned.\r
   @param  VendorGuid             A unique identifier for the vendor.\r
   @param  Attributes             Attributes bitmask to set for the variable.\r
-  @param  DataSize               The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE, \r
-                                 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, or \r
-                                 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero \r
-                                 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is \r
-                                 set, then a SetVariable() call with a DataSize of zero will not cause any change to \r
-                                 the variable value (the timestamp associated with the variable may be updated however \r
-                                 even if no new data value is provided,see the description of the \r
-                                 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not \r
-                                 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated). \r
+  @param  DataSize               The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE,\r
+                                 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero\r
+                                 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is\r
+                                 set, then a SetVariable() call with a DataSize of zero will not cause any change to\r
+                                 the variable value (the timestamp associated with the variable may be updated however\r
+                                 even if no new data value is provided,see the description of the\r
+                                 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not\r
+                                 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated).\r
   @param  Data                   The contents for the variable.\r
 \r
   @retval EFI_SUCCESS            The firmware has successfully stored the variable and its data as\r
@@ -326,9 +317,8 @@ BmSetMemoryTypeInformationVariable (
   @retval EFI_DEVICE_ERROR       The variable could not be retrieved due to a hardware error.\r
   @retval EFI_WRITE_PROTECTED    The variable in question is read-only.\r
   @retval EFI_WRITE_PROTECTED    The variable in question cannot be deleted.\r
-  @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS \r
-                                 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS being set, but the AuthInfo \r
-                                 does NOT pass the validation check carried out by the firmware.\r
+  @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS\r
+                                 being set, but the AuthInfo does NOT pass the validation check carried out by the firmware.\r
 \r
   @retval EFI_NOT_FOUND          The variable trying to be updated or deleted was not found.\r
 **/\r
@@ -415,14 +405,131 @@ BmCharToUint (
   )\r
 {\r
   if ((Char >= L'0') && (Char <= L'9')) {\r
-    return (UINTN) (Char - L'0');\r
+    return (Char - L'0');\r
   }\r
 \r
   if ((Char >= L'A') && (Char <= L'F')) {\r
-    return (UINTN) (Char - L'A' + 0xA);\r
+    return (Char - L'A' + 0xA);\r
   }\r
 \r
-  ASSERT (FALSE);\r
   return (UINTN) -1;\r
 }\r
 \r
+/**\r
+  Dispatch the deferred images that are returned from all DeferredImageLoad instances.\r
+\r
+  @retval EFI_SUCCESS       At least one deferred image is loaded successfully and started.\r
+  @retval EFI_NOT_FOUND     There is no deferred image.\r
+  @retval EFI_ACCESS_DENIED There are deferred images but all of them are failed to load.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+EfiBootManagerDispatchDeferredImages (\r
+  VOID\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
+  EFI_HANDLE                         ImageHandle;\r
+  UINTN                              ImageCount;\r
+  UINTN                              LoadCount;\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 EFI_NOT_FOUND;\r
+  }\r
+\r
+  ImageCount = 0;\r
+  LoadCount  = 0;\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
+      ImageCount++;\r
+      //\r
+      // Load and start the image.\r
+      //\r
+      Status = gBS->LoadImage (\r
+        BootOption,\r
+        gImageHandle,\r
+        ImageDevicePath,\r
+        NULL,\r
+        0,\r
+        &ImageHandle\r
+      );\r
+      if (EFI_ERROR (Status)) {\r
+        //\r
+        // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created\r
+        // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.\r
+        // If the caller doesn't have the option to defer the execution of an image, we should\r
+        // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.\r
+        //\r
+        if (Status == EFI_SECURITY_VIOLATION) {\r
+          gBS->UnloadImage (ImageHandle);\r
+        }\r
+      } else {\r
+        LoadCount++;\r
+        //\r
+        // Before calling the image, enable the Watchdog Timer for\r
+        // a 5 Minute period\r
+        //\r
+        gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);\r
+        gBS->StartImage (ImageHandle, NULL, NULL);\r
+\r
+        //\r
+        // Clear the Watchdog Timer after the image returns.\r
+        //\r
+        gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);\r
+      }\r
+    }\r
+  }\r
+  if (Handles != NULL) {\r
+    FreePool (Handles);\r
+  }\r
+\r
+  if (ImageCount == 0) {\r
+    return EFI_NOT_FOUND;\r
+  } else {\r
+    if (LoadCount == 0) {\r
+      return EFI_ACCESS_DENIED;\r
+    } else {\r
+      return EFI_SUCCESS;\r
+    }\r
+  }\r
+}\r