public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Sami Mujawar" <sami.mujawar@arm.com>
To: <devel@edk2.groups.io>
Cc: Sami Mujawar <sami.mujawar@arm.com>, <Alexei.Fedorov@arm.com>,
	<pierre.gondois@arm.com>, <ard.biesheuvel@arm.com>,
	<Matteo.Carlini@arm.com>, <Ben.Adderson@arm.com>, <nd@arm.com>
Subject: [PATCH v1 09/30] DynamicTablesPkg: AML tree/node cloning
Date: Wed, 12 Aug 2020 16:22:15 +0100	[thread overview]
Message-ID: <20200812152236.31164-10-sami.mujawar@arm.com> (raw)
In-Reply-To: <20200812152236.31164-1-sami.mujawar@arm.com>

From: Pierre Gondois <pierre.gondois@arm.com>

It is often desirable to clone an AML branch/tree
or an AML node. An example of could be to clone
an AML template before fixup so that the original
AML template remains unmodified. Another example
would be replicating a device branch in the AML
tree and fixing up the device information.

To facilitate such scenarios the AmlLib library
provides functions that can be used to clone an
AML branch/tree or an AML node.

Signed-off-by: Pierre Gondois <pierre.gondois@arm.com>
Signed-off-by: Sami Mujawar <sami.mujawar@arm.com>
---
 DynamicTablesPkg/Library/Common/AmlLib/Tree/AmlClone.c | 205 ++++++++++++++++++++
 1 file changed, 205 insertions(+)

