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 13/30] DynamicTablesPkg: AML serialise interface
Date: Wed, 12 Aug 2020 16:22:19 +0100	[thread overview]
Message-ID: <20200812152236.31164-14-sami.mujawar@arm.com> (raw)
In-Reply-To: <20200812152236.31164-1-sami.mujawar@arm.com>

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

AML Fixup and AML Codegen facilitate dynamic generation
of Definition Block tables. The AML byte stream that is
generated is represented in an AML tree. Once the AML
table generation is completed, the AML tree needs to be
serialised for installing as an ACPI table.

The AML serialise interface implements the functionality
to iterate the nodes in the AML tree, collating the AML
bytecode, computing the checksum and writing the AML byte
stream to a buffer that represents the Definition Block
table.

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

diff --git a/DynamicTablesPkg/Library/Common/AmlLib/Serialize/AmlSerialize.c b/DynamicTablesPkg/Library/Common/AmlLib/Serialize/AmlSerialize.c
new file mode 100644
index 0000000000000000000000000000000000000000..2a47c229d7480128ed0f782efa009c7a86f4a492
--- /dev/null
+++ b/DynamicTablesPkg/Library/Common/AmlLib/Serialize/AmlSerialize.c
@@ -0,0 +1,324 @@
+/** @file
+  AML Serialize.
+
+  Copyright (c) 2019 - 2020, Arm Limited. All rights reserved.<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+**/
+
+#include <AmlNodeDefines.h>
+
+#include <AmlCoreInterface.h>
+#include <Stream/AmlStream.h>
+#include <Tree/AmlNode.h>
+#include <Tree/AmlTree.h>
+#include <Utils/AmlUtility.h>
+
+/** Callback function to copy the AML bytecodes contained in a node
+    to the Stream stored in the Context.
+    The SDT header data contained in the root node is not serialized
+    by this function.
+
+  @param  [in]      Node      Pointer to the node to copy the AML bytecodes
+                              from.
+  @param  [in, out] Context   Contains a forward Stream to write to.
+                              (AML_STREAM*)Context.
+  @param  [in, out] Status    At entry, contains the status returned by the
+                              last call to this exact function during the
+                              enumeration.
+                              As exit, contains the returned status of the
+                              call to this function.
+                              Optional, can be NULL.
+
+  @retval TRUE if the enumeration can continue or has finished without
+          interruption.
+  @retval FALSE if the enumeration needs to stopped or has stopped.
+**/
+STATIC
+BOOLEAN
+EFIAPI
+AmlSerializeNodeCallback (
+  IN       AML_NODE_HEADER   * Node,
+  IN  OUT  VOID              * Context,    OPTIONAL
+  IN  OUT  EFI_STATUS        * Status      OPTIONAL
+  )
+{
+  EFI_STATUS                Status1;
+
+  CONST AML_DATA_NODE     * DataNode;
+  CONST AML_OBJECT_NODE   * ObjectNode;
+  AML_STREAM              * FStream;
+
+  // Bytes needed to store OpCode[1] + SubOpcode[1] + MaxPkgLen[4] = 6 bytes.
+  UINT8                     ObjectNodeInfoArray[6];
+  UINT32                    Index;
+  BOOLEAN                   ContinueEnum;
+
+  CONST AML_OBJECT_NODE   * ParentNode;
+  EAML_PARSE_INDEX          IndexPtr;
+
+  if (!IS_AML_NODE_VALID (Node)   ||
+      (Context == NULL)) {
+    ASSERT (0);
+    Status1 = EFI_INVALID_PARAMETER;
+    ContinueEnum = FALSE;
+    goto error_handler;
+  }
+
+  // Ignore the second fixed argument of method invocation nodes
+  // as the information stored there (the argument count) is not in the
+  // ACPI specification.
+  ParentNode = (CONST AML_OBJECT_NODE*)AmlGetParent ((AML_NODE_HEADER*)Node);
+  if (IS_AML_OBJECT_NODE (ParentNode)                             &&
+      AmlNodeCompareOpCode (ParentNode, AML_METHOD_INVOC_OP, 0)   &&
+      AmlIsNodeFixedArgument (Node, &IndexPtr)) {
+    if (IndexPtr == EAmlParseIndexTerm1) {
+      if (Status != NULL) {
+        *Status = EFI_SUCCESS;
+      }
+      return TRUE;
+    }
+  }
+
+  Status1 = EFI_SUCCESS;
+  ContinueEnum = TRUE;
+  FStream = (AML_STREAM*)Context;
+
+  if (IS_AML_DATA_NODE (Node)) {
+    // Copy the content of the Buffer for a DataNode.
+    DataNode = (AML_DATA_NODE*)Node;
+    Status1 = AmlStreamWrite (
+                FStream,
+                DataNode->Buffer,
+                DataNode->Size
+                );
+    if (EFI_ERROR (Status1)) {
+      ASSERT (0);
+      ContinueEnum = FALSE;
+      goto error_handler;
+    }
+
+  } else if (IS_AML_OBJECT_NODE (Node)  &&
+             !AmlNodeHasAttribute (
+                (CONST AML_OBJECT_NODE*)Node,
+                AML_IS_PSEUDO_OPCODE)) {
+    // Ignore pseudo-opcodes as they are not part of the
+    // ACPI specification.
+
+    ObjectNode = (AML_OBJECT_NODE*)Node;
+
+    Index = 0;
+    // Copy the opcode(s).
+    ObjectNodeInfoArray[Index++] = ObjectNode->AmlByteEncoding->OpCode;
+    if (ObjectNode->AmlByteEncoding->OpCode == AML_EXT_OP) {
+      ObjectNodeInfoArray[Index++] = ObjectNode->AmlByteEncoding->SubOpCode;
+    }
+
+    // Copy the PkgLen.
+    if (AmlNodeHasAttribute (ObjectNode, AML_HAS_PKG_LENGTH)) {
+      Index += AmlSetPkgLength (
+                 ObjectNode->PkgLen,
+                 &ObjectNodeInfoArray[Index]
+                 );
+    }
+
+    Status1 = AmlStreamWrite (
+                FStream,
+                ObjectNodeInfoArray,
+                Index
+                );
+    if (EFI_ERROR (Status1)) {
+      ASSERT (0);
+      ContinueEnum = FALSE;
+      goto error_handler;
+    }
+  } // IS_AML_OBJECT_NODE (Node)
+
+error_handler:
+  if (Status != NULL) {
+    *Status = Status1;
+  }
+  return ContinueEnum;
+}
+
+/** Serialize a tree to create an ACPI DSDT/SSDT table.
+
+  If:
+   - the content of BufferSize is >= to the size needed to serialize the
+     definition block;
+   - Buffer is not NULL;
+  first serialize the ACPI DSDT/SSDT header from the root node,
+  then serialize the AML blob from the rest of the tree.
+
+  The content of BufferSize is always updated to the size needed to
+  serialize the definition block.
+
+  @param  [in]      RootNode    Pointer to a root node.
+  @param  [in]      Buffer      Buffer to write the DSDT/SSDT table to.
+                                If Buffer is NULL, the size needed to
+                                serialize the DSDT/SSDT table is returned
+                                in BufferSize.
+  @param  [in, out] BufferSize  Pointer holding the size of the Buffer.
+                                Its content is always updated to the size
+                                needed to serialize the DSDT/SSDT table.
+
+  @retval EFI_SUCCESS             The function completed successfully.
+  @retval EFI_INVALID_PARAMETER   Invalid parameter.
+  @retval EFI_BUFFER_TOO_SMALL    No space left in the buffer.
+**/
+EFI_STATUS
+EFIAPI
+AmlSerializeTree (
+  IN      AML_ROOT_NODE   * RootNode,
+  IN      UINT8           * Buffer,     OPTIONAL
+  IN  OUT UINT32          * BufferSize
+  )
+{
+  EFI_STATUS    Status;
+  AML_STREAM    FStream;
+  UINT32        TableSize;
+
+  if (!IS_AML_ROOT_NODE (RootNode) ||
+      (BufferSize == NULL)) {
+    ASSERT (0);
+    return EFI_INVALID_PARAMETER;
+  }
+
+  // Compute the total size of the AML blob.
+  Status = AmlComputeSize (
+              (CONST AML_NODE_HEADER*)RootNode,
+              &TableSize
+              );
+  if (EFI_ERROR (Status)) {
+    ASSERT (0);
+    return Status;
+  }
+
+  // Add the size of the ACPI header.
+  TableSize += (UINT32)sizeof (EFI_ACPI_DESCRIPTION_HEADER);
+
+  // Check the size against the SDT header.
+  // The Length field in the SDT Header is updated if the tree has
+  // been modified.
+  if (TableSize != RootNode->SdtHeader->Length) {
+    ASSERT (0);
+    return EFI_INVALID_PARAMETER;
+  }
+
+  // Buffer is not big enough, or NULL.
+  if ((TableSize < *BufferSize) || (Buffer == NULL)) {
+    *BufferSize = TableSize;
+    return EFI_SUCCESS;
+  }
+
+  // Initialize the stream to the TableSize that is needed.
+  Status = AmlStreamInit (
+             &FStream,
+             Buffer,
+             TableSize,
+             EAmlStreamDirectionForward
+             );
+  if (EFI_ERROR (Status)) {
+    ASSERT (0);
+    return Status;
+  }
+
+  // Serialize the header.
+  Status = AmlStreamWrite (
+             &FStream,
+             (UINT8*)RootNode->SdtHeader,
+             sizeof (EFI_ACPI_DESCRIPTION_HEADER)
+             );
+  if (EFI_ERROR (Status)) {
+    ASSERT (0);
+    return Status;
+  }
+
+  Status = EFI_SUCCESS;
+  AmlEnumTree (
+    (AML_NODE_HEADER*)RootNode,
+    AmlSerializeNodeCallback,
+    (VOID*)&FStream,
+    &Status
+    );
+  if (EFI_ERROR (Status)) {
+    ASSERT (0);
+    return Status;
+  }
+
+  // Update the checksum.
+  return AcpiPlatformChecksum ((EFI_ACPI_DESCRIPTION_HEADER*)Buffer);
+}
+
+/** Serialize an AML definition block.
+
+  This functions allocates memory with the "AllocateZeroPool ()"
+  function. This memory is used to serialize the AML tree and is
+  returned in the Table.
+
+  @param [in]  RootNode         Root node of the tree.
+  @param [out] Table            On return, hold the serialized
+                                definition block.
+
+  @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
+AmlSerializeDefinitionBlock (
+  IN  AML_ROOT_NODE                   * RootNode,
+  OUT EFI_ACPI_DESCRIPTION_HEADER    ** Table
+  )
+{
+  EFI_STATUS    Status;
+  UINT8       * TableBuffer;
+  UINT32        TableSize;
+
+  if (!IS_AML_ROOT_NODE (RootNode) ||
+      (Table == NULL)) {
+    ASSERT (0);
+    return EFI_INVALID_PARAMETER;
+  }
+
+  *Table = NULL;
+  TableBuffer = NULL;
+  TableSize = 0;
+
+  // Get the size of the SSDT table.
+  Status = AmlSerializeTree (
+             RootNode,
+             TableBuffer,
+             &TableSize
+             );
+  if (EFI_ERROR (Status)) {
+    ASSERT (0);
+    return Status;
+  }
+
+  TableBuffer = (UINT8*)AllocateZeroPool (TableSize);
+  if (TableBuffer == NULL) {
+    DEBUG ((
+      DEBUG_ERROR,
+      "ERROR: Failed to allocate memory for Table Buffer."
+      ));
+    ASSERT (0);
+    return EFI_OUT_OF_RESOURCES;
+  }
+
+  // Serialize the tree to a SSDT table.
+  Status = AmlSerializeTree (
+             RootNode,
+             TableBuffer,
+             &TableSize
+             );
+  if (EFI_ERROR (Status)) {
+    FreePool (TableBuffer);
+    ASSERT (0);
+  } else {
+    // Save the allocated Table buffer in the table list
+    *Table = (EFI_ACPI_DESCRIPTION_HEADER*)TableBuffer;
+  }
+
+  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 ` [PATCH v1 09/30] DynamicTablesPkg: AML tree/node cloning Sami Mujawar
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 ` Sami Mujawar [this message]
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-14-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