From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from EUR05-VI1-obe.outbound.protection.outlook.com (EUR05-VI1-obe.outbound.protection.outlook.com [40.107.21.77]) by mx.groups.io with SMTP id smtpd.web12.16966.1597245792439210740 for ; Wed, 12 Aug 2020 08:23:13 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@armh.onmicrosoft.com header.s=selector2-armh-onmicrosoft-com header.b=PFiwQ5mS; spf=pass (domain: arm.com, ip: 40.107.21.77, mailfrom: sami.mujawar@arm.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=UPnzeZU3h57aQAh7WpqlaTv7cL3WAaETHFbul1ns8ew=; b=PFiwQ5mScQEok89mjGMsr7KUeCJ8nYsF05PFtipABBkNCU+0ErsA0eG5CYI6BZ69E33es9B+6j1UJb+tkqWCd/HVt2X+sVSdqhuun0g4yOlS9j4I6RZNcA4I2bIE8ZWl+uBi+srljLLf063WatjTvXyenqKd16eqFULByr0ZF5I= Received: from AM6P195CA0099.EURP195.PROD.OUTLOOK.COM (2603:10a6:209:86::40) by HE1PR0802MB2426.eurprd08.prod.outlook.com (2603:10a6:3:e1::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3283.15; Wed, 12 Aug 2020 15:23:08 +0000 Received: from VE1EUR03FT005.eop-EUR03.prod.protection.outlook.com (2603:10a6:209:86:cafe::ad) by AM6P195CA0099.outlook.office365.com (2603:10a6:209:86::40) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3283.15 via Frontend Transport; Wed, 12 Aug 2020 15:23:08 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; edk2.groups.io; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;edk2.groups.io; dmarc=bestguesspass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 63.35.35.123 as permitted sender) receiver=protection.outlook.com; client-ip=63.35.35.123; helo=64aa7808-outbound-1.mta.getcheckrecipient.com; Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by VE1EUR03FT005.mail.protection.outlook.com (10.152.18.172) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3283.16 via Frontend Transport; Wed, 12 Aug 2020 15:23:08 +0000 Received: ("Tessian outbound 7a6fb63c1e64:v64"); Wed, 12 Aug 2020 15:23:08 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: c583ff2ae84a63c6 X-CR-MTA-TID: 64aa7808 Received: from e805a9d0093e.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 26B992BF-E1A5-4DE0-A229-3354111565FD.1; Wed, 12 Aug 2020 15:23:02 +0000 Received: from EUR04-HE1-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id e805a9d0093e.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Wed, 12 Aug 2020 15:23:02 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=d8cXsxwdjMEYEroTCQVw5c3ccGxGZrHfkPVpDzM2xqj4Xvla+ZR0E8bsX54Y+MxIBslj6acAl9+34/ErRRpyJWOzbdkyKvqj56SDgUqcdwMThPJzx/muiLt3e45fpIfg2bYnZmvh0ai8EbfzQUHpOZA5QVmkrFWoaEs7aYz0PiP7wjy4sfkNHem84ZHQIa+uGHE6YJ3TW9Ig5OcC/9qiqlSJzWXcFAhgccgk/CTLiuVgM2rrRdTDLUcxhcBKB9LLAs8asKxZff4aepVFYBL5eKMsIQQ9TcaGqXqzhVzQF4+kGOhcniSwnRPauCsqu4N7RnzZ1yiIcde3uLfzyb+yCw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=UPnzeZU3h57aQAh7WpqlaTv7cL3WAaETHFbul1ns8ew=; b=nPGnKrrvuVCDHNeC79ILSWzBfo1QZruCs24NOGpkTuS2ZKG0zALIfWcYCIcNYut0iFwMZbOHMj9XEOUPRoc/hqk+dFm2tszL+04m92zMgVPtr0oMjaHg1XZrP/BSpoeIOnCQG3W5uITRgRIuYjqf+We4i/pvBlJ9hnmyO/qcsOxoYcve2D7TEbovbN8HScioGdN6IXXMd/BYWhxKp26iWgelvcEi2DDgV/1XxqD73Lpq055X8OYFy9AY28chBMinEKG2a5xz2q8t9TCYT/dCOZYoycLglynwb+toTWOOfymUVTGj7WK+jXPHsqP0Sbukr1QQE7SorAZUKpHN/gP1jQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 40.67.248.234) smtp.rcpttodomain=edk2.groups.io smtp.mailfrom=arm.com; dmarc=bestguesspass action=none header.from=arm.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=UPnzeZU3h57aQAh7WpqlaTv7cL3WAaETHFbul1ns8ew=; b=PFiwQ5mScQEok89mjGMsr7KUeCJ8nYsF05PFtipABBkNCU+0ErsA0eG5CYI6BZ69E33es9B+6j1UJb+tkqWCd/HVt2X+sVSdqhuun0g4yOlS9j4I6RZNcA4I2bIE8ZWl+uBi+srljLLf063WatjTvXyenqKd16eqFULByr0ZF5I= Received: from DB6PR0301CA0057.eurprd03.prod.outlook.com (2603:10a6:4:54::25) by DB6PR0802MB2294.eurprd08.prod.outlook.com (2603:10a6:4:85::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3283.15; Wed, 12 Aug 2020 15:22:59 +0000 Received: from DB5EUR03FT060.eop-EUR03.prod.protection.outlook.com (2603:10a6:4:54:cafe::50) by DB6PR0301CA0057.outlook.office365.com (2603:10a6:4:54::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3283.15 via Frontend Transport; Wed, 12 Aug 2020 15:22:59 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 40.67.248.234) smtp.mailfrom=arm.com; edk2.groups.io; dkim=none (message not signed) header.d=none;edk2.groups.io; dmarc=bestguesspass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 40.67.248.234 as permitted sender) receiver=protection.outlook.com; client-ip=40.67.248.234; helo=nebula.arm.com; Received: from nebula.arm.com (40.67.248.234) by DB5EUR03FT060.mail.protection.outlook.com (10.152.21.231) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.3283.16 via Frontend Transport; Wed, 12 Aug 2020 15:22:59 +0000 Received: from AZ-NEU-EX04.Arm.com (10.251.24.32) by AZ-NEU-EX04.Arm.com (10.251.24.32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2044.4; Wed, 12 Aug 2020 15:22:48 +0000 Received: from E107187.Arm.com (10.57.41.222) by mail.arm.com (10.251.24.32) with Microsoft SMTP Server id 15.1.2044.4 via Frontend Transport; Wed, 12 Aug 2020 15:22:47 +0000 From: "Sami Mujawar" To: CC: Sami Mujawar , , , , , , Subject: [PATCH v1 04/30] DynamicTablesPkg: AML node definitions Date: Wed, 12 Aug 2020 16:22:10 +0100 Message-ID: <20200812152236.31164-5-sami.mujawar@arm.com> X-Mailer: git-send-email 2.11.0.windows.3 In-Reply-To: <20200812152236.31164-1-sami.mujawar@arm.com> References: <20200812152236.31164-1-sami.mujawar@arm.com> MIME-Version: 1.0 X-EOPAttributedMessage: 1 X-MS-Office365-Filtering-HT: Tenant X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 29709bb0-17d4-4784-41c1-08d83ed39e19 X-MS-TrafficTypeDiagnostic: DB6PR0802MB2294:|HE1PR0802MB2426: X-Microsoft-Antispam-PRVS: x-checkrecipientrouted: true NoDisclaimer: true X-MS-Oob-TLC-OOBClassifiers: OLM:2150;OLM:2150; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: 2VaEO7Inqgapi92+Wujyp0afL3/88Bb5fYr7xKFDev89e6IZ820hzXGAGFzlUKj9XOZFSGPna4xy4mTXM7ZOeLtjPXtZ3KjqHAPJQm3tTH2po1vlCq2AYu1UgDLoDqM1ueEiXUOhJ66ao60p6koo3iAfXHNnthwP8SnQoZ03xMmW5BC+YlxKU7UlRym9VGzobNfE/x9KVkIlnBBE3YxEdjrG/EDEch17DItxusk4hD8LFp0W4kdmT3Fr2svDm4Y9Lfk3dLFgrKdtKWSKhfzDlSpUh/5t3aafu8PgbFFuZlJwFj9qEnRU9IrfgDNJKvNYyM6cQ1uvVfQbkfVAHZcpjxthuGtp3MuDHIoC1u8V97v13UTzwIToXch8QHvTjPt6MfszalnViSjM/zvp2WOZfw== X-Forefront-Antispam-Report-Untrusted: CIP:40.67.248.234;CTRY:IE;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:nebula.arm.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(4636009)(346002)(376002)(136003)(396003)(39860400002)(46966005)(8676002)(8936002)(5660300002)(70206006)(26005)(19627235002)(54906003)(316002)(478600001)(2906002)(6666004)(70586007)(83380400001)(6916009)(47076004)(82740400003)(36756003)(336012)(426003)(82310400002)(81166007)(356005)(2616005)(1076003)(44832011)(86362001)(186003)(7696005)(4326008)(30864003)(579004)(559001);DIR:OUT;SFP:1101; X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB6PR0802MB2294 Return-Path: Sami.Mujawar@arm.com X-MS-Exchange-Transport-CrossTenantHeadersStripped: VE1EUR03FT005.eop-EUR03.prod.protection.outlook.com X-MS-Office365-Filtering-Correlation-Id-Prvs: 76181e82-16bc-4612-7209-08d83ed39896 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: enz9jfjN4dQJ85Pq6DFqWJrC1dXnhDCGwznesdns/tWImQCBQ89Lwdwkhdi9kfyPhNYAi3Y20RpgLvYqZn5mhJQIXBwRYi0z8FcxNF5wCtxEkxGDloojvWwOrhWzz3nFcETqkiaiVNsYleq5g+xJCXMwlZ4Ykg5gW+yf1B9rkLmUblvWTwFYjigm0ojTquliVMFw+qy8TiLS6MXhnvtOZC6w2OozPF3xAC8LfI1JcESlb3ECbTAi36LpBzK+E1Xiss8G/Teqy9JyQBQxQqLP/QBECA5J3ma2JTMC2eoA/aJm+GITdEyY87XGegPlBOC8SQKPa1K3snw17PDl7B0mtw5w6v1AfvB0gkO1tUnBl3LR2wCLe/qIMQ7crLLreiBjMwH2oj3AsbGRqavzBS6iJg== X-Forefront-Antispam-Report: CIP:63.35.35.123;CTRY:IE;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:64aa7808-outbound-1.mta.getcheckrecipient.com;PTR:ec2-63-35-35-123.eu-west-1.compute.amazonaws.com;CAT:NONE;SFS:(4636009)(346002)(136003)(376002)(39860400002)(396003)(46966005)(30864003)(70206006)(6916009)(5660300002)(36756003)(7696005)(186003)(54906003)(316002)(70586007)(336012)(4326008)(86362001)(26005)(36906005)(6666004)(1076003)(44832011)(8676002)(2906002)(47076004)(82740400003)(8936002)(19627235002)(426003)(2616005)(478600001)(82310400002)(81166007)(83380400001)(559001)(579004);DIR:OUT;SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Aug 2020 15:23:08.3284 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 29709bb0-17d4-4784-41c1-08d83ed39e19 X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d;Ip=[63.35.35.123];Helo=[64aa7808-outbound-1.mta.getcheckrecipient.com] X-MS-Exchange-CrossTenant-AuthSource: VE1EUR03FT005.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR0802MB2426 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: Pierre Gondois AML has a complex grammar, and this makes runtime modifications on an AML byte stream difficult. A solution is to parse the AML bytecode and represent it in a tree data structure, henceforth called the AML tree. The AML tree is composite in the sense it has the following node types: - A 'Root node' that represents the root of the AML tree. - An 'Object node' that contains the OP Code (AML Encoding). - A 'Data node' that contains a data buffer. The Root node contains the Definition block header (ACPI header) and a Variable Argument list. The Object node is composed of an array of Fixed Arguments and a Variable Argument list. Fixed arguments can be either Object Nodes or Data nodes. Their placement (index) in the Fixed Argument array is defined by the AML encoding of the enclosing Object Node. Variable arguments can be Object nodes or Data nodes. Following is a depiction of a typical AML tree: (/) # Root Node \ |-{(N1)->...} # Variable Argument list, N1 is \ # an Object Node \ /-i # Child of fixed argument b \ / |- [a][b][c][d] # Fixed Arguments |- {(e)->(f)->(g)} # Variable Arguments \ \-h # Child of variable argument e Signed-off-by: Pierre Gondois Signed-off-by: Sami Mujawar --- DynamicTablesPkg/Library/Common/AmlLib/AmlNodeDefines.h | 183 +++++= + DynamicTablesPkg/Library/Common/AmlLib/Tree/AmlNode.c | 673 +++++= +++++++++++++++ DynamicTablesPkg/Library/Common/AmlLib/Tree/AmlNode.h | 212 +++++= + DynamicTablesPkg/Library/Common/AmlLib/Tree/AmlNodeInterface.c | 566 +++++= +++++++++++ 4 files changed, 1634 insertions(+) diff --git a/DynamicTablesPkg/Library/Common/AmlLib/AmlNodeDefines.h b/Dyna= micTablesPkg/Library/Common/AmlLib/AmlNodeDefines.h new file mode 100644 index 0000000000000000000000000000000000000000..fffba6d54b64e8f37ca33d99465= ec22eb81b5a99 --- /dev/null +++ b/DynamicTablesPkg/Library/Common/AmlLib/AmlNodeDefines.h @@ -0,0 +1,183 @@ +/** @file + AML Node Definition. + + Copyright (c) 2020, Arm Limited. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#ifndef AML_NODE_DEFINES_H_ +#define AML_NODE_DEFINES_H_ + +#include +#include + +/** AML header node. + + This abstract class represents either a root/object/data node. + All the enumerated nodes have this same common header. +*/ +typedef struct AmlNodeHeader { + /// This must be the first field in this structure. + LIST_ENTRY Link; + + /// Parent of this node. NULL for the root node. + struct AmlNodeHeader * Parent; + + /// Node type allowing to identify a root/object/data node. + EAML_NODE_TYPE NodeType; +} AML_NODE_HEADER; + +/** Node handle. +*/ +typedef AML_NODE_HEADER* AML_NODE_HANDLE; + +/** AML root node. + + The root node is unique and at the head of of tree. It is a fake node us= ed + to maintain the list of AML statements (stored as object nodes) which ar= e + at the first scope level. +*/ +typedef struct AmlRootNode { + /// Header information. Must be the first field of the struct. + AML_NODE_HEADER NodeHeader; + + /// List of object nodes being at the first scope level. + /// These are children and can only be object nodes. + LIST_ENTRY VariableArgs; + + /// ACPI DSDT/SSDT header. + EFI_ACPI_DESCRIPTION_HEADER * SdtHeader; +} AML_ROOT_NODE; + +/** Root Node handle. +*/ +typedef AML_ROOT_NODE* AML_ROOT_NODE_HANDLE; + +/** AML object node. + + Object nodes match AML statements. They are associated with an + OpCode/SubOpCode, and can have children. +*/ +typedef struct AmlObjectNode { + /// Header information. Must be the first field of the struct. + AML_NODE_HEADER NodeHeader; + + /// Some object nodes have a variable list of arguments. + /// These are children and can only be object/data nodes. + /// Cf ACPI specification, s20.3. + LIST_ENTRY VariableArgs; + + /// Fixed arguments of this object node. + /// These are children and can be object/data nodes. + /// Cf ACPI specification, s20.3. + AML_NODE_HEADER * FixedArgs[EAmlParseIndexMax]; + + /// AML byte encoding. Stores the encoding information: + /// (OpCode/SubOpCode/number of fixed arguments/ attributes). + CONST AML_BYTE_ENCODING * AmlByteEncoding; + + /// Some nodes have a PkgLen following their OpCode/SubOpCode in the + /// AML bytestream. This field stores the decoded value of the PkgLen. + UINT32 PkgLen; +} AML_OBJECT_NODE; + +/** Object Node handle. +*/ +typedef AML_OBJECT_NODE* AML_OBJECT_NODE_HANDLE; + +/** AML data node. + + Data nodes store the smallest pieces of information. + E.g.: UINT8, UINT64, NULL terminated string, etc. + Data node don't have children nodes. +*/ +typedef struct AmlDataNode { + /// Header information. Must be the first field of the struct. + AML_NODE_HEADER NodeHeader; + + /// Tag identifying what data is stored in this node. + /// E.g. UINT, NULL terminated string, resource data element, etc. + EAML_NODE_DATA_TYPE DataType; + + /// Buffer containing the data stored by this node. + UINT8 * Buffer; + + /// Size of the Buffer. + UINT32 Size; +} AML_DATA_NODE; + +/** Data Node handle. +*/ +typedef AML_DATA_NODE* AML_DATA_NODE_HANDLE; + +/** Check whether a Node has a valid NodeType. + + @param [in] Node The node to check. + + @retval TRUE The Node has a valid NodeType. + @retval FALSE Otherwise. +*/ +#define IS_AML_NODE_VALID(Node) = \ + ((Node !=3D NULL) = && \ + ((((CONST AML_NODE_HEADER*)Node)->NodeType > EAmlNodeUnknown) = || \ + (((CONST AML_NODE_HEADER*)Node)->NodeType < EAmlNodeMax))) + +/** Check whether a Node is a root node. + + @param [in] Node The node to check. + + @retval TRUE The Node is a root node. + @retval FALSE Otherwise. +*/ +#define IS_AML_ROOT_NODE(Node) = \ + ((Node !=3D NULL) = && \ + (((CONST AML_NODE_HEADER*)Node)->NodeType =3D=3D EAmlNodeRoot)) + +/** Check whether a Node is an object node. + + @param [in] Node The node to check. + + @retval TRUE The Node is an object node. + @retval FALSE Otherwise. +*/ +#define IS_AML_OBJECT_NODE(Node) = \ + ((Node !=3D NULL) = && \ + (((CONST AML_NODE_HEADER*)Node)->NodeType =3D=3D EAmlNodeObject= )) + +/** Check whether a Node is a data node. + + @param [in] Node The node to check. + + @retval TRUE The Node is a data node. + @retval FALSE Otherwise. +*/ +#define IS_AML_DATA_NODE(Node) = \ + ((Node !=3D NULL) = && \ + (((CONST AML_NODE_HEADER*)Node)->NodeType =3D=3D EAmlNodeData)) + +/** Check whether a Node has a parent. + + @param [in] Node The node to check. + + @retval TRUE The Node is a data node. + @retval FALSE Otherwise. +*/ +#define AML_NODE_HAS_PARENT(Node) = \ + (IS_AML_NODE_VALID (Node) = && \ + (((CONST AML_NODE_HEADER*)Node)->Parent !=3D NULL)) + +/** Check that the Node is not attached somewhere. + This doesn't mean the node cannot have children. + + @param [in] Node The node to check. + + @retval TRUE The Node has been detached. + @retval FALSE Otherwise. +*/ +#define AML_NODE_IS_DETACHED(Node) = \ + (IS_AML_NODE_VALID (Node) = && \ + IsListEmpty ((CONST LIST_ENTRY*)Node) = && \ + (((CONST AML_NODE_HEADER*)Node)->Parent =3D=3D NULL)) + +#endif // AML_NODE_DEFINES_H_ diff --git a/DynamicTablesPkg/Library/Common/AmlLib/Tree/AmlNode.c b/Dynami= cTablesPkg/Library/Common/AmlLib/Tree/AmlNode.c new file mode 100644 index 0000000000000000000000000000000000000000..2c80440a447ad261290a2cf84a8= edd3434cf9e5e --- /dev/null +++ b/DynamicTablesPkg/Library/Common/AmlLib/Tree/AmlNode.c @@ -0,0 +1,673 @@ +/** @file + AML Node. + + Copyright (c) 2019 - 2020, Arm Limited. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#include + +#include +#include + +/** Initialize an AML_NODE_HEADER structure. + + @param [in] Node Pointer to a node header. + @param [in] NodeType NodeType to initialize the Node with. + Must be an EAML_NODE_TYPE. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. +**/ +STATIC +EFI_STATUS +EFIAPI +AmlInitializeNodeHeader ( + IN AML_NODE_HEADER * Node, + IN EAML_NODE_TYPE NodeType + ) +{ + if (Node =3D=3D NULL) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + InitializeListHead (&Node->Link); + + Node->Parent =3D NULL; + Node->NodeType =3D NodeType; + + return EFI_SUCCESS; +} + +/** Delete a root node and its ACPI DSDT/SSDT header. + + It is the caller's responsibility to check the RootNode has been removed + from the tree and is not referencing any other node in the tree. + + @param [in] RootNode Pointer to a root node. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. +**/ +STATIC +EFI_STATUS +EFIAPI +AmlDeleteRootNode ( + IN AML_ROOT_NODE * RootNode + ) +{ + if (!IS_AML_ROOT_NODE (RootNode)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + if ((RootNode->SdtHeader !=3D NULL)) { + FreePool (RootNode->SdtHeader); + } else { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + FreePool (RootNode); + return EFI_SUCCESS; +} + +/** Create an AML_ROOT_NODE. + This node will be the root of the tree. + + @param [in] SdtHeader Pointer to an ACPI DSDT/SSDT header to cop= y + the data from. + @param [out] NewRootNodePtr The created AML_ROOT_NODE. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval EFI_OUT_OF_RESOURCES Could not allocate memory. +**/ +EFI_STATUS +EFIAPI +AmlCreateRootNode ( + IN CONST EFI_ACPI_DESCRIPTION_HEADER * SdtHeader, + OUT AML_ROOT_NODE ** NewRootNodePtr + ) +{ + EFI_STATUS Status; + AML_ROOT_NODE * RootNode; + + if ((SdtHeader =3D=3D NULL) || + (NewRootNodePtr =3D=3D NULL)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + RootNode =3D AllocateZeroPool (sizeof (AML_ROOT_NODE)); + if (RootNode =3D=3D NULL) { + ASSERT (0); + return EFI_OUT_OF_RESOURCES; + } + + Status =3D AmlInitializeNodeHeader (&RootNode->NodeHeader, EAmlNodeRoot)= ; + if (EFI_ERROR (Status)) { + FreePool (RootNode); + ASSERT (0); + return Status; + } + + InitializeListHead (&RootNode->VariableArgs); + + RootNode->SdtHeader =3D AllocateCopyPool ( + sizeof (EFI_ACPI_DESCRIPTION_HEADER), + SdtHeader + ); + if (RootNode->SdtHeader =3D=3D NULL) { + ASSERT (0); + AmlDeleteRootNode (RootNode); + return EFI_OUT_OF_RESOURCES; + } + + *NewRootNodePtr =3D RootNode; + + return EFI_SUCCESS; +} + +/** Delete an object node. + + It is the caller's responsibility to check the ObjectNode has been remov= ed + from the tree and is not referencing any other node in the tree. + + @param [in] ObjectNode Pointer to an object node. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. +**/ +STATIC +EFI_STATUS +EFIAPI +AmlDeleteObjectNode ( + IN AML_OBJECT_NODE * ObjectNode + ) +{ + if (!IS_AML_OBJECT_NODE (ObjectNode)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + FreePool (ObjectNode); + return EFI_SUCCESS; +} + +/** Create an AML_OBJECT_NODE. + + @param [in] AmlByteEncoding Byte encoding entry. + @param [in] PkgLength PkgLength of the node if the AmlByteEnco= ding + has the PkgLen attribute. + 0 otherwise. + @param [out] NewObjectNodePtr The created AML_OBJECT_NODE. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval EFI_OUT_OF_RESOURCES Could not allocate memory. +**/ +EFI_STATUS +EFIAPI +AmlCreateObjectNode ( + IN CONST AML_BYTE_ENCODING * AmlByteEncoding, + IN UINT32 PkgLength, + OUT AML_OBJECT_NODE ** NewObjectNodePtr + ) +{ + EFI_STATUS Status; + AML_OBJECT_NODE * ObjectNode; + + if ((AmlByteEncoding =3D=3D NULL) || + (NewObjectNodePtr =3D=3D NULL)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + ObjectNode =3D AllocateZeroPool (sizeof (AML_OBJECT_NODE)); + if (ObjectNode =3D=3D NULL) { + ASSERT (0); + return EFI_OUT_OF_RESOURCES; + } + + Status =3D AmlInitializeNodeHeader (&ObjectNode->NodeHeader, EAmlNodeObj= ect); + if (EFI_ERROR (Status)) { + FreePool (ObjectNode); + ASSERT (0); + return Status; + } + + InitializeListHead (&ObjectNode->VariableArgs); + + // ObjectNode->FixedArgs[...] is already initialised to NULL as the + // ObjectNode is Zero allocated. + ObjectNode->AmlByteEncoding =3D AmlByteEncoding; + ObjectNode->PkgLen =3D PkgLength; + + *NewObjectNodePtr =3D ObjectNode; + + return EFI_SUCCESS; +} + +/** Delete a data node and its buffer. + + It is the caller's responsibility to check the DataNode has been removed + from the tree and is not referencing any other node in the tree. + + @param [in] DataNode Pointer to a data node. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval EFI_OUT_OF_RESOURCES Could not allocate memory. +**/ +STATIC +EFI_STATUS +EFIAPI +AmlDeleteDataNode ( + IN AML_DATA_NODE * DataNode + ) +{ + if (!IS_AML_DATA_NODE (DataNode)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + if (DataNode->Buffer !=3D NULL) { + FreePool (DataNode->Buffer); + } else { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + FreePool (DataNode); + return EFI_SUCCESS; +} + +/** Create an AML_DATA_NODE. + + @param [in] DataType DataType of the node. + @param [in] Data Pointer to the AML bytecode corresponding = to + this node. Data is copied from there. + @param [in] DataSize Number of bytes to consider at the address + pointed by Data. + @param [out] NewDataNodePtr The created AML_DATA_NODE. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval EFI_OUT_OF_RESOURCES Could not allocate memory. +**/ +EFI_STATUS +EFIAPI +AmlCreateDataNode ( + IN EAML_NODE_DATA_TYPE DataType, + IN CONST UINT8 * Data, + IN UINT32 DataSize, + OUT AML_DATA_NODE ** NewDataNodePtr + ) +{ + EFI_STATUS Status; + AML_DATA_NODE * DataNode; + + // A data node must not be created for certain data types. + if ((DataType =3D=3D EAmlNodeDataTypeNone) || + (DataType =3D=3D EAmlNodeDataTypeReserved1) || + (DataType =3D=3D EAmlNodeDataTypeReserved2) || + (DataType =3D=3D EAmlNodeDataTypeReserved3) || + (DataType =3D=3D EAmlNodeDataTypeReserved4) || + (DataType =3D=3D EAmlNodeDataTypeReserved5) || + (Data =3D=3D NULL) || + (DataSize =3D=3D 0) || + (NewDataNodePtr =3D=3D NULL)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + DataNode =3D AllocateZeroPool (sizeof (AML_DATA_NODE)); + if (DataNode =3D=3D NULL) { + ASSERT (0); + return EFI_OUT_OF_RESOURCES; + } + + Status =3D AmlInitializeNodeHeader (&DataNode->NodeHeader, EAmlNodeData)= ; + if (EFI_ERROR (Status)) { + FreePool (DataNode); + ASSERT (0); + return Status; + } + + DataNode->Buffer =3D AllocateCopyPool (DataSize, Data); + if (DataNode->Buffer =3D=3D NULL) { + AmlDeleteDataNode (DataNode); + ASSERT (0); + return EFI_OUT_OF_RESOURCES; + } + + DataNode->DataType =3D DataType; + DataNode->Size =3D DataSize; + + *NewDataNodePtr =3D DataNode; + + return EFI_SUCCESS; +} + +/** Delete a Node. + + @param [in] Node Pointer to a Node. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. +**/ +EFI_STATUS +EFIAPI +AmlDeleteNode ( + IN AML_NODE_HEADER * Node + ) +{ + EFI_STATUS Status; + EAML_PARSE_INDEX Index; + + // Check that the node being deleted is unlinked. + // When removing the node, its parent and list are reset + // with InitializeListHead. Thus it must be empty. + if (!IS_AML_NODE_VALID (Node) || + !AML_NODE_IS_DETACHED (Node)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + switch (Node->NodeType) { + case EAmlNodeRoot: + { + // Check the variable list of arguments has been cleaned. + if (!IsListEmpty (AmlNodeGetVariableArgList (Node))) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + Status =3D AmlDeleteRootNode ((AML_ROOT_NODE*)Node); + if (EFI_ERROR (Status)) { + ASSERT (0); + } + break; + } + + case EAmlNodeObject: + { + // Check the variable list of arguments has been cleaned. + if (!IsListEmpty (AmlNodeGetVariableArgList (Node))) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + // Check the fixed argument list has been cleaned. + for (Index =3D EAmlParseIndexTerm0; Index < EAmlParseIndexMax; Index= ++) { + if (((AML_OBJECT_NODE*)Node)->FixedArgs[Index] !=3D NULL) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + } + + Status =3D AmlDeleteObjectNode ((AML_OBJECT_NODE*)Node); + if (EFI_ERROR (Status)) { + ASSERT (0); + } + break; + } + + case EAmlNodeData: + { + Status =3D AmlDeleteDataNode ((AML_DATA_NODE*)Node); + if (EFI_ERROR (Status)) { + ASSERT (0); + } + break; + } + + default: + { + ASSERT (0); + Status =3D EFI_INVALID_PARAMETER; + break; + } + } // switch + + return Status; +} + +/** Check whether ObjectNode has the input attribute. + This function can be used to check ObjectNode is an object node + at the same time. + + @param [in] ObjectNode Pointer to an object node. + @param [in] Attribute Attribute to check for. + + @retval TRUE The node is an AML object and the attribute is present. + @retval FALSE Otherwise. +**/ +BOOLEAN +EFIAPI +AmlNodeHasAttribute ( + IN CONST AML_OBJECT_NODE * ObjectNode, + IN AML_OP_ATTRIBUTE Attribute + ) +{ + if (!IS_AML_OBJECT_NODE (ObjectNode) || + (ObjectNode->AmlByteEncoding =3D=3D NULL)) { + return FALSE; + } + + return ((ObjectNode->AmlByteEncoding->Attribute & + Attribute) =3D=3D 0 ? FALSE : TRUE); +} + +/** Check whether ObjectNode has the input OpCode/SubOpcode couple. + + @param [in] ObjectNode Pointer to an object node. + @param [in] OpCode OpCode to check + @param [in] SubOpCode SubOpCode to check + + @retval TRUE The node is an AML object and + the Opcode and the SubOpCode match. + @retval FALSE Otherwise. +**/ +BOOLEAN +EFIAPI +AmlNodeCompareOpCode ( + IN CONST AML_OBJECT_NODE * ObjectNode, + IN UINT8 OpCode, + IN UINT8 SubOpCode + ) +{ + if (!IS_AML_OBJECT_NODE (ObjectNode) || + (ObjectNode->AmlByteEncoding =3D=3D NULL)) { + return FALSE; + } + + ASSERT (AmlIsOpCodeValid (OpCode, SubOpCode)); + + return ((ObjectNode->AmlByteEncoding->OpCode =3D=3D OpCode) && + (ObjectNode->AmlByteEncoding->SubOpCode =3D=3D SubOpCode)) ? + TRUE : FALSE; +} + +/** Check whether a Node is an integer node. + + By integer node we mean an object node having one of the following opcod= e: + - AML_BYTE_PREFIX; + - AML_WORD_PREFIX; + - AML_DWORD_PREFIX; + - AML_QWORD_PREFIX. + + @param [in] Node The node to check. + + @retval TRUE The Node is an integer node. + @retval FALSE Otherwise. +*/ +BOOLEAN +EFIAPI +IsIntegerNode ( + IN AML_OBJECT_NODE * Node + ) +{ + UINT8 OpCode; + + if (!IS_AML_OBJECT_NODE (Node) || + (Node->AmlByteEncoding =3D=3D NULL)) { + return FALSE; + } + + // Check Node is an integer node. + OpCode =3D Node->AmlByteEncoding->OpCode; + if ((OpCode !=3D AML_BYTE_PREFIX) && + (OpCode !=3D AML_WORD_PREFIX) && + (OpCode !=3D AML_DWORD_PREFIX) && + (OpCode !=3D AML_QWORD_PREFIX)) { + return FALSE; + } + + return TRUE; +} + +/** Check whether a Node is a ZeroOp, a OneOp or a OnesOp. + + These two objects don't have a data node holding + a value. This require special handling. + + @param [in] Node The node to check. + + @retval TRUE The Node is a ZeroOp or OneOp. + @retval FALSE Otherwise. +*/ +BOOLEAN +EFIAPI +IsSpecialIntegerNode ( + IN AML_OBJECT_NODE * Node + ) +{ + UINT8 OpCode; + + if (!IS_AML_OBJECT_NODE (Node) || + (Node->AmlByteEncoding =3D=3D NULL)) { + return FALSE; + } + + OpCode =3D Node->AmlByteEncoding->OpCode; + + if ((OpCode !=3D AML_ZERO_OP) && + (OpCode !=3D AML_ONE_OP) && + (OpCode !=3D AML_ONES_OP)) { + return FALSE; + } + + return TRUE; +} + +/** Check whether Node corresponds to a method definition. + + A method definition can be introduced: + - By a method object, having an AML_METHOD_OP OpCode; + - By an external definition of a method, having an AML_EXTERNAL_OP OpCo= de + and an ObjectType byte set to the MethodObj. + + Note: + An alias node, having an AML_ALIAS_OP, can be resolved to a method + definition. This function doesn't handle this case. + + @param [in] Node Node to check whether it is a method definition. + + @retval TRUE The Node is a method definition. + @retval FALSE Otherwise. +**/ +BOOLEAN +EFIAPI +AmlIsMethodDefinitionNode ( + IN CONST AML_OBJECT_NODE * Node + ) +{ + AML_DATA_NODE * ObjectType; + + // Node is checked to be an object node aswell. + if (AmlNodeCompareOpCode (Node, AML_METHOD_OP, 0)) { + return TRUE; + } else if (AmlNodeCompareOpCode (Node, AML_EXTERNAL_OP, 0)) { + // If the node is an external definition, check this is a method. + // DefExternal :=3D ExternalOp NameString ObjectType ArgumentCount + // ExternalOp :=3D 0x15 + // ObjectType :=3D ByteData + // ArgumentCount :=3D ByteData (0 =E2=80=93 7) + ObjectType =3D (AML_DATA_NODE*)AmlGetFixedArgument ( + (AML_OBJECT_NODE*)Node, + EAmlParseIndexTerm1 + ); + if (IS_AML_DATA_NODE (ObjectType) && + (ObjectType->DataType =3D=3D EAmlNodeDataTypeUInt) && + ((ObjectType->Size =3D=3D 1))) { + if (*((UINT8*)ObjectType->Buffer) =3D=3D (UINT8)EAmlObjTypeMethodObj= ) { + // The external definition is a method. + return TRUE; + } else { + // The external definition is not a method. + return FALSE; + } + } else { + // The tree is inconsistent. + ASSERT (0); + return FALSE; + } + } + + // This is not a method definition. + return FALSE; +} + +/** Get the index at which the name of the node is stored. + + @param [in] ObjectNode Pointer to an object node. + Must have the AML_IN_NAMESPACE attribute. + @param [out] Index Index of the name in the fixed list of argumen= ts. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. +**/ +EFI_STATUS +AmlNodeGetNameIndex ( + IN CONST AML_OBJECT_NODE * ObjectNode, + OUT EAML_PARSE_INDEX * Index + ) +{ + EAML_PARSE_INDEX NameIndex; + + if (!AmlNodeHasAttribute (ObjectNode, AML_IN_NAMESPACE) || + (ObjectNode->AmlByteEncoding =3D=3D NULL) || + (Index =3D=3D NULL)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + NameIndex =3D ObjectNode->AmlByteEncoding->NameIndex; + + if ((NameIndex > ObjectNode->AmlByteEncoding->MaxIndex) || + (ObjectNode->AmlByteEncoding->Format[NameIndex] !=3D EAmlName)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + *Index =3D NameIndex; + + return EFI_SUCCESS; +} + +/** Get the name of the Node. + + Node must be part of the namespace. + + @param [in] ObjectNode Pointer to an object node, + which is part of the namespace. + + @return A pointer to the name. + NULL otherwise. + Return NULL for the root node. +**/ +CHAR8 * +EFIAPI +AmlNodeGetName ( + IN CONST AML_OBJECT_NODE * ObjectNode + ) +{ + EFI_STATUS Status; + EAML_PARSE_INDEX NameIndex; + AML_DATA_NODE * DataNode; + + if (!AmlNodeHasAttribute (ObjectNode, AML_IN_NAMESPACE)) { + ASSERT (0); + return NULL; + } + + // Get the index at which the name is stored in the fixed arguments list= . + Status =3D AmlNodeGetNameIndex (ObjectNode, &NameIndex); + if (EFI_ERROR (Status)) { + ASSERT (0); + return NULL; + } + + // The name is stored in a Data node. + DataNode =3D (AML_DATA_NODE*)ObjectNode->FixedArgs[NameIndex]; + if (IS_AML_DATA_NODE (DataNode) && + (DataNode->DataType =3D=3D EAmlNodeDataTypeNameString)) { + return (CHAR8*)DataNode->Buffer; + } + + /* Return NULL if no name is found. + This can occur if the name of a node is defined as a further + fixed argument. + E.g.: CreateField (BD03, 0x28, Add (ID03 + 0x08), BF33) + ^ + The parser is here. + The parent of the Add statement is the CreateField statement. This + statement defines a name in the AML namespace. This name defined as + the fourth fixed argument. It hasn't been parsed yet. + */ + return NULL; +} diff --git a/DynamicTablesPkg/Library/Common/AmlLib/Tree/AmlNode.h b/Dynami= cTablesPkg/Library/Common/AmlLib/Tree/AmlNode.h new file mode 100644 index 0000000000000000000000000000000000000000..3584b572baae5d48f5016c0b889= f557c5ba94d07 --- /dev/null +++ b/DynamicTablesPkg/Library/Common/AmlLib/Tree/AmlNode.h @@ -0,0 +1,212 @@ +/** @file + AML Node. + + Copyright (c) 2019 - 2020, Arm Limited. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#ifndef AML_NODE_H_ +#define AML_NODE_H_ + +#include +#include + +/** Create an AML_ROOT_NODE. + This node will be the root of the tree. + + @param [in] SdtHeader Pointer to an ACPI DSDT/SSDT header to cop= y + the data from. + @param [out] NewRootNodePtr The created AML_ROOT_NODE. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval EFI_OUT_OF_RESOURCES Could not allocate memory. +**/ +EFI_STATUS +EFIAPI +AmlCreateRootNode ( + IN CONST EFI_ACPI_DESCRIPTION_HEADER * SdtHeader, + OUT AML_ROOT_NODE ** NewRootNodePtr + ); + +/** Create an AML_OBJECT_NODE. + + @param [in] AmlByteEncoding Byte encoding entry. + @param [in] PkgLength PkgLength of the node if the AmlByteEnco= ding + has the PkgLen attribute. + 0 otherwise. + @param [out] NewObjectNodePtr The created AML_OBJECT_NODE. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval EFI_OUT_OF_RESOURCES Could not allocate memory. +**/ +EFI_STATUS +EFIAPI +AmlCreateObjectNode ( + IN CONST AML_BYTE_ENCODING * AmlByteEncoding, + IN UINT32 PkgLength, + OUT AML_OBJECT_NODE ** NewObjectNodePtr + ); + +/** Create an AML_DATA_NODE. + + @param [in] DataType DataType of the node. + @param [in] Data Pointer to the AML bytecode corresponding = to + this node. Data is copied from there. + @param [in] DataSize Number of bytes to consider at the address + pointed by Data. + @param [out] NewDataNodePtr The created AML_DATA_NODE. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval EFI_OUT_OF_RESOURCES Could not allocate memory. +**/ +EFI_STATUS +EFIAPI +AmlCreateDataNode ( + IN EAML_NODE_DATA_TYPE DataType, + IN CONST UINT8 * Data, + IN UINT32 DataSize, + OUT AML_DATA_NODE ** NewDataNodePtr + ); + +/** Delete a Node. + + @param [in] Node Pointer to a Node. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. +**/ +EFI_STATUS +EFIAPI +AmlDeleteNode ( + IN AML_NODE_HEADER * Node + ); + +/** Check whether ObjectNode has the input attribute. + This function can be used to check ObjectNode is an object node + at the same time. + + @param [in] ObjectNode Pointer to an object node. + @param [in] Attribute Attribute to check for. + + @retval TRUE The node is an AML object and the attribute is present. + @retval FALSE Otherwise. +**/ +BOOLEAN +EFIAPI +AmlNodeHasAttribute ( + IN CONST AML_OBJECT_NODE * ObjectNode, + IN AML_OP_ATTRIBUTE Attribute + ); + +/** Check whether ObjectNode has the input OpCode/SubOpcode couple. + + @param [in] ObjectNode Pointer to an object node. + @param [in] OpCode OpCode to check + @param [in] SubOpCode SubOpCode to check + + @retval TRUE The node is an AML object and + the Opcode and the SubOpCode match. + @retval FALSE Otherwise. +**/ +BOOLEAN +EFIAPI +AmlNodeCompareOpCode ( + IN CONST AML_OBJECT_NODE * ObjectNode, + IN UINT8 OpCode, + IN UINT8 SubOpCode + ); + +/** Check whether a Node is an integer node. + + By integer node we mean an object node having one of the following opcod= e: + - AML_BYTE_PREFIX; + - AML_WORD_PREFIX; + - AML_DWORD_PREFIX; + - AML_QWORD_PREFIX. + + @param [in] Node The node to check. + + @retval TRUE The Node is an integer node. + @retval FALSE Otherwise. +*/ +BOOLEAN +EFIAPI +IsIntegerNode ( + IN AML_OBJECT_NODE * Node + ); + +/** Check whether a Node is a ZeroOp, a OneOp or a OnesOp. + + These two objects don't have a data node holding + a value. This require special handling. + + @param [in] Node The node to check. + + @retval TRUE The Node is a ZeroOp or OneOp. + @retval FALSE Otherwise. +*/ +BOOLEAN +EFIAPI +IsSpecialIntegerNode ( + IN AML_OBJECT_NODE * Node + ); + +/** Check whether Node corresponds to a method definition. + + A method definition can be introduced: + - By a method object, having an AML_METHOD_OP OpCode; + - By an external definition of a method, having an AML_EXTERNAL_OP OpCo= de + and an ObjectType byte set to the MethodObj. + + Note: + An alias node, having an AML_ALIAS_OP, can be resolved to a method + definition. This function doesn't handle this case. + + @param [in] Node Node to check whether it is a method definition. + + @retval TRUE The Node is a method definition. + @retval FALSE Otherwise. +**/ +BOOLEAN +EFIAPI +AmlIsMethodDefinitionNode ( + IN CONST AML_OBJECT_NODE * Node + ); + +/** Get the index at which the name of the node is stored. + + @param [in] ObjectNode Pointer to an object node. + Must have the AML_IN_NAMESPACE attribute. + @param [out] Index Index of the name in the fixed list of argumen= ts. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. +**/ +EFI_STATUS +AmlNodeGetNameIndex ( + IN CONST AML_OBJECT_NODE * ObjectNode, + OUT EAML_PARSE_INDEX * Index + ); + +/** Get the name of the Node. + + Node must be part of the namespace. + + @param [in] ObjectNode Pointer to an object node, + which is part of the namespace. + + @return A pointer to the name. + NULL otherwise. + Return NULL for the root node. +**/ +CHAR8 * +EFIAPI +AmlNodeGetName ( + IN CONST AML_OBJECT_NODE * ObjectNode + ); + +#endif // AML_NODE_H_ diff --git a/DynamicTablesPkg/Library/Common/AmlLib/Tree/AmlNodeInterface.c= b/DynamicTablesPkg/Library/Common/AmlLib/Tree/AmlNodeInterface.c new file mode 100644 index 0000000000000000000000000000000000000000..870346c40a5d9df44d01953035c= 760671817b960 --- /dev/null +++ b/DynamicTablesPkg/Library/Common/AmlLib/Tree/AmlNodeInterface.c @@ -0,0 +1,566 @@ +/** @file + AML Node Interface. + + Copyright (c) 2019 - 2020, Arm Limited. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#include + +#include +#include +#include +#include +#include +#include + +/** Returns the tree node type (Root/Object/Data). + + @param [in] Node Pointer to a Node. + + @return The node type. + EAmlNodeUnknown if invalid parameter. +**/ +EAML_NODE_TYPE +EFIAPI +AmlGetNodeType ( + IN AML_NODE_HEADER * Node + ) +{ + if (!IS_AML_NODE_VALID (Node)) { + ASSERT (0); + return EAmlNodeUnknown; + } + + return Node->NodeType; +} + +/** Get the RootNode information. + The Node must be a root node. + + @param [in] RootNode Pointer to a root node. + @param [out] SdtHeaderBuffer Buffer to copy the ACPI DSDT/SSDT header= to. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. +**/ +EFI_STATUS +EFIAPI +AmlGetRootNodeInfo ( + IN AML_ROOT_NODE * RootNode, + OUT EFI_ACPI_DESCRIPTION_HEADER * SdtHeaderBuffer + ) +{ + if (!IS_AML_ROOT_NODE (RootNode) || + (SdtHeaderBuffer =3D=3D NULL)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + CopyMem ( + SdtHeaderBuffer, + RootNode->SdtHeader, + sizeof (EFI_ACPI_DESCRIPTION_HEADER) + ); + + return EFI_SUCCESS; +} + +/** Get the ObjectNode information. + The Node must be an object node. + + @ingroup NodeInterfaceApi + + @param [in] ObjectNode Pointer to an object node. + @param [out] OpCode Pointer holding the OpCode. + Optional, can be NULL. + @param [out] SubOpCode Pointer holding the SubOpCode. + Optional, can be NULL. + @param [out] PkgLen Pointer holding the PkgLen. + The PkgLen is 0 for nodes + not having the Pkglen attribute. + Optional, can be NULL. + @param [out] IsNameSpaceNode Pointer holding TRUE if the node is defi= ning + or changing the NameSpace scope. + E.g.: The "Name ()" and "Scope ()" ASL + statements add/modify the NameSpace scop= e. + Their corresponding node are NameSpace n= odes. + Optional, can be NULL. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. +**/ +EFI_STATUS +EFIAPI +AmlGetObjectNodeInfo ( + IN AML_OBJECT_NODE * ObjectNode, + OUT UINT8 * OpCode, OPTIONAL + OUT UINT8 * SubOpCode, OPTIONAL + OUT UINT32 * PkgLen, OPTIONAL + OUT BOOLEAN * IsNameSpaceNode OPTIONAL + ) +{ + if (!IS_AML_OBJECT_NODE (ObjectNode)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + if (OpCode !=3D NULL) { + *OpCode =3D ObjectNode->AmlByteEncoding->OpCode; + } + if (SubOpCode !=3D NULL) { + *SubOpCode =3D ObjectNode->AmlByteEncoding->SubOpCode; + } + if (PkgLen !=3D NULL) { + *PkgLen =3D ObjectNode->PkgLen; + } + if (IsNameSpaceNode !=3D NULL) { + *IsNameSpaceNode =3D AmlNodeHasAttribute (ObjectNode, AML_IN_NAMESPACE= ); + } + + return EFI_SUCCESS; +} + +/** Returns the count of the fixed arguments for the input Node. + + @param [in] Node Pointer to an object node. + + @return Number of fixed arguments of the object node. + Return 0 if the node is not an object node. +**/ +UINT8 +AmlGetFixedArgumentCount ( + IN AML_OBJECT_NODE * Node + ) +{ + if (IS_AML_OBJECT_NODE (Node) && + (Node->AmlByteEncoding !=3D NULL)) { + return (UINT8)Node->AmlByteEncoding->MaxIndex; + } + + return 0; +} + +/** Get the data type of the DataNode. + The Node must be a data node. + + @param [in] DataNode Pointer to a data node. + @param [out] DataType Pointer holding the data type of the data buffer= . + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. +**/ +EFI_STATUS +EFIAPI +AmlGetNodeDataType ( + IN AML_DATA_NODE * DataNode, + OUT EAML_NODE_DATA_TYPE * DataType + ) +{ + if (!IS_AML_DATA_NODE (DataNode) || + (DataType =3D=3D NULL)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + *DataType =3D DataNode->DataType; + + return EFI_SUCCESS; +} + +/** Get the descriptor Id of the resource data element + contained in the DataNode. + + The Node must be a data node. + The Node must have the resource data type, i.e. have the + EAmlNodeDataTypeResourceData data type. + + @param [in] DataNode Pointer to a data node containing a + resource data element. + @param [out] ResourceDataType Pointer holding the descriptor Id of + the resource data. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. +**/ +EFI_STATUS +EFIAPI +AmlGetResourceDataType ( + IN AML_DATA_NODE * DataNode, + OUT AML_RD_HEADER * ResourceDataType + ) +{ + if (!IS_AML_DATA_NODE (DataNode) || + (ResourceDataType =3D=3D NULL) || + (DataNode->DataType !=3D EAmlNodeDataTypeResourceData)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + *ResourceDataType =3D AmlRdGetDescId (DataNode->Buffer); + + return EFI_SUCCESS; +} + +/** Get the data buffer and size of the DataNode. + The Node must be a data node. + + BufferSize is always updated to the size of buffer of the DataNode. + + If: + - the content of BufferSize is >=3D to the DataNode's buffer size; + - Buffer is not NULL; + then copy the content of the DataNode's buffer in Buffer. + + @param [in] DataNode Pointer to a data node. + @param [out] Buffer Buffer to write the data to. + Optional, if NULL, only update BufferSiz= e. + @param [in, out] BufferSize Pointer holding: + - At entry, the size of the Buffer; + - At exit, the size of the DataNode's + buffer size. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. +**/ +EFI_STATUS +EFIAPI +AmlGetDataNodeBuffer ( + IN AML_DATA_NODE * DataNode, + OUT UINT8 * Buffer, OPTIONAL + IN OUT UINT32 * BufferSize + ) +{ + if (!IS_AML_DATA_NODE (DataNode) || + (BufferSize =3D=3D NULL)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + if ((*BufferSize >=3D DataNode->Size) && + (Buffer !=3D NULL)) { + CopyMem (Buffer, DataNode->Buffer, DataNode->Size); + } + + *BufferSize =3D DataNode->Size; + + return EFI_SUCCESS; +} + +/** Update the ACPI DSDT/SSDT table header. + + The input SdtHeader information is copied to the tree RootNode. + The table Length field is automatically updated. + The checksum field is only updated when serializing the tree. + + @param [in] RootNode Pointer to a root node. + @param [in] SdtHeader Pointer to an ACPI DSDT/SSDT table header. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. +**/ +EFI_STATUS +EFIAPI +AmlUpdateRootNode ( + IN AML_ROOT_NODE * RootNode, + IN CONST EFI_ACPI_DESCRIPTION_HEADER * SdtHeader + ) +{ + EFI_STATUS Status; + UINT32 Length; + + if (!IS_AML_ROOT_NODE (RootNode) || + (SdtHeader =3D=3D NULL) || + ((SdtHeader->Signature !=3D + EFI_ACPI_6_3_SECONDARY_SYSTEM_DESCRIPTION_TABLE_SIGNATURE) && + (SdtHeader->Signature !=3D + EFI_ACPI_6_3_DIFFERENTIATED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE))) = { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + CopyMem ( + RootNode->SdtHeader, + SdtHeader, + sizeof (EFI_ACPI_DESCRIPTION_HEADER) + ); + + // Update the Length field. + Status =3D AmlComputeSize ((AML_NODE_HEADER*)RootNode, &Length); + if (EFI_ERROR (Status)) { + ASSERT (0); + return Status; + } + + RootNode->SdtHeader->Length =3D Length + + (UINT32)sizeof (EFI_ACPI_DESCRIPTION_HEA= DER); + + return Status; +} + +/** Update an object node representing an integer with a new value. + + The object node must have one of the following OpCodes: + - AML_BYTE_PREFIX + - AML_WORD_PREFIX + - AML_DWORD_PREFIX + - AML_QWORD_PREFIX + - AML_ZERO_OP + - AML_ONE_OP + + The following OpCode is not supported: + - AML_ONES_OP + + @param [in] IntegerOpNode Pointer an object node containing an intege= r. + Must not be an object node with an AML_ONES= _OP + OpCode. + @param [in] NewInteger New integer value to set. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. +**/ +EFI_STATUS +EFIAPI +AmlUpdateInteger ( + IN AML_OBJECT_NODE * IntegerOpNode, + IN UINT64 NewInteger + ) +{ + EFI_STATUS Status; + + INT8 ValueWidthDiff; + + if (!IS_AML_OBJECT_NODE (IntegerOpNode) || + (!IsIntegerNode (IntegerOpNode) && + !IsSpecialIntegerNode (IntegerOpNode)) || + AmlNodeCompareOpCode (IntegerOpNode, AML_ONES_OP, 0)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + Status =3D AmlNodeSetIntegerValue (IntegerOpNode, NewInteger, &ValueWidt= hDiff); + if (EFI_ERROR (Status)) { + ASSERT (0); + return Status; + } + + // If the new size is different from the old size, propagate the new siz= e. + if (ValueWidthDiff !=3D 0) { + // Propagate the information. + Status =3D AmlPropagateInformation ( + (AML_NODE_HEADER*)IntegerOpNode, + (ValueWidthDiff > 0) ? TRUE : FALSE, + ABS (ValueWidthDiff), + 0 + ); + if (EFI_ERROR (Status)) { + ASSERT (0); + } + } + + return Status; +} + +/** Update the buffer of a data node. + + Note: The data type of the buffer's content must match the data type of = the + DataNode. This is a hard restriction to prevent undesired behaviou= r. + + @param [in] DataNode Pointer to a data node. + @param [in] DataType Data type of the Buffer's content. + @param [in] Buffer Buffer containing the new data. The content of + the Buffer is copied. + @param [in] Size Size of the Buffer. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval EFI_UNSUPPORTED Operation not supporter. +**/ +EFI_STATUS +EFIAPI +AmlUpdateDataNode ( + IN AML_DATA_NODE * DataNode, + IN EAML_NODE_DATA_TYPE DataType, + IN UINT8 * Buffer, + IN UINT32 Size + ) +{ + EFI_STATUS Status; + + UINT32 ExpectedSize; + AML_OBJECT_NODE * ParentNode; + EAML_NODE_DATA_TYPE ExpectedArgType; + EAML_PARSE_INDEX Index; + + if (!IS_AML_DATA_NODE (DataNode) || + (DataType > EAmlNodeDataTypeMax) || + (Buffer =3D=3D NULL) || + (Size =3D=3D 0)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + ParentNode =3D (AML_OBJECT_NODE*)AmlGetParent ((AML_NODE_HEADER*)DataNod= e); + if (!IS_AML_OBJECT_NODE (ParentNode)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + // The NewNode and OldNode must have the same type. + // We do not allow to change the argument type of a data node. + // If required, the initial ASL template should be modified + // accordingly. + // It is however possible to interchange a raw buffer and a + // resource data element, since raw data can be misinterpreted + // as a resource data element. + ExpectedArgType =3D DataNode->DataType; + if ((ExpectedArgType !=3D DataType) && + (((ExpectedArgType !=3D EAmlNodeDataTypeRaw) && + (ExpectedArgType !=3D EAmlNodeDataTypeResourceData)) || + ((DataType !=3D EAmlNodeDataTypeRaw) && + (DataType !=3D EAmlNodeDataTypeResourceData)))) { + ASSERT (0); + return EFI_UNSUPPORTED; + } + + // Perform some compatibility checks. + switch (DataType) { + case EAmlNodeDataTypeNameString: + { + // Check the name contained in the Buffer is an AML name + // with the right size. + Status =3D AmlGetNameStringSize ((CONST CHAR8*)Buffer, &ExpectedSize= ); + if (EFI_ERROR (Status) || + (Size !=3D ExpectedSize)) { + ASSERT (0); + return Status; + } + break; + } + case EAmlNodeDataTypeString: + { + ExpectedSize =3D 0; + while (ExpectedSize < Size) { + // Cf ACPI 6.3 specification 20.2.3 Data Objects Encoding. + // AsciiCharList :=3D Nothing | + // AsciiChar :=3D 0x01 - 0x7F + // NullChar :=3D 0x00 + if (Buffer[ExpectedSize] > 0x7F) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + ExpectedSize++; + } + + if (ExpectedSize !=3D Size) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + break; + } + case EAmlNodeDataTypeUInt: + { + if (AmlIsNodeFixedArgument ((CONST AML_NODE_HEADER*)DataNode, &Index= )) { + if ((ParentNode->AmlByteEncoding =3D=3D NULL) || + (ParentNode->AmlByteEncoding->Format =3D=3D NULL)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + // It is not possible to change the size of a fixed length UintX. + // E.g. for PackageOp the first fixed argument is of type EAmlUInt= 8 + // and represents the count of elements. This type cannot be chang= ed. + if ((ParentNode->AmlByteEncoding->Format[Index] !=3D EAmlObject) &= & + (DataNode->Size !=3D Size)) { + ASSERT (0); + return EFI_UNSUPPORTED; + } + } + break; + } + case EAmlNodeDataTypeRaw: + { + // Check if the parent node has the byte list flag set. + if (!AmlNodeHasAttribute (ParentNode, AML_HAS_BYTE_LIST)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + break; + } + case EAmlNodeDataTypeResourceData: + { + // The resource data can be either small or large resource data. + // Small resource data must be at least 1 byte. + // Large resource data must be at least as long as the header + // of a large resource data. + if (AML_RD_IS_LARGE (Buffer) && + (Size < sizeof (ACPI_LARGE_RESOURCE_HEADER))) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + // Check if the parent node has the byte list flag set. + if (!AmlNodeHasAttribute (ParentNode, AML_HAS_BYTE_LIST)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + // Check the size of the buffer is equal to the resource data size + // encoded in the input buffer. + ExpectedSize =3D AmlRdGetSize (Buffer); + if (ExpectedSize !=3D Size) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + break; + } + case EAmlNodeDataTypeFieldPkgLen: + { + // Check the parent is a FieldNamed field element. + if (!AmlNodeCompareOpCode (ParentNode, AML_FIELD_NAMED_OP, 0)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + break; + } + // None and reserved types. + default: + { + ASSERT (0); + return EFI_INVALID_PARAMETER; + break; + } + } // switch + + // If the new size is different from the old size, propagate the new siz= e. + if (DataNode->Size !=3D Size) { + // Propagate the information. + Status =3D AmlPropagateInformation ( + DataNode->NodeHeader.Parent, + (Size > DataNode->Size) ? TRUE : FALSE, + (Size > DataNode->Size) ? + (Size - DataNode->Size) : + (DataNode->Size - Size), + 0 + ); + if (EFI_ERROR (Status)) { + ASSERT (0); + return Status; + } + + // Free the old DataNode buffer and allocate a new buffer to store the + // new data. + FreePool (DataNode->Buffer); + DataNode->Buffer =3D AllocateZeroPool (Size); + if (DataNode->Buffer =3D=3D NULL) { + ASSERT (0); + return EFI_OUT_OF_RESOURCES; + } + DataNode->Size =3D Size; + } + + CopyMem (DataNode->Buffer, Buffer, Size); + + return EFI_SUCCESS; +} --=20 'Guid(CE165669-3EF3-493F-B85D-6190EE5B9759)'