diff --git a/DynamicTablesPkg/Library/Common/AmlLib/Tree/AmlClone.c b/DynamicTablesPkg/Library/Common/AmlLib/Tree/AmlClone.c
new file mode 100644
index 0000000000000000000000000000000000000000..e09372b039f1f232de500effae7d403d56c989cd
--- /dev/null
+++ b/DynamicTablesPkg/Library/Common/AmlLib/Tree/AmlClone.c
@@ -0,0 +1,205 @@
+/** @file
+  AML Clone.
+
+  Copyright (c) 2019 - 2020, Arm Limited. All rights reserved.<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+**/
+
+#include <AmlNodeDefines.h>
+
+#include <AmlCoreInterface.h>
+#include <Tree/AmlNode.h>
+#include <Tree/AmlTree.h>
+
+/** Clone a node.
+
+  This function does not clone the children nodes.
+  The cloned node returned is not attached to any tree.
+
+  @param  [in]  Node        Pointer to a node.
+  @param  [out] ClonedNode  Pointer holding the cloned 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
+AmlCloneNode (
+  IN  AML_NODE_HEADER   * Node,
+  OUT AML_NODE_HEADER  ** ClonedNode
+  )
+{
+  EFI_STATUS              Status;
+
+  AML_OBJECT_NODE       * ObjectNode;
+  AML_DATA_NODE         * DataNode;
+  AML_ROOT_NODE         * RootNode;
+
+  if (!IS_AML_NODE_VALID (Node) ||
+      (ClonedNode == NULL)) {
+    ASSERT (0);
+    return EFI_INVALID_PARAMETER;
+  }
+
+  *ClonedNode = NULL;
+
+  if (IS_AML_DATA_NODE (Node)) {
+    DataNode = (AML_DATA_NODE*)Node;
+    Status = AmlCreateDataNode (
+                DataNode->DataType,
+                DataNode->Buffer,
+                DataNode->Size,
+                (AML_DATA_NODE**)ClonedNode
+                );
+    if (EFI_ERROR (Status)) {
+      ASSERT (0);
+    }
+  } else if (IS_AML_OBJECT_NODE (Node)) {
+    ObjectNode = (AML_OBJECT_NODE*)Node;
+
+    Status = AmlCreateObjectNode (
+                ObjectNode->AmlByteEncoding,
+                ObjectNode->PkgLen,
+                (AML_OBJECT_NODE**)ClonedNode
+                );
+    if (EFI_ERROR (Status)) {
+      ASSERT (0);
+    }
+  } else if (IS_AML_ROOT_NODE (Node)) {
+    RootNode = (AML_ROOT_NODE*)Node;
+
+    Status = AmlCreateRootNode (
+               RootNode->SdtHeader,
+               (AML_ROOT_NODE**)ClonedNode
+               );
+    if (EFI_ERROR (Status)) {
+      ASSERT (0);
+    }
+  } else {
+    ASSERT (0);
+    return EFI_INVALID_PARAMETER;
+  }
+
+  return Status;
+}
+
+/** Clone a node and its children (clone a tree branch).
+
+  The cloned branch returned is not attached to any tree.
+
+  @param  [in]  Node        Pointer to a node.
+                            Node is the head of the branch to clone.
+  @param  [out] ClonedNode  Pointer holding the head of the created cloned
+                            branch.
+
+  @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
+AmlCloneTree (
+  IN  AML_NODE_HEADER   * Node,
+  OUT AML_NODE_HEADER  ** ClonedNode
+  )
+{
+  EFI_STATUS              Status;
+
+  AML_NODE_HEADER       * HeadNode;
+  AML_NODE_HEADER       * ClonedChildNode;
+  AML_NODE_HEADER       * FixedArgNode;
+
+  EAML_PARSE_INDEX        Index;
+  EAML_PARSE_INDEX        MaxIndex;
+
+  LIST_ENTRY            * StartLink;
+  LIST_ENTRY            * CurrentLink;
+
+  if (!IS_AML_NODE_VALID (Node) ||
+      (ClonedNode == NULL)) {
+    ASSERT (0);
+    return EFI_INVALID_PARAMETER;
+  }
+
+  Status = AmlCloneNode (Node, &HeadNode);
+  if (EFI_ERROR (Status)) {
+    ASSERT (0);
+    return Status;
+  }
+
+  // Clone the fixed arguments and bind them to their parent.
+  MaxIndex = (EAML_PARSE_INDEX)AmlGetFixedArgumentCount (
+                                 (AML_OBJECT_NODE*)Node
+                                 );
+  for (Index = EAmlParseIndexTerm0; Index < MaxIndex; Index++) {
+    FixedArgNode = AmlGetFixedArgument ((AML_OBJECT_NODE*)Node, Index);
+    if (FixedArgNode == NULL) {
+      Status = EFI_INVALID_PARAMETER;
+      ASSERT (0);
+      goto error_handler;
+    }
+
+    // Clone child.
+    Status = AmlCloneTree (
+               FixedArgNode,
+               &ClonedChildNode
+               );
+    if (EFI_ERROR (Status)) {
+      ASSERT (0);
+      goto error_handler;
+    }
+
+    // Bind child.
+    Status = AmlSetFixedArgument (
+               (AML_OBJECT_NODE*)HeadNode,
+               Index,
+               ClonedChildNode
+               );
+    if (EFI_ERROR (Status)) {
+      AmlDeleteTree (ClonedChildNode);
+      ASSERT (0);
+      goto error_handler;
+    }
+  } // for
+
+  // Clone the variable arguments and bind them to their parent.
+  StartLink = AmlNodeGetVariableArgList (Node);
+  if (StartLink != NULL) {
+    CurrentLink = StartLink->ForwardLink;
+    while (CurrentLink != StartLink) {
+      // Clone child.
+      Status = AmlCloneTree ((AML_NODE_HEADER*)CurrentLink, &ClonedChildNode);
+      if (EFI_ERROR (Status)) {
+        ASSERT (0);
+        goto error_handler;
+      }
+
+      // Bind child.
+      Status = AmlVarListAddTailInternal (
+                 HeadNode,
+                 ClonedChildNode
+                 );
+      if (EFI_ERROR (Status)) {
+        AmlDeleteTree (ClonedChildNode);
+        ASSERT (0);
+        goto error_handler;
+      }
+
+      CurrentLink = CurrentLink->ForwardLink;
+    } // while
+  }
+
+  *ClonedNode = HeadNode;
+  return Status;
+
+error_handler:
+  *ClonedNode = NULL;
+
+  if (HeadNode != NULL) {
+    AmlDeleteTree (HeadNode);
+  }
+
+  return Status;
+}
-- 
'Guid(CE165669-3EF3-493F-B85D-6190EE5B9759)'


  parent reply	other threads:[~2020-08-12 15:23 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-12 15:22 [PATCH v1 00/30] Add Dynamic AML generation support Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 01/30] DynamicTablesPkg: Introduction to Dynamic AML Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 02/30] DynamicTablesPkg: AmlLib definitions Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 03/30] DynamicTablesPkg: AML grammar definition Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 04/30] DynamicTablesPkg: AML node definitions Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 05/30] DynamicTablesPkg: AML tree interface Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 06/30] DynamicTablesPkg: AML tree enumerator Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 07/30] DynamicTablesPkg: AML tree traversal Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 08/30] DynamicTablesPkg: AML tree iterator Sami Mujawar
2020-08-12 15:22 ` Sami Mujawar [this message]
2020-08-12 15:22 ` [PATCH v1 10/30] DynamicTablesPkg: AML utility interfaces Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 11/30] DynamicTablesPkg: AML and ASL string helper Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 12/30] DynamicTablesPkg: AML stream interface Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 13/30] DynamicTablesPkg: AML serialise interface Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 14/30] DynamicTablesPkg: AML debug logging Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 15/30] DynamicTablesPkg: AML ACPI Namespace interface Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 16/30] DynamicTablesPkg: AML Parser Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 17/30] DynamicTablesPkg: AML resource data helper Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 18/30] DynamicTablesPkg: AML resource data parser Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 19/30] DynamicTablesPkg: AML Method parser Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 20/30] DynamicTablesPkg: AML Field list parser Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 21/30] DynamicTablesPkg: AML Codegen Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 22/30] DynamicTablesPkg: AML Resource Data Codegen Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 23/30] DynamicTablesPkg: AML Core interface Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 24/30] DynamicTablesPkg: AmlLib APIs Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 25/30] DynamicTablesPkg: Dynamic AML: Add AmlLib library Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 26/30] DynamicTablesPkg: Add AsciiFromHex helper function Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 27/30] DynamicTablesPkg: SSDT Serial Port Fixup library Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 28/30] DynamicTablesPkg: SSDT Serial Port generator Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 29/30] DynamicTablesPkg: Add SSDT Serial port for SPCR Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 30/30] DynamicTablesPkg: Add SSDT Serial port for DBG2 Sami Mujawar
2020-08-13 15:16 ` [edk2-devel] [PATCH v1 00/30] Add Dynamic AML generation support Alexei Fedorov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200812152236.31164-10-sami.mujawar@arm.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox