]> git.proxmox.com Git - mirror_edk2.git/commitdiff
OvmfPkg/QemuBootOrderLib: add ConnectDevicesFromQemu()
authorLaszlo Ersek <lersek@redhat.com>
Tue, 13 Mar 2018 17:37:17 +0000 (18:37 +0100)
committerLaszlo Ersek <lersek@redhat.com>
Wed, 14 Mar 2018 10:24:24 +0000 (11:24 +0100)
QemuBootOrderLib expects PlatformBootManagerLib to call the following
triplet:

(1) EfiBootManagerConnectAll(),
(2) EfiBootManagerRefreshAllBootOption(),
(3) SetBootOrderFromQemu().

This leads to bad performance, when many devices exist such that the
firmware can drive them, but they aren't marked for booting in the
"bootorder" fw_cfg file. Namely,

(1) EfiBootManagerConnectAll() talks to all hardware, which takes long.
    Plus some DriverBindingStart() functions write NV variables, which is
    also slow. (For example, the IP config policy for each NIC is stored
    in an NV var that is named after the MAC).

(2) EfiBootManagerRefreshAllBootOption() generates boot options from the
    protocol instances produced by (1). Writing boot options is slow.

(3) Under the above circumstances, SetBootOrderFromQemu() removes most of
    the boot options produced by (2). Erasing boot options is slow.

Introduce ConnectDevicesFromQemu() as a replacement for (1): only connect
devices that the QEMU user actually wants to boot off of.

(There's a slight loss of compatibility when a platform switches from
EfiBootManagerConnectAll() to ConnectDevicesFromQemu().
EfiBootManagerConnectAll() may produce UEFI device paths that are unknown
to QemuBootOrderLib (that is, for neither PCI- nor virtio-mmio-based
devices). The BootOrderComplete() function lets such unmatched boot
options survive at the end of the boot order. With
ConnectDevicesFromQemu(), these options will not be auto-generated in the
first place. They may still be produced by other means.

SetBootOrderFromQemu() is not modified in any way; reordering+filtering
boot options remains a separate task.)

Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Shannon Zhao <zhaoshenglong@huawei.com>
Cc: Xiang Zheng <xiang.zheng@linaro.org>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Tested-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> # ArmVirtQemu
Reviewed-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Tested-by: Xiang Zheng <xiang.zheng@linaro.org>
OvmfPkg/Include/Library/QemuBootOrderLib.h
OvmfPkg/Library/QemuBootOrderLib/QemuBootOrderLib.c

index 874344a95d80258e6f1d4a07e0a67fd81f9cdb7c..d17de3bb8fae2c732846b7e62b82e6727c933c78 100644 (file)
 #include <Base.h>\r
 \r
 \r
