From nobody Mon Feb 9 07:39:48 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+86054+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+86054+1787277+3901457@groups.io ARC-Seal: i=1; a=rsa-sha256; t=1643121269; cv=none; d=zohomail.com; s=zohoarc; b=IwIC4RywkSchF0Wzci18eMCnXZ5MF9JRgyWfGMrQkpHD5f4eN6Ft/KNA/t8ZdFJ93Ef/HN5fBFZZMVbMkP3a1MjHoSwHb/GH6tWPIosm3Ax7v+wmI1nrZBJ+BwUmETiS7e1bmzEn9jGjQ/B01X2fBimG0GvEmLkEaelgH9opa4k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1643121269; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=lL2MMem1C9+H6hZGNsDLLXXKasDDgiYRcXc9olV6QCI=; b=eZYyC6s66VaNflh/80PWhO7k1WDIEOeSv+jn0cEpOXuRAA9oURpFHeWMLd/lxtbgIVnMeIEMAjHRis5Ermj/GYDV4v0K9ozvvcnNEKazMHBt/GamhQO8UJOX3D130603l4ORF8tEeuqbjjs8qDHgqMpjWFjXwIQWfkfxnh4bvoo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+86054+1787277+3901457@groups.io Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1643121269667430.8017898894294; Tue, 25 Jan 2022 06:34:29 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id Djh3YY1788612xRJvMzvaMeI; Tue, 25 Jan 2022 06:34:29 -0800 X-Received: from smtp-fw-2101.amazon.com (smtp-fw-2101.amazon.com [72.21.196.25]) by mx.groups.io with SMTP id smtpd.web11.7692.1643121268398909240 for ; Tue, 25 Jan 2022 06:34:28 -0800 X-IronPort-AV: E=Sophos;i="5.88,315,1635206400"; d="scan'208";a="168388205" X-Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-iad-1a-c92fe759.us-east-1.amazon.com) ([10.43.8.2]) by smtp-border-fw-2101.iad2.amazon.com with ESMTP; 25 Jan 2022 14:34:27 +0000 X-Received: from EX13D49EUC003.ant.amazon.com (iad12-ws-svc-p26-lb9-vlan2.iad.amazon.com [10.40.163.34]) by email-inbound-relay-iad-1a-c92fe759.us-east-1.amazon.com (Postfix) with ESMTPS id 33524C08EC; Tue, 25 Jan 2022 14:34:26 +0000 (UTC) X-Received: from ub4014a598e6c52.ant.amazon.com (10.43.160.207) by EX13D49EUC003.ant.amazon.com (10.43.164.91) with Microsoft SMTP Server (TLS) id 15.0.1497.28; Tue, 25 Jan 2022 14:34:23 +0000 From: "Ojeda Leon, Nicolas via groups.io" To: CC: , Nicolas Ojeda Leon , Alexander Graf , Gerd Hoffmann Subject: [edk2-devel] [PATCH v3 3/8] Ovmf/HardwareInfoLib: Add Dxe lib to dynamically parse heterogenous data Date: Tue, 25 Jan 2022 15:30:10 +0100 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [10.43.160.207] X-ClientProxiedBy: EX13D46UWB004.ant.amazon.com (10.43.161.204) To EX13D49EUC003.ant.amazon.com (10.43.164.91) Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,ncoleon@amazon.com X-Gm-Message-State: RoTEbb4xFjxueheG4ZrI4ToNx1787277AA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1643121269; bh=erzpR3HzYeV2i6Ueyxv7Y1ldTvPA9oLi81uq3YUCi3A=; h=CC:Content-Type:Date:From:Reply-To:Subject:To; b=RMloZuETXH7gZ5OMsX5l46F+8+l1HUyXGA1RBnY9RL7SFjArfj2DVAjvT0pAObszrrr cxLSE2tKE/ACTUR3BIZ91WRG0Qc8L7L5R0Wq2uhSC5eXjV0NysLbUWeGy86NYLRk44p/i DYF8gNMuVWIWFo9a2aP5rg0skn6L4vfpAx8= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1643121271220100003 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Following the Hardware Info library, create the DxeHardwareInfoLib which implements the whole API capable of parsing heterogeneous hardware information. The list-like API grants callers a flexible and common pattern to retrieve the data. Moreover, the initial source is a BLOB which generalizes the host-to-guest transmission mechanism. The Hardware Info library main objective is to provide a way to describe non-discoverable hardware so that the host can share the available resources with the guest in Ovmf platforms. This change features and embraces the main idea behind the library by providing an API that parses a BLOB into a linked list to retrieve hardware data from any source. Additionally, list-like APIs are provided so that the hardware info list can be traversed conveniently. Similarly, the capability is provided to filter results by specific hardware types. However, heterogeneous elements can be added to the list, increasing the flexibility. This way, a single source, for example a fw-cfg file, can be used to describe several instances of multiple types of hardware. This part of the Hardware Info library makes use of dynamic memory and is intended for stages in which memory services are available. A motivation example is the PciHostBridgeLib. This library, part of the PCI driver populates the list of PCI root bridges during DXE stage for future steps to discover the resources under them. The hardware info library can be used to obtain the detailed description of available host bridges, for instance in the form of a fw-cfg file, and parse that information into a dynmaic list that allows, first to verify consistency of the data, and second discover the resources availabe for each root bridge. Cc: Alexander Graf Cc: Gerd Hoffmann Signed-off-by: Nicolas Ojeda Leon Acked-by: Gerd Hoffmann --- .../HardwareInfoLib/DxeHardwareInfoLib.inf | 43 +++ .../Library/HardwareInfoLib/HardwareInfoDxe.c | 255 ++++++++++++++++++ OvmfPkg/OvmfPkgX64.dsc | 1 + 3 files changed, 299 insertions(+) create mode 100644 OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf create mode 100644 OvmfPkg/Library/HardwareInfoLib/HardwareInfoDxe.c diff --git a/OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf b/OvmfP= kg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf new file mode 100644 index 0000000000..0a93d24b8a --- /dev/null +++ b/OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf @@ -0,0 +1,43 @@ +## @file +# Hardware information library to describe non-discoverable hardware reso= urces +# +# Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. +# +# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the B= SD License +# which accompanies this distribution. The full text of the license may = be found at +# http://opensource.org/licenses/bsd-license.php +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IM= PLIED. +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D DxeHardwareInfoLib + FILE_GUID =3D F60B206A-5C56-11EC-AEAC-67CB080BCFF2 + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D DxeHardwareInfoLib + +# +# The following information is for reference only and not required by the = build +# tools. +# +# VALID_ARCHITECTURES =3D X64 +# + +[Sources] + HardwareInfoDxe.c + HardwareInfoPciHostBridgeLib.c + QemuFwCfgHardwareInfoLib.c + +[Packages] + MdeModulePkg/MdeModulePkg.dec + MdePkg/MdePkg.dec + OvmfPkg/OvmfPkg.dec + +[LibraryClasses] + BaseMemoryLib + DebugLib + MemoryAllocationLib diff --git a/OvmfPkg/Library/HardwareInfoLib/HardwareInfoDxe.c b/OvmfPkg/Li= brary/HardwareInfoLib/HardwareInfoDxe.c new file mode 100644 index 0000000000..4f0b51c942 --- /dev/null +++ b/OvmfPkg/Library/HardwareInfoLib/HardwareInfoDxe.c @@ -0,0 +1,255 @@ +/*/@file + Hardware info parsing functions. + Binary data is expected as a consecutive series of header - object pairs. + Complete library providing list-like interface to dynamically manipulate + hardware info objects and parsing from a generic blob. + + Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e found at + http://opensource.org/licenses/bsd-license.php + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMP= LIED. +**/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +EFI_STATUS +CreateHardwareInfoList ( + IN UINT8 *Blob, + IN UINTN BlobSize, + IN HARDWARE_INFO_TYPE TypeFilter, + OUT LIST_ENTRY *ListHead + ) +{ + UINT8 *Index; + HARDWARE_INFO *HwComponent; + + if ((Blob =3D=3D NULL) || (BlobSize <=3D 0) || + (ListHead =3D=3D NULL)) + { + return EFI_INVALID_PARAMETER; + } + + Index =3D Blob; + while (Index < (Blob + BlobSize)) { + HwComponent =3D AllocateZeroPool (sizeof (HARDWARE_INFO)); + + if (HwComponent =3D=3D NULL) { + goto FailedAllocate; + } + + HwComponent->Header.Type.Uint64 =3D *((UINT64 *)Index); + Index +=3D sizeof (HwComponent->Header.Type); + HwComponent->Header.Size =3D *((UINT64 *)(Index)); + Index +=3D sizeof (HwComponent->Header.Size); + + // + // Check if optional TypeFilter is set, skip if the current + // object is of a different type and release the partially + // allocated object + // + if ((TypeFilter !=3D HardwareInfoTypeUndefined) && + (HwComponent->Header.Type.Value !=3D TypeFilter)) + { + FreePool (HwComponent); + Index +=3D HwComponent->Header.Size; + continue; + } + + HwComponent->Data.Raw =3D AllocateZeroPool (HwComponent->Header.Size); + if (HwComponent->Data.Raw =3D=3D NULL) { + goto FreeResources; + } + + CopyMem (HwComponent->Data.Raw, Index, HwComponent->Header.Size); + Index +=3D HwComponent->Header.Size; + + InsertTailList (ListHead, &HwComponent->Link); + } + + return EFI_SUCCESS; + +FreeResources: + // + // Clean the resources allocated in the incomplete cycle + // + FreePool (HwComponent); + +FailedAllocate: + DEBUG (( + EFI_D_ERROR, + "%a: Failed to allocate memory for hardware info\n", + __FUNCTION__ + )); + + return EFI_OUT_OF_RESOURCES; +} + +VOID +FreeHardwareInfoList ( + IN OUT LIST_ENTRY *ListHead + ) +{ + LIST_ENTRY *CurrentLink; + HARDWARE_INFO *HwComponent; + + if (IsListEmpty (ListHead)) { + return; + } + + CurrentLink =3D ListHead->ForwardLink; + while (CurrentLink !=3D NULL && CurrentLink !=3D ListHead) { + HwComponent =3D HARDWARE_INFO_FROM_LINK (CurrentLink); + + // + // Remove item from list before invalidating the pointers + // + CurrentLink =3D RemoveEntryList (CurrentLink); + + FreePool (HwComponent->Data.Raw); + FreePool (HwComponent); + } +} + +/** + Validates if the specified Node has a valid data size and is of + specified type. + The data size can be less or equal to the provided type size to be + regarded as valid and thus accessible with the typed pointer. + + For future compatibility the size is allowed to be smaller so that + different versions interpret fields differently and, particularly, + have smaller data structures. However, it cannot be larger than the + type size to avoid accessing memory out of bounds. + + @param[in] Node Hardware Info node to be validated + @param[in] TypeSize Size (in bytes) of the data type intended to be + used to dereference the data. + @retval TRUE Node is valid and can be accessed + @retval FALSE Node is not valid +/*/ +STATIC +BOOLEAN +IsHardwareInfoNodeValidByType ( + IN LIST_ENTRY *ListHead, + IN LIST_ENTRY *Link, + IN HARDWARE_INFO_TYPE Type, + IN UINTN TypeSize + ) +{ + HARDWARE_INFO *HwComponent; + + if (IsNull (ListHead, Link)) { + return FALSE; + } + + HwComponent =3D HARDWARE_INFO_FROM_LINK (Link); + + // + // Verify if the node type is the specified one and the size of + // the data allocated to the node is greater than the size of + // the type intended to dereference it in order to avoid access + // to memory out of bondaries. + // + if ((HwComponent->Header.Type.Value =3D=3D Type) && + (HwComponent->Header.Size >=3D TypeSize)) + { + return TRUE; + } + + return FALSE; +} + +UINTN +GetHardwareInfoCountByType ( + IN LIST_ENTRY *ListHead, + IN HARDWARE_INFO_TYPE Type, + IN UINTN TypeSize + ) +{ + UINTN Count; + LIST_ENTRY *Link; + + Count =3D 0; + for (Link =3D GetFirstHardwareInfoByType (ListHead, Type, TypeSize); + !IsNull (ListHead, Link); + Link =3D GetNextHardwareInfoByType (ListHead, Link, Type, TypeSize)) + { + if (IsHardwareInfoNodeValidByType (ListHead, Link, Type, TypeSize)) { + Count++; + } + } + + return Count; +} + +LIST_ENTRY * +GetFirstHardwareInfoByType ( + IN LIST_ENTRY *ListHead, + IN HARDWARE_INFO_TYPE Type, + IN UINTN TypeSize + ) +{ + LIST_ENTRY *Link; + + if (IsListEmpty (ListHead)) { + return ListHead; + } + + Link =3D GetFirstNode (ListHead); + + if (IsHardwareInfoNodeValidByType (ListHead, Link, Type, TypeSize)) { + return Link; + } + + return GetNextHardwareInfoByType (ListHead, Link, Type, TypeSize); +} + +LIST_ENTRY * +GetNextHardwareInfoByType ( + IN LIST_ENTRY *ListHead, + IN LIST_ENTRY *Node, + IN HARDWARE_INFO_TYPE Type, + IN UINTN TypeSize + ) +{ + LIST_ENTRY *Link; + + Link =3D GetNextNode (ListHead, Node); + + while (!IsNull (ListHead, Link)) { + if (IsHardwareInfoNodeValidByType (ListHead, Link, Type, TypeSize)) { + // + // Found a node of specified type and with valid size. Break and + // return the found node. + // + break; + } + + Link =3D GetNextNode (ListHead, Link); + } + + return Link; +} + +BOOLEAN +EndOfHardwareInfoList ( + IN LIST_ENTRY *ListHead, + IN LIST_ENTRY *Node + ) +{ + return IsNull (ListHead, Node); +} diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc index 7a36f62e9f..b376387b7c 100644 --- a/OvmfPkg/OvmfPkgX64.dsc +++ b/OvmfPkg/OvmfPkgX64.dsc @@ -181,6 +181,7 @@ LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLi= b.inf PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf + DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf !if $(SMM_REQUIRE) =3D=3D FALSE LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf !endif --=20 2.17.1 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B Sitz: Berlin Ust-ID: DE 289 237 879 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#86054): https://edk2.groups.io/g/devel/message/86054 Mute This Topic: https://groups.io/mt/88672481/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-