]> git.proxmox.com Git - mirror_edk2.git/commitdiff
OvmfPkg/QemuNewBootOrderLib: Build with UefiBootManagerLib
authorRuiyu Ni <ruiyu.ni@intel.com>
Wed, 20 Apr 2016 07:27:48 +0000 (15:27 +0800)
committerRuiyu Ni <ruiyu.ni@intel.com>
Wed, 4 May 2016 00:47:35 +0000 (08:47 +0800)
NOTE: SetBootOrderFromQemu() interface is not changed.
But when the old IntelFrameworkModulePkg/BDS is no longer used in
OVMF and ArmVirtPkg, additional patch will be submitted to change
this interface to remove parameter BootOptionList.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Tested-by: Laszlo Ersek <lersek@redhat.com>
OvmfPkg/Library/QemuNewBootOrderLib/QemuBootOrderLib.c
OvmfPkg/Library/QemuNewBootOrderLib/QemuBootOrderLib.inf

index 15065b7c3d50699a9bbb8c54b38ee57ad8a6a948..6c576121c2d0971f0bfd3fe59fc39400d74f87bb 100644 (file)
@@ -2,7 +2,7 @@
   Rewrite the BootOrder NvVar based on QEMU's "bootorder" fw_cfg file.\r
 \r
   Copyright (C) 2012 - 2014, Red Hat, Inc.\r
-  Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.<BR>\r
 \r
   This program and the accompanying materials are licensed and made available\r
   under the terms and conditions of the BSD License which accompanies this\r
@@ -16,7 +16,7 @@
 #include <Library/QemuFwCfgLib.h>\r
 #include <Library/DebugLib.h>\r
 #include <Library/MemoryAllocationLib.h>\r
-#include <Library/GenericBdsLib.h>\r
+#include <Library/UefiBootManagerLib.h>\r
 #include <Library/UefiBootServicesTableLib.h>\r
 #include <Library/UefiRuntimeServicesTableLib.h>\r
 #include <Library/BaseLib.h>\r
@@ -253,8 +253,10 @@ typedef struct {
   LOAD_OPTION_ACTIVE attribute.\r
 **/\r
 typedef struct {\r
-  CONST BDS_COMMON_OPTION *BootOption; // reference only, no ownership\r
-  BOOLEAN                 Appended;    // has been added to a BOOT_ORDER?\r
+  CONST EFI_BOOT_MANAGER_LOAD_OPTION *BootOption; // reference only, no\r
+                                                  //   ownership\r
+  BOOLEAN                            Appended;    // has been added to a\r
+                                                  //   BOOT_ORDER?\r
 } ACTIVE_OPTION;\r
 \r
 \r
@@ -300,7 +302,7 @@ BootOrderAppend (
   }\r
 \r
   BootOrder->Data[BootOrder->Produced++] =\r
-                                         ActiveOption->BootOption->BootCurrent;\r
+                               (UINT16) ActiveOption->BootOption->OptionNumber;\r
   ActiveOption->Appended = TRUE;\r
   return RETURN_SUCCESS;\r
 }\r
@@ -308,22 +310,25 @@ BootOrderAppend (
 \r
 /**\r
 \r
-  Create an array of ACTIVE_OPTION elements for a boot option list.\r
+  Create an array of ACTIVE_OPTION elements for a boot option array.\r
 \r
-  @param[in]  BootOptionList  A boot option list, created with\r
-                              BdsLibEnumerateAllBootOption().\r
+  @param[in]  BootOptions      A boot option array, created with\r
+                               EfiBootManagerRefreshAllBootOption () and\r
+                               EfiBootManagerGetLoadOptions ().\r
 \r
-  @param[out] ActiveOption    Pointer to the first element in the new array.\r
-                              The caller is responsible for freeing the array\r
-                              with FreePool() after use.\r
+  @param[in]  BootOptionCount  The number of elements in BootOptions.\r
 \r
-  @param[out] Count           Number of elements in the new array.\r
+  @param[out] ActiveOption     Pointer to the first element in the new array.\r
+                               The caller is responsible for freeing the array\r
+                               with FreePool() after use.\r
+\r
+  @param[out] Count            Number of elements in the new array.\r
 \r
 \r
   @retval RETURN_SUCCESS           The ActiveOption array has been created.\r
 \r
   @retval RETURN_NOT_FOUND         No active entry has been found in\r
-                                   BootOptionList.\r
+                                   BootOptions.\r
 \r
   @retval RETURN_OUT_OF_RESOURCES  Memory allocation failed.\r
 \r
@@ -331,11 +336,13 @@ BootOrderAppend (
 STATIC\r
 RETURN_STATUS\r
 CollectActiveOptions (\r
-  IN   CONST LIST_ENTRY *BootOptionList,\r
-  OUT  ACTIVE_OPTION    **ActiveOption,\r
-  OUT  UINTN            *Count\r
+  IN   CONST EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions,\r
+  IN   UINTN                              BootOptionCount,\r
+  OUT  ACTIVE_OPTION                      **ActiveOption,\r
+  OUT  UINTN                              *Count\r
   )\r
 {\r
+  UINTN Index;\r
   UINTN ScanMode;\r
 \r
   *ActiveOption = NULL;\r
@@ -346,22 +353,15 @@ CollectActiveOptions (
   // - store links to active entries.\r
   //\r
   for (ScanMode = 0; ScanMode < 2; ++ScanMode) {\r
-    CONST LIST_ENTRY *Link;\r
-\r
-    Link = BootOptionList->ForwardLink;\r
     *Count = 0;\r
-    while (Link != BootOptionList) {\r
-      CONST BDS_COMMON_OPTION *Current;\r
-\r
-      Current = CR (Link, BDS_COMMON_OPTION, Link, BDS_LOAD_OPTION_SIGNATURE);\r
-      if (IS_LOAD_OPTION_TYPE (Current->Attribute, LOAD_OPTION_ACTIVE)) {\r
+    for (Index = 0; Index < BootOptionCount; Index++) {\r
+      if ((BootOptions[Index].Attributes & LOAD_OPTION_ACTIVE) != 0) {\r
         if (ScanMode == 1) {\r
-          (*ActiveOption)[*Count].BootOption = Current;\r
+          (*ActiveOption)[*Count].BootOption = &BootOptions[Index];\r
           (*ActiveOption)[*Count].Appended   = FALSE;\r
         }\r
         ++*Count;\r
       }\r
-      Link = Link->ForwardLink;\r
     }\r
 \r
     if (ScanMode == 0) {\r
@@ -1437,11 +1437,17 @@ BOOLEAN
 Match (\r
   IN  CONST CHAR16                           *Translated,\r
   IN  UINTN                                  TranslatedLength,\r
-  IN  CONST EFI_DEVICE_PATH_PROTOCOL         *DevicePath\r
+  IN  EFI_DEVICE_PATH_PROTOCOL               *DevicePath\r
   )\r
 {\r
-  CHAR16  *Converted;\r
-  BOOLEAN Result;\r
+  CHAR16                   *Converted;\r
+  BOOLEAN                  Result;\r
+  VOID                     *FileBuffer;\r
+  UINTN                    FileSize;\r
+  EFI_DEVICE_PATH_PROTOCOL *AbsDevicePath;\r
+  CHAR16                   *AbsConverted;\r
+  BOOLEAN                  Shortform;\r
+  EFI_DEVICE_PATH_PROTOCOL *Node;\r
 \r
   Converted = ConvertDevicePathToText (\r
                 DevicePath,\r
@@ -1452,24 +1458,57 @@ Match (
     return FALSE;\r
   }\r
 \r
+  Result = FALSE;\r
+  Shortform = FALSE;\r
   //\r
-  // Attempt to expand any relative UEFI device path starting with HD() to an\r
-  // absolute device path first. The logic imitates BdsLibBootViaBootOption().\r
-  // We don't have to free the absolute device path,\r
-  // BdsExpandPartitionPartialDevicePathToFull() has internal caching.\r
+  // Expand the short-form device path to full device path\r
   //\r
-  Result = FALSE;\r
-  if (DevicePathType (DevicePath) == MEDIA_DEVICE_PATH &&\r
-      DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP) {\r
-    EFI_DEVICE_PATH_PROTOCOL *AbsDevicePath;\r
-    CHAR16                   *AbsConverted;\r
-\r
-    AbsDevicePath = BdsExpandPartitionPartialDevicePathToFull (\r
-                      (HARDDRIVE_DEVICE_PATH *) DevicePath);\r
-    if (AbsDevicePath == NULL) {\r
+  if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) &&\r
+      (DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)) {\r
+    //\r
+    // Harddrive shortform device path\r
+    //\r
+    Shortform = TRUE;\r
+  } else if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) &&\r
+             (DevicePathSubType (DevicePath) == MEDIA_FILEPATH_DP)) {\r
+    //\r
+    // File-path shortform device path\r
+    //\r
+    Shortform = TRUE;\r
+  } else if ((DevicePathType (DevicePath) == MESSAGING_DEVICE_PATH) &&\r
+             (DevicePathSubType (DevicePath) == MSG_URI_DP)) {\r
+    //\r
+    // URI shortform device path\r
+    //\r
+    Shortform = TRUE;\r
+  } else {\r
+    for ( Node = DevicePath\r
+        ; !IsDevicePathEnd (Node)\r
+        ; Node = NextDevicePathNode (Node)\r
+        ) {\r
+      if ((DevicePathType (Node) == MESSAGING_DEVICE_PATH) &&\r
+          ((DevicePathSubType (Node) == MSG_USB_CLASS_DP) ||\r
+           (DevicePathSubType (Node) == MSG_USB_WWID_DP))) {\r
+        Shortform = TRUE;\r
+        break;\r
+      }\r
+    }\r
+  }\r
+\r
+  //\r
+  // Attempt to expand any relative UEFI device path to\r
+  // an absolute device path first.\r
+  //\r
+  if (Shortform) {\r
+    FileBuffer = EfiBootManagerGetLoadOptionBuffer (\r
+                   DevicePath, &AbsDevicePath, &FileSize\r
+                   );\r
+    if (FileBuffer == NULL) {\r
       goto Exit;\r
     }\r
+    FreePool (FileBuffer);\r
     AbsConverted = ConvertDevicePathToText (AbsDevicePath, FALSE, FALSE);\r
+    FreePool (AbsDevicePath);\r
     if (AbsConverted == NULL) {\r
       goto Exit;\r
     }\r
@@ -1538,11 +1577,11 @@ BootOrderComplete (
   Idx = 0;\r
   while (!RETURN_ERROR (Status) && Idx < ActiveCount) {\r
     if (!ActiveOption[Idx].Appended) {\r
-      CONST BDS_COMMON_OPTION        *Current;\r
-      CONST EFI_DEVICE_PATH_PROTOCOL *FirstNode;\r
+      CONST EFI_BOOT_MANAGER_LOAD_OPTION *Current;\r
+      CONST EFI_DEVICE_PATH_PROTOCOL     *FirstNode;\r
 \r
       Current = ActiveOption[Idx].BootOption;\r
-      FirstNode = Current->DevicePath;\r
+      FirstNode = Current->FilePath;\r
       if (FirstNode != NULL) {\r
         CHAR16        *Converted;\r
         STATIC CHAR16 ConvFallBack[] = L"<unable to convert>";\r
@@ -1635,7 +1674,7 @@ PruneBootVariables (
       CHAR16 VariableName[9];\r
 \r
       UnicodeSPrintAsciiFormat (VariableName, sizeof VariableName, "Boot%04x",\r
-        ActiveOption[Idx].BootOption->BootCurrent);\r
+        ActiveOption[Idx].BootOption->OptionNumber);\r
 \r
       //\r
       // "The space consumed by the deleted variable may not be available until\r
@@ -1699,6 +1738,17 @@ SetBootOrderFromQemu (
 \r
   UINTN                            TranslatedSize;\r
   CHAR16                           Translated[TRANSLATION_OUTPUT_SIZE];\r
+  EFI_BOOT_MANAGER_LOAD_OPTION     *BootOptions;\r
+  UINTN                            BootOptionCount;\r
+\r
+  //\r
+  // The QemuBootOrderLib is linked by OvmfPkg and ArmVirtPkg.\r
+  // OvmfPkg was changed to use the new BDS @ MdeModulePkg, so boot options\r
+  // are no longer stored in linked list.\r
+  // But we don't change the QemuBootOrderLib class interface because\r
+  // ArmVirtPkg are still using old BDS @ IntelFrameworkModulePkg.\r
+  //\r
+  ASSERT (BootOptionList == NULL);\r
 \r
   Status = QemuFwCfgFindFile ("bootorder", &FwCfgItem, &FwCfgSize);\r
   if (Status != RETURN_SUCCESS) {\r
@@ -1736,11 +1786,21 @@ SetBootOrderFromQemu (
     goto ErrorFreeFwCfg;\r
   }\r
 \r
-  Status = CollectActiveOptions (BootOptionList, &ActiveOption, &ActiveCount);\r
-  if (RETURN_ERROR (Status)) {\r
+  BootOptions = EfiBootManagerGetLoadOptions (\r
+                  &BootOptionCount, LoadOptionTypeBoot\r
+                  );\r
+  if (BootOptions == NULL) {\r
+    Status = RETURN_NOT_FOUND;\r
     goto ErrorFreeBootOrder;\r
   }\r
 \r
+  Status = CollectActiveOptions (\r
+             BootOptions, BootOptionCount, &ActiveOption, &ActiveCount\r
+             );\r
+  if (RETURN_ERROR (Status)) {\r
+    goto ErrorFreeBootOptions;\r
+  }\r
+\r
   if (FeaturePcdGet (PcdQemuBootOrderPciTranslation)) {\r
     Status = CreateExtraRootBusMap (&ExtraPciRoots);\r
     if (EFI_ERROR (Status)) {\r
@@ -1770,7 +1830,7 @@ SetBootOrderFromQemu (
         if (Match (\r
               Translated,\r
               TranslatedSize, // contains length, not size, in CHAR16's here\r
-              ActiveOption[Idx].BootOption->DevicePath\r
+              ActiveOption[Idx].BootOption->FilePath\r
               )\r
             ) {\r
           //\r
@@ -1831,6 +1891,9 @@ ErrorFreeExtraPciRoots:
 ErrorFreeActiveOption:\r
   FreePool (ActiveOption);\r
 \r
+ErrorFreeBootOptions:\r
+  EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);\r
+\r
 ErrorFreeBootOrder:\r
   FreePool (BootOrder.Data);\r
 \r
index 9374a612a7681ed2ed08209f4c1179fd8f95b751..40a315b5b52f7ab852720fb3831a1d58905e8c46 100644 (file)
 \r
 [Packages]\r
   MdePkg/MdePkg.dec\r
-  IntelFrameworkModulePkg/IntelFrameworkModulePkg.dec\r
+  MdeModulePkg/MdeModulePkg.dec\r
   OvmfPkg/OvmfPkg.dec\r
 \r
 [LibraryClasses]\r
   QemuFwCfgLib\r
   DebugLib\r
   MemoryAllocationLib\r
-  GenericBdsLib\r
+  UefiBootManagerLib\r
   UefiBootServicesTableLib\r
   UefiRuntimeServicesTableLib\r
   BaseLib\r