+/**\r
+  Connect devices based on the boot order retrieved from QEMU.\r
+\r
+  Attempt to retrieve the "bootorder" fw_cfg file from QEMU. Translate the\r
+  OpenFirmware device paths therein to UEFI device path fragments. Connect the\r
+  devices identified by the UEFI devpath prefixes as narrowly as possible, then\r
+  connect all their child devices, recursively.\r
+\r
+  If this function fails, then platform BDS should fall back to\r
+  EfiBootManagerConnectAll(), or some other method for connecting any expected\r
+  boot devices.\r
+\r
+  @retval RETURN_SUCCESS            The "bootorder" fw_cfg file has been\r
+                                    parsed, and the referenced device-subtrees\r
+                                    have been connected.\r
+\r
+  @retval RETURN_UNSUPPORTED        QEMU's fw_cfg is not supported.\r
+\r
+  @retval RETURN_NOT_FOUND          Empty or nonexistent "bootorder" fw_cfg\r
+                                    file.\r
+\r
+  @retval RETURN_INVALID_PARAMETER  Parse error in the "bootorder" fw_cfg file.\r
+\r
+  @retval RETURN_OUT_OF_RESOURCES   Memory allocation failed.\r
+\r
+  @return                           Error statuses propagated from underlying\r
+                                    functions.\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+ConnectDevicesFromQemu (\r
+  VOID\r
+  );\r
+\r
+\r
 /**\r
 \r
   Set the boot order based on configuration retrieved from QEMU.\r
@@ -29,8 +64,8 @@
   translated fragments against the current list of boot options, and rewrite\r
   the BootOrder NvVar so that it corresponds to the order described in fw_cfg.\r
 \r
-  Platform BDS should call this function after EfiBootManagerConnectAll () and\r
-  EfiBootManagerRefreshAllBootOption () return.\r
+  Platform BDS should call this function after connecting any expected boot\r
+  devices and calling EfiBootManagerRefreshAllBootOption ().\r
 \r
   @retval RETURN_SUCCESS            BootOrder NvVar rewritten.\r
 \r
index a4213cf6d267660a56cc34c3e405a5c03e09ec96..15e4c67e48c57fbc075c3c13f5eba575c6b373db 100644 (file)
@@ -1453,6 +1453,156 @@ TranslateOfwPath (
 }\r
 \r
 \r
+/**\r
+  Connect devices based on the boot order retrieved from QEMU.\r
+\r
+  Attempt to retrieve the "bootorder" fw_cfg file from QEMU. Translate the\r
+  OpenFirmware device paths therein to UEFI device path fragments. Connect the\r
+  devices identified by the UEFI devpath prefixes as narrowly as possible, then\r
+  connect all their child devices, recursively.\r
+\r
+  If this function fails, then platform BDS should fall back to\r
+  EfiBootManagerConnectAll(), or some other method for connecting any expected\r
+  boot devices.\r
+\r
+  @retval RETURN_SUCCESS            The "bootorder" fw_cfg file has been\r
+                                    parsed, and the referenced device-subtrees\r
+                                    have been connected.\r
+\r
+  @retval RETURN_UNSUPPORTED        QEMU's fw_cfg is not supported.\r
+\r
+  @retval RETURN_NOT_FOUND          Empty or nonexistent "bootorder" fw_cfg\r
+                                    file.\r
+\r
+  @retval RETURN_INVALID_PARAMETER  Parse error in the "bootorder" fw_cfg file.\r
+\r
+  @retval RETURN_OUT_OF_RESOURCES   Memory allocation failed.\r
+\r
+  @return                           Error statuses propagated from underlying\r
+                                    functions.\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+ConnectDevicesFromQemu (\r
+  VOID\r
+  )\r
+{\r
+  RETURN_STATUS        Status;\r
+  FIRMWARE_CONFIG_ITEM FwCfgItem;\r
+  UINTN                FwCfgSize;\r
+  CHAR8                *FwCfg;\r
+  EFI_STATUS           EfiStatus;\r
+  EXTRA_ROOT_BUS_MAP   *ExtraPciRoots;\r
+  CONST CHAR8          *FwCfgPtr;\r
+  UINTN                NumConnected;\r
+  UINTN                TranslatedSize;\r
+  CHAR16               Translated[TRANSLATION_OUTPUT_SIZE];\r
+\r
+  Status = QemuFwCfgFindFile ("bootorder", &FwCfgItem, &FwCfgSize);\r
+  if (RETURN_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  if (FwCfgSize == 0) {\r
+    return RETURN_NOT_FOUND;\r
+  }\r
+\r
+  FwCfg = AllocatePool (FwCfgSize);\r
+  if (FwCfg == NULL) {\r
+    return RETURN_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  QemuFwCfgSelectItem (FwCfgItem);\r
+  QemuFwCfgReadBytes (FwCfgSize, FwCfg);\r
+  if (FwCfg[FwCfgSize - 1] != '\0') {\r
+    Status = RETURN_INVALID_PARAMETER;\r
+    goto FreeFwCfg;\r
+  }\r
+  DEBUG ((DEBUG_VERBOSE, "%a: FwCfg:\n", __FUNCTION__));\r
+  DEBUG ((DEBUG_VERBOSE, "%a\n", FwCfg));\r
+  DEBUG ((DEBUG_VERBOSE, "%a: FwCfg: <end>\n", __FUNCTION__));\r
+\r
+  if (FeaturePcdGet (PcdQemuBootOrderPciTranslation)) {\r
+    EfiStatus = CreateExtraRootBusMap (&ExtraPciRoots);\r
+    if (EFI_ERROR (EfiStatus)) {\r
+      Status = (RETURN_STATUS)EfiStatus;\r
+      goto FreeFwCfg;\r
+    }\r
+  } else {\r
+    ExtraPciRoots = NULL;\r
+  }\r
+\r
+  //\r
+  // Translate each OpenFirmware path to a UEFI devpath prefix.\r
+  //\r
+  FwCfgPtr = FwCfg;\r
+  NumConnected = 0;\r
+  TranslatedSize = ARRAY_SIZE (Translated);\r
+  Status = TranslateOfwPath (&FwCfgPtr, ExtraPciRoots, Translated,\r
+             &TranslatedSize);\r
+  while (!RETURN_ERROR (Status)) {\r
+    EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
+    EFI_HANDLE               Controller;\r
+\r
+    //\r
+    // Convert the UEFI devpath prefix to binary representation.\r
+    //\r
+    ASSERT (Translated[TranslatedSize] == L'\0');\r
+    DevicePath = ConvertTextToDevicePath (Translated);\r
+    if (DevicePath == NULL) {\r
+      Status = RETURN_OUT_OF_RESOURCES;\r
+      goto FreeExtraPciRoots;\r
+    }\r
+    //\r
+    // Advance along DevicePath, connecting the nodes individually, and asking\r
+    // drivers not to produce sibling nodes. Retrieve the controller handle\r
+    // associated with the full DevicePath -- this is the device that QEMU's\r
+    // OFW devpath refers to.\r
+    //\r
+    EfiStatus = EfiBootManagerConnectDevicePath (DevicePath, &Controller);\r
+    FreePool (DevicePath);\r
+    if (EFI_ERROR (EfiStatus)) {\r
+      Status = (RETURN_STATUS)EfiStatus;\r
+      goto FreeExtraPciRoots;\r
+    }\r
+    //\r
+    // Because QEMU's OFW devpaths have lesser expressive power than UEFI\r
+    // devpaths (i.e., DevicePath is considered a prefix), connect the tree\r
+    // rooted at Controller, recursively. If no children are produced\r
+    // (EFI_NOT_FOUND), that's OK.\r
+    //\r
+    EfiStatus = gBS->ConnectController (Controller, NULL, NULL, TRUE);\r
+    if (EFI_ERROR (EfiStatus) && EfiStatus != EFI_NOT_FOUND) {\r
+      Status = (RETURN_STATUS)EfiStatus;\r
+      goto FreeExtraPciRoots;\r
+    }\r
+    ++NumConnected;\r
+    //\r
+    // Move to the next OFW devpath.\r
+    //\r
+    TranslatedSize = ARRAY_SIZE (Translated);\r
+    Status = TranslateOfwPath (&FwCfgPtr, ExtraPciRoots, Translated,\r
+               &TranslatedSize);\r
+  }\r
+\r
+  if (Status == RETURN_NOT_FOUND && NumConnected > 0) {\r
+    DEBUG ((DEBUG_INFO, "%a: %Lu OpenFirmware device path(s) connected\n",\r
+      __FUNCTION__, (UINT64)NumConnected));\r
+    Status = RETURN_SUCCESS;\r
+  }\r
+\r
+FreeExtraPciRoots:\r
+  if (ExtraPciRoots != NULL) {\r
+    DestroyExtraRootBusMap (ExtraPciRoots);\r
+  }\r
+\r
+FreeFwCfg:\r
+  FreePool (FwCfg);\r
+\r
+  return Status;\r
+}\r
+\r
+\r
 /**\r
 \r
   Convert the UEFI DevicePath to full text representation with DevPathToText,\r
@@ -1743,8 +1893,8 @@ PruneBootVariables (
   translated fragments against the current list of boot options, and rewrite\r
   the BootOrder NvVar so that it corresponds to the order described in fw_cfg.\r
 \r
-  Platform BDS should call this function after EfiBootManagerConnectAll () and\r
-  EfiBootManagerRefreshAllBootOption () return.\r
+  Platform BDS should call this function after connecting any expected boot\r
+  devices and calling EfiBootManagerRefreshAllBootOption ().\r
 \r
   @retval RETURN_SUCCESS            BootOrder NvVar rewritten.\r
 \r