From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=66.187.233.73; helo=mx1.redhat.com; envelope-from=lersek@redhat.com; receiver=edk2-devel@lists.01.org Received: from mx1.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id B3477223CDC22 for ; Tue, 13 Mar 2018 14:16:22 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4243F4023BB3; Tue, 13 Mar 2018 21:22:44 +0000 (UTC) Received: from lacos-laptop-7.usersys.redhat.com (ovpn-124-72.rdu2.redhat.com [10.10.124.72]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4AA4A50335; Tue, 13 Mar 2018 21:22:43 +0000 (UTC) From: Laszlo Ersek To: edk2-devel-01 Cc: Ard Biesheuvel , Jordan Justen , Shannon Zhao , Xiang Zheng Date: Tue, 13 Mar 2018 22:22:31 +0100 Message-Id: <20180313212233.19215-5-lersek@redhat.com> In-Reply-To: <20180313212233.19215-1-lersek@redhat.com> References: <20180313212233.19215-1-lersek@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Tue, 13 Mar 2018 21:22:44 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Tue, 13 Mar 2018 21:22:44 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'lersek@redhat.com' RCPT:'' Subject: [PATCH 4/6] OvmfPkg/QemuBootOrderLib: add ConnectDevicesFromQemu() X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 13 Mar 2018 21:16:23 -0000 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 Cc: Jordan Justen Cc: Shannon Zhao Cc: Xiang Zheng Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Laszlo Ersek --- OvmfPkg/Include/Library/QemuBootOrderLib.h | 39 ++++- OvmfPkg/Library/QemuBootOrderLib/QemuBootOrderLib.c | 154 +++++++++++++++++++- 2 files changed, 189 insertions(+), 4 deletions(-) diff --git a/OvmfPkg/Include/Library/QemuBootOrderLib.h b/OvmfPkg/Include/Library/QemuBootOrderLib.h index 874344a95d80..d17de3bb8fae 100644 --- a/OvmfPkg/Include/Library/QemuBootOrderLib.h +++ b/OvmfPkg/Include/Library/QemuBootOrderLib.h @@ -20,6 +20,41 @@ #include +/** + Connect devices based on the boot order retrieved from QEMU. + + Attempt to retrieve the "bootorder" fw_cfg file from QEMU. Translate the + OpenFirmware device paths therein to UEFI device path fragments. Connect the + devices identified by the UEFI devpath prefixes as narrowly as possible, then + connect all their child devices, recursively. + + If this function fails, then platform BDS should fall back to + EfiBootManagerConnectAll(), or some other method for connecting any expected + boot devices. + + @retval RETURN_SUCCESS The "bootorder" fw_cfg file has been + parsed, and the referenced device-subtrees + have been connected. + + @retval RETURN_UNSUPPORTED QEMU's fw_cfg is not supported. + + @retval RETURN_NOT_FOUND Empty or nonexistent "bootorder" fw_cfg + file. + + @retval RETURN_INVALID_PARAMETER Parse error in the "bootorder" fw_cfg file. + + @retval RETURN_OUT_OF_RESOURCES Memory allocation failed. + + @return Error statuses propagated from underlying + functions. +**/ +RETURN_STATUS +EFIAPI +ConnectDevicesFromQemu ( + VOID + ); + + /** Set the boot order based on configuration retrieved from QEMU. @@ -29,8 +64,8 @@ translated fragments against the current list of boot options, and rewrite the BootOrder NvVar so that it corresponds to the order described in fw_cfg. - Platform BDS should call this function after EfiBootManagerConnectAll () and - EfiBootManagerRefreshAllBootOption () return. + Platform BDS should call this function after connecting any expected boot + devices and calling EfiBootManagerRefreshAllBootOption (). @retval RETURN_SUCCESS BootOrder NvVar rewritten. diff --git a/OvmfPkg/Library/QemuBootOrderLib/QemuBootOrderLib.c b/OvmfPkg/Library/QemuBootOrderLib/QemuBootOrderLib.c index a4213cf6d267..15e4c67e48c5 100644 --- a/OvmfPkg/Library/QemuBootOrderLib/QemuBootOrderLib.c +++ b/OvmfPkg/Library/QemuBootOrderLib/QemuBootOrderLib.c @@ -1453,6 +1453,156 @@ TranslateOfwPath ( } +/** + Connect devices based on the boot order retrieved from QEMU. + + Attempt to retrieve the "bootorder" fw_cfg file from QEMU. Translate the + OpenFirmware device paths therein to UEFI device path fragments. Connect the + devices identified by the UEFI devpath prefixes as narrowly as possible, then + connect all their child devices, recursively. + + If this function fails, then platform BDS should fall back to + EfiBootManagerConnectAll(), or some other method for connecting any expected + boot devices. + + @retval RETURN_SUCCESS The "bootorder" fw_cfg file has been + parsed, and the referenced device-subtrees + have been connected. + + @retval RETURN_UNSUPPORTED QEMU's fw_cfg is not supported. + + @retval RETURN_NOT_FOUND Empty or nonexistent "bootorder" fw_cfg + file. + + @retval RETURN_INVALID_PARAMETER Parse error in the "bootorder" fw_cfg file. + + @retval RETURN_OUT_OF_RESOURCES Memory allocation failed. + + @return Error statuses propagated from underlying + functions. +**/ +RETURN_STATUS +EFIAPI +ConnectDevicesFromQemu ( + VOID + ) +{ + RETURN_STATUS Status; + FIRMWARE_CONFIG_ITEM FwCfgItem; + UINTN FwCfgSize; + CHAR8 *FwCfg; + EFI_STATUS EfiStatus; + EXTRA_ROOT_BUS_MAP *ExtraPciRoots; + CONST CHAR8 *FwCfgPtr; + UINTN NumConnected; + UINTN TranslatedSize; + CHAR16 Translated[TRANSLATION_OUTPUT_SIZE]; + + Status = QemuFwCfgFindFile ("bootorder", &FwCfgItem, &FwCfgSize); + if (RETURN_ERROR (Status)) { + return Status; + } + + if (FwCfgSize == 0) { + return RETURN_NOT_FOUND; + } + + FwCfg = AllocatePool (FwCfgSize); + if (FwCfg == NULL) { + return RETURN_OUT_OF_RESOURCES; + } + + QemuFwCfgSelectItem (FwCfgItem); + QemuFwCfgReadBytes (FwCfgSize, FwCfg); + if (FwCfg[FwCfgSize - 1] != '\0') { + Status = RETURN_INVALID_PARAMETER; + goto FreeFwCfg; + } + DEBUG ((DEBUG_VERBOSE, "%a: FwCfg:\n", __FUNCTION__)); + DEBUG ((DEBUG_VERBOSE, "%a\n", FwCfg)); + DEBUG ((DEBUG_VERBOSE, "%a: FwCfg: \n", __FUNCTION__)); + + if (FeaturePcdGet (PcdQemuBootOrderPciTranslation)) { + EfiStatus = CreateExtraRootBusMap (&ExtraPciRoots); + if (EFI_ERROR (EfiStatus)) { + Status = (RETURN_STATUS)EfiStatus; + goto FreeFwCfg; + } + } else { + ExtraPciRoots = NULL; + } + + // + // Translate each OpenFirmware path to a UEFI devpath prefix. + // + FwCfgPtr = FwCfg; + NumConnected = 0; + TranslatedSize = ARRAY_SIZE (Translated); + Status = TranslateOfwPath (&FwCfgPtr, ExtraPciRoots, Translated, + &TranslatedSize); + while (!RETURN_ERROR (Status)) { + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + EFI_HANDLE Controller; + + // + // Convert the UEFI devpath prefix to binary representation. + // + ASSERT (Translated[TranslatedSize] == L'\0'); + DevicePath = ConvertTextToDevicePath (Translated); + if (DevicePath == NULL) { + Status = RETURN_OUT_OF_RESOURCES; + goto FreeExtraPciRoots; + } + // + // Advance along DevicePath, connecting the nodes individually, and asking + // drivers not to produce sibling nodes. Retrieve the controller handle + // associated with the full DevicePath -- this is the device that QEMU's + // OFW devpath refers to. + // + EfiStatus = EfiBootManagerConnectDevicePath (DevicePath, &Controller); + FreePool (DevicePath); + if (EFI_ERROR (EfiStatus)) { + Status = (RETURN_STATUS)EfiStatus; + goto FreeExtraPciRoots; + } + // + // Because QEMU's OFW devpaths have lesser expressive power than UEFI + // devpaths (i.e., DevicePath is considered a prefix), connect the tree + // rooted at Controller, recursively. If no children are produced + // (EFI_NOT_FOUND), that's OK. + // + EfiStatus = gBS->ConnectController (Controller, NULL, NULL, TRUE); + if (EFI_ERROR (EfiStatus) && EfiStatus != EFI_NOT_FOUND) { + Status = (RETURN_STATUS)EfiStatus; + goto FreeExtraPciRoots; + } + ++NumConnected; + // + // Move to the next OFW devpath. + // + TranslatedSize = ARRAY_SIZE (Translated); + Status = TranslateOfwPath (&FwCfgPtr, ExtraPciRoots, Translated, + &TranslatedSize); + } + + if (Status == RETURN_NOT_FOUND && NumConnected > 0) { + DEBUG ((DEBUG_INFO, "%a: %Lu OpenFirmware device path(s) connected\n", + __FUNCTION__, (UINT64)NumConnected)); + Status = RETURN_SUCCESS; + } + +FreeExtraPciRoots: + if (ExtraPciRoots != NULL) { + DestroyExtraRootBusMap (ExtraPciRoots); + } + +FreeFwCfg: + FreePool (FwCfg); + + return Status; +} + + /** Convert the UEFI DevicePath to full text representation with DevPathToText, @@ -1743,8 +1893,8 @@ PruneBootVariables ( translated fragments against the current list of boot options, and rewrite the BootOrder NvVar so that it corresponds to the order described in fw_cfg. - Platform BDS should call this function after EfiBootManagerConnectAll () and - EfiBootManagerRefreshAllBootOption () return. + Platform BDS should call this function after connecting any expected boot + devices and calling EfiBootManagerRefreshAllBootOption (). @retval RETURN_SUCCESS BootOrder NvVar rewritten. -- 2.14.1.3.gb7cf6e02